parser.c 62 KB

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