parser.c 30 KB

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