parser.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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. * \author Soeren Gebbert added Dec. 2009 WPS process_description document
  74. */
  75. #include <stdio.h>
  76. #include <stdlib.h>
  77. #include <string.h>
  78. #include <unistd.h>
  79. #include <grass/gis.h>
  80. #include <grass/spawn.h>
  81. #include <grass/glocale.h>
  82. #include "parser_local_proto.h"
  83. enum opt_error {
  84. BAD_SYNTAX = 1,
  85. OUT_OF_RANGE = 2,
  86. MISSING_VALUE = 3,
  87. AMBIGUOUS = 4,
  88. REPLACED = 5
  89. };
  90. #define KEYLENGTH 64
  91. /* initialize the global struct */
  92. struct state state;
  93. struct state *st = &state;
  94. /* local prototypes */
  95. static int set_flag(int);
  96. static int contains(const char *, int);
  97. static int is_option(const char *);
  98. static int set_option(const char *);
  99. static int check_opts(void);
  100. static int check_an_opt(const char *, int, const char *, const char **, char **);
  101. static int check_int(const char *, const char **);
  102. static int check_double(const char *, const char **);
  103. static int check_string(const char *, const char **, int *);
  104. static int check_required(void);
  105. static void split_opts(void);
  106. static int check_multiple_opts(void);
  107. static int check_overwrite(void);
  108. static void define_keywords(void);
  109. static void split_gisprompt(const char *gisprompt, char *age, char *element, char *desc);
  110. static void module_gui_wx(void);
  111. /*!
  112. * \brief Disables the ability of the parser to operate interactively.
  113. *
  114. * When a user calls a command with no arguments on the command line,
  115. * the parser will enter its own standardized interactive session in
  116. * which all flags and options are presented to the user for input. A
  117. * call to G_disable_interactive() disables the parser's interactive
  118. * prompting.
  119. *
  120. */
  121. void G_disable_interactive(void)
  122. {
  123. st->no_interactive = 1;
  124. }
  125. /*!
  126. * \brief Initializes a Flag struct.
  127. *
  128. * Allocates memory for the Flag structure and returns a pointer to
  129. * this memory.
  130. *
  131. * Flags are always represented by single letters. A user "turns them
  132. * on" at the command line using a minus sign followed by the
  133. * character representing the flag.
  134. *
  135. * \return Pointer to a Flag struct
  136. */
  137. struct Flag *G_define_flag(void)
  138. {
  139. struct Flag *flag;
  140. struct Item *item;
  141. /* Allocate memory if not the first flag */
  142. if (st->n_flags) {
  143. flag = (struct Flag *)G_malloc(sizeof(struct Flag));
  144. st->current_flag->next_flag = flag;
  145. }
  146. else
  147. flag = &st->first_flag;
  148. /* Zero structure */
  149. G_zero((char *)flag, sizeof(struct Flag));
  150. st->current_flag = flag;
  151. st->n_flags++;
  152. if (st->n_items) {
  153. item = (struct Item *)G_malloc(sizeof(struct Item));
  154. st->current_item->next_item = item;
  155. }
  156. else
  157. item = &st->first_item;
  158. G_zero((char *)item, sizeof(struct Item));
  159. item->flag = flag;
  160. item->option = NULL;
  161. st->current_item = item;
  162. st->n_items++;
  163. return (flag);
  164. }
  165. /*!
  166. * \brief Initializes an Option struct.
  167. *
  168. * Allocates memory for the Option structure and returns a pointer to
  169. * this memory.
  170. *
  171. * Options are provided by user on command line using the standard
  172. * format: <i>key=value</i>. Options identified as REQUIRED must be
  173. * specified by user on command line. The option string can either
  174. * specify a range of values (e.g. "10-100") or a list of acceptable
  175. * values (e.g. "red,orange,yellow"). Unless the option string is
  176. * NULL, user provided input will be evaluated agaist this string.
  177. *
  178. * \return pointer to an Option struct
  179. */
  180. struct Option *G_define_option(void)
  181. {
  182. struct Option *opt;
  183. struct Item *item;
  184. /* Allocate memory if not the first option */
  185. if (st->n_opts) {
  186. opt = (struct Option *)G_malloc(sizeof(struct Option));
  187. st->current_option->next_opt = opt;
  188. }
  189. else
  190. opt = &st->first_option;
  191. /* Zero structure */
  192. G_zero((char *)opt, sizeof(struct Option));
  193. opt->required = NO;
  194. opt->multiple = NO;
  195. opt->answer = NULL;
  196. opt->answers = NULL;
  197. opt->def = NULL;
  198. opt->checker = NULL;
  199. opt->options = NULL;
  200. opt->key_desc = NULL;
  201. opt->gisprompt = NULL;
  202. opt->label = NULL;
  203. opt->opts = NULL;
  204. opt->description = NULL;
  205. opt->descriptions = NULL;
  206. opt->guisection = NULL;
  207. opt->guidependency = NULL;
  208. st->current_option = opt;
  209. st->n_opts++;
  210. if (st->n_items) {
  211. item = (struct Item *)G_malloc(sizeof(struct Item));
  212. st->current_item->next_item = item;
  213. }
  214. else
  215. item = &st->first_item;
  216. G_zero((char *)item, sizeof(struct Item));
  217. item->option = opt;
  218. item->flag = NULL;
  219. st->current_item = item;
  220. st->n_items++;
  221. return (opt);
  222. }
  223. /*!
  224. * \brief Initializes a new module.
  225. *
  226. * \return pointer to a GModule struct
  227. */
  228. struct GModule *G_define_module(void)
  229. {
  230. struct GModule *module;
  231. /* Allocate memory */
  232. module = &st->module_info;
  233. /* Zero structure */
  234. G_zero((char *)module, sizeof(struct GModule));
  235. /* Allocate keywords array */
  236. define_keywords();
  237. return (module);
  238. }
  239. /*!
  240. * \brief Parse command line.
  241. *
  242. * The command line parameters <i>argv</i> and the number of
  243. * parameters <i>argc</i> from the main() routine are passed directly
  244. * to G_parser(). G_parser() accepts the command line input entered by
  245. * the user, and parses this input according to the input options
  246. * and/or flags that were defined by the programmer.
  247. *
  248. * <b>Note:</b> The only functions which can legitimately be called
  249. * before G_parser() are:
  250. *
  251. * - G_gisinit()
  252. * - G_no_gisinit()
  253. * - G_define_module()
  254. * - G_define_flag()
  255. * - G_define_option()
  256. * - G_define_standard_option()
  257. * - G_disable_interactive()
  258. *
  259. * The usual order a module calls functions is:
  260. *
  261. * # G_gisinit()
  262. * # G_define_module()
  263. * # G_define_flag()
  264. * # G_define_option()
  265. * # G_parser()
  266. *
  267. * \param argc number of arguments
  268. * \param argv argument list
  269. *
  270. * \return 0 on success
  271. * \return -1 on error and calls G_usage()
  272. */
  273. int G_parser(int argc, char **argv)
  274. {
  275. int need_first_opt;
  276. int opt_checked = 0;
  277. int error;
  278. char *ptr, *tmp_name;
  279. int i;
  280. struct Option *opt;
  281. char force_gui = FALSE;
  282. error = 0;
  283. need_first_opt = 1;
  284. tmp_name = G_store(argv[0]);
  285. st->pgm_path = tmp_name;
  286. i = strlen(tmp_name);
  287. while (--i >= 0) {
  288. if (G_is_dirsep(tmp_name[i])) {
  289. tmp_name += i + 1;
  290. break;
  291. }
  292. }
  293. G_basename(tmp_name, "exe");
  294. st->pgm_name = tmp_name;
  295. /* Stash default answers */
  296. opt = &st->first_option;
  297. while (opt != NULL) {
  298. if (opt->required)
  299. st->has_required = 1;
  300. /* Parse options */
  301. if (opt->options) {
  302. int cnt = 0;
  303. char **tokens, delm[2];
  304. delm[0] = ',';
  305. delm[1] = '\0';
  306. tokens = G_tokenize(opt->options, delm);
  307. i = 0;
  308. while (tokens[i]) {
  309. cnt++;
  310. i++;
  311. }
  312. opt->opts =
  313. (const char **)G_calloc(cnt + 1, sizeof(const char *));
  314. i = 0;
  315. while (tokens[i]) {
  316. opt->opts[i] = G_store(tokens[i]);
  317. i++;
  318. }
  319. G_free_tokens(tokens);
  320. if (opt->descriptions) {
  321. delm[0] = ';';
  322. opt->descs =
  323. (const char **)G_calloc(cnt + 1, sizeof(const char *));
  324. tokens = G_tokenize(opt->descriptions, delm);
  325. i = 0;
  326. while (tokens[i]) {
  327. int j, found;
  328. if (!tokens[i + 1])
  329. break;
  330. j = 0;
  331. found = 0;
  332. while (opt->opts[j]) {
  333. if (strcmp(opt->opts[j], tokens[i]) == 0) {
  334. found = 1;
  335. break;
  336. }
  337. j++;
  338. }
  339. if (!found) {
  340. G_warning(_("BUG in descriptions, option '%s' in <%s> does not exist"),
  341. tokens[i], opt->key);
  342. }
  343. else {
  344. opt->descs[j] = G_store(tokens[i + 1]);
  345. }
  346. i += 2;
  347. }
  348. G_free_tokens(tokens);
  349. }
  350. }
  351. /* Copy answer */
  352. if (opt->multiple && opt->answers && opt->answers[0]) {
  353. opt->answer = (char *)G_malloc(strlen(opt->answers[0]) + 1);
  354. strcpy(opt->answer, opt->answers[0]);
  355. for (i = 1; opt->answers[i]; i++) {
  356. opt->answer = (char *)G_realloc(opt->answer,
  357. strlen(opt->answer) +
  358. strlen(opt->answers[i]) + 2);
  359. strcat(opt->answer, ",");
  360. strcat(opt->answer, opt->answers[i]);
  361. }
  362. }
  363. opt->def = opt->answer;
  364. opt = opt->next_opt;
  365. }
  366. /* If there are NO arguments, go interactive */
  367. if (argc < 2 && st->has_required && !st->no_interactive && isatty(0)) {
  368. module_gui_wx();
  369. return -1;
  370. }
  371. else if (argc < 2 && st->has_required && isatty(0)) {
  372. G_usage();
  373. return -1;
  374. }
  375. else if (argc >= 2) {
  376. /* If first arg is "help" give a usage/syntax message */
  377. if (strcmp(argv[1], "help") == 0 ||
  378. strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0) {
  379. G_usage();
  380. exit(EXIT_SUCCESS);
  381. }
  382. /* If first arg is "--help-text" give a usage/syntax message
  383. * with machine-readable sentinels */
  384. if (strcmp(argv[1], "--help-text") == 0) {
  385. G__usage_text();
  386. exit(EXIT_SUCCESS);
  387. }
  388. /* If first arg is "--interface-description" then print out
  389. * a xml description of the task */
  390. if (strcmp(argv[1], "--interface-description") == 0) {
  391. G__usage_xml();
  392. exit(EXIT_SUCCESS);
  393. }
  394. /* If first arg is "--html-description" then print out
  395. * a html description of the task */
  396. if (strcmp(argv[1], "--html-description") == 0) {
  397. G__usage_html();
  398. exit(EXIT_SUCCESS);
  399. }
  400. /* If first arg is "--wps-process-description" then print out
  401. * the wps process description of the task */
  402. if (strcmp(argv[1], "--wps-process-description") == 0) {
  403. G__wps_print_process_description();
  404. exit(EXIT_SUCCESS);
  405. }
  406. /* If first arg is "--script" then then generate
  407. * g.parser boilerplate */
  408. if (strcmp(argv[1], "--script") == 0) {
  409. G__script();
  410. exit(EXIT_SUCCESS);
  411. }
  412. /* Loop thru all command line arguments */
  413. while (--argc) {
  414. ptr = *(++argv);
  415. /* Overwrite option */
  416. if (strcmp(ptr, "--o") == 0 || strcmp(ptr, "--overwrite") == 0) {
  417. st->overwrite = 1;
  418. }
  419. /* Verbose option */
  420. else if (strcmp(ptr, "--v") == 0 || strcmp(ptr, "--verbose") == 0) {
  421. char buff[32];
  422. /* print everything: max verbosity level */
  423. st->module_info.verbose = G_verbose_max();
  424. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_max());
  425. putenv(G_store(buff));
  426. if (st->quiet == 1) {
  427. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --verbose."));
  428. }
  429. st->quiet = -1;
  430. }
  431. /* Quiet option */
  432. else if (strcmp(ptr, "--q") == 0 || strcmp(ptr, "--quiet") == 0) {
  433. char buff[32];
  434. /* print nothing, but errors and warnings */
  435. st->module_info.verbose = G_verbose_min();
  436. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_min());
  437. putenv(G_store(buff));
  438. if (st->quiet == -1) {
  439. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --quiet."));
  440. }
  441. st->quiet = 1; /* for passing to gui init */
  442. }
  443. /* Force gui to come up */
  444. else if (strcmp(ptr, "--ui") == 0) {
  445. force_gui = TRUE;
  446. }
  447. /* If we see a flag */
  448. else if (*ptr == '-') {
  449. while (*(++ptr))
  450. error += set_flag(*ptr);
  451. }
  452. /* If we see standard option format (option=val) */
  453. else if (is_option(ptr)) {
  454. error += set_option(ptr);
  455. need_first_opt = 0;
  456. }
  457. /* If we see the first option with no equal sign */
  458. else if (need_first_opt && st->n_opts) {
  459. st->first_option.answer = G_store(ptr);
  460. need_first_opt = 0;
  461. }
  462. /* If we see the non valid argument (no "=", just argument) */
  463. else if (contains(ptr, '=') == 0) {
  464. fprintf(stderr, _("Sorry <%s> is not a valid option\n"), ptr);
  465. error = 1;
  466. }
  467. }
  468. }
  469. /* Run the gui if it was specifically requested */
  470. if (force_gui) {
  471. module_gui_wx();
  472. return -1;
  473. }
  474. /* Split options where multiple answers are OK */
  475. split_opts();
  476. /* Check multiple options */
  477. error += check_multiple_opts();
  478. /* Check answers against options and check subroutines */
  479. if (!opt_checked)
  480. error += check_opts();
  481. /* Make sure all required options are set */
  482. error += check_required();
  483. if (error) {
  484. if (G_verbose() > G_verbose_min())
  485. G_usage();
  486. return -1;
  487. }
  488. if (check_overwrite())
  489. return -1;
  490. return (0);
  491. }
  492. /*!
  493. * \brief Creates command to run non-interactive.
  494. *
  495. * Creates a command-line that runs the current command completely
  496. * non-interactive.
  497. *
  498. * \return pointer to a char string
  499. */
  500. char *G_recreate_command(void)
  501. {
  502. char *buff;
  503. char flg[4];
  504. char *cur;
  505. const char *tmp;
  506. struct Flag *flag;
  507. struct Option *opt;
  508. int n, len, slen;
  509. int nalloced = 0;
  510. G_debug(3, "G_recreate_command()");
  511. /* Flag is not valid if there are no flags to set */
  512. buff = G_calloc(1024, sizeof(char));
  513. nalloced += 1024;
  514. tmp = G_program_name();
  515. len = strlen(tmp);
  516. if (len >= nalloced) {
  517. nalloced += (1024 > len) ? 1024 : len + 1;
  518. buff = G_realloc(buff, nalloced);
  519. }
  520. cur = buff;
  521. strcpy(cur, tmp);
  522. cur += len;
  523. if (st->n_flags) {
  524. flag = &st->first_flag;
  525. while (flag != '\0') {
  526. if (flag->answer == 1) {
  527. flg[0] = ' ';
  528. flg[1] = '-';
  529. flg[2] = flag->key;
  530. flg[3] = '\0';
  531. slen = strlen(flg);
  532. if (len + slen >= nalloced) {
  533. nalloced +=
  534. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  535. buff = G_realloc(buff, nalloced);
  536. cur = buff + len;
  537. }
  538. strcpy(cur, flg);
  539. cur += slen;
  540. len += slen;
  541. }
  542. flag = flag->next_flag;
  543. }
  544. }
  545. opt = &st->first_option;
  546. while (opt != '\0') {
  547. if (opt->answer != '\0' && opt->answers[0] != NULL) {
  548. slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
  549. if (len + slen >= nalloced) {
  550. nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  551. buff = G_realloc(buff, nalloced);
  552. cur = buff + len;
  553. }
  554. strcpy(cur, " ");
  555. cur++;
  556. strcpy(cur, opt->key);
  557. cur = strchr(cur, '\0');
  558. strcpy(cur, "=");
  559. cur++;
  560. if (opt->type == TYPE_STRING) {
  561. strcpy(cur, "\"");
  562. cur++;
  563. }
  564. strcpy(cur, opt->answers[0]);
  565. cur = strchr(cur, '\0');
  566. len = cur - buff;
  567. for (n = 1; opt->answers[n] != NULL && opt->answers[n] != '\0';
  568. n++) {
  569. if (opt->answers[n] == NULL)
  570. break;
  571. slen = strlen(opt->answers[n]) + 2; /* +2 for , " */
  572. if (len + slen >= nalloced) {
  573. nalloced +=
  574. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  575. buff = G_realloc(buff, nalloced);
  576. cur = buff + len;
  577. }
  578. strcpy(cur, ",");
  579. cur++;
  580. strcpy(cur, opt->answers[n]);
  581. cur = strchr(cur, '\0');
  582. len = cur - buff;
  583. }
  584. if (opt->type == TYPE_STRING) {
  585. strcpy(cur, "\"");
  586. cur++;
  587. len = cur - buff;
  588. }
  589. }
  590. opt = opt->next_opt;
  591. }
  592. return (buff);
  593. }
  594. /*!
  595. \brief Add keyword to the list
  596. \param keyword keyword string
  597. */
  598. void G_add_keyword(const char *keyword)
  599. {
  600. if (st->n_keys >= st->n_keys_alloc) {
  601. st->n_keys_alloc += 10;
  602. st->module_info.keywords = (const char **) G_realloc(st->module_info.keywords,
  603. st->n_keys_alloc * sizeof(char *));
  604. }
  605. st->module_info.keywords[st->n_keys++] = G_store(keyword);
  606. }
  607. /*!
  608. \brief Set keywords from the string
  609. \param keywords keywords separated by commas
  610. */
  611. void G_set_keywords(const char *keywords)
  612. {
  613. st->module_info.keywords = (const char**)G_tokenize(keywords, ",");
  614. st->n_keys = st->n_keys_alloc = G_number_of_tokens((char **)st->module_info.keywords);
  615. }
  616. int G__uses_new_gisprompt(void)
  617. {
  618. struct Option *opt;
  619. char age[KEYLENGTH];
  620. char element[KEYLENGTH];
  621. char desc[KEYLENGTH];
  622. if (st->module_info.overwrite)
  623. return 1;
  624. /* figure out if any of the options use a "new" gisprompt */
  625. /* This is to see if we should spit out the --o flag */
  626. if (st->n_opts) {
  627. opt = &st->first_option;
  628. while (opt != NULL) {
  629. if (opt->gisprompt) {
  630. split_gisprompt(opt->gisprompt, age, element, desc);
  631. if (strcmp(age, "new") == 0)
  632. return 1;
  633. }
  634. opt = opt->next_opt;
  635. }
  636. }
  637. return 0;
  638. }
  639. void G__print_keywords(FILE *fd, void (*format)(FILE *, const char *))
  640. {
  641. int i;
  642. for(i = 0; i < st->n_keys; i++) {
  643. if (!format) {
  644. fprintf(fd, "%s", st->module_info.keywords[i]);
  645. }
  646. else {
  647. format(fd, st->module_info.keywords[i]);
  648. }
  649. if (i < st->n_keys - 1)
  650. fprintf(fd, ", ");
  651. }
  652. fflush(fd);
  653. }
  654. void define_keywords(void)
  655. {
  656. st->n_keys = 0;
  657. st->n_keys_alloc = 0;
  658. }
  659. /**************************************************************************
  660. *
  661. * The remaining routines are all local (static) routines used to support
  662. * the parsing process.
  663. *
  664. **************************************************************************/
  665. /*!
  666. \brief Invoke GUI dialog
  667. */
  668. static void module_gui_wx(void)
  669. {
  670. char script[GPATH_MAX];
  671. if (!st->pgm_path)
  672. st->pgm_path = G_program_name();
  673. if (!st->pgm_path)
  674. G_fatal_error(_("Unable to determine program name"));
  675. sprintf(script, "%s/etc/wxpython/gui_modules/menuform.py",
  676. getenv("GISBASE"));
  677. G_spawn(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), script, st->pgm_path, NULL);
  678. }
  679. static int set_flag(int f)
  680. {
  681. struct Flag *flag;
  682. /* Flag is not valid if there are no flags to set */
  683. if (!st->n_flags) {
  684. fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
  685. return (1);
  686. }
  687. /* Find flag with corrrect keyword */
  688. flag = &st->first_flag;
  689. while (flag != NULL) {
  690. if (flag->key == f) {
  691. flag->answer = 1;
  692. return (0);
  693. }
  694. flag = flag->next_flag;
  695. }
  696. fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
  697. return (1);
  698. }
  699. /* contents() is used to find things strings with characters like commas and
  700. * dashes.
  701. */
  702. static int contains(const char *s, int c)
  703. {
  704. while (*s) {
  705. if (*s == c)
  706. return (1);
  707. s++;
  708. }
  709. return (0);
  710. }
  711. static int is_option(const char *string)
  712. {
  713. int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
  714. return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
  715. }
  716. static int match_option_1(const char *string, const char *option)
  717. {
  718. const char *next;
  719. if (*string == '\0')
  720. return 1;
  721. if (*option == '\0')
  722. return 0;
  723. if (*string == *option && match_option_1(string + 1, option + 1))
  724. return 1;
  725. if (*option == '_' && match_option_1(string, option + 1))
  726. return 1;
  727. next = strchr(option, '_');
  728. if (!next)
  729. return 0;
  730. if (*string == '_')
  731. return match_option_1(string + 1, next + 1);
  732. return match_option_1(string, next + 1);
  733. }
  734. static int match_option(const char *string, const char *option)
  735. {
  736. return (*string == *option)
  737. && match_option_1(string + 1, option + 1);
  738. }
  739. static int set_option(const char *string)
  740. {
  741. struct Option *at_opt = NULL;
  742. struct Option *opt = NULL;
  743. int got_one;
  744. size_t key_len;
  745. char the_key[KEYLENGTH];
  746. char *ptr;
  747. for (ptr = the_key; *string != '='; ptr++, string++)
  748. *ptr = *string;
  749. *ptr = '\0';
  750. string++;
  751. /* Find option with best keyword match */
  752. got_one = 0;
  753. key_len = strlen(the_key);
  754. for (at_opt = &st->first_option; at_opt != NULL; at_opt = at_opt->next_opt) {
  755. if (!at_opt->key)
  756. continue;
  757. #if 1
  758. if (!match_option(the_key, at_opt->key))
  759. continue;
  760. #else
  761. if (strncmp(the_key, at_opt->key, key_len))
  762. continue;
  763. #endif
  764. got_one++;
  765. opt = at_opt;
  766. /* changed 1/15/91 -dpg old code is in parser.old */
  767. /* overide ambiguous check, if we get an exact match */
  768. if (strlen(at_opt->key) == key_len) {
  769. opt = at_opt;
  770. got_one = 1;
  771. break;
  772. }
  773. }
  774. if (got_one > 1) {
  775. fprintf(stderr, _("Sorry, <%s=> is ambiguous\n"), the_key);
  776. return (1);
  777. }
  778. /* If there is no match, complain */
  779. if (got_one == 0) {
  780. fprintf(stderr, _("Sorry, <%s> is not a valid parameter\n"), the_key);
  781. return (1);
  782. }
  783. /* Allocate memory where answer is stored */
  784. if (opt->count++) {
  785. opt->answer = (char *)G_realloc(opt->answer,
  786. strlen(opt->answer) + strlen(string) +
  787. 2);
  788. strcat(opt->answer, ",");
  789. strcat(opt->answer, string);
  790. }
  791. else
  792. opt->answer = G_store(string);
  793. return (0);
  794. }
  795. static int check_opts(void)
  796. {
  797. struct Option *opt;
  798. int error;
  799. int ans;
  800. error = 0;
  801. if (!st->n_opts)
  802. return (0);
  803. opt = &st->first_option;
  804. while (opt != NULL) {
  805. /* Check answer against options if any */
  806. if (opt->answer) {
  807. if (opt->multiple == 0)
  808. error += check_an_opt(opt->key, opt->type,
  809. opt->options, opt->opts, &opt->answer);
  810. else {
  811. for (ans = 0; opt->answers[ans] != '\0'; ans++)
  812. error += check_an_opt(opt->key, opt->type,
  813. opt->options, opt->opts, &opt->answers[ans]);
  814. }
  815. }
  816. /* Check answer against user's check subroutine if any */
  817. if (opt->checker)
  818. error += opt->checker(opt->answer);
  819. opt = opt->next_opt;
  820. }
  821. return (error);
  822. }
  823. static int check_an_opt(const char *key, int type, const char *options,
  824. const char **opts, char **answerp)
  825. {
  826. const char *answer = *answerp;
  827. int error;
  828. int found;
  829. error = 0;
  830. switch (type) {
  831. case TYPE_INTEGER:
  832. error = check_int(answer, opts);
  833. break;
  834. case TYPE_DOUBLE:
  835. error = check_double(answer, opts);
  836. break;
  837. case TYPE_STRING:
  838. error = check_string(answer, opts, &found);
  839. break;
  840. }
  841. switch (error) {
  842. case 0:
  843. break;
  844. case BAD_SYNTAX:
  845. fprintf(stderr,
  846. _("\nERROR: illegal range syntax for parameter <%s>\n"), key);
  847. fprintf(stderr, _(" Presented as: %s\n"), options);
  848. break;
  849. case OUT_OF_RANGE:
  850. fprintf(stderr,
  851. _("\nERROR: value <%s> out of range for parameter <%s>\n"),
  852. answer, key);
  853. fprintf(stderr, _(" Legal range: %s\n"), options);
  854. break;
  855. case MISSING_VALUE:
  856. fprintf(stderr, _("\nERROR: Missing value for parameter <%s>\n"),
  857. key);
  858. break;
  859. case AMBIGUOUS:
  860. fprintf(stderr, _("\nERROR: value <%s> ambiguous for parameter <%s>\n"),
  861. answer, key);
  862. fprintf(stderr, _(" valid options: %s\n"), options);
  863. break;
  864. case REPLACED:
  865. *answerp = G_store(opts[found]);
  866. error = 0;
  867. break;
  868. }
  869. return error;
  870. }
  871. static int check_int(const char *ans, const char **opts)
  872. {
  873. int d, i;
  874. if (sscanf(ans, "%d", &d) != 1)
  875. return MISSING_VALUE;
  876. if (!opts)
  877. return 0;
  878. for (i = 0; opts[i]; i++) {
  879. const char *opt = opts[i];
  880. int lo, hi;
  881. if (contains(opt, '-')) {
  882. if (sscanf(opt, "%d-%d", &lo, &hi) == 2) {
  883. if (d >= lo && d <= hi)
  884. return 0;
  885. }
  886. else if (sscanf(opt, "-%d", &hi) == 1) {
  887. if (d <= hi)
  888. return 0;
  889. }
  890. else if (sscanf(opt, "%d-", &lo) == 1) {
  891. if (d >= lo)
  892. return 0;
  893. }
  894. else
  895. return BAD_SYNTAX;
  896. }
  897. else {
  898. if (sscanf(opt, "%d", &lo) == 1) {
  899. if (d == lo)
  900. return 0;
  901. }
  902. else
  903. return BAD_SYNTAX;
  904. }
  905. }
  906. return OUT_OF_RANGE;
  907. }
  908. static int check_double(const char *ans, const char **opts)
  909. {
  910. double d;
  911. int i;
  912. if (sscanf(ans, "%lf", &d) != 1)
  913. return MISSING_VALUE;
  914. if (!opts)
  915. return 0;
  916. for (i = 0; opts[i]; i++) {
  917. const char *opt = opts[i];
  918. double lo, hi;
  919. if (contains(opt, '-')) {
  920. if (sscanf(opt, "%lf-%lf", &lo, &hi) == 2) {
  921. if (d >= lo && d <= hi)
  922. return 0;
  923. }
  924. else if (sscanf(opt, "-%lf", &hi) == 1) {
  925. if (d <= hi)
  926. return 0;
  927. }
  928. else if (sscanf(opt, "%lf-", &lo) == 1) {
  929. if (d >= lo)
  930. return 0;
  931. }
  932. else
  933. return BAD_SYNTAX;
  934. }
  935. else {
  936. if (sscanf(opt, "%lf", &lo) == 1) {
  937. if (d == lo)
  938. return 0;
  939. }
  940. else
  941. return BAD_SYNTAX;
  942. }
  943. }
  944. return OUT_OF_RANGE;
  945. }
  946. static int check_string(const char *ans, const char **opts, int *result)
  947. {
  948. int len = strlen(ans);
  949. int found = 0;
  950. int i;
  951. if (!opts)
  952. return 0;
  953. for (i = 0; opts[i]; i++) {
  954. if (strcmp(ans, opts[i]) == 0)
  955. return 0;
  956. if (strncmp(ans, opts[i], len) == 0) {
  957. *result = i;
  958. found++;
  959. }
  960. }
  961. switch (found) {
  962. case 0: return OUT_OF_RANGE;
  963. case 1: return REPLACED;
  964. default: return AMBIGUOUS;
  965. }
  966. }
  967. static int check_required(void)
  968. {
  969. struct Option *opt;
  970. int err;
  971. err = 0;
  972. if (!st->n_opts)
  973. return (0);
  974. opt = &st->first_option;
  975. while (opt != NULL) {
  976. if (opt->required && opt->answer == NULL) {
  977. fprintf(stderr,
  978. _("\nERROR: Required parameter <%s> not set:\n (%s).\n"),
  979. opt->key, (opt->label ? opt->label : opt->description) );
  980. err++;
  981. }
  982. opt = opt->next_opt;
  983. }
  984. return (err);
  985. }
  986. static void split_opts(void)
  987. {
  988. struct Option *opt;
  989. char *ptr1;
  990. char *ptr2;
  991. int allocated;
  992. int ans_num;
  993. int len;
  994. if (!st->n_opts)
  995. return;
  996. opt = &st->first_option;
  997. while (opt != NULL) {
  998. if ( /*opt->multiple && */ (opt->answer != NULL)) {
  999. /* Allocate some memory to store array of pointers */
  1000. allocated = 10;
  1001. opt->answers = (char **)G_malloc(allocated * sizeof(char *));
  1002. ans_num = 0;
  1003. ptr1 = opt->answer;
  1004. opt->answers[ans_num] = NULL;
  1005. for (;;) {
  1006. for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
  1007. ptr2++, len++) ;
  1008. if (len > 0) { /* skip ,, */
  1009. opt->answers[ans_num] = (char *)G_malloc(len + 1);
  1010. memcpy(opt->answers[ans_num], ptr1, len);
  1011. opt->answers[ans_num][len] = 0;
  1012. ans_num++;
  1013. if (ans_num >= allocated) {
  1014. allocated += 10;
  1015. opt->answers =
  1016. (char **)G_realloc((char *)opt->answers,
  1017. allocated * sizeof(char *));
  1018. }
  1019. opt->answers[ans_num] = NULL;
  1020. }
  1021. if (*ptr2 == '\0')
  1022. break;
  1023. ptr1 = ptr2 + 1;
  1024. if (*ptr1 == '\0')
  1025. break;
  1026. }
  1027. }
  1028. opt = opt->next_opt;
  1029. }
  1030. }
  1031. static int check_multiple_opts(void)
  1032. {
  1033. struct Option *opt;
  1034. const char *ptr;
  1035. int n_commas;
  1036. int n;
  1037. int error;
  1038. if (!st->n_opts)
  1039. return (0);
  1040. error = 0;
  1041. opt = &st->first_option;
  1042. while (opt != NULL) {
  1043. if ((opt->answer != NULL) && (opt->key_desc != NULL)) {
  1044. /* count commas */
  1045. n_commas = 1;
  1046. for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
  1047. if (*ptr == ',')
  1048. n_commas++;
  1049. /* count items */
  1050. for (n = 0; opt->answers[n] != '\0'; n++) ;
  1051. /* if not correct multiple of items */
  1052. if (n % n_commas) {
  1053. fprintf(stderr,
  1054. _("\nERROR: option <%s> must be provided in multiples of %d\n"),
  1055. opt->key, n_commas);
  1056. fprintf(stderr, _(" You provided %d items:\n"), n);
  1057. fprintf(stderr, " %s\n", opt->answer);
  1058. error++;
  1059. }
  1060. }
  1061. opt = opt->next_opt;
  1062. }
  1063. return (error);
  1064. }
  1065. /* Check for all 'new' if element already exists */
  1066. static int check_overwrite(void)
  1067. {
  1068. struct Option *opt;
  1069. char age[KEYLENGTH];
  1070. char element[KEYLENGTH];
  1071. char desc[KEYLENGTH];
  1072. int error = 0;
  1073. const char *overstr;
  1074. int over;
  1075. st->module_info.overwrite = 0;
  1076. if (!st->n_opts)
  1077. return (0);
  1078. over = 0;
  1079. /* Check the GRASS OVERWRITE variable */
  1080. if ((overstr = G__getenv("OVERWRITE"))) {
  1081. over = atoi(overstr);
  1082. }
  1083. /* Check the GRASS_OVERWRITE environment variable */
  1084. if ((overstr = getenv("GRASS_OVERWRITE"))) {
  1085. if (atoi(overstr))
  1086. over = 1;
  1087. }
  1088. if (st->overwrite || over) {
  1089. st->module_info.overwrite = 1;
  1090. /* Set the environment so that programs run in a script also obey --o */
  1091. putenv("GRASS_OVERWRITE=1");
  1092. /* No need to check options for existing files if overwrite is true */
  1093. return error;
  1094. }
  1095. opt = &st->first_option;
  1096. while (opt != NULL) {
  1097. if ((opt->answer != NULL) && (opt->gisprompt != NULL)) {
  1098. split_gisprompt(opt->gisprompt, age, element, desc);
  1099. if (strcmp(age, "new") == 0) {
  1100. int i;
  1101. for (i = 0; opt->answers[i]; i++) {
  1102. if (G_find_file(element, opt->answers[i], G_mapset())) { /* found */
  1103. if (!st->overwrite && !over) {
  1104. if (G_info_format() != G_INFO_FORMAT_GUI) {
  1105. fprintf(stderr,
  1106. _("ERROR: option <%s>: <%s> exists.\n"),
  1107. opt->key, opt->answers[i]);
  1108. }
  1109. else {
  1110. fprintf(stderr,
  1111. "GRASS_INFO_ERROR(%d,1): option <%s>: <%s> exists.\n",
  1112. getpid(), opt->key, opt->answers[i]);
  1113. fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
  1114. getpid());
  1115. }
  1116. error = 1;
  1117. }
  1118. }
  1119. }
  1120. }
  1121. }
  1122. opt = opt->next_opt;
  1123. }
  1124. return (error);
  1125. }
  1126. static void split_gisprompt(const char *gisprompt, char *age, char *element,
  1127. char *desc)
  1128. {
  1129. const char *ptr1;
  1130. char *ptr2;
  1131. for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
  1132. if (*ptr1 == ',')
  1133. break;
  1134. *ptr2 = *ptr1;
  1135. }
  1136. *ptr2 = '\0';
  1137. for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
  1138. if (*ptr1 == ',')
  1139. break;
  1140. *ptr2 = *ptr1;
  1141. }
  1142. *ptr2 = '\0';
  1143. for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
  1144. if (*ptr1 == ',')
  1145. break;
  1146. *ptr2 = *ptr1;
  1147. }
  1148. *ptr2 = '\0';
  1149. }