main.c 10 KB

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