parser.c 62 KB

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