87#include "parser_local_proto.h"
105static void set_flag(
int);
106static int contains(
const char *,
int);
107static int valid_option_name(
const char *);
108static int is_option(
const char *);
109static int match_option_1(
const char *,
const char *);
110static int match_option(
const char *,
const char *);
111static void set_option(
const char *);
112static void check_opts(
void);
113static void check_an_opt(
const char *,
int,
const char *,
const char **,
115static int check_int(
const char *,
const char **);
116static int check_double(
const char *,
const char **);
117static int check_string(
const char *,
const char **,
int *);
118static void check_required(
void);
119static void split_opts(
void);
120static void check_multiple_opts(
void);
121static int check_overwrite(
void);
122static void define_keywords(
void);
123static int module_gui_wx(
void);
124static void append_error(
const char *);
125static const char *get_renamed_option(
const char *);
139 st->no_interactive = 1;
163 st->current_flag->next_flag =
flag;
177 st->current_item->next_item = item;
180 item = &
st->first_item;
187 st->current_item = item;
217 st->current_option->next_opt =
opt;
220 opt = &
st->first_option;
228 st->current_option =
opt;
233 st->current_item->next_item = item;
236 item = &
st->first_item;
242 st->current_item = item;
258 module = &st->module_info;
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 (
opt->key && !valid_option_name(
opt->key))
359 G_warning(
_(
"Bug in UI description. Option key <%s> is not valid"),
361 if (!
opt->label && !
opt->description)
363 _(
"Bug in UI description. Description for option <%s> missing"),
364 opt->key ?
opt->key :
"?");
391 if (
opt->descriptions) {
408 while (
opt->opts[
j]) {
416 G_warning(
_(
"Bug in UI description. Option '%s' in "
417 "<%s> does not exist"),
431 if (
opt->multiple &&
opt->answers &&
opt->answers[0]) {
434 for (i = 1;
opt->answers[i]; i++) {
451 if (module_gui_wx() == 0)
459 else if (
argc >= 2) {
477 if (
strcmp(
argv[1],
"--interface-description") == 0) {
484 if (
strcmp(
argv[1],
"--html-description") == 0) {
491 if (
strcmp(
argv[1],
"--rst-description") == 0) {
498 if (
strcmp(
argv[1],
"--md-description") == 0) {
505 if (
strcmp(
argv[1],
"--wps-process-description") == 0) {
522 if (
strcmp(ptr,
"help") == 0 ||
strcmp(ptr,
"--h") == 0 ||
523 strcmp(ptr,
"-help") == 0 ||
strcmp(ptr,
"--help") == 0) {
529 if (
strcmp(ptr,
"--json") == 0) {
535 if (
strcmp(ptr,
"--o") == 0 ||
strcmp(ptr,
"--overwrite") == 0) {
540 else if (
strcmp(ptr,
"--v") == 0 ||
strcmp(ptr,
"--verbose") == 0) {
545 snprintf(buff,
sizeof(buff),
"GRASS_VERBOSE=%d",
548 if (
st->quiet == 1) {
549 G_warning(
_(
"Use either --quiet or --verbose flag, not "
550 "both. Assuming --verbose."));
556 else if (
strcmp(ptr,
"--q") == 0 ||
strcmp(ptr,
"--quiet") == 0) {
561 snprintf(buff,
sizeof(buff),
"GRASS_VERBOSE=%d",
564 if (
st->quiet == -1) {
565 G_warning(
_(
"Use either --quiet or --verbose flag, not "
566 "both. Assuming --quiet."));
572 else if (
strcmp(ptr,
"--qq") == 0) {
577 snprintf(buff,
sizeof(buff),
"GRASS_VERBOSE=%d",
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)) {
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) {
647 for (i = 0; i <
st->n_errors; i++) {
660 if (!
st->suppress_overwrite) {
661 if (check_overwrite())
689 G_debug(3,
"G_recreate_command()");
693 buff =
G_calloc(1024,
sizeof(
char));
701 nalloced += (1024 > len) ? 1024 : len + 1;
711 nalloced += (1024 > len) ? 1024 : len + 1;
714 strcpy(cur,
" --overwrite");
729 nalloced += (1024 > len) ? 1024 : len + 1;
740 if (
flag->answer == 1) {
760 opt = &
st->first_option;
761 while (
st->n_opts &&
opt) {
762 if (
opt->answer &&
opt->answer[0] ==
'\0') {
781 else if (
opt->answer &&
opt->answers &&
opt->answers[0]) {
802 for (n = 1;
opt->answers[n]; n++) {
803 if (!
opt->answers[n])
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 *));
886 st->module_info.keywords = (
const char **)
tokens;
897 if (
st->module_info.overwrite)
903 opt = &
st->first_option;
905 if (
opt->gisprompt) {
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]);
959 return st->module_info.overwrite;
962void define_keywords(
void)
965 st->n_keys_alloc = 0;
978int module_gui_wx(
void)
1018 if (
flag->key == f) {
1020 if (
flag->suppress_required)
1021 st->suppress_required = 1;
1022 if (
flag->suppress_overwrite)
1023 st->suppress_overwrite = 1;
1041 G_warning(
_(
"Please update the usage of <%s>: "
1042 "flag <%c> has been renamed to <%s>"),
1051 _(
"Please update the usage of <%s>: "
1052 "flag <%c> has been renamed to <%s>"),
1061 G_warning(
_(
"Please update the usage of <%s>: "
1062 "flag <%c> has been renamed to <%s>"),
1065 if (
flag->suppress_required)
1066 st->suppress_required = 1;
1067 if (
flag->suppress_overwrite)
1068 st->suppress_overwrite = 1;
1080 _(
"Please update the usage of <%s>: "
1081 "flag <%c> has been renamed to option <%s>"),
1098int contains(
const char *s,
int c)
1108int valid_option_name(
const char *
string)
1111 int n =
strspn(
string,
"abcdefghijklmnopqrstuvwxyz0123456789_");
1119 if (
string[
m - 1] ==
'_')
1125int is_option(
const char *
string)
1127 int n =
strspn(
string,
"abcdefghijklmnopqrstuvwxyz0123456789_");
1129 return n > 0 &&
string[n] ==
'=' &&
string[0] !=
'_' &&
1130 string[n - 1] !=
'_';
1133int match_option_1(
const char *
string,
const char *
option)
1137 if (*
string ==
'\0')
1143 if (*
string == *
option && match_option_1(
string + 1,
option + 1))
1146 if (*
option ==
'_' && match_option_1(
string,
option + 1))
1154 return match_option_1(
string + 1, next + 1);
1156 return match_option_1(
string, next + 1);
1159int match_option(
const char *
string,
const char *
option)
1161 return (*
string == *
option) && match_option_1(
string + 1,
option + 1);
1164void set_option(
const char *
string)
1176 for (ptr =
the_key; *
string !=
'='; ptr++,
string++)
1211 for (i = 1; i <
found; i++) {
1230 for (i = 0; i <
found; i++) {
1251 _(
"Please update the usage of <%s>: "
1252 "option <%s> has been renamed to flag <%s>"),
1256 _(
"Please update the usage of <%s>: "
1257 "option <%s> has been renamed to flag <%c>"),
1267 G_warning(
_(
"Please update the usage of <%s>: "
1268 "option <%s> has been renamed to <%s>"),
1291 if (!
opt->multiple) {
1292 G_asprintf(&
err,
_(
"Option <%s> does not accept multiple answers"),
1305void check_opts(
void)
1313 opt = &
st->first_option;
1318 if (
opt->multiple == 0)
1319 check_an_opt(
opt->key,
opt->type,
opt->options,
opt->opts,
1323 check_an_opt(
opt->key,
opt->type,
opt->options,
opt->opts,
1331 opt->checker(
opt->answer);
1365 _(
"Illegal range syntax for parameter <%s>\n"
1366 "\tPresented as: %s"),
1372 _(
"Value <%s> out of range for parameter <%s>\n"
1373 "\tLegal range: %s"),
1388 _(
"Value <%s> ambiguous for parameter <%s>\n"
1389 "\tValid options: %s"),
1400int check_int(
const char *
ans,
const char **
opts)
1417 for (i = 0;
opts[i]; i++) {
1421 if (contains(
opt,
'-')) {
1423 if (d >=
lo && d <=
hi)
1450int check_double(
const char *
ans,
const char **
opts)
1468 for (i = 0;
opts[i]; i++) {
1472 if (contains(
opt,
'-')) {
1474 if (d >=
lo && d <=
hi)
1501int check_string(
const char *
ans,
const char **
opts,
int *result)
1511 for (i = 0;
opts[i]; i++) {
1526 for (i = 1; i <
found; i++) {
1546 if (
found > 0 &&
getenv(
"GRASS_FULL_OPTION_NAMES") &&
1560void check_required(
void)
1570 opt = &
st->first_option;
1572 if (
opt->required && !
opt->answer) {
1574 _(
"Required parameter <%s> not set:\n"
1583void split_opts(
void)
1595 opt = &
st->first_option;
1600 opt->answers =
G_malloc(allocated *
sizeof(
char *));
1640void check_multiple_opts(
void)
1652 opt = &
st->first_option;
1658 for (ptr =
opt->key_desc; *ptr !=
'\0'; ptr++)
1662 for (n = 0;
opt->answers[n] !=
NULL; n++)
1667 _(
"Option <%s> must be provided in multiples of %d\n"
1668 "\tYou provided %d item(s): %s"),
1678int check_overwrite(
void)
1688 st->module_info.overwrite = 0;
1705 if (
st->overwrite ||
over) {
1706 st->module_info.overwrite = 1;
1708 putenv(
"GRASS_OVERWRITE=1");
1713 opt = &
st->first_option;
1715 if (
opt->answer &&
opt->gisprompt) {
1718 if (
strcmp(age,
"new") == 0) {
1722 for (i = 0;
opt->answers[i]; i++) {
1737 if (!
st->overwrite && !
over) {
1742 _(
"option <%s>: <%s> exists. To "
1743 "overwrite, use the --overwrite "
1745 opt->key,
opt->answers[i]);
1752 _(
"option <%s>: <%s> exists. To "
1753 "overwrite, use the --overwrite "
1755 opt->key,
opt->answers[i]);
1801void append_error(
const char *
msg)
1803 st->error =
G_realloc(
st->error,
sizeof(
char *) * (
st->n_errors + 1));
1807const char *get_renamed_option(
const char *
key)
1812 if (!
st->renamed_options) {
1863 strcmp(
option->gisprompt,
"old,separator,separator") != 0)
1884 G_debug(3,
"G_option_to_separator(): key = %s -> sep = '%s'",
option->key,
1930 else if (
option->multiple)
1933 else if (
strcmp(
option->gisprompt,
"old,file,file") == 0) {
1940 else if (
strcmp(
option->gisprompt,
"new,file,file") == 0) {
const char * G_program_name(void)
Return module name.
const char * G_getenv_nofatal(const char *)
Get environment variable.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_free(void *)
Free allocated memory.
const char * G_original_program_name(void)
Return original path of the executed program.
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_gisbase(void)
Get full path name of the top level module directory.
const char * G_find_file(const char *, char *, const char *)
Searches for a file from the mapset search list or in a specified mapset.
char ** G_tokenize(const char *, const char *)
Tokenize string.
struct Key_Value * G_read_key_value_file(const char *)
Read key/values pairs from file.
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
int G_number_of_tokens(char **)
Return number of tokens.
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
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)
char * G_chop(char *)
Chop leading and trailing white spaces.
char * G_store(const char *)
Copy string to allocated memory.
void G_usage(void)
Command line help/usage message.
int G_info_format(void)
Get current message format.
char * G_basename(char *, const char *)
Truncates filename to the base part (before the last '.') if it matches the extension,...
int G_debug(int, const char *,...) __attribute__((format(printf
int G_verbose_std(void)
Get standard verbosity level.
void int G_suppress_warnings(int)
Suppress printing a warning message to stderr.
const char * G_mapset(void)
Get current mapset name.
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)
struct Flag * G_define_flag(void)
Initializes a Flag struct.
int G_parser(int argc, char **argv)
Parse command line.
void G_set_keywords(const char *keywords)
Set keywords from the string.
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 '-',...
int G__uses_new_gisprompt(void)
void G_add_keyword(const char *keyword)
Add keyword to the list.
int G_get_overwrite(void)
Get overwrite value.
char * G_option_to_separator(const struct Option *option)
Get separator string from the option.
char * recreate_command(int original_path)
Creates command to run non-interactive.
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.
struct Option * G_define_option(void)
Initializes an Option struct.
void G_disable_interactive(void)
Disables the ability of the parser to operate interactively.
char * G_recreate_command_original_path(void)
Creates command to run non-interactive.
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.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)