parser.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  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. opt2 = G_define_option() ;
  17. opt2->key = "color",
  18. opt2->type = TYPE_STRING,
  19. opt2->required = NO,
  20. opt2->answer = "white",
  21. opt2->options = "red,orange,blue,white,black",
  22. opt2->description= "Color used to display the map" ;
  23. opt3 = G_define_option() ;
  24. opt3->key = "number",
  25. opt3->type = TYPE_DOUBLE,
  26. opt3->required = NO,
  27. opt3->answer = "12345.67",
  28. opt3->options = "0-99999",
  29. opt3->description= "Number to test parser" ;
  30. \endcode
  31. *
  32. * G_parser() will respond to the following command lines as described:
  33. *
  34. \verbatim
  35. command (No command line arguments)
  36. \endverbatim
  37. * Parser enters interactive mode.
  38. *
  39. \verbatim
  40. command map=map.name
  41. \endverbatim
  42. * Parser will accept this line. Map will be set to "map.name", the
  43. * 'a' and 'b' flags will remain off and the num option will be set
  44. * to the default of 5.
  45. *
  46. \verbatim
  47. command -ab map=map.name num=9
  48. command -a -b map=map.name num=9
  49. command -ab map.name num=9
  50. command map.name num=9 -ab
  51. command num=9 -a map=map.name -b
  52. \endverbatim
  53. * These are all treated as acceptable and identical. Both flags are
  54. * set to on, the map option is "map.name" and the num option is "9".
  55. * Note that the "map=" may be omitted from the command line if it
  56. * is part of the first option (flags do not count).
  57. *
  58. \verbatim
  59. command num=12
  60. \endverbatim
  61. * This command line is in error in two ways. The user will be told
  62. * that the "map" option is required and also that the number 12 is
  63. * out of range. The acceptable range (or list) will be printed.
  64. *
  65. * The exclusive member of the Option and Flag structures is a comma-separated
  66. * string. Whitespaces are not ignored. Each name separated by comma can be
  67. * used to group options/flags together, make them mutually exclusive, or make
  68. * one of them conditionally required. Names starting with "+" tie together
  69. * options/flags and names starting with "*" (name ignored) make them
  70. * conditionally required (not always required, but if some other options/flags
  71. * are not used, they become required). Other names make options/flags mutually
  72. * exclusive in the same group. These three different types of grouping can be
  73. * mixed. G_parser() raises a fatal error if any violations are found.
  74. *
  75. * Examples
  76. *
  77. * 1. opt1 & opt2 are mutually exclusive and opt2 & opt3 are mutually exclusive.
  78. *
  79. \code
  80. opt1->exclusive = "1";
  81. opt2->exclusive = "1,2";
  82. opt3->exclusive = "2";
  83. \endcode
  84. *
  85. * 2. opt1 & opt2 must be used together.
  86. *
  87. \code
  88. opt1->exclusive = "+1";
  89. opt2->exclusive = "+1";
  90. opt3->exclusive = "";
  91. \endcode
  92. *
  93. * 3. opt1 or opt2 must be used. Both can be used together. Naming ignored.
  94. *
  95. \code
  96. opt1->exclusive = "*ignored";
  97. opt2->exclusive = "*";
  98. opt3->exclusive = "";
  99. \endcode
  100. *
  101. * 4. (opt1 & opt2 together) or (opt3 & opt4 together) must be used. All four
  102. * can be used together.
  103. *
  104. \code
  105. opt1->exclusive = "+1,*";
  106. opt2->exclusive = "+1"; // * is optional because opt2 is tied with opt1
  107. opt3->exclusive = "+2,*";
  108. opt4->exclusive = "+2";
  109. \endcode
  110. *
  111. * 5. Only one of (opt1 & opt2 together) or (opt3 & opt4 together) must be used.
  112. * All four cannot be used together.
  113. *
  114. \code
  115. opt1->exclusive = "+1,*,1";
  116. opt2->exclusive = "+1"; // * is optional because opt2 is tied with opt1
  117. opt3->exclusive = "+2,*,1";
  118. opt4->exclusive = "+2"; // 1 is optional because opt4 is tied with opt3
  119. \endcode
  120. *
  121. *
  122. * (C) 2001-2009, 2011-2014 by the GRASS Development Team
  123. *
  124. * This program is free software under the GNU General Public License
  125. * (>=v2). Read the file COPYING that comes with GRASS for details.
  126. *
  127. * \author Original author CERL
  128. * \author Soeren Gebbert added Dec. 2009 WPS process_description document
  129. */
  130. #include <stdio.h>
  131. #include <stdlib.h>
  132. #include <string.h>
  133. #include <unistd.h>
  134. #include <grass/gis.h>
  135. #include <grass/spawn.h>
  136. #include <grass/glocale.h>
  137. #include "parser_local_proto.h"
  138. enum opt_error {
  139. BAD_SYNTAX = 1,
  140. OUT_OF_RANGE = 2,
  141. MISSING_VALUE = 3,
  142. AMBIGUOUS = 4,
  143. REPLACED = 5
  144. };
  145. #define KEYLENGTH 64
  146. /* initialize the global struct */
  147. struct state state;
  148. struct state *st = &state;
  149. /* local prototypes */
  150. static void set_flag(int);
  151. static int contains(const char *, int);
  152. static int is_option(const char *);
  153. static void set_option(const char *);
  154. static void check_opts(void);
  155. static void check_an_opt(const char *, int, const char *, const char **, char **);
  156. static int check_int(const char *, const char **);
  157. static int check_double(const char *, const char **);
  158. static int check_string(const char *, const char **, int *);
  159. static void check_required(void);
  160. static void split_opts(void);
  161. static void check_multiple_opts(void);
  162. static int check_overwrite(void);
  163. static void define_keywords(void);
  164. static void split_gisprompt(const char *, char *, char *, char *);
  165. static void module_gui_wx(void);
  166. static void add_exclusive(const char *, int, const char *);
  167. static struct Exclusive *find_exclusive(char *);
  168. static int has_exclusive_name(const char *, char *);
  169. static int has_exclusive_key(int, char **, char *);
  170. static int has_either_or(const char *);
  171. static void check_exclusive();
  172. static void check_mutually_exclusive_inputs();
  173. static void check_tied_together_inputs();
  174. static void check_either_or_inputs();
  175. static void append_error(const char *);
  176. /*!
  177. * \brief Disables the ability of the parser to operate interactively.
  178. *
  179. * When a user calls a command with no arguments on the command line,
  180. * the parser will enter its own standardized interactive session in
  181. * which all flags and options are presented to the user for input. A
  182. * call to G_disable_interactive() disables the parser's interactive
  183. * prompting.
  184. *
  185. */
  186. void G_disable_interactive(void)
  187. {
  188. st->no_interactive = 1;
  189. }
  190. /*!
  191. * \brief Initializes a Flag struct.
  192. *
  193. * Allocates memory for the Flag structure and returns a pointer to
  194. * this memory.
  195. *
  196. * Flags are always represented by single letters. A user "turns them
  197. * on" at the command line using a minus sign followed by the
  198. * character representing the flag.
  199. *
  200. * \return Pointer to a Flag struct
  201. */
  202. struct Flag *G_define_flag(void)
  203. {
  204. struct Flag *flag;
  205. struct Item *item;
  206. /* Allocate memory if not the first flag */
  207. if (st->n_flags) {
  208. flag = G_malloc(sizeof(struct Flag));
  209. st->current_flag->next_flag = flag;
  210. }
  211. else
  212. flag = &st->first_flag;
  213. /* Zero structure */
  214. G_zero(flag, sizeof(struct Flag));
  215. st->current_flag = flag;
  216. st->n_flags++;
  217. if (st->n_items) {
  218. item = G_malloc(sizeof(struct Item));
  219. st->current_item->next_item = item;
  220. }
  221. else
  222. item = &st->first_item;
  223. G_zero(item, sizeof(struct Item));
  224. item->flag = flag;
  225. item->option = NULL;
  226. st->current_item = item;
  227. st->n_items++;
  228. return (flag);
  229. }
  230. /*!
  231. * \brief Initializes an Option struct.
  232. *
  233. * Allocates memory for the Option structure and returns a pointer to
  234. * this memory.
  235. *
  236. * Options are provided by user on command line using the standard
  237. * format: <i>key=value</i>. Options identified as REQUIRED must be
  238. * specified by user on command line. The option string can either
  239. * specify a range of values (e.g. "10-100") or a list of acceptable
  240. * values (e.g. "red,orange,yellow"). Unless the option string is
  241. * NULL, user provided input will be evaluated agaist this string.
  242. *
  243. * \return pointer to an Option struct
  244. */
  245. struct Option *G_define_option(void)
  246. {
  247. struct Option *opt;
  248. struct Item *item;
  249. /* Allocate memory if not the first option */
  250. if (st->n_opts) {
  251. opt = G_malloc(sizeof(struct Option));
  252. st->current_option->next_opt = opt;
  253. }
  254. else
  255. opt = &st->first_option;
  256. /* Zero structure */
  257. G_zero(opt, sizeof(struct Option));
  258. opt->required = NO;
  259. opt->multiple = NO;
  260. st->current_option = opt;
  261. st->n_opts++;
  262. if (st->n_items) {
  263. item = G_malloc(sizeof(struct Item));
  264. st->current_item->next_item = item;
  265. }
  266. else
  267. item = &st->first_item;
  268. G_zero(item, sizeof(struct Item));
  269. item->option = opt;
  270. st->current_item = item;
  271. st->n_items++;
  272. return (opt);
  273. }
  274. /*!
  275. * \brief Initializes a new module.
  276. *
  277. * \return pointer to a GModule struct
  278. */
  279. struct GModule *G_define_module(void)
  280. {
  281. struct GModule *module;
  282. /* Allocate memory */
  283. module = &st->module_info;
  284. /* Zero structure */
  285. G_zero(module, sizeof(struct GModule));
  286. /* Allocate keywords array */
  287. define_keywords();
  288. return (module);
  289. }
  290. /*!
  291. * \brief Parse command line.
  292. *
  293. * The command line parameters <i>argv</i> and the number of
  294. * parameters <i>argc</i> from the main() routine are passed directly
  295. * to G_parser(). G_parser() accepts the command line input entered by
  296. * the user, and parses this input according to the input options
  297. * and/or flags that were defined by the programmer.
  298. *
  299. * <b>Note:</b> The only functions which can legitimately be called
  300. * before G_parser() are:
  301. *
  302. * - G_gisinit()
  303. * - G_no_gisinit()
  304. * - G_define_module()
  305. * - G_define_flag()
  306. * - G_define_option()
  307. * - G_define_standard_option()
  308. * - G_disable_interactive()
  309. *
  310. * The usual order a module calls functions is:
  311. *
  312. * # G_gisinit()
  313. * # G_define_module()
  314. * # G_define_flag()
  315. * # G_define_option()
  316. * # G_parser()
  317. *
  318. * \param argc number of arguments
  319. * \param argv argument list
  320. *
  321. * \return 0 on success
  322. * \return -1 on error and calls G_usage()
  323. */
  324. int G_parser(int argc, char **argv)
  325. {
  326. int need_first_opt;
  327. int opt_checked = 0;
  328. char *ptr, *tmp_name, *err;
  329. int i;
  330. struct Option *opt;
  331. char force_gui = FALSE;
  332. err = NULL;
  333. need_first_opt = 1;
  334. tmp_name = G_store(argv[0]);
  335. st->pgm_path = tmp_name;
  336. st->n_errors = 0;
  337. st->error = NULL;
  338. i = strlen(tmp_name);
  339. while (--i >= 0) {
  340. if (G_is_dirsep(tmp_name[i])) {
  341. tmp_name += i + 1;
  342. break;
  343. }
  344. }
  345. G_basename(tmp_name, "exe");
  346. st->pgm_name = tmp_name;
  347. st->allocated_exclusive = 10;
  348. st->n_exclusive = 0;
  349. st->exclusive = G_malloc(st->allocated_exclusive *
  350. sizeof(struct Exclusive));
  351. /* Stash default answers */
  352. opt = &st->first_option;
  353. while (opt) {
  354. if (opt->required)
  355. st->has_required = 1;
  356. /* Parse options */
  357. if (opt->options) {
  358. int cnt = 0;
  359. char **tokens, delm[2];
  360. delm[0] = ',';
  361. delm[1] = '\0';
  362. tokens = G_tokenize(opt->options, delm);
  363. i = 0;
  364. while (tokens[i]) {
  365. cnt++;
  366. i++;
  367. }
  368. opt->opts = G_calloc(cnt + 1, sizeof(const char *));
  369. i = 0;
  370. while (tokens[i]) {
  371. opt->opts[i] = G_store(tokens[i]);
  372. i++;
  373. }
  374. G_free_tokens(tokens);
  375. if (opt->descriptions) {
  376. delm[0] = ';';
  377. opt->descs = G_calloc(cnt + 1, sizeof(const char *));
  378. tokens = G_tokenize(opt->descriptions, delm);
  379. i = 0;
  380. while (tokens[i]) {
  381. int j, found;
  382. if (!tokens[i + 1])
  383. break;
  384. j = 0;
  385. found = 0;
  386. while (opt->opts[j]) {
  387. if (strcmp(opt->opts[j], tokens[i]) == 0) {
  388. found = 1;
  389. break;
  390. }
  391. j++;
  392. }
  393. if (!found) {
  394. G_warning(_("BUG in descriptions, option '%s' in <%s> does not exist"),
  395. tokens[i], opt->key);
  396. }
  397. else {
  398. opt->descs[j] = G_store(tokens[i + 1]);
  399. }
  400. i += 2;
  401. }
  402. G_free_tokens(tokens);
  403. }
  404. }
  405. /* Copy answer */
  406. if (opt->multiple && opt->answers && opt->answers[0]) {
  407. opt->answer = G_malloc(strlen(opt->answers[0]) + 1);
  408. strcpy(opt->answer, opt->answers[0]);
  409. for (i = 1; opt->answers[i]; i++) {
  410. opt->answer = G_realloc(opt->answer,
  411. strlen(opt->answer) +
  412. strlen(opt->answers[i]) + 2);
  413. strcat(opt->answer, ",");
  414. strcat(opt->answer, opt->answers[i]);
  415. }
  416. }
  417. opt->def = opt->answer;
  418. opt = opt->next_opt;
  419. }
  420. /* If there are NO arguments, go interactive */
  421. if (argc < 2 && st->has_required && !st->no_interactive && isatty(0)) {
  422. module_gui_wx();
  423. return -1;
  424. }
  425. else if (argc < 2 && st->has_required && isatty(0)) {
  426. G_usage();
  427. return -1;
  428. }
  429. else if (argc >= 2) {
  430. /* If first arg is "help" give a usage/syntax message */
  431. if (strcmp(argv[1], "help") == 0 ||
  432. strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0) {
  433. G_usage();
  434. exit(EXIT_SUCCESS);
  435. }
  436. /* If first arg is "--help-text" give a usage/syntax message
  437. * with machine-readable sentinels */
  438. if (strcmp(argv[1], "--help-text") == 0) {
  439. G__usage_text();
  440. exit(EXIT_SUCCESS);
  441. }
  442. /* If first arg is "--interface-description" then print out
  443. * a xml description of the task */
  444. if (strcmp(argv[1], "--interface-description") == 0) {
  445. G__usage_xml();
  446. exit(EXIT_SUCCESS);
  447. }
  448. /* If first arg is "--html-description" then print out
  449. * a html description of the task */
  450. if (strcmp(argv[1], "--html-description") == 0) {
  451. G__usage_html();
  452. exit(EXIT_SUCCESS);
  453. }
  454. /* If first arg is "--rest-description" then print out
  455. * a ReST description of the task */
  456. if (strcmp(argv[1], "--rest-description") == 0) {
  457. G__usage_rest();
  458. exit(EXIT_SUCCESS);
  459. }
  460. /* If first arg is "--wps-process-description" then print out
  461. * the wps process description of the task */
  462. if (strcmp(argv[1], "--wps-process-description") == 0) {
  463. G__wps_print_process_description();
  464. exit(EXIT_SUCCESS);
  465. }
  466. /* If first arg is "--script" then then generate
  467. * g.parser boilerplate */
  468. if (strcmp(argv[1], "--script") == 0) {
  469. G__script();
  470. exit(EXIT_SUCCESS);
  471. }
  472. /* Loop thru all command line arguments */
  473. while (--argc) {
  474. ptr = *(++argv);
  475. if (strcmp(ptr, "help") == 0 || strcmp(ptr, "--h") == 0 ||
  476. strcmp(ptr, "-help") == 0 || strcmp(ptr, "--help") == 0) {
  477. G_usage();
  478. exit(EXIT_SUCCESS);
  479. }
  480. /* Overwrite option */
  481. if (strcmp(ptr, "--o") == 0 || strcmp(ptr, "--overwrite") == 0) {
  482. st->overwrite = 1;
  483. }
  484. /* Verbose option */
  485. else if (strcmp(ptr, "--v") == 0 || strcmp(ptr, "--verbose") == 0) {
  486. char buff[32];
  487. /* print everything: max verbosity level */
  488. st->module_info.verbose = G_verbose_max();
  489. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_max());
  490. putenv(G_store(buff));
  491. if (st->quiet == 1) {
  492. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --verbose."));
  493. }
  494. st->quiet = -1;
  495. }
  496. /* Quiet option */
  497. else if (strcmp(ptr, "--q") == 0 || strcmp(ptr, "--quiet") == 0) {
  498. char buff[32];
  499. /* print nothing, but errors and warnings */
  500. st->module_info.verbose = G_verbose_min();
  501. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_min());
  502. putenv(G_store(buff));
  503. if (st->quiet == -1) {
  504. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --quiet."));
  505. }
  506. st->quiet = 1; /* for passing to gui init */
  507. }
  508. /* Force gui to come up */
  509. else if (strcmp(ptr, "--ui") == 0) {
  510. force_gui = TRUE;
  511. }
  512. /* If we see a flag */
  513. else if (*ptr == '-') {
  514. while (*(++ptr))
  515. set_flag(*ptr);
  516. }
  517. /* If we see standard option format (option=val) */
  518. else if (is_option(ptr)) {
  519. set_option(ptr);
  520. need_first_opt = 0;
  521. }
  522. /* If we see the first option with no equal sign */
  523. else if (need_first_opt && st->n_opts) {
  524. st->first_option.answer = G_store(ptr);
  525. st->first_option.count++;
  526. need_first_opt = 0;
  527. add_exclusive(st->first_option.key, 0,
  528. st->first_option.exclusive);
  529. }
  530. /* If we see the non valid argument (no "=", just argument) */
  531. else {
  532. G_asprintf(&err, _("Sorry, <%s> is not a valid option"), ptr);
  533. append_error(err);
  534. }
  535. }
  536. check_exclusive();
  537. }
  538. /* Split options where multiple answers are OK */
  539. split_opts();
  540. /* Run the gui if it was specifically requested */
  541. if (force_gui) {
  542. module_gui_wx();
  543. return -1;
  544. }
  545. /* Check multiple options */
  546. check_multiple_opts();
  547. /* Check answers against options and check subroutines */
  548. if (!opt_checked)
  549. check_opts();
  550. /* Make sure all required options are set */
  551. if (!st->suppress_required)
  552. check_required();
  553. if (st->n_errors > 0) {
  554. if (G_verbose() > -1) {
  555. if (G_verbose() > G_verbose_min())
  556. G_usage();
  557. fprintf(stderr, "\n");
  558. for (i = 0; i < st->n_errors; i++) {
  559. fprintf(stderr, "%s: %s\n", _("ERROR"), st->error[i]);
  560. }
  561. }
  562. return -1;
  563. }
  564. if (check_overwrite())
  565. return -1;
  566. return 0;
  567. }
  568. /*!
  569. * \brief Creates command to run non-interactive.
  570. *
  571. * Creates a command-line that runs the current command completely
  572. * non-interactive.
  573. *
  574. * \return pointer to a char string
  575. */
  576. char *G_recreate_command(void)
  577. {
  578. char *buff;
  579. char flg[4];
  580. char *cur;
  581. const char *tmp;
  582. struct Flag *flag;
  583. struct Option *opt;
  584. int n, len, slen;
  585. int nalloced = 0;
  586. G_debug(3, "G_recreate_command()");
  587. /* Flag is not valid if there are no flags to set */
  588. buff = G_calloc(1024, sizeof(char));
  589. nalloced += 1024;
  590. tmp = G_program_name();
  591. len = strlen(tmp);
  592. if (len >= nalloced) {
  593. nalloced += (1024 > len) ? 1024 : len + 1;
  594. buff = G_realloc(buff, nalloced);
  595. }
  596. cur = buff;
  597. strcpy(cur, tmp);
  598. cur += len;
  599. if (st->n_flags) {
  600. flag = &st->first_flag;
  601. while (flag) {
  602. if (flag->answer == 1) {
  603. flg[0] = ' ';
  604. flg[1] = '-';
  605. flg[2] = flag->key;
  606. flg[3] = '\0';
  607. slen = strlen(flg);
  608. if (len + slen >= nalloced) {
  609. nalloced +=
  610. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  611. buff = G_realloc(buff, nalloced);
  612. cur = buff + len;
  613. }
  614. strcpy(cur, flg);
  615. cur += slen;
  616. len += slen;
  617. }
  618. flag = flag->next_flag;
  619. }
  620. }
  621. opt = &st->first_option;
  622. while (opt) {
  623. if (opt->answer && opt->answers && opt->answers[0]) {
  624. slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
  625. if (len + slen >= nalloced) {
  626. nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  627. buff = G_realloc(buff, nalloced);
  628. cur = buff + len;
  629. }
  630. strcpy(cur, " ");
  631. cur++;
  632. strcpy(cur, opt->key);
  633. cur = strchr(cur, '\0');
  634. strcpy(cur, "=");
  635. cur++;
  636. if (opt->type == TYPE_STRING) {
  637. strcpy(cur, "\"");
  638. cur++;
  639. }
  640. strcpy(cur, opt->answers[0]);
  641. cur = strchr(cur, '\0');
  642. len = cur - buff;
  643. for (n = 1; opt->answers[n]; n++) {
  644. if (!opt->answers[n])
  645. break;
  646. slen = strlen(opt->answers[n]) + 2; /* +2 for , " */
  647. if (len + slen >= nalloced) {
  648. nalloced +=
  649. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  650. buff = G_realloc(buff, nalloced);
  651. cur = buff + len;
  652. }
  653. strcpy(cur, ",");
  654. cur++;
  655. strcpy(cur, opt->answers[n]);
  656. cur = strchr(cur, '\0');
  657. len = cur - buff;
  658. }
  659. if (opt->type == TYPE_STRING) {
  660. strcpy(cur, "\"");
  661. cur++;
  662. len = cur - buff;
  663. }
  664. }
  665. opt = opt->next_opt;
  666. }
  667. return buff;
  668. }
  669. /*!
  670. \brief Add keyword to the list
  671. \param keyword keyword string
  672. */
  673. void G_add_keyword(const char *keyword)
  674. {
  675. if (st->n_keys >= st->n_keys_alloc) {
  676. st->n_keys_alloc += 10;
  677. st->module_info.keywords = G_realloc(st->module_info.keywords,
  678. st->n_keys_alloc * sizeof(char *));
  679. }
  680. st->module_info.keywords[st->n_keys++] = G_store(keyword);
  681. }
  682. /*!
  683. \brief Set keywords from the string
  684. \param keywords keywords separated by commas
  685. */
  686. void G_set_keywords(const char *keywords)
  687. {
  688. char **tokens = G_tokenize(keywords, ",");
  689. st->module_info.keywords = (const char **)tokens;
  690. st->n_keys = st->n_keys_alloc = G_number_of_tokens(tokens);
  691. }
  692. int G__uses_new_gisprompt(void)
  693. {
  694. struct Option *opt;
  695. char age[KEYLENGTH];
  696. char element[KEYLENGTH];
  697. char desc[KEYLENGTH];
  698. if (st->module_info.overwrite)
  699. return 1;
  700. /* figure out if any of the options use a "new" gisprompt */
  701. /* This is to see if we should spit out the --o flag */
  702. if (st->n_opts) {
  703. opt = &st->first_option;
  704. while (opt) {
  705. if (opt->gisprompt) {
  706. split_gisprompt(opt->gisprompt, age, element, desc);
  707. if (strcmp(age, "new") == 0)
  708. return 1;
  709. }
  710. opt = opt->next_opt;
  711. }
  712. }
  713. return 0;
  714. }
  715. /*!
  716. \brief Print list of keywords (internal use only)
  717. If <em>format</em> function is NULL than list of keywords is printed
  718. comma-separated.
  719. \param[out] fd file where to print
  720. \param format pointer to print function
  721. */
  722. void G__print_keywords(FILE *fd, void (*format)(FILE *, const char *))
  723. {
  724. int i;
  725. for(i = 0; i < st->n_keys; i++) {
  726. if (!format) {
  727. fprintf(fd, "%s", st->module_info.keywords[i]);
  728. }
  729. else {
  730. format(fd, st->module_info.keywords[i]);
  731. }
  732. if (i < st->n_keys - 1)
  733. fprintf(fd, ", ");
  734. }
  735. fflush(fd);
  736. }
  737. /*!
  738. \brief Get overwrite value
  739. \return 1 overwrite enabled
  740. \return 0 overwrite disabled
  741. */
  742. int G_get_overwrite()
  743. {
  744. return st->overwrite;
  745. }
  746. void define_keywords(void)
  747. {
  748. st->n_keys = 0;
  749. st->n_keys_alloc = 0;
  750. }
  751. /**************************************************************************
  752. *
  753. * The remaining routines are all local (static) routines used to support
  754. * the parsing process.
  755. *
  756. **************************************************************************/
  757. /*!
  758. \brief Invoke GUI dialog
  759. */
  760. static void module_gui_wx(void)
  761. {
  762. char script[GPATH_MAX];
  763. if (!st->pgm_path)
  764. st->pgm_path = G_program_name();
  765. if (!st->pgm_path)
  766. G_fatal_error(_("Unable to determine program name"));
  767. sprintf(script, "%s/gui/wxpython/gui_core/forms.py",
  768. getenv("GISBASE"));
  769. G_spawn(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), script, G_recreate_command(), NULL);
  770. }
  771. static void add_exclusive(const char *option_key, int flag_key,
  772. const char *names)
  773. {
  774. char *keyname, *ptr1;
  775. if (!names || !*names)
  776. return;
  777. if (option_key)
  778. G_asprintf(&keyname, "%s=", option_key);
  779. else
  780. G_asprintf(&keyname, "-%c", flag_key);
  781. for (ptr1 = (char *)names;;) {
  782. int len;
  783. char *ptr2;
  784. for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
  785. ptr2++, len++) ;
  786. if (len > 0) { /* skip ,, */
  787. char *name;
  788. struct Exclusive *exclusive;
  789. name = G_malloc(len + 1);
  790. memcpy(name, ptr1, len);
  791. name[len] = 0;
  792. if (!(exclusive = find_exclusive(name))) {
  793. if (st->n_exclusive >= st->allocated_exclusive) {
  794. st->allocated_exclusive += 10;
  795. st->exclusive = G_realloc(st->exclusive,
  796. st->allocated_exclusive *
  797. sizeof(struct Exclusive));
  798. }
  799. exclusive = &st->exclusive[st->n_exclusive++];
  800. exclusive->name = name;
  801. exclusive->allocated_keys = 10;
  802. exclusive->n_keys = 0;
  803. exclusive->keys = G_malloc(exclusive->allocated_keys *
  804. sizeof(char *));
  805. }
  806. if (!has_exclusive_key(exclusive->n_keys, exclusive->keys,
  807. keyname)) {
  808. exclusive->keys[exclusive->n_keys++] = keyname;
  809. if (exclusive->n_keys >= exclusive->allocated_keys) {
  810. exclusive->allocated_keys += 10;
  811. exclusive->keys = G_realloc(exclusive->keys,
  812. exclusive->allocated_keys *
  813. sizeof(char *));
  814. }
  815. }
  816. }
  817. if (*ptr2 == '\0')
  818. break;
  819. ptr1 = ptr2 + 1;
  820. if (*ptr1 == '\0')
  821. break;
  822. }
  823. }
  824. static struct Exclusive *find_exclusive(char *name)
  825. {
  826. int i;
  827. for (i = 0; i < st->n_exclusive; i++) {
  828. if (strcmp(st->exclusive[i].name, name) == 0)
  829. return &st->exclusive[i];
  830. }
  831. return NULL;
  832. }
  833. static int has_exclusive_name(const char *names, char *name)
  834. {
  835. char *ptr1;
  836. for (ptr1 = (char *)names;;) {
  837. int len;
  838. char *ptr2;
  839. for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
  840. ptr2++, len++) ;
  841. if (len > 0) { /* skip ,, */
  842. char *aname;
  843. aname = G_malloc(len + 1);
  844. memcpy(aname, ptr1, len);
  845. aname[len] = 0;
  846. if (strcmp(name, aname) == 0) {
  847. G_free(aname);
  848. return 1;
  849. }
  850. G_free(aname);
  851. }
  852. if (*ptr2 == '\0')
  853. break;
  854. ptr1 = ptr2 + 1;
  855. if (*ptr1 == '\0')
  856. break;
  857. }
  858. return 0;
  859. }
  860. static int has_exclusive_key(int n_keys, char **keys, char *key)
  861. {
  862. int i;
  863. for (i = 0; i < n_keys; i++) {
  864. if (strcmp(keys[i], key) == 0)
  865. return 1;
  866. }
  867. return 0;
  868. }
  869. static int has_either_or(const char *names)
  870. {
  871. return names && (names[0] == '*' || strstr(names, ",*"));
  872. }
  873. static void check_exclusive()
  874. {
  875. check_mutually_exclusive_inputs();
  876. check_tied_together_inputs();
  877. check_either_or_inputs();
  878. }
  879. static void check_mutually_exclusive_inputs()
  880. {
  881. int i;
  882. for (i = 0; i < st->n_exclusive; i++) {
  883. struct Exclusive *exclusive;
  884. exclusive = &st->exclusive[i];
  885. if (exclusive->name[0] == '+' || exclusive->name[0] == '*')
  886. continue;
  887. G_debug(1, "check_exclusive(): Mutually exclusive option/flag group: "
  888. "%s", exclusive->name);
  889. G_debug(1, "check_exclusive():\t%s", exclusive->keys[0]);
  890. if (exclusive->n_keys > 1) {
  891. int len, j;
  892. char *err;
  893. len = 0;
  894. for (j = 0; j < exclusive->n_keys; j++) {
  895. len += strlen(exclusive->keys[j]) + 2; /* 2 for comma adn space
  896. */
  897. if (j > 0)
  898. G_debug(1, "check_exclusive():\t%s", exclusive->keys[j]);
  899. }
  900. err = G_malloc(len + 100);
  901. sprintf(err, _("The following options/flags cannot be used "
  902. "together: "));
  903. len = strlen(err);
  904. for (j = 0; j < exclusive->n_keys - 2; j++) {
  905. sprintf(err + len, "%s, ", exclusive->keys[j]);
  906. len = strlen(err);
  907. }
  908. sprintf(err + len, _("%s and %s"), exclusive->keys[j],
  909. exclusive->keys[j + 1]);
  910. append_error(err);
  911. G_free(err);
  912. }
  913. }
  914. }
  915. static void check_tied_together_inputs()
  916. {
  917. int i;
  918. for (i = 0; i < st->n_exclusive; i++) {
  919. struct Exclusive *exclusive;
  920. struct Option *opt;
  921. struct Flag *flag;
  922. int n, n_keys, len;
  923. char *err;
  924. exclusive = &st->exclusive[i];
  925. if (exclusive->name[0] != '+')
  926. continue;
  927. G_debug(1, "check_exclusive(): Tied-together option/flag group: %s",
  928. exclusive->name);
  929. n_keys = 0;
  930. len = 0;
  931. opt = &st->first_option;
  932. while (opt) {
  933. if (has_exclusive_name(opt->exclusive, exclusive->name)) {
  934. n_keys++;
  935. len += strlen(opt->key) + 3; /* 3 for =, comma and space */
  936. G_debug(1, "check_exclusive():\t%s=", opt->key);
  937. }
  938. opt = opt->next_opt;
  939. }
  940. flag = &st->first_flag;
  941. while (flag) {
  942. if (has_exclusive_name(flag->exclusive, exclusive->name)) {
  943. n_keys++;
  944. len += 4; /* 4 for -, flag, comma and space */
  945. G_debug(1, "check_exclusive():\t-%c", flag->key);
  946. }
  947. flag = flag->next_flag;
  948. }
  949. if (n_keys == 1 || exclusive->n_keys == n_keys)
  950. continue;
  951. err = G_malloc(len + 100);
  952. sprintf(err, _("The following options/flags must be used together: "));
  953. n = 0;
  954. len = strlen(err);
  955. opt = &st->first_option;
  956. while (opt) {
  957. if (has_exclusive_name(opt->exclusive, exclusive->name)) {
  958. n++;
  959. if (n <= n_keys - 2)
  960. sprintf(err + len, "%s=, ", opt->key);
  961. else if (n == n_keys - 1)
  962. sprintf(err + len, "%s= ", opt->key);
  963. else
  964. sprintf(err + len, "and %s=", opt->key);
  965. len = strlen(err);
  966. }
  967. opt = opt->next_opt;
  968. }
  969. flag = &st->first_flag;
  970. while (flag) {
  971. if (has_exclusive_name(flag->exclusive, exclusive->name)) {
  972. n++;
  973. if (n <= n_keys - 2)
  974. sprintf(err + len, "-%c, ", flag->key);
  975. else if (n == n_keys - 1)
  976. sprintf(err + len, "-%c ", flag->key);
  977. else
  978. sprintf(err + len, "and -%c", flag->key);
  979. len = strlen(err);
  980. }
  981. flag = flag->next_flag;
  982. }
  983. append_error(err);
  984. G_free(err);
  985. }
  986. }
  987. static void check_either_or_inputs()
  988. {
  989. int n_keys, len;
  990. struct Option *opt;
  991. struct Flag *flag;
  992. G_debug(1, "check_exclusive(): Either-or options/flags "
  993. "(group names ignored)");
  994. n_keys = 0;
  995. len = 0;
  996. opt = &st->first_option;
  997. while (opt) {
  998. if (has_either_or(opt->exclusive)) {
  999. n_keys++;
  1000. len += strlen(opt->key) + 3; /* 3 for =, comma and space */
  1001. G_debug(1, "check_exclusive():\t%s=", opt->key);
  1002. }
  1003. opt = opt->next_opt;
  1004. }
  1005. flag = &st->first_flag;
  1006. while (flag) {
  1007. if (has_either_or(flag->exclusive)) {
  1008. n_keys++;
  1009. len += 4; /* 4 for -, flag, comma and space */
  1010. G_debug(1, "check_exclusive():\t-%c", flag->key);
  1011. }
  1012. flag = flag->next_flag;
  1013. }
  1014. if (n_keys > 0) {
  1015. int i;
  1016. for (i = 0; i < st->n_exclusive; i++) {
  1017. if (st->exclusive[i].name[0] == '*')
  1018. break;
  1019. }
  1020. if (i == st->n_exclusive) {
  1021. int n;
  1022. char *err;
  1023. err = G_malloc(len + 100);
  1024. sprintf(err, _("One or more of the following options/flags "
  1025. "must be used: "));
  1026. n = 0;
  1027. len = strlen(err);
  1028. opt = &st->first_option;
  1029. while (opt) {
  1030. if (has_either_or(opt->exclusive)) {
  1031. n++;
  1032. if (n <= n_keys - 2)
  1033. sprintf(err + len, "%s=, ", opt->key);
  1034. else if (n == n_keys - 1)
  1035. sprintf(err + len, "%s= ", opt->key);
  1036. else
  1037. sprintf(err + len, "or %s=", opt->key);
  1038. len = strlen(err);
  1039. }
  1040. opt = opt->next_opt;
  1041. }
  1042. flag = &st->first_flag;
  1043. while (flag) {
  1044. if (has_either_or(flag->exclusive)) {
  1045. n++;
  1046. if (n <= n_keys - 2)
  1047. sprintf(err + len, "-%c, ", flag->key);
  1048. else if (n == n_keys - 1)
  1049. sprintf(err + len, "-%c ", flag->key);
  1050. else
  1051. sprintf(err + len, "or -%c", flag->key);
  1052. len = strlen(err);
  1053. }
  1054. flag = flag->next_flag;
  1055. }
  1056. append_error(err);
  1057. G_free(err);
  1058. }
  1059. }
  1060. }
  1061. static void set_flag(int f)
  1062. {
  1063. struct Flag *flag;
  1064. char *err;
  1065. err = NULL;
  1066. /* Flag is not valid if there are no flags to set */
  1067. if (!st->n_flags) {
  1068. G_asprintf(&err, _("Sorry, <%c> is not a valid flag"), f);
  1069. append_error(err);
  1070. return;
  1071. }
  1072. /* Find flag with corrrect keyword */
  1073. flag = &st->first_flag;
  1074. while (flag) {
  1075. if (flag->key == f) {
  1076. flag->answer = 1;
  1077. if (flag->suppress_required)
  1078. st->suppress_required = 1;
  1079. add_exclusive(NULL, f, flag->exclusive);
  1080. return;
  1081. }
  1082. flag = flag->next_flag;
  1083. }
  1084. G_asprintf(&err, _("Sorry, <%c> is not a valid flag"), f);
  1085. append_error(err);
  1086. }
  1087. /* contents() is used to find things strings with characters like commas and
  1088. * dashes.
  1089. */
  1090. static int contains(const char *s, int c)
  1091. {
  1092. while (*s) {
  1093. if (*s == c)
  1094. return TRUE;
  1095. s++;
  1096. }
  1097. return FALSE;
  1098. }
  1099. static int is_option(const char *string)
  1100. {
  1101. int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
  1102. return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
  1103. }
  1104. static int match_option_1(const char *string, const char *option)
  1105. {
  1106. const char *next;
  1107. if (*string == '\0')
  1108. return 1;
  1109. if (*option == '\0')
  1110. return 0;
  1111. if (*string == *option && match_option_1(string + 1, option + 1))
  1112. return 1;
  1113. if (*option == '_' && match_option_1(string, option + 1))
  1114. return 1;
  1115. next = strchr(option, '_');
  1116. if (!next)
  1117. return 0;
  1118. if (*string == '_')
  1119. return match_option_1(string + 1, next + 1);
  1120. return match_option_1(string, next + 1);
  1121. }
  1122. static int match_option(const char *string, const char *option)
  1123. {
  1124. return (*string == *option)
  1125. && match_option_1(string + 1, option + 1);
  1126. }
  1127. static void set_option(const char *string)
  1128. {
  1129. struct Option *at_opt = NULL;
  1130. struct Option *opt = NULL;
  1131. int found;
  1132. int prefix;
  1133. size_t key_len;
  1134. char the_key[KEYLENGTH];
  1135. char *ptr, *err;
  1136. err = NULL;
  1137. for (ptr = the_key; *string != '='; ptr++, string++)
  1138. *ptr = *string;
  1139. *ptr = '\0';
  1140. string++;
  1141. /* Find option with best keyword match */
  1142. found = 0;
  1143. prefix = 0;
  1144. key_len = strlen(the_key);
  1145. for (at_opt = &st->first_option; at_opt; at_opt = at_opt->next_opt) {
  1146. if (!at_opt->key)
  1147. continue;
  1148. if (strcmp(the_key, at_opt->key) == 0) {
  1149. opt = at_opt;
  1150. found = 1;
  1151. break;
  1152. }
  1153. if (strncmp(the_key, at_opt->key, key_len) == 0) {
  1154. opt = at_opt;
  1155. found++;
  1156. prefix++;
  1157. }
  1158. else if (match_option(the_key, at_opt->key)) {
  1159. if (!found)
  1160. opt = at_opt;
  1161. found++;
  1162. }
  1163. }
  1164. if (found > 1 && prefix > 1) {
  1165. G_asprintf(&err, _("Sorry, <%s=> is ambiguous"), the_key);
  1166. append_error(err);
  1167. return;
  1168. }
  1169. /* If there is no match, complain */
  1170. if (found == 0) {
  1171. G_asprintf(&err, _("Sorry, <%s> is not a valid parameter"), the_key);
  1172. append_error(err);
  1173. return;
  1174. }
  1175. /* Allocate memory where answer is stored */
  1176. if (opt->count++) {
  1177. if (!opt->multiple) {
  1178. G_asprintf(&err, _("Option <%s> does not accept multiple answers"), opt->key);
  1179. append_error(err);
  1180. }
  1181. opt->answer = G_realloc(opt->answer,
  1182. strlen(opt->answer) + strlen(string) + 2);
  1183. strcat(opt->answer, ",");
  1184. strcat(opt->answer, string);
  1185. }
  1186. else
  1187. opt->answer = G_store(string);
  1188. add_exclusive(opt->key, 0, opt->exclusive);
  1189. }
  1190. static void check_opts(void)
  1191. {
  1192. struct Option *opt;
  1193. int ans;
  1194. if (!st->n_opts)
  1195. return;
  1196. opt = &st->first_option;
  1197. while (opt) {
  1198. /* Check answer against options if any */
  1199. if (opt->answer) {
  1200. if (opt->multiple == 0)
  1201. check_an_opt(opt->key, opt->type,
  1202. opt->options, opt->opts, &opt->answer);
  1203. else {
  1204. for (ans = 0; opt->answers[ans] != '\0'; ans++)
  1205. check_an_opt(opt->key, opt->type,
  1206. opt->options, opt->opts, &opt->answers[ans]);
  1207. }
  1208. }
  1209. /* Check answer against user's check subroutine if any */
  1210. if (opt->checker)
  1211. opt->checker(opt->answer);
  1212. opt = opt->next_opt;
  1213. }
  1214. }
  1215. static void check_an_opt(const char *key, int type, const char *options,
  1216. const char **opts, char **answerp)
  1217. {
  1218. const char *answer = *answerp;
  1219. int error;
  1220. char *err;
  1221. int found;
  1222. error = 0;
  1223. err = NULL;
  1224. switch (type) {
  1225. case TYPE_INTEGER:
  1226. error = check_int(answer, opts);
  1227. break;
  1228. case TYPE_DOUBLE:
  1229. error = check_double(answer, opts);
  1230. break;
  1231. case TYPE_STRING:
  1232. error = check_string(answer, opts, &found);
  1233. break;
  1234. }
  1235. switch (error) {
  1236. case 0:
  1237. break;
  1238. case BAD_SYNTAX:
  1239. G_asprintf(&err,
  1240. _("Illegal range syntax for parameter <%s>\n"
  1241. "\tPresented as: %s"), key, options);
  1242. append_error(err);
  1243. break;
  1244. case OUT_OF_RANGE:
  1245. G_asprintf(&err,
  1246. _("Value <%s> out of range for parameter <%s>\n"
  1247. "\tLegal range: %s"), answer, key, options);
  1248. append_error(err);
  1249. break;
  1250. case MISSING_VALUE:
  1251. G_asprintf(&err,
  1252. _("Missing value for parameter <%s>"),
  1253. key);
  1254. append_error(err);
  1255. break;
  1256. case AMBIGUOUS:
  1257. G_asprintf(&err,
  1258. _("Value <%s> ambiguous for parameter <%s>\n"
  1259. "\tValid options: %s"), answer, key, options);
  1260. append_error(err);
  1261. break;
  1262. case REPLACED:
  1263. *answerp = G_store(opts[found]);
  1264. error = 0;
  1265. break;
  1266. }
  1267. }
  1268. static int check_int(const char *ans, const char **opts)
  1269. {
  1270. int d, i;
  1271. /* "-" is reserved for standard input */
  1272. if (strcmp(ans, "-") == 0)
  1273. return 0;
  1274. if (sscanf(ans, "%d", &d) != 1)
  1275. return MISSING_VALUE;
  1276. if (!opts)
  1277. return 0;
  1278. for (i = 0; opts[i]; i++) {
  1279. const char *opt = opts[i];
  1280. int lo, hi;
  1281. if (contains(opt, '-')) {
  1282. if (sscanf(opt, "%d-%d", &lo, &hi) == 2) {
  1283. if (d >= lo && d <= hi)
  1284. return 0;
  1285. }
  1286. else if (sscanf(opt, "-%d", &hi) == 1) {
  1287. if (d <= hi)
  1288. return 0;
  1289. }
  1290. else if (sscanf(opt, "%d-", &lo) == 1) {
  1291. if (d >= lo)
  1292. return 0;
  1293. }
  1294. else
  1295. return BAD_SYNTAX;
  1296. }
  1297. else {
  1298. if (sscanf(opt, "%d", &lo) == 1) {
  1299. if (d == lo)
  1300. return 0;
  1301. }
  1302. else
  1303. return BAD_SYNTAX;
  1304. }
  1305. }
  1306. return OUT_OF_RANGE;
  1307. }
  1308. static int check_double(const char *ans, const char **opts)
  1309. {
  1310. double d;
  1311. int i;
  1312. /* "-" is reserved for standard input */
  1313. if (strcmp(ans, "-") == 0)
  1314. return 0;
  1315. if (sscanf(ans, "%lf", &d) != 1)
  1316. return MISSING_VALUE;
  1317. if (!opts)
  1318. return 0;
  1319. for (i = 0; opts[i]; i++) {
  1320. const char *opt = opts[i];
  1321. double lo, hi;
  1322. if (contains(opt, '-')) {
  1323. if (sscanf(opt, "%lf-%lf", &lo, &hi) == 2) {
  1324. if (d >= lo && d <= hi)
  1325. return 0;
  1326. }
  1327. else if (sscanf(opt, "-%lf", &hi) == 1) {
  1328. if (d <= hi)
  1329. return 0;
  1330. }
  1331. else if (sscanf(opt, "%lf-", &lo) == 1) {
  1332. if (d >= lo)
  1333. return 0;
  1334. }
  1335. else
  1336. return BAD_SYNTAX;
  1337. }
  1338. else {
  1339. if (sscanf(opt, "%lf", &lo) == 1) {
  1340. if (d == lo)
  1341. return 0;
  1342. }
  1343. else
  1344. return BAD_SYNTAX;
  1345. }
  1346. }
  1347. return OUT_OF_RANGE;
  1348. }
  1349. static int check_string(const char *ans, const char **opts, int *result)
  1350. {
  1351. int len = strlen(ans);
  1352. int found = 0;
  1353. int prefix = 0;
  1354. int i;
  1355. if (!opts)
  1356. return 0;
  1357. for (i = 0; opts[i]; i++) {
  1358. if (strcmp(ans, opts[i]) == 0)
  1359. return 0;
  1360. if (strncmp(ans, opts[i], len) == 0) {
  1361. *result = i;
  1362. found++;
  1363. prefix++;
  1364. }
  1365. else if (match_option(ans, opts[i])) {
  1366. if (!found)
  1367. *result = i;
  1368. found++;
  1369. }
  1370. }
  1371. switch (found) {
  1372. case 0: return OUT_OF_RANGE;
  1373. case 1: return REPLACED;
  1374. default:
  1375. switch (prefix) {
  1376. case 1: return REPLACED;
  1377. default: return AMBIGUOUS;
  1378. }
  1379. }
  1380. }
  1381. static void check_required(void)
  1382. {
  1383. struct Option *opt;
  1384. char *err;
  1385. err = NULL;
  1386. if (!st->n_opts)
  1387. return;
  1388. opt = &st->first_option;
  1389. while (opt) {
  1390. if (opt->required && !opt->answer) {
  1391. G_asprintf(&err, _("Required parameter <%s> not set:\n"
  1392. "\t(%s)"),
  1393. opt->key, (opt->label ? opt->label : opt->description));
  1394. append_error(err);
  1395. }
  1396. opt = opt->next_opt;
  1397. }
  1398. }
  1399. static void split_opts(void)
  1400. {
  1401. struct Option *opt;
  1402. const char *ptr1;
  1403. const char *ptr2;
  1404. int allocated;
  1405. int ans_num;
  1406. int len;
  1407. if (!st->n_opts)
  1408. return;
  1409. opt = &st->first_option;
  1410. while (opt) {
  1411. if ( /*opt->multiple && */ opt->answer) {
  1412. /* Allocate some memory to store array of pointers */
  1413. allocated = 10;
  1414. opt->answers = G_malloc(allocated * sizeof(char *));
  1415. ans_num = 0;
  1416. ptr1 = opt->answer;
  1417. opt->answers[ans_num] = NULL;
  1418. for (;;) {
  1419. for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
  1420. ptr2++, len++) ;
  1421. if (len > 0) { /* skip ,, */
  1422. opt->answers[ans_num] = G_malloc(len + 1);
  1423. memcpy(opt->answers[ans_num], ptr1, len);
  1424. opt->answers[ans_num][len] = 0;
  1425. ans_num++;
  1426. if (ans_num >= allocated) {
  1427. allocated += 10;
  1428. opt->answers = G_realloc(opt->answers,
  1429. allocated * sizeof(char *));
  1430. }
  1431. opt->answers[ans_num] = NULL;
  1432. }
  1433. if (*ptr2 == '\0')
  1434. break;
  1435. ptr1 = ptr2 + 1;
  1436. if (*ptr1 == '\0')
  1437. break;
  1438. }
  1439. }
  1440. opt = opt->next_opt;
  1441. }
  1442. }
  1443. static void check_multiple_opts(void)
  1444. {
  1445. struct Option *opt;
  1446. const char *ptr;
  1447. int n_commas;
  1448. int n;
  1449. char *err;
  1450. if (!st->n_opts)
  1451. return;
  1452. err = NULL;
  1453. opt = &st->first_option;
  1454. while (opt) {
  1455. /* "-" is reserved from standard input/output */
  1456. if (opt->answer && strcmp(opt->answer, "-") && opt->key_desc) {
  1457. /* count commas */
  1458. n_commas = 1;
  1459. for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
  1460. if (*ptr == ',')
  1461. n_commas++;
  1462. /* count items */
  1463. for (n = 0; opt->answers[n] != '\0'; n++) ;
  1464. /* if not correct multiple of items */
  1465. if (n % n_commas) {
  1466. G_asprintf(&err,
  1467. _("Option <%s> must be provided in multiples of %d\n"
  1468. "\tYou provided %d item(s): %s"),
  1469. opt->key, n_commas, n, opt->answer);
  1470. append_error(err);
  1471. }
  1472. }
  1473. opt = opt->next_opt;
  1474. }
  1475. }
  1476. /* Check for all 'new' if element already exists */
  1477. static int check_overwrite(void)
  1478. {
  1479. struct Option *opt;
  1480. char age[KEYLENGTH];
  1481. char element[KEYLENGTH];
  1482. char desc[KEYLENGTH];
  1483. int error = 0;
  1484. const char *overstr;
  1485. int over;
  1486. st->module_info.overwrite = 0;
  1487. if (!st->n_opts)
  1488. return (0);
  1489. over = 0;
  1490. /* Check the GRASS OVERWRITE variable */
  1491. if ((overstr = G__getenv("OVERWRITE"))) {
  1492. over = atoi(overstr);
  1493. }
  1494. /* Check the GRASS_OVERWRITE environment variable */
  1495. if ((overstr = getenv("GRASS_OVERWRITE"))) {
  1496. if (atoi(overstr))
  1497. over = 1;
  1498. }
  1499. if (st->overwrite || over) {
  1500. st->module_info.overwrite = 1;
  1501. /* Set the environment so that programs run in a script also obey --o */
  1502. putenv("GRASS_OVERWRITE=1");
  1503. /* No need to check options for existing files if overwrite is true */
  1504. return error;
  1505. }
  1506. opt = &st->first_option;
  1507. while (opt) {
  1508. if (opt->answer && opt->gisprompt) {
  1509. split_gisprompt(opt->gisprompt, age, element, desc);
  1510. if (strcmp(age, "new") == 0) {
  1511. int i;
  1512. char found;
  1513. for (i = 0; opt->answers[i]; i++) {
  1514. found = FALSE;
  1515. if (strcmp(element, "file") == 0) {
  1516. if (access(opt->answers[i], F_OK) == 0)
  1517. found = TRUE;
  1518. }
  1519. else if (strcmp(element, "mapset") != 0) {
  1520. /* TODO: also other elements should be
  1521. probably skipped */
  1522. if (G_find_file(element, opt->answers[i], G_mapset())) {
  1523. found = TRUE;
  1524. }
  1525. }
  1526. if (found) { /* found */
  1527. if (!st->overwrite && !over) {
  1528. if (G_verbose() > -1) {
  1529. if (G_info_format() != G_INFO_FORMAT_GUI) {
  1530. fprintf(stderr,
  1531. _("ERROR: option <%s>: <%s> exists.\n"),
  1532. opt->key, opt->answers[i]);
  1533. }
  1534. else {
  1535. fprintf(stderr,
  1536. "GRASS_INFO_ERROR(%d,1): option <%s>: <%s> exists.\n",
  1537. getpid(), opt->key, opt->answers[i]);
  1538. fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
  1539. getpid());
  1540. }
  1541. }
  1542. error = 1;
  1543. }
  1544. }
  1545. }
  1546. }
  1547. }
  1548. opt = opt->next_opt;
  1549. }
  1550. return (error);
  1551. }
  1552. static void split_gisprompt(const char *gisprompt, char *age, char *element,
  1553. char *desc)
  1554. {
  1555. const char *ptr1;
  1556. char *ptr2;
  1557. for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
  1558. if (*ptr1 == ',')
  1559. break;
  1560. *ptr2 = *ptr1;
  1561. }
  1562. *ptr2 = '\0';
  1563. for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
  1564. if (*ptr1 == ',')
  1565. break;
  1566. *ptr2 = *ptr1;
  1567. }
  1568. *ptr2 = '\0';
  1569. for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
  1570. if (*ptr1 == ',')
  1571. break;
  1572. *ptr2 = *ptr1;
  1573. }
  1574. *ptr2 = '\0';
  1575. }
  1576. static void append_error(const char *msg)
  1577. {
  1578. st->error = G_realloc(st->error, sizeof(char *) * (st->n_errors + 1));
  1579. st->error[st->n_errors++] = G_store(msg);
  1580. }
  1581. /*!
  1582. \brief Get separator string from the option.
  1583. Calls G_fatal_error() on error. Allocated string can be later freed
  1584. by G_free().
  1585. \code
  1586. char *fs;
  1587. struct Option *opt_fs;
  1588. opt_fs = G_define_standard_option(G_OPT_F_SEP);
  1589. if (G_parser(argc, argv))
  1590. exit(EXIT_FAILURE);
  1591. fs = G_option_to_separator(opt_fs);
  1592. \endcode
  1593. \param option pointer to separator option
  1594. \return allocated string with separator
  1595. */
  1596. char* G_option_to_separator(const struct Option *option)
  1597. {
  1598. char* sep;
  1599. if (option->gisprompt == NULL ||
  1600. strcmp(option->gisprompt, "old,separator,separator") != 0)
  1601. G_fatal_error(_("%s= is not a separator option"), option->key);
  1602. if (option->answer == NULL)
  1603. G_fatal_error(_("No separator given for %s="), option->key);
  1604. if (strcmp(option->answer, "pipe") == 0)
  1605. sep = G_store("|");
  1606. else if (strcmp(option->answer, "comma") == 0)
  1607. sep = G_store(",");
  1608. else if (strcmp(option->answer, "space") == 0)
  1609. sep = G_store(" ");
  1610. else if (strcmp(option->answer, "tab") == 0 ||
  1611. strcmp(option->answer, "\\t") == 0)
  1612. sep = G_store("\t");
  1613. else if (strcmp(option->answer, "newline") == 0 ||
  1614. strcmp(option->answer, "\\n") == 0)
  1615. sep = G_store("\n");
  1616. else
  1617. sep = G_store(option->answer);
  1618. G_debug(1, "G_option_to_separator(): key = %s -> sep = '%s'",
  1619. option->key, sep);
  1620. return sep;
  1621. }
  1622. /*!
  1623. \brief Get an input/output file pointer from the option. If the file name is
  1624. omitted or '-', it returns either stdin or stdout based on the gisprompt.
  1625. Calls G_fatal_error() on error. File pointer can be later closed by
  1626. G_close_option_file().
  1627. \code
  1628. FILE *fp_input;
  1629. FILE *fp_output;
  1630. struct Option *opt_input;
  1631. struct Option *opt_output;
  1632. opt_input = G_define_standard_option(G_OPT_F_INPUT);
  1633. opt_output = G_define_standard_option(G_OPT_F_OUTPUT);
  1634. if (G_parser(argc, argv))
  1635. exit(EXIT_FAILURE);
  1636. fp_input = G_open_option_file(opt_input);
  1637. fp_output = G_open_option_file(opt_output);
  1638. ...
  1639. G_close_option_file(fp_input);
  1640. G_close_option_file(fp_output);
  1641. \endcode
  1642. \param option pointer to a file option
  1643. \return file pointer
  1644. */
  1645. FILE *G_open_option_file(const struct Option *option)
  1646. {
  1647. int stdinout;
  1648. FILE *fp;
  1649. stdinout = !option->answer || !*(option->answer) ||
  1650. strcmp(option->answer, "-") == 0;
  1651. if (option->gisprompt == NULL)
  1652. G_fatal_error(_("%s= is not a file option"), option->key);
  1653. else if (option->multiple)
  1654. G_fatal_error(_("Opening multiple files not supported for %s="),
  1655. option->key);
  1656. else if (strcmp(option->gisprompt, "old,file,file") == 0) {
  1657. if (stdinout)
  1658. fp = stdin;
  1659. else if ((fp = fopen(option->answer, "r")) == NULL)
  1660. G_fatal_error(_("Unable to open %s file <%s>"),
  1661. option->key, option->answer);
  1662. } else if (strcmp(option->gisprompt, "new,file,file") == 0) {
  1663. if (stdinout)
  1664. fp = stdout;
  1665. else if ((fp = fopen(option->answer, "w")) == NULL)
  1666. G_fatal_error(_("Unable to create %s file <%s>"),
  1667. option->key, option->answer);
  1668. } else
  1669. G_fatal_error(_("%s= is not a file option"), option->key);
  1670. return fp;
  1671. }
  1672. /*!
  1673. \brief Close an input/output file returned by G_open_option_file(). If the
  1674. file pointer is stdin, stdout, or stderr, nothing happens.
  1675. \param file pointer
  1676. */
  1677. void G_close_option_file(FILE *fp)
  1678. {
  1679. if (fp != stdin && fp != stdout && fp != stderr)
  1680. fclose(fp);
  1681. }