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