sorthelp.c 5.5 KB

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