makehelp.c 5.4 KB

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