parse.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include "proto.h"
  4. #include <grass/glocale.h>
  5. static char *xstrdup(const char *arg)
  6. {
  7. return (G_strcasecmp(arg, "{NULL}") == 0)
  8. ? NULL : strdup(arg);
  9. }
  10. int parse_boolean(struct context *ctx, const char *arg)
  11. {
  12. if (G_strcasecmp(arg, "yes") == 0)
  13. return YES;
  14. if (G_strcasecmp(arg, "no") == 0)
  15. return NO;
  16. fprintf(stderr, _("Unknown boolean value \"%s\" at line %d\n"),
  17. arg, ctx->line);
  18. return NO;
  19. }
  20. void parse_toplevel(struct context *ctx, const char *cmd)
  21. {
  22. char **tokens;
  23. if (G_strcasecmp(cmd, "module") == 0) {
  24. ctx->state = S_MODULE;
  25. ctx->module = G_define_module();
  26. return;
  27. }
  28. if (G_strcasecmp(cmd, "flag") == 0) {
  29. ctx->state = S_FLAG;
  30. ctx->flag = G_define_flag();
  31. if (!ctx->first_flag)
  32. ctx->first_flag = ctx->flag;
  33. return;
  34. }
  35. if (G_strncasecmp(cmd, "option", strlen("option")) == 0) {
  36. ctx->state = S_OPTION;
  37. tokens = G_tokenize(cmd, " ");
  38. if (G_number_of_tokens(tokens) > 1) {
  39. /* standard option */
  40. ctx->option = define_standard_option(tokens[1]);
  41. }
  42. else {
  43. ctx->option = G_define_option();
  44. }
  45. if (!ctx->first_option)
  46. ctx->first_option = ctx->option;
  47. G_free_tokens(tokens);
  48. return;
  49. }
  50. if (G_strcasecmp(cmd, "rules") == 0) {
  51. ctx->state = S_RULES;
  52. return;
  53. }
  54. fprintf(stderr, _("Unknown command \"%s\" at line %d\n"), cmd, ctx->line);
  55. }
  56. void parse_module(struct context *ctx, const char *cmd, const char *arg)
  57. {
  58. /* Label and description can be internationalized */
  59. if (G_strcasecmp(cmd, "label") == 0) {
  60. ctx->module->label = translate(xstrdup(arg));
  61. return;
  62. }
  63. if (G_strcasecmp(cmd, "description") == 0) {
  64. ctx->module->description = translate(xstrdup(arg));
  65. return;
  66. }
  67. if (G_strcasecmp(cmd, "keyword") == 0 || G_strcasecmp(cmd, "keywords") == 0) {
  68. /* (still) accept 'keywords' for backward compatibility */
  69. G_add_keyword(translate(xstrdup(arg)));
  70. return;
  71. }
  72. if (G_strcasecmp(cmd, "overwrite") == 0) {
  73. ctx->module->overwrite = parse_boolean(ctx, arg);
  74. return;
  75. }
  76. if (G_strcasecmp(cmd, "end") == 0) {
  77. ctx->state = S_TOPLEVEL;
  78. return;
  79. }
  80. fprintf(stderr, _("Unknown module parameter \"%s\" at line %d\n"),
  81. cmd, ctx->line);
  82. }
  83. void parse_flag(struct context *ctx, const char *cmd, const char *arg)
  84. {
  85. if (G_strcasecmp(cmd, "key") == 0) {
  86. ctx->flag->key = arg[0];
  87. return;
  88. }
  89. if (G_strcasecmp(cmd, "suppress_required") == 0) {
  90. ctx->flag->suppress_required = parse_boolean(ctx, arg);
  91. return;
  92. }
  93. /* Label, description, and guisection can all be internationalized */
  94. if (G_strcasecmp(cmd, "label") == 0) {
  95. ctx->flag->label = translate(xstrdup(arg));
  96. return;
  97. }
  98. if (G_strcasecmp(cmd, "description") == 0) {
  99. ctx->flag->description = translate(xstrdup(arg));
  100. return;
  101. }
  102. if (G_strcasecmp(cmd, "guisection") == 0) {
  103. ctx->flag->guisection = translate(xstrdup(arg));
  104. return;
  105. }
  106. if (G_strcasecmp(cmd, "end") == 0) {
  107. ctx->state = S_TOPLEVEL;
  108. return;
  109. }
  110. fprintf(stderr, _("Unknown flag parameter \"%s\" at line %d\n"),
  111. cmd, ctx->line);
  112. }
  113. int parse_type(struct context *ctx, const char *arg)
  114. {
  115. if (G_strcasecmp(arg, "integer") == 0)
  116. return TYPE_INTEGER;
  117. if (G_strcasecmp(arg, "double") == 0)
  118. return TYPE_DOUBLE;
  119. if (G_strcasecmp(arg, "string") == 0)
  120. return TYPE_STRING;
  121. fprintf(stderr, _("Unknown type \"%s\" at line %d\n"), arg, ctx->line);
  122. return TYPE_STRING;
  123. }
  124. void parse_option(struct context *ctx, const char *cmd, const char *arg)
  125. {
  126. if (G_strcasecmp(cmd, "key") == 0) {
  127. ctx->option->key = xstrdup(arg);
  128. return;
  129. }
  130. if (G_strcasecmp(cmd, "type") == 0) {
  131. ctx->option->type = parse_type(ctx, arg);
  132. return;
  133. }
  134. if (G_strcasecmp(cmd, "required") == 0) {
  135. ctx->option->required = parse_boolean(ctx, arg);
  136. return;
  137. }
  138. if (G_strcasecmp(cmd, "multiple") == 0) {
  139. ctx->option->multiple = parse_boolean(ctx, arg);
  140. return;
  141. }
  142. if (G_strcasecmp(cmd, "options") == 0) {
  143. ctx->option->options = xstrdup(arg);
  144. return;
  145. }
  146. if (G_strcasecmp(cmd, "key_desc") == 0) {
  147. ctx->option->key_desc = xstrdup(arg);
  148. return;
  149. }
  150. /* Label, description, descriptions, and guisection can all be internationalized */
  151. if (G_strcasecmp(cmd, "label") == 0) {
  152. ctx->option->label = translate(xstrdup(arg));
  153. return;
  154. }
  155. if (G_strcasecmp(cmd, "description") == 0) {
  156. ctx->option->description = translate(xstrdup(arg));
  157. return;
  158. }
  159. if (G_strcasecmp(cmd, "descriptions") == 0) {
  160. ctx->option->descriptions = translate(xstrdup(arg));
  161. return;
  162. }
  163. if (G_strcasecmp(cmd, "answer") == 0) {
  164. ctx->option->answer = xstrdup(arg);
  165. return;
  166. }
  167. if (G_strcasecmp(cmd, "gisprompt") == 0) {
  168. ctx->option->gisprompt = xstrdup(arg);
  169. return;
  170. }
  171. if (G_strcasecmp(cmd, "guisection") == 0) {
  172. ctx->option->guisection = translate(xstrdup(arg));
  173. return;
  174. }
  175. if (G_strcasecmp(cmd, "guidependency") == 0) {
  176. ctx->option->guidependency = translate(xstrdup(arg));
  177. return;
  178. }
  179. if (G_strcasecmp(cmd, "end") == 0) {
  180. ctx->state = S_TOPLEVEL;
  181. return;
  182. }
  183. fprintf(stderr, _("Unknown option parameter \"%s\" at line %d\n"),
  184. cmd, ctx->line);
  185. }
  186. int print_options(const struct context *ctx, int sep)
  187. {
  188. struct Option *option;
  189. struct Flag *flag;
  190. const char *overwrite = getenv("GRASS_OVERWRITE");
  191. const char *verbose = getenv("GRASS_VERBOSE");
  192. printf("@ARGS_PARSED@%c", sep);
  193. if (overwrite)
  194. printf("GRASS_OVERWRITE=%s%c", overwrite, sep);
  195. if (verbose)
  196. printf("GRASS_VERBOSE=%s%c", verbose, sep);
  197. for (flag = ctx->first_flag; flag; flag = flag->next_flag)
  198. printf("flag_%c=%d%c", flag->key, flag->answer ? 1 : 0, sep);
  199. for (option = ctx->first_option; option; option = option->next_opt)
  200. printf("opt_%s=%s%c", option->key,
  201. option->answer ? option->answer : "", sep);
  202. fflush(stdout);
  203. return EXIT_SUCCESS;
  204. }