parser.c 31 KB

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