| 1 |
|
|---|
| 2 | /****************************************************************************
|
|---|
| 3 | *
|
|---|
| 4 | * MODULE: g.gisenv
|
|---|
| 5 | * AUTHOR(S): Michael Shapiro CERL (original contributor)
|
|---|
| 6 | * Radim Blazek <radim.blazek gmail.com>,
|
|---|
| 7 | * Glynn Clements <glynn gclements.plus.com>,
|
|---|
| 8 | * Hamish Bowman <hamish_b yahoo.com>,
|
|---|
| 9 | * Markus Neteler <neteler itc.it>
|
|---|
| 10 | * Martin Landa <landa.martin gmail.com>
|
|---|
| 11 | * PURPOSE:
|
|---|
| 12 | * COPYRIGHT: (C) 2003-2015 by the GRASS Development Team
|
|---|
| 13 | *
|
|---|
| 14 | * This program is free software under the GNU General Public
|
|---|
| 15 | * License (>=v2). Read the file COPYING that comes with GRASS
|
|---|
| 16 | * for details.
|
|---|
| 17 | *
|
|---|
| 18 | *****************************************************************************/
|
|---|
| 19 |
|
|---|
| 20 | #include <stdio.h>
|
|---|
| 21 | #include <stdlib.h>
|
|---|
| 22 | #include <string.h>
|
|---|
| 23 | #include <unistd.h>
|
|---|
| 24 |
|
|---|
| 25 | #include <grass/gis.h>
|
|---|
| 26 | #include <grass/glocale.h>
|
|---|
| 27 |
|
|---|
| 28 | static char *parse_variable(const char *, char **);
|
|---|
| 29 |
|
|---|
| 30 | int main(int argc, char *argv[])
|
|---|
| 31 | {
|
|---|
| 32 | int n, store;
|
|---|
| 33 | const char *name, *u_name, *sep;
|
|---|
| 34 | char *value;
|
|---|
| 35 | struct Option *get_opt, *set_opt, *unset_opt, *store_opt, *sep_opt;
|
|---|
| 36 | struct Flag *flag_s, *flag_n;
|
|---|
| 37 | struct GModule *module;
|
|---|
| 38 |
|
|---|
| 39 | G_set_program_name(argv[0]);
|
|---|
| 40 | G_no_gisinit();
|
|---|
| 41 |
|
|---|
| 42 | module = G_define_module();
|
|---|
| 43 | G_add_keyword(_("general"));
|
|---|
| 44 | G_add_keyword(_("settings"));
|
|---|
| 45 | G_add_keyword(_("variables"));
|
|---|
| 46 | G_add_keyword(_("scripts"));
|
|---|
| 47 | module->label =
|
|---|
| 48 | _("Outputs and modifies the user's current GRASS variable settings.");
|
|---|
| 49 | module->description = _("Prints all defined GRASS variables if no option is given.");
|
|---|
| 50 |
|
|---|
| 51 | get_opt = G_define_option();
|
|---|
| 52 | get_opt->key = "get";
|
|---|
| 53 | get_opt->type = TYPE_STRING;
|
|---|
| 54 | get_opt->description = _("GRASS variable to get");
|
|---|
| 55 | get_opt->key_desc = "variable";
|
|---|
| 56 | get_opt->required = NO;
|
|---|
| 57 | get_opt->guisection = _("Get");
|
|---|
| 58 | get_opt->multiple = YES;
|
|---|
| 59 |
|
|---|
| 60 | set_opt = G_define_option();
|
|---|
| 61 | set_opt->key = "set";
|
|---|
| 62 | set_opt->type = TYPE_STRING;
|
|---|
| 63 | set_opt->description = _("GRASS variable to set");
|
|---|
| 64 | set_opt->key_desc = "\"variable=value\"";
|
|---|
| 65 | set_opt->required = NO;
|
|---|
| 66 | set_opt->guisection = _("Set");
|
|---|
| 67 |
|
|---|
| 68 | unset_opt = G_define_option();
|
|---|
| 69 | unset_opt->key = "unset";
|
|---|
| 70 | unset_opt->type = TYPE_STRING;
|
|---|
| 71 | unset_opt->description = _("GRASS variable to unset");
|
|---|
| 72 | unset_opt->key_desc = "variable";
|
|---|
| 73 | unset_opt->required = NO;
|
|---|
| 74 | unset_opt->guisection = _("Set");
|
|---|
| 75 | unset_opt->multiple = YES;
|
|---|
| 76 |
|
|---|
| 77 | store_opt = G_define_option();
|
|---|
| 78 | store_opt->key = "store";
|
|---|
| 79 | store_opt->type = TYPE_STRING;
|
|---|
| 80 | store_opt->options = "gisrc,mapset";
|
|---|
| 81 | store_opt->answer = "gisrc";
|
|---|
| 82 | store_opt->description = _("Where GRASS variable is stored");
|
|---|
| 83 | store_opt->required = NO;
|
|---|
| 84 | store_opt->guisection = _("Set");
|
|---|
| 85 |
|
|---|
| 86 | sep_opt = G_define_standard_option(G_OPT_F_SEP);
|
|---|
| 87 | sep_opt->label = _("Separator for multiple GRASS variables");
|
|---|
| 88 | sep_opt->answer = "newline";
|
|---|
| 89 |
|
|---|
| 90 | flag_s = G_define_flag();
|
|---|
| 91 | flag_s->key = 's';
|
|---|
| 92 | flag_s->description = _("Use shell syntax (for \"eval\")");
|
|---|
| 93 | flag_s->guisection = _("Format");
|
|---|
| 94 |
|
|---|
| 95 | flag_n = G_define_flag();
|
|---|
| 96 | flag_n->key = 'n';
|
|---|
| 97 | flag_n->description = _("Do not use shell syntax");
|
|---|
| 98 | flag_n->guisection = _("Format");
|
|---|
| 99 |
|
|---|
| 100 | G_option_exclusive(flag_s, flag_n, NULL);
|
|---|
| 101 | G_option_exclusive(get_opt, set_opt, unset_opt, NULL);
|
|---|
| 102 |
|
|---|
| 103 | if (G_parser(argc, argv))
|
|---|
| 104 | exit(EXIT_FAILURE);
|
|---|
| 105 |
|
|---|
| 106 | sep = G_option_to_separator(sep_opt);
|
|---|
| 107 |
|
|---|
| 108 | if (!get_opt->answer && !set_opt->answer && !unset_opt->answer) {
|
|---|
| 109 | /* Print or optionally set environment variables */
|
|---|
| 110 | int quote;
|
|---|
| 111 |
|
|---|
| 112 | if (flag_s->answer)
|
|---|
| 113 | quote = TRUE;
|
|---|
| 114 | else if (flag_n->answer)
|
|---|
| 115 | quote = FALSE;
|
|---|
| 116 | else
|
|---|
| 117 | quote = !isatty(fileno(stdout));
|
|---|
| 118 |
|
|---|
| 119 | for (n = 0; (name = G_get_env_name(n)); n++) {
|
|---|
| 120 | value = (char *)G_getenv_nofatal(name);
|
|---|
| 121 | if (value) {
|
|---|
| 122 | if (!quote)
|
|---|
| 123 | fprintf(stdout, "%s=%s\n", name, value);
|
|---|
| 124 | else
|
|---|
| 125 | fprintf(stdout, "%s='%s';\n", name, value);
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | exit(EXIT_SUCCESS);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | store = G_VAR_GISRC;
|
|---|
| 132 | if (store_opt->answer[0] == 'm')
|
|---|
| 133 | store = G_VAR_MAPSET;
|
|---|
| 134 |
|
|---|
| 135 | if (get_opt->answer) {
|
|---|
| 136 | n = 0;
|
|---|
| 137 | while (get_opt->answers[n]) {
|
|---|
| 138 | if (n > 0)
|
|---|
| 139 | fprintf(stdout, "%s", sep);
|
|---|
| 140 | u_name = parse_variable(get_opt->answers[n], NULL);
|
|---|
| 141 | value = (char *)G_getenv2(u_name, store);
|
|---|
| 142 | fprintf(stdout, "%s", value);
|
|---|
| 143 | n++;
|
|---|
| 144 | }
|
|---|
| 145 | if (strcmp(sep, "\n") != 0)
|
|---|
| 146 | fprintf(stdout, "\n");
|
|---|
| 147 |
|
|---|
| 148 | exit(EXIT_SUCCESS);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | u_name = NULL;
|
|---|
| 152 | if (set_opt->answer) {
|
|---|
| 153 | u_name = parse_variable(set_opt->answer, &value);
|
|---|
| 154 | if (value) {
|
|---|
| 155 | G_setenv2(u_name, value, store);
|
|---|
| 156 | }
|
|---|
| 157 | else {
|
|---|
| 158 | /* unset */
|
|---|
| 159 | G_getenv2(u_name, store); /* G_fatal_error() if not defined */
|
|---|
| 160 | G_unsetenv2(u_name, store);
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | if (unset_opt->answer) {
|
|---|
| 165 | n = 0;
|
|---|
| 166 | while (unset_opt->answers[n]) {
|
|---|
| 167 | u_name = parse_variable(unset_opt->answers[n], &value);
|
|---|
| 168 | if (G_strcasecmp(u_name, "GISDBASE") == 0 ||
|
|---|
| 169 | G_strcasecmp(u_name, "LOCATION_NAME") == 0 ||
|
|---|
| 170 | G_strcasecmp(u_name, "MAPSET") == 0) {
|
|---|
| 171 | G_warning(_("Variable <%s> is mandatory. No operation performed."),
|
|---|
| 172 | u_name);
|
|---|
| 173 | n++;
|
|---|
| 174 | continue;
|
|---|
| 175 | }
|
|---|
| 176 | if (value)
|
|---|
| 177 | G_warning(_("Value '%s' ignored when unsetting the GRASS variable"),
|
|---|
| 178 | value);
|
|---|
| 179 |
|
|---|
| 180 | G_getenv2(u_name, store); /* G_fatal_error() if not defined */
|
|---|
| 181 | G_unsetenv2(u_name, store);
|
|---|
| 182 | n++;
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | if (u_name)
|
|---|
| 187 | exit(EXIT_SUCCESS);
|
|---|
| 188 |
|
|---|
| 189 | /* Something's wrong if we got this far */
|
|---|
| 190 | G_usage();
|
|---|
| 191 |
|
|---|
| 192 | exit(EXIT_FAILURE);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | char *parse_variable(const char *v_name, char **value)
|
|---|
| 196 | {
|
|---|
| 197 | char *u_name; /* uppercase variable name */
|
|---|
| 198 | char *name, *ptr;
|
|---|
| 199 |
|
|---|
| 200 | name = G_store(v_name);
|
|---|
| 201 | if (value)
|
|---|
| 202 | *value = NULL;
|
|---|
| 203 |
|
|---|
| 204 | ptr = strchr(name, '=');
|
|---|
| 205 | if (ptr != NULL) {
|
|---|
| 206 | *ptr = '\0';
|
|---|
| 207 | if (value)
|
|---|
| 208 | *value = ptr + 1;
|
|---|
| 209 | }
|
|---|
| 210 | /* Allow unset without '=' sign */
|
|---|
| 211 | if (value) {
|
|---|
| 212 | if (*value != NULL && **value == '\0')
|
|---|
| 213 | *value = NULL;
|
|---|
| 214 | }
|
|---|
| 215 | if (strlen(name) < 1)
|
|---|
| 216 | G_fatal_error(_("GRASS variable not defined"));
|
|---|
| 217 |
|
|---|
| 218 | /* Check variable uppercase */
|
|---|
| 219 | u_name = G_store(name);
|
|---|
| 220 | G_str_to_upper(u_name);
|
|---|
| 221 | if (strcmp(name, u_name) != 0) {
|
|---|
| 222 | G_verbose_message(_("GRASS variable must be uppercase. Using '%s'."),
|
|---|
| 223 | u_name);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | return u_name;
|
|---|
| 227 | }
|
|---|