makeres.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. static char lower_resps[1024] = "";
  6. char *strtoupper(char *string)
  7. {
  8. static char ret[51] = "";
  9. int i = 0;
  10. while (*string) {
  11. ret[i] = toupper(*string);
  12. i++;
  13. string++;
  14. ret[i] = 0;
  15. }
  16. return ret;
  17. }
  18. char *replace(char *string, char *oldie, char *newbie)
  19. {
  20. static char newstring[1024] = "";
  21. int str_index, newstr_index, oldie_index, end, new_len, old_len, cpy_len;
  22. char *c = NULL;
  23. if (string == NULL) return "";
  24. if ((c = (char *) strstr(string, oldie)) == NULL) return string;
  25. new_len = strlen(newbie);
  26. old_len = strlen(oldie);
  27. end = strlen(string) - old_len;
  28. oldie_index = c - string;
  29. newstr_index = 0;
  30. str_index = 0;
  31. while(str_index <= end && c != NULL) {
  32. cpy_len = oldie_index-str_index;
  33. strncpy(newstring + newstr_index, string + str_index, cpy_len);
  34. newstr_index += cpy_len;
  35. str_index += cpy_len;
  36. strcpy(newstring + newstr_index, newbie);
  37. newstr_index += new_len;
  38. str_index += old_len;
  39. if((c = (char *) strstr(string + str_index, oldie)) != NULL)
  40. oldie_index = c - string;
  41. }
  42. strcpy(newstring + newstr_index, string + str_index);
  43. return (newstring);
  44. }
  45. char *step_thru_file(FILE *fd)
  46. {
  47. char tempBuf[1024] = "";
  48. char *retStr = NULL;
  49. if (fd == NULL) {
  50. return NULL;
  51. }
  52. retStr = NULL;
  53. while (!feof(fd)) {
  54. fgets(tempBuf, sizeof(tempBuf), fd);
  55. if (!feof(fd)) {
  56. if (retStr == NULL) {
  57. retStr = (char *) calloc(1, strlen(tempBuf) + 2);
  58. strcpy(retStr, tempBuf);
  59. } else {
  60. retStr = (char *) realloc(retStr, strlen(retStr) + strlen(tempBuf));
  61. strcat(retStr, tempBuf);
  62. }
  63. if (retStr[strlen(retStr)-1] == '\n') {
  64. retStr[strlen(retStr)-1] = 0;
  65. break;
  66. }
  67. }
  68. }
  69. return retStr;
  70. }
  71. char *newsplit(char **rest)
  72. {
  73. register char *o, *r;
  74. if (!rest)
  75. return *rest = "";
  76. o = *rest;
  77. while (*o == ' ')
  78. o++;
  79. r = o;
  80. while (*o && (*o != ' '))
  81. o++;
  82. if (*o)
  83. *o++ = 0;
  84. *rest = o;
  85. return r;
  86. }
  87. int skipline (char *line, int *skip) {
  88. static int multi = 0;
  89. if ((!strncmp(line, "//", 2))) {
  90. (*skip)++;
  91. } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
  92. multi = 0;
  93. (*skip)++;
  94. } else if ( (strstr(line, "/*")) ) {
  95. (*skip)++;
  96. multi = 1;
  97. } else if ( (strstr(line, "*/")) ) {
  98. multi = 0;
  99. } else {
  100. if (!multi) (*skip) = 0;
  101. }
  102. return (*skip);
  103. }
  104. int parse_res(char *in, char *out, char *outs) {
  105. FILE *inf = NULL, *outf = NULL, *outsf = NULL;
  106. char *buffer = NULL, *cmd = NULL;
  107. int skip = 0, line = 0, total_responses = 0;
  108. if (!(inf = fopen(in, "r"))) {
  109. printf("Error: Cannot open '%s' for reading\n", in);
  110. return 1;
  111. }
  112. if (!(outf = fopen(out, "w"))) {
  113. printf("Error: Cannot open '%s' for writing\n", out);
  114. return 1;
  115. }
  116. if (!(outsf = fopen(outs, "w"))) {
  117. printf("Error: Cannot open '%s' for writing\n", outs);
  118. return 1;
  119. }
  120. printf("Parsing res file '%s'", in);
  121. fprintf(outf, "/* DO NOT EDIT THIS FILE. */\n\
  122. #ifndef _RESPONSE_H\n\
  123. #define _RESPONSE_H\n\
  124. \n\
  125. typedef unsigned int response_t;\n\n\
  126. enum {\n");
  127. fprintf(outsf, "/* DO NOT EDIT THIS FILE. */\n\
  128. #ifndef _RESPONSES_H\n\
  129. #define _RESPONSES_H\n\
  130. \n\
  131. typedef char * res_t;\n\n");
  132. while ((!feof(inf)) && ((buffer = step_thru_file(inf)) != NULL) ) {
  133. line++;
  134. if ((*buffer)) {
  135. if (strchr(buffer, '\n')) *(char*)strchr(buffer, '\n') = 0;
  136. if ((skipline(buffer, &skip))) continue;
  137. if (buffer[0] == ':') { /* New cmd */
  138. char *p = NULL;
  139. if (cmd && cmd[0]) { /* CLOSE LAST RES */
  140. fprintf(outf, ",\n"); /* for enum */
  141. fprintf(outsf, "\tNULL\n};\n\n");
  142. free(cmd);
  143. }
  144. p = strchr(buffer, ':');
  145. p++;
  146. if (strcmp(p, "end")) { /* NEXT RES */
  147. cmd = (char *) calloc(1, strlen(p) + 1);
  148. total_responses++;
  149. strcpy(cmd, p);
  150. printf(".");
  151. fprintf(outf, "\tRES_%s", strtoupper(cmd));
  152. if (total_responses == 1)
  153. fprintf(outf, " = 1");
  154. fprintf(outsf, "static res_t res_%s[] = {\n", cmd);
  155. sprintf(lower_resps, "%s,\n\tres_%s", lower_resps, cmd);
  156. } else { /* END */
  157. fprintf(outf, "\tRES_END\n};\n\n#define RES_TYPES %d\n", total_responses);
  158. fprintf(outf, "const char *response(response_t);\nvoid init_responses();\nconst char *r_banned();\n\n#endif /* !_RESPONSE_H */\n");
  159. fprintf(outsf, "static res_t *res[] = {\n\tNULL%s\n};\n#endif /* !_RESPONSES_H */\n", lower_resps);
  160. }
  161. } else { /* NEXT RES TEXT */
  162. buffer++;
  163. fprintf(outsf, "\t\"%s\",\n", buffer);
  164. /* fprintf(outf, "%s\\n", replace(buffer, "\"", "\\\"")); */
  165. }
  166. }
  167. buffer = NULL;
  168. }
  169. printf(" Success\n");
  170. if (inf) fclose(inf);
  171. if (outf) fclose(outf);
  172. if (outsf) fclose(outsf);
  173. return 0;
  174. }
  175. int main(int argc, char **argv) {
  176. char *in = NULL, out[1024] = "", outs[1024] = "";
  177. int ret = 0;
  178. if (argc < 3) return 1;
  179. in = (char *) calloc(1, strlen(argv[1]) + 1);
  180. strcpy(in, argv[1]);
  181. sprintf(out, "%s/response.h%s", argv[2], argc == 4 ? "~" : "");
  182. sprintf(outs, "%s/responses.h%s", argv[2], argc == 4 ? "~" : "");
  183. ret = parse_res(in, out, outs);
  184. free(in);
  185. return ret;
  186. }