83 #include <grass/gis.h> 84 #include <grass/spawn.h> 85 #include <grass/glocale.h> 87 #include "parser_local_proto.h" 98 #define MAX_MATCHES 50 105 static void set_flag(
int);
106 static int contains(
const char *,
int);
107 static int valid_option_name(
const char *);
108 static int is_option(
const char *);
109 static int match_option_1(
const char *,
const char *);
110 static int match_option(
const char *,
const char *);
111 static void set_option(
const char *);
112 static void check_opts(
void);
113 static void check_an_opt(
const char *,
int,
const char *,
const char **,
char **);
114 static int check_int(
const char *,
const char **);
115 static int check_double(
const char *,
const char **);
116 static int check_string(
const char *,
const char **,
int *);
117 static void check_required(
void);
118 static void split_opts(
void);
119 static void check_multiple_opts(
void);
120 static int check_overwrite(
void);
121 static void define_keywords(
void);
122 static int module_gui_wx(
void);
123 static void append_error(
const char *);
124 static const char *get_renamed_option(
const char *);
139 st->no_interactive = 1;
162 flag = G_malloc(
sizeof(
struct Flag));
163 st->current_flag->next_flag = flag;
166 flag = &
st->first_flag;
172 st->current_flag = flag;
176 item = G_malloc(
sizeof(
struct Item));
177 st->current_item->next_item = item;
180 item = &
st->first_item;
182 G_zero(item,
sizeof(
struct Item));
187 st->current_item = item;
216 opt = G_malloc(
sizeof(
struct Option));
217 st->current_option->next_opt = opt;
220 opt = &
st->first_option;
228 st->current_option = opt;
232 item = G_malloc(
sizeof(
struct Item));
233 st->current_item->next_item = item;
236 item = &
st->first_item;
238 G_zero(item,
sizeof(
struct Item));
242 st->current_item = item;
258 module = &
st->module_info;
322 const char *gui_envvar;
323 char *ptr, *tmp_name, *
err;
326 char force_gui =
FALSE;
332 st->pgm_path = tmp_name;
336 i = strlen(tmp_name);
344 st->pgm_name = tmp_name;
346 if (!
st->module_info.label && !
st->module_info.description)
347 G_warning(
_(
"Bug in UI description. Missing module description"));
351 opt = &
st->first_option;
352 while (
st->n_opts && opt) {
354 st->has_required = 1;
357 G_warning(
_(
"Bug in UI description. Missing option key"));
358 if (!valid_option_name(opt->
key))
359 G_warning(
_(
"Bug in UI description. Option key <%s> is not valid"), opt->
key);
361 G_warning(
_(
"Bug in UI description. Description for option <%s> missing"), opt->
key ? opt->
key :
"?");
366 char **tokens, delm[2];
379 opt->
opts = G_calloc(cnt + 1,
sizeof(
const char *));
391 opt->
descs = G_calloc(cnt + 1,
sizeof(
const char *));
405 while (opt->
opts[j]) {
406 if (strcmp(opt->
opts[j], tokens[i]) == 0) {
413 G_warning(
_(
"Bug in UI description. Option '%s' in <%s> does not exist"),
414 tokens[i], opt->
key);
430 for (i = 1; opt->
answers[i]; i++) {
445 && !
st->no_interactive && isatty(0) &&
446 (gui_envvar &&
G_strcasecmp(gui_envvar,
"text") != 0)) {
447 if (module_gui_wx() == 0)
451 if (argc < 2 && st->has_required && isatty(0)) {
455 else if (argc >= 2) {
458 if (strcmp(argv[1],
"help") == 0 ||
459 strcmp(argv[1],
"-help") == 0 || strcmp(argv[1],
"--help") == 0) {
466 if (strcmp(argv[1],
"--help-text") == 0) {
473 if (strcmp(argv[1],
"--interface-description") == 0) {
480 if (strcmp(argv[1],
"--html-description") == 0) {
487 if (strcmp(argv[1],
"--rst-description") == 0) {
494 if (strcmp(argv[1],
"--wps-process-description") == 0) {
501 if (strcmp(argv[1],
"--script") == 0) {
511 if (strcmp(ptr,
"help") == 0 || strcmp(ptr,
"--h") == 0 ||
512 strcmp(ptr,
"-help") == 0 || strcmp(ptr,
"--help") == 0) {
518 if (strcmp(ptr,
"--json") == 0) {
524 if (strcmp(ptr,
"--o") == 0 || strcmp(ptr,
"--overwrite") == 0) {
529 else if (strcmp(ptr,
"--v") == 0 || strcmp(ptr,
"--verbose") == 0) {
536 if (
st->quiet == 1) {
537 G_warning(
_(
"Use either --quiet or --verbose flag, not both. Assuming --verbose."));
543 else if (strcmp(ptr,
"--q") == 0 || strcmp(ptr,
"--quiet") == 0) {
550 if (
st->quiet == -1) {
551 G_warning(
_(
"Use either --quiet or --verbose flag, not both. Assuming --quiet."));
557 else if (strcmp(ptr,
"--qq") == 0 ) {
565 if (
st->quiet == -1) {
566 G_warning(
_(
"Use either --qq or --verbose flag, not both. Assuming --qq."));
572 else if (strcmp(ptr,
"--ui") == 0) {
577 else if (*ptr ==
'-') {
583 else if (is_option(ptr)) {
589 else if (need_first_opt &&
st->n_opts) {
591 st->first_option.count++;
597 G_asprintf(&err,
_(
"Sorry <%s> is not a valid option"), ptr);
609 if (module_gui_wx() != 0)
610 G_fatal_error(
_(
"Your installation doesn't include GUI, exiting."));
615 check_multiple_opts();
622 if (!
st->suppress_required)
627 if (
st->n_errors > 0) {
631 fprintf(stderr,
"\n");
632 for (i = 0; i <
st->n_errors; i++) {
633 fprintf(stderr,
"%s: %s\n",
_(
"ERROR"),
st->error[i]);
640 if(print_json == 1) {
645 if (!
st->suppress_overwrite) {
646 if (check_overwrite())
674 G_debug(3,
"G_recreate_command()");
678 buff = G_calloc(1024,
sizeof(
char));
685 if (len >= nalloced) {
686 nalloced += (1024 > len) ? 1024 : len + 1;
687 buff = G_realloc(buff, nalloced);
694 slen = strlen(
" --overwrite");
695 if (len + slen >= nalloced) {
696 nalloced += (1024 > len) ? 1024 : len + 1;
697 buff = G_realloc(buff, nalloced);
699 strcpy(cur,
" --overwrite");
712 if (len + slen >= nalloced) {
713 nalloced += (1024 > len) ? 1024 : len + 1;
714 buff = G_realloc(buff, nalloced);
722 flag = &
st->first_flag;
730 if (len + slen >= nalloced) {
732 (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
733 buff = G_realloc(buff, nalloced);
744 opt = &
st->first_option;
745 while (
st->n_opts && opt) {
747 slen = strlen(opt->
key) + 4;
748 if (len + slen >= nalloced) {
749 nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
750 buff = G_realloc(buff, nalloced);
755 strcpy(cur, opt->
key);
756 cur = strchr(cur,
'\0');
765 slen = strlen(opt->
key) + strlen(opt->
answers[0]) + 4;
766 if (len + slen >= nalloced) {
767 nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
768 buff = G_realloc(buff, nalloced);
773 strcpy(cur, opt->
key);
774 cur = strchr(cur,
'\0');
782 cur = strchr(cur,
'\0');
784 for (n = 1; opt->
answers[n]; n++) {
787 slen = strlen(opt->
answers[n]) + 2;
788 if (len + slen >= nalloced) {
790 (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
791 buff = G_realloc(buff, nalloced);
797 cur = strchr(cur,
'\0');
850 if (
st->n_keys >=
st->n_keys_alloc) {
851 st->n_keys_alloc += 10;
852 st->module_info.keywords = G_realloc(
st->module_info.keywords,
853 st->n_keys_alloc *
sizeof(
char *));
856 st->module_info.keywords[
st->n_keys++] =
G_store(keyword);
867 st->module_info.keywords = (
const char **)tokens;
877 char desc[KEYLENGTH];
879 if (
st->module_info.overwrite)
885 opt = &
st->first_option;
889 if (strcmp(age,
"new") == 0)
912 for(i = 0; i <
st->n_keys; i++) {
914 fprintf(fd,
"%s",
st->module_info.keywords[i]);
917 format(fd,
st->module_info.keywords[i]);
919 if (i < st->n_keys - 1)
934 return st->module_info.overwrite;
937 void define_keywords(
void)
940 st->n_keys_alloc = 0;
953 int module_gui_wx(
void)
963 sprintf(script,
"%s/gui/wxpython/gui_core/forms.py",
965 if (access(script, F_OK) != -1)
989 flag = &
st->first_flag;
991 if (flag->
key == f) {
994 st->suppress_required = 1;
996 st->suppress_overwrite = 1;
1009 int contains(
const char *s,
int c)
1019 int valid_option_name(
const char *
string)
1021 int m = strlen(
string);
1022 int n = strspn(
string,
"abcdefghijklmnopqrstuvwxyz0123456789_");
1030 if (
string[m-1] ==
'_')
1036 int is_option(
const char *
string)
1038 int n = strspn(
string,
"abcdefghijklmnopqrstuvwxyz0123456789_");
1040 return n > 0 &&
string[n] ==
'=' &&
string[0] !=
'_' &&
string[n-1] !=
'_';
1043 int match_option_1(
const char *
string,
const char *option)
1047 if (*
string ==
'\0')
1050 if (*option ==
'\0')
1053 if (*
string == *option && match_option_1(
string + 1, option + 1))
1056 if (*option ==
'_' && match_option_1(
string, option + 1))
1059 next = strchr(option,
'_');
1064 return match_option_1(
string + 1, next + 1);
1066 return match_option_1(
string, next + 1);
1069 int match_option(
const char *
string,
const char *option)
1071 return (*
string == *option)
1072 && match_option_1(
string + 1, option + 1);
1075 void set_option(
const char *
string)
1080 char the_key[KEYLENGTH];
1087 for (ptr = the_key; *
string !=
'='; ptr++,
string++)
1097 key_len = strlen(the_key);
1098 for (at_opt = &
st->first_option; at_opt; at_opt = at_opt->
next_opt) {
1102 if (strcmp(the_key, at_opt->
key) == 0) {
1103 matches[0] = at_opt;
1108 if (strncmp(the_key, at_opt->
key, key_len) == 0 ||
1109 match_option(the_key, at_opt->
key)) {
1112 matches[found++] = at_opt;
1118 int length = strlen(matches[0]->
key);
1121 for (i = 1; i < found; i++) {
1122 int len = strlen(matches[i]->
key);
1128 for (i = 0; prefix && i < found; i++)
1129 if (strncmp(matches[i]->
key, matches[shortest]->
key, length) != 0)
1132 matches[0] = matches[shortest];
1138 for (i = 0; i < found; i++) {
1151 const char *renamed_key =
NULL;
1153 renamed_key = get_renamed_option(the_key);
1155 for (at_opt = &
st->first_option; at_opt; at_opt = at_opt->
next_opt) {
1156 if (strcmp(renamed_key, at_opt->
key) == 0) {
1157 G_warning(
_(
"Please update the usage of <%s>: " 1158 "option <%s> has been renamed to <%s>"),
1175 if (
getenv(
"GRASS_FULL_OPTION_NAMES") && strcmp(the_key, opt->
key) != 0)
1176 G_warning(
_(
"<%s> is an abbreviation for <%s>"), the_key, opt->
key);
1181 G_asprintf(&err,
_(
"Option <%s> does not accept multiple answers"), opt->
key);
1185 strlen(opt->
answer) + strlen(
string) + 2);
1186 strcat(opt->
answer,
",");
1187 strcat(opt->
answer,
string);
1193 void check_opts(
void)
1201 opt = &
st->first_option;
1207 check_an_opt(opt->
key, opt->
type,
1211 check_an_opt(opt->
key, opt->
type,
1225 void check_an_opt(
const char *
key,
int type,
const char *
options,
1226 const char **
opts,
char **answerp)
1228 const char *
answer = *answerp;
1239 error = check_int(answer, opts);
1242 error = check_double(answer, opts);
1245 error = check_string(answer, opts, &found);
1253 _(
"Illegal range syntax for parameter <%s>\n" 1254 "\tPresented as: %s"), key, options);
1259 _(
"Value <%s> out of range for parameter <%s>\n" 1260 "\tLegal range: %s"), answer, key, options);
1265 _(
"Missing value for parameter <%s>"),
1271 _(
"Value <%s> ambiguous for parameter <%s>\n" 1272 "\tValid options: %s"), answer, key, options);
1276 *answerp =
G_store(opts[found]);
1282 int check_int(
const char *ans,
const char **opts)
1287 if (strcmp(ans,
"-") == 0)
1290 if (sscanf(ans,
"%d", &d) != 1)
1296 for (i = 0; opts[i]; i++) {
1297 const char *opt = opts[i];
1300 if (contains(opt,
'-')) {
1301 if (sscanf(opt,
"%d-%d", &lo, &hi) == 2) {
1302 if (d >= lo && d <= hi)
1305 else if (sscanf(opt,
"-%d", &hi) == 1) {
1309 else if (sscanf(opt,
"%d-", &lo) == 1) {
1317 if (sscanf(opt,
"%d", &lo) == 1) {
1329 int check_double(
const char *ans,
const char **opts)
1335 if (strcmp(ans,
"-") == 0)
1338 if (sscanf(ans,
"%lf", &d) != 1)
1344 for (i = 0; opts[i]; i++) {
1345 const char *opt = opts[i];
1348 if (contains(opt,
'-')) {
1349 if (sscanf(opt,
"%lf-%lf", &lo, &hi) == 2) {
1350 if (d >= lo && d <= hi)
1353 else if (sscanf(opt,
"-%lf", &hi) == 1) {
1357 else if (sscanf(opt,
"%lf-", &lo) == 1) {
1365 if (sscanf(opt,
"%lf", &lo) == 1) {
1377 int check_string(
const char *ans,
const char **opts,
int *result)
1379 int len = strlen(ans);
1387 for (i = 0; opts[i]; i++) {
1388 if (strcmp(ans, opts[i]) == 0)
1390 if (strncmp(ans, opts[i], len) == 0 || match_option(ans, opts[i])) {
1393 matches[found++] = i;
1399 int length = strlen(opts[matches[0]]);
1402 for (i = 1; i < found; i++) {
1403 int len = strlen(opts[matches[i]]);
1409 for (i = 0; prefix && i < found; i++)
1410 if (strncmp(opts[matches[i]], opts[matches[shortest]], length) != 0)
1413 matches[0] = matches[shortest];
1419 *result = matches[0];
1421 if (found > 0 &&
getenv(
"GRASS_FULL_OPTION_NAMES") && strcmp(ans, opts[matches[0]]) != 0)
1422 G_warning(
_(
"<%s> is an abbreviation for <%s>"), ans, opts[matches[0]]);
1431 void check_required(
void)
1441 opt = &
st->first_option;
1444 G_asprintf(&err,
_(
"Required parameter <%s> not set:\n" 1453 void split_opts(
void)
1466 opt = &
st->first_option;
1471 opt->
answers = G_malloc(allocated *
sizeof(
char *));
1478 for (len = 0, ptr2 = ptr1; *ptr2 !=
'\0' && *ptr2 !=
',';
1482 opt->
answers[ans_num] = G_malloc(len + 1);
1483 memcpy(opt->
answers[ans_num], ptr1, len);
1484 opt->
answers[ans_num][len] = 0;
1488 if (ans_num >= allocated) {
1491 allocated *
sizeof(
char *));
1510 void check_multiple_opts(
void)
1522 opt = &
st->first_option;
1528 for (ptr = opt->
key_desc; *ptr !=
'\0'; ptr++)
1536 _(
"Option <%s> must be provided in multiples of %d\n" 1537 "\tYou provided %d item(s): %s"),
1548 int check_overwrite(
void)
1551 char age[KEYLENGTH];
1553 char desc[KEYLENGTH];
1555 const char *overstr;
1558 st->module_info.overwrite = 0;
1566 over = atoi(overstr);
1570 if ((overstr =
getenv(
"GRASS_OVERWRITE"))) {
1575 if (
st->overwrite || over) {
1576 st->module_info.overwrite = 1;
1578 putenv(
"GRASS_OVERWRITE=1");
1583 opt = &
st->first_option;
1588 if (strcmp(age,
"new") == 0) {
1592 for (i = 0; opt->
answers[i]; i++) {
1594 if (strcmp(element,
"file") == 0) {
1595 if (access(opt->
answers[i], F_OK) == 0)
1598 else if (strcmp(element,
"mapset") != 0) {
1607 if (!
st->overwrite && !over) {
1610 fprintf(stderr,
_(
"ERROR: "));
1612 _(
"option <%s>: <%s> exists. To overwrite, use the --overwrite flag"),
1614 fprintf(stderr,
"\n");
1617 fprintf(stderr,
"GRASS_INFO_ERROR(%d,1): ", getpid());
1619 _(
"option <%s>: <%s> exists. To overwrite, use the --overwrite flag"),
1621 fprintf(stderr,
"\n");
1622 fprintf(stderr,
"GRASS_INFO_END(%d,1)\n",
1644 for (ptr1 = gisprompt, ptr2 = age; *ptr1 !=
'\0'; ptr1++, ptr2++) {
1651 for (ptr1++, ptr2 = element; *ptr1 !=
'\0'; ptr1++, ptr2++) {
1658 for (ptr1++, ptr2 = desc; *ptr1 !=
'\0'; ptr1++, ptr2++) {
1666 void append_error(
const char *msg)
1668 st->error = G_realloc(
st->error,
sizeof(
char *) * (
st->n_errors + 1));
1672 const char *get_renamed_option(
const char *key)
1674 const char *pgm, *key_new;
1677 if (!
st->renamed_options) {
1692 pgm_key = (
char *) G_malloc (strlen(pgm) + strlen(key) + 2);
1728 strcmp(option->
gisprompt,
"old,separator,separator") != 0)
1734 if (strcmp(option->
answer,
"pipe") == 0)
1736 else if (strcmp(option->
answer,
"comma") == 0)
1738 else if (strcmp(option->
answer,
"space") == 0)
1740 else if (strcmp(option->
answer,
"tab") == 0 ||
1741 strcmp(option->
answer,
"\\t") == 0)
1743 else if (strcmp(option->
answer,
"newline") == 0 ||
1744 strcmp(option->
answer,
"\\n") == 0)
1749 G_debug(3,
"G_option_to_separator(): key = %s -> sep = '%s'",
1791 strcmp(option->
answer,
"-") == 0;
1798 else if (strcmp(option->
gisprompt,
"old,file,file") == 0) {
1801 else if ((fp = fopen(option->
answer,
"r")) ==
NULL)
1804 }
else if (strcmp(option->
gisprompt,
"new,file,file") == 0) {
1807 else if ((fp = fopen(option->
answer,
"w")) ==
NULL)
1824 if (fp != stdin && fp != stdout && fp != stderr)
int G__uses_new_gisprompt(void)
int G_info_format(void)
Get current message format.
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
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.
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
void G_free(void *buf)
Free allocated memory.
const char * G_original_program_name(void)
Return original path of the executed program.
char * G__json(void)
This function generates actinia JSON process chain building blocks from the command line arguments th...
void G__usage_xml(void)
Print module usage description in XML format.
struct GModule * G_define_module(void)
Initializes a new module.
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.
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.
char ** G_tokenize(const char *buf, const char *delim)
Tokenize string.
char * G_store(const char *s)
Copy string to allocated memory.
void G__check_option_rules(void)
Check for option rules (internal use only)
int G_asprintf(char **out, const char *fmt,...)
int G_get_overwrite()
Get overwrite value.
Structure that stores module info.
char * G_chop(char *line)
Chop leading and trailing white spaces.
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_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G_snprintf(char *str, size_t size, const char *fmt,...)
snprintf() clone.
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_suppress_warnings(int flag)
Suppress printing a warning message to stderr.
struct Key_Value * G_read_key_value_file(const char *file)
Read key/values pairs from file.
int G_number_of_tokens(char **tokens)
Return number of tokens.
int G_parser(int argc, char **argv)
Parse command line.
struct Option * G_define_option(void)
Initializes an Option struct.
int G_verbose_max(void)
Get max verbosity level.
int G_debug(int level, const char *msg,...)
Print debugging message.
const char * G_getenv_nofatal(const char *name)
Get environment variable.
void G_set_keywords(const char *keywords)
Set keywords from the string.
Structure that stores flag info.
void G__script(void)
Generate Python script-like output.
char * G_basename(char *filename, const char *desired_ext)
Truncates filename to the base part (before the last '.') if it matches the extension, otherwise leaves it unchanged.
const char * G_program_name(void)
Return module name.
int G_verbose_std(void)
Get standard verbosity level.
int G_verbose(void)
Get current verbosity level.
void G_usage(void)
Command line help/usage message.
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.
void G_disable_interactive(void)
Disables the ability of the parser to operate interactively.
void G_free_tokens(char **tokens)
Free memory allocated to tokens.
Structure that stores option information.
int G_verbose_min(void)
Get min verbosity level.
int(* checker)(const char *)
const char * G_mapset(void)
Get current mapset name.
void G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
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.
int G_is_dirsep(char c)
Checks if a specified character is a valid directory separator character on the host system...
void G__wps_print_process_description(void)
Print the WPS 1.0.0 process description XML document to stdout.
const char * G_gisbase(void)
Get full path name of the top level module directory.
void G_warning(const char *msg,...)
Print a warning message to stderr.
int G_spawn(const char *command,...)
Spawn new process based on command.