makehelp.c 5.4 KB

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