1
0

makehelp.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 = (char *) calloc(1, strlen(tempBuf) + 2);
  45. strcpy(retStr, tempBuf);
  46. } else {
  47. retStr = (char *) 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. #define type(hub, leaf) (hub ? 1 : (leaf ? 2 : 0))
  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 = (char *) calloc(1, strlen(buffer) + 1), *p = NULL;
  124. int cl = 0, doleaf = 0, dohub = 0;
  125. buffer++;
  126. strcpy(ifdef, buffer);
  127. p = strchr(ifdef, ':');
  128. *p = 0;
  129. if (ifdef && ifdef[0]) {
  130. if (!strcasecmp(ifdef, "leaf")) {
  131. if (hub) { cl = 1; hub = 0; }
  132. if (!leaf) {
  133. doleaf = leaf = 1;
  134. }
  135. } else if (!strcasecmp(ifdef, "hub")) {
  136. if (leaf) { cl = 1; hub = 0; }
  137. if (!hub) {
  138. dohub = hub = 1;
  139. }
  140. }
  141. } else { if (leaf || hub) { cl = 1; } leaf = 0; hub = 0; }
  142. if (cmd && cmd[0]) { /* CLOSE LAST CMD */
  143. if (strchr(cmd, ':')) /* garbled */
  144. fprintf(out,"\")},\n");
  145. else
  146. fprintf(out,"\"},\n");
  147. // if (cl) { cl = 0; fprintf(out, "#endif\n"); }
  148. // if (dohub) { dohub = 0; fprintf(out, "#ifdef HUB\n"); }
  149. // else if (doleaf) { doleaf = 0; fprintf(out, "#ifdef LEAF\n"); }
  150. free(cmd);
  151. }
  152. p = strchr(buffer, ':');
  153. p++;
  154. if (strcmp(p, "end")) { /* NEXT CMD */
  155. cmd = (char *) calloc(1, strlen(p) + 1);
  156. strcpy(cmd, p);
  157. printf(".");
  158. // if (dohub) { dohub = 0; fprintf(out, "#ifdef HUB\n"); }
  159. // else if (doleaf) { doleaf = 0; fprintf(out, "#ifdef LEAF\n"); }
  160. if (strchr(cmd, ':')) {
  161. char *p2 = NULL, *cmdn = (char *) calloc(1,strlen(cmd) + 1);
  162. strcpy(cmdn, cmd);
  163. p2 = strchr(cmdn, ':');
  164. *p2 = 0;
  165. fprintf(out, " {%d, \"%s\", STR(\"", type(dohub, doleaf), cmdn);
  166. } else
  167. fprintf(out, " {%d, \"%s\", 0, \"", type(dohub, doleaf), cmd);
  168. } else { /* END */
  169. fprintf(out, " {0, NULL, 0, NULL}\n};\n");
  170. }
  171. } else { /* CMD HELP INFO */
  172. fprintf(out, "%s\\n", replace(buffer, "\"", "\\\""));
  173. }
  174. }
  175. buffer = NULL;
  176. }
  177. fprintf(out, "#endif /* HELP_H */\n");
  178. printf(" Success\n");
  179. if (in) fclose(in);
  180. if (out) fclose(out);
  181. return 0;
  182. }
  183. int main(int argc, char **argv) {
  184. char *in = NULL, *out = NULL;
  185. int ret = 0;
  186. if (argc < 3) return 1;
  187. in = (char *) calloc(1, strlen(argv[1]) + 1);
  188. strcpy(in, argv[1]);
  189. out = (char *) calloc(1, strlen(argv[2]) + 1);
  190. strcpy(out, argv[2]);
  191. ret = parse_help(in, out);
  192. free(in);
  193. free(out);
  194. return ret;
  195. }