parser.c 60 KB

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