parser.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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 = 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(flag, sizeof(struct Flag));
  150. st->current_flag = flag;
  151. st->n_flags++;
  152. if (st->n_items) {
  153. item = G_malloc(sizeof(struct Item));
  154. st->current_item->next_item = item;
  155. }
  156. else
  157. item = &st->first_item;
  158. G_zero(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 = 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(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 = G_malloc(sizeof(struct Item));
  212. st->current_item->next_item = item;
  213. }
  214. else
  215. item = &st->first_item;
  216. G_zero(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(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) {
  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 = G_calloc(cnt + 1, sizeof(const char *));
  313. i = 0;
  314. while (tokens[i]) {
  315. opt->opts[i] = G_store(tokens[i]);
  316. i++;
  317. }
  318. G_free_tokens(tokens);
  319. if (opt->descriptions) {
  320. delm[0] = ';';
  321. opt->descs = G_calloc(cnt + 1, sizeof(const char *));
  322. tokens = G_tokenize(opt->descriptions, delm);
  323. i = 0;
  324. while (tokens[i]) {
  325. int j, found;
  326. if (!tokens[i + 1])
  327. break;
  328. j = 0;
  329. found = 0;
  330. while (opt->opts[j]) {
  331. if (strcmp(opt->opts[j], tokens[i]) == 0) {
  332. found = 1;
  333. break;
  334. }
  335. j++;
  336. }
  337. if (!found) {
  338. G_warning(_("BUG in descriptions, option '%s' in <%s> does not exist"),
  339. tokens[i], opt->key);
  340. }
  341. else {
  342. opt->descs[j] = G_store(tokens[i + 1]);
  343. }
  344. i += 2;
  345. }
  346. G_free_tokens(tokens);
  347. }
  348. }
  349. /* Copy answer */
  350. if (opt->multiple && opt->answers && opt->answers[0]) {
  351. opt->answer = G_malloc(strlen(opt->answers[0]) + 1);
  352. strcpy(opt->answer, opt->answers[0]);
  353. for (i = 1; opt->answers[i]; i++) {
  354. opt->answer = G_realloc(opt->answer,
  355. strlen(opt->answer) +
  356. strlen(opt->answers[i]) + 2);
  357. strcat(opt->answer, ",");
  358. strcat(opt->answer, opt->answers[i]);
  359. }
  360. }
  361. opt->def = opt->answer;
  362. opt = opt->next_opt;
  363. }
  364. /* If there are NO arguments, go interactive */
  365. if (argc < 2 && st->has_required && !st->no_interactive && isatty(0)) {
  366. module_gui_wx();
  367. return -1;
  368. }
  369. else if (argc < 2 && st->has_required && isatty(0)) {
  370. G_usage();
  371. return -1;
  372. }
  373. else if (argc >= 2) {
  374. /* If first arg is "help" give a usage/syntax message */
  375. if (strcmp(argv[1], "help") == 0 ||
  376. strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0) {
  377. G_usage();
  378. exit(EXIT_SUCCESS);
  379. }
  380. /* If first arg is "--help-text" give a usage/syntax message
  381. * with machine-readable sentinels */
  382. if (strcmp(argv[1], "--help-text") == 0) {
  383. G__usage_text();
  384. exit(EXIT_SUCCESS);
  385. }
  386. /* If first arg is "--interface-description" then print out
  387. * a xml description of the task */
  388. if (strcmp(argv[1], "--interface-description") == 0) {
  389. G__usage_xml();
  390. exit(EXIT_SUCCESS);
  391. }
  392. /* If first arg is "--html-description" then print out
  393. * a html description of the task */
  394. if (strcmp(argv[1], "--html-description") == 0) {
  395. G__usage_html();
  396. exit(EXIT_SUCCESS);
  397. }
  398. /* If first arg is "--wps-process-description" then print out
  399. * the wps process description of the task */
  400. if (strcmp(argv[1], "--wps-process-description") == 0) {
  401. G__wps_print_process_description();
  402. exit(EXIT_SUCCESS);
  403. }
  404. /* If first arg is "--script" then then generate
  405. * g.parser boilerplate */
  406. if (strcmp(argv[1], "--script") == 0) {
  407. G__script();
  408. exit(EXIT_SUCCESS);
  409. }
  410. /* Loop thru all command line arguments */
  411. while (--argc) {
  412. ptr = *(++argv);
  413. /* Overwrite option */
  414. if (strcmp(ptr, "--o") == 0 || strcmp(ptr, "--overwrite") == 0) {
  415. st->overwrite = 1;
  416. }
  417. /* Verbose option */
  418. else if (strcmp(ptr, "--v") == 0 || strcmp(ptr, "--verbose") == 0) {
  419. char buff[32];
  420. /* print everything: max verbosity level */
  421. st->module_info.verbose = G_verbose_max();
  422. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_max());
  423. putenv(G_store(buff));
  424. if (st->quiet == 1) {
  425. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --verbose."));
  426. }
  427. st->quiet = -1;
  428. }
  429. /* Quiet option */
  430. else if (strcmp(ptr, "--q") == 0 || strcmp(ptr, "--quiet") == 0) {
  431. char buff[32];
  432. /* print nothing, but errors and warnings */
  433. st->module_info.verbose = G_verbose_min();
  434. sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_min());
  435. putenv(G_store(buff));
  436. if (st->quiet == -1) {
  437. G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --quiet."));
  438. }
  439. st->quiet = 1; /* for passing to gui init */
  440. }
  441. /* Force gui to come up */
  442. else if (strcmp(ptr, "--ui") == 0) {
  443. force_gui = TRUE;
  444. }
  445. /* If we see a flag */
  446. else if (*ptr == '-') {
  447. while (*(++ptr))
  448. error += set_flag(*ptr);
  449. }
  450. /* If we see standard option format (option=val) */
  451. else if (is_option(ptr)) {
  452. error += set_option(ptr);
  453. need_first_opt = 0;
  454. }
  455. /* If we see the first option with no equal sign */
  456. else if (need_first_opt && st->n_opts) {
  457. st->first_option.answer = G_store(ptr);
  458. need_first_opt = 0;
  459. }
  460. /* If we see the non valid argument (no "=", just argument) */
  461. else if (contains(ptr, '=') == 0) {
  462. fprintf(stderr, _("Sorry <%s> is not a valid option\n"), ptr);
  463. error = 1;
  464. }
  465. }
  466. }
  467. /* Split options where multiple answers are OK */
  468. split_opts();
  469. /* Run the gui if it was specifically requested */
  470. if (force_gui) {
  471. module_gui_wx();
  472. return -1;
  473. }
  474. /* Check multiple options */
  475. error += check_multiple_opts();
  476. /* Check answers against options and check subroutines */
  477. if (!opt_checked)
  478. error += check_opts();
  479. /* Make sure all required options are set */
  480. if (!st->suppress_required)
  481. error += check_required();
  482. if (error) {
  483. if (G_verbose() > G_verbose_min())
  484. G_usage();
  485. return -1;
  486. }
  487. if (check_overwrite())
  488. return -1;
  489. return (0);
  490. }
  491. /*!
  492. * \brief Creates command to run non-interactive.
  493. *
  494. * Creates a command-line that runs the current command completely
  495. * non-interactive.
  496. *
  497. * \return pointer to a char string
  498. */
  499. char *G_recreate_command(void)
  500. {
  501. char *buff;
  502. char flg[4];
  503. char *cur;
  504. const char *tmp;
  505. struct Flag *flag;
  506. struct Option *opt;
  507. int n, len, slen;
  508. int nalloced = 0;
  509. G_debug(3, "G_recreate_command()");
  510. /* Flag is not valid if there are no flags to set */
  511. buff = G_calloc(1024, sizeof(char));
  512. nalloced += 1024;
  513. tmp = G_program_name();
  514. len = strlen(tmp);
  515. if (len >= nalloced) {
  516. nalloced += (1024 > len) ? 1024 : len + 1;
  517. buff = G_realloc(buff, nalloced);
  518. }
  519. cur = buff;
  520. strcpy(cur, tmp);
  521. cur += len;
  522. if (st->n_flags) {
  523. flag = &st->first_flag;
  524. while (flag) {
  525. if (flag->answer == 1) {
  526. flg[0] = ' ';
  527. flg[1] = '-';
  528. flg[2] = flag->key;
  529. flg[3] = '\0';
  530. slen = strlen(flg);
  531. if (len + slen >= nalloced) {
  532. nalloced +=
  533. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  534. buff = G_realloc(buff, nalloced);
  535. cur = buff + len;
  536. }
  537. strcpy(cur, flg);
  538. cur += slen;
  539. len += slen;
  540. }
  541. flag = flag->next_flag;
  542. }
  543. }
  544. opt = &st->first_option;
  545. while (opt) {
  546. if (opt->answer && opt->answers && opt->answers[0]) {
  547. slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
  548. if (len + slen >= nalloced) {
  549. nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  550. buff = G_realloc(buff, nalloced);
  551. cur = buff + len;
  552. }
  553. strcpy(cur, " ");
  554. cur++;
  555. strcpy(cur, opt->key);
  556. cur = strchr(cur, '\0');
  557. strcpy(cur, "=");
  558. cur++;
  559. if (opt->type == TYPE_STRING) {
  560. strcpy(cur, "\"");
  561. cur++;
  562. }
  563. strcpy(cur, opt->answers[0]);
  564. cur = strchr(cur, '\0');
  565. len = cur - buff;
  566. for (n = 1; opt->answers[n]; n++) {
  567. if (!opt->answers[n])
  568. break;
  569. slen = strlen(opt->answers[n]) + 2; /* +2 for , " */
  570. if (len + slen >= nalloced) {
  571. nalloced +=
  572. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  573. buff = G_realloc(buff, nalloced);
  574. cur = buff + len;
  575. }
  576. strcpy(cur, ",");
  577. cur++;
  578. strcpy(cur, opt->answers[n]);
  579. cur = strchr(cur, '\0');
  580. len = cur - buff;
  581. }
  582. if (opt->type == TYPE_STRING) {
  583. strcpy(cur, "\"");
  584. cur++;
  585. len = cur - buff;
  586. }
  587. }
  588. opt = opt->next_opt;
  589. }
  590. return (buff);
  591. }
  592. /*!
  593. \brief Add keyword to the list
  594. \param keyword keyword string
  595. */
  596. void G_add_keyword(const char *keyword)
  597. {
  598. if (st->n_keys >= st->n_keys_alloc) {
  599. st->n_keys_alloc += 10;
  600. st->module_info.keywords = G_realloc(st->module_info.keywords,
  601. st->n_keys_alloc * sizeof(char *));
  602. }
  603. st->module_info.keywords[st->n_keys++] = G_store(keyword);
  604. }
  605. /*!
  606. \brief Set keywords from the string
  607. \param keywords keywords separated by commas
  608. */
  609. void G_set_keywords(const char *keywords)
  610. {
  611. char **tokens = G_tokenize(keywords, ",");
  612. st->module_info.keywords = (const char **)tokens;
  613. st->n_keys = st->n_keys_alloc = G_number_of_tokens(tokens);
  614. }
  615. int G__uses_new_gisprompt(void)
  616. {
  617. struct Option *opt;
  618. char age[KEYLENGTH];
  619. char element[KEYLENGTH];
  620. char desc[KEYLENGTH];
  621. if (st->module_info.overwrite)
  622. return 1;
  623. /* figure out if any of the options use a "new" gisprompt */
  624. /* This is to see if we should spit out the --o flag */
  625. if (st->n_opts) {
  626. opt = &st->first_option;
  627. while (opt) {
  628. if (opt->gisprompt) {
  629. split_gisprompt(opt->gisprompt, age, element, desc);
  630. if (strcmp(age, "new") == 0)
  631. return 1;
  632. }
  633. opt = opt->next_opt;
  634. }
  635. }
  636. return 0;
  637. }
  638. void G__print_keywords(FILE *fd, void (*format)(FILE *, const char *))
  639. {
  640. int i;
  641. for(i = 0; i < st->n_keys; i++) {
  642. if (!format) {
  643. fprintf(fd, "%s", st->module_info.keywords[i]);
  644. }
  645. else {
  646. format(fd, st->module_info.keywords[i]);
  647. }
  648. if (i < st->n_keys - 1)
  649. fprintf(fd, ", ");
  650. }
  651. fflush(fd);
  652. }
  653. void define_keywords(void)
  654. {
  655. st->n_keys = 0;
  656. st->n_keys_alloc = 0;
  657. }
  658. /**************************************************************************
  659. *
  660. * The remaining routines are all local (static) routines used to support
  661. * the parsing process.
  662. *
  663. **************************************************************************/
  664. /*!
  665. \brief Invoke GUI dialog
  666. */
  667. static void module_gui_wx(void)
  668. {
  669. char script[GPATH_MAX];
  670. if (!st->pgm_path)
  671. st->pgm_path = G_program_name();
  672. if (!st->pgm_path)
  673. G_fatal_error(_("Unable to determine program name"));
  674. sprintf(script, "%s/etc/gui/wxpython/gui_modules/menuform.py",
  675. getenv("GISBASE"));
  676. G_spawn(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), script, G_recreate_command(), NULL);
  677. }
  678. static int set_flag(int f)
  679. {
  680. struct Flag *flag;
  681. /* Flag is not valid if there are no flags to set */
  682. if (!st->n_flags) {
  683. fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
  684. return (1);
  685. }
  686. /* Find flag with corrrect keyword */
  687. flag = &st->first_flag;
  688. while (flag) {
  689. if (flag->key == f) {
  690. flag->answer = 1;
  691. if (flag->suppress_required)
  692. st->suppress_required = 1;
  693. return (0);
  694. }
  695. flag = flag->next_flag;
  696. }
  697. fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
  698. return (1);
  699. }
  700. /* contents() is used to find things strings with characters like commas and
  701. * dashes.
  702. */
  703. static int contains(const char *s, int c)
  704. {
  705. while (*s) {
  706. if (*s == c)
  707. return (1);
  708. s++;
  709. }
  710. return (0);
  711. }
  712. static int is_option(const char *string)
  713. {
  714. int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
  715. return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
  716. }
  717. static int match_option_1(const char *string, const char *option)
  718. {
  719. const char *next;
  720. if (*string == '\0')
  721. return 1;
  722. if (*option == '\0')
  723. return 0;
  724. if (*string == *option && match_option_1(string + 1, option + 1))
  725. return 1;
  726. if (*option == '_' && match_option_1(string, option + 1))
  727. return 1;
  728. next = strchr(option, '_');
  729. if (!next)
  730. return 0;
  731. if (*string == '_')
  732. return match_option_1(string + 1, next + 1);
  733. return match_option_1(string, next + 1);
  734. }
  735. static int match_option(const char *string, const char *option)
  736. {
  737. return (*string == *option)
  738. && match_option_1(string + 1, option + 1);
  739. }
  740. static int set_option(const char *string)
  741. {
  742. struct Option *at_opt = NULL;
  743. struct Option *opt = NULL;
  744. int got_one;
  745. size_t key_len;
  746. char the_key[KEYLENGTH];
  747. char *ptr;
  748. for (ptr = the_key; *string != '='; ptr++, string++)
  749. *ptr = *string;
  750. *ptr = '\0';
  751. string++;
  752. /* Find option with best keyword match */
  753. got_one = 0;
  754. key_len = strlen(the_key);
  755. for (at_opt = &st->first_option; at_opt; at_opt = at_opt->next_opt) {
  756. if (!at_opt->key)
  757. continue;
  758. #if 1
  759. if (!match_option(the_key, at_opt->key))
  760. continue;
  761. #else
  762. if (strncmp(the_key, at_opt->key, key_len))
  763. continue;
  764. #endif
  765. got_one++;
  766. opt = at_opt;
  767. /* changed 1/15/91 -dpg old code is in parser.old */
  768. /* overide ambiguous check, if we get an exact match */
  769. if (strlen(at_opt->key) == key_len) {
  770. opt = at_opt;
  771. got_one = 1;
  772. break;
  773. }
  774. }
  775. if (got_one > 1) {
  776. fprintf(stderr, _("Sorry, <%s=> is ambiguous\n"), the_key);
  777. return (1);
  778. }
  779. /* If there is no match, complain */
  780. if (got_one == 0) {
  781. fprintf(stderr, _("Sorry, <%s> is not a valid parameter\n"), the_key);
  782. return (1);
  783. }
  784. /* Allocate memory where answer is stored */
  785. if (opt->count++) {
  786. opt->answer = G_realloc(opt->answer,
  787. strlen(opt->answer) + strlen(string) + 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) {
  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) {
  976. if (opt->required && !opt->answer) {
  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. const char *ptr1;
  990. const 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) {
  998. if ( /*opt->multiple && */ opt->answer) {
  999. /* Allocate some memory to store array of pointers */
  1000. allocated = 10;
  1001. opt->answers = 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] = 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 = G_realloc(opt->answers,
  1016. allocated * sizeof(char *));
  1017. }
  1018. opt->answers[ans_num] = NULL;
  1019. }
  1020. if (*ptr2 == '\0')
  1021. break;
  1022. ptr1 = ptr2 + 1;
  1023. if (*ptr1 == '\0')
  1024. break;
  1025. }
  1026. }
  1027. opt = opt->next_opt;
  1028. }
  1029. }
  1030. static int check_multiple_opts(void)
  1031. {
  1032. struct Option *opt;
  1033. const char *ptr;
  1034. int n_commas;
  1035. int n;
  1036. int error;
  1037. if (!st->n_opts)
  1038. return (0);
  1039. error = 0;
  1040. opt = &st->first_option;
  1041. while (opt) {
  1042. if (opt->answer && opt->key_desc) {
  1043. /* count commas */
  1044. n_commas = 1;
  1045. for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
  1046. if (*ptr == ',')
  1047. n_commas++;
  1048. /* count items */
  1049. for (n = 0; opt->answers[n] != '\0'; n++) ;
  1050. /* if not correct multiple of items */
  1051. if (n % n_commas) {
  1052. fprintf(stderr,
  1053. _("\nERROR: option <%s> must be provided in multiples of %d\n"),
  1054. opt->key, n_commas);
  1055. fprintf(stderr, _(" You provided %d items:\n"), n);
  1056. fprintf(stderr, " %s\n", opt->answer);
  1057. error++;
  1058. }
  1059. }
  1060. opt = opt->next_opt;
  1061. }
  1062. return (error);
  1063. }
  1064. /* Check for all 'new' if element already exists */
  1065. static int check_overwrite(void)
  1066. {
  1067. struct Option *opt;
  1068. char age[KEYLENGTH];
  1069. char element[KEYLENGTH];
  1070. char desc[KEYLENGTH];
  1071. int error = 0;
  1072. const char *overstr;
  1073. int over;
  1074. st->module_info.overwrite = 0;
  1075. if (!st->n_opts)
  1076. return (0);
  1077. over = 0;
  1078. /* Check the GRASS OVERWRITE variable */
  1079. if ((overstr = G__getenv("OVERWRITE"))) {
  1080. over = atoi(overstr);
  1081. }
  1082. /* Check the GRASS_OVERWRITE environment variable */
  1083. if ((overstr = getenv("GRASS_OVERWRITE"))) {
  1084. if (atoi(overstr))
  1085. over = 1;
  1086. }
  1087. if (st->overwrite || over) {
  1088. st->module_info.overwrite = 1;
  1089. /* Set the environment so that programs run in a script also obey --o */
  1090. putenv("GRASS_OVERWRITE=1");
  1091. /* No need to check options for existing files if overwrite is true */
  1092. return error;
  1093. }
  1094. opt = &st->first_option;
  1095. while (opt) {
  1096. if (opt->answer && opt->gisprompt) {
  1097. split_gisprompt(opt->gisprompt, age, element, desc);
  1098. if (strcmp(age, "new") == 0) {
  1099. int i;
  1100. for (i = 0; opt->answers[i]; i++) {
  1101. if (G_find_file(element, opt->answers[i], G_mapset())) { /* found */
  1102. if (!st->overwrite && !over) {
  1103. if (G_info_format() != G_INFO_FORMAT_GUI) {
  1104. fprintf(stderr,
  1105. _("ERROR: option <%s>: <%s> exists.\n"),
  1106. opt->key, opt->answers[i]);
  1107. }
  1108. else {
  1109. fprintf(stderr,
  1110. "GRASS_INFO_ERROR(%d,1): option <%s>: <%s> exists.\n",
  1111. getpid(), opt->key, opt->answers[i]);
  1112. fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
  1113. getpid());
  1114. }
  1115. error = 1;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. opt = opt->next_opt;
  1122. }
  1123. return (error);
  1124. }
  1125. static void split_gisprompt(const char *gisprompt, char *age, char *element,
  1126. char *desc)
  1127. {
  1128. const char *ptr1;
  1129. char *ptr2;
  1130. for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
  1131. if (*ptr1 == ',')
  1132. break;
  1133. *ptr2 = *ptr1;
  1134. }
  1135. *ptr2 = '\0';
  1136. for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
  1137. if (*ptr1 == ',')
  1138. break;
  1139. *ptr2 = *ptr1;
  1140. }
  1141. *ptr2 = '\0';
  1142. for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
  1143. if (*ptr1 == ',')
  1144. break;
  1145. *ptr2 = *ptr1;
  1146. }
  1147. *ptr2 = '\0';
  1148. }