sorthelp.c 5.2 KB

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