makehelp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include "conf.h"
  6. char *replace(char *string, char *oldie, char *newbie)
  7. {
  8. static char newstring[1024] = "";
  9. int str_index, newstr_index, oldie_index, end, new_len, old_len, cpy_len;
  10. char *c;
  11. if (string == NULL) return "";
  12. if ((c = (char *) strstr(string, oldie)) == NULL) return string;
  13. new_len = strlen(newbie);
  14. old_len = strlen(oldie);
  15. end = strlen(string) - old_len;
  16. oldie_index = c - string;
  17. newstr_index = 0;
  18. str_index = 0;
  19. while(str_index <= end && c != NULL) {
  20. cpy_len = oldie_index-str_index;
  21. strncpy(newstring + newstr_index, string + str_index, cpy_len);
  22. newstr_index += cpy_len;
  23. str_index += cpy_len;
  24. strcpy(newstring + newstr_index, newbie);
  25. newstr_index += new_len;
  26. str_index += old_len;
  27. if((c = (char *) strstr(string + str_index, oldie)) != NULL)
  28. oldie_index = c - string;
  29. }
  30. strcpy(newstring + newstr_index, string + str_index);
  31. return (newstring);
  32. }
  33. char *step_thru_file(FILE *fd)
  34. {
  35. const int tempBufSize = 1024;
  36. char tempBuf[tempBufSize];
  37. char *retStr = NULL;
  38. if (fd == NULL) {
  39. return NULL;
  40. }
  41. retStr = NULL;
  42. while (!feof(fd)) {
  43. fgets(tempBuf, tempBufSize, fd);
  44. if (!feof(fd)) {
  45. if (retStr == NULL) {
  46. retStr = malloc(strlen(tempBuf) + 2);
  47. strcpy(retStr, tempBuf);
  48. } else {
  49. retStr = realloc(retStr, strlen(retStr) + strlen(tempBuf));
  50. strcat(retStr, tempBuf);
  51. }
  52. if (retStr[strlen(retStr)-1] == '\n') {
  53. retStr[strlen(retStr)-1] = 0;
  54. break;
  55. }
  56. }
  57. }
  58. return retStr;
  59. }
  60. char *newsplit(char **rest)
  61. {
  62. register char *o, *r;
  63. if (!rest)
  64. return *rest = "";
  65. o = *rest;
  66. while (*o == ' ')
  67. o++;
  68. r = o;
  69. while (*o && (*o != ' '))
  70. o++;
  71. if (*o)
  72. *o++ = 0;
  73. *rest = o;
  74. return r;
  75. }
  76. int skipline (char *line, int *skip) {
  77. static int multi = 0;
  78. if ((!strncmp(line, "//", 2))) {
  79. (*skip)++;
  80. } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
  81. multi = 0;
  82. (*skip)++;
  83. } else if ( (strstr(line, "/*")) ) {
  84. (*skip)++;
  85. multi = 1;
  86. } else if ( (strstr(line, "*/")) ) {
  87. multi = 0;
  88. } else {
  89. if (!multi) (*skip) = 0;
  90. }
  91. return (*skip);
  92. }
  93. int parse_help(char *infile, char *outfile) {
  94. FILE *in = NULL, *out = NULL;
  95. char *buffer = NULL, *cmd = NULL;
  96. int skip = 0, line = 0, leaf = 0, hub = 0;
  97. if (!(in = fopen(infile, "r"))) {
  98. printf("Error: Cannot open '%s' for reading\n", infile);
  99. return 1;
  100. }
  101. if (!(out = fopen(outfile, "w"))) {
  102. printf("Error: Cannot open '%s' for writing\n", outfile);
  103. return 1;
  104. }
  105. printf("Parsing help file '%s'", infile);
  106. fprintf(out, "/* DO NOT EDIT THIS FILE, EDIT misc/help.txt INSTEAD */\n#ifndef HELP_H\n\
  107. #define HELP_H\n\
  108. #define STR(x) x\n\
  109. typedef struct { \n\
  110. int type; \n\
  111. char *cmd; \n\
  112. int garble; \n\
  113. char *desc; \n\
  114. } help_t; \n\
  115. \n\
  116. help_t help[] = \n\
  117. { \n");
  118. while ((!feof(in)) && ((buffer = step_thru_file(in)) != NULL) ) {
  119. line++;
  120. if ((*buffer)) {
  121. if (strchr(buffer, '\n')) *(char*)strchr(buffer, '\n') = 0;
  122. if ((skipline(buffer, &skip))) continue;
  123. if (buffer[0] == ':') { //New cmd
  124. char *ifdef = malloc(strlen(buffer) + 1), *p;
  125. int cl = 0, doleaf = 0, dohub = 0;
  126. buffer++;
  127. ifdef[0] = 0;
  128. strcpy(ifdef, buffer);
  129. p = strchr(ifdef, ':');
  130. *p = 0;
  131. if (ifdef && ifdef[0]) {
  132. if (!strcasecmp(ifdef, "leaf")) {
  133. if (hub) { cl = 1; hub = 0; }
  134. if (!leaf) {
  135. doleaf = leaf = 1;
  136. }
  137. } else if (!strcasecmp(ifdef, "hub")) {
  138. if (leaf) { cl = 1; hub = 0; }
  139. if (!hub) {
  140. dohub = hub = 1;
  141. }
  142. }
  143. } else { if (leaf || hub) { cl = 1; } leaf = 0; hub = 0; }
  144. if (cmd && cmd[0]) { /* CLOSE LAST CMD */
  145. if (strchr(cmd, ':')) /* garbled */
  146. fprintf(out,"\")},\n");
  147. else
  148. fprintf(out,"\"},\n");
  149. if (cl) { cl = 0; fprintf(out, "#endif\n"); }
  150. if (dohub) { dohub = 0; fprintf(out, "#ifdef HUB\n"); }
  151. else if (doleaf) { doleaf = 0; fprintf(out, "#ifdef LEAF\n"); }
  152. free(cmd);
  153. }
  154. p = strchr(buffer, ':');
  155. p++;
  156. if (strcmp(p, "end")) { /* NEXT CMD */
  157. cmd = malloc(strlen(p) + 1);
  158. strcpy(cmd, p);
  159. printf(".");
  160. if (dohub) { dohub = 0; fprintf(out, "#ifdef HUB\n"); }
  161. else if (doleaf) { doleaf = 0; fprintf(out, "#ifdef LEAF\n"); }
  162. if (strchr(cmd, ':')) {
  163. char *p2, *cmdn = malloc(strlen(cmd) + 1);
  164. strcpy(cmdn, cmd);
  165. p2 = strchr(cmdn, ':');
  166. *p2 = 0;
  167. fprintf(out, " {2, \"%s\", STR(\"", cmdn);
  168. } else
  169. fprintf(out, " {2, \"%s\", 0, \"", cmd);
  170. } else { /* END */
  171. fprintf(out, " {0, NULL}\n};\n");
  172. }
  173. } else { /* CMD HELP INFO */
  174. fprintf(out, "%s\\n", replace(buffer, "\"", "\\\""));
  175. }
  176. }
  177. buffer = NULL;
  178. }
  179. fprintf(out, "#endif /* HELP_H */\n");
  180. printf(" Success\n");
  181. if (in) fclose(in);
  182. if (out) fclose(out);
  183. return 0;
  184. }
  185. int main(int argc, char **argv) {
  186. char *in, *out;
  187. int ret = 0;
  188. if (argc < 3) return 1;
  189. in = malloc(strlen(argv[1]) + 1);
  190. strcpy(in, argv[1]);
  191. out = malloc(strlen(argv[2]) + 1);
  192. strcpy(out, argv[2]);
  193. ret = parse_help(in, out);
  194. free(in);
  195. free(out);
  196. return ret;
  197. }