main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /****************************************************************************
  2. *
  3. * MODULE: g.parser
  4. * AUTHOR(S): Glynn Clements <glynn gclements.plus.com> (original contributor)
  5. * Bernhard Reiter <bernhard intevation.de>,
  6. * Cedric Shock <cedricgrass shockfamily.net>,
  7. * Hamish Bowman <hamish_b yahoo.com>,
  8. * Paul Kelly <paul-grass stjohnspoint.co.uk>,
  9. * Radim Blazek <radim.blazek gmail.com>
  10. * PURPOSE:
  11. * COPYRIGHT: (C) 2001-2007 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 <ctype.h>
  23. #include <grass/gis.h>
  24. #include <grass/glocale.h>
  25. enum state
  26. {
  27. S_TOPLEVEL,
  28. S_MODULE,
  29. S_FLAG,
  30. S_OPTION
  31. };
  32. struct context
  33. {
  34. struct GModule *module;
  35. struct Option *option;
  36. struct Flag *flag;
  37. struct Option *first_option;
  38. struct Flag *first_flag;
  39. int state;
  40. FILE *fp;
  41. int line;
  42. };
  43. int translate_output = 0;
  44. /* Returns translated version of a string.
  45. If global variable to output strings for translation is set it spits them out */
  46. char *translate(const char *arg)
  47. {
  48. static const char *domain;
  49. if (*arg && translate_output) {
  50. fputs(arg, stdout);
  51. fputs("\n", stdout);
  52. }
  53. #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
  54. if (!domain) {
  55. domain = getenv("GRASS_TRANSLATION_DOMAIN");
  56. if (domain)
  57. G_putenv("GRASS_TRANSLATION_DOMAIN", "grassmods");
  58. else
  59. domain = PACKAGE;
  60. }
  61. return G_gettext(domain, arg);
  62. #else
  63. return arg;
  64. #endif
  65. }
  66. static void parse_toplevel(struct context *ctx, const char *cmd)
  67. {
  68. if (strcasecmp(cmd, "module") == 0) {
  69. ctx->state = S_MODULE;
  70. ctx->module = G_define_module();
  71. return;
  72. }
  73. if (strcasecmp(cmd, "flag") == 0) {
  74. ctx->state = S_FLAG;
  75. ctx->flag = G_define_flag();
  76. if (!ctx->first_flag)
  77. ctx->first_flag = ctx->flag;
  78. return;
  79. }
  80. if (strcasecmp(cmd, "option") == 0) {
  81. ctx->state = S_OPTION;
  82. ctx->option = G_define_option();
  83. if (!ctx->first_option)
  84. ctx->first_option = ctx->option;
  85. return;
  86. }
  87. fprintf(stderr, "Unknown command \"%s\" at line %d\n", cmd, ctx->line);
  88. }
  89. static void parse_module(struct context *ctx, const char *cmd,
  90. const char *arg)
  91. {
  92. /* Label and description can be internationalized */
  93. if (strcasecmp(cmd, "label") == 0) {
  94. ctx->module->label = translate(strdup(arg));
  95. return;
  96. }
  97. if (strcasecmp(cmd, "description") == 0) {
  98. ctx->module->description = translate(strdup(arg));
  99. return;
  100. }
  101. if (strcasecmp(cmd, "keywords") == 0) {
  102. G_add_keyword(translate(strdup(arg)));
  103. return;
  104. }
  105. if (strcasecmp(cmd, "end") == 0) {
  106. ctx->state = S_TOPLEVEL;
  107. return;
  108. }
  109. fprintf(stderr, "Unknown module parameter \"%s\" at line %d\n",
  110. cmd, ctx->line);
  111. }
  112. static void parse_flag(struct context *ctx, const char *cmd, const char *arg)
  113. {
  114. if (strcasecmp(cmd, "key") == 0) {
  115. ctx->flag->key = arg[0];
  116. return;
  117. }
  118. if (strcasecmp(cmd, "answer") == 0) {
  119. ctx->flag->answer = atoi(arg);
  120. return;
  121. }
  122. /* Label, description, and guisection can all be internationalized */
  123. if (strcasecmp(cmd, "label") == 0) {
  124. ctx->flag->label = translate(strdup(arg));
  125. return;
  126. }
  127. if (strcasecmp(cmd, "description") == 0) {
  128. ctx->flag->description = translate(strdup(arg));
  129. return;
  130. }
  131. if (strcasecmp(cmd, "guisection") == 0) {
  132. ctx->flag->guisection = translate(strdup(arg));
  133. return;
  134. }
  135. if (strcasecmp(cmd, "end") == 0) {
  136. ctx->state = S_TOPLEVEL;
  137. return;
  138. }
  139. fprintf(stderr, "Unknown flag parameter \"%s\" at line %d\n",
  140. cmd, ctx->line);
  141. }
  142. static int parse_type(struct context *ctx, const char *arg)
  143. {
  144. if (strcasecmp(arg, "integer") == 0)
  145. return TYPE_INTEGER;
  146. if (strcasecmp(arg, "double") == 0)
  147. return TYPE_DOUBLE;
  148. if (strcasecmp(arg, "string") == 0)
  149. return TYPE_STRING;
  150. fprintf(stderr, "Unknown type \"%s\" at line %d\n", arg, ctx->line);
  151. return TYPE_STRING;
  152. }
  153. static int parse_boolean(struct context *ctx, const char *arg)
  154. {
  155. if (strcasecmp(arg, "yes") == 0)
  156. return YES;
  157. if (strcasecmp(arg, "no") == 0)
  158. return NO;
  159. fprintf(stderr, "Unknown boolean value \"%s\" at line %d\n",
  160. arg, ctx->line);
  161. return NO;
  162. }
  163. static void parse_option(struct context *ctx, const char *cmd,
  164. const char *arg)
  165. {
  166. if (strcasecmp(cmd, "key") == 0) {
  167. ctx->option->key = strdup(arg);
  168. return;
  169. }
  170. if (strcasecmp(cmd, "type") == 0) {
  171. ctx->option->type = parse_type(ctx, arg);
  172. return;
  173. }
  174. if (strcasecmp(cmd, "required") == 0) {
  175. ctx->option->required = parse_boolean(ctx, arg);
  176. return;
  177. }
  178. if (strcasecmp(cmd, "multiple") == 0) {
  179. ctx->option->multiple = parse_boolean(ctx, arg);
  180. return;
  181. }
  182. if (strcasecmp(cmd, "options") == 0) {
  183. ctx->option->options = strdup(arg);
  184. return;
  185. }
  186. if (strcasecmp(cmd, "key_desc") == 0) {
  187. ctx->option->key_desc = strdup(arg);
  188. return;
  189. }
  190. /* Label, description, descriptions, and guisection can all be internationalized */
  191. if (strcasecmp(cmd, "label") == 0) {
  192. ctx->option->label = translate(strdup(arg));
  193. return;
  194. }
  195. if (strcasecmp(cmd, "description") == 0) {
  196. ctx->option->description = translate(strdup(arg));
  197. return;
  198. }
  199. if (strcasecmp(cmd, "descriptions") == 0) {
  200. ctx->option->descriptions = translate(strdup(arg));
  201. return;
  202. }
  203. if (strcasecmp(cmd, "answer") == 0) {
  204. ctx->option->answer = strdup(arg);
  205. return;
  206. }
  207. if (strcasecmp(cmd, "gisprompt") == 0) {
  208. ctx->option->gisprompt = strdup(arg);
  209. return;
  210. }
  211. if (strcasecmp(cmd, "guisection") == 0) {
  212. ctx->option->guisection = translate(strdup(arg));
  213. return;
  214. }
  215. if (strcasecmp(cmd, "guidependency") == 0) {
  216. ctx->option->guidependency = translate(strdup(arg));
  217. return;
  218. }
  219. if (strcasecmp(cmd, "end") == 0) {
  220. ctx->state = S_TOPLEVEL;
  221. return;
  222. }
  223. fprintf(stderr, "Unknown option parameter \"%s\" at line %d\n",
  224. cmd, ctx->line);
  225. }
  226. static int print_options(const struct context *ctx)
  227. {
  228. struct Option *option;
  229. struct Flag *flag;
  230. const char *overwrite = getenv("GRASS_OVERWRITE");
  231. const char *verbose = getenv("GRASS_VERBOSE");
  232. printf("@ARGS_PARSED@\n");
  233. if (overwrite)
  234. printf("GRASS_OVERWRITE=%s\n", overwrite);
  235. if (verbose)
  236. printf("GRASS_VERBOSE=%s\n", verbose);
  237. for (flag = ctx->first_flag; flag; flag = flag->next_flag)
  238. printf("flag_%c=%d\n", flag->key, flag->answer ? 1 : 0);
  239. for (option = ctx->first_option; option; option = option->next_opt)
  240. printf("opt_%s=%s\n", option->key,
  241. option->answer ? option->answer : "");
  242. return 0;
  243. }
  244. static int reinvoke_script(const struct context *ctx, const char *filename)
  245. {
  246. struct Option *option;
  247. struct Flag *flag;
  248. /* Because shell from MINGW and CygWin converts all variables
  249. * to uppercase it was necessary to use uppercase variables.
  250. * Set both until all scripts are updated */
  251. for (flag = ctx->first_flag; flag; flag = flag->next_flag) {
  252. char buff[16];
  253. sprintf(buff, "GIS_FLAG_%c=%d", flag->key, flag->answer ? 1 : 0);
  254. putenv(G_store(buff));
  255. sprintf(buff, "GIS_FLAG_%c=%d", toupper(flag->key),
  256. flag->answer ? 1 : 0);
  257. G_debug(2, "set %s", buff);
  258. putenv(G_store(buff));
  259. }
  260. for (option = ctx->first_option; option; option = option->next_opt) {
  261. char upper[4096];
  262. char *str;
  263. G_asprintf(&str, "GIS_OPT_%s=%s", option->key,
  264. option->answer ? option->answer : "");
  265. putenv(str);
  266. strcpy(upper, option->key);
  267. G_str_to_upper(upper);
  268. G_asprintf(&str, "GIS_OPT_%s=%s", upper,
  269. option->answer ? option->answer : "");
  270. G_debug(2, "set %s", str);
  271. putenv(str);
  272. }
  273. #ifdef __MINGW32__
  274. {
  275. /* execlp() and _spawnlp ( _P_OVERLAY,..) do not work, they return
  276. * immediately and that breaks scripts running GRASS scripts
  277. * because they dont wait until GRASS script finished */
  278. /* execlp( "sh", "sh", filename, "@ARGS_PARSED@", NULL); */
  279. /* _spawnlp ( _P_OVERLAY, filename, filename, "@ARGS_PARSED@", NULL ); */
  280. int ret;
  281. char *shell = getenv("GRASS_SH");
  282. if (shell == NULL)
  283. shell = "sh";
  284. ret = G_spawn(shell, shell, filename, "@ARGS_PARSED@", NULL);
  285. G_debug(1, "ret = %d", ret);
  286. if (ret == -1) {
  287. perror("G_spawn() failed");
  288. return 1;
  289. }
  290. return ret;
  291. }
  292. #else
  293. execl(filename, filename, "@ARGS_PARSED@", NULL);
  294. perror("execl() failed");
  295. return 1;
  296. #endif
  297. }
  298. int main(int argc, char *argv[])
  299. {
  300. struct context ctx;
  301. const char *filename;
  302. int standard_output = 0;
  303. ctx.module = NULL;
  304. ctx.option = NULL;
  305. ctx.flag = NULL;
  306. ctx.first_option = NULL;
  307. ctx.first_flag = NULL;
  308. ctx.state = S_TOPLEVEL;
  309. /* Detect request to get strings to translate from a file */
  310. /* It comes BEFORE the filename to completely avoid confusion with parser.c behaviours */
  311. if (argc >= 2 && (strcmp(argv[1], "-t") == 0)) {
  312. /* Turn on translation output */
  313. translate_output = 1;
  314. argv++, argc--;
  315. }
  316. if (argc >= 2 && (strcmp(argv[1], "-s") == 0)) {
  317. /* write to stdout rather than re-invoking */
  318. standard_output = 1;
  319. argv++, argc--;
  320. }
  321. if ((argc < 2) || ((strcmp(argv[1], "help") == 0) ||
  322. (strcmp(argv[1], "-help") == 0) ||
  323. (strcmp(argv[1], "--help") == 0))) {
  324. fprintf(stderr, "Usage: %s [-t] [-s] <filename> [<argument> ...]\n",
  325. argv[0]);
  326. return 1;
  327. }
  328. filename = argv[1];
  329. argv++, argc--;
  330. G_debug(2, "filename = %s", filename);
  331. ctx.fp = fopen(filename, "r");
  332. if (!ctx.fp) {
  333. perror("Unable to open script file");
  334. return 1;
  335. }
  336. G_gisinit((char *)filename);
  337. for (ctx.line = 1;; ctx.line++) {
  338. char buff[4096];
  339. char *cmd, *arg;
  340. if (!fgets(buff, sizeof(buff), ctx.fp))
  341. break;
  342. arg = strchr(buff, '\n');
  343. if (!arg) {
  344. fprintf(stderr, "Line too long or missing newline at line %d\n",
  345. ctx.line);
  346. return 1;
  347. }
  348. *arg = '\0';
  349. if (buff[0] != '#' || buff[1] != '%')
  350. continue;
  351. cmd = buff + 2;
  352. G_chop(cmd);
  353. arg = strchr(cmd, ':');
  354. if (arg) {
  355. *(arg++) = '\0';
  356. G_strip(cmd);
  357. G_strip(arg);
  358. }
  359. switch (ctx.state) {
  360. case S_TOPLEVEL:
  361. parse_toplevel(&ctx, cmd);
  362. break;
  363. case S_MODULE:
  364. parse_module(&ctx, cmd, arg);
  365. break;
  366. case S_FLAG:
  367. parse_flag(&ctx, cmd, arg);
  368. break;
  369. case S_OPTION:
  370. parse_option(&ctx, cmd, arg);
  371. break;
  372. }
  373. }
  374. if (fclose(ctx.fp) != 0) {
  375. perror("Error closing script file");
  376. return 1;
  377. }
  378. /* Stop here successfully if all that was desired was output of text to translate */
  379. /* Continuing from here would get argc and argv all wrong in G_parser. */
  380. if (translate_output)
  381. return EXIT_SUCCESS;
  382. if (G_parser(argc, argv))
  383. return 1;
  384. return standard_output
  385. ? print_options(&ctx)
  386. : reinvoke_script(&ctx, filename);
  387. }