parser.c 30 KB

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