parser.c 63 KB

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