parser.c 31 KB

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