parser.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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. error += check_required();
  481. if (error) {
  482. if (G_verbose() > G_verbose_min())
  483. G_usage();
  484. return -1;
  485. }
  486. if (check_overwrite())
  487. return -1;
  488. return (0);
  489. }
  490. /*!
  491. * \brief Creates command to run non-interactive.
  492. *
  493. * Creates a command-line that runs the current command completely
  494. * non-interactive.
  495. *
  496. * \return pointer to a char string
  497. */
  498. char *G_recreate_command(void)
  499. {
  500. char *buff;
  501. char flg[4];
  502. char *cur;
  503. const char *tmp;
  504. struct Flag *flag;
  505. struct Option *opt;
  506. int n, len, slen;
  507. int nalloced = 0;
  508. G_debug(3, "G_recreate_command()");
  509. /* Flag is not valid if there are no flags to set */
  510. buff = G_calloc(1024, sizeof(char));
  511. nalloced += 1024;
  512. tmp = G_program_name();
  513. len = strlen(tmp);
  514. if (len >= nalloced) {
  515. nalloced += (1024 > len) ? 1024 : len + 1;
  516. buff = G_realloc(buff, nalloced);
  517. }
  518. cur = buff;
  519. strcpy(cur, tmp);
  520. cur += len;
  521. if (st->n_flags) {
  522. flag = &st->first_flag;
  523. while (flag) {
  524. if (flag->answer == 1) {
  525. flg[0] = ' ';
  526. flg[1] = '-';
  527. flg[2] = flag->key;
  528. flg[3] = '\0';
  529. slen = strlen(flg);
  530. if (len + slen >= nalloced) {
  531. nalloced +=
  532. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  533. buff = G_realloc(buff, nalloced);
  534. cur = buff + len;
  535. }
  536. strcpy(cur, flg);
  537. cur += slen;
  538. len += slen;
  539. }
  540. flag = flag->next_flag;
  541. }
  542. }
  543. opt = &st->first_option;
  544. while (opt) {
  545. if (opt->answer && opt->answers && opt->answers[0]) {
  546. slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
  547. if (len + slen >= nalloced) {
  548. nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  549. buff = G_realloc(buff, nalloced);
  550. cur = buff + len;
  551. }
  552. strcpy(cur, " ");
  553. cur++;
  554. strcpy(cur, opt->key);
  555. cur = strchr(cur, '\0');
  556. strcpy(cur, "=");
  557. cur++;
  558. if (opt->type == TYPE_STRING) {
  559. strcpy(cur, "\"");
  560. cur++;
  561. }
  562. strcpy(cur, opt->answers[0]);
  563. cur = strchr(cur, '\0');
  564. len = cur - buff;
  565. for (n = 1; opt->answers[n]; n++) {
  566. if (!opt->answers[n])
  567. break;
  568. slen = strlen(opt->answers[n]) + 2; /* +2 for , " */
  569. if (len + slen >= nalloced) {
  570. nalloced +=
  571. (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
  572. buff = G_realloc(buff, nalloced);
  573. cur = buff + len;
  574. }
  575. strcpy(cur, ",");
  576. cur++;
  577. strcpy(cur, opt->answers[n]);
  578. cur = strchr(cur, '\0');
  579. len = cur - buff;
  580. }
  581. if (opt->type == TYPE_STRING) {
  582. strcpy(cur, "\"");
  583. cur++;
  584. len = cur - buff;
  585. }
  586. }
  587. opt = opt->next_opt;
  588. }
  589. return (buff);
  590. }
  591. /*!
  592. \brief Add keyword to the list
  593. \param keyword keyword string
  594. */
  595. void G_add_keyword(const char *keyword)
  596. {
  597. if (st->n_keys >= st->n_keys_alloc) {
  598. st->n_keys_alloc += 10;
  599. st->module_info.keywords = G_realloc(st->module_info.keywords,
  600. st->n_keys_alloc * sizeof(char *));
  601. }
  602. st->module_info.keywords[st->n_keys++] = G_store(keyword);
  603. }
  604. /*!
  605. \brief Set keywords from the string
  606. \param keywords keywords separated by commas
  607. */
  608. void G_set_keywords(const char *keywords)
  609. {
  610. char **tokens = G_tokenize(keywords, ",");
  611. st->module_info.keywords = (const char **)tokens;
  612. st->n_keys = st->n_keys_alloc = G_number_of_tokens(tokens);
  613. }
  614. int G__uses_new_gisprompt(void)
  615. {
  616. struct Option *opt;
  617. char age[KEYLENGTH];
  618. char element[KEYLENGTH];
  619. char desc[KEYLENGTH];
  620. if (st->module_info.overwrite)
  621. return 1;
  622. /* figure out if any of the options use a "new" gisprompt */
  623. /* This is to see if we should spit out the --o flag */
  624. if (st->n_opts) {
  625. opt = &st->first_option;
  626. while (opt) {
  627. if (opt->gisprompt) {
  628. split_gisprompt(opt->gisprompt, age, element, desc);
  629. if (strcmp(age, "new") == 0)
  630. return 1;
  631. }
  632. opt = opt->next_opt;
  633. }
  634. }
  635. return 0;
  636. }
  637. void G__print_keywords(FILE *fd, void (*format)(FILE *, const char *))
  638. {
  639. int i;
  640. for(i = 0; i < st->n_keys; i++) {
  641. if (!format) {
  642. fprintf(fd, "%s", st->module_info.keywords[i]);
  643. }
  644. else {
  645. format(fd, st->module_info.keywords[i]);
  646. }
  647. if (i < st->n_keys - 1)
  648. fprintf(fd, ", ");
  649. }
  650. fflush(fd);
  651. }
  652. void define_keywords(void)
  653. {
  654. st->n_keys = 0;
  655. st->n_keys_alloc = 0;
  656. }
  657. /**************************************************************************
  658. *
  659. * The remaining routines are all local (static) routines used to support
  660. * the parsing process.
  661. *
  662. **************************************************************************/
  663. /*!
  664. \brief Invoke GUI dialog
  665. */
  666. static void module_gui_wx(void)
  667. {
  668. char script[GPATH_MAX];
  669. if (!st->pgm_path)
  670. st->pgm_path = G_program_name();
  671. if (!st->pgm_path)
  672. G_fatal_error(_("Unable to determine program name"));
  673. sprintf(script, "%s/etc/wxpython/gui_modules/menuform.py",
  674. getenv("GISBASE"));
  675. G_spawn(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), script, G_recreate_command(), NULL);
  676. }
  677. static int set_flag(int f)
  678. {
  679. struct Flag *flag;
  680. /* Flag is not valid if there are no flags to set */
  681. if (!st->n_flags) {
  682. fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
  683. return (1);
  684. }
  685. /* Find flag with corrrect keyword */
  686. flag = &st->first_flag;
  687. while (flag) {
  688. if (flag->key == f) {
  689. flag->answer = 1;
  690. return (0);
  691. }
  692. flag = flag->next_flag;
  693. }
  694. fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
  695. return (1);
  696. }
  697. /* contents() is used to find things strings with characters like commas and
  698. * dashes.
  699. */
  700. static int contains(const char *s, int c)
  701. {
  702. while (*s) {
  703. if (*s == c)
  704. return (1);
  705. s++;
  706. }
  707. return (0);
  708. }
  709. static int is_option(const char *string)
  710. {
  711. int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
  712. return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
  713. }
  714. static int match_option_1(const char *string, const char *option)
  715. {
  716. const char *next;
  717. if (*string == '\0')
  718. return 1;
  719. if (*option == '\0')
  720. return 0;
  721. if (*string == *option && match_option_1(string + 1, option + 1))
  722. return 1;
  723. if (*option == '_' && match_option_1(string, option + 1))
  724. return 1;
  725. next = strchr(option, '_');
  726. if (!next)
  727. return 0;
  728. if (*string == '_')
  729. return match_option_1(string + 1, next + 1);
  730. return match_option_1(string, next + 1);
  731. }
  732. static int match_option(const char *string, const char *option)
  733. {
  734. return (*string == *option)
  735. && match_option_1(string + 1, option + 1);
  736. }
  737. static int set_option(const char *string)
  738. {
  739. struct Option *at_opt = NULL;
  740. struct Option *opt = NULL;
  741. int got_one;
  742. size_t key_len;
  743. char the_key[KEYLENGTH];
  744. char *ptr;
  745. for (ptr = the_key; *string != '='; ptr++, string++)
  746. *ptr = *string;
  747. *ptr = '\0';
  748. string++;
  749. /* Find option with best keyword match */
  750. got_one = 0;
  751. key_len = strlen(the_key);
  752. for (at_opt = &st->first_option; at_opt; at_opt = at_opt->next_opt) {
  753. if (!at_opt->key)
  754. continue;
  755. #if 1
  756. if (!match_option(the_key, at_opt->key))
  757. continue;
  758. #else
  759. if (strncmp(the_key, at_opt->key, key_len))
  760. continue;
  761. #endif
  762. got_one++;
  763. opt = at_opt;
  764. /* changed 1/15/91 -dpg old code is in parser.old */
  765. /* overide ambiguous check, if we get an exact match */
  766. if (strlen(at_opt->key) == key_len) {
  767. opt = at_opt;
  768. got_one = 1;
  769. break;
  770. }
  771. }
  772. if (got_one > 1) {
  773. fprintf(stderr, _("Sorry, <%s=> is ambiguous\n"), the_key);
  774. return (1);
  775. }
  776. /* If there is no match, complain */
  777. if (got_one == 0) {
  778. fprintf(stderr, _("Sorry, <%s> is not a valid parameter\n"), the_key);
  779. return (1);
  780. }
  781. /* Allocate memory where answer is stored */
  782. if (opt->count++) {
  783. opt->answer = G_realloc(opt->answer,
  784. strlen(opt->answer) + strlen(string) + 2);
  785. strcat(opt->answer, ",");
  786. strcat(opt->answer, string);
  787. }
  788. else
  789. opt->answer = G_store(string);
  790. return (0);
  791. }
  792. static int check_opts(void)
  793. {
  794. struct Option *opt;
  795. int error;
  796. int ans;
  797. error = 0;
  798. if (!st->n_opts)
  799. return (0);
  800. opt = &st->first_option;
  801. while (opt) {
  802. /* Check answer against options if any */
  803. if (opt->answer) {
  804. if (opt->multiple == 0)
  805. error += check_an_opt(opt->key, opt->type,
  806. opt->options, opt->opts, &opt->answer);
  807. else {
  808. for (ans = 0; opt->answers[ans] != '\0'; ans++)
  809. error += check_an_opt(opt->key, opt->type,
  810. opt->options, opt->opts, &opt->answers[ans]);
  811. }
  812. }
  813. /* Check answer against user's check subroutine if any */
  814. if (opt->checker)
  815. error += opt->checker(opt->answer);
  816. opt = opt->next_opt;
  817. }
  818. return (error);
  819. }
  820. static int check_an_opt(const char *key, int type, const char *options,
  821. const char **opts, char **answerp)
  822. {
  823. const char *answer = *answerp;
  824. int error;
  825. int found;
  826. error = 0;
  827. switch (type) {
  828. case TYPE_INTEGER:
  829. error = check_int(answer, opts);
  830. break;
  831. case TYPE_DOUBLE:
  832. error = check_double(answer, opts);
  833. break;
  834. case TYPE_STRING:
  835. error = check_string(answer, opts, &found);
  836. break;
  837. }
  838. switch (error) {
  839. case 0:
  840. break;
  841. case BAD_SYNTAX:
  842. fprintf(stderr,
  843. _("\nERROR: illegal range syntax for parameter <%s>\n"), key);
  844. fprintf(stderr, _(" Presented as: %s\n"), options);
  845. break;
  846. case OUT_OF_RANGE:
  847. fprintf(stderr,
  848. _("\nERROR: value <%s> out of range for parameter <%s>\n"),
  849. answer, key);
  850. fprintf(stderr, _(" Legal range: %s\n"), options);
  851. break;
  852. case MISSING_VALUE:
  853. fprintf(stderr, _("\nERROR: Missing value for parameter <%s>\n"),
  854. key);
  855. break;
  856. case AMBIGUOUS:
  857. fprintf(stderr, _("\nERROR: value <%s> ambiguous for parameter <%s>\n"),
  858. answer, key);
  859. fprintf(stderr, _(" valid options: %s\n"), options);
  860. break;
  861. case REPLACED:
  862. *answerp = G_store(opts[found]);
  863. error = 0;
  864. break;
  865. }
  866. return error;
  867. }
  868. static int check_int(const char *ans, const char **opts)
  869. {
  870. int d, i;
  871. if (sscanf(ans, "%d", &d) != 1)
  872. return MISSING_VALUE;
  873. if (!opts)
  874. return 0;
  875. for (i = 0; opts[i]; i++) {
  876. const char *opt = opts[i];
  877. int lo, hi;
  878. if (contains(opt, '-')) {
  879. if (sscanf(opt, "%d-%d", &lo, &hi) == 2) {
  880. if (d >= lo && d <= hi)
  881. return 0;
  882. }
  883. else if (sscanf(opt, "-%d", &hi) == 1) {
  884. if (d <= hi)
  885. return 0;
  886. }
  887. else if (sscanf(opt, "%d-", &lo) == 1) {
  888. if (d >= lo)
  889. return 0;
  890. }
  891. else
  892. return BAD_SYNTAX;
  893. }
  894. else {
  895. if (sscanf(opt, "%d", &lo) == 1) {
  896. if (d == lo)
  897. return 0;
  898. }
  899. else
  900. return BAD_SYNTAX;
  901. }
  902. }
  903. return OUT_OF_RANGE;
  904. }
  905. static int check_double(const char *ans, const char **opts)
  906. {
  907. double d;
  908. int i;
  909. if (sscanf(ans, "%lf", &d) != 1)
  910. return MISSING_VALUE;
  911. if (!opts)
  912. return 0;
  913. for (i = 0; opts[i]; i++) {
  914. const char *opt = opts[i];
  915. double lo, hi;
  916. if (contains(opt, '-')) {
  917. if (sscanf(opt, "%lf-%lf", &lo, &hi) == 2) {
  918. if (d >= lo && d <= hi)
  919. return 0;
  920. }
  921. else if (sscanf(opt, "-%lf", &hi) == 1) {
  922. if (d <= hi)
  923. return 0;
  924. }
  925. else if (sscanf(opt, "%lf-", &lo) == 1) {
  926. if (d >= lo)
  927. return 0;
  928. }
  929. else
  930. return BAD_SYNTAX;
  931. }
  932. else {
  933. if (sscanf(opt, "%lf", &lo) == 1) {
  934. if (d == lo)
  935. return 0;
  936. }
  937. else
  938. return BAD_SYNTAX;
  939. }
  940. }
  941. return OUT_OF_RANGE;
  942. }
  943. static int check_string(const char *ans, const char **opts, int *result)
  944. {
  945. int len = strlen(ans);
  946. int found = 0;
  947. int i;
  948. if (!opts)
  949. return 0;
  950. for (i = 0; opts[i]; i++) {
  951. if (strcmp(ans, opts[i]) == 0)
  952. return 0;
  953. if (strncmp(ans, opts[i], len) == 0) {
  954. *result = i;
  955. found++;
  956. }
  957. }
  958. switch (found) {
  959. case 0: return OUT_OF_RANGE;
  960. case 1: return REPLACED;
  961. default: return AMBIGUOUS;
  962. }
  963. }
  964. static int check_required(void)
  965. {
  966. struct Option *opt;
  967. int err;
  968. err = 0;
  969. if (!st->n_opts)
  970. return (0);
  971. opt = &st->first_option;
  972. while (opt) {
  973. if (opt->required && !opt->answer) {
  974. fprintf(stderr,
  975. _("\nERROR: Required parameter <%s> not set:\n (%s).\n"),
  976. opt->key, (opt->label ? opt->label : opt->description) );
  977. err++;
  978. }
  979. opt = opt->next_opt;
  980. }
  981. return (err);
  982. }
  983. static void split_opts(void)
  984. {
  985. struct Option *opt;
  986. const char *ptr1;
  987. const char *ptr2;
  988. int allocated;
  989. int ans_num;
  990. int len;
  991. if (!st->n_opts)
  992. return;
  993. opt = &st->first_option;
  994. while (opt) {
  995. if ( /*opt->multiple && */ opt->answer) {
  996. /* Allocate some memory to store array of pointers */
  997. allocated = 10;
  998. opt->answers = G_malloc(allocated * sizeof(char *));
  999. ans_num = 0;
  1000. ptr1 = opt->answer;
  1001. opt->answers[ans_num] = NULL;
  1002. for (;;) {
  1003. for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
  1004. ptr2++, len++) ;
  1005. if (len > 0) { /* skip ,, */
  1006. opt->answers[ans_num] = G_malloc(len + 1);
  1007. memcpy(opt->answers[ans_num], ptr1, len);
  1008. opt->answers[ans_num][len] = 0;
  1009. ans_num++;
  1010. if (ans_num >= allocated) {
  1011. allocated += 10;
  1012. opt->answers = G_realloc(opt->answers,
  1013. allocated * sizeof(char *));
  1014. }
  1015. opt->answers[ans_num] = NULL;
  1016. }
  1017. if (*ptr2 == '\0')
  1018. break;
  1019. ptr1 = ptr2 + 1;
  1020. if (*ptr1 == '\0')
  1021. break;
  1022. }
  1023. }
  1024. opt = opt->next_opt;
  1025. }
  1026. }
  1027. static int check_multiple_opts(void)
  1028. {
  1029. struct Option *opt;
  1030. const char *ptr;
  1031. int n_commas;
  1032. int n;
  1033. int error;
  1034. if (!st->n_opts)
  1035. return (0);
  1036. error = 0;
  1037. opt = &st->first_option;
  1038. while (opt) {
  1039. if (opt->answer && opt->key_desc) {
  1040. /* count commas */
  1041. n_commas = 1;
  1042. for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
  1043. if (*ptr == ',')
  1044. n_commas++;
  1045. /* count items */
  1046. for (n = 0; opt->answers[n] != '\0'; n++) ;
  1047. /* if not correct multiple of items */
  1048. if (n % n_commas) {
  1049. fprintf(stderr,
  1050. _("\nERROR: option <%s> must be provided in multiples of %d\n"),
  1051. opt->key, n_commas);
  1052. fprintf(stderr, _(" You provided %d items:\n"), n);
  1053. fprintf(stderr, " %s\n", opt->answer);
  1054. error++;
  1055. }
  1056. }
  1057. opt = opt->next_opt;
  1058. }
  1059. return (error);
  1060. }
  1061. /* Check for all 'new' if element already exists */
  1062. static int check_overwrite(void)
  1063. {
  1064. struct Option *opt;
  1065. char age[KEYLENGTH];
  1066. char element[KEYLENGTH];
  1067. char desc[KEYLENGTH];
  1068. int error = 0;
  1069. const char *overstr;
  1070. int over;
  1071. st->module_info.overwrite = 0;
  1072. if (!st->n_opts)
  1073. return (0);
  1074. over = 0;
  1075. /* Check the GRASS OVERWRITE variable */
  1076. if ((overstr = G__getenv("OVERWRITE"))) {
  1077. over = atoi(overstr);
  1078. }
  1079. /* Check the GRASS_OVERWRITE environment variable */
  1080. if ((overstr = getenv("GRASS_OVERWRITE"))) {
  1081. if (atoi(overstr))
  1082. over = 1;
  1083. }
  1084. if (st->overwrite || over) {
  1085. st->module_info.overwrite = 1;
  1086. /* Set the environment so that programs run in a script also obey --o */
  1087. putenv("GRASS_OVERWRITE=1");
  1088. /* No need to check options for existing files if overwrite is true */
  1089. return error;
  1090. }
  1091. opt = &st->first_option;
  1092. while (opt) {
  1093. if (opt->answer && opt->gisprompt) {
  1094. split_gisprompt(opt->gisprompt, age, element, desc);
  1095. if (strcmp(age, "new") == 0) {
  1096. int i;
  1097. for (i = 0; opt->answers[i]; i++) {
  1098. if (G_find_file(element, opt->answers[i], G_mapset())) { /* found */
  1099. if (!st->overwrite && !over) {
  1100. if (G_info_format() != G_INFO_FORMAT_GUI) {
  1101. fprintf(stderr,
  1102. _("ERROR: option <%s>: <%s> exists.\n"),
  1103. opt->key, opt->answers[i]);
  1104. }
  1105. else {
  1106. fprintf(stderr,
  1107. "GRASS_INFO_ERROR(%d,1): option <%s>: <%s> exists.\n",
  1108. getpid(), opt->key, opt->answers[i]);
  1109. fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
  1110. getpid());
  1111. }
  1112. error = 1;
  1113. }
  1114. }
  1115. }
  1116. }
  1117. }
  1118. opt = opt->next_opt;
  1119. }
  1120. return (error);
  1121. }
  1122. static void split_gisprompt(const char *gisprompt, char *age, char *element,
  1123. char *desc)
  1124. {
  1125. const char *ptr1;
  1126. char *ptr2;
  1127. for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
  1128. if (*ptr1 == ',')
  1129. break;
  1130. *ptr2 = *ptr1;
  1131. }
  1132. *ptr2 = '\0';
  1133. for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
  1134. if (*ptr1 == ',')
  1135. break;
  1136. *ptr2 = *ptr1;
  1137. }
  1138. *ptr2 = '\0';
  1139. for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
  1140. if (*ptr1 == ',')
  1141. break;
  1142. *ptr2 = *ptr1;
  1143. }
  1144. *ptr2 = '\0';
  1145. }