sorthelp.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <stdlib.h>
  24. typedef struct {
  25. int leaf;
  26. int hub;
  27. char *name;
  28. char *txt;
  29. } cmds;
  30. #define BUFSIZE 20240
  31. cmds cmdlist[900];
  32. int cmdi = 0;
  33. char *replace(char *string, char *oldie, char *newbie)
  34. {
  35. static char newstring[BUFSIZE] = "";
  36. int str_index, newstr_index, oldie_index, end, new_len, old_len, cpy_len;
  37. char *c = NULL;
  38. if (string == NULL) return "";
  39. if ((c = (char *) strstr(string, oldie)) == NULL) return string;
  40. new_len = strlen(newbie);
  41. old_len = strlen(oldie);
  42. end = strlen(string) - old_len;
  43. oldie_index = c - string;
  44. newstr_index = 0;
  45. str_index = 0;
  46. while(str_index <= end && c != NULL) {
  47. cpy_len = oldie_index-str_index;
  48. strncpy(newstring + newstr_index, string + str_index, cpy_len);
  49. newstr_index += cpy_len;
  50. str_index += cpy_len;
  51. strcpy(newstring + newstr_index, newbie);
  52. newstr_index += new_len;
  53. str_index += old_len;
  54. if((c = (char *) strstr(string + str_index, oldie)) != NULL)
  55. oldie_index = c - string;
  56. }
  57. strcpy(newstring + newstr_index, string + str_index);
  58. return (newstring);
  59. }
  60. char *step_thru_file(FILE *fd)
  61. {
  62. char tempBuf[BUFSIZE] = "", *retStr = NULL;
  63. if (fd == NULL) {
  64. return NULL;
  65. }
  66. retStr = NULL;
  67. while (!feof(fd)) {
  68. fgets(tempBuf, sizeof(tempBuf), fd);
  69. if (!feof(fd)) {
  70. if (retStr == NULL) {
  71. retStr = (char *) calloc(1, strlen(tempBuf) + 2);
  72. strcpy(retStr, tempBuf);
  73. } else {
  74. retStr = (char *) realloc(retStr, strlen(retStr) + strlen(tempBuf));
  75. strcat(retStr, tempBuf);
  76. }
  77. if (retStr[strlen(retStr)-1] == '\n') {
  78. retStr[strlen(retStr)-1] = 0;
  79. break;
  80. }
  81. }
  82. }
  83. return retStr;
  84. }
  85. char *newsplit(char **rest)
  86. {
  87. register char *o, *r;
  88. if (!rest)
  89. return *rest = "";
  90. o = *rest;
  91. while (*o == ' ')
  92. o++;
  93. r = o;
  94. while (*o && (*o != ' '))
  95. o++;
  96. if (*o)
  97. *o++ = 0;
  98. *rest = o;
  99. return r;
  100. }
  101. int skipline (char *line, int *skip) {
  102. static int multi = 0;
  103. if ((!strncmp(line, "//", 2))) {
  104. (*skip)++;
  105. } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
  106. multi = 0;
  107. (*skip)++;
  108. } else if ( (strstr(line, "/*")) ) {
  109. (*skip)++;
  110. multi = 1;
  111. } else if ( (strstr(line, "*/")) ) {
  112. multi = 0;
  113. } else {
  114. if (!multi) (*skip) = 0;
  115. }
  116. return (*skip);
  117. }
  118. int my_cmp (const cmds *c1, const cmds *c2)
  119. {
  120. return strcmp (c1->name, c2->name);
  121. }
  122. int parse_help(char *infile, char *outfile) {
  123. FILE *in = NULL, *out = NULL;
  124. char *buffer = NULL, my_buf[BUFSIZE] = "", *fulllist = (char *) calloc(1, 1);
  125. int skip = 0, line = 0, i = 0, leaf = 0, hub = 0;
  126. if (!(in = fopen(infile, "r"))) {
  127. printf("Error: Cannot open '%s' for reading\n", infile);
  128. return 1;
  129. }
  130. printf("Sorting help file '%s'", infile);
  131. while ((!feof(in)) && ((buffer = step_thru_file(in)) != NULL) ) {
  132. line++;
  133. if ((*buffer)) {
  134. if (strchr(buffer, '\n')) *(char*)strchr(buffer, '\n') = 0;
  135. if ((skipline(buffer, &skip))) continue;
  136. if (buffer[0] == ':') { //New cmd
  137. char *ifdef = (char *) calloc(1, strlen(buffer) + 1), *p;
  138. buffer++;
  139. strcpy(ifdef, buffer);
  140. p = strchr(ifdef, ':');
  141. *p = 0;
  142. if (ifdef && ifdef[0]) {
  143. if (!strcasecmp(ifdef, "leaf"))
  144. leaf++;
  145. else if (!strcasecmp(ifdef, "hub"))
  146. hub++;
  147. }
  148. /* finish last command */
  149. if (my_buf && my_buf[0]) {
  150. my_buf[strlen(my_buf)] = 0;
  151. cmdlist[cmdi].txt = (char *) calloc(1, strlen(my_buf) + 1);
  152. strcpy(cmdlist[cmdi].txt, my_buf);
  153. i++;
  154. cmdi++;
  155. }
  156. /* move on to next cmd now */
  157. p = strchr(buffer, ':');
  158. p++;
  159. if (strcmp(p, "end")) { /* NEXT CMD */
  160. printf(".");
  161. my_buf[0] = 0;
  162. cmdlist[cmdi].leaf = leaf;
  163. cmdlist[cmdi].hub = hub;
  164. hub = leaf = 0;
  165. for (i = 0; i < cmdi; i++ ) /* Eliminate duplicates */
  166. if (!strcmp(cmdlist[i].name, p)) {
  167. printf("\b[%s]", p);
  168. cmdi--;
  169. my_buf[0] = 0;
  170. continue;
  171. }
  172. cmdlist[cmdi].name = (char *) calloc(1, strlen(p) + 1);
  173. strcpy(cmdlist[cmdi].name, p);
  174. } else { /* END */
  175. break;
  176. }
  177. } else { /* CMD HELP INFO */
  178. strcat(my_buf, buffer);
  179. strcat(my_buf, "\\n");
  180. }
  181. }
  182. buffer = NULL;
  183. }
  184. if (in) fclose(in);
  185. if (!(out = fopen(outfile, "w"))) {
  186. printf("Error: Cannot open '%s' for writing\n", outfile);
  187. return 1;
  188. }
  189. qsort(cmdlist, cmdi, sizeof(cmds), (int (*)(const void *, const void *)) &my_cmp);
  190. for (i = 0; i < cmdi; i++ ) {
  191. fulllist = (char *) realloc(fulllist, strlen(fulllist) + strlen(cmdlist[i].name) + 2);
  192. strcat(fulllist, cmdlist[i].name);
  193. strcat(fulllist, " ");
  194. fprintf(out, ":");
  195. if (cmdlist[i].leaf) fprintf(out, "leaf");
  196. else if (cmdlist[i].hub) fprintf(out, "hub");
  197. fprintf(out, ":%s\n", cmdlist[i].name);
  198. fprintf(out, "%s", replace(cmdlist[i].txt, "\\n", "\n"));
  199. }
  200. fprintf(out, "::end\n");
  201. if (out) fclose(out);
  202. printf(" Success\n");
  203. fulllist[strlen(fulllist)] = 0;
  204. printf("Sorted (%d): %s\n", cmdi, fulllist);
  205. return 0;
  206. }
  207. int main(int argc, char **argv) {
  208. char *in = NULL, *out = NULL;
  209. int ret = 0;
  210. if (argc < 3) return 1;
  211. in = (char *) calloc(1, strlen(argv[1]) + 1);
  212. strcpy(in, argv[1]);
  213. out = (char *) calloc(1, strlen(argv[2]) + 1);
  214. strcpy(out, argv[2]);
  215. ret = parse_help(in, out);
  216. free(in);
  217. free(out);
  218. return ret;
  219. }