123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <limits.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int exitstatus = 0;
- typedef void DFL(char *);
- DFL *defaultline;
- typedef void FILEONLY(char * file);
- FILEONLY *internalfunctions;
- FILEONLY *externalfunctions;
- FILEONLY *symbolsonly;
- typedef void FILELINE(char * file, char * line);
- FILELINE * singlefunctions;
- FILELINE * entity_system;
- #define MAXLINESZ 2048
- #define MAXFILES 250
- #define KERNELDOCPATH "scripts/"
- #define KERNELDOC "kernel-doc"
- #define DOCBOOK "-docbook"
- #define FUNCTION "-function"
- #define NOFUNCTION "-nofunction"
- void usage (void)
- {
- fprintf(stderr, "Usage: docproc {doc|depend} file\n");
- fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
- fprintf(stderr, "doc: frontend when generating kernel documentation\n");
- fprintf(stderr, "depend: generate list of files referenced within file\n");
- }
- void exec_kernel_doc(char **svec)
- {
- pid_t pid;
- int ret;
- char *real_filename;
- int rflen;
-
- fflush(stdout);
- switch(pid=fork()) {
- case -1:
- perror("vfork"+1);
- exit(1);
- case 0:
- rflen = strlen(getenv("SRCTREE"));
- rflen += strlen(KERNELDOCPATH KERNELDOC);
- real_filename = alloca(rflen + 1);
- strcpy(real_filename, getenv("SRCTREE"));
- strcat(real_filename, KERNELDOCPATH KERNELDOC);
- execvp(real_filename, svec);
- fprintf(stderr, "exec ");
- perror(real_filename);
- exit(1);
- default:
- waitpid(pid, &ret ,0);
- }
- if (WIFEXITED(ret))
- exitstatus |= WEXITSTATUS(ret);
- else
- exitstatus = 0xff;
- }
- struct symbols
- {
- char *name;
- };
- struct symfile
- {
- char *filename;
- struct symbols *symbollist;
- int symbolcnt;
- };
- struct symfile symfilelist[MAXFILES];
- int symfilecnt = 0;
- void add_new_symbol(struct symfile *sym, char * symname)
- {
- sym->symbollist =
- realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *));
- sym->symbollist[sym->symbolcnt++].name = strdup(symname);
- }
- struct symfile * add_new_file(char * filename)
- {
- symfilelist[symfilecnt++].filename = strdup(filename);
- return &symfilelist[symfilecnt - 1];
- }
- struct symfile * filename_exist(char * filename)
- {
- int i;
- for (i=0; i < symfilecnt; i++)
- if (strcmp(symfilelist[i].filename, filename) == 0)
- return &symfilelist[i];
- return NULL;
- }
- void adddep(char * file) { printf("\t%s", file); }
- void adddep2(char * file, char * line) { line = line; adddep(file); }
- void noaction(char * line) { line = line; }
- void noaction2(char * file, char * line) { file = file; line = line; }
- void printline(char * line) { printf("%s", line); }
- void find_export_symbols(char * filename)
- {
- FILE * fp;
- struct symfile *sym;
- char line[MAXLINESZ];
- if (filename_exist(filename) == NULL) {
- int rflen = strlen(getenv("SRCTREE")) + strlen(filename);
- char *real_filename = alloca(rflen + 1);
- strcpy(real_filename, getenv("SRCTREE"));
- strcat(real_filename, filename);
- sym = add_new_file(filename);
- fp = fopen(real_filename, "r");
- if (fp == NULL)
- {
- fprintf(stderr, "docproc: ");
- perror(real_filename);
- }
- while (fgets(line, MAXLINESZ, fp)) {
- char *p;
- char *e;
- if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != 0) ||
- ((p = strstr(line, "EXPORT_SYMBOL")) != 0)) {
-
- while (isalnum(*p) || *p == '_')
- p++;
-
- while (isspace(*p))
- p++;
- if (*p != '(')
- continue;
- else
- p++;
- while (isspace(*p))
- p++;
- e = p;
- while (isalnum(*e) || *e == '_')
- e++;
- *e = '\0';
- add_new_symbol(sym, p);
- }
- }
- fclose(fp);
- }
- }
- void docfunctions(char * filename, char * type)
- {
- int i,j;
- int symcnt = 0;
- int idx = 0;
- char **vec;
- for (i=0; i <= symfilecnt; i++)
- symcnt += symfilelist[i].symbolcnt;
- vec = malloc((2 + 2 * symcnt + 2) * sizeof(char*));
- if (vec == NULL) {
- perror("docproc: ");
- exit(1);
- }
- vec[idx++] = KERNELDOC;
- vec[idx++] = DOCBOOK;
- for (i=0; i < symfilecnt; i++) {
- struct symfile * sym = &symfilelist[i];
- for (j=0; j < sym->symbolcnt; j++) {
- vec[idx++] = type;
- vec[idx++] = sym->symbollist[j].name;
- }
- }
- vec[idx++] = filename;
- vec[idx] = NULL;
- printf("<!-- %s -->\n", filename);
- exec_kernel_doc(vec);
- fflush(stdout);
- free(vec);
- }
- void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); }
- void extfunc(char * filename) { docfunctions(filename, FUNCTION); }
- void singfunc(char * filename, char * line)
- {
- char *vec[200];
- int i, idx = 0;
- int startofsym = 1;
- vec[idx++] = KERNELDOC;
- vec[idx++] = DOCBOOK;
-
- for (i=0; line[i]; i++) {
- if (isspace(line[i])) {
- line[i] = '\0';
- startofsym = 1;
- continue;
- }
- if (startofsym) {
- startofsym = 0;
- vec[idx++] = FUNCTION;
- vec[idx++] = &line[i];
- }
- }
- vec[idx++] = filename;
- vec[idx] = NULL;
- exec_kernel_doc(vec);
- }
- void parse_file(FILE *infile)
- {
- char line[MAXLINESZ];
- char * s;
- while (fgets(line, MAXLINESZ, infile)) {
- if (line[0] == '!') {
- s = line + 2;
- switch (line[1]) {
- case 'E':
- while (*s && !isspace(*s)) s++;
- *s = '\0';
- externalfunctions(line+2);
- break;
- case 'I':
- while (*s && !isspace(*s)) s++;
- *s = '\0';
- internalfunctions(line+2);
- break;
- case 'D':
- while (*s && !isspace(*s)) s++;
- *s = '\0';
- symbolsonly(line+2);
- break;
- case 'F':
-
- while (*s && !isspace(*s)) s++;
- *s++ = '\0';
-
- while (isspace(*s))
- s++;
- singlefunctions(line +2, s);
- break;
- default:
- defaultline(line);
- }
- }
- else {
- defaultline(line);
- }
- }
- fflush(stdout);
- }
- int main(int argc, char **argv)
- {
- FILE * infile;
- if (argc != 3) {
- usage();
- exit(1);
- }
-
- infile = fopen(argv[2], "r");
- if (infile == NULL) {
- fprintf(stderr, "docproc: ");
- perror(argv[2]);
- exit(2);
- }
- if (strcmp("doc", argv[1]) == 0)
- {
-
-
- defaultline = noaction;
- internalfunctions = find_export_symbols;
- externalfunctions = find_export_symbols;
- symbolsonly = find_export_symbols;
- singlefunctions = noaction2;
- parse_file(infile);
-
- fseek(infile, 0, SEEK_SET);
- defaultline = printline;
- internalfunctions = intfunc;
- externalfunctions = extfunc;
- symbolsonly = printline;
- singlefunctions = singfunc;
- parse_file(infile);
- }
- else if (strcmp("depend", argv[1]) == 0)
- {
-
- printf("%s\t", argv[2]);
- defaultline = noaction;
- internalfunctions = adddep;
- externalfunctions = adddep;
- symbolsonly = adddep;
- singlefunctions = adddep2;
- parse_file(infile);
- printf("\n");
- }
- else
- {
- fprintf(stderr, "Unknown option: %s\n", argv[1]);
- exit(1);
- }
- fclose(infile);
- fflush(stdout);
- return exitstatus;
- }
|