88 #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 **,
char **);
116 static int check_int(
const char *,
const char **);
117 static int check_double(
const char *,
const char **);
118 static int check_string(
const char *,
const char **,
int *);
119 static void check_required(
void);
120 static void split_opts(
void);
121 static void check_multiple_opts(
void);
122 static int check_overwrite(
void);
123 static void define_keywords(
void);
124 static int module_gui_wx(
void);
125 static void append_error(
const char *);
126 static const char *get_renamed_option(
const char *);
141 st->no_interactive = 1;
165 st->current_flag->next_flag = flag;
168 flag = &
st->first_flag;
174 st->current_flag = flag;
178 item =
G_malloc(
sizeof(
struct Item));
179 st->current_item->next_item = item;
182 item = &
st->first_item;
184 G_zero(item,
sizeof(
struct Item));
189 st->current_item = item;
219 st->current_option->next_opt = opt;
222 opt = &
st->first_option;
230 st->current_option = opt;
234 item =
G_malloc(
sizeof(
struct Item));
235 st->current_item->next_item = item;
238 item = &
st->first_item;
240 G_zero(item,
sizeof(
struct Item));
244 st->current_item = item;
260 module = &
st->module_info;
324 const char *gui_envvar;
325 char *ptr, *tmp_name, *
err;
328 char force_gui =
FALSE;
334 st->pgm_path = tmp_name;
338 i = strlen(tmp_name);
346 st->pgm_name = tmp_name;
348 if (!
st->module_info.label && !
st->module_info.description)
349 G_warning(
_(
"Bug in UI description. Missing module description"));
353 opt = &
st->first_option;
354 while (
st->n_opts && opt) {
356 st->has_required = 1;
359 G_warning(
_(
"Bug in UI description. Missing option key"));
360 if (!valid_option_name(opt->
key))
361 G_warning(
_(
"Bug in UI description. Option key <%s> is not valid"), opt->
key);
363 G_warning(
_(
"Bug in UI description. Description for option <%s> missing"), opt->
key ? opt->
key :
"?");
368 char **tokens, delm[2];
407 while (opt->
opts[j]) {
408 if (strcmp(opt->
opts[j], tokens[i]) == 0) {
415 G_warning(
_(
"Bug in UI description. Option '%s' in <%s> does not exist"),
416 tokens[i], opt->
key);
432 for (i = 1; opt->
answers[i]; i++) {
447 && !
st->no_interactive && isatty(0) &&
448 (gui_envvar &&
G_strcasecmp(gui_envvar,
"text") != 0)) {
449 if (module_gui_wx() == 0)
453 if (argc < 2 && st->has_required && isatty(0)) {
457 else if (argc >= 2) {
460 if (strcmp(argv[1],
"help") == 0 ||
461 strcmp(argv[1],
"-help") == 0 || strcmp(argv[1],
"--help") == 0) {
468 if (strcmp(argv[1],
"--help-text") == 0) {
475 if (strcmp(argv[1],
"--interface-description") == 0) {
482 if (strcmp(argv[1],
"--html-description") == 0) {
489 if (strcmp(argv[1],
"--rst-description") == 0) {
496 if (strcmp(argv[1],
"--wps-process-description") == 0) {
503 if (strcmp(argv[1],
"--script") == 0) {
513 if (strcmp(ptr,
"help") == 0 || strcmp(ptr,
"--h") == 0 ||
514 strcmp(ptr,
"-help") == 0 || strcmp(ptr,
"--help") == 0) {
520 if (strcmp(ptr,
"--json") == 0) {
526 if (strcmp(ptr,
"--o") == 0 || strcmp(ptr,
"--overwrite") == 0) {
531 else if (strcmp(ptr,
"--v") == 0 || strcmp(ptr,
"--verbose") == 0) {
538 if (
st->quiet == 1) {
539 G_warning(
_(
"Use either --quiet or --verbose flag, not both. Assuming --verbose."));
545 else if (strcmp(ptr,
"--q") == 0 || strcmp(ptr,
"--quiet") == 0) {
552 if (
st->quiet == -1) {
553 G_warning(
_(
"Use either --quiet or --verbose flag, not both. Assuming --quiet."));
559 else if (strcmp(ptr,
"--qq") == 0 ) {
567 if (
st->quiet == -1) {
568 G_warning(
_(
"Use either --qq or --verbose flag, not both. Assuming --qq."));
574 else if (strcmp(ptr,
"--ui") == 0) {
579 else if (*ptr ==
'-') {
585 else if (is_option(ptr)) {
591 else if (need_first_opt &&
st->n_opts) {
593 st->first_option.count++;
599 G_asprintf(&err,
_(
"Sorry <%s> is not a valid option"), ptr);
611 if (module_gui_wx() != 0)
612 G_fatal_error(
_(
"Your installation doesn't include GUI, exiting."));
617 check_multiple_opts();
624 if (!
st->suppress_required)
629 if (
st->n_errors > 0) {
633 fprintf(stderr,
"\n");
634 for (i = 0; i <
st->n_errors; i++) {
635 fprintf(stderr,
"%s: %s\n",
_(
"ERROR"),
st->error[i]);
642 if(print_json == 1) {
647 if (!
st->suppress_overwrite) {
648 if (check_overwrite())
676 G_debug(3,
"G_recreate_command()");
680 buff =
G_calloc(1024,
sizeof(
char));
687 if (len >= nalloced) {
688 nalloced += (1024 > len) ? 1024 : len + 1;
696 slen = strlen(
" --overwrite");
697 if (len + slen >= nalloced) {
698 nalloced += (1024 > len) ? 1024 : len + 1;
701 strcpy(cur,
" --overwrite");
714 if (len + slen >= nalloced) {
715 nalloced += (1024 > len) ? 1024 : len + 1;
724 flag = &
st->first_flag;
732 if (len + slen >= nalloced) {
734 (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
746 opt = &
st->first_option;
747 while (
st->n_opts && opt) {
749 slen = strlen(opt->
key) + 4;
750 if (len + slen >= nalloced) {
751 nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
757 strcpy(cur, opt->
key);
758 cur = strchr(cur,
'\0');
767 slen = strlen(opt->
key) + strlen(opt->
answers[0]) + 4;
768 if (len + slen >= nalloced) {
769 nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
775 strcpy(cur, opt->
key);
776 cur = strchr(cur,
'\0');
784 cur = strchr(cur,
'\0');
786 for (n = 1; opt->
answers[n]; n++) {
789 slen = strlen(opt->
answers[n]) + 2;
790 if (len + slen >= nalloced) {
792 (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
799 cur = strchr(cur,
'\0');
852 if (
st->n_keys >=
st->n_keys_alloc) {
853 st->n_keys_alloc += 10;
854 st->module_info.keywords =
G_realloc(
st->module_info.keywords,
855 st->n_keys_alloc *
sizeof(
char *));
858 st->module_info.keywords[
st->n_keys++] =
G_store(keyword);
869 st->module_info.keywords = (
const char **)tokens;
879 char desc[KEYLENGTH];
881 if (
st->module_info.overwrite)
887 opt = &
st->first_option;
891 if (strcmp(age,
"new") == 0)
914 for(i = 0; i <
st->n_keys; i++) {
916 fprintf(fd,
"%s",
st->module_info.keywords[i]);
919 format(fd,
st->module_info.keywords[i]);
921 if (i < st->n_keys - 1)
936 return st->module_info.overwrite;
939 void define_keywords(
void)
942 st->n_keys_alloc = 0;
955 int module_gui_wx(
void)
965 sprintf(script,
"%s/gui/wxpython/gui_core/forms.py",
967 if (access(script, F_OK) != -1)
991 flag = &
st->first_flag;
993 if (flag->
key == f) {
996 st->suppress_required = 1;
998 st->suppress_overwrite = 1;
1011 int contains(
const char *s,
int c)
1021 int valid_option_name(
const char *
string)
1023 int m = strlen(
string);
1024 int n = strspn(
string,
"abcdefghijklmnopqrstuvwxyz0123456789_");
1032 if (
string[m-1] ==
'_')
1038 int is_option(
const char *
string)
1040 int n = strspn(
string,
"abcdefghijklmnopqrstuvwxyz0123456789_");
1042 return n > 0 &&
string[n] ==
'=' &&
string[0] !=
'_' &&
string[n-1] !=
'_';
1045 int match_option_1(
const char *
string,
const char *option)
1049 if (*
string ==
'\0')
1052 if (*option ==
'\0')
1055 if (*
string == *option && match_option_1(
string + 1, option + 1))
1058 if (*option ==
'_' && match_option_1(
string, option + 1))
1061 next = strchr(option,
'_');
1066 return match_option_1(
string + 1, next + 1);
1068 return match_option_1(
string, next + 1);
1071 int match_option(
const char *
string,
const char *option)
1073 return (*
string == *option)
1074 && match_option_1(
string + 1, option + 1);
1077 void set_option(
const char *
string)
1082 char the_key[KEYLENGTH];
1089 for (ptr = the_key; *
string !=
'='; ptr++,
string++)
1099 key_len = strlen(the_key);
1100 for (at_opt = &
st->first_option; at_opt; at_opt = at_opt->
next_opt) {
1104 if (strcmp(the_key, at_opt->
key) == 0) {
1105 matches[0] = at_opt;
1110 if (strncmp(the_key, at_opt->
key, key_len) == 0 ||
1111 match_option(the_key, at_opt->
key)) {
1114 matches[found++] = at_opt;
1120 int length = strlen(matches[0]->
key);
1123 for (i = 1; i < found; i++) {
1124 int len = strlen(matches[i]->
key);
1130 for (i = 0; prefix && i < found; i++)
1131 if (strncmp(matches[i]->
key, matches[shortest]->
key, length) != 0)
1134 matches[0] = matches[shortest];
1140 for (i = 0; i < found; i++) {
1153 const char *renamed_key =
NULL;
1155 renamed_key = get_renamed_option(the_key);
1157 for (at_opt = &
st->first_option; at_opt; at_opt = at_opt->
next_opt) {
1158 if (strcmp(renamed_key, at_opt->
key) == 0) {
1159 G_warning(
_(
"Please update the usage of <%s>: " 1160 "option <%s> has been renamed to <%s>"),
1177 if (
getenv(
"GRASS_FULL_OPTION_NAMES") && strcmp(the_key, opt->
key) != 0)
1178 G_warning(
_(
"<%s> is an abbreviation for <%s>"), the_key, opt->
key);
1183 G_asprintf(&err,
_(
"Option <%s> does not accept multiple answers"), opt->
key);
1187 strlen(opt->
answer) + strlen(
string) + 2);
1188 strcat(opt->
answer,
",");
1189 strcat(opt->
answer,
string);
1195 void check_opts(
void)
1203 opt = &
st->first_option;
1209 check_an_opt(opt->
key, opt->
type,
1213 check_an_opt(opt->
key, opt->
type,
1227 void check_an_opt(
const char *
key,
int type,
const char *
options,
1228 const char **
opts,
char **answerp)
1230 const char *
answer = *answerp;
1241 error = check_int(answer, opts);
1244 error = check_double(answer, opts);
1247 error = check_string(answer, opts, &found);
1255 _(
"Illegal range syntax for parameter <%s>\n" 1256 "\tPresented as: %s"), key, options);
1261 _(
"Value <%s> out of range for parameter <%s>\n" 1262 "\tLegal range: %s"), answer, key, options);
1267 _(
"Missing value for parameter <%s>"),
1273 _(
"Invalid value <%s> for parameter <%s>"),
1279 _(
"Value <%s> ambiguous for parameter <%s>\n" 1280 "\tValid options: %s"), answer, key, options);
1284 *answerp =
G_store(opts[found]);
1290 int check_int(
const char *ans,
const char **opts)
1295 if (strcmp(ans,
"-") == 0)
1301 if (sscanf(ans,
"%d", &d) != 1)
1307 for (i = 0; opts[i]; i++) {
1308 const char *opt = opts[i];
1311 if (contains(opt,
'-')) {
1312 if (sscanf(opt,
"%d-%d", &lo, &hi) == 2) {
1313 if (d >= lo && d <= hi)
1316 else if (sscanf(opt,
"-%d", &hi) == 1) {
1320 else if (sscanf(opt,
"%d-", &lo) == 1) {
1328 if (sscanf(opt,
"%d", &lo) == 1) {
1340 int check_double(
const char *ans,
const char **opts)
1346 if (strcmp(ans,
"-") == 0)
1352 if (sscanf(ans,
"%lf", &d) != 1)
1358 for (i = 0; opts[i]; i++) {
1359 const char *opt = opts[i];
1362 if (contains(opt,
'-')) {
1363 if (sscanf(opt,
"%lf-%lf", &lo, &hi) == 2) {
1364 if (d >= lo && d <= hi)
1367 else if (sscanf(opt,
"-%lf", &hi) == 1) {
1371 else if (sscanf(opt,
"%lf-", &lo) == 1) {
1379 if (sscanf(opt,
"%lf", &lo) == 1) {
1391 int check_string(
const char *ans,
const char **opts,
int *result)
1393 int len = strlen(ans);
1401 for (i = 0; opts[i]; i++) {
1402 if (strcmp(ans, opts[i]) == 0)
1404 if (strncmp(ans, opts[i], len) == 0 || match_option(ans, opts[i])) {
1407 matches[found++] = i;
1413 int length = strlen(opts[matches[0]]);
1416 for (i = 1; i < found; i++) {
1417 int lengthi = strlen(opts[matches[i]]);
1419 if (lengthi < length) {
1424 for (i = 0; prefix && i < found; i++)
1425 if (strncmp(opts[matches[i]], opts[matches[shortest]], length) != 0)
1428 matches[0] = matches[shortest];
1434 *result = matches[0];
1436 if (found > 0 &&
getenv(
"GRASS_FULL_OPTION_NAMES") && strcmp(ans, opts[matches[0]]) != 0)
1437 G_warning(
_(
"<%s> is an abbreviation for <%s>"), ans, opts[matches[0]]);
1446 void check_required(
void)
1456 opt = &
st->first_option;
1459 G_asprintf(&err,
_(
"Required parameter <%s> not set:\n" 1468 void split_opts(
void)
1481 opt = &
st->first_option;
1493 for (len = 0, ptr2 = ptr1; *ptr2 !=
'\0' && *ptr2 !=
',';
1498 memcpy(opt->
answers[ans_num], ptr1, len);
1499 opt->
answers[ans_num][len] = 0;
1503 if (ans_num >= allocated) {
1506 allocated *
sizeof(
char *));
1525 void check_multiple_opts(
void)
1537 opt = &
st->first_option;
1543 for (ptr = opt->
key_desc; *ptr !=
'\0'; ptr++)
1551 _(
"Option <%s> must be provided in multiples of %d\n" 1552 "\tYou provided %d item(s): %s"),
1563 int check_overwrite(
void)
1566 char age[KEYLENGTH];
1568 char desc[KEYLENGTH];
1570 const char *overstr;
1573 st->module_info.overwrite = 0;
1581 over = atoi(overstr);
1585 if ((overstr =
getenv(
"GRASS_OVERWRITE"))) {
1590 if (
st->overwrite || over) {
1591 st->module_info.overwrite = 1;
1593 putenv(
"GRASS_OVERWRITE=1");
1598 opt = &
st->first_option;
1603 if (strcmp(age,
"new") == 0) {
1607 for (i = 0; opt->
answers[i]; i++) {
1609 if (strcmp(element,
"file") == 0) {
1610 if (access(opt->
answers[i], F_OK) == 0)
1613 else if (strcmp(element,
"mapset") != 0) {
1622 if (!
st->overwrite && !over) {
1625 fprintf(stderr,
_(
"ERROR: "));
1627 _(
"option <%s>: <%s> exists. To overwrite, use the --overwrite flag"),
1629 fprintf(stderr,
"\n");
1632 fprintf(stderr,
"GRASS_INFO_ERROR(%d,1): ", getpid());
1634 _(
"option <%s>: <%s> exists. To overwrite, use the --overwrite flag"),
1636 fprintf(stderr,
"\n");
1637 fprintf(stderr,
"GRASS_INFO_END(%d,1)\n",
1659 for (ptr1 = gisprompt, ptr2 = age; *ptr1 !=
'\0'; ptr1++, ptr2++) {
1666 for (ptr1++, ptr2 = element; *ptr1 !=
'\0'; ptr1++, ptr2++) {
1673 for (ptr1++, ptr2 = desc; *ptr1 !=
'\0'; ptr1++, ptr2++) {
1681 void append_error(
const char *msg)
1683 st->error =
G_realloc(
st->error,
sizeof(
char *) * (
st->n_errors + 1));
1687 const char *get_renamed_option(
const char *key)
1689 const char *pgm, *key_new;
1692 if (!
st->renamed_options) {
1707 pgm_key = (
char *)
G_malloc (strlen(pgm) + strlen(key) + 2);
1743 strcmp(option->
gisprompt,
"old,separator,separator") != 0)
1749 if (strcmp(option->
answer,
"pipe") == 0)
1751 else if (strcmp(option->
answer,
"comma") == 0)
1753 else if (strcmp(option->
answer,
"space") == 0)
1755 else if (strcmp(option->
answer,
"tab") == 0 ||
1756 strcmp(option->
answer,
"\\t") == 0)
1758 else if (strcmp(option->
answer,
"newline") == 0 ||
1759 strcmp(option->
answer,
"\\n") == 0)
1764 G_debug(3,
"G_option_to_separator(): key = %s -> sep = '%s'",
1806 strcmp(option->
answer,
"-") == 0;
1813 else if (strcmp(option->
gisprompt,
"old,file,file") == 0) {
1816 else if ((fp = fopen(option->
answer,
"r")) ==
NULL)
1818 option->
key, option->
answer, strerror(errno));
1819 }
else if (strcmp(option->
gisprompt,
"new,file,file") == 0) {
1822 else if ((fp = fopen(option->
answer,
"w")) ==
NULL)
1824 option->
key, option->
answer, strerror(errno));
1839 if (fp != stdin && fp != stdout && fp != stderr)
int G__uses_new_gisprompt(void)
int G__has_required_rule(void)
Checks if there is any rule RULE_REQUIRED (internal use only).
const char * G_program_name(void)
Return module name.
void G__usage_html(void)
Print module usage description in HTML format.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int G_verbose(void)
Get current verbosity level.
char * G__json(void)
This function generates actinia JSON process chain building blocks from the command line arguments th...
struct Key_Value * G_read_key_value_file(const char *)
Read key/values pairs from file.
int G_verbose_min(void)
Get min verbosity level.
void G__usage_xml(void)
Print module usage description in XML format.
struct GModule * G_define_module(void)
Initializes a new module.
char * G_basename(char *, const char *)
Truncates filename to the base part (before the last '.') if it matches the extension, otherwise leaves it unchanged.
char * recreate_command(int original_path)
Creates command to run non-interactive.
const char * descriptions
void G__usage_rest(void)
Print module usage description in reStructuredText format.
void G_free(void *)
Free allocated memory.
void G__check_option_rules(void)
Check for option rules (internal use only)
int G_get_overwrite()
Get overwrite value.
Structure that stores module info.
char ** G_tokenize(const char *, const char *)
Tokenize string.
const char * G_original_program_name(void)
Return original path of the executed program.
char * G_option_to_separator(const struct Option *option)
Get separator string from the option.
char * G_recreate_command(void)
Creates command to run non-interactive.
char * G_recreate_command_original_path(void)
Creates command to run non-interactive.
struct Flag * G_define_flag(void)
Initializes a Flag struct.
#define G_INFO_FORMAT_GUI
void G_close_option_file(FILE *fp)
Close an input/output file returned by G_open_option_file(). If the file pointer is stdin...
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
int G_spawn(const char *command,...)
Spawn new process based on command.
int G_parser(int argc, char **argv)
Parse command line.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
struct Option * G_define_option(void)
Initializes an Option struct.
void G_set_keywords(const char *keywords)
Set keywords from the string.
int G_number_of_tokens(char **)
Return number of tokens.
void int G_suppress_warnings(int)
Suppress printing a warning message to stderr.
Structure that stores flag info.
int G_is_dirsep(char)
Checks if a specified character is a valid directory separator character on the host system...
void G__script(void)
Generate Python script-like output.
char * G_chop(char *)
Chop leading and trailing white spaces.
void G__split_gisprompt(const char *gisprompt, char *age, char *element, char *desc)
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 '-', it returns either stdin or stdout based on the gisprompt.
const char * G_find_file(const char *, char *, const char *)
Searches for a file from the mapset search list or in a specified mapset.
const char * G_mapset(void)
Get current mapset name.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
int G_snprintf(char *, size_t, const char *,...) __attribute__((format(printf
const char * G_gisbase(void)
Get full path name of the top level module directory.
void G_disable_interactive(void)
Disables the ability of the parser to operate interactively.
void G_free_tokens(char **)
Free memory allocated to tokens.
void G_warning(const char *,...) __attribute__((format(printf
Structure that stores option information.
int G_info_format(void)
Get current message format.
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.
int G_verbose_max(void)
Get max verbosity level.
int(* checker)(const char *)
int G_asprintf(char **, const char *,...) __attribute__((format(printf
void G__print_keywords(FILE *fd, void(*format)(FILE *, const char *))
Print list of keywords (internal use only)
void G_add_keyword(const char *keyword)
Add keyword to the list.
void G_usage(void)
Command line help/usage message.
void G__wps_print_process_description(void)
Print the WPS 1.0.0 process description XML document to stdout.
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)