rules-debug.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * Author......: Jens Steube <jens.steube@gmail.com>
  3. * License.....: MIT
  4. */
  5. #include "common.h"
  6. #include "rp.h"
  7. #define INCR_RULES_PTR 1
  8. int compare_string (const void *p1, const void *p2)
  9. {
  10. const char *s1 = (const char *) p1;
  11. const char *s2 = (const char *) p2;
  12. return strcmp (s1, s2);
  13. }
  14. void incr_rules_buf (rules_t *rules)
  15. {
  16. if (rules->rules_cnt == rules->rules_avail)
  17. {
  18. rules->rules_avail += INCR_RULES_PTR;
  19. rules->rules_buf = myrealloc (rules->rules_buf, rules->rules_avail * sizeof (char *));
  20. rules->rules_len = myrealloc (rules->rules_len, rules->rules_avail * sizeof (uint32_t));
  21. }
  22. }
  23. int fgetl (FILE *fp, char *line_buf)
  24. {
  25. if (fgets (line_buf, BLOCK_SIZE, fp) == NULL) return (-1);
  26. int line_len = strlen (line_buf);
  27. if (line_buf[line_len - 1] == '\n')
  28. {
  29. line_len--;
  30. line_buf[line_len] = '\0';
  31. }
  32. if (line_buf[line_len - 1] == '\r')
  33. {
  34. line_len--;
  35. line_buf[line_len] = '\0';
  36. }
  37. return (line_len);
  38. }
  39. void *root = NULL;
  40. int add_rule (char *rule_buf, int rule_len, rules_t *rules)
  41. {
  42. if (tfind (rule_buf, &root, compare_string) != NULL) return (-3);
  43. char in[BLOCK_SIZE];
  44. char out[BLOCK_SIZE];
  45. memset (in, 0, BLOCK_SIZE);
  46. memset (out, 0, BLOCK_SIZE);
  47. int result = apply_rule (rule_buf, rule_len, in, 1, out);
  48. if (result == -1) return (-1);
  49. char *next_rule = mystrdup (rule_buf);
  50. tsearch (next_rule, &root, compare_string);
  51. incr_rules_buf (rules);
  52. rules->rules_buf[rules->rules_cnt] = next_rule;
  53. rules->rules_len[rules->rules_cnt] = rule_len;
  54. rules->rules_cnt++;
  55. return (0);
  56. }
  57. int main (int argc, char *argv[])
  58. {
  59. if (argc != 2)
  60. {
  61. fprintf (stderr, "usage: %s rules-file\n", argv[0]);
  62. return (-1);
  63. }
  64. rules_t *rules = malloc (sizeof (rules_t));
  65. memset (rules, 0, sizeof (rules_t));
  66. char *file_rules;
  67. if ((file_rules = argv[1]) != NULL)
  68. {
  69. FILE *fp;
  70. if ((fp = fopen (file_rules, "rb")) != NULL)
  71. {
  72. char rule_buf[RP_RULE_BUFSIZ];
  73. int rule_len;
  74. while ((rule_len = fgetl (fp, rule_buf)) != -1)
  75. {
  76. if (rule_len == 0) continue;
  77. if (rule_buf[0] == '#') continue;
  78. int rc = add_rule (rule_buf, rule_len, rules);
  79. if (rc == 0)
  80. {
  81. /* all ok */
  82. }
  83. else if (rc == -1)
  84. {
  85. fprintf (stderr, "Skipping rule: %s (syntax error)\n", rule_buf);
  86. }
  87. else if (rc == -3)
  88. {
  89. fprintf (stderr, "Skipping rule: %s (duplicate rule)\n", rule_buf);
  90. }
  91. else if (rc == -4)
  92. {
  93. fprintf (stderr, "Skipping rule: %s (duplicate result)\n", rule_buf);
  94. }
  95. }
  96. fclose (fp);
  97. }
  98. else
  99. {
  100. fprintf (stderr, "%s: %s\n", file_rules, strerror (errno));
  101. free (rules);
  102. return (-1);
  103. }
  104. }
  105. char word_buf[BLOCK_SIZE];
  106. int word_len;
  107. while ((word_len = fgetl (stdin, word_buf)) != -1)
  108. {
  109. uint32_t rules_idx;
  110. for (rules_idx = 0; rules_idx < rules->rules_cnt; rules_idx++)
  111. {
  112. char next_buf[BLOCK_SIZE];
  113. int next_len = apply_rule (rules->rules_buf[rules_idx], rules->rules_len[rules_idx], word_buf, word_len, next_buf);
  114. if (next_len < 0) continue;
  115. puts (next_buf);
  116. }
  117. }
  118. return 0;
  119. }