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