parser.c 65 KB

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