parser.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717
  1. /**
  2. * \file parser.c
  3. *
  4. * \brief GIS Library - Argument parsing functions.
  5. *
  6. * Parses the command line provided through argc and argv. Example:
  7. * Assume the previous calls:
  8. *
  9. * opt1 = G_define_option() ;
  10. * opt1->key = "map",
  11. * opt1->type = TYPE_STRING,
  12. * opt1->required = YES,
  13. * opt1->checker = sub,
  14. * opt1->description= "Name of an existing raster map" ;
  15. *
  16. * opt2 = G_define_option() ;
  17. * opt2->key = "color",
  18. * opt2->type = TYPE_STRING,
  19. * opt2->required = NO,
  20. * opt2->answer = "white",
  21. * opt2->options = "red,orange,blue,white,black",
  22. * opt2->description= "Color used to display the map" ;
  23. *
  24. * opt3 = G_define_option() ;
  25. * opt3->key = "number",
  26. * opt3->type = TYPE_DOUBLE,
  27. * opt3->required = NO,
  28. * opt3->answer = "12345.67",
  29. * opt3->options = "0-99999",
  30. * opt3->description= "Number to test parser" ;
  31. *
  32. * G_parser() will respond to the following command lines as described:
  33. *
  34. * command (No command line arguments)
  35. * Parser enters interactive mode.
  36. *
  37. * command map=map.name
  38. * Parser will accept this line. Map will be set to "map.name", the
  39. * 'a' and 'b' flags will remain off and the num option will be set
  40. * to the default of 5.
  41. *
  42. * command -ab map=map.name num=9
  43. * command -a -b map=map.name num=9
  44. * command -ab map.name num=9
  45. * command map.name num=9 -ab
  46. * command num=9 -a map=map.name -b
  47. * These are all treated as acceptable and identical. Both flags are
  48. * set to on, the map option is "map.name" and the num option is "9".
  49. * Note that the "map=" may be omitted from the command line if it
  50. * is part of the first option (flags do not count).
  51. *
  52. * command num=12
  53. * This command line is in error in two ways. The user will be told
  54. * that the "map" option is required and also that the number 12 is
  55. * out of range. The acceptable range (or list) will be printed.
  56. *
  57. * (C) 2001-2008 by the GRASS Development Team
  58. *
  59. * This program is free software under the GNU General Public License
  60. * (>=v2). Read the file COPYING that comes with GRASS for details.
  61. *
  62. * \author Radim Blazek
  63. *
  64. * \date 2003-2008
  65. *
  66. */
  67. #include <grass/config.h>
  68. #if defined(HAVE_LANGINFO_H)
  69. #include <langinfo.h>
  70. #endif
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #include <ctype.h>
  75. #include <unistd.h>
  76. #include <stdarg.h>
  77. #include <sys/types.h>
  78. #include <grass/gis.h>
  79. #include <grass/glocale.h>
  80. #include <grass/spawn.h>
  81. #define BAD_SYNTAX 1
  82. #define OUT_OF_RANGE 2
  83. #define MISSING_VALUE 3
  84. #define AMBIGUOUS 4
  85. #define REPLACED 5
  86. #define KEYLENGTH 64
  87. static int interactive_ok = 1;
  88. static int n_opts;
  89. static int n_flags;
  90. static int overwrite;
  91. static int quiet;
  92. static int has_required;
  93. static struct Flag first_flag; /* First flag in a linked list */
  94. static struct Flag *current_flag; /* Pointer for traversing list */
  95. static struct Option first_option;
  96. static struct Option *current_option;
  97. static struct GModule module_info; /* general information on the corresponding module */
  98. static const char *pgm_name = NULL;
  99. struct Item
  100. {
  101. struct Option *option;
  102. struct Flag *flag;
  103. struct Item *next_item;
  104. };
  105. static struct Item first_item;
  106. static struct Item *current_item;
  107. static int n_items = 0;
  108. static int show_options(int, const char *);
  109. static int show(const char *, int);
  110. static int set_flag(int);
  111. static int contains(const char *, int);
  112. static int is_option(const char *);
  113. static int set_option(char *);
  114. static int check_opts();
  115. static int check_an_opt(const char *, int, const char *, const char **, char **);
  116. static int check_int(const char *, const char **);
  117. static int check_double(const char *, const char **);
  118. static int check_string(const char *, const char **, int *);
  119. static int check_required(void);
  120. static int split_opts(void);
  121. static int check_multiple_opts(void);
  122. static int check_overwrite(void);
  123. static int split_gisprompt(const char *, char *, char *, char *);
  124. static void G_gui(void);
  125. static void G_tcltk(void);
  126. static void G_usage_xml(void);
  127. static void G_usage_html(void);
  128. static void G_script(void);
  129. /**
  130. * \brief Disables the ability of the parser to operate interactively.
  131. *
  132. * When a user calls a command with no arguments on the command line,
  133. * the parser will enter its own standardized interactive session in
  134. * which all flags and options are presented to the user for input. A
  135. * call to <i>G_disable_interactive()</i> disables the parser's
  136. * interactive prompting.
  137. *
  138. * \return always returns 0
  139. */
  140. int G_disable_interactive(void)
  141. {
  142. interactive_ok = 0;
  143. return 0;
  144. }
  145. /**
  146. * \brief Initializes a Flag struct.
  147. *
  148. * Allocates memory for the Flag structure and returns a pointer to this
  149. * memory (of <i>type struct Flag *</i>).<br>
  150. *
  151. * Flags are always represented by single letters. A user "turns them on"
  152. * at the command line using a minus sign followed by the character
  153. * representing the flag.
  154. *
  155. * \return Flag * Pointer to a Flag struct
  156. */
  157. struct Flag *G_define_flag(void)
  158. {
  159. struct Flag *flag;
  160. struct Item *item;
  161. /* Allocate memory if not the first flag */
  162. if (n_flags) {
  163. flag = (struct Flag *)G_malloc(sizeof(struct Flag));
  164. current_flag->next_flag = flag;
  165. }
  166. else
  167. flag = &first_flag;
  168. /* Zero structure */
  169. G_zero((char *)flag, sizeof(struct Flag));
  170. current_flag = flag;
  171. n_flags++;
  172. if (n_items) {
  173. item = (struct Item *)G_malloc(sizeof(struct Item));
  174. current_item->next_item = item;
  175. }
  176. else
  177. item = &first_item;
  178. G_zero((char *)item, sizeof(struct Item));
  179. item->flag = flag;
  180. item->option = NULL;
  181. current_item = item;
  182. n_items++;
  183. return (flag);
  184. }
  185. /**
  186. * \brief Initializes an Option struct.
  187. *
  188. * Allocates memory for the Option structure and returns a pointer to
  189. * this memory (of <i>type struct Option *</i>).<br>
  190. *
  191. * Options are provided by user on command line using the standard
  192. * format: <i>key=value</i>. Options identified as REQUIRED must be
  193. * specified by user on command line. The option string can either
  194. * specify a range of values (e.g. "10-100") or a list of acceptable
  195. * values (e.g. "red,orange,yellow"). Unless the option string is NULL,
  196. * user provided input will be evaluated agaist this string.
  197. *
  198. * \return Option * Pointer to an Option struct
  199. */
  200. struct Option *G_define_option(void)
  201. {
  202. struct Option *opt;
  203. struct Item *item;
  204. /* Allocate memory if not the first option */
  205. if (n_opts) {
  206. opt = (struct Option *)G_malloc(sizeof(struct Option));
  207. current_option->next_opt = opt;
  208. }
  209. else
  210. opt = &first_option;
  211. /* Zero structure */
  212. G_zero((char *)opt, sizeof(struct Option));
  213. opt->required = NO;
  214. opt->multiple = NO;
  215. opt->answer = NULL;
  216. opt->answers = NULL;
  217. opt->def = NULL;
  218. opt->checker = NULL;
  219. opt->options = NULL;
  220. opt->key_desc = NULL;
  221. opt->gisprompt = NULL;
  222. opt->label = NULL;
  223. opt->opts = NULL;
  224. opt->description = NULL;
  225. opt->descriptions = NULL;
  226. opt->guisection = NULL;
  227. current_option = opt;
  228. n_opts++;
  229. if (n_items) {
  230. item = (struct Item *)G_malloc(sizeof(struct Item));
  231. current_item->next_item = item;
  232. }
  233. else
  234. item = &first_item;
  235. G_zero((char *)item, sizeof(struct Item));
  236. item->option = opt;
  237. item->flag = NULL;
  238. current_item = item;
  239. n_items++;
  240. return (opt);
  241. }
  242. /**
  243. * \brief Create standardised Option structure.
  244. *
  245. * This function will create a standardised Option structure
  246. * defined by parameter opt. A list of valid parameters can be found in gis.h.
  247. * It allocates memory for the Option structure and returns a pointer to
  248. * this memory (of <i>type struct Option *</i>).<br>
  249. *
  250. * If an invalid parameter was specified a empty Option structure will
  251. * be returned (not NULL).
  252. *
  253. * - general:
  254. * - G_OPT_DB_WHERE
  255. * - G_OPT_DB_COLUMN
  256. * - G_OPT_DB_COLUMNS
  257. * - G_OPT_DB_TABLE
  258. * - G_OPT_DB_DRIVER
  259. * - G_OPT_DB_DATABASE
  260. *
  261. * - imagery:
  262. * - G_OPT_I_GROUP
  263. * - G_OPT_I_SUBGROUP
  264. *
  265. * - raster:
  266. * - G_OPT_R_INPUT
  267. * - G_OPT_R_INPUTS
  268. * - G_OPT_R_OUTPUT
  269. * - G_OPT_R_MAP
  270. * - G_OPT_R_MAPS
  271. * - G_OPT_R_BASE
  272. * - G_OPT_R_COVER
  273. * - G_OPT_R_ELEV
  274. * - G_OPT_R_ELEVS
  275. *
  276. * - raster3d:
  277. * - G_OPT_R3_INPUT
  278. * - G_OPT_R3_INPUTS
  279. * - G_OPT_R3_OUTPUT
  280. * - G_OPT_R3_MAP
  281. * - G_OPT_R3_MAPS
  282. *
  283. * - vector:
  284. * - G_OPT_V_INPUT
  285. * - G_OPT_V_INPUTS
  286. * - G_OPT_V_OUTPUT
  287. * - G_OPT_V_MAP
  288. * - G_OPT_V_MAPS
  289. * - G_OPT_V_TYPE
  290. * - G_OPT_V_FIELD
  291. * - G_OPT_V_CAT
  292. * - G_OPT_V_CATS
  293. *
  294. * \param[in] opt Type of Option struct to create
  295. *
  296. * \return Option * Pointer to an Option struct
  297. */
  298. struct Option *G_define_standard_option(int opt)
  299. {
  300. struct Option *Opt;
  301. Opt = G_define_option();
  302. switch (opt) {
  303. case G_OPT_DB_WHERE:
  304. Opt->key = "where";
  305. Opt->type = TYPE_STRING;
  306. Opt->key_desc = "sql_query";
  307. Opt->required = NO;
  308. Opt->label = _("WHERE conditions of SQL statement without 'where' keyword");
  309. Opt->description = _("Example: income < 1000 and inhab >= 10000");
  310. break;
  311. case G_OPT_DB_TABLE:
  312. Opt->key = "table";
  313. Opt->type = TYPE_STRING;
  314. Opt->key_desc = "name";
  315. Opt->required = NO;
  316. Opt->multiple = NO;
  317. Opt->description = _("Table name");
  318. Opt->gisprompt = "old_dbtable,dbtable,dbtable";
  319. break;
  320. case G_OPT_DB_DRIVER:
  321. Opt->key = "driver";
  322. Opt->type = TYPE_STRING;
  323. Opt->key_desc = "name";
  324. Opt->required = NO;
  325. Opt->multiple = NO;
  326. Opt->description = _("Driver name");
  327. Opt->gisprompt = "old_dbdriver,dbdriver,dbdriver";
  328. break;
  329. case G_OPT_DB_DATABASE:
  330. Opt->key = "database";
  331. Opt->type = TYPE_STRING;
  332. Opt->key_desc = "name";
  333. Opt->required = NO;
  334. Opt->multiple = NO;
  335. Opt->description = _("Database name");
  336. Opt->gisprompt = "old_dbname,dbname,dbname";
  337. break;
  338. case G_OPT_DB_COLUMN:
  339. Opt->key = "column";
  340. Opt->type = TYPE_STRING;
  341. Opt->key_desc = "name";
  342. Opt->required = NO;
  343. Opt->multiple = NO;
  344. Opt->description = _("Name of attribute column");
  345. Opt->gisprompt = "old_dbcolumn,dbcolumn,dbcolumn";
  346. break;
  347. case G_OPT_DB_COLUMNS:
  348. Opt->key = "columns";
  349. Opt->type = TYPE_STRING;
  350. Opt->key_desc = "name";
  351. Opt->required = NO;
  352. Opt->multiple = YES;
  353. Opt->description = _("Name of attribute column(s)");
  354. Opt->gisprompt = "old_table,table,dbcolumn";
  355. break;
  356. /* imagery group */
  357. case G_OPT_I_GROUP:
  358. Opt->key = "group";
  359. Opt->type = TYPE_STRING;
  360. Opt->key_desc = "name";
  361. Opt->required = YES;
  362. Opt->gisprompt = "old,group,group";
  363. Opt->description = _("Name of input imagery group");
  364. break;
  365. case G_OPT_I_SUBGROUP:
  366. Opt->key = "subgroup";
  367. Opt->type = TYPE_STRING;
  368. Opt->key_desc = "name";
  369. Opt->required = YES;
  370. Opt->gisprompt = "old,subgroup,subgroup";
  371. Opt->description = _("Name of input imagery subgroup");
  372. break;
  373. /* raster maps */
  374. case G_OPT_R_INPUT:
  375. Opt->key = "input";
  376. Opt->type = TYPE_STRING;
  377. Opt->key_desc = "name";
  378. Opt->required = YES;
  379. Opt->gisprompt = "old,cell,raster";
  380. Opt->description = _("Name of input raster map");
  381. break;
  382. case G_OPT_R_INPUTS:
  383. Opt->key = "input";
  384. Opt->type = TYPE_STRING;
  385. Opt->key_desc = "name";
  386. Opt->required = YES;
  387. Opt->multiple = YES;
  388. Opt->gisprompt = "old,cell,raster";
  389. Opt->description = _("Name of input raster map(s)");
  390. break;
  391. case G_OPT_R_OUTPUT:
  392. Opt->key = "output";
  393. Opt->type = TYPE_STRING;
  394. Opt->key_desc = "name";
  395. Opt->required = YES;
  396. Opt->gisprompt = "new,cell,raster";
  397. Opt->description = _("Name for output raster map");
  398. break;
  399. case G_OPT_R_MAP:
  400. Opt->key = "map";
  401. Opt->type = TYPE_STRING;
  402. Opt->key_desc = "name";
  403. Opt->required = YES;
  404. Opt->gisprompt = "old,cell,raster";
  405. Opt->description = _("Name of input raster map");
  406. break;
  407. case G_OPT_R_MAPS:
  408. Opt->key = "map";
  409. Opt->type = TYPE_STRING;
  410. Opt->key_desc = "name";
  411. Opt->required = YES;
  412. Opt->multiple = YES;
  413. Opt->gisprompt = "old,cell,raster";
  414. Opt->description = _("Name of input raster map(s)");
  415. break;
  416. case G_OPT_R_BASE:
  417. Opt->key = "base";
  418. Opt->type = TYPE_STRING;
  419. Opt->key_desc = "name";
  420. Opt->required = YES;
  421. Opt->gisprompt = "old,cell,raster";
  422. Opt->description = _("Name of base raster map");
  423. break;
  424. case G_OPT_R_COVER:
  425. Opt->key = "cover";
  426. Opt->type = TYPE_STRING;
  427. Opt->key_desc = "name";
  428. Opt->required = YES;
  429. Opt->gisprompt = "old,cell,raster";
  430. Opt->description = _("Name of cover raster map");
  431. break;
  432. case G_OPT_R_ELEV:
  433. Opt->key = "elevation";
  434. Opt->type = TYPE_STRING;
  435. Opt->key_desc = "name";
  436. Opt->required = YES;
  437. Opt->gisprompt = "old,cell,raster";
  438. Opt->description = _("Name of elevation raster map");
  439. break;
  440. case G_OPT_R_ELEVS:
  441. Opt->key = "elevation";
  442. Opt->type = TYPE_STRING;
  443. Opt->key_desc = "name";
  444. Opt->required = YES;
  445. Opt->multiple = YES;
  446. Opt->gisprompt = "old,cell,raster";
  447. Opt->description = _("Name of elevation raster map(s)");
  448. break;
  449. /*g3d maps */
  450. case G_OPT_R3_INPUT:
  451. Opt->key = "input";
  452. Opt->type = TYPE_STRING;
  453. Opt->key_desc = "name";
  454. Opt->required = YES;
  455. Opt->gisprompt = "old,grid3,3d-raster";
  456. Opt->description = _("Name of input raster3d map");
  457. break;
  458. case G_OPT_R3_INPUTS:
  459. Opt->key = "input";
  460. Opt->type = TYPE_STRING;
  461. Opt->key_desc = "name";
  462. Opt->required = YES;
  463. Opt->multiple = YES;
  464. Opt->gisprompt = "old,grid3,3d-raster";
  465. Opt->description = _("Name of input raster3d map(s)");
  466. break;
  467. case G_OPT_R3_OUTPUT:
  468. Opt->key = "output";
  469. Opt->type = TYPE_STRING;
  470. Opt->key_desc = "name";
  471. Opt->required = YES;
  472. Opt->gisprompt = "new,grid3,3d-raster";
  473. Opt->description = _("Name for output raster3d map");
  474. break;
  475. case G_OPT_R3_MAP:
  476. Opt->key = "map";
  477. Opt->type = TYPE_STRING;
  478. Opt->key_desc = "name";
  479. Opt->required = YES;
  480. Opt->gisprompt = "old,grid3,3d-raster";
  481. Opt->description = _("Name of input raster3d map");
  482. break;
  483. case G_OPT_R3_MAPS:
  484. Opt->key = "map";
  485. Opt->type = TYPE_STRING;
  486. Opt->key_desc = "name";
  487. Opt->required = YES;
  488. Opt->multiple = YES;
  489. Opt->gisprompt = "old,grid3,3d-raster";
  490. Opt->description = _("Name of input raster3d map(s)");
  491. break;
  492. /*vector maps */
  493. case G_OPT_V_INPUT:
  494. Opt->key = "input";
  495. Opt->type = TYPE_STRING;
  496. Opt->key_desc = "name";
  497. Opt->required = YES;
  498. Opt->gisprompt = "old,vector,vector";
  499. Opt->description = _("Name of input vector map");
  500. break;
  501. case G_OPT_V_INPUTS:
  502. Opt->key = "input";
  503. Opt->type = TYPE_STRING;
  504. Opt->key_desc = "name";
  505. Opt->required = YES;
  506. Opt->multiple = YES;
  507. Opt->gisprompt = "old,vector,vector";
  508. Opt->description = _("Name of input vector map(s)");
  509. break;
  510. case G_OPT_V_OUTPUT:
  511. Opt->key = "output";
  512. Opt->type = TYPE_STRING;
  513. Opt->key_desc = "name";
  514. Opt->required = YES;
  515. Opt->gisprompt = "new,vector,vector";
  516. Opt->description = _("Name for output vector map");
  517. break;
  518. case G_OPT_V_MAP:
  519. Opt->key = "map";
  520. Opt->type = TYPE_STRING;
  521. Opt->key_desc = "name";
  522. Opt->required = YES;
  523. Opt->gisprompt = "old,vector,vector";
  524. Opt->description = _("Name of input vector map");
  525. break;
  526. case G_OPT_V_MAPS:
  527. Opt->key = "map";
  528. Opt->type = TYPE_STRING;
  529. Opt->key_desc = "name";
  530. Opt->required = YES;
  531. Opt->multiple = YES;
  532. Opt->gisprompt = "old,vector,vector";
  533. Opt->description = _("Name of input vector map(s)");
  534. break;
  535. case G_OPT_V_TYPE:
  536. Opt->key = "type";
  537. Opt->type = TYPE_STRING;
  538. Opt->required = NO;
  539. Opt->multiple = YES;
  540. Opt->answer = "point,line,boundary,centroid,area";
  541. Opt->options = "point,line,boundary,centroid,area";
  542. Opt->label = _("Type");
  543. Opt->description = _("Feature type(s)");
  544. break;
  545. case G_OPT_V_FIELD:
  546. Opt->key = "layer";
  547. Opt->type = TYPE_INTEGER;
  548. Opt->required = NO;
  549. Opt->answer = "1";
  550. Opt->label = _("Layer number");
  551. Opt->description =
  552. _("A single vector map can be connected to multiple database "
  553. "tables. This number determines which table to use.");
  554. Opt->gisprompt = "old_layer,layer,layer";
  555. break;
  556. case G_OPT_V_CAT:
  557. Opt->key = "cat";
  558. Opt->type = TYPE_INTEGER;
  559. Opt->required = NO;
  560. Opt->description = _("Category value");
  561. break;
  562. case G_OPT_V_CATS:
  563. Opt->key = "cats";
  564. Opt->type = TYPE_STRING;
  565. Opt->key_desc = "range";
  566. Opt->required = NO;
  567. Opt->label = _("Category values");
  568. Opt->description = _("Example: 1,3,7-9,13");
  569. break;
  570. /* files */
  571. case G_OPT_F_INPUT:
  572. Opt->key = "input";
  573. Opt->type = TYPE_STRING;
  574. Opt->key_desc = "name";
  575. Opt->required = YES;
  576. Opt->gisprompt = "old_file,file,input";
  577. Opt->description = _("Name of input file");
  578. break;
  579. case G_OPT_F_OUTPUT:
  580. Opt->key = "output";
  581. Opt->type = TYPE_STRING;
  582. Opt->key_desc = "name";
  583. Opt->required = YES;
  584. Opt->gisprompt = "new_file,file,output";
  585. Opt->description = _("Name for output file");
  586. break;
  587. case G_OPT_F_SEP:
  588. Opt->key = "fs";
  589. Opt->type = TYPE_STRING;
  590. Opt->key_desc = "character";
  591. Opt->required = NO;
  592. Opt->answer = "|";
  593. Opt->description = _("Field separator");
  594. break;
  595. /* colors */
  596. case G_OPT_C_FG:
  597. Opt->key = "color";
  598. Opt->type = TYPE_STRING;
  599. Opt->key_desc = "name";
  600. Opt->required = NO;
  601. Opt->answer = DEFAULT_FG_COLOR;
  602. Opt->gisprompt = "old_color,color,color";
  603. Opt->label = _("Color");
  604. Opt->description = _("Either a standard color name or R:G:B triplet");
  605. break;
  606. case G_OPT_C_BG:
  607. Opt->key = "bgcolor";
  608. Opt->type = TYPE_STRING;
  609. Opt->key_desc = "name";
  610. Opt->required = NO;
  611. Opt->answer = DEFAULT_BG_COLOR;
  612. Opt->gisprompt = "old_color,color,color_none";
  613. Opt->label = _("Background color");
  614. Opt->description =
  615. _("Either a standard GRASS color, R:G:B triplet, or \"none\"");
  616. break;
  617. }
  618. return (Opt);
  619. }
  620. /**
  621. * \brief Initializes a new module.
  622. *
  623. * \return GModule * Pointer to a GModule struct
  624. */
  625. struct GModule *G_define_module(void)
  626. {
  627. struct GModule *module;
  628. /* Allocate memory */
  629. module = &module_info;
  630. /* Zero structure */
  631. G_zero((char *)module, sizeof(struct GModule));
  632. return (module);
  633. }
  634. /* The main parsing routine */
  635. /**
  636. * \brief Parse command line.
  637. *
  638. * The command line parameters <b>argv</b> and the number of parameters
  639. * <b>argc</b> from the main() routine are passed directly to
  640. * <i>G_parser()</i>. <i>G_parser()</i> accepts the command line input
  641. * entered by the user, and parses this input according to the input
  642. * options and/or flags that were defined by the programmer.<br>
  643. *
  644. * <b>Note:</b> The only functions which can legitimately be called
  645. * before G_parser() are:<br>
  646. * <ul>
  647. * <li>G_gisinit()</li>
  648. * <li>G_no_gisinit()</li>
  649. * <li>G_define_module()</li>
  650. * <li>G_define_flag()</li>
  651. * <li>G_define_option()</li>
  652. * <li>G_define_standard_option()</li>
  653. * <li>G_disable_interactive()</li>
  654. * </ul>
  655. *
  656. * The usual order a module calls functions is:<br>
  657. * <ul>
  658. * <li>G_gisinit()</li>
  659. * <li>G_define_module()</li>
  660. * <li>G_define_{flag,option}()</li>
  661. * <li>G_parser()</li>
  662. * </ul>
  663. *
  664. * \param[in] argc number of arguments
  665. * \param[in] argv argument list
  666. * \return 0 on success
  667. * \return -1 on error and calls <b>G_usage()</b>
  668. */
  669. int G_parser(int argc, char **argv)
  670. {
  671. int need_first_opt;
  672. int opt_checked = 0;
  673. int error;
  674. char *ptr, *tmp_name;
  675. int i;
  676. struct Option *opt;
  677. char force_gui = FALSE;
  678. error = 0;
  679. need_first_opt = 1;
  680. i = strlen(tmp_name = G_store(argv[0]));
  681. while (--i >= 0) {
  682. if (G_is_dirsep(tmp_name[i])) {
  683. tmp_name += i + 1;
  684. break;
  685. }
  686. }
  687. G_basename(tmp_name, "exe");
  688. pgm_name = tmp_name;
  689. /* Stash default answers */
  690. opt = &first_option;
  691. while (opt != NULL) {
  692. if (opt->required)
  693. has_required = 1;
  694. /* Parse options */
  695. if (opt->options) {
  696. int cnt = 0;
  697. char **tokens, delm[2];
  698. delm[0] = ',';
  699. delm[1] = '\0';
  700. tokens = G_tokenize(opt->options, delm);
  701. i = 0;
  702. while (tokens[i]) {
  703. cnt++;
  704. i++;
  705. }
  706. opt->opts =
  707. (const char **)G_calloc(cnt + 1, sizeof(const char *));
  708. i = 0;
  709. while (tokens[i]) {
  710. opt->opts[i] = G_store(tokens[i]);
  711. i++;
  712. }
  713. G_free_tokens(tokens);
  714. if (opt->descriptions) {
  715. delm[0] = ';';
  716. opt->descs =
  717. (const char **)G_calloc(cnt + 1, sizeof(const char *));
  718. tokens = G_tokenize(opt->descriptions, delm);
  719. i = 0;
  720. while (tokens[i]) {
  721. int j, found;
  722. if (!tokens[i + 1])
  723. break;
  724. j = 0;
  725. found = 0;
  726. while (opt->opts[j]) {
  727. if (strcmp(opt->opts[j], tokens[i]) == 0) {
  728. found = 1;
  729. break;
  730. }
  731. j++;
  732. }
  733. if (!found) {
  734. G_warning(_("BUG in descriptions, option '%s' in <%s> does not exist"),
  735. tokens[i], opt->key);
  736. }
  737. else {
  738. opt->descs[j] = G_store(tokens[i + 1]);
  739. }
  740. i += 2;
  741. }
  742. G_free_tokens(tokens);
  743. }
  744. }
  745. /* Copy answer */
  746. if (opt->multiple && opt->answers && opt->answers[0]) {
  747. opt->answer = (char *)G_malloc(strlen(opt->answers[0]) + 1);
  748. strcpy(opt->answer, opt->answers[0]);
  749. for (i = 1; opt->answers[i]; i++) {
  750. opt->answer = (char *)G_realloc(opt->answer,
  751. strlen(opt->answer) +
  752. strlen(opt->answers[i]) + 2);
  753. strcat(opt->answer, ",");
  754. strcat(opt->answer, opt->answers[i]);
  755. }
  756. }
  757. opt->def = opt->answer;
  758. opt = opt->next_opt;
  759. }
  760. /* If there are NO arguments, go interactive */
  761. if (argc < 2 && has_required && interactive_ok && isatty(0)) {
  762. G_gui();
  763. return -1;
  764. }
  765. else if (argc < 2 && has_required && isatty(0)) {
  766. G_usage();
  767. return -1;
  768. }
  769. else if (argc >= 2) {
  770. /* If first arg is "help" give a usage/syntax message */
  771. if (strcmp(argv[1], "help") == 0 ||
  772. strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0) {
  773. G_usage();
  774. exit(EXIT_SUCCESS);
  775. }
  776. /* If first arg is "--interface-description" then print out
  777. * a xml description of the task */
  778. if (strcmp(argv[1], "--interface-description") == 0) {
  779. G_usage_xml();
  780. exit(EXIT_SUCCESS);
  781. }
  782. /* If first arg is "--html-description" then print out
  783. * a html description of the task */
  784. if (strcmp(argv[1], "--html-description") == 0) {
  785. G_usage_html();
  786. exit(EXIT_SUCCESS);
  787. }
  788. /* If first arg is "--tcltk" then generate
  789. * code for tcltkgrass */
  790. if (strcmp(argv[1], "--tcltk") == 0) {
  791. G_tcltk();
  792. exit(EXIT_SUCCESS);
  793. }
  794. /* If first arg is "--script" then then generate
  795. * g.parser boilerplate */
  796. if (strcmp(argv[1], "--script") == 0) {
  797. G_script();
  798. exit(EXIT_SUCCESS);
  799. }
  800. /* Loop thru all command line arguments */
  801. while (--argc) {
  802. ptr = *(++argv);
  803. /* Overwrite option */
  804. if (strcmp(ptr, "--o") == 0 || strcmp(ptr, "--overwrite") == 0) {
  805. overwrite = 1;
  806. }
  807. /* Verbose option */
  808. else if (strcmp(ptr, "--v") == 0 || strcmp(ptr, "--verbose") == 0) {
  809. char buff[32];
  810. /* print everything: max verbosity level */
  811. module_info.verbose = G_verbose_max();
  812. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_max());
  813. putenv(G_store(buff));
  814. if (quiet == 1) {
  815. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --verbose."));
  816. }
  817. quiet = -1;
  818. }
  819. /* Quiet option */
  820. else if (strcmp(ptr, "--q") == 0 || strcmp(ptr, "--quiet") == 0) {
  821. char buff[32];
  822. /* print nothing, but errors and warnings */
  823. module_info.verbose = G_verbose_min();
  824. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_min());
  825. putenv(G_store(buff));
  826. if (quiet == -1) {
  827. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --quiet."));
  828. }
  829. quiet = 1; /* for passing to gui init */
  830. }
  831. /* Force gui to come up */
  832. else if (strcmp(ptr, "--ui") == 0) {
  833. force_gui = TRUE;
  834. }
  835. /* If we see a flag */
  836. else if (*ptr == '-') {
  837. while (*(++ptr))
  838. error += set_flag(*ptr);
  839. }
  840. /* If we see standard option format (option=val) */
  841. else if (is_option(ptr)) {
  842. error += set_option(ptr);
  843. need_first_opt = 0;
  844. }
  845. /* If we see the first option with no equal sign */
  846. else if (need_first_opt && n_opts) {
  847. first_option.answer = G_store(ptr);
  848. need_first_opt = 0;
  849. }
  850. /* If we see the non valid argument (no "=", just argument) */
  851. else if (contains(ptr, '=') == 0) {
  852. fprintf(stderr, _("Sorry <%s> is not a valid option\n"), ptr);
  853. error = 1;
  854. }
  855. }
  856. }
  857. /* Run the gui if it was specifically requested */
  858. if (force_gui) {
  859. G_gui();
  860. return -1;
  861. }
  862. /* Split options where multiple answers are OK */
  863. split_opts();
  864. /* Check multiple options */
  865. error += check_multiple_opts();
  866. /* Check answers against options and check subroutines */
  867. if (!opt_checked)
  868. error += check_opts();
  869. /* Make sure all required options are set */
  870. error += check_required();
  871. if (error) {
  872. if (G_verbose() > G_verbose_min())
  873. G_usage();
  874. return -1;
  875. }
  876. if (check_overwrite())
  877. return -1;
  878. return (0);
  879. }
  880. static int uses_new_gisprompt(void)
  881. {
  882. struct Option *opt;
  883. char age[KEYLENGTH];
  884. char element[KEYLENGTH];
  885. char desc[KEYLENGTH];
  886. if (module_info.overwrite)
  887. return 1;
  888. /* figure out if any of the options use a "new" gisprompt */
  889. /* This is to see if we should spit out the --o flag */
  890. if (n_opts) {
  891. opt = &first_option;
  892. while (opt != NULL) {
  893. if (opt->gisprompt) {
  894. split_gisprompt(opt->gisprompt, age, element, desc);
  895. if (strcmp(age, "new") == 0)
  896. return 1;
  897. }
  898. opt = opt->next_opt;
  899. }
  900. }
  901. return 0;
  902. }
  903. /**
  904. * \brief Command line help/usage message.
  905. *
  906. * Calls to <i>G_usage()</i> allow the programmer to print the usage
  907. * message at any time. This will explain the allowed and required
  908. * command line input to the user. This description is given according
  909. * to the programmer's definitions for options and flags. This function
  910. * becomes useful when the user enters options and/or flags on the
  911. * command line that are syntactically valid to the parser, but
  912. * functionally invalid for the command (e.g. an invalid file name.)<br>
  913. * For example, the parser logic doesn't directly support grouping
  914. * options. If two options be specified together or not at all, the
  915. * parser must be told that these options are not required and the
  916. * programmer must check that if one is specified the other must be as
  917. * well. If this additional check fails, then <i>G_parser()</i> will
  918. * succeed, but the programmer can then call <i>G_usage()</i> to print
  919. * the standard usage message and print additional information about how
  920. * the two options work together.
  921. *
  922. * \return always returns 0
  923. */
  924. int G_usage(void)
  925. {
  926. struct Option *opt;
  927. struct Flag *flag;
  928. char item[256];
  929. const char *key_desc;
  930. int maxlen;
  931. int len, n;
  932. int new_prompt = 0;
  933. new_prompt = uses_new_gisprompt();
  934. if (!pgm_name) /* v.dave && r.michael */
  935. pgm_name = G_program_name();
  936. if (!pgm_name)
  937. pgm_name = "??";
  938. if (module_info.label || module_info.description) {
  939. fprintf(stderr, _("\nDescription:\n"));
  940. if (module_info.label)
  941. fprintf(stderr, " %s\n", module_info.label);
  942. if (module_info.description)
  943. fprintf(stderr, " %s\n", module_info.description);
  944. }
  945. if (module_info.keywords) {
  946. fprintf(stderr, _("\nKeywords:\n"));
  947. fprintf(stderr, " %s\n", module_info.keywords);
  948. }
  949. fprintf(stderr, _("\nUsage:\n "));
  950. len = show(pgm_name, 1);
  951. /* Print flags */
  952. if (n_flags) {
  953. item[0] = ' ';
  954. item[1] = '[';
  955. item[2] = '-';
  956. flag = &first_flag;
  957. for (n = 3; flag != NULL; n++, flag = flag->next_flag)
  958. item[n] = flag->key;
  959. item[n++] = ']';
  960. item[n] = 0;
  961. len = show(item, len);
  962. }
  963. maxlen = 0;
  964. if (n_opts) {
  965. opt = &first_option;
  966. while (opt != NULL) {
  967. if (opt->key_desc != NULL)
  968. key_desc = opt->key_desc;
  969. else if (opt->type == TYPE_STRING)
  970. key_desc = "string";
  971. else
  972. key_desc = "value";
  973. n = strlen(opt->key);
  974. if (n > maxlen)
  975. maxlen = n;
  976. strcpy(item, " ");
  977. if (!opt->required)
  978. strcat(item, "[");
  979. strcat(item, opt->key);
  980. strcat(item, "=");
  981. strcat(item, key_desc);
  982. if (opt->multiple) {
  983. strcat(item, "[,");
  984. strcat(item, key_desc);
  985. strcat(item, ",...]");
  986. }
  987. if (!opt->required)
  988. strcat(item, "]");
  989. len = show(item, len);
  990. opt = opt->next_opt;
  991. }
  992. }
  993. if (new_prompt) {
  994. strcpy(item, " [--overwrite]");
  995. len = show(item, len);
  996. }
  997. strcpy(item, " [--verbose]");
  998. len = show(item, len);
  999. strcpy(item, " [--quiet]");
  1000. len = show(item, len);
  1001. fprintf(stderr, "\n");
  1002. /* Print help info for flags */
  1003. fprintf(stderr, _("\nFlags:\n"));
  1004. if (n_flags) {
  1005. flag = &first_flag;
  1006. while (flag != NULL) {
  1007. fprintf(stderr, " -%c ", flag->key);
  1008. if (flag->label) {
  1009. fprintf(stderr, "%s\n", flag->label);
  1010. if (flag->description)
  1011. fprintf(stderr, " %s\n", flag->description);
  1012. }
  1013. else if (flag->description) {
  1014. fprintf(stderr, "%s\n", flag->description);
  1015. }
  1016. flag = flag->next_flag;
  1017. }
  1018. }
  1019. if (new_prompt)
  1020. fprintf(stderr, " --o %s\n",
  1021. _("Allow output files to overwrite existing files"));
  1022. fprintf(stderr, " --v %s\n", _("Verbose module output"));
  1023. fprintf(stderr, " --q %s\n", _("Quiet module output"));
  1024. /* Print help info for options */
  1025. if (n_opts) {
  1026. fprintf(stderr, _("\nParameters:\n"));
  1027. opt = &first_option;
  1028. while (opt != NULL) {
  1029. fprintf(stderr, " %*s ", maxlen, opt->key);
  1030. if (opt->label) {
  1031. fprintf(stderr, "%s\n", opt->label);
  1032. if (opt->description) {
  1033. fprintf(stderr, " %*s %s\n",
  1034. maxlen, " ", opt->description);
  1035. }
  1036. }
  1037. else if (opt->description) {
  1038. fprintf(stderr, "%s\n", opt->description);
  1039. }
  1040. if (opt->options)
  1041. show_options(maxlen, opt->options);
  1042. /*
  1043. fprintf (stderr, " %*s options: %s\n", maxlen, " ",
  1044. _(opt->options)) ;
  1045. */
  1046. if (opt->def)
  1047. fprintf(stderr, _(" %*s default: %s\n"), maxlen, " ",
  1048. opt->def);
  1049. if (opt->descs) {
  1050. int i = 0;
  1051. while (opt->opts[i]) {
  1052. if (opt->descs[i])
  1053. fprintf(stderr, " %*s %s: %s\n",
  1054. maxlen, " ", opt->opts[i], opt->descs[i]);
  1055. i++;
  1056. }
  1057. }
  1058. opt = opt->next_opt;
  1059. }
  1060. }
  1061. return 0;
  1062. }
  1063. /**
  1064. * \brief Formats text for XML.
  1065. *
  1066. * \param[in,out] fp file to write to
  1067. * \param[in] str string to write
  1068. */
  1069. static void print_escaped_for_xml(FILE * fp, const char *str)
  1070. {
  1071. for (; *str; str++) {
  1072. switch (*str) {
  1073. case '&':
  1074. fputs("&amp;", fp);
  1075. break;
  1076. case '<':
  1077. fputs("&lt;", fp);
  1078. break;
  1079. case '>':
  1080. fputs("&gt;", fp);
  1081. break;
  1082. default:
  1083. fputc(*str, fp);
  1084. }
  1085. }
  1086. }
  1087. /**
  1088. * \brief Format text for HTML output
  1089. */
  1090. #define do_escape(c,escaped) case c: fputs(escaped,f);break
  1091. static void print_escaped_for_html(FILE * f, const char *str)
  1092. {
  1093. const char *s;
  1094. for (s = str; *s; s++) {
  1095. switch (*s) {
  1096. do_escape('&', "&amp;");
  1097. do_escape('<', "&lt;");
  1098. do_escape('>', "&gt;");
  1099. do_escape('\n', "<br>");
  1100. default:
  1101. fputc(*s, f);
  1102. }
  1103. }
  1104. }
  1105. #undef do_escape
  1106. /**
  1107. \brief Print module usage description in XML format.
  1108. **/
  1109. static void G_usage_xml(void)
  1110. {
  1111. struct Option *opt;
  1112. struct Flag *flag;
  1113. char *type;
  1114. char *s, *top;
  1115. int i;
  1116. char *encoding;
  1117. int new_prompt = 0;
  1118. new_prompt = uses_new_gisprompt();
  1119. /* gettext converts strings to encoding returned by nl_langinfo(CODESET) */
  1120. #if defined(HAVE_LANGINFO_H)
  1121. encoding = nl_langinfo(CODESET);
  1122. if (!encoding || strlen(encoding) == 0) {
  1123. encoding = "UTF-8";
  1124. }
  1125. #else
  1126. encoding = "UTF-8";
  1127. #endif
  1128. if (!pgm_name) /* v.dave && r.michael */
  1129. pgm_name = G_program_name();
  1130. if (!pgm_name)
  1131. pgm_name = "??";
  1132. fprintf(stdout, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", encoding);
  1133. fprintf(stdout, "<!DOCTYPE task SYSTEM \"grass-interface.dtd\">\n");
  1134. fprintf(stdout, "<task name=\"%s\">\n", pgm_name);
  1135. if (module_info.label) {
  1136. fprintf(stdout, "\t<label>\n\t\t");
  1137. print_escaped_for_xml(stdout, module_info.label);
  1138. fprintf(stdout, "\n\t</label>\n");
  1139. }
  1140. if (module_info.description) {
  1141. fprintf(stdout, "\t<description>\n\t\t");
  1142. print_escaped_for_xml(stdout, module_info.description);
  1143. fprintf(stdout, "\n\t</description>\n");
  1144. }
  1145. if (module_info.keywords) {
  1146. fprintf(stdout, "\t<keywords>\n\t\t");
  1147. print_escaped_for_xml(stdout, module_info.keywords);
  1148. fprintf(stdout, "\n\t</keywords>\n");
  1149. }
  1150. /***** Don't use parameter-groups for now. We'll reimplement this later
  1151. ***** when we have a concept of several mutually exclusive option
  1152. ***** groups
  1153. if (n_opts || n_flags)
  1154. fprintf(stdout, "\t<parameter-group>\n");
  1155. *****
  1156. *****
  1157. *****/
  1158. if (n_opts) {
  1159. opt = &first_option;
  1160. while (opt != NULL) {
  1161. /* TODO: make this a enumeration type? */
  1162. switch (opt->type) {
  1163. case TYPE_INTEGER:
  1164. type = "integer";
  1165. break;
  1166. case TYPE_DOUBLE:
  1167. type = "float";
  1168. break;
  1169. case TYPE_STRING:
  1170. type = "string";
  1171. break;
  1172. default:
  1173. type = "string";
  1174. break;
  1175. }
  1176. fprintf(stdout, "\t<parameter "
  1177. "name=\"%s\" "
  1178. "type=\"%s\" "
  1179. "required=\"%s\" "
  1180. "multiple=\"%s\">\n",
  1181. opt->key,
  1182. type,
  1183. opt->required == YES ? "yes" : "no",
  1184. opt->multiple == YES ? "yes" : "no");
  1185. if (opt->label) {
  1186. fprintf(stdout, "\t\t<label>\n\t\t\t");
  1187. print_escaped_for_xml(stdout, opt->label);
  1188. fprintf(stdout, "\n\t\t</label>\n");
  1189. }
  1190. if (opt->description) {
  1191. fprintf(stdout, "\t\t<description>\n\t\t\t");
  1192. print_escaped_for_xml(stdout, opt->description);
  1193. fprintf(stdout, "\n\t\t</description>\n");
  1194. }
  1195. if (opt->key_desc) {
  1196. fprintf(stdout, "\t\t<keydesc>\n");
  1197. top = G_calloc(strlen(opt->key_desc) + 1, 1);
  1198. strcpy(top, opt->key_desc);
  1199. s = strtok(top, ",");
  1200. for (i = 1; s != NULL; i++) {
  1201. fprintf(stdout, "\t\t\t<item order=\"%d\">", i);
  1202. print_escaped_for_xml(stdout, s);
  1203. fprintf(stdout, "</item>\n");
  1204. s = strtok(NULL, ",");
  1205. }
  1206. fprintf(stdout, "\t\t</keydesc>\n");
  1207. G_free(top);
  1208. }
  1209. if (opt->gisprompt) {
  1210. const char *atts[] = { "age", "element", "prompt", NULL };
  1211. top = G_calloc(strlen(opt->gisprompt) + 1, 1);
  1212. strcpy(top, opt->gisprompt);
  1213. s = strtok(top, ",");
  1214. fprintf(stdout, "\t\t<gisprompt ");
  1215. for (i = 0; s != NULL && atts[i] != NULL; i++) {
  1216. fprintf(stdout, "%s=\"%s\" ", atts[i], s);
  1217. s = strtok(NULL, ",");
  1218. }
  1219. fprintf(stdout, "/>\n");
  1220. G_free(top);
  1221. }
  1222. if (opt->def) {
  1223. fprintf(stdout, "\t\t<default>\n\t\t\t");
  1224. print_escaped_for_xml(stdout, opt->def);
  1225. fprintf(stdout, "\n\t\t</default>\n");
  1226. }
  1227. if (opt->options) {
  1228. /* TODO:
  1229. * add something like
  1230. * <range min="xxx" max="xxx"/>
  1231. * to <values> */
  1232. i = 0;
  1233. fprintf(stdout, "\t\t<values>\n");
  1234. while (opt->opts[i]) {
  1235. fprintf(stdout, "\t\t\t<value>\n");
  1236. fprintf(stdout, "\t\t\t\t<name>");
  1237. print_escaped_for_xml(stdout, opt->opts[i]);
  1238. fprintf(stdout, "</name>\n");
  1239. if (opt->descs && opt->opts[i] && opt->descs[i]) {
  1240. fprintf(stdout, "\t\t\t\t<description>");
  1241. print_escaped_for_xml(stdout, opt->descs[i]);
  1242. fprintf(stdout, "</description>\n");
  1243. }
  1244. fprintf(stdout, "\t\t\t</value>\n");
  1245. i++;
  1246. }
  1247. fprintf(stdout, "\t\t</values>\n");
  1248. }
  1249. if (opt->guisection) {
  1250. fprintf(stdout, "\t\t<guisection>\n\t\t\t");
  1251. print_escaped_for_xml(stdout, opt->guisection);
  1252. fprintf(stdout, "\n\t\t</guisection>\n");
  1253. }
  1254. /* TODO:
  1255. * - key_desc?
  1256. * - there surely are some more. which ones?
  1257. */
  1258. opt = opt->next_opt;
  1259. fprintf(stdout, "\t</parameter>\n");
  1260. }
  1261. }
  1262. if (n_flags) {
  1263. flag = &first_flag;
  1264. while (flag != NULL) {
  1265. fprintf(stdout, "\t<flag name=\"%c\">\n", flag->key);
  1266. if (flag->label) {
  1267. fprintf(stdout, "\t\t<label>\n\t\t\t");
  1268. print_escaped_for_xml(stdout, flag->label);
  1269. fprintf(stdout, "\n\t\t</label>\n");
  1270. }
  1271. if (flag->description) {
  1272. fprintf(stdout, "\t\t<description>\n\t\t\t");
  1273. print_escaped_for_xml(stdout, flag->description);
  1274. fprintf(stdout, "\n\t\t</description>\n");
  1275. }
  1276. if (flag->guisection) {
  1277. fprintf(stdout, " \t\t<guisection>\n\t\t\t");
  1278. print_escaped_for_xml(stdout, flag->guisection);
  1279. fprintf(stdout, "\n\t\t</guisection>\n");
  1280. }
  1281. flag = flag->next_flag;
  1282. fprintf(stdout, "\t</flag>\n");
  1283. }
  1284. }
  1285. /***** Don't use parameter-groups for now. We'll reimplement this later
  1286. ***** when we have a concept of several mutually exclusive option
  1287. ***** groups
  1288. if (n_opts || n_flags)
  1289. fprintf(stdout, "\t</parameter-group>\n");
  1290. *****
  1291. *****
  1292. *****/
  1293. if (new_prompt) {
  1294. /* overwrite */
  1295. fprintf(stdout, "\t<flag name=\"%s\">\n", "overwrite");
  1296. fprintf(stdout, "\t\t<description>\n\t\t\t");
  1297. print_escaped_for_xml(stdout,
  1298. "Allow output files to overwrite existing files");
  1299. fprintf(stdout, "\n\t\t</description>\n");
  1300. fprintf(stdout, "\t</flag>\n");
  1301. }
  1302. /* verbose */
  1303. fprintf(stdout, "\t<flag name=\"%s\">\n", "verbose");
  1304. fprintf(stdout, "\t\t<description>\n\t\t\t");
  1305. print_escaped_for_xml(stdout, "Verbose module output");
  1306. fprintf(stdout, "\n\t\t</description>\n");
  1307. fprintf(stdout, "\t</flag>\n");
  1308. /* quiet */
  1309. fprintf(stdout, "\t<flag name=\"%s\">\n", "quiet");
  1310. fprintf(stdout, "\t\t<description>\n\t\t\t");
  1311. print_escaped_for_xml(stdout, "Quiet module output");
  1312. fprintf(stdout, "\n\t\t</description>\n");
  1313. fprintf(stdout, "\t</flag>\n");
  1314. fprintf(stdout, "</task>\n");
  1315. }
  1316. /**
  1317. \brief Print module usage description in HTML format.
  1318. **/
  1319. static void G_usage_html(void)
  1320. {
  1321. struct Option *opt;
  1322. struct Flag *flag;
  1323. const char *type;
  1324. int new_prompt = 0;
  1325. new_prompt = uses_new_gisprompt();
  1326. if (!pgm_name) /* v.dave && r.michael */
  1327. pgm_name = G_program_name();
  1328. if (!pgm_name)
  1329. pgm_name = "??";
  1330. fprintf(stdout,
  1331. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
  1332. fprintf(stdout, "<html>\n<head>\n");
  1333. fprintf(stdout, "<title>GRASS GIS: %s</title>\n", pgm_name);
  1334. fprintf(stdout,
  1335. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
  1336. fprintf(stdout,
  1337. "<link rel=\"stylesheet\" href=\"grassdocs.css\" type=\"text/css\">\n");
  1338. fprintf(stdout, "</head>\n");
  1339. fprintf(stdout, "<body bgcolor=\"white\">\n\n");
  1340. fprintf(stdout,
  1341. "<img src=\"grass_logo.png\" alt=\"GRASS logo\"><hr align=center size=6 noshade>\n\n");
  1342. fprintf(stdout, "<h2>%s</h2>\n", _("NAME"));
  1343. fprintf(stdout, "<em><b>%s</b></em> ", pgm_name);
  1344. if (module_info.label || module_info.description)
  1345. fprintf(stdout, " - ");
  1346. if (module_info.label)
  1347. fprintf(stdout, "%s<BR>\n", module_info.label);
  1348. if (module_info.description)
  1349. fprintf(stdout, "%s\n", module_info.description);
  1350. fprintf(stdout, "<h2>%s</h2>\n", _("KEYWORDS"));
  1351. if (module_info.keywords) {
  1352. fprintf(stdout, "%s", module_info.keywords);
  1353. fprintf(stdout, "\n");
  1354. }
  1355. fprintf(stdout, "<h2>%s</h2>\n", _("SYNOPSIS"));
  1356. fprintf(stdout, "<b>%s</b><br>\n", pgm_name);
  1357. fprintf(stdout, "<b>%s help</b><br>\n", pgm_name);
  1358. fprintf(stdout, "<b>%s</b>", pgm_name);
  1359. /* print short version first */
  1360. if (n_flags) {
  1361. flag = &first_flag;
  1362. fprintf(stdout, " [-<b>");
  1363. while (flag != NULL) {
  1364. fprintf(stdout, "%c", flag->key);
  1365. flag = flag->next_flag;
  1366. }
  1367. fprintf(stdout, "</b>] ");
  1368. }
  1369. else
  1370. fprintf(stdout, " ");
  1371. if (n_opts) {
  1372. opt = &first_option;
  1373. while (opt != NULL) {
  1374. if (opt->key_desc != NULL)
  1375. type = opt->key_desc;
  1376. else
  1377. switch (opt->type) {
  1378. case TYPE_INTEGER:
  1379. type = "integer";
  1380. break;
  1381. case TYPE_DOUBLE:
  1382. type = "float";
  1383. break;
  1384. case TYPE_STRING:
  1385. type = "string";
  1386. break;
  1387. default:
  1388. type = "string";
  1389. break;
  1390. }
  1391. if (!opt->required)
  1392. fprintf(stdout, " [");
  1393. fprintf(stdout, "<b>%s</b>=<em>%s</em>", opt->key, type);
  1394. if (opt->multiple) {
  1395. fprintf(stdout, "[,<i>%s</i>,...]", type);
  1396. }
  1397. if (!opt->required)
  1398. fprintf(stdout, "] ");
  1399. opt = opt->next_opt;
  1400. fprintf(stdout, " ");
  1401. }
  1402. }
  1403. if (new_prompt)
  1404. fprintf(stdout, " [--<b>overwrite</b>] ");
  1405. fprintf(stdout, " [--<b>verbose</b>] ");
  1406. fprintf(stdout, " [--<b>quiet</b>] ");
  1407. fprintf(stdout, "\n");
  1408. /* now long version */
  1409. fprintf(stdout, "\n");
  1410. if (n_flags || new_prompt) {
  1411. flag = &first_flag;
  1412. fprintf(stdout, "<h3>%s:</h3>\n", _("Flags"));
  1413. fprintf(stdout, "<DL>\n");
  1414. while (n_flags && flag != NULL) {
  1415. fprintf(stdout, "<DT><b>-%c</b></DT>\n", flag->key);
  1416. if (flag->label) {
  1417. fprintf(stdout, "<DD>");
  1418. fprintf(stdout, "%s", flag->label);
  1419. fprintf(stdout, "</DD>\n");
  1420. }
  1421. if (flag->description) {
  1422. fprintf(stdout, "<DD>");
  1423. fprintf(stdout, "%s", flag->description);
  1424. fprintf(stdout, "</DD>\n");
  1425. }
  1426. flag = flag->next_flag;
  1427. fprintf(stdout, "\n");
  1428. }
  1429. if (new_prompt) {
  1430. fprintf(stdout, "<DT><b>--overwrite</b></DT>\n");
  1431. fprintf(stdout, "<DD>%s</DD>\n",
  1432. _("Allow output files to overwrite existing files"));
  1433. }
  1434. fprintf(stdout, "<DT><b>--verbose</b></DT>\n");
  1435. fprintf(stdout, "<DD>%s</DD>\n", _("Verbose module output"));
  1436. fprintf(stdout, "<DT><b>--quiet</b></DT>\n");
  1437. fprintf(stdout, "<DD>%s</DD>\n", _("Quiet module output"));
  1438. fprintf(stdout, "</DL>\n");
  1439. }
  1440. fprintf(stdout, "\n");
  1441. if (n_opts) {
  1442. opt = &first_option;
  1443. fprintf(stdout, "<h3>%s:</h3>\n", _("Parameters"));
  1444. fprintf(stdout, "<DL>\n");
  1445. while (opt != NULL) {
  1446. /* TODO: make this a enumeration type? */
  1447. if (opt->key_desc != NULL)
  1448. type = opt->key_desc;
  1449. else
  1450. switch (opt->type) {
  1451. case TYPE_INTEGER:
  1452. type = "integer";
  1453. break;
  1454. case TYPE_DOUBLE:
  1455. type = "float";
  1456. break;
  1457. case TYPE_STRING:
  1458. type = "string";
  1459. break;
  1460. default:
  1461. type = "string";
  1462. break;
  1463. }
  1464. fprintf(stdout, "<DT><b>%s</b>=<em>%s", opt->key, type);
  1465. if (opt->multiple) {
  1466. fprintf(stdout, "[,<i>%s</i>,...]", type);
  1467. }
  1468. fprintf(stdout, "</em></DT>\n");
  1469. if (opt->label) {
  1470. fprintf(stdout, "<DD>");
  1471. print_escaped_for_html(stdout, opt->label);
  1472. fprintf(stdout, "</DD>\n");
  1473. }
  1474. if (opt->description) {
  1475. fprintf(stdout, "<DD>");
  1476. print_escaped_for_html(stdout, opt->description);
  1477. fprintf(stdout, "</DD>\n");
  1478. }
  1479. if (opt->options) {
  1480. fprintf(stdout, "<DD>%s: <em>", _("Options"));
  1481. print_escaped_for_html(stdout, opt->options);
  1482. fprintf(stdout, "</em></DD>\n");
  1483. }
  1484. if (opt->def) {
  1485. fprintf(stdout, "<DD>%s: <em>", _("Default"));
  1486. print_escaped_for_html(stdout, opt->def);
  1487. fprintf(stdout, "</em></DD>\n");
  1488. }
  1489. if (opt->descs) {
  1490. int i = 0;
  1491. while (opt->opts[i]) {
  1492. if (opt->descs[i]) {
  1493. fprintf(stdout, "<DD><b>");
  1494. print_escaped_for_html(stdout, opt->opts[i]);
  1495. fprintf(stdout, "</b>: ");
  1496. print_escaped_for_html(stdout, opt->descs[i]);
  1497. fprintf(stdout, "</DD>\n");
  1498. }
  1499. i++;
  1500. }
  1501. }
  1502. opt = opt->next_opt;
  1503. fprintf(stdout, "\n");
  1504. }
  1505. fprintf(stdout, "</DL>\n");
  1506. }
  1507. fprintf(stdout, "</body>\n</html>\n");
  1508. }
  1509. /**
  1510. \brief Print a module parameter template to assist with creating shell script wrappers.
  1511. **/
  1512. static void G_script(void)
  1513. {
  1514. FILE *fp = stdout;
  1515. char *type;
  1516. fprintf(fp, "#!/bin/sh\n\n");
  1517. fprintf(fp,
  1518. "############################################################################\n");
  1519. fprintf(fp, "#\n");
  1520. fprintf(fp, "# MODULE: %s_wrapper\n", G_program_name());
  1521. fprintf(fp, "# AUTHOR(S): %s\n", G_whoami());
  1522. fprintf(fp, "# PURPOSE: \n");
  1523. fprintf(fp, "# COPYRIGHT: (C) 2008 GRASS Development Team/%s\n",
  1524. G_whoami());
  1525. fprintf(fp, "#\n");
  1526. fprintf(fp,
  1527. "# This program is free software; you can redistribute it and/or modify\n");
  1528. fprintf(fp,
  1529. "# it under the terms of the GNU General Public License as published by\n");
  1530. fprintf(fp,
  1531. "# the Free Software Foundation; either version 2 of the License, or\n");
  1532. fprintf(fp, "# (at your option) any later version.\n");
  1533. fprintf(fp, "#\n");
  1534. fprintf(fp,
  1535. "# This program is distributed in the hope that it will be useful,\n");
  1536. fprintf(fp,
  1537. "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  1538. fprintf(fp,
  1539. "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
  1540. fprintf(fp, "# GNU General Public License for more details.\n");
  1541. fprintf(fp, "#\n");
  1542. fprintf(fp,
  1543. "#############################################################################/\n");
  1544. fprintf(fp, "#%%Module\n");
  1545. if (module_info.label)
  1546. fprintf(fp, "#%% label: %s\n", module_info.label);
  1547. if (module_info.description)
  1548. fprintf(fp, "#%% description: %s\n", module_info.description);
  1549. if (module_info.keywords)
  1550. fprintf(fp, "#%% keywords: %s\n", module_info.keywords);
  1551. fprintf(fp, "#%%End\n");
  1552. if (n_flags) {
  1553. struct Flag *flag;
  1554. for (flag = &first_flag; flag; flag = flag->next_flag) {
  1555. fprintf(fp, "#%%Flag\n");
  1556. fprintf(fp, "#%% key: %c\n", flag->key);
  1557. if (flag->label)
  1558. fprintf(fp, "#%% label: %s\n", flag->label);
  1559. if (flag->description)
  1560. fprintf(fp, "#%% description: %s\n", flag->description);
  1561. if (flag->guisection)
  1562. fprintf(fp, "#%% guisection: %s\n", flag->guisection);
  1563. fprintf(fp, "#%%End\n");
  1564. }
  1565. }
  1566. if (n_opts) {
  1567. struct Option *opt;
  1568. for (opt = &first_option; opt; opt = opt->next_opt) {
  1569. switch (opt->type) {
  1570. case TYPE_INTEGER:
  1571. type = "integer";
  1572. break;
  1573. case TYPE_DOUBLE:
  1574. type = "double";
  1575. break;
  1576. case TYPE_STRING:
  1577. type = "string";
  1578. break;
  1579. default:
  1580. type = "string";
  1581. break;
  1582. }
  1583. fprintf(fp, "#%%Option\n");
  1584. fprintf(fp, "#%% key: %s\n", opt->key);
  1585. fprintf(fp, "#%% type: %s\n", type);
  1586. fprintf(fp, "#%% required: %s\n", opt->required ? "yes" : "no");
  1587. fprintf(fp, "#%% multiple: %s\n", opt->multiple ? "yes" : "no");
  1588. if (opt->options)
  1589. fprintf(fp, "#%% options: %s\n", opt->options);
  1590. if (opt->key_desc)
  1591. fprintf(fp, "#%% key_desc: %s\n", opt->key_desc);
  1592. if (opt->label)
  1593. fprintf(fp, "#%% label: %s\n", opt->label);
  1594. if (opt->description)
  1595. fprintf(fp, "#%% description: %s\n", opt->description);
  1596. if (opt->descriptions)
  1597. fprintf(fp, "#%% descriptions: %s\n", opt->descriptions);
  1598. if (opt->answer)
  1599. fprintf(fp, "#%% answer: %s\n", opt->answer);
  1600. if (opt->gisprompt)
  1601. fprintf(fp, "#%% gisprompt: %s\n", opt->gisprompt);
  1602. if (opt->guisection)
  1603. fprintf(fp, "#%% guisection: %s\n", opt->guisection);
  1604. fprintf(fp, "#%%End\n");
  1605. }
  1606. }
  1607. fprintf(fp,
  1608. "\nif [ -z \"$GISBASE\" ] ; then\n"
  1609. " echo \"You must be in GRASS GIS to run this program.\" 1>&2\n"
  1610. " exit 1\n"
  1611. "fi\n"
  1612. "\n"
  1613. "if [ \"$1\" != \"@ARGS_PARSED@\" ] ; then\n"
  1614. " exec g.parser \"$0\" \"$@\"\n"
  1615. "fi\n" "\n" "# CODE GOES HERE\n" "\n");
  1616. }
  1617. /**
  1618. \brief Generates tcltk dialog.
  1619. \param[out] fp File to store tcltk code
  1620. **/
  1621. static void generate_tcl(FILE * fp)
  1622. {
  1623. int new_prompt = uses_new_gisprompt();
  1624. const char *type;
  1625. int optn;
  1626. fprintf(fp, "begin_dialog {%s} {\n", pgm_name);
  1627. fprintf(fp, " label {%s}\n", module_info.label ? module_info.label : "");
  1628. fprintf(fp, " desc {%s}\n",
  1629. module_info.description ? module_info.description : "");
  1630. fprintf(fp, " key {%s}\n",
  1631. module_info.keywords ? module_info.keywords : "");
  1632. fprintf(fp, "}\n");
  1633. optn = 1;
  1634. if (n_flags) {
  1635. struct Flag *flag;
  1636. for (flag = &first_flag; flag; flag = flag->next_flag, optn++) {
  1637. fprintf(fp, "add_flag %d {\n", optn);
  1638. fprintf(fp, " name {%c}\n", flag->key);
  1639. fprintf(fp, " desc {%s}\n", flag->description);
  1640. fprintf(fp, " answer %d\n", flag->answer);
  1641. /* It should be up to the gui as to what
  1642. to do with the label and description */
  1643. fprintf(fp, " label {%s}\n", flag->label ? flag->label : "");
  1644. fprintf(fp, " guisection {%s}\n",
  1645. flag->guisection ? flag->guisection : "");
  1646. fprintf(fp, "}\n");
  1647. }
  1648. }
  1649. if (n_opts) {
  1650. struct Option *opt;
  1651. for (opt = &first_option; opt; opt = opt->next_opt, optn++) {
  1652. if (opt->key_desc != NULL)
  1653. type = opt->key_desc;
  1654. else
  1655. switch (opt->type) {
  1656. case TYPE_INTEGER:
  1657. type = "integer";
  1658. break;
  1659. case TYPE_DOUBLE:
  1660. type = "float";
  1661. break;
  1662. case TYPE_STRING:
  1663. type = "string";
  1664. break;
  1665. default:
  1666. type = "string";
  1667. break;
  1668. }
  1669. fprintf(fp, "add_option %d {\n", optn);
  1670. fprintf(fp, " name {%s}\n", opt->key);
  1671. fprintf(fp, " type {%s}\n", type);
  1672. fprintf(fp, " multi %d\n", opt->multiple);
  1673. fprintf(fp, " desc {%s}\n", opt->description);
  1674. fprintf(fp, " required %d\n", opt->required);
  1675. fprintf(fp, " options {%s}\n", opt->options ? opt->options : "");
  1676. fprintf(fp, " descs {%s}\n",
  1677. opt->descriptions ? opt->descriptions : "");
  1678. fprintf(fp, " answer {%s}\n", opt->answer ? opt->answer : "");
  1679. fprintf(fp, " prompt {%s}\n",
  1680. opt->gisprompt ? opt->gisprompt : "");
  1681. /* It should be up to the gui as to what
  1682. to do with the label and description */
  1683. fprintf(fp, " label {%s}\n", opt->label ? opt->label : "");
  1684. fprintf(fp, " guisection {%s}\n",
  1685. opt->guisection ? opt->guisection : "");
  1686. fprintf(fp, "}\n");
  1687. }
  1688. }
  1689. if (new_prompt) {
  1690. fprintf(fp, "add_xflag %d {\n", optn);
  1691. fprintf(fp, " name {overwrite}\n");
  1692. fprintf(fp, " desc {%s}\n",
  1693. _("Allow output files to overwrite existing files"));
  1694. fprintf(fp, " answer %d\n", overwrite);
  1695. fprintf(fp, " label {%s}\n", _("Allow overwrite"));
  1696. fprintf(fp, " guisection {}\n");
  1697. fprintf(fp, "}\n");
  1698. optn++;
  1699. }
  1700. fprintf(fp, "add_xflag %d {\n", optn);
  1701. fprintf(fp, " name {quiet}\n");
  1702. fprintf(fp, " desc {%s}\n", _("Run with minimal output messages"));
  1703. fprintf(fp, " answer %d\n", quiet);
  1704. fprintf(fp, " label {%s}\n", _("Run quietly"));
  1705. fprintf(fp, " guisection {}\n");
  1706. fprintf(fp, "}\n");
  1707. optn++;
  1708. fprintf(fp, "end_dialog %d\n", optn - 1);
  1709. }
  1710. /**
  1711. \brief Build Tcl/Tk GUI dialog
  1712. **/
  1713. static void G_gui_tcltk(void)
  1714. {
  1715. FILE *fp;
  1716. if (!pgm_name)
  1717. pgm_name = G_program_name();
  1718. if (!pgm_name)
  1719. pgm_name = "??";
  1720. #ifdef __MINGW32__
  1721. if (getenv("GRASS_DEBUG_GUI"))
  1722. fp = G_popen("tee gui_dump.tcl | %GRASS_WISH%", "w");
  1723. else
  1724. fp = G_popen("%GRASS_WISH%", "w");
  1725. #else
  1726. if (getenv("GRASS_DEBUG_GUI"))
  1727. fp = G_popen("tee gui_dump.tcl | $GRASS_WISH", "w");
  1728. else
  1729. fp = G_popen("$GRASS_WISH", "w");
  1730. #endif
  1731. if (!fp)
  1732. G_fatal_error(_("Unable to spawn the 'wish' program"));
  1733. fprintf(fp, "source $env(GISBASE)/etc/gui.tcl\n");
  1734. generate_tcl(fp);
  1735. G_pclose(fp);
  1736. }
  1737. /**
  1738. \brief Build wxPython GUI dialog
  1739. **/
  1740. static void G_gui_wx(void)
  1741. {
  1742. char script[GPATH_MAX];
  1743. if (!pgm_name)
  1744. pgm_name = G_program_name();
  1745. if (!pgm_name)
  1746. G_fatal_error(_("Unable to determine program name"));
  1747. sprintf(script, "%s/etc/wxpython/gui_modules/menuform.py",
  1748. getenv("GISBASE"));
  1749. G_spawn("python", "menuform.py", script, pgm_name, NULL);
  1750. }
  1751. /**
  1752. \brief Invoke GUI dialog
  1753. Use G_gui_wx() or G_gui_tcltk() to generate GUI dialog.
  1754. G_gui_tcltk() is called by default (if GRASS_GUI is not defined)
  1755. **/
  1756. static void G_gui(void)
  1757. {
  1758. /* read environment variables first then internal GRASS variable */
  1759. char *gui = getenv("GRASS_GUI");
  1760. if (!gui) {
  1761. gui = G_getenv("GRASS_GUI");
  1762. }
  1763. if (gui && strcmp(gui, "wxpython") == 0)
  1764. G_gui_wx();
  1765. else
  1766. G_gui_tcltk();
  1767. return;
  1768. }
  1769. /**
  1770. \brief Send Tcl/Tk code to tcltkgrass
  1771. **/
  1772. static void G_tcltk(void)
  1773. {
  1774. if (!pgm_name)
  1775. pgm_name = G_program_name();
  1776. if (!pgm_name)
  1777. pgm_name = "??";
  1778. generate_tcl(stdout);
  1779. }
  1780. /**************************************************************************
  1781. *
  1782. * The remaining routines are all local (static) routines used to support
  1783. * the parsing process.
  1784. *
  1785. **************************************************************************/
  1786. static int show_options(int maxlen, const char *str)
  1787. {
  1788. char *buff = G_store(str);
  1789. char *p1, *p2;
  1790. int totlen, len;
  1791. fprintf(stderr, _(" %*s options: "), maxlen, " ");
  1792. totlen = maxlen + 13;
  1793. p1 = buff;
  1794. while ((p2 = G_index(p1, ','))) {
  1795. *p2 = '\0';
  1796. len = strlen(p1) + 1;
  1797. if ((len + totlen) > 76) {
  1798. totlen = maxlen + 13;
  1799. fprintf(stderr, "\n %*s", maxlen + 13, " ");
  1800. }
  1801. fprintf(stderr, "%s,", p1);
  1802. totlen += len;
  1803. p1 = p2 + 1;
  1804. }
  1805. len = strlen(p1);
  1806. if ((len + totlen) > 76)
  1807. fprintf(stderr, "\n %*s", maxlen + 13, " ");
  1808. fprintf(stderr, "%s\n", p1);
  1809. G_free(buff);
  1810. return 0;
  1811. }
  1812. static int show(const char *item, int len)
  1813. {
  1814. int n;
  1815. n = strlen(item) + (len > 0);
  1816. if (n + len > 76) {
  1817. if (len)
  1818. fprintf(stderr, "\n ");
  1819. len = 0;
  1820. }
  1821. fprintf(stderr, "%s", item);
  1822. return n + len;
  1823. }
  1824. static int set_flag(int f)
  1825. {
  1826. struct Flag *flag;
  1827. /* Flag is not valid if there are no flags to set */
  1828. if (!n_flags) {
  1829. fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
  1830. return (1);
  1831. }
  1832. /* Find flag with corrrect keyword */
  1833. flag = &first_flag;
  1834. while (flag != NULL) {
  1835. if (flag->key == f) {
  1836. flag->answer = 1;
  1837. return (0);
  1838. }
  1839. flag = flag->next_flag;
  1840. }
  1841. fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
  1842. return (1);
  1843. }
  1844. /* contents() is used to find things strings with characters like commas and
  1845. * dashes.
  1846. */
  1847. static int contains(const char *s, int c)
  1848. {
  1849. while (*s) {
  1850. if (*s == c)
  1851. return (1);
  1852. s++;
  1853. }
  1854. return (0);
  1855. }
  1856. static int is_option(const char *string)
  1857. {
  1858. int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
  1859. return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
  1860. }
  1861. static int set_option(char *string)
  1862. {
  1863. struct Option *at_opt = NULL;
  1864. struct Option *opt = NULL;
  1865. int got_one;
  1866. size_t key_len;
  1867. char the_key[KEYLENGTH];
  1868. char *ptr;
  1869. for (ptr = the_key; *string != '='; ptr++, string++)
  1870. *ptr = *string;
  1871. *ptr = '\0';
  1872. string++;
  1873. /* Find option with best keyword match */
  1874. got_one = 0;
  1875. key_len = strlen(the_key);
  1876. for (at_opt = &first_option; at_opt != NULL; at_opt = at_opt->next_opt) {
  1877. if (at_opt->key == NULL || strncmp(the_key, at_opt->key, key_len))
  1878. continue;
  1879. got_one++;
  1880. opt = at_opt;
  1881. /* changed 1/15/91 -dpg old code is in parser.old */
  1882. /* overide ambiguous check, if we get an exact match */
  1883. if (strlen(at_opt->key) == key_len) {
  1884. opt = at_opt;
  1885. got_one = 1;
  1886. break;
  1887. }
  1888. }
  1889. if (got_one > 1) {
  1890. fprintf(stderr, _("Sorry, <%s=> is ambiguous\n"), the_key);
  1891. return (1);
  1892. }
  1893. /* If there is no match, complain */
  1894. if (got_one == 0) {
  1895. fprintf(stderr, _("Sorry, <%s> is not a valid parameter\n"), the_key);
  1896. return (1);
  1897. }
  1898. /* Allocate memory where answer is stored */
  1899. if (opt->count++) {
  1900. opt->answer = (char *)G_realloc(opt->answer,
  1901. strlen(opt->answer) + strlen(string) +
  1902. 2);
  1903. strcat(opt->answer, ",");
  1904. strcat(opt->answer, string);
  1905. }
  1906. else
  1907. opt->answer = G_store(string);
  1908. return (0);
  1909. }
  1910. static int check_opts(void)
  1911. {
  1912. struct Option *opt;
  1913. int error;
  1914. int ans;
  1915. error = 0;
  1916. if (!n_opts)
  1917. return (0);
  1918. opt = &first_option;
  1919. while (opt != NULL) {
  1920. /* Check answer against options if any */
  1921. if (opt->options && opt->answer) {
  1922. if (opt->multiple == 0)
  1923. error += check_an_opt(opt->key, opt->type,
  1924. opt->options, opt->opts, &opt->answer);
  1925. else {
  1926. for (ans = 0; opt->answers[ans] != '\0'; ans++)
  1927. error += check_an_opt(opt->key, opt->type,
  1928. opt->options, opt->opts, &opt->answers[ans]);
  1929. }
  1930. }
  1931. /* Check answer against user's check subroutine if any */
  1932. if (opt->checker)
  1933. error += opt->checker(opt->answer);
  1934. opt = opt->next_opt;
  1935. }
  1936. return (error);
  1937. }
  1938. static int check_an_opt(const char *key, int type, const char *options,
  1939. const char **opts, char **answerp)
  1940. {
  1941. const char *answer = *answerp;
  1942. int error;
  1943. int found;
  1944. error = 0;
  1945. switch (type) {
  1946. case TYPE_INTEGER:
  1947. error = check_int(answer, opts);
  1948. break;
  1949. case TYPE_DOUBLE:
  1950. error = check_double(answer, opts);
  1951. break;
  1952. case TYPE_STRING:
  1953. error = check_string(answer, opts, &found);
  1954. break;
  1955. }
  1956. switch (error) {
  1957. case 0:
  1958. break;
  1959. case BAD_SYNTAX:
  1960. fprintf(stderr,
  1961. _("\nERROR: illegal range syntax for parameter <%s>\n"), key);
  1962. fprintf(stderr, _(" Presented as: %s\n"), options);
  1963. break;
  1964. case OUT_OF_RANGE:
  1965. fprintf(stderr,
  1966. _("\nERROR: value <%s> out of range for parameter <%s>\n"),
  1967. answer, key);
  1968. fprintf(stderr, _(" Legal range: %s\n"), options);
  1969. break;
  1970. case MISSING_VALUE:
  1971. fprintf(stderr, _("\nERROR: Missing value for parameter <%s>\n"),
  1972. key);
  1973. break;
  1974. case AMBIGUOUS:
  1975. fprintf(stderr, _("\nERROR: value <%s> ambiguous for parameter <%s>\n"),
  1976. answer, key);
  1977. fprintf(stderr, _(" valid options: %s\n"), options);
  1978. break;
  1979. case REPLACED:
  1980. *answerp = G_store(opts[found]);
  1981. error = 0;
  1982. break;
  1983. }
  1984. return error;
  1985. }
  1986. static int check_int(const char *ans, const char **opts)
  1987. {
  1988. int d, i;
  1989. if (sscanf(ans, "%d", &d) != 1)
  1990. return MISSING_VALUE;
  1991. for (i = 0; opts[i]; i++) {
  1992. const char *opt = opts[i];
  1993. int lo, hi;
  1994. if (contains(opt, '-')) {
  1995. if (sscanf(opt, "%d-%d", &lo, &hi) == 2) {
  1996. if (d >= lo && d <= hi)
  1997. return 0;
  1998. }
  1999. else if (sscanf(opt, "-%d", &hi) == 1) {
  2000. if (d <= hi)
  2001. return 0;
  2002. }
  2003. else if (sscanf(opt, "%d-", &lo) == 1) {
  2004. if (d >= lo)
  2005. return 0;
  2006. }
  2007. else
  2008. return BAD_SYNTAX;
  2009. }
  2010. else {
  2011. if (sscanf(opt, "%d", &lo) == 1) {
  2012. if (d == lo)
  2013. return 0;
  2014. }
  2015. else
  2016. return BAD_SYNTAX;
  2017. }
  2018. }
  2019. return OUT_OF_RANGE;
  2020. }
  2021. static int check_double(const char *ans, const char **opts)
  2022. {
  2023. double d;
  2024. int i;
  2025. if (sscanf(ans, "%lf", &d) != 1)
  2026. return MISSING_VALUE;
  2027. for (i = 0; opts[i]; i++) {
  2028. const char *opt = opts[i];
  2029. double lo, hi;
  2030. if (contains(opt, '-')) {
  2031. if (sscanf(opt, "%lf-%lf", &lo, &hi) == 2) {
  2032. if (d >= lo && d <= hi)
  2033. return 0;
  2034. }
  2035. else if (sscanf(opt, "-%lf", &hi) == 1) {
  2036. if (d <= hi)
  2037. return 0;
  2038. }
  2039. else if (sscanf(opt, "%lf-", &lo) == 1) {
  2040. if (d >= lo)
  2041. return 0;
  2042. }
  2043. else
  2044. return BAD_SYNTAX;
  2045. }
  2046. else {
  2047. if (sscanf(opt, "%lf", &lo) == 1) {
  2048. if (d == lo)
  2049. return 0;
  2050. }
  2051. else
  2052. return BAD_SYNTAX;
  2053. }
  2054. }
  2055. return OUT_OF_RANGE;
  2056. }
  2057. static int check_string(const char *ans, const char **opts, int *result)
  2058. {
  2059. int len = strlen(ans);
  2060. int found = 0;
  2061. int i;
  2062. for (i = 0; opts[i]; i++) {
  2063. if (strcmp(ans, opts[i]) == 0)
  2064. return 0;
  2065. if (strncmp(ans, opts[i], len) == 0) {
  2066. *result = i;
  2067. found++;
  2068. }
  2069. }
  2070. switch (found) {
  2071. case 0: return OUT_OF_RANGE;
  2072. case 1: return REPLACED;
  2073. default: return AMBIGUOUS;
  2074. }
  2075. }
  2076. static int check_required(void)
  2077. {
  2078. struct Option *opt;
  2079. int err;
  2080. err = 0;
  2081. if (!n_opts)
  2082. return (0);
  2083. opt = &first_option;
  2084. while (opt != NULL) {
  2085. if (opt->required && opt->answer == NULL) {
  2086. fprintf(stderr,
  2087. _("\nERROR: Required parameter <%s> not set:\n (%s).\n"),
  2088. opt->key, opt->description);
  2089. err++;
  2090. }
  2091. opt = opt->next_opt;
  2092. }
  2093. return (err);
  2094. }
  2095. static int split_opts(void)
  2096. {
  2097. struct Option *opt;
  2098. char *ptr1;
  2099. char *ptr2;
  2100. int allocated;
  2101. int ans_num;
  2102. int len;
  2103. if (!n_opts)
  2104. return 0;
  2105. opt = &first_option;
  2106. while (opt != NULL) {
  2107. if ( /*opt->multiple && */ (opt->answer != NULL)) {
  2108. /* Allocate some memory to store array of pointers */
  2109. allocated = 10;
  2110. opt->answers = (char **)G_malloc(allocated * sizeof(char *));
  2111. ans_num = 0;
  2112. ptr1 = opt->answer;
  2113. opt->answers[ans_num] = NULL;
  2114. for (;;) {
  2115. for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
  2116. ptr2++, len++) ;
  2117. if (len > 0) { /* skip ,, */
  2118. opt->answers[ans_num] = (char *)G_malloc(len + 1);
  2119. G_copy(opt->answers[ans_num], ptr1, len);
  2120. opt->answers[ans_num][len] = 0;
  2121. ans_num++;
  2122. if (ans_num >= allocated) {
  2123. allocated += 10;
  2124. opt->answers =
  2125. (char **)G_realloc((char *)opt->answers,
  2126. allocated * sizeof(char *));
  2127. }
  2128. opt->answers[ans_num] = NULL;
  2129. }
  2130. if (*ptr2 == '\0')
  2131. break;
  2132. ptr1 = ptr2 + 1;
  2133. if (*ptr1 == '\0')
  2134. break;
  2135. }
  2136. }
  2137. opt = opt->next_opt;
  2138. }
  2139. return 0;
  2140. }
  2141. static int check_multiple_opts(void)
  2142. {
  2143. struct Option *opt;
  2144. const char *ptr;
  2145. int n_commas;
  2146. int n;
  2147. int error;
  2148. if (!n_opts)
  2149. return (0);
  2150. error = 0;
  2151. opt = &first_option;
  2152. while (opt != NULL) {
  2153. if ((opt->answer != NULL) && (opt->key_desc != NULL)) {
  2154. /* count commas */
  2155. n_commas = 1;
  2156. for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
  2157. if (*ptr == ',')
  2158. n_commas++;
  2159. /* count items */
  2160. for (n = 0; opt->answers[n] != '\0'; n++) ;
  2161. /* if not correct multiple of items */
  2162. if (n % n_commas) {
  2163. fprintf(stderr,
  2164. _("\nERROR: option <%s> must be provided in multiples of %d\n"),
  2165. opt->key, n_commas);
  2166. fprintf(stderr, _(" You provided %d items:\n"), n);
  2167. fprintf(stderr, " %s\n", opt->answer);
  2168. error++;
  2169. }
  2170. }
  2171. opt = opt->next_opt;
  2172. }
  2173. return (error);
  2174. }
  2175. /* Check for all 'new' if element already exists */
  2176. static int check_overwrite(void)
  2177. {
  2178. struct Option *opt;
  2179. char age[KEYLENGTH];
  2180. char element[KEYLENGTH];
  2181. char desc[KEYLENGTH];
  2182. int error = 0;
  2183. char *overstr;
  2184. int over;
  2185. module_info.overwrite = 0;
  2186. if (!n_opts)
  2187. return (0);
  2188. over = 0;
  2189. /* Check the GRASS OVERWRITE variable */
  2190. if ((overstr = G__getenv("OVERWRITE"))) {
  2191. over = atoi(overstr);
  2192. }
  2193. /* Check the GRASS_OVERWRITE environment variable */
  2194. if ((overstr = getenv("GRASS_OVERWRITE"))) {
  2195. if (atoi(overstr))
  2196. over = 1;
  2197. }
  2198. if (overwrite || over) {
  2199. module_info.overwrite = 1;
  2200. /* Set the environment so that programs run in a script also obey --o */
  2201. putenv("GRASS_OVERWRITE=1");
  2202. /* No need to check options for existing files if overwrite is true */
  2203. return error;
  2204. }
  2205. opt = &first_option;
  2206. while (opt != NULL) {
  2207. if ((opt->answer != NULL) && (opt->gisprompt != NULL)) {
  2208. split_gisprompt(opt->gisprompt, age, element, desc);
  2209. if (strcmp(age, "new") == 0) {
  2210. int i;
  2211. for (i = 0; opt->answers[i]; i++) {
  2212. if (G_find_file(element, opt->answers[i], G_mapset())) { /* found */
  2213. if (!overwrite && !over) {
  2214. if (G_info_format() != G_INFO_FORMAT_GUI) {
  2215. fprintf(stderr,
  2216. _("ERROR: option <%s>: <%s> exists.\n"),
  2217. opt->key, opt->answers[i]);
  2218. }
  2219. else {
  2220. fprintf(stderr,
  2221. "GRASS_INFO_ERROR(%d,1): option <%s>: <%s> exists.\n",
  2222. getpid(), opt->key, opt->answers[i]);
  2223. fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
  2224. getpid());
  2225. }
  2226. error = 1;
  2227. }
  2228. }
  2229. }
  2230. }
  2231. }
  2232. opt = opt->next_opt;
  2233. }
  2234. return (error);
  2235. }
  2236. static int split_gisprompt(const char *gisprompt, char *age, char *element,
  2237. char *desc)
  2238. {
  2239. const char *ptr1;
  2240. char *ptr2;
  2241. for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
  2242. if (*ptr1 == ',')
  2243. break;
  2244. *ptr2 = *ptr1;
  2245. }
  2246. *ptr2 = '\0';
  2247. for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
  2248. if (*ptr1 == ',')
  2249. break;
  2250. *ptr2 = *ptr1;
  2251. }
  2252. *ptr2 = '\0';
  2253. for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
  2254. if (*ptr1 == ',')
  2255. break;
  2256. *ptr2 = *ptr1;
  2257. }
  2258. *ptr2 = '\0';
  2259. return 0;
  2260. }
  2261. /**
  2262. * \brief Creates command to run non-interactive.
  2263. *
  2264. * Creates a command-line that runs the current command completely
  2265. * non-interactive.
  2266. *
  2267. * \return char * Pointer to a char string
  2268. */
  2269. char *G_recreate_command(void)
  2270. {
  2271. static char *buff;
  2272. char flg[4];
  2273. char *cur;
  2274. const char *tmp;
  2275. struct Flag *flag;
  2276. struct Option *opt;
  2277. int n, len, slen;
  2278. int nalloced = 0;
  2279. G_debug(3, "G_recreate_command()");
  2280. /* Flag is not valid if there are no flags to set */
  2281. buff = G_calloc(1024, sizeof(char));
  2282. nalloced += 1024;
  2283. tmp = G_program_name();
  2284. len = strlen(tmp);
  2285. if (len >= nalloced) {
  2286. nalloced += (1024 > len) ? 1024 : len + 1;
  2287. buff = G_realloc(buff, nalloced);
  2288. }
  2289. cur = buff;
  2290. strcpy(cur, tmp);
  2291. cur += len;
  2292. if (n_flags) {
  2293. flag = &first_flag;
  2294. while (flag != '\0') {
  2295. if (flag->answer == 1) {
  2296. flg[0] = ' ';
  2297. flg[1] = '-';
  2298. flg[2] = flag->key;
  2299. flg[3] = '\0';
  2300. slen = strlen(flg);
  2301. if (len + slen >= nalloced) {
  2302. nalloced +=
  2303. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  2304. buff = G_realloc(buff, nalloced);
  2305. cur = buff + len;
  2306. }
  2307. strcpy(cur, flg);
  2308. cur += slen;
  2309. len += slen;
  2310. }
  2311. flag = flag->next_flag;
  2312. }
  2313. }
  2314. opt = &first_option;
  2315. while (opt != '\0') {
  2316. if (opt->answer != '\0' && opt->answers[0] != NULL) {
  2317. slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
  2318. if (len + slen >= nalloced) {
  2319. nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  2320. buff = G_realloc(buff, nalloced);
  2321. cur = buff + len;
  2322. }
  2323. strcpy(cur, " ");
  2324. cur++;
  2325. strcpy(cur, opt->key);
  2326. cur = strchr(cur, '\0');
  2327. strcpy(cur, "=");
  2328. cur++;
  2329. if (opt->type == TYPE_STRING) {
  2330. strcpy(cur, "\"");
  2331. cur++;
  2332. }
  2333. strcpy(cur, opt->answers[0]);
  2334. cur = strchr(cur, '\0');
  2335. len = cur - buff;
  2336. for (n = 1; opt->answers[n] != NULL && opt->answers[n] != '\0';
  2337. n++) {
  2338. if (opt->answers[n] == NULL)
  2339. break;
  2340. slen = strlen(opt->answers[n]) + 2; /* +2 for , " */
  2341. if (len + slen >= nalloced) {
  2342. nalloced +=
  2343. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  2344. buff = G_realloc(buff, nalloced);
  2345. cur = buff + len;
  2346. }
  2347. strcpy(cur, ",");
  2348. cur++;
  2349. strcpy(cur, opt->answers[n]);
  2350. cur = strchr(cur, '\0');
  2351. len = cur - buff;
  2352. }
  2353. if (opt->type == TYPE_STRING) {
  2354. strcpy(cur, "\"");
  2355. cur++;
  2356. len = cur - buff;
  2357. }
  2358. }
  2359. opt = opt->next_opt;
  2360. }
  2361. return (buff);
  2362. }