| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- #include <string.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- char *replace(char *string, char *oldie, char *newbie)
- {
- static char newstring[1024] = "";
- int str_index, newstr_index, oldie_index, end, new_len, old_len, cpy_len;
- char *c = NULL;
- if (string == NULL) return "";
- if ((c = (char *) strstr(string, oldie)) == NULL) return string;
- new_len = strlen(newbie);
- old_len = strlen(oldie);
- end = strlen(string) - old_len;
- oldie_index = c - string;
- newstr_index = 0;
- str_index = 0;
- while(str_index <= end && c != NULL) {
- cpy_len = oldie_index-str_index;
- strncpy(newstring + newstr_index, string + str_index, cpy_len);
- newstr_index += cpy_len;
- str_index += cpy_len;
- strcpy(newstring + newstr_index, newbie);
- newstr_index += new_len;
- str_index += old_len;
- if((c = (char *) strstr(string + str_index, oldie)) != NULL)
- oldie_index = c - string;
- }
- strcpy(newstring + newstr_index, string + str_index);
- return (newstring);
- }
- char *step_thru_file(FILE *fd)
- {
- char tempBuf[1024] = "";
- char *retStr = NULL;
- if (fd == NULL) {
- return NULL;
- }
- retStr = NULL;
- while (!feof(fd)) {
- fgets(tempBuf, sizeof(tempBuf), fd);
- if (!feof(fd)) {
- if (retStr == NULL) {
- retStr = calloc(1, strlen(tempBuf) + 2);
- strcpy(retStr, tempBuf);
- } else {
- retStr = realloc(retStr, strlen(retStr) + strlen(tempBuf));
- strcat(retStr, tempBuf);
- }
- if (retStr[strlen(retStr)-1] == '\n') {
- retStr[strlen(retStr)-1] = 0;
- break;
- }
- }
- }
- return retStr;
- }
- char *newsplit(char **rest)
- {
- register char *o, *r;
- if (!rest)
- return *rest = "";
- o = *rest;
- while (*o == ' ')
- o++;
- r = o;
- while (*o && (*o != ' '))
- o++;
- if (*o)
- *o++ = 0;
- *rest = o;
- return r;
- }
- int skipline (char *line, int *skip) {
- static int multi = 0;
- if ((!strncmp(line, "//", 2))) {
- (*skip)++;
- } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
- multi = 0;
- (*skip)++;
- } else if ( (strstr(line, "/*")) ) {
- (*skip)++;
- multi = 1;
- } else if ( (strstr(line, "*/")) ) {
- multi = 0;
- } else {
- if (!multi) (*skip) = 0;
- }
- return (*skip);
- }
- int parse_help(char *infile, char *outfile) {
- FILE *in = NULL, *out = NULL;
- char *buffer = NULL, *cmd = NULL;
- int skip = 0, line = 0, leaf = 0, hub = 0;
- if (!(in = fopen(infile, "r"))) {
- printf("Error: Cannot open '%s' for reading\n", infile);
- return 1;
- }
- if (!(out = fopen(outfile, "w"))) {
- printf("Error: Cannot open '%s' for writing\n", outfile);
- return 1;
- }
- printf("Parsing help file '%s'", infile);
- fprintf(out, "/* DO NOT EDIT THIS FILE, EDIT misc/help.txt INSTEAD */\n#ifndef HELP_H\n\
- #define HELP_H\n\
- #define STR(x) x\n\
- typedef struct { \n\
- int type; \n\
- char *cmd; \n\
- int garble; \n\
- char *desc; \n\
- } help_t; \n\
- \n\
- help_t help[] = \n\
- { \n");
- while ((!feof(in)) && ((buffer = step_thru_file(in)) != NULL) ) {
- line++;
- if ((*buffer)) {
- if (strchr(buffer, '\n')) *(char*)strchr(buffer, '\n') = 0;
- if ((skipline(buffer, &skip))) continue;
- if (buffer[0] == ':') { /* New cmd */
- char *ifdef = calloc(1, strlen(buffer) + 1), *p = NULL;
- int cl = 0, doleaf = 0, dohub = 0;
- buffer++;
- strcpy(ifdef, buffer);
- p = strchr(ifdef, ':');
- *p = 0;
- if (ifdef && ifdef[0]) {
- if (!strcasecmp(ifdef, "leaf")) {
- if (hub) { cl = 1; hub = 0; }
- if (!leaf) {
- doleaf = leaf = 1;
- }
- } else if (!strcasecmp(ifdef, "hub")) {
- if (leaf) { cl = 1; hub = 0; }
- if (!hub) {
- dohub = hub = 1;
- }
- }
- } else { if (leaf || hub) { cl = 1; } leaf = 0; hub = 0; }
- if (cmd && cmd[0]) { /* CLOSE LAST CMD */
- if (strchr(cmd, ':')) /* garbled */
- fprintf(out,"\")},\n");
- else
- fprintf(out,"\"},\n");
- if (cl) { cl = 0; fprintf(out, "#endif\n"); }
- if (dohub) { dohub = 0; fprintf(out, "#ifdef HUB\n"); }
- else if (doleaf) { doleaf = 0; fprintf(out, "#ifdef LEAF\n"); }
- free(cmd);
- }
- p = strchr(buffer, ':');
- p++;
- if (strcmp(p, "end")) { /* NEXT CMD */
- cmd = calloc(1, strlen(p) + 1);
- strcpy(cmd, p);
- printf(".");
- if (dohub) { dohub = 0; fprintf(out, "#ifdef HUB\n"); }
- else if (doleaf) { doleaf = 0; fprintf(out, "#ifdef LEAF\n"); }
- if (strchr(cmd, ':')) {
- char *p2 = NULL, *cmdn = calloc(1,strlen(cmd) + 1);
- strcpy(cmdn, cmd);
- p2 = strchr(cmdn, ':');
- *p2 = 0;
- fprintf(out, " {2, \"%s\", STR(\"", cmdn);
- } else
- fprintf(out, " {2, \"%s\", 0, \"", cmd);
- } else { /* END */
- fprintf(out, " {0, NULL, 0, NULL}\n};\n");
- }
- } else { /* CMD HELP INFO */
- fprintf(out, "%s\\n", replace(buffer, "\"", "\\\""));
- }
- }
- buffer = NULL;
- }
- fprintf(out, "#endif /* HELP_H */\n");
- printf(" Success\n");
- if (in) fclose(in);
- if (out) fclose(out);
- return 0;
- }
- int main(int argc, char **argv) {
- char *in = NULL, *out = NULL;
- int ret = 0;
- if (argc < 3) return 1;
- in = calloc(1, strlen(argv[1]) + 1);
- strcpy(in, argv[1]);
- out = calloc(1, strlen(argv[2]) + 1);
- strcpy(out, argv[2]);
- ret = parse_help(in, out);
- free(in);
- free(out);
- return ret;
- }
|