parser.c 29 KB

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