GRASS GIS 7 Programmer's Manual  7.7.svn(2018)-r73390
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parser.c
Go to the documentation of this file.
1 /*!
2  * \file lib/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  * Overview table: <a href="parser_standard_options.html">Parser standard options</a>
68  *
69  * (C) 2001-2015 by the GRASS Development Team
70  *
71  * This program is free software under the GNU General Public License
72  * (>=v2). Read the file COPYING that comes with GRASS for details.
73  *
74  * \author Original author CERL
75  * \author Soeren Gebbert added Dec. 2009 WPS process_description document
76  */
77 
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <unistd.h>
82 
83 #include <grass/gis.h>
84 #include <grass/spawn.h>
85 #include <grass/glocale.h>
86 
87 #include "parser_local_proto.h"
88 
89 enum opt_error {
93  AMBIGUOUS = 4,
95 };
96 
97 #define KEYLENGTH 64
98 
99 #define MAX_MATCHES 50
100 
101 /* initialize the global struct */
102 struct state state;
103 struct state *st = &state;
104 
105 /* local prototypes */
106 static void set_flag(int);
107 static int contains(const char *, int);
108 static int valid_option_name(const char *);
109 static int is_option(const char *);
110 static int match_option_1(const char *, const char *);
111 static int match_option(const char *, const char *);
112 static void set_option(const char *);
113 static void check_opts(void);
114 static void check_an_opt(const char *, int, const char *, const char **, char **);
115 static int check_int(const char *, const char **);
116 static int check_double(const char *, const char **);
117 static int check_string(const char *, const char **, int *);
118 static void check_required(void);
119 static void split_opts(void);
120 static void check_multiple_opts(void);
121 static int check_overwrite(void);
122 static void define_keywords(void);
123 static void split_gisprompt(const char *, char *, char *, char *);
124 static int module_gui_wx(void);
125 static void append_error(const char *);
126 static const char *get_renamed_option(const char *);
127 
128 /*!
129  * \brief Disables the ability of the parser to operate interactively.
130  *
131  * When a user calls a command with no arguments on the command line,
132  * the parser will enter its own standardized interactive session in
133  * which all flags and options are presented to the user for input. A
134  * call to G_disable_interactive() disables the parser's interactive
135  * prompting.
136  *
137  */
138 
140 {
141  st->no_interactive = 1;
142 }
143 
144 /*!
145  * \brief Initializes a Flag struct.
146  *
147  * Allocates memory for the Flag structure and returns a pointer to
148  * this memory.
149  *
150  * Flags are always represented by single letters. A user "turns them
151  * on" at the command line using a minus sign followed by the
152  * character representing the flag.
153  *
154  * \return Pointer to a Flag struct
155  */
156 struct Flag *G_define_flag(void)
157 {
158  struct Flag *flag;
159  struct Item *item;
160 
161  /* Allocate memory if not the first flag */
162 
163  if (st->n_flags) {
164  flag = G_malloc(sizeof(struct Flag));
165  st->current_flag->next_flag = flag;
166  }
167  else
168  flag = &st->first_flag;
169 
170  /* Zero structure */
171 
172  G_zero(flag, sizeof(struct Flag));
173 
174  st->current_flag = flag;
175  st->n_flags++;
176 
177  if (st->n_items) {
178  item = G_malloc(sizeof(struct Item));
179  st->current_item->next_item = item;
180  }
181  else
182  item = &st->first_item;
183 
184  G_zero(item, sizeof(struct Item));
185 
186  item->flag = flag;
187  item->option = NULL;
188 
189  st->current_item = item;
190  st->n_items++;
191 
192  return (flag);
193 }
194 
195 /*!
196  * \brief Initializes an Option struct.
197  *
198  * Allocates memory for the Option structure and returns a pointer to
199  * this memory.
200  *
201  * Options are provided by user on command line using the standard
202  * format: <i>key=value</i>. Options identified as REQUIRED must be
203  * specified by user on command line. The option string can either
204  * specify a range of values (e.g. "10-100") or a list of acceptable
205  * values (e.g. "red,orange,yellow"). Unless the option string is
206  * NULL, user provided input will be evaluated against this string.
207  *
208  * \return pointer to an Option struct
209  */
210 struct Option *G_define_option(void)
211 {
212  struct Option *opt;
213  struct Item *item;
214 
215  /* Allocate memory if not the first option */
216 
217  if (st->n_opts) {
218  opt = G_malloc(sizeof(struct Option));
219  st->current_option->next_opt = opt;
220  }
221  else
222  opt = &st->first_option;
223 
224  /* Zero structure */
225  G_zero(opt, sizeof(struct Option));
226 
227  opt->required = NO;
228  opt->multiple = NO;
229 
230  st->current_option = opt;
231  st->n_opts++;
232 
233  if (st->n_items) {
234  item = G_malloc(sizeof(struct Item));
235  st->current_item->next_item = item;
236  }
237  else
238  item = &st->first_item;
239 
240  G_zero(item, sizeof(struct Item));
241 
242  item->option = opt;
243 
244  st->current_item = item;
245  st->n_items++;
246 
247  return (opt);
248 }
249 
250 /*!
251  * \brief Initializes a new module.
252  *
253  * \return pointer to a GModule struct
254  */
256 {
257  struct GModule *module;
258 
259  /* Allocate memory */
260  module = &st->module_info;
261 
262  /* Zero structure */
263  G_zero(module, sizeof(struct GModule));
264 
265  /* Allocate keywords array */
266  define_keywords();
267 
268  return (module);
269 }
270 
271 /*!
272  * \brief Parse command line.
273  *
274  * The command line parameters <i>argv</i> and the number of
275  * parameters <i>argc</i> from the main() routine are passed directly
276  * to G_parser(). G_parser() accepts the command line input entered by
277  * the user, and parses this input according to the input options
278  * and/or flags that were defined by the programmer.
279  *
280  * <b>Note:</b> The only functions which can legitimately be called
281  * before G_parser() are:
282  *
283  * - G_gisinit()
284  * - G_no_gisinit()
285  * - G_define_module()
286  * - G_define_flag()
287  * - G_define_option()
288  * - G_define_standard_flag()
289  * - G_define_standard_option()
290  * - G_disable_interactive()
291  * - G_option_exclusive()
292  * - G_option_required()
293  * - G_option_requires()
294  * - G_option_requires_all()
295  * - G_option_excludes()
296  * - G_option_collective()
297  *
298  * The usual order a module calls functions is:
299  *
300  * 1. G_gisinit()
301  * 2. G_define_module()
302  * 3. G_define_standard_flag()
303  * 4. G_define_standard_option()
304  * 5. G_define_flag()
305  * 6. G_define_option()
306  * 7. G_option_exclusive()
307  * 8. G_option_required()
308  * 9. G_option_requires()
309  * 10. G_option_requires_all()
310  * 11. G_option_excludes()
311  * 12. G_option_collective()
312  * 13. G_parser()
313  *
314  * \param argc number of arguments
315  * \param argv argument list
316  *
317  * \return 0 on success
318  * \return -1 on error and calls G_usage()
319  */
320 int G_parser(int argc, char **argv)
321 {
322  int need_first_opt;
323  int opt_checked = 0;
324  const char *gui_envvar;
325  char *ptr, *tmp_name, *err;
326  int i;
327  struct Option *opt;
328  char force_gui = FALSE;
329 
330  err = NULL;
331  need_first_opt = 1;
332  tmp_name = G_store(argv[0]);
333  st->pgm_path = tmp_name;
334  st->n_errors = 0;
335  st->error = NULL;
336  st->module_info.verbose = G_verbose_std();
337  i = strlen(tmp_name);
338  while (--i >= 0) {
339  if (G_is_dirsep(tmp_name[i])) {
340  tmp_name += i + 1;
341  break;
342  }
343  }
344  G_basename(tmp_name, "exe");
345  st->pgm_name = tmp_name;
346 
347  if (!st->module_info.label && !st->module_info.description)
348  G_warning(_("Bug in UI description. Missing module description"));
349 
350  /* Stash default answers */
351 
352  opt = &st->first_option;
353  while (st->n_opts && opt) {
354  if (opt->required)
355  st->has_required = 1;
356 
357  if (!opt->key)
358  G_warning(_("Bug in UI description. Missing option key"));
359  if (!valid_option_name(opt->key))
360  G_warning(_("Bug in UI description. Option key <%s> is not valid"), opt->key);
361  if (!opt->label && !opt->description)
362  G_warning(_("Bug in UI description. Description for option <%s> missing"), opt->key ? opt->key : "?");
363 
364  /* Parse options */
365  if (opt->options) {
366  int cnt = 0;
367  char **tokens, delm[2];
368 
369  delm[0] = ',';
370  delm[1] = '\0';
371  tokens = G_tokenize(opt->options, delm);
372 
373  i = 0;
374  while (tokens[i]) {
375  G_chop(tokens[i]);
376  cnt++;
377  i++;
378  }
379 
380  opt->opts = G_calloc(cnt + 1, sizeof(const char *));
381 
382  i = 0;
383  while (tokens[i]) {
384  opt->opts[i] = G_store(tokens[i]);
385  i++;
386  }
387  G_free_tokens(tokens);
388 
389  if (opt->descriptions) {
390  delm[0] = ';';
391 
392  opt->descs = G_calloc(cnt + 1, sizeof(const char *));
393  tokens = G_tokenize(opt->descriptions, delm);
394 
395  i = 0;
396  while (tokens[i]) {
397  int j, found;
398 
399  if (!tokens[i + 1])
400  break;
401 
402  G_chop(tokens[i]);
403 
404  j = 0;
405  found = 0;
406  while (opt->opts[j]) {
407  if (strcmp(opt->opts[j], tokens[i]) == 0) {
408  found = 1;
409  break;
410  }
411  j++;
412  }
413  if (!found) {
414  G_warning(_("Bug in UI description. Option '%s' in <%s> does not exist"),
415  tokens[i], opt->key);
416  }
417  else {
418  opt->descs[j] = G_store(tokens[i + 1]);
419  }
420 
421  i += 2;
422  }
423  G_free_tokens(tokens);
424  }
425  }
426 
427  /* Copy answer */
428  if (opt->multiple && opt->answers && opt->answers[0]) {
429  opt->answer = G_malloc(strlen(opt->answers[0]) + 1);
430  strcpy(opt->answer, opt->answers[0]);
431  for (i = 1; opt->answers[i]; i++) {
432  opt->answer = G_realloc(opt->answer,
433  strlen(opt->answer) +
434  strlen(opt->answers[i]) + 2);
435  strcat(opt->answer, ",");
436  strcat(opt->answer, opt->answers[i]);
437  }
438  }
439  opt->def = opt->answer;
440  opt = opt->next_opt;
441  }
442 
443  /* If there are NO arguments, go interactive */
444  gui_envvar = G_getenv_nofatal("GUI");
445  if (argc < 2 && (st->has_required || G__has_required_rule())
446  && !st->no_interactive && isatty(0) &&
447  (gui_envvar && G_strcasecmp(gui_envvar, "text") != 0)) {
448  if (module_gui_wx() == 0)
449  return -1;
450  }
451 
452  if (argc < 2 && st->has_required && isatty(0)) {
453  G_usage();
454  return -1;
455  }
456  else if (argc >= 2) {
457 
458  /* If first arg is "help" give a usage/syntax message */
459  if (strcmp(argv[1], "help") == 0 ||
460  strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0) {
461  G_usage();
462  exit(EXIT_SUCCESS);
463  }
464 
465  /* If first arg is "--help-text" give a usage/syntax message
466  * with machine-readable sentinels */
467  if (strcmp(argv[1], "--help-text") == 0) {
468  G__usage_text();
469  exit(EXIT_SUCCESS);
470  }
471 
472  /* If first arg is "--interface-description" then print out
473  * a xml description of the task */
474  if (strcmp(argv[1], "--interface-description") == 0) {
475  G__usage_xml();
476  exit(EXIT_SUCCESS);
477  }
478 
479  /* If first arg is "--html-description" then print out
480  * a html description of the task */
481  if (strcmp(argv[1], "--html-description") == 0) {
482  G__usage_html();
483  exit(EXIT_SUCCESS);
484  }
485 
486  /* If first arg is "--rst-description" then print out
487  * a reStructuredText description of the task */
488  if (strcmp(argv[1], "--rst-description") == 0) {
489  G__usage_rest();
490  exit(EXIT_SUCCESS);
491  }
492 
493  /* If first arg is "--wps-process-description" then print out
494  * the wps process description of the task */
495  if (strcmp(argv[1], "--wps-process-description") == 0) {
497  exit(EXIT_SUCCESS);
498  }
499 
500  /* If first arg is "--script" then then generate
501  * g.parser boilerplate */
502  if (strcmp(argv[1], "--script") == 0) {
503  G__script();
504  exit(EXIT_SUCCESS);
505  }
506 
507  /* Loop through all command line arguments */
508 
509  while (--argc) {
510  ptr = *(++argv);
511 
512  if (strcmp(ptr, "help") == 0 || strcmp(ptr, "--h") == 0 ||
513  strcmp(ptr, "-help") == 0 || strcmp(ptr, "--help") == 0) {
514  G_usage();
515  exit(EXIT_SUCCESS);
516  }
517 
518  /* Overwrite option */
519  if (strcmp(ptr, "--o") == 0 || strcmp(ptr, "--overwrite") == 0) {
520  st->overwrite = 1;
521  }
522 
523  /* Verbose option */
524  else if (strcmp(ptr, "--v") == 0 || strcmp(ptr, "--verbose") == 0) {
525  char buff[32];
526 
527  /* print everything: max verbosity level */
528  st->module_info.verbose = G_verbose_max();
529  sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_max());
530  putenv(G_store(buff));
531  if (st->quiet == 1) {
532  G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --verbose."));
533  }
534  st->quiet = -1;
535  }
536 
537  /* Quiet option */
538  else if (strcmp(ptr, "--q") == 0 || strcmp(ptr, "--quiet") == 0) {
539  char buff[32];
540 
541  /* print nothing, but errors and warnings */
542  st->module_info.verbose = G_verbose_min();
543  sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_min());
544  putenv(G_store(buff));
545  if (st->quiet == -1) {
546  G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --quiet."));
547  }
548  st->quiet = 1; /* for passing to gui init */
549  }
550 
551  /* Super quiet option */
552  else if (strcmp(ptr, "--qq") == 0 ) {
553  char buff[32];
554 
555  /* print nothing, but errors */
556  st->module_info.verbose = G_verbose_min();
557  sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_min());
558  putenv(G_store(buff));
560  if (st->quiet == -1) {
561  G_warning(_("Use either --qq or --verbose flag, not both. Assuming --qq."));
562  }
563  st->quiet = 1; /* for passing to gui init */
564  }
565 
566  /* Force gui to come up */
567  else if (strcmp(ptr, "--ui") == 0) {
568  force_gui = TRUE;
569  }
570 
571  /* If we see a flag */
572  else if (*ptr == '-') {
573  while (*(++ptr))
574  set_flag(*ptr);
575 
576  }
577  /* If we see standard option format (option=val) */
578  else if (is_option(ptr)) {
579  set_option(ptr);
580  need_first_opt = 0;
581  }
582 
583  /* If we see the first option with no equal sign */
584  else if (need_first_opt && st->n_opts) {
585  st->first_option.answer = G_store(ptr);
586  st->first_option.count++;
587  need_first_opt = 0;
588  }
589 
590  /* If we see the non valid argument (no "=", just argument) */
591  else {
592  G_asprintf(&err, _("Sorry <%s> is not a valid option"), ptr);
593  append_error(err);
594  }
595 
596  }
597  }
598 
599  /* Split options where multiple answers are OK */
600  split_opts();
601 
602  /* Run the gui if it was specifically requested */
603  if (force_gui) {
604  if (module_gui_wx() != 0)
605  G_fatal_error(_("Your installation doesn't include GUI, exiting."));
606  return -1;
607  }
608 
609  /* Check multiple options */
610  check_multiple_opts();
611 
612  /* Check answers against options and check subroutines */
613  if (!opt_checked)
614  check_opts();
615 
616  /* Make sure all required options are set */
617  if (!st->suppress_required)
618  check_required();
619 
621 
622  if (st->n_errors > 0) {
623  if (G_verbose() > -1) {
624  if (G_verbose() > G_verbose_min())
625  G_usage();
626  fprintf(stderr, "\n");
627  for (i = 0; i < st->n_errors; i++) {
628  fprintf(stderr, "%s: %s\n", _("ERROR"), st->error[i]);
629  }
630  }
631  return -1;
632  }
633 
634  if (!st->suppress_overwrite) {
635  if (check_overwrite())
636  return -1;
637  }
638 
639  return 0;
640 }
641 
642 /*!
643  * \brief Creates command to run non-interactive.
644  *
645  * Creates a command-line that runs the current command completely
646  * non-interactive.
647  *
648  * \param original_path TRUE if original path should be used, FALSE for
649  * stripped and clean name of the module
650  * \return pointer to a char string
651  */
652 char *recreate_command(int original_path)
653 {
654  char *buff;
655  char flg[4];
656  char *cur;
657  const char *tmp;
658  struct Flag *flag;
659  struct Option *opt;
660  int n, len, slen;
661  int nalloced = 0;
662 
663  G_debug(3, "G_recreate_command()");
664 
665  /* Flag is not valid if there are no flags to set */
666 
667  buff = G_calloc(1024, sizeof(char));
668  nalloced += 1024;
669  if (original_path)
670  tmp = G_original_program_name();
671  else
672  tmp = G_program_name();
673  len = strlen(tmp);
674  if (len >= nalloced) {
675  nalloced += (1024 > len) ? 1024 : len + 1;
676  buff = G_realloc(buff, nalloced);
677  }
678  cur = buff;
679  strcpy(cur, tmp);
680  cur += len;
681 
682  if (st->overwrite) {
683  slen = strlen(" --overwrite");
684  if (len + slen >= nalloced) {
685  nalloced += (1024 > len) ? 1024 : len + 1;
686  buff = G_realloc(buff, nalloced);
687  }
688  strcpy(cur, " --overwrite");
689  cur += slen;
690  len += slen;
691  }
692 
693  if (st->module_info.verbose != G_verbose_std()) {
694  char *sflg;
695  if (st->module_info.verbose == G_verbose_max())
696  sflg = " --verbose";
697  else
698  sflg = " --quiet";
699 
700  slen = strlen(sflg);
701  if (len + slen >= nalloced) {
702  nalloced += (1024 > len) ? 1024 : len + 1;
703  buff = G_realloc(buff, nalloced);
704  }
705  strcpy(cur, sflg);
706  cur += slen;
707  len += slen;
708  }
709 
710  if (st->n_flags) {
711  flag = &st->first_flag;
712  while (flag) {
713  if (flag->answer == 1) {
714  flg[0] = ' ';
715  flg[1] = '-';
716  flg[2] = flag->key;
717  flg[3] = '\0';
718  slen = strlen(flg);
719  if (len + slen >= nalloced) {
720  nalloced +=
721  (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
722  buff = G_realloc(buff, nalloced);
723  cur = buff + len;
724  }
725  strcpy(cur, flg);
726  cur += slen;
727  len += slen;
728  }
729  flag = flag->next_flag;
730  }
731  }
732 
733  opt = &st->first_option;
734  while (st->n_opts && opt) {
735  if (opt->answer && opt->answer[0] == '\0') { /* answer = "" */
736  slen = strlen(opt->key) + 4; /* +4 for: ' ' = " " */
737  if (len + slen >= nalloced) {
738  nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
739  buff = G_realloc(buff, nalloced);
740  cur = buff + len;
741  }
742  strcpy(cur, " ");
743  cur++;
744  strcpy(cur, opt->key);
745  cur = strchr(cur, '\0');
746  strcpy(cur, "=");
747  cur++;
748  if (opt->type == TYPE_STRING) {
749  strcpy(cur, "\"\"");
750  cur += 2;
751  }
752  len = cur - buff;
753  } else if (opt->answer && opt->answers && opt->answers[0]) {
754  slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
755  if (len + slen >= nalloced) {
756  nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
757  buff = G_realloc(buff, nalloced);
758  cur = buff + len;
759  }
760  strcpy(cur, " ");
761  cur++;
762  strcpy(cur, opt->key);
763  cur = strchr(cur, '\0');
764  strcpy(cur, "=");
765  cur++;
766  if (opt->type == TYPE_STRING) {
767  strcpy(cur, "\"");
768  cur++;
769  }
770  strcpy(cur, opt->answers[0]);
771  cur = strchr(cur, '\0');
772  len = cur - buff;
773  for (n = 1; opt->answers[n]; n++) {
774  if (!opt->answers[n])
775  break;
776  slen = strlen(opt->answers[n]) + 2; /* +2 for , " */
777  if (len + slen >= nalloced) {
778  nalloced +=
779  (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
780  buff = G_realloc(buff, nalloced);
781  cur = buff + len;
782  }
783  strcpy(cur, ",");
784  cur++;
785  strcpy(cur, opt->answers[n]);
786  cur = strchr(cur, '\0');
787  len = cur - buff;
788  }
789  if (opt->type == TYPE_STRING) {
790  strcpy(cur, "\"");
791  cur++;
792  len = cur - buff;
793  }
794  }
795  opt = opt->next_opt;
796  }
797 
798  return buff;
799 }
800 
801 /*!
802  * \brief Creates command to run non-interactive.
803  *
804  * Creates a command-line that runs the current command completely
805  * non-interactive.
806  *
807  * \return pointer to a char string
808  */
810 {
811  return recreate_command(FALSE);
812 }
813 
814 /* TODO: update to docs of these 3 functions to whatever general purpose
815  * they have now. */
816 /*!
817  * \brief Creates command to run non-interactive.
818  *
819  * Creates a command-line that runs the current command completely
820  * non-interactive.
821  *
822  * This gives the same as G_recreate_command() but the original path
823  * from the command line is used instead of the module name only.
824  *
825  * \return pointer to a char string
826  */
828 {
829  return recreate_command(TRUE);
830 }
831 
832 /*!
833  \brief Add keyword to the list
834 
835  \param keyword keyword string
836 */
837 void G_add_keyword(const char *keyword)
838 {
839  if (st->n_keys >= st->n_keys_alloc) {
840  st->n_keys_alloc += 10;
841  st->module_info.keywords = G_realloc(st->module_info.keywords,
842  st->n_keys_alloc * sizeof(char *));
843  }
844 
845  st->module_info.keywords[st->n_keys++] = G_store(keyword);
846 }
847 
848 /*!
849  \brief Set keywords from the string
850 
851  \param keywords keywords separated by commas
852 */
853 void G_set_keywords(const char *keywords)
854 {
855  char **tokens = G_tokenize(keywords, ",");
856  st->module_info.keywords = (const char **)tokens;
857  st->n_keys = st->n_keys_alloc = G_number_of_tokens(tokens);
858 }
859 
860 
862 {
863  struct Option *opt;
864  char age[KEYLENGTH];
865  char element[KEYLENGTH];
866  char desc[KEYLENGTH];
867 
868  if (st->module_info.overwrite)
869  return 1;
870 
871  /* figure out if any of the options use a "new" gisprompt */
872  /* This is to see if we should spit out the --o flag */
873  if (st->n_opts) {
874  opt = &st->first_option;
875  while (opt) {
876  if (opt->gisprompt) {
877  split_gisprompt(opt->gisprompt, age, element, desc);
878  if (strcmp(age, "new") == 0)
879  return 1;
880  }
881  opt = opt->next_opt;
882  }
883  }
884 
885  return 0;
886 }
887 
888 /*!
889  \brief Print list of keywords (internal use only)
890 
891  If <em>format</em> function is NULL than list of keywords is printed
892  comma-separated.
893 
894  \param[out] fd file where to print
895  \param format pointer to print function
896 */
897 void G__print_keywords(FILE *fd, void (*format)(FILE *, const char *))
898 {
899  int i;
900 
901  for(i = 0; i < st->n_keys; i++) {
902  if (!format) {
903  fprintf(fd, "%s", st->module_info.keywords[i]);
904  }
905  else {
906  format(fd, st->module_info.keywords[i]);
907  }
908  if (i < st->n_keys - 1)
909  fprintf(fd, ", ");
910  }
911 
912  fflush(fd);
913 }
914 
915 /*!
916  \brief Get overwrite value
917 
918  \return 1 overwrite enabled
919  \return 0 overwrite disabled
920 */
922 {
923  return st->module_info.overwrite;
924 }
925 
926 void define_keywords(void)
927 {
928  st->n_keys = 0;
929  st->n_keys_alloc = 0;
930 }
931 
932 /**************************************************************************
933  *
934  * The remaining routines are all local (static) routines used to support
935  * the parsing process.
936  *
937  **************************************************************************/
938 
939 /*!
940  \brief Invoke GUI dialog
941 */
942 int module_gui_wx(void)
943 {
944  char script[GPATH_MAX];
945 
946  /* TODO: the 4 following lines seems useless */
947  if (!st->pgm_path)
948  st->pgm_path = G_program_name();
949  if (!st->pgm_path)
950  G_fatal_error(_("Unable to determine program name"));
951 
952  sprintf(script, "%s/gui/wxpython/gui_core/forms.py",
953  getenv("GISBASE"));
954  if (access(script, F_OK) != -1)
955  G_spawn(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"),
957  else
958  return -1;
959 
960  return 0;
961 }
962 
963 void set_flag(int f)
964 {
965  struct Flag *flag;
966  char *err;
967 
968  err = NULL;
969 
970  /* Flag is not valid if there are no flags to set */
971  if (!st->n_flags) {
972  G_asprintf(&err, _("%s: Sorry, <%c> is not a valid flag"), G_program_name(), f);
973  append_error(err);
974  return;
975  }
976 
977  /* Find flag with corrrect keyword */
978  flag = &st->first_flag;
979  while (flag) {
980  if (flag->key == f) {
981  flag->answer = 1;
982  if (flag->suppress_required)
983  st->suppress_required = 1;
984  if (flag->suppress_overwrite)
985  st->suppress_overwrite = 1;
986  return;
987  }
988  flag = flag->next_flag;
989  }
990 
991  G_asprintf(&err, _("%s: Sorry, <%c> is not a valid flag"), G_program_name(), f);
992  append_error(err);
993 }
994 
995 /* contents() is used to find things strings with characters like commas and
996  * dashes.
997  */
998 int contains(const char *s, int c)
999 {
1000  while (*s) {
1001  if (*s == c)
1002  return TRUE;
1003  s++;
1004  }
1005  return FALSE;
1006 }
1007 
1008 int valid_option_name(const char *string)
1009 {
1010  int m = strlen(string);
1011  int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
1012 
1013  if (!m)
1014  return 0;
1015 
1016  if (m != n)
1017  return 0;
1018 
1019  if (string[m-1] == '_')
1020  return 0;
1021 
1022  return 1;
1023 }
1024 
1025 int is_option(const char *string)
1026 {
1027  int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
1028 
1029  return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
1030 }
1031 
1032 int match_option_1(const char *string, const char *option)
1033 {
1034  const char *next;
1035 
1036  if (*string == '\0')
1037  return 1;
1038 
1039  if (*option == '\0')
1040  return 0;
1041 
1042  if (*string == *option && match_option_1(string + 1, option + 1))
1043  return 1;
1044 
1045  if (*option == '_' && match_option_1(string, option + 1))
1046  return 1;
1047 
1048  next = strchr(option, '_');
1049  if (!next)
1050  return 0;
1051 
1052  if (*string == '_')
1053  return match_option_1(string + 1, next + 1);
1054 
1055  return match_option_1(string, next + 1);
1056 }
1057 
1058 int match_option(const char *string, const char *option)
1059 {
1060  return (*string == *option)
1061  && match_option_1(string + 1, option + 1);
1062 }
1063 
1064 void set_option(const char *string)
1065 {
1066  struct Option *at_opt = NULL;
1067  struct Option *opt = NULL;
1068  size_t key_len;
1069  char the_key[KEYLENGTH];
1070  char *ptr, *err;
1071  struct Option *matches[MAX_MATCHES];
1072  int found = 0;
1073 
1074  err = NULL;
1075 
1076  for (ptr = the_key; *string != '='; ptr++, string++)
1077  *ptr = *string;
1078  *ptr = '\0';
1079  string++;
1080 
1081  /* Find option with best keyword match */
1082  key_len = strlen(the_key);
1083  for (at_opt = &st->first_option; at_opt; at_opt = at_opt->next_opt) {
1084  if (!at_opt->key)
1085  continue;
1086 
1087  if (strcmp(the_key, at_opt->key) == 0) {
1088  matches[0] = at_opt;
1089  found = 1;
1090  break;
1091  }
1092 
1093  if (strncmp(the_key, at_opt->key, key_len) == 0 ||
1094  match_option(the_key, at_opt->key)) {
1095  if (found >= MAX_MATCHES)
1096  G_fatal_error("Too many matches (limit %d)", MAX_MATCHES);
1097  matches[found++] = at_opt;
1098  }
1099  }
1100 
1101  if (found > 1) {
1102  int shortest = 0;
1103  int length = strlen(matches[0]->key);
1104  int prefix = 1;
1105  int i;
1106  for (i = 1; i < found; i++) {
1107  int len = strlen(matches[i]->key);
1108  if (len < length) {
1109  length = len;
1110  shortest = i;
1111  }
1112  }
1113  for (i = 0; prefix && i < found; i++)
1114  if (strncmp(matches[i]->key, matches[shortest]->key, length) != 0)
1115  prefix = 0;
1116  if (prefix) {
1117  matches[0] = matches[shortest];
1118  found = 1;
1119  }
1120  else {
1121  G_asprintf(&err, _("%s: Sorry, <%s=> is ambiguous"), G_program_name(), the_key);
1122  append_error(err);
1123  for (i = 0; i < found; i++) {
1124  G_asprintf(&err, _("Option <%s=> matches"), matches[i]->key);
1125  append_error(err);
1126  }
1127  return;
1128  }
1129  }
1130 
1131  if (found)
1132  opt = matches[0];
1133 
1134  /* First, check if key has been renamed in GRASS 7 */
1135  if (found == 0) {
1136  const char *renamed_key = NULL;
1137 
1138  renamed_key = get_renamed_option(the_key);
1139  if (renamed_key) {
1140  for (at_opt = &st->first_option; at_opt; at_opt = at_opt->next_opt) {
1141  if (strcmp(renamed_key, at_opt->key) == 0) {
1142  G_warning(_("Please update the usage of <%s>: "
1143  "option <%s> has been renamed to <%s>"),
1144  G_program_name(), the_key, renamed_key);
1145  opt = at_opt;
1146  found = 1;
1147  break;
1148  }
1149  }
1150  }
1151  }
1152 
1153  /* If there is no match, complain */
1154  if (found == 0) {
1155  G_asprintf(&err, _("%s: Sorry, <%s> is not a valid parameter"), G_program_name(), the_key);
1156  append_error(err);
1157  return;
1158  }
1159 
1160  if (getenv("GRASS_FULL_OPTION_NAMES") && strcmp(the_key, opt->key) != 0)
1161  G_warning(_("<%s> is an abbreviation for <%s>"), the_key, opt->key);
1162 
1163  /* Allocate memory where answer is stored */
1164  if (opt->count++) {
1165  if (!opt->multiple) {
1166  G_asprintf(&err, _("Option <%s> does not accept multiple answers"), opt->key);
1167  append_error(err);
1168  }
1169  opt->answer = G_realloc(opt->answer,
1170  strlen(opt->answer) + strlen(string) + 2);
1171  strcat(opt->answer, ",");
1172  strcat(opt->answer, string);
1173  }
1174  else
1175  opt->answer = G_store(string);
1176 }
1177 
1178 void check_opts(void)
1179 {
1180  struct Option *opt;
1181  int ans;
1182 
1183  if (!st->n_opts)
1184  return;
1185 
1186  opt = &st->first_option;
1187  while (opt) {
1188  /* Check answer against options if any */
1189 
1190  if (opt->answer) {
1191  if (opt->multiple == 0)
1192  check_an_opt(opt->key, opt->type,
1193  opt->options, opt->opts, &opt->answer);
1194  else {
1195  for (ans = 0; opt->answers[ans] != '\0'; ans++)
1196  check_an_opt(opt->key, opt->type,
1197  opt->options, opt->opts, &opt->answers[ans]);
1198  }
1199  }
1200 
1201  /* Check answer against user's check subroutine if any */
1202 
1203  if (opt->checker)
1204  opt->checker(opt->answer);
1205 
1206  opt = opt->next_opt;
1207  }
1208 }
1209 
1210 void check_an_opt(const char *key, int type, const char *options,
1211  const char **opts, char **answerp)
1212 {
1213  const char *answer = *answerp;
1214  int error;
1215  char *err;
1216  int found;
1217 
1218  error = 0;
1219  err = NULL;
1220  found = 0;
1221 
1222  switch (type) {
1223  case TYPE_INTEGER:
1224  error = check_int(answer, opts);
1225  break;
1226  case TYPE_DOUBLE:
1227  error = check_double(answer, opts);
1228  break;
1229  case TYPE_STRING:
1230  error = check_string(answer, opts, &found);
1231  break;
1232  }
1233  switch (error) {
1234  case 0:
1235  break;
1236  case BAD_SYNTAX:
1237  G_asprintf(&err,
1238  _("Illegal range syntax for parameter <%s>\n"
1239  "\tPresented as: %s"), key, options);
1240  append_error(err);
1241  break;
1242  case OUT_OF_RANGE:
1243  G_asprintf(&err,
1244  _("Value <%s> out of range for parameter <%s>\n"
1245  "\tLegal range: %s"), answer, key, options);
1246  append_error(err);
1247  break;
1248  case MISSING_VALUE:
1249  G_asprintf(&err,
1250  _("Missing value for parameter <%s>"),
1251  key);
1252  append_error(err);
1253  break;
1254  case AMBIGUOUS:
1255  G_asprintf(&err,
1256  _("Value <%s> ambiguous for parameter <%s>\n"
1257  "\tValid options: %s"), answer, key, options);
1258  append_error(err);
1259  break;
1260  case REPLACED:
1261  *answerp = G_store(opts[found]);
1262  error = 0;
1263  break;
1264  }
1265 }
1266 
1267 int check_int(const char *ans, const char **opts)
1268 {
1269  int d, i;
1270 
1271  /* "-" is reserved for standard input */
1272  if (strcmp(ans, "-") == 0)
1273  return 0;
1274 
1275  if (sscanf(ans, "%d", &d) != 1)
1276  return MISSING_VALUE;
1277 
1278  if (!opts)
1279  return 0;
1280 
1281  for (i = 0; opts[i]; i++) {
1282  const char *opt = opts[i];
1283  int lo, hi;
1284 
1285  if (contains(opt, '-')) {
1286  if (sscanf(opt, "%d-%d", &lo, &hi) == 2) {
1287  if (d >= lo && d <= hi)
1288  return 0;
1289  }
1290  else if (sscanf(opt, "-%d", &hi) == 1) {
1291  if (d <= hi)
1292  return 0;
1293  }
1294  else if (sscanf(opt, "%d-", &lo) == 1) {
1295  if (d >= lo)
1296  return 0;
1297  }
1298  else
1299  return BAD_SYNTAX;
1300  }
1301  else {
1302  if (sscanf(opt, "%d", &lo) == 1) {
1303  if (d == lo)
1304  return 0;
1305  }
1306  else
1307  return BAD_SYNTAX;
1308  }
1309  }
1310 
1311  return OUT_OF_RANGE;
1312 }
1313 
1314 int check_double(const char *ans, const char **opts)
1315 {
1316  double d;
1317  int i;
1318 
1319  /* "-" is reserved for standard input */
1320  if (strcmp(ans, "-") == 0)
1321  return 0;
1322 
1323  if (sscanf(ans, "%lf", &d) != 1)
1324  return MISSING_VALUE;
1325 
1326  if (!opts)
1327  return 0;
1328 
1329  for (i = 0; opts[i]; i++) {
1330  const char *opt = opts[i];
1331  double lo, hi;
1332 
1333  if (contains(opt, '-')) {
1334  if (sscanf(opt, "%lf-%lf", &lo, &hi) == 2) {
1335  if (d >= lo && d <= hi)
1336  return 0;
1337  }
1338  else if (sscanf(opt, "-%lf", &hi) == 1) {
1339  if (d <= hi)
1340  return 0;
1341  }
1342  else if (sscanf(opt, "%lf-", &lo) == 1) {
1343  if (d >= lo)
1344  return 0;
1345  }
1346  else
1347  return BAD_SYNTAX;
1348  }
1349  else {
1350  if (sscanf(opt, "%lf", &lo) == 1) {
1351  if (d == lo)
1352  return 0;
1353  }
1354  else
1355  return BAD_SYNTAX;
1356  }
1357  }
1358 
1359  return OUT_OF_RANGE;
1360 }
1361 
1362 int check_string(const char *ans, const char **opts, int *result)
1363 {
1364  int len = strlen(ans);
1365  int found = 0;
1366  int matches[MAX_MATCHES];
1367  int i;
1368 
1369  if (!opts)
1370  return 0;
1371 
1372  for (i = 0; opts[i]; i++) {
1373  if (strcmp(ans, opts[i]) == 0)
1374  return 0;
1375  if (strncmp(ans, opts[i], len) == 0 || match_option(ans, opts[i])) {
1376  if (found >= MAX_MATCHES)
1377  G_fatal_error("too many matches (limit %d)", MAX_MATCHES);
1378  matches[found++] = i;
1379  }
1380  }
1381 
1382  if (found > 1) {
1383  int shortest = 0;
1384  int length = strlen(opts[matches[0]]);
1385  int prefix = 1;
1386  int i;
1387  for (i = 1; i < found; i++) {
1388  int len = strlen(opts[matches[i]]);
1389  if (len < length) {
1390  length = len;
1391  shortest = i;
1392  }
1393  }
1394  for (i = 0; prefix && i < found; i++)
1395  if (strncmp(opts[matches[i]], opts[matches[shortest]], length) != 0)
1396  prefix = 0;
1397  if (prefix) {
1398  matches[0] = matches[shortest];
1399  found = 1;
1400  }
1401  }
1402 
1403  if (found == 1)
1404  *result = matches[0];
1405 
1406  if (found > 0 && getenv("GRASS_FULL_OPTION_NAMES") && strcmp(ans, opts[matches[0]]) != 0)
1407  G_warning(_("<%s> is an abbreviation for <%s>"), ans, opts[matches[0]]);
1408 
1409  switch (found) {
1410  case 0: return OUT_OF_RANGE;
1411  case 1: return REPLACED;
1412  default: return AMBIGUOUS;
1413  }
1414 }
1415 
1416 void check_required(void)
1417 {
1418  struct Option *opt;
1419  char *err;
1420 
1421  err = NULL;
1422 
1423  if (!st->n_opts)
1424  return;
1425 
1426  opt = &st->first_option;
1427  while (opt) {
1428  if (opt->required && !opt->answer) {
1429  G_asprintf(&err, _("Required parameter <%s> not set:\n"
1430  "\t(%s)"),
1431  opt->key, (opt->label ? opt->label : opt->description));
1432  append_error(err);
1433  }
1434  opt = opt->next_opt;
1435  }
1436 }
1437 
1438 void split_opts(void)
1439 {
1440  struct Option *opt;
1441  const char *ptr1;
1442  const char *ptr2;
1443  int allocated;
1444  int ans_num;
1445  int len;
1446 
1447 
1448  if (!st->n_opts)
1449  return;
1450 
1451  opt = &st->first_option;
1452  while (opt) {
1453  if ( /*opt->multiple && */ opt->answer) {
1454  /* Allocate some memory to store array of pointers */
1455  allocated = 10;
1456  opt->answers = G_malloc(allocated * sizeof(char *));
1457 
1458  ans_num = 0;
1459  ptr1 = opt->answer;
1460  opt->answers[ans_num] = NULL;
1461 
1462  for (;;) {
1463  for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
1464  ptr2++, len++) ;
1465 
1466  if (len > 0) { /* skip ,, */
1467  opt->answers[ans_num] = G_malloc(len + 1);
1468  memcpy(opt->answers[ans_num], ptr1, len);
1469  opt->answers[ans_num][len] = 0;
1470 
1471  ans_num++;
1472 
1473  if (ans_num >= allocated) {
1474  allocated += 10;
1475  opt->answers = G_realloc(opt->answers,
1476  allocated * sizeof(char *));
1477  }
1478 
1479  opt->answers[ans_num] = NULL;
1480  }
1481 
1482  if (*ptr2 == '\0')
1483  break;
1484 
1485  ptr1 = ptr2 + 1;
1486 
1487  if (*ptr1 == '\0')
1488  break;
1489  }
1490  }
1491  opt = opt->next_opt;
1492  }
1493 }
1494 
1495 void check_multiple_opts(void)
1496 {
1497  struct Option *opt;
1498  const char *ptr;
1499  int n_commas;
1500  int n;
1501  char *err;
1502 
1503  if (!st->n_opts)
1504  return;
1505 
1506  err = NULL;
1507  opt = &st->first_option;
1508  while (opt) {
1509  /* "-" is reserved from standard input/output */
1510  if (opt->answer && strcmp(opt->answer, "-") && opt->key_desc) {
1511  /* count commas */
1512  n_commas = 1;
1513  for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
1514  if (*ptr == ',')
1515  n_commas++;
1516  /* count items */
1517  for (n = 0; opt->answers[n] != '\0'; n++) ;
1518  /* if not correct multiple of items */
1519  if (n % n_commas) {
1520  G_asprintf(&err,
1521  _("Option <%s> must be provided in multiples of %d\n"
1522  "\tYou provided %d item(s): %s"),
1523  opt->key, n_commas, n, opt->answer);
1524  append_error(err);
1525 
1526  }
1527  }
1528  opt = opt->next_opt;
1529  }
1530 }
1531 
1532 /* Check for all 'new' if element already exists */
1533 int check_overwrite(void)
1534 {
1535  struct Option *opt;
1536  char age[KEYLENGTH];
1537  char element[KEYLENGTH];
1538  char desc[KEYLENGTH];
1539  int error = 0;
1540  const char *overstr;
1541  int over;
1542 
1543  st->module_info.overwrite = 0;
1544 
1545  if (!st->n_opts)
1546  return (0);
1547 
1548  over = 0;
1549  /* Check the GRASS OVERWRITE variable */
1550  if ((overstr = G_getenv_nofatal("OVERWRITE"))) {
1551  over = atoi(overstr);
1552  }
1553 
1554  /* Check the GRASS_OVERWRITE environment variable */
1555  if ((overstr = getenv("GRASS_OVERWRITE"))) {
1556  if (atoi(overstr))
1557  over = 1;
1558  }
1559 
1560  if (st->overwrite || over) {
1561  st->module_info.overwrite = 1;
1562  /* Set the environment so that programs run in a script also obey --o */
1563  putenv("GRASS_OVERWRITE=1");
1564  /* No need to check options for existing files if overwrite is true */
1565  return error;
1566  }
1567 
1568  opt = &st->first_option;
1569  while (opt) {
1570  if (opt->answer && opt->gisprompt) {
1571  split_gisprompt(opt->gisprompt, age, element, desc);
1572 
1573  if (strcmp(age, "new") == 0) {
1574  int i;
1575  char found;
1576 
1577  for (i = 0; opt->answers[i]; i++) {
1578  found = FALSE;
1579  if (strcmp(element, "file") == 0) {
1580  if (access(opt->answers[i], F_OK) == 0)
1581  found = TRUE;
1582  }
1583  else if (strcmp(element, "mapset") != 0) {
1584  /* TODO: also other elements should be
1585  probably skipped */
1586  if (G_find_file(element, opt->answers[i], G_mapset())) {
1587  found = TRUE;
1588  }
1589  }
1590 
1591  if (found) { /* found */
1592  if (!st->overwrite && !over) {
1593  if (G_verbose() > -1) {
1594  if (G_info_format() != G_INFO_FORMAT_GUI) {
1595  fprintf(stderr, _("ERROR: "));
1596  fprintf(stderr,
1597  _("option <%s>: <%s> exists. To overwrite, use the --overwrite flag"),
1598  opt->key, opt->answers[i]);
1599  fprintf(stderr, "\n");
1600  }
1601  else {
1602  fprintf(stderr, "GRASS_INFO_ERROR(%d,1): ", getpid());
1603  fprintf(stderr,
1604  _("option <%s>: <%s> exists. To overwrite, use the --overwrite flag"),
1605  opt->key, opt->answers[i]);
1606  fprintf(stderr, "\n");
1607  fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
1608  getpid());
1609  }
1610  }
1611  error = 1;
1612  }
1613  }
1614  }
1615  }
1616  }
1617  opt = opt->next_opt;
1618  }
1619 
1620  return (error);
1621 }
1622 
1623 void split_gisprompt(const char *gisprompt, char *age, char *element,
1624  char *desc)
1625 {
1626  const char *ptr1;
1627  char *ptr2;
1628 
1629  for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
1630  if (*ptr1 == ',')
1631  break;
1632  *ptr2 = *ptr1;
1633  }
1634  *ptr2 = '\0';
1635 
1636  for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
1637  if (*ptr1 == ',')
1638  break;
1639  *ptr2 = *ptr1;
1640  }
1641  *ptr2 = '\0';
1642 
1643  for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
1644  if (*ptr1 == ',')
1645  break;
1646  *ptr2 = *ptr1;
1647  }
1648  *ptr2 = '\0';
1649 }
1650 
1651 void append_error(const char *msg)
1652 {
1653  st->error = G_realloc(st->error, sizeof(char *) * (st->n_errors + 1));
1654  st->error[st->n_errors++] = G_store(msg);
1655 }
1656 
1657 const char *get_renamed_option(const char *key)
1658 {
1659  const char *pgm, *key_new;
1660  char *pgm_key;
1661 
1662  if (!st->renamed_options) {
1663  /* read renamed options from file (renamed_options) */
1664  char path[GPATH_MAX];
1665 
1666  G_snprintf(path, GPATH_MAX, "%s/etc/renamed_options", G_gisbase());
1667  st->renamed_options = G_read_key_value_file(path);
1668  }
1669 
1670  /* try to check global changes first */
1671  key_new = G_find_key_value(key, st->renamed_options);
1672  if (key_new)
1673  return key_new;
1674 
1675  /* then check module-relevant changes */
1676  pgm = G_program_name();
1677  pgm_key = (char *) G_malloc (strlen(pgm) + strlen(key) + 2);
1678  G_asprintf(&pgm_key, "%s|%s", pgm, key);
1679 
1680  key_new = G_find_key_value(pgm_key, st->renamed_options);
1681  G_free(pgm_key);
1682 
1683  return key_new;
1684 }
1685 
1686 /*!
1687  \brief Get separator string from the option.
1688 
1689  Calls G_fatal_error() on error. Allocated string can be later freed
1690  by G_free().
1691 
1692  \code
1693  char *fs;
1694  struct Option *opt_fs;
1695 
1696  opt_fs = G_define_standard_option(G_OPT_F_SEP);
1697 
1698  if (G_parser(argc, argv))
1699  exit(EXIT_FAILURE);
1700 
1701  fs = G_option_to_separator(opt_fs);
1702  \endcode
1703 
1704  \param option pointer to separator option
1705 
1706  \return allocated string with separator
1707 */
1708 char* G_option_to_separator(const struct Option *option)
1709 {
1710  char* sep;
1711 
1712  if (option->gisprompt == NULL ||
1713  strcmp(option->gisprompt, "old,separator,separator") != 0)
1714  G_fatal_error(_("%s= is not a separator option"), option->key);
1715 
1716  if (option->answer == NULL)
1717  G_fatal_error(_("No separator given for %s="), option->key);
1718 
1719  if (strcmp(option->answer, "pipe") == 0)
1720  sep = G_store("|");
1721  else if (strcmp(option->answer, "comma") == 0)
1722  sep = G_store(",");
1723  else if (strcmp(option->answer, "space") == 0)
1724  sep = G_store(" ");
1725  else if (strcmp(option->answer, "tab") == 0 ||
1726  strcmp(option->answer, "\\t") == 0)
1727  sep = G_store("\t");
1728  else if (strcmp(option->answer, "newline") == 0 ||
1729  strcmp(option->answer, "\\n") == 0)
1730  sep = G_store("\n");
1731  else
1732  sep = G_store(option->answer);
1733 
1734  G_debug(3, "G_option_to_separator(): key = %s -> sep = '%s'",
1735  option->key, sep);
1736 
1737  return sep;
1738 }
1739 
1740 /*!
1741  \brief Get an input/output file pointer from the option. If the file name is
1742  omitted or '-', it returns either stdin or stdout based on the gisprompt.
1743 
1744  Calls G_fatal_error() on error. File pointer can be later closed by
1745  G_close_option_file().
1746 
1747  \code
1748  FILE *fp_input;
1749  FILE *fp_output;
1750  struct Option *opt_input;
1751  struct Option *opt_output;
1752 
1753  opt_input = G_define_standard_option(G_OPT_F_INPUT);
1754  opt_output = G_define_standard_option(G_OPT_F_OUTPUT);
1755 
1756  if (G_parser(argc, argv))
1757  exit(EXIT_FAILURE);
1758 
1759  fp_input = G_open_option_file(opt_input);
1760  fp_output = G_open_option_file(opt_output);
1761  ...
1762  G_close_option_file(fp_input);
1763  G_close_option_file(fp_output);
1764  \endcode
1765 
1766  \param option pointer to a file option
1767 
1768  \return file pointer
1769 */
1770 FILE *G_open_option_file(const struct Option *option)
1771 {
1772  int stdinout;
1773  FILE *fp;
1774 
1775  stdinout = !option->answer || !*(option->answer) ||
1776  strcmp(option->answer, "-") == 0;
1777 
1778  if (option->gisprompt == NULL)
1779  G_fatal_error(_("%s= is not a file option"), option->key);
1780  else if (option->multiple)
1781  G_fatal_error(_("Opening multiple files not supported for %s="),
1782  option->key);
1783  else if (strcmp(option->gisprompt, "old,file,file") == 0) {
1784  if (stdinout)
1785  fp = stdin;
1786  else if ((fp = fopen(option->answer, "r")) == NULL)
1787  G_fatal_error(_("Unable to open %s file <%s>"),
1788  option->key, option->answer);
1789  } else if (strcmp(option->gisprompt, "new,file,file") == 0) {
1790  if (stdinout)
1791  fp = stdout;
1792  else if ((fp = fopen(option->answer, "w")) == NULL)
1793  G_fatal_error(_("Unable to create %s file <%s>"),
1794  option->key, option->answer);
1795  } else
1796  G_fatal_error(_("%s= is not a file option"), option->key);
1797 
1798  return fp;
1799 }
1800 
1801 /*!
1802  \brief Close an input/output file returned by G_open_option_file(). If the
1803  file pointer is stdin, stdout, or stderr, nothing happens.
1804 
1805  \param file pointer
1806 */
1807 void G_close_option_file(FILE *fp)
1808 {
1809  if (fp != stdin && fp != stdout && fp != stderr)
1810  fclose(fp);
1811 }
int G__uses_new_gisprompt(void)
Definition: parser.c:861
const char ** opts
Definition: gis.h:516
int G_info_format(void)
Get current message format.
Definition: gis/error.c:532
#define TRUE
Definition: gis.h:49
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:46
#define NO
Definition: gis.h:145
int G__has_required_rule(void)
Checks if there is any rule RULE_REQUIRED (internal use only).
void G__usage_html(void)
Print module usage description in HTML format.
Definition: parser_html.c:29
#define TYPE_DOUBLE
Definition: gis.h:142
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition: key_value1.c:84
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
const char * G_original_program_name(void)
Return original path of the executed program.
Definition: progrm_nme.c:46
int count
Definition: gis.h:530
#define KEYLENGTH
Definition: parser.c:97
char answer
Definition: gis.h:541
void G__usage_xml(void)
Print module usage description in XML format.
struct GModule * G_define_module(void)
Initializes a new module.
Definition: parser.c:255
const char * G_find_file(const char *element, char *name, const char *mapset)
Searches for a file from the mapset search list or in a specified mapset.
Definition: find_file.c:203
char * recreate_command(int original_path)
Creates command to run non-interactive.
Definition: parser.c:652
const char * descriptions
Definition: gis.h:520
void G__usage_rest(void)
Print module usage description in reStructuredText format.
Definition: parser_rest.c:29
char ** G_tokenize(const char *buf, const char *delim)
Tokenize string.
Definition: gis/token.c:48
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
#define TYPE_STRING
Definition: gis.h:143
void G__check_option_rules(void)
Check for option rules (internal use only)
int G_asprintf(char **out, const char *fmt,...)
Definition: asprintf.c:70
int G_get_overwrite()
Get overwrite value.
Definition: parser.c:921
Structure that stores module info.
Definition: gis.h:555
#define NULL
Definition: ccmath.h:32
int(* checker)(const char *)
Definition: gis.h:529
char * G_chop(char *line)
Chop leading and trailing white spaces.
Definition: strings.c:286
void G__usage_text(void)
Definition: parser_help.c:53
char * G_option_to_separator(const struct Option *option)
Get separator string from the option.
Definition: parser.c:1708
char * G_recreate_command(void)
Creates command to run non-interactive.
Definition: parser.c:809
char * G_recreate_command_original_path(void)
Creates command to run non-interactive.
Definition: parser.c:827
struct Flag * G_define_flag(void)
Initializes a Flag struct.
Definition: parser.c:156
#define G_INFO_FORMAT_GUI
Definition: gis.h:338
fd
Definition: d/range.c:69
const char * def
Definition: gis.h:523
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:160
int G_snprintf(char *str, size_t size, const char *fmt,...)
snprintf() clone.
Definition: snprintf.c:43
Definition: lidar.h:89
void G_close_option_file(FILE *fp)
Close an input/output file returned by G_open_option_file(). If the file pointer is stdin...
Definition: parser.c:1807
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
Definition: symbol/read.c:220
const char * description
Definition: gis.h:519
int G_suppress_warnings(int flag)
Suppress printing a warning message to stderr.
Definition: gis/error.c:223
#define TYPE_INTEGER
Definition: gis.h:141
int type
Definition: gis.h:512
struct state * st
Definition: parser.c:103
struct Key_Value * G_read_key_value_file(const char *file)
Read key/values pairs from file.
Definition: key_value3.c:53
int G_number_of_tokens(char **tokens)
Return number of tokens.
Definition: gis/token.c:185
int G_parser(int argc, char **argv)
Parse command line.
Definition: parser.c:320
struct Option * G_define_option(void)
Initializes an Option struct.
Definition: parser.c:210
int G_verbose_max(void)
Get max verbosity level.
Definition: verbose.c:76
#define FALSE
Definition: gis.h:53
char * answer
Definition: gis.h:522
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
const char * G_getenv_nofatal(const char *name)
Get environment variable.
Definition: env.c:381
int multiple
Definition: gis.h:514
void G_set_keywords(const char *keywords)
Set keywords from the string.
Definition: parser.c:853
Structure that stores flag info.
Definition: gis.h:538
fclose(fd)
void G__script(void)
Generate Python script-like output.
Definition: parser_script.c:24
char key
Definition: gis.h:540
int required
Definition: gis.h:513
char * G_basename(char *filename, const char *desired_ext)
Truncates filename to the base part (before the last &#39;.&#39;) if it matches the extension, otherwise leaves it unchanged.
Definition: basename.c:38
#define GPATH_MAX
Definition: gis.h:151
char suppress_overwrite
Definition: gis.h:543
const char * G_program_name(void)
Return module name.
Definition: progrm_nme.c:28
int G_verbose_std(void)
Get standard verbosity level.
Definition: verbose.c:86
struct Flag * next_flag
Definition: gis.h:547
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:55
const char * label
Definition: gis.h:518
const char ** descs
Definition: gis.h:521
void G_usage(void)
Command line help/usage message.
Definition: parser_help.c:48
FILE * G_open_option_file(const struct Option *option)
Get an input/output file pointer from the option. If the file name is omitted or &#39;-&#39;, it returns either stdin or stdout based on the gisprompt.
Definition: parser.c:1770
char suppress_required
Definition: gis.h:542
void G_disable_interactive(void)
Disables the ability of the parser to operate interactively.
Definition: parser.c:139
#define MAX_MATCHES
Definition: parser.c:99
Definition: path.h:16
void G_free_tokens(char **tokens)
Free memory allocated to tokens.
Definition: gis/token.c:204
Structure that stores option information.
Definition: gis.h:509
#define _(str)
Definition: glocale.h:13
int G_verbose_min(void)
Get min verbosity level.
Definition: verbose.c:96
char ** answers
Definition: gis.h:524
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
const char * key
Definition: gis.h:511
char buff[1024]
Definition: raster3d/cats.c:81
const char * gisprompt
Definition: gis.h:526
void G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
void G__print_keywords(FILE *fd, void(*format)(FILE *, const char *))
Print list of keywords (internal use only)
Definition: parser.c:897
void G_add_keyword(const char *keyword)
Add keyword to the list.
Definition: parser.c:837
const char * options
Definition: gis.h:515
int G_is_dirsep(char c)
Checks if a specified character is a valid directory separator character on the host system...
Definition: paths.c:45
void G__wps_print_process_description(void)
Print the WPS 1.0.0 process description XML document to stdout.
Definition: parser_wps.c:140
char * getenv()
struct state state
Definition: parser.c:102
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition: gisbase.c:41
const char * key_desc
Definition: gis.h:517
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204
opt_error
Definition: parser.c:89
struct Option * next_opt
Definition: gis.h:525
int G_spawn(const char *command,...)
Spawn new process based on command.
Definition: spawn.c:924