parser.c 61 KB

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