main.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. ctx->module->keywords = 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, "end") == 0) {
  204. ctx->state = S_TOPLEVEL;
  205. return;
  206. }
  207. fprintf(stderr, "Unknown option parameter \"%s\" at line %d\n",
  208. cmd, ctx->line);
  209. }
  210. int main(int argc, char *argv[])
  211. {
  212. struct context ctx;
  213. struct Option *option;
  214. struct Flag *flag;
  215. const char *filename;
  216. ctx.module = NULL;
  217. ctx.option = NULL;
  218. ctx.flag = NULL;
  219. ctx.first_option = NULL;
  220. ctx.first_flag = NULL;
  221. ctx.state = S_TOPLEVEL;
  222. /* Detect request to get strings to translate from a file */
  223. /* It comes BEFORE the filename to completely avoid confusion with parser.c behaviours */
  224. if (argc >= 2 && (strcmp(argv[1], "-t") == 0)) {
  225. /* Turn on translation output */
  226. translate_output = 1;
  227. }
  228. if ((argc < 2 + translate_output) || (argc >= 2 && ((strcmp
  229. (argv[1],
  230. "help") == 0) ||
  231. (strcmp
  232. (argv[1],
  233. "-help") == 0) ||
  234. (strcmp
  235. (argv[1],
  236. "--help") == 0)))) {
  237. fprintf(stderr, "Usage: %s [-t] <filename> [<argument> ...]\n",
  238. argv[0]);
  239. return 1;
  240. }
  241. filename = argv[1 + translate_output];
  242. G_debug(2, "filename = %s", filename);
  243. ctx.fp = fopen(filename, "r");
  244. if (!ctx.fp) {
  245. perror("Unable to open script file");
  246. return 1;
  247. }
  248. G_gisinit((char *)filename);
  249. for (ctx.line = 1;; ctx.line++) {
  250. char buff[4096];
  251. char *cmd, *arg;
  252. if (!fgets(buff, sizeof(buff), ctx.fp))
  253. break;
  254. arg = strchr(buff, '\n');
  255. if (!arg) {
  256. fprintf(stderr, "Line too long or missing newline at line %d\n",
  257. ctx.line);
  258. return 1;
  259. }
  260. *arg = '\0';
  261. if (buff[0] != '#' || buff[1] != '%')
  262. continue;
  263. cmd = buff + 2;
  264. G_chop(cmd);
  265. arg = strchr(cmd, ':');
  266. if (arg) {
  267. *(arg++) = '\0';
  268. G_strip(cmd);
  269. G_strip(arg);
  270. }
  271. switch (ctx.state) {
  272. case S_TOPLEVEL:
  273. parse_toplevel(&ctx, cmd);
  274. break;
  275. case S_MODULE:
  276. parse_module(&ctx, cmd, arg);
  277. break;
  278. case S_FLAG:
  279. parse_flag(&ctx, cmd, arg);
  280. break;
  281. case S_OPTION:
  282. parse_option(&ctx, cmd, arg);
  283. break;
  284. }
  285. }
  286. if (fclose(ctx.fp) != 0) {
  287. perror("Error closing script file");
  288. return 1;
  289. }
  290. /* Stop here successfully if all that was desired was output of text to translate */
  291. /* Continuing from here would get argc and argv all wrong in G_parser. */
  292. if (translate_output)
  293. return EXIT_SUCCESS;
  294. if (G_parser(argc - 1, argv + 1) < 0)
  295. return 1;
  296. /* Because shell from MINGW and CygWin converts all variables
  297. * to uppercase it was necessary to use uppercase variables.
  298. * Set both until all scripts are updated */
  299. for (flag = ctx.first_flag; flag; flag = flag->next_flag) {
  300. char buff[12];
  301. sprintf(buff, "GIS_FLAG_%c=%d", flag->key, flag->answer ? 1 : 0);
  302. putenv(G_store(buff));
  303. sprintf(buff, "GIS_FLAG_%c=%d", toupper(flag->key),
  304. flag->answer ? 1 : 0);
  305. G_debug(2, "set %s", buff);
  306. putenv(G_store(buff));
  307. }
  308. for (option = ctx.first_option; option; option = option->next_opt) {
  309. char buff[1024], upper[1024];
  310. sprintf(buff, "GIS_OPT_%s=%s", option->key,
  311. option->answer ? option->answer : "");
  312. putenv(G_store(buff));
  313. strcpy(upper, option->key);
  314. G_str_to_upper(upper);
  315. sprintf(buff, "GIS_OPT_%s=%s", upper,
  316. option->answer ? option->answer : "");
  317. G_debug(2, "set %s", buff);
  318. putenv(G_store(buff));
  319. }
  320. #ifdef __MINGW32__
  321. {
  322. /* execlp() and _spawnlp ( _P_OVERLAY,..) do not work, they return
  323. * immediately and that breaks scripts running GRASS scripts
  324. * because they dont wait until GRASS script finished */
  325. /* execlp( "sh", "sh", filename, "@ARGS_PARSED@", NULL); */
  326. /* _spawnlp ( _P_OVERLAY, "sh", "sh", filename, "@ARGS_PARSED@", NULL ); */
  327. int ret;
  328. char *shell = getenv("GRASS_SH");
  329. if (shell == NULL)
  330. shell = "sh";
  331. ret =
  332. _spawnlp(_P_WAIT, shell, shell, filename, "@ARGS_PARSED@", NULL);
  333. G_debug(1, "ret = %d", ret);
  334. if (ret == -1) {
  335. perror("_spawnlp() failed");
  336. return 1;
  337. }
  338. return ret;
  339. }
  340. #else
  341. execl(filename, filename, "@ARGS_PARSED@", NULL);
  342. perror("execl() failed");
  343. return 1;
  344. #endif
  345. }