parser.c 30 KB

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