main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, 2010-2011 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 <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <grass/glocale.h>
  22. #include "proto.h"
  23. int translate_output;
  24. int main(int argc, char *argv[])
  25. {
  26. struct context ctx;
  27. const char *filename;
  28. int standard_output;
  29. ctx.module = NULL;
  30. ctx.option = NULL;
  31. ctx.flag = NULL;
  32. ctx.first_option = NULL;
  33. ctx.first_flag = NULL;
  34. ctx.state = S_TOPLEVEL;
  35. standard_output = translate_output = FALSE;
  36. /* Detect request to get strings to translate from a file */
  37. /* It comes BEFORE the filename to completely avoid confusion with parser.c behaviours */
  38. if (argc >= 2 && (strcmp(argv[1], "-t") == 0)) {
  39. /* Turn on translation output */
  40. translate_output = TRUE;
  41. argv++, argc--;
  42. }
  43. if (argc >= 2 && (strcmp(argv[1], "-s") == 0)) {
  44. /* write to stdout rather than re-invoking */
  45. standard_output = TRUE;
  46. argv++, argc--;
  47. }
  48. if ((argc < 2) || ((strcmp(argv[1], "help") == 0) ||
  49. (strcmp(argv[1], "-help") == 0) ||
  50. (strcmp(argv[1], "--help") == 0))) {
  51. fprintf(stderr, "%s: %s [-t] [-s] <filename> [<argument> ...]\n",
  52. _("Usage"), argv[0]);
  53. exit(EXIT_FAILURE);
  54. }
  55. filename = argv[1];
  56. argv++, argc--;
  57. G_debug(2, "filename = %s", filename);
  58. ctx.fp = fopen(filename, "r");
  59. if (!ctx.fp) {
  60. perror(_("Unable to open script file"));
  61. exit(EXIT_FAILURE);
  62. }
  63. G_gisinit((char *)filename);
  64. for (ctx.line = 1;; ctx.line++) {
  65. char buff[4096];
  66. char *cmd, *arg;
  67. if (!fgets(buff, sizeof(buff), ctx.fp))
  68. break;
  69. arg = strchr(buff, '\n');
  70. if (!arg) {
  71. fprintf(stderr, _("Line too long or missing newline at line %d\n"),
  72. ctx.line);
  73. exit(EXIT_FAILURE);
  74. }
  75. *arg = '\0';
  76. if (buff[0] != '#' || buff[1] != '%')
  77. continue;
  78. cmd = buff + 2;
  79. G_chop(cmd);
  80. arg = strchr(cmd, ':');
  81. if (arg) {
  82. *(arg++) = '\0';
  83. G_strip(cmd);
  84. G_strip(arg);
  85. }
  86. switch (ctx.state) {
  87. case S_TOPLEVEL:
  88. parse_toplevel(&ctx, cmd);
  89. break;
  90. case S_MODULE:
  91. parse_module(&ctx, cmd, arg);
  92. break;
  93. case S_FLAG:
  94. parse_flag(&ctx, cmd, arg);
  95. break;
  96. case S_OPTION:
  97. parse_option(&ctx, cmd, arg);
  98. break;
  99. }
  100. }
  101. if (fclose(ctx.fp) != 0) {
  102. perror(_("Error closing script file"));
  103. exit(EXIT_FAILURE);
  104. }
  105. /* Stop here successfully if all that was desired was output of text to translate */
  106. /* Continuing from here would get argc and argv all wrong in G_parser. */
  107. if (translate_output)
  108. exit(EXIT_SUCCESS);
  109. if (G_parser(argc, argv))
  110. exit(EXIT_FAILURE);
  111. return standard_output
  112. ? print_options(&ctx)
  113. : reinvoke_script(&ctx, filename);
  114. }