main.c 6.4 KB

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