89 #include "parser_local_proto.h"
100 #define MAX_MATCHES 50
107 static void set_flag(
int);
108 static int contains(
const char *,
int);
109 static int valid_option_name(
const char *);
110 static int is_option(
const char *);
111 static int match_option_1(
const char *,
const char *);
112 static int match_option(
const char *,
const char *);
113 static void set_option(
const char *);
114 static void check_opts(
void);
115 static void check_an_opt(
const char *,
int,
const char *,
const char **,
117 static int check_int(
const char *,
const char **);
118 static int check_double(
const char *,
const char **);
119 static int check_string(
const char *,
const char **,
int *);
120 static void check_required(
void);
121 static void split_opts(
void);
122 static void check_multiple_opts(
void);
123 static int check_overwrite(
void);
124 static void define_keywords(
void);
125 static int module_gui_wx(
void);
126 static void append_error(
const char *);
127 static const char *get_renamed_option(
const char *);
142 st->no_interactive = 1;
166 st->current_flag->next_flag = flag;
169 flag = &
st->first_flag;
175 st->current_flag = flag;
179 item =
G_malloc(
sizeof(
struct Item));
180 st->current_item->next_item = item;
183 item = &
st->first_item;
185 G_zero(item,
sizeof(
struct Item));
190 st->current_item = item;
220 st->current_option->next_opt = opt;
223 opt = &
st->first_option;
231 st->current_option = opt;
235 item =
G_malloc(
sizeof(
struct Item));
236 st->current_item->next_item = item;
239 item = &
st->first_item;
241 G_zero(item,
sizeof(
struct Item));
245 st->current_item = item;
261 module = &
st->module_info;
325 const char *gui_envvar;
326 char *ptr, *tmp_name, *
err;
329 char force_gui =
FALSE;
335 st->pgm_path = tmp_name;
339 i = strlen(tmp_name);
347 st->pgm_name = tmp_name;
349 if (!
st->module_info.label && !
st->module_info.description)
350 G_warning(
_(
"Bug in UI description. Missing module description"));
354 opt = &
st->first_option;
355 while (
st->n_opts && opt) {
357 st->has_required = 1;
360 G_warning(
_(
"Bug in UI description. Missing option key"));
361 if (!valid_option_name(opt->
key))
362 G_warning(
_(
"Bug in UI description. Option key <%s> is not valid"),
366 _(
"Bug in UI description. Description for option <%s> missing"),
367 opt->
key ? opt->
key :
"?");
372 char **tokens, delm[2];
411 while (opt->
opts[j]) {
412 if (strcmp(opt->
opts[j], tokens[i]) == 0) {
419 G_warning(
_(
"Bug in UI description. Option '%s' in "
420 "<%s> does not exist"),
421 tokens[i], opt->
key);
437 for (i = 1; opt->
answers[i]; i++) {
452 !
st->no_interactive && isatty(0) &&
453 (gui_envvar &&
G_strcasecmp(gui_envvar,
"text") != 0)) {
454 if (module_gui_wx() == 0)
458 if (argc < 2 && st->has_required && isatty(0)) {
462 else if (argc >= 2) {
465 if (strcmp(argv[1],
"help") == 0 || strcmp(argv[1],
"-help") == 0 ||
466 strcmp(argv[1],
"--help") == 0) {
473 if (strcmp(argv[1],
"--help-text") == 0) {
480 if (strcmp(argv[1],
"--interface-description") == 0) {
487 if (strcmp(argv[1],
"--html-description") == 0) {
494 if (strcmp(argv[1],
"--rst-description") == 0) {
501 if (strcmp(argv[1],
"--md-description") == 0) {
508 if (strcmp(argv[1],
"--wps-process-description") == 0) {
515 if (strcmp(argv[1],
"--script") == 0) {
525 if (strcmp(ptr,
"help") == 0 || strcmp(ptr,
"--h") == 0 ||
526 strcmp(ptr,
"-help") == 0 || strcmp(ptr,
"--help") == 0) {
532 if (strcmp(ptr,
"--json") == 0) {
538 if (strcmp(ptr,
"--o") == 0 || strcmp(ptr,
"--overwrite") == 0) {
543 else if (strcmp(ptr,
"--v") == 0 || strcmp(ptr,
"--verbose") == 0) {
550 if (
st->quiet == 1) {
551 G_warning(
_(
"Use either --quiet or --verbose flag, not "
552 "both. Assuming --verbose."));
558 else if (strcmp(ptr,
"--q") == 0 || strcmp(ptr,
"--quiet") == 0) {
565 if (
st->quiet == -1) {
566 G_warning(
_(
"Use either --quiet or --verbose flag, not "
567 "both. Assuming --quiet."));
573 else if (strcmp(ptr,
"--qq") == 0) {
581 if (
st->quiet == -1) {
582 G_warning(
_(
"Use either --qq or --verbose flag, not both. "
589 else if (strcmp(ptr,
"--ui") == 0) {
594 else if (*ptr ==
'-') {
599 else if (is_option(ptr)) {
605 else if (need_first_opt &&
st->n_opts) {
607 st->first_option.count++;
624 if (module_gui_wx() != 0)
625 G_fatal_error(
_(
"Your installation doesn't include GUI, exiting."));
630 check_multiple_opts();
637 if (!
st->suppress_required)
642 if (
st->n_errors > 0) {
646 fprintf(stderr,
"\n");
647 for (i = 0; i <
st->n_errors; i++) {
648 fprintf(stderr,
"%s: %s\n",
_(
"ERROR"),
st->error[i]);
655 if (print_json == 1) {
660 if (!
st->suppress_overwrite) {
661 if (check_overwrite())
689 G_debug(3,
"G_recreate_command()");
693 buff =
G_calloc(1024,
sizeof(
char));
700 if (len >= nalloced) {
701 nalloced += (1024 > len) ? 1024 : len + 1;
709 slen = strlen(
" --overwrite");
710 if (len + slen >= nalloced) {
711 nalloced += (1024 > len) ? 1024 : len + 1;
714 strcpy(cur,
" --overwrite");
728 if (len + slen >= nalloced) {
729 nalloced += (1024 > len) ? 1024 : len + 1;
738 flag = &
st->first_flag;
746 if (len + slen >= nalloced) {
748 (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
760 opt = &
st->first_option;
761 while (
st->n_opts && opt) {
763 slen = strlen(opt->
key) + 4;
764 if (len + slen >= nalloced) {
765 nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
772 cur = strchr(cur,
'\0');
782 slen = strlen(opt->
key) + strlen(opt->
answers[0]) +
784 if (len + slen >= nalloced) {
785 nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
792 cur = strchr(cur,
'\0');
800 cur = strchr(cur,
'\0');
802 for (n = 1; opt->
answers[n]; n++) {
805 slen = strlen(opt->
answers[n]) + 2;
806 if (len + slen >= nalloced) {
808 (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
815 cur = strchr(cur,
'\0');
868 if (
st->n_keys >=
st->n_keys_alloc) {
869 st->n_keys_alloc += 10;
870 st->module_info.keywords =
G_realloc(
st->module_info.keywords,
871 st->n_keys_alloc *
sizeof(
char *));
874 st->module_info.keywords[
st->n_keys++] =
G_store(keyword);
886 st->module_info.keywords = (
const char **)tokens;
895 char desc[KEYLENGTH];
897 if (
st->module_info.overwrite)
903 opt = &
st->first_option;
907 if (strcmp(age,
"new") == 0)
932 for (i = 0; i <
st->n_keys; i++) {
934 fprintf(fd,
"%s",
st->module_info.keywords[i]);
937 format(fd,
st->module_info.keywords[i]);
939 if (i < st->n_keys - 1) {
959 return st->module_info.overwrite;
962 void define_keywords(
void)
965 st->n_keys_alloc = 0;
978 int module_gui_wx(
void)
988 sprintf(script,
"%s/gui/wxpython/gui_core/forms.py", getenv(
"GISBASE"));
989 if (access(script, F_OK) != -1)
990 G_spawn(getenv(
"GRASS_PYTHON"), getenv(
"GRASS_PYTHON"), script,
1002 const char *renamed_key;
1015 flag = &
st->first_flag;
1017 if (flag->
key == f) {
1020 st->suppress_required = 1;
1022 st->suppress_overwrite = 1;
1030 renamed_key = get_renamed_option(
key);
1035 if (*renamed_key ==
'-') {
1037 if (renamed_key[1] ==
'-') {
1038 if (strcmp(renamed_key,
"--overwrite") == 0) {
1040 G_warning(
_(
"Please update the usage of <%s>: "
1041 "flag <%c> has been renamed to <%s>"),
1050 _(
"Please update the usage of <%s>: "
1051 "flag <%c> has been renamed to <%s>"),
1058 for (flag = &
st->first_flag; flag; flag = flag->
next_flag) {
1059 if (renamed_key[1] == flag->
key) {
1060 G_warning(
_(
"Please update the usage of <%s>: "
1061 "flag <%c> has been renamed to <%s>"),
1065 st->suppress_required = 1;
1067 st->suppress_overwrite = 1;
1076 for (opt = &
st->first_option; opt; opt = opt->
next_opt) {
1077 if (strcmp(renamed_key, opt->
key) == 0) {
1079 _(
"Please update the usage of <%s>: "
1080 "flag <%c> has been renamed to option <%s>"),
1097 int contains(
const char *s,
int c)
1107 int valid_option_name(
const char *
string)
1109 int m = strlen(
string);
1110 int n = strspn(
string,
"abcdefghijklmnopqrstuvwxyz0123456789_");
1118 if (
string[m - 1] ==
'_')
1124 int is_option(
const char *
string)
1126 int n = strspn(
string,
"abcdefghijklmnopqrstuvwxyz0123456789_");
1128 return n > 0 &&
string[n] ==
'=' &&
string[0] !=
'_' &&
1129 string[n - 1] !=
'_';
1132 int match_option_1(
const char *
string,
const char *option)
1136 if (*
string ==
'\0')
1139 if (*option ==
'\0')
1142 if (*
string == *option && match_option_1(
string + 1, option + 1))
1145 if (*option ==
'_' && match_option_1(
string, option + 1))
1148 next = strchr(option,
'_');
1153 return match_option_1(
string + 1, next + 1);
1155 return match_option_1(
string, next + 1);
1158 int match_option(
const char *
string,
const char *option)
1160 return (*
string == *option) && match_option_1(
string + 1, option + 1);
1163 void set_option(
const char *
string)
1168 char the_key[KEYLENGTH];
1175 for (ptr = the_key; *
string !=
'='; ptr++,
string++)
1185 key_len = strlen(the_key);
1186 for (at_opt = &
st->first_option; at_opt; at_opt = at_opt->
next_opt) {
1190 if (strcmp(the_key, at_opt->
key) == 0) {
1191 matches[0] = at_opt;
1196 if (strncmp(the_key, at_opt->
key, key_len) == 0 ||
1197 match_option(the_key, at_opt->
key)) {
1200 matches[found++] = at_opt;
1206 int length = strlen(matches[0]->
key);
1210 for (i = 1; i < found; i++) {
1211 int len = strlen(matches[i]->
key);
1218 for (i = 0; prefix && i < found; i++)
1219 if (strncmp(matches[i]->
key, matches[shortest]->
key, length) != 0)
1222 matches[0] = matches[shortest];
1229 for (i = 0; i < found; i++) {
1242 const char *renamed_key = get_renamed_option(the_key);
1247 if (*renamed_key ==
'-') {
1248 if (renamed_key[1] ==
'-')
1250 _(
"Please update the usage of <%s>: "
1251 "option <%s> has been renamed to flag <%s>"),
1255 _(
"Please update the usage of <%s>: "
1256 "option <%s> has been renamed to flag <%c>"),
1263 for (at_opt = &
st->first_option; at_opt;
1265 if (strcmp(renamed_key, at_opt->
key) == 0) {
1266 G_warning(
_(
"Please update the usage of <%s>: "
1267 "option <%s> has been renamed to <%s>"),
1285 if (getenv(
"GRASS_FULL_OPTION_NAMES") && strcmp(the_key, opt->
key) != 0)
1286 G_warning(
_(
"<%s> is an abbreviation for <%s>"), the_key, opt->
key);
1291 G_asprintf(&
err,
_(
"Option <%s> does not accept multiple answers"),
1297 strcat(opt->
answer,
",");
1298 strcat(opt->
answer,
string);
1304 void check_opts(
void)
1312 opt = &
st->first_option;
1336 void check_an_opt(
const char *
key,
int type,
const char *
options,
1337 const char **
opts,
char **answerp)
1339 const char *
answer = *answerp;
1364 _(
"Illegal range syntax for parameter <%s>\n"
1365 "\tPresented as: %s"),
1371 _(
"Value <%s> out of range for parameter <%s>\n"
1372 "\tLegal range: %s"),
1387 _(
"Value <%s> ambiguous for parameter <%s>\n"
1388 "\tValid options: %s"),
1399 int check_int(
const char *ans,
const char **
opts)
1404 if (strcmp(ans,
"-") == 0)
1410 if (sscanf(ans,
"%d", &d) != 1)
1416 for (i = 0;
opts[i]; i++) {
1417 const char *opt =
opts[i];
1420 if (contains(opt,
'-')) {
1421 if (sscanf(opt,
"%d-%d", &lo, &hi) == 2) {
1422 if (d >= lo && d <= hi)
1425 else if (sscanf(opt,
"-%d", &hi) == 1) {
1429 else if (sscanf(opt,
"%d-", &lo) == 1) {
1437 if (sscanf(opt,
"%d", &lo) == 1) {
1449 int check_double(
const char *ans,
const char **
opts)
1455 if (strcmp(ans,
"-") == 0)
1461 if (sscanf(ans,
"%lf", &d) != 1)
1467 for (i = 0;
opts[i]; i++) {
1468 const char *opt =
opts[i];
1471 if (contains(opt,
'-')) {
1472 if (sscanf(opt,
"%lf-%lf", &lo, &hi) == 2) {
1473 if (d >= lo && d <= hi)
1476 else if (sscanf(opt,
"-%lf", &hi) == 1) {
1480 else if (sscanf(opt,
"%lf-", &lo) == 1) {
1488 if (sscanf(opt,
"%lf", &lo) == 1) {
1500 int check_string(
const char *ans,
const char **
opts,
int *result)
1502 int len = strlen(ans);
1510 for (i = 0;
opts[i]; i++) {
1511 if (strcmp(ans,
opts[i]) == 0)
1513 if (strncmp(ans,
opts[i], len) == 0 || match_option(ans,
opts[i])) {
1516 matches[found++] = i;
1522 int length = strlen(
opts[matches[0]]);
1525 for (i = 1; i < found; i++) {
1526 int lengthi = strlen(
opts[matches[i]]);
1528 if (lengthi < length) {
1533 for (i = 0; prefix && i < found; i++)
1534 if (strncmp(
opts[matches[i]],
opts[matches[shortest]], length) != 0)
1537 matches[0] = matches[shortest];
1543 *result = matches[0];
1545 if (found > 0 && getenv(
"GRASS_FULL_OPTION_NAMES") &&
1546 strcmp(ans,
opts[matches[0]]) != 0)
1547 G_warning(
_(
"<%s> is an abbreviation for <%s>"), ans,
opts[matches[0]]);
1559 void check_required(
void)
1569 opt = &
st->first_option;
1573 _(
"Required parameter <%s> not set:\n"
1582 void split_opts(
void)
1594 opt = &
st->first_option;
1606 for (len = 0, ptr2 = ptr1; *ptr2 !=
'\0' && *ptr2 !=
',';
1612 memcpy(opt->
answers[ans_num], ptr1, len);
1613 opt->
answers[ans_num][len] = 0;
1617 if (ans_num >= allocated) {
1639 void check_multiple_opts(
void)
1651 opt = &
st->first_option;
1657 for (ptr = opt->
key_desc; *ptr !=
'\0'; ptr++)
1666 _(
"Option <%s> must be provided in multiples of %d\n"
1667 "\tYou provided %d item(s): %s"),
1677 int check_overwrite(
void)
1680 char age[KEYLENGTH];
1682 char desc[KEYLENGTH];
1684 const char *overstr;
1687 st->module_info.overwrite = 0;
1695 over = atoi(overstr);
1699 if ((overstr = getenv(
"GRASS_OVERWRITE"))) {
1704 if (
st->overwrite || over) {
1705 st->module_info.overwrite = 1;
1707 putenv(
"GRASS_OVERWRITE=1");
1712 opt = &
st->first_option;
1717 if (strcmp(age,
"new") == 0) {
1721 for (i = 0; opt->
answers[i]; i++) {
1723 if (strcmp(
element,
"file") == 0) {
1724 if (access(opt->
answers[i], F_OK) == 0)
1727 else if (strcmp(
element,
"mapset") != 0) {
1736 if (!
st->overwrite && !over) {
1739 fprintf(stderr,
_(
"ERROR: "));
1741 _(
"option <%s>: <%s> exists. To "
1742 "overwrite, use the --overwrite "
1745 fprintf(stderr,
"\n");
1748 fprintf(stderr,
"GRASS_INFO_ERROR(%d,1): ",
1751 _(
"option <%s>: <%s> exists. To "
1752 "overwrite, use the --overwrite "
1755 fprintf(stderr,
"\n");
1756 fprintf(stderr,
"GRASS_INFO_END(%d,1)\n",
1778 for (ptr1 =
gisprompt, ptr2 = age; *ptr1 !=
'\0'; ptr1++, ptr2++) {
1785 for (ptr1++, ptr2 =
element; *ptr1 !=
'\0'; ptr1++, ptr2++) {
1792 for (ptr1++, ptr2 = desc; *ptr1 !=
'\0'; ptr1++, ptr2++) {
1800 void append_error(
const char *msg)
1802 st->error =
G_realloc(
st->error,
sizeof(
char *) * (
st->n_errors + 1));
1806 const char *get_renamed_option(
const char *
key)
1808 const char *pgm, *key_new;
1811 if (!
st->renamed_options) {
1826 pgm_key = (
char *)
G_malloc(strlen(pgm) + strlen(
key) + 2);
1862 strcmp(option->
gisprompt,
"old,separator,separator") != 0)
1868 if (strcmp(option->
answer,
"pipe") == 0)
1870 else if (strcmp(option->
answer,
"comma") == 0)
1872 else if (strcmp(option->
answer,
"space") == 0)
1874 else if (strcmp(option->
answer,
"tab") == 0 ||
1875 strcmp(option->
answer,
"\\t") == 0)
1877 else if (strcmp(option->
answer,
"newline") == 0 ||
1878 strcmp(option->
answer,
"\\n") == 0)
1883 G_debug(3,
"G_option_to_separator(): key = %s -> sep = '%s'", option->
key,
1925 strcmp(option->
answer,
"-") == 0;
1932 else if (strcmp(option->
gisprompt,
"old,file,file") == 0) {
1935 else if ((fp = fopen(option->
answer,
"r")) ==
NULL)
1937 option->
answer, strerror(errno));
1939 else if (strcmp(option->
gisprompt,
"new,file,file") == 0) {
1942 else if ((fp = fopen(option->
answer,
"w")) ==
NULL)
1944 option->
answer, strerror(errno));
1960 if (fp != stdin && fp != stdout && fp != stderr)
char * G_basename(char *, const char *)
Truncates filename to the base part (before the last '.') if it matches the extension,...
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int G_verbose_max(void)
Get max verbosity level.
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
const char * G_mapset(void)
Get current mapset name.
int G_verbose(void)
Get current verbosity level.
int G_verbose_min(void)
Get min verbosity level.
void G_free_tokens(char **)
Free memory allocated to tokens.
int G_asprintf(char **, const char *,...) __attribute__((format(printf
struct Key_Value * G_read_key_value_file(const char *)
Read key/values pairs from file.
int G_number_of_tokens(char **)
Return number of tokens.
int G_is_dirsep(char)
Checks if a specified character is a valid directory separator character on the host system.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
const char * G_original_program_name(void)
Return original path of the executed program.
void G_usage(void)
Command line help/usage message.
int G_info_format(void)
Get current message format.
const char * G_gisbase(void)
Get full path name of the top level module directory.
const char * G_program_name(void)
Return module name.
char * G_chop(char *)
Chop leading and trailing white spaces.
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_getenv_nofatal(const char *)
Get environment variable.
int G_verbose_std(void)
Get standard verbosity level.
char * G_store(const char *)
Copy string to allocated memory.
char ** G_tokenize(const char *, const char *)
Tokenize string.
void int G_suppress_warnings(int)
Suppress printing a warning message to stderr.
const char * G_find_file(const char *, char *, const char *)
Searches for a file from the mapset search list or in a specified mapset.
int G_spawn(const char *command,...)
Spawn new process based on command.
#define G_INFO_FORMAT_GUI
struct GModule * G_define_module(void)
Initializes a new module.
void G__print_keywords(FILE *fd, void(*format)(FILE *, const char *), int newline)
Print list of keywords (internal use only)
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 '-',...
char * G_recreate_command_original_path(void)
Creates command to run non-interactive.
int G_parser(int argc, char **argv)
Parse command line.
void G_set_keywords(const char *keywords)
Set keywords from the string.
int G__uses_new_gisprompt(void)
char * recreate_command(int original_path)
Creates command to run non-interactive.
struct Option * G_define_option(void)
Initializes an Option struct.
void G_add_keyword(const char *keyword)
Add keyword to the list.
int G_get_overwrite(void)
Get overwrite value.
void G_close_option_file(FILE *fp)
Close an input/output file returned by G_open_option_file(). If the file pointer is stdin,...
char * G_recreate_command(void)
Creates command to run non-interactive.
void G_disable_interactive(void)
Disables the ability of the parser to operate interactively.
struct Flag * G_define_flag(void)
Initializes a Flag struct.
char * G_option_to_separator(const struct Option *option)
Get separator string from the option.
void G__split_gisprompt(const char *gisprompt, char *age, char *element, char *desc)
void G__check_option_rules(void)
Check for option rules (internal use only)
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.
void G__usage_xml(void)
Print module usage description in XML format.
char * G__json(void)
This function generates actinia JSON process chain building blocks from the command line arguments th...
void G__usage_markdown(void)
Print module usage description in Markdown format.
void G__usage_rest(void)
Print module usage description in reStructuredText format.
void G__script(void)
Generate Python script-like output.
void G__wps_print_process_description(void)
Print the WPS 1.0.0 process description XML document to stdout.
Structure that stores flag info.
Structure that stores module info.
Structure that stores option information.
int(* checker)(const char *)
const char * descriptions
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)