/*!
* \file gis/parser.c
*
* \brief GIS Library - Argument parsing functions.
*
* Parses the command line provided through argc and argv. Example:
* Assume the previous calls:
*
* \code
* opt1 = G_define_option() ;
* opt1->key = "map",
* opt1->type = TYPE_STRING,
* opt1->required = YES,
* opt1->checker = sub,
* opt1->description= "Name of an existing raster map" ;
*
* opt2 = G_define_option() ;
* opt2->key = "color",
* opt2->type = TYPE_STRING,
* opt2->required = NO,
* opt2->answer = "white",
* opt2->options = "red,orange,blue,white,black",
* opt2->description= "Color used to display the map" ;
*
* opt3 = G_define_option() ;
* opt3->key = "number",
* opt3->type = TYPE_DOUBLE,
* opt3->required = NO,
* opt3->answer = "12345.67",
* opt3->options = "0-99999",
* opt3->description= "Number to test parser" ;
* \endcode
*
* G_parser() will respond to the following command lines as described:
*
* \verbatim
* command (No command line arguments)
* \endverbatim
* Parser enters interactive mode.
*
* \verbatim
* command map=map.name
* \endverbatim
* Parser will accept this line. Map will be set to "map.name", the
* 'a' and 'b' flags will remain off and the num option will be set
* to the default of 5.
*
* \verbatim
* command -ab map=map.name num=9
* command -a -b map=map.name num=9
* command -ab map.name num=9
* command map.name num=9 -ab
* command num=9 -a map=map.name -b
* \endverbatim
* These are all treated as acceptable and identical. Both flags are
* set to on, the map option is "map.name" and the num option is "9".
* Note that the "map=" may be omitted from the command line if it
* is part of the first option (flags do not count).
*
* \verbatim
* command num=12
* \endverbatim
* This command line is in error in two ways. The user will be told
* that the "map" option is required and also that the number 12 is
* out of range. The acceptable range (or list) will be printed.
*
* (C) 2001-2009 by the GRASS Development Team
*
* This program is free software under the GNU General Public License
* (>=v2). Read the file COPYING that comes with GRASS for details.
*
* \author Original author CERL
* \author Soeren Gebbert added Dec. 2009 WPS process_description document
*/
#include "parser_local_proto.h"
/* initialize the global struct */
struct state state;
struct state *st = &state;
/* local prototypes */
static int set_flag(int);
static int contains(const char *, int);
static int is_option(const char *);
static int set_option(const char *);
static int check_opts(void);
static int check_an_opt(const char *, int, const char *, const char **, char **);
static int check_int(const char *, const char **);
static int check_double(const char *, const char **);
static int check_string(const char *, const char **, int *);
static int check_required(void);
static void split_opts(void);
static int check_multiple_opts(void);
static int check_overwrite(void);
static void define_keywords(void);
static void split_gisprompt(const char *gisprompt, char *age, char *element, char *desc);
static void module_gui_wx(void);
/*!
* \brief Disables the ability of the parser to operate interactively.
*
* When a user calls a command with no arguments on the command line,
* the parser will enter its own standardized interactive session in
* which all flags and options are presented to the user for input. A
* call to G_disable_interactive() disables the parser's interactive
* prompting.
*
*/
void G_disable_interactive(void)
{
st->no_interactive = 1;
}
/*!
* \brief Initializes a Flag struct.
*
* Allocates memory for the Flag structure and returns a pointer to
* this memory.
*
* Flags are always represented by single letters. A user "turns them
* on" at the command line using a minus sign followed by the
* character representing the flag.
*
* \return Pointer to a Flag struct
*/
struct Flag *G_define_flag(void)
{
struct Flag *flag;
struct Item *item;
/* Allocate memory if not the first flag */
if (st->n_flags) {
flag = (struct Flag *)G_malloc(sizeof(struct Flag));
st->current_flag->next_flag = flag;
}
else
flag = &st->first_flag;
/* Zero structure */
G_zero((char *)flag, sizeof(struct Flag));
st->current_flag = flag;
st->n_flags++;
if (st->n_items) {
item = (struct Item *)G_malloc(sizeof(struct Item));
st->current_item->next_item = item;
}
else
item = &st->first_item;
G_zero((char *)item, sizeof(struct Item));
item->flag = flag;
item->option = NULL;
st->current_item = item;
st->n_items++;
return (flag);
}
/*!
* \brief Initializes an Option struct.
*
* Allocates memory for the Option structure and returns a pointer to
* this memory.
*
* Options are provided by user on command line using the standard
* format: key=value. Options identified as REQUIRED must be
* specified by user on command line. The option string can either
* specify a range of values (e.g. "10-100") or a list of acceptable
* values (e.g. "red,orange,yellow"). Unless the option string is
* NULL, user provided input will be evaluated agaist this string.
*
* \return pointer to an Option struct
*/
struct Option *G_define_option(void)
{
struct Option *opt;
struct Item *item;
/* Allocate memory if not the first option */
if (st->n_opts) {
opt = (struct Option *)G_malloc(sizeof(struct Option));
st->current_option->next_opt = opt;
}
else
opt = &st->first_option;
/* Zero structure */
G_zero((char *)opt, sizeof(struct Option));
opt->required = NO;
opt->multiple = NO;
opt->answer = NULL;
opt->answers = NULL;
opt->def = NULL;
opt->checker = NULL;
opt->options = NULL;
opt->key_desc = NULL;
opt->gisprompt = NULL;
opt->label = NULL;
opt->opts = NULL;
opt->description = NULL;
opt->descriptions = NULL;
opt->guisection = NULL;
opt->guidependency = NULL;
st->current_option = opt;
st->n_opts++;
if (st->n_items) {
item = (struct Item *)G_malloc(sizeof(struct Item));
st->current_item->next_item = item;
}
else
item = &st->first_item;
G_zero((char *)item, sizeof(struct Item));
item->option = opt;
item->flag = NULL;
st->current_item = item;
st->n_items++;
return (opt);
}
/*!
* \brief Initializes a new module.
*
* \return pointer to a GModule struct
*/
struct GModule *G_define_module(void)
{
struct GModule *module;
/* Allocate memory */
module = &st->module_info;
/* Zero structure */
G_zero((char *)module, sizeof(struct GModule));
/* Allocate keywords array */
define_keywords();
return (module);
}
/*!
* \brief Parse command line.
*
* The command line parameters argv and the number of
* parameters argc from the main() routine are passed directly
* to G_parser(). G_parser() accepts the command line input entered by
* the user, and parses this input according to the input options
* and/or flags that were defined by the programmer.
*
* Note: The only functions which can legitimately be called
* before G_parser() are:
*
* - G_gisinit()
* - G_no_gisinit()
* - G_define_module()
* - G_define_flag()
* - G_define_option()
* - G_define_standard_option()
* - G_disable_interactive()
*
* The usual order a module calls functions is:
*
* # G_gisinit()
* # G_define_module()
* # G_define_flag()
* # G_define_option()
* # G_parser()
*
* \param argc number of arguments
* \param argv argument list
*
* \return 0 on success
* \return -1 on error and calls G_usage()
*/
int G_parser(int argc, char **argv)
{
int need_first_opt;
int opt_checked = 0;
int error;
char *ptr, *tmp_name;
int i;
struct Option *opt;
char force_gui = FALSE;
error = 0;
need_first_opt = 1;
tmp_name = G_store(argv[0]);
st->pgm_path = tmp_name;
i = strlen(tmp_name);
while (--i >= 0) {
if (G_is_dirsep(tmp_name[i])) {
tmp_name += i + 1;
break;
}
}
G_basename(tmp_name, "exe");
st->pgm_name = tmp_name;
/* Stash default answers */
opt = &st->first_option;
while (opt != NULL) {
if (opt->required)
st->has_required = 1;
/* Parse options */
if (opt->options) {
int cnt = 0;
char **tokens, delm[2];
delm[0] = ',';
delm[1] = '\0';
tokens = G_tokenize(opt->options, delm);
i = 0;
while (tokens[i]) {
cnt++;
i++;
}
opt->opts =
(const char **)G_calloc(cnt + 1, sizeof(const char *));
i = 0;
while (tokens[i]) {
opt->opts[i] = G_store(tokens[i]);
i++;
}
G_free_tokens(tokens);
if (opt->descriptions) {
delm[0] = ';';
opt->descs =
(const char **)G_calloc(cnt + 1, sizeof(const char *));
tokens = G_tokenize(opt->descriptions, delm);
i = 0;
while (tokens[i]) {
int j, found;
if (!tokens[i + 1])
break;
j = 0;
found = 0;
while (opt->opts[j]) {
if (strcmp(opt->opts[j], tokens[i]) == 0) {
found = 1;
break;
}
j++;
}
if (!found) {
G_warning(_("BUG in descriptions, option '%s' in <%s> does not exist"),
tokens[i], opt->key);
}
else {
opt->descs[j] = G_store(tokens[i + 1]);
}
i += 2;
}
G_free_tokens(tokens);
}
}
/* Copy answer */
if (opt->multiple && opt->answers && opt->answers[0]) {
opt->answer = (char *)G_malloc(strlen(opt->answers[0]) + 1);
strcpy(opt->answer, opt->answers[0]);
for (i = 1; opt->answers[i]; i++) {
opt->answer = (char *)G_realloc(opt->answer,
strlen(opt->answer) +
strlen(opt->answers[i]) + 2);
strcat(opt->answer, ",");
strcat(opt->answer, opt->answers[i]);
}
}
opt->def = opt->answer;
opt = opt->next_opt;
}
/* If there are NO arguments, go interactive */
if (argc < 2 && st->has_required && !st->no_interactive && isatty(0)) {
module_gui_wx();
return -1;
}
else if (argc < 2 && st->has_required && isatty(0)) {
G_usage();
return -1;
}
else if (argc >= 2) {
/* If first arg is "help" give a usage/syntax message */
if (strcmp(argv[1], "help") == 0 ||
strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0) {
G_usage();
exit(EXIT_SUCCESS);
}
/* If first arg is "--interface-description" then print out
* a xml description of the task */
if (strcmp(argv[1], "--interface-description") == 0) {
G__usage_xml();
exit(EXIT_SUCCESS);
}
/* If first arg is "--html-description" then print out
* a html description of the task */
if (strcmp(argv[1], "--html-description") == 0) {
G__usage_html();
exit(EXIT_SUCCESS);
}
/* If first arg is "--wps-process-description" then print out
* the wps process description of the task */
if (strcmp(argv[1], "--wps-process-description") == 0) {
G__wps_print_process_description();
exit(EXIT_SUCCESS);
}
/* If first arg is "--script" then then generate
* g.parser boilerplate */
if (strcmp(argv[1], "--script") == 0) {
G__script();
exit(EXIT_SUCCESS);
}
/* Loop thru all command line arguments */
while (--argc) {
ptr = *(++argv);
/* Overwrite option */
if (strcmp(ptr, "--o") == 0 || strcmp(ptr, "--overwrite") == 0) {
st->overwrite = 1;
}
/* Verbose option */
else if (strcmp(ptr, "--v") == 0 || strcmp(ptr, "--verbose") == 0) {
char buff[32];
/* print everything: max verbosity level */
st->module_info.verbose = G_verbose_max();
sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_max());
putenv(G_store(buff));
if (st->quiet == 1) {
G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --verbose."));
}
st->quiet = -1;
}
/* Quiet option */
else if (strcmp(ptr, "--q") == 0 || strcmp(ptr, "--quiet") == 0) {
char buff[32];
/* print nothing, but errors and warnings */
st->module_info.verbose = G_verbose_min();
sprintf(buff, "GRASS_VERBOSE=%d", G_verbose_min());
putenv(G_store(buff));
if (st->quiet == -1) {
G_warning(_("Use either --quiet or --verbose flag, not both. Assuming --quiet."));
}
st->quiet = 1; /* for passing to gui init */
}
/* Force gui to come up */
else if (strcmp(ptr, "--ui") == 0) {
force_gui = TRUE;
}
/* If we see a flag */
else if (*ptr == '-') {
while (*(++ptr))
error += set_flag(*ptr);
}
/* If we see standard option format (option=val) */
else if (is_option(ptr)) {
error += set_option(ptr);
need_first_opt = 0;
}
/* If we see the first option with no equal sign */
else if (need_first_opt && st->n_opts) {
st->first_option.answer = G_store(ptr);
need_first_opt = 0;
}
/* If we see the non valid argument (no "=", just argument) */
else if (contains(ptr, '=') == 0) {
fprintf(stderr, _("Sorry <%s> is not a valid option\n"), ptr);
error = 1;
}
}
}
/* Run the gui if it was specifically requested */
if (force_gui) {
module_gui_wx();
return -1;
}
/* Split options where multiple answers are OK */
split_opts();
/* Check multiple options */
error += check_multiple_opts();
/* Check answers against options and check subroutines */
if (!opt_checked)
error += check_opts();
/* Make sure all required options are set */
error += check_required();
if (error) {
if (G_verbose() > G_verbose_min())
G_usage();
return -1;
}
if (check_overwrite())
return -1;
return (0);
}
/*!
* \brief Creates command to run non-interactive.
*
* Creates a command-line that runs the current command completely
* non-interactive.
*
* \return pointer to a char string
*/
char *G_recreate_command(void)
{
char *buff;
char flg[4];
char *cur;
const char *tmp;
struct Flag *flag;
struct Option *opt;
int n, len, slen;
int nalloced = 0;
G_debug(3, "G_recreate_command()");
/* Flag is not valid if there are no flags to set */
buff = G_calloc(1024, sizeof(char));
nalloced += 1024;
tmp = G_program_name();
len = strlen(tmp);
if (len >= nalloced) {
nalloced += (1024 > len) ? 1024 : len + 1;
buff = G_realloc(buff, nalloced);
}
cur = buff;
strcpy(cur, tmp);
cur += len;
if (st->n_flags) {
flag = &st->first_flag;
while (flag != '\0') {
if (flag->answer == 1) {
flg[0] = ' ';
flg[1] = '-';
flg[2] = flag->key;
flg[3] = '\0';
slen = strlen(flg);
if (len + slen >= nalloced) {
nalloced +=
(nalloced + 1024 > len + slen) ? 1024 : slen + 1;
buff = G_realloc(buff, nalloced);
cur = buff + len;
}
strcpy(cur, flg);
cur += slen;
len += slen;
}
flag = flag->next_flag;
}
}
opt = &st->first_option;
while (opt != '\0') {
if (opt->answer != '\0' && opt->answers[0] != NULL) {
slen = strlen(opt->key) + strlen(opt->answers[0]) + 4; /* +4 for: ' ' = " " */
if (len + slen >= nalloced) {
nalloced += (nalloced + 1024 > len + slen) ? 1024 : slen + 1;
buff = G_realloc(buff, nalloced);
cur = buff + len;
}
strcpy(cur, " ");
cur++;
strcpy(cur, opt->key);
cur = strchr(cur, '\0');
strcpy(cur, "=");
cur++;
if (opt->type == TYPE_STRING) {
strcpy(cur, "\"");
cur++;
}
strcpy(cur, opt->answers[0]);
cur = strchr(cur, '\0');
len = cur - buff;
for (n = 1; opt->answers[n] != NULL && opt->answers[n] != '\0';
n++) {
if (opt->answers[n] == NULL)
break;
slen = strlen(opt->answers[n]) + 2; /* +2 for , " */
if (len + slen >= nalloced) {
nalloced +=
(nalloced + 1024 > len + slen) ? 1024 : slen + 1;
buff = G_realloc(buff, nalloced);
cur = buff + len;
}
strcpy(cur, ",");
cur++;
strcpy(cur, opt->answers[n]);
cur = strchr(cur, '\0');
len = cur - buff;
}
if (opt->type == TYPE_STRING) {
strcpy(cur, "\"");
cur++;
len = cur - buff;
}
}
opt = opt->next_opt;
}
return (buff);
}
/*!
\brief Add keyword to the list
\param keyword keyword string
*/
void G_add_keyword(const char *keyword)
{
if (st->n_keys >= st->n_keys_alloc) {
st->n_keys_alloc += 10;
st->module_info.keywords = (const char **) G_realloc(st->module_info.keywords,
st->n_keys_alloc * sizeof(char *));
}
st->module_info.keywords[st->n_keys++] = G_store(keyword);
}
/*!
\brief Set keywords from the string
\param keywords keywords separated by commas
*/
void G_set_keywords(const char *keywords)
{
st->module_info.keywords = (const char**)G_tokenize(keywords, ",");
st->n_keys = st->n_keys_alloc = G_number_of_tokens((char **)st->module_info.keywords);
}
int G__uses_new_gisprompt(void)
{
struct Option *opt;
char age[KEYLENGTH];
char element[KEYLENGTH];
char desc[KEYLENGTH];
if (st->module_info.overwrite)
return 1;
/* figure out if any of the options use a "new" gisprompt */
/* This is to see if we should spit out the --o flag */
if (st->n_opts) {
opt = &st->first_option;
while (opt != NULL) {
if (opt->gisprompt) {
split_gisprompt(opt->gisprompt, age, element, desc);
if (strcmp(age, "new") == 0)
return 1;
}
opt = opt->next_opt;
}
}
return 0;
}
void G__print_keywords(FILE *fd, void (*format)(FILE *, const char *))
{
int i;
for(i = 0; i < st->n_keys; i++) {
if (!format) {
fprintf(fd, "%s", st->module_info.keywords[i]);
}
else {
format(fd, st->module_info.keywords[i]);
}
if (i < st->n_keys - 1)
fprintf(fd, ", ");
}
fflush(fd);
}
void define_keywords(void)
{
st->n_keys = 0;
st->n_keys_alloc = 0;
}
/**************************************************************************
*
* The remaining routines are all local (static) routines used to support
* the parsing process.
*
**************************************************************************/
/*!
\brief Invoke GUI dialog
*/
static void module_gui_wx(void)
{
char script[GPATH_MAX];
if (!st->pgm_path)
st->pgm_path = G_program_name();
if (!st->pgm_path)
G_fatal_error(_("Unable to determine program name"));
sprintf(script, "%s/etc/wxpython/gui_modules/menuform.py",
getenv("GISBASE"));
G_spawn(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), script, st->pgm_path, NULL);
}
static int set_flag(int f)
{
struct Flag *flag;
/* Flag is not valid if there are no flags to set */
if (!st->n_flags) {
fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
return (1);
}
/* Find flag with corrrect keyword */
flag = &st->first_flag;
while (flag != NULL) {
if (flag->key == f) {
flag->answer = 1;
return (0);
}
flag = flag->next_flag;
}
fprintf(stderr, _("Sorry, <%c> is not a valid flag\n"), f);
return (1);
}
/* contents() is used to find things strings with characters like commas and
* dashes.
*/
static int contains(const char *s, int c)
{
while (*s) {
if (*s == c)
return (1);
s++;
}
return (0);
}
static int is_option(const char *string)
{
int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
return n > 0 && string[n] == '=' && string[0] != '_' && string[n-1] != '_';
}
static int match_option_1(const char *string, const char *option)
{
const char *next;
if (*string == '\0')
return 1;
if (*option == '\0')
return 0;
if (*string == *option && match_option_1(string + 1, option + 1))
return 1;
if (*option == '_' && match_option_1(string, option + 1))
return 1;
next = strchr(option, '_');
if (!next)
return 0;
if (*string == '_')
return match_option_1(string + 1, next + 1);
return match_option_1(string, next + 1);
}
static int match_option(const char *string, const char *option)
{
return (*string == *option)
&& match_option_1(string + 1, option + 1);
}
static int set_option(const char *string)
{
struct Option *at_opt = NULL;
struct Option *opt = NULL;
int got_one;
size_t key_len;
char the_key[KEYLENGTH];
char *ptr;
for (ptr = the_key; *string != '='; ptr++, string++)
*ptr = *string;
*ptr = '\0';
string++;
/* Find option with best keyword match */
got_one = 0;
key_len = strlen(the_key);
for (at_opt = &st->first_option; at_opt != NULL; at_opt = at_opt->next_opt) {
if (!at_opt->key)
continue;
#if 1
if (!match_option(the_key, at_opt->key))
continue;
#else
if (strncmp(the_key, at_opt->key, key_len))
continue;
#endif
got_one++;
opt = at_opt;
/* changed 1/15/91 -dpg old code is in parser.old */
/* overide ambiguous check, if we get an exact match */
if (strlen(at_opt->key) == key_len) {
opt = at_opt;
got_one = 1;
break;
}
}
if (got_one > 1) {
fprintf(stderr, _("Sorry, <%s=> is ambiguous\n"), the_key);
return (1);
}
/* If there is no match, complain */
if (got_one == 0) {
fprintf(stderr, _("Sorry, <%s> is not a valid parameter\n"), the_key);
return (1);
}
/* Allocate memory where answer is stored */
if (opt->count++) {
opt->answer = (char *)G_realloc(opt->answer,
strlen(opt->answer) + strlen(string) +
2);
strcat(opt->answer, ",");
strcat(opt->answer, string);
}
else
opt->answer = G_store(string);
return (0);
}
static int check_opts(void)
{
struct Option *opt;
int error;
int ans;
error = 0;
if (!st->n_opts)
return (0);
opt = &st->first_option;
while (opt != NULL) {
/* Check answer against options if any */
if (opt->answer) {
if (opt->multiple == 0)
error += check_an_opt(opt->key, opt->type,
opt->options, opt->opts, &opt->answer);
else {
for (ans = 0; opt->answers[ans] != '\0'; ans++)
error += check_an_opt(opt->key, opt->type,
opt->options, opt->opts, &opt->answers[ans]);
}
}
/* Check answer against user's check subroutine if any */
if (opt->checker)
error += opt->checker(opt->answer);
opt = opt->next_opt;
}
return (error);
}
static int check_an_opt(const char *key, int type, const char *options,
const char **opts, char **answerp)
{
const char *answer = *answerp;
int error;
int found;
error = 0;
switch (type) {
case TYPE_INTEGER:
error = check_int(answer, opts);
break;
case TYPE_DOUBLE:
error = check_double(answer, opts);
break;
case TYPE_STRING:
error = check_string(answer, opts, &found);
break;
}
switch (error) {
case 0:
break;
case BAD_SYNTAX:
fprintf(stderr,
_("\nERROR: illegal range syntax for parameter <%s>\n"), key);
fprintf(stderr, _(" Presented as: %s\n"), options);
break;
case OUT_OF_RANGE:
fprintf(stderr,
_("\nERROR: value <%s> out of range for parameter <%s>\n"),
answer, key);
fprintf(stderr, _(" Legal range: %s\n"), options);
break;
case MISSING_VALUE:
fprintf(stderr, _("\nERROR: Missing value for parameter <%s>\n"),
key);
break;
case AMBIGUOUS:
fprintf(stderr, _("\nERROR: value <%s> ambiguous for parameter <%s>\n"),
answer, key);
fprintf(stderr, _(" valid options: %s\n"), options);
break;
case REPLACED:
*answerp = G_store(opts[found]);
error = 0;
break;
}
return error;
}
static int check_int(const char *ans, const char **opts)
{
int d, i;
if (sscanf(ans, "%d", &d) != 1)
return MISSING_VALUE;
if (!opts)
return 0;
for (i = 0; opts[i]; i++) {
const char *opt = opts[i];
int lo, hi;
if (contains(opt, '-')) {
if (sscanf(opt, "%d-%d", &lo, &hi) == 2) {
if (d >= lo && d <= hi)
return 0;
}
else if (sscanf(opt, "-%d", &hi) == 1) {
if (d <= hi)
return 0;
}
else if (sscanf(opt, "%d-", &lo) == 1) {
if (d >= lo)
return 0;
}
else
return BAD_SYNTAX;
}
else {
if (sscanf(opt, "%d", &lo) == 1) {
if (d == lo)
return 0;
}
else
return BAD_SYNTAX;
}
}
return OUT_OF_RANGE;
}
static int check_double(const char *ans, const char **opts)
{
double d;
int i;
if (sscanf(ans, "%lf", &d) != 1)
return MISSING_VALUE;
if (!opts)
return 0;
for (i = 0; opts[i]; i++) {
const char *opt = opts[i];
double lo, hi;
if (contains(opt, '-')) {
if (sscanf(opt, "%lf-%lf", &lo, &hi) == 2) {
if (d >= lo && d <= hi)
return 0;
}
else if (sscanf(opt, "-%lf", &hi) == 1) {
if (d <= hi)
return 0;
}
else if (sscanf(opt, "%lf-", &lo) == 1) {
if (d >= lo)
return 0;
}
else
return BAD_SYNTAX;
}
else {
if (sscanf(opt, "%lf", &lo) == 1) {
if (d == lo)
return 0;
}
else
return BAD_SYNTAX;
}
}
return OUT_OF_RANGE;
}
static int check_string(const char *ans, const char **opts, int *result)
{
int len = strlen(ans);
int found = 0;
int i;
if (!opts)
return 0;
for (i = 0; opts[i]; i++) {
if (strcmp(ans, opts[i]) == 0)
return 0;
if (strncmp(ans, opts[i], len) == 0) {
*result = i;
found++;
}
}
switch (found) {
case 0: return OUT_OF_RANGE;
case 1: return REPLACED;
default: return AMBIGUOUS;
}
}
static int check_required(void)
{
struct Option *opt;
int err;
err = 0;
if (!st->n_opts)
return (0);
opt = &st->first_option;
while (opt != NULL) {
if (opt->required && opt->answer == NULL) {
fprintf(stderr,
_("\nERROR: Required parameter <%s> not set:\n (%s).\n"),
opt->key, (opt->label ? opt->label : opt->description) );
err++;
}
opt = opt->next_opt;
}
return (err);
}
static void split_opts(void)
{
struct Option *opt;
char *ptr1;
char *ptr2;
int allocated;
int ans_num;
int len;
if (!st->n_opts)
return;
opt = &st->first_option;
while (opt != NULL) {
if ( /*opt->multiple && */ (opt->answer != NULL)) {
/* Allocate some memory to store array of pointers */
allocated = 10;
opt->answers = (char **)G_malloc(allocated * sizeof(char *));
ans_num = 0;
ptr1 = opt->answer;
opt->answers[ans_num] = NULL;
for (;;) {
for (len = 0, ptr2 = ptr1; *ptr2 != '\0' && *ptr2 != ',';
ptr2++, len++) ;
if (len > 0) { /* skip ,, */
opt->answers[ans_num] = (char *)G_malloc(len + 1);
G_copy(opt->answers[ans_num], ptr1, len);
opt->answers[ans_num][len] = 0;
ans_num++;
if (ans_num >= allocated) {
allocated += 10;
opt->answers =
(char **)G_realloc((char *)opt->answers,
allocated * sizeof(char *));
}
opt->answers[ans_num] = NULL;
}
if (*ptr2 == '\0')
break;
ptr1 = ptr2 + 1;
if (*ptr1 == '\0')
break;
}
}
opt = opt->next_opt;
}
}
static int check_multiple_opts(void)
{
struct Option *opt;
const char *ptr;
int n_commas;
int n;
int error;
if (!st->n_opts)
return (0);
error = 0;
opt = &st->first_option;
while (opt != NULL) {
if ((opt->answer != NULL) && (opt->key_desc != NULL)) {
/* count commas */
n_commas = 1;
for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
if (*ptr == ',')
n_commas++;
/* count items */
for (n = 0; opt->answers[n] != '\0'; n++) ;
/* if not correct multiple of items */
if (n % n_commas) {
fprintf(stderr,
_("\nERROR: option <%s> must be provided in multiples of %d\n"),
opt->key, n_commas);
fprintf(stderr, _(" You provided %d items:\n"), n);
fprintf(stderr, " %s\n", opt->answer);
error++;
}
}
opt = opt->next_opt;
}
return (error);
}
/* Check for all 'new' if element already exists */
static int check_overwrite(void)
{
struct Option *opt;
char age[KEYLENGTH];
char element[KEYLENGTH];
char desc[KEYLENGTH];
int error = 0;
const char *overstr;
int over;
st->module_info.overwrite = 0;
if (!st->n_opts)
return (0);
over = 0;
/* Check the GRASS OVERWRITE variable */
if ((overstr = G__getenv("OVERWRITE"))) {
over = atoi(overstr);
}
/* Check the GRASS_OVERWRITE environment variable */
if ((overstr = getenv("GRASS_OVERWRITE"))) {
if (atoi(overstr))
over = 1;
}
if (st->overwrite || over) {
st->module_info.overwrite = 1;
/* Set the environment so that programs run in a script also obey --o */
putenv("GRASS_OVERWRITE=1");
/* No need to check options for existing files if overwrite is true */
return error;
}
opt = &st->first_option;
while (opt != NULL) {
if ((opt->answer != NULL) && (opt->gisprompt != NULL)) {
split_gisprompt(opt->gisprompt, age, element, desc);
if (strcmp(age, "new") == 0) {
int i;
for (i = 0; opt->answers[i]; i++) {
if (G_find_file(element, opt->answers[i], G_mapset())) { /* found */
if (!st->overwrite && !over) {
if (G_info_format() != G_INFO_FORMAT_GUI) {
fprintf(stderr,
_("ERROR: option <%s>: <%s> exists.\n"),
opt->key, opt->answers[i]);
}
else {
fprintf(stderr,
"GRASS_INFO_ERROR(%d,1): option <%s>: <%s> exists.\n",
getpid(), opt->key, opt->answers[i]);
fprintf(stderr, "GRASS_INFO_END(%d,1)\n",
getpid());
}
error = 1;
}
}
}
}
}
opt = opt->next_opt;
}
return (error);
}
static void split_gisprompt(const char *gisprompt, char *age, char *element,
char *desc)
{
const char *ptr1;
char *ptr2;
for (ptr1 = gisprompt, ptr2 = age; *ptr1 != '\0'; ptr1++, ptr2++) {
if (*ptr1 == ',')
break;
*ptr2 = *ptr1;
}
*ptr2 = '\0';
for (ptr1++, ptr2 = element; *ptr1 != '\0'; ptr1++, ptr2++) {
if (*ptr1 == ',')
break;
*ptr2 = *ptr1;
}
*ptr2 = '\0';
for (ptr1++, ptr2 = desc; *ptr1 != '\0'; ptr1++, ptr2++) {
if (*ptr1 == ',')
break;
*ptr2 = *ptr1;
}
*ptr2 = '\0';
}