parser.c 64 KB

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