sorthelp.c 5.5 KB

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