rules.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <string.h>
  2. #include "proto.h"
  3. #include <grass/glocale.h>
  4. static void *find_flag(const struct context *ctx, int key)
  5. {
  6. struct Flag *flag;
  7. for (flag = ctx->first_flag; flag; flag = flag->next_flag)
  8. if (flag->key == key)
  9. return flag;
  10. fprintf(stderr, _("Unknown flag \"-%c\" in rule\n"), key);
  11. return NULL;
  12. }
  13. static void *find_option(const struct context *ctx, const char *key)
  14. {
  15. struct Option *option;
  16. for (option = ctx->first_option; option; option = option->next_opt)
  17. if (G_strcasecmp(option->key, key) == 0)
  18. return option;
  19. fprintf(stderr, _("Unknown option \"%s\" in rule\n"), key);
  20. return NULL;
  21. }
  22. static void add_rule(struct context *ctx, int type, const char *data)
  23. {
  24. char **tokens;
  25. int ntokens;
  26. void **opts;
  27. int i;
  28. tokens = G_tokenize(data, ",");
  29. ntokens = G_number_of_tokens(tokens);
  30. opts = G_malloc(ntokens * sizeof(void *));
  31. for (i = 0; i < ntokens; i++) {
  32. char buf[256];
  33. char *name;
  34. strcpy(buf, tokens[i]);
  35. name = G_chop(buf);
  36. opts[i] = (name[0] == '-')
  37. ? find_flag(ctx, name[1])
  38. : find_option(ctx, name);
  39. }
  40. G_free_tokens(tokens);
  41. G_option_rule(type, ntokens, opts);
  42. }
  43. void parse_rule(struct context *ctx, const char *cmd, const char *arg)
  44. {
  45. if (G_strcasecmp(cmd, "exclusive") == 0) {
  46. add_rule(ctx, RULE_EXCLUSIVE, arg);
  47. return;
  48. }
  49. if (G_strcasecmp(cmd, "required") == 0) {
  50. add_rule(ctx, RULE_REQUIRED, arg);
  51. return;
  52. }
  53. if (G_strcasecmp(cmd, "requires") == 0) {
  54. add_rule(ctx, RULE_REQUIRES, arg);
  55. return;
  56. }
  57. if (G_strcasecmp(cmd, "requires_all") == 0) {
  58. add_rule(ctx, RULE_REQUIRES_ALL, arg);
  59. return;
  60. }
  61. if (G_strcasecmp(cmd, "excludes") == 0) {
  62. add_rule(ctx, RULE_EXCLUDES, arg);
  63. return;
  64. }
  65. if (G_strcasecmp(cmd, "collective") == 0) {
  66. add_rule(ctx, RULE_COLLECTIVE, arg);
  67. return;
  68. }
  69. if (G_strcasecmp(cmd, "end") == 0) {
  70. ctx->state = S_TOPLEVEL;
  71. return;
  72. }
  73. fprintf(stderr, _("Unknown rule type \"%s\" at line %d\n"),
  74. cmd, ctx->line);
  75. }