Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * docproc is a simple preprocessor for the template files |
| 3 | * used as placeholders for the kernel internal documentation. |
| 4 | * docproc is used for documentation-frontend and |
| 5 | * dependency-generator. |
| 6 | * The two usages have in common that they require |
| 7 | * some knowledge of the .tmpl syntax, therefore they |
| 8 | * are kept together. |
| 9 | * |
| 10 | * documentation-frontend |
| 11 | * Scans the template file and call kernel-doc for |
| 12 | * all occurrences of ![EIF]file |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 13 | * Beforehand each referenced file is scanned for |
| 14 | * any symbols that are exported via these macros: |
| 15 | * EXPORT_SYMBOL(), EXPORT_SYMBOL_GPL(), & |
| 16 | * EXPORT_SYMBOL_GPL_FUTURE() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * This is used to create proper -function and |
| 18 | * -nofunction arguments in calls to kernel-doc. |
| 19 | * Usage: docproc doc file.tmpl |
| 20 | * |
| 21 | * dependency-generator: |
| 22 | * Scans the template file and list all files |
| 23 | * referenced in a format recognized by make. |
| 24 | * Usage: docproc depend file.tmpl |
| 25 | * Writes dependency information to stdout |
| 26 | * in the following format: |
| 27 | * file.tmpl src.c src2.c |
| 28 | * The filenames are obtained from the following constructs: |
| 29 | * !Efilename |
| 30 | * !Ifilename |
| 31 | * !Dfilename |
| 32 | * !Ffilename |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 33 | * !Pfilename |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | * |
| 35 | */ |
| 36 | |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 37 | #define _GNU_SOURCE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <ctype.h> |
| 42 | #include <unistd.h> |
| 43 | #include <limits.h> |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 44 | #include <errno.h> |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 45 | #include <getopt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <sys/types.h> |
| 47 | #include <sys/wait.h> |
Jani Nikula | 0e95abf | 2016-05-12 16:15:44 +0300 | [diff] [blame] | 48 | #include <time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | /* exitstatus is used to keep track of any failing calls to kernel-doc, |
| 51 | * but execution continues. */ |
| 52 | int exitstatus = 0; |
| 53 | |
| 54 | typedef void DFL(char *); |
| 55 | DFL *defaultline; |
| 56 | |
| 57 | typedef void FILEONLY(char * file); |
| 58 | FILEONLY *internalfunctions; |
| 59 | FILEONLY *externalfunctions; |
| 60 | FILEONLY *symbolsonly; |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 61 | FILEONLY *findall; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
J.A. Magallon | 48b9d03 | 2005-06-25 14:59:22 -0700 | [diff] [blame] | 63 | typedef void FILELINE(char * file, char * line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | FILELINE * singlefunctions; |
| 65 | FILELINE * entity_system; |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 66 | FILELINE * docsection; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | #define MAXLINESZ 2048 |
| 69 | #define MAXFILES 250 |
| 70 | #define KERNELDOCPATH "scripts/" |
| 71 | #define KERNELDOC "kernel-doc" |
| 72 | #define DOCBOOK "-docbook" |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 73 | #define RST "-rst" |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 74 | #define LIST "-list" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #define FUNCTION "-function" |
| 76 | #define NOFUNCTION "-nofunction" |
Johannes Berg | 2e95972 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 77 | #define NODOCSECTIONS "-no-doc-sections" |
Johannes Berg | e946c43a | 2013-11-12 15:11:12 -0800 | [diff] [blame] | 78 | #define SHOWNOTFOUND "-show-not-found" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 80 | enum file_format { |
| 81 | FORMAT_AUTO, |
| 82 | FORMAT_DOCBOOK, |
| 83 | FORMAT_RST, |
| 84 | }; |
| 85 | |
| 86 | static enum file_format file_format = FORMAT_AUTO; |
| 87 | |
| 88 | #define KERNELDOC_FORMAT (file_format == FORMAT_RST ? RST : DOCBOOK) |
| 89 | |
Jiri Slaby | 2d51005 | 2009-05-31 18:05:34 +0200 | [diff] [blame] | 90 | static char *srctree, *kernsrctree; |
Rob Landley | bb13be5 | 2007-10-09 01:25:18 -0500 | [diff] [blame] | 91 | |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 92 | static char **all_list = NULL; |
| 93 | static int all_list_len = 0; |
| 94 | |
| 95 | static void consume_symbol(const char *sym) |
| 96 | { |
| 97 | int i; |
| 98 | |
| 99 | for (i = 0; i < all_list_len; i++) { |
| 100 | if (!all_list[i]) |
| 101 | continue; |
| 102 | if (strcmp(sym, all_list[i])) |
| 103 | continue; |
| 104 | all_list[i] = NULL; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 109 | static void usage (void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 111 | fprintf(stderr, "Usage: docproc [{--docbook|--rst}] {doc|depend} file\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n"); |
| 113 | fprintf(stderr, "doc: frontend when generating kernel documentation\n"); |
| 114 | fprintf(stderr, "depend: generate list of files referenced within file\n"); |
Jiri Slaby | 2d51005 | 2009-05-31 18:05:34 +0200 | [diff] [blame] | 115 | fprintf(stderr, "Environment variable SRCTREE: absolute path to sources.\n"); |
| 116 | fprintf(stderr, " KBUILD_SRC: absolute path to kernel source tree.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 120 | * Execute kernel-doc with parameters given in svec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | */ |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 122 | static void exec_kernel_doc(char **svec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | pid_t pid; |
| 125 | int ret; |
| 126 | char real_filename[PATH_MAX + 1]; |
| 127 | /* Make sure output generated so far are flushed */ |
| 128 | fflush(stdout); |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 129 | switch (pid=fork()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | case -1: |
| 131 | perror("fork"); |
| 132 | exit(1); |
| 133 | case 0: |
| 134 | memset(real_filename, 0, sizeof(real_filename)); |
Jiri Slaby | 2d51005 | 2009-05-31 18:05:34 +0200 | [diff] [blame] | 135 | strncat(real_filename, kernsrctree, PATH_MAX); |
| 136 | strncat(real_filename, "/" KERNELDOCPATH KERNELDOC, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | PATH_MAX - strlen(real_filename)); |
| 138 | execvp(real_filename, svec); |
| 139 | fprintf(stderr, "exec "); |
| 140 | perror(real_filename); |
| 141 | exit(1); |
| 142 | default: |
| 143 | waitpid(pid, &ret ,0); |
| 144 | } |
| 145 | if (WIFEXITED(ret)) |
| 146 | exitstatus |= WEXITSTATUS(ret); |
| 147 | else |
| 148 | exitstatus = 0xff; |
| 149 | } |
| 150 | |
| 151 | /* Types used to create list of all exported symbols in a number of files */ |
| 152 | struct symbols |
| 153 | { |
| 154 | char *name; |
| 155 | }; |
| 156 | |
| 157 | struct symfile |
| 158 | { |
| 159 | char *filename; |
| 160 | struct symbols *symbollist; |
| 161 | int symbolcnt; |
| 162 | }; |
| 163 | |
| 164 | struct symfile symfilelist[MAXFILES]; |
| 165 | int symfilecnt = 0; |
| 166 | |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 167 | static void add_new_symbol(struct symfile *sym, char * symname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | sym->symbollist = |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 170 | realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | sym->symbollist[sym->symbolcnt++].name = strdup(symname); |
| 172 | } |
| 173 | |
| 174 | /* Add a filename to the list */ |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 175 | static struct symfile * add_new_file(char * filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
| 177 | symfilelist[symfilecnt++].filename = strdup(filename); |
| 178 | return &symfilelist[symfilecnt - 1]; |
| 179 | } |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* Check if file already are present in the list */ |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 182 | static struct symfile * filename_exist(char * filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | int i; |
| 185 | for (i=0; i < symfilecnt; i++) |
| 186 | if (strcmp(symfilelist[i].filename, filename) == 0) |
| 187 | return &symfilelist[i]; |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * List all files referenced within the template file. |
| 193 | * Files are separated by tabs. |
| 194 | */ |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 195 | static void adddep(char * file) { printf("\t%s", file); } |
| 196 | static void adddep2(char * file, char * line) { line = line; adddep(file); } |
| 197 | static void noaction(char * line) { line = line; } |
| 198 | static void noaction2(char * file, char * line) { file = file; line = line; } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | /* Echo the line without further action */ |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 201 | static void printline(char * line) { printf("%s", line); } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | /* |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 204 | * Find all symbols in filename that are exported with EXPORT_SYMBOL & |
| 205 | * EXPORT_SYMBOL_GPL (& EXPORT_SYMBOL_GPL_FUTURE implicitly). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | * All symbols located are stored in symfilelist. |
| 207 | */ |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 208 | static void find_export_symbols(char * filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | FILE * fp; |
| 211 | struct symfile *sym; |
| 212 | char line[MAXLINESZ]; |
| 213 | if (filename_exist(filename) == NULL) { |
| 214 | char real_filename[PATH_MAX + 1]; |
| 215 | memset(real_filename, 0, sizeof(real_filename)); |
Rob Landley | bb13be5 | 2007-10-09 01:25:18 -0500 | [diff] [blame] | 216 | strncat(real_filename, srctree, PATH_MAX); |
Jiri Slaby | 2d51005 | 2009-05-31 18:05:34 +0200 | [diff] [blame] | 217 | strncat(real_filename, "/", PATH_MAX - strlen(real_filename)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | strncat(real_filename, filename, |
| 219 | PATH_MAX - strlen(real_filename)); |
| 220 | sym = add_new_file(filename); |
| 221 | fp = fopen(real_filename, "r"); |
Jesper Juhl | f0f3ca8 | 2011-06-15 11:53:13 +0200 | [diff] [blame] | 222 | if (fp == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | fprintf(stderr, "docproc: "); |
| 224 | perror(real_filename); |
Henrik Kretzschmar | 074a5dd | 2006-09-29 02:00:56 -0700 | [diff] [blame] | 225 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 227 | while (fgets(line, MAXLINESZ, fp)) { |
J.A. Magallon | 48b9d03 | 2005-06-25 14:59:22 -0700 | [diff] [blame] | 228 | char *p; |
| 229 | char *e; |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 230 | if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != NULL) || |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 231 | ((p = strstr(line, "EXPORT_SYMBOL")) != NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | /* Skip EXPORT_SYMBOL{_GPL} */ |
| 233 | while (isalnum(*p) || *p == '_') |
| 234 | p++; |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 235 | /* Remove parentheses & additional whitespace */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | while (isspace(*p)) |
| 237 | p++; |
| 238 | if (*p != '(') |
| 239 | continue; /* Syntax error? */ |
| 240 | else |
| 241 | p++; |
| 242 | while (isspace(*p)) |
| 243 | p++; |
| 244 | e = p; |
| 245 | while (isalnum(*e) || *e == '_') |
| 246 | e++; |
| 247 | *e = '\0'; |
| 248 | add_new_symbol(sym, p); |
| 249 | } |
| 250 | } |
| 251 | fclose(fp); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Document all external or internal functions in a file. |
| 257 | * Call kernel-doc with following parameters: |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 258 | * kernel-doc [-docbook|-rst] -nofunction function_name1 filename |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 259 | * Function names are obtained from all the src files |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | * by find_export_symbols. |
| 261 | * intfunc uses -nofunction |
| 262 | * extfunc uses -function |
| 263 | */ |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 264 | static void docfunctions(char * filename, char * type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
| 266 | int i,j; |
| 267 | int symcnt = 0; |
| 268 | int idx = 0; |
| 269 | char **vec; |
| 270 | |
| 271 | for (i=0; i <= symfilecnt; i++) |
| 272 | symcnt += symfilelist[i].symbolcnt; |
Johannes Berg | 2e95972 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 273 | vec = malloc((2 + 2 * symcnt + 3) * sizeof(char *)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | if (vec == NULL) { |
| 275 | perror("docproc: "); |
| 276 | exit(1); |
| 277 | } |
| 278 | vec[idx++] = KERNELDOC; |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 279 | vec[idx++] = KERNELDOC_FORMAT; |
Johannes Berg | 2e95972 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 280 | vec[idx++] = NODOCSECTIONS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | for (i=0; i < symfilecnt; i++) { |
| 282 | struct symfile * sym = &symfilelist[i]; |
| 283 | for (j=0; j < sym->symbolcnt; j++) { |
| 284 | vec[idx++] = type; |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 285 | consume_symbol(sym->symbollist[j].name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | vec[idx++] = sym->symbollist[j].name; |
| 287 | } |
| 288 | } |
| 289 | vec[idx++] = filename; |
| 290 | vec[idx] = NULL; |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 291 | if (file_format == FORMAT_RST) |
| 292 | printf(".. %s\n", filename); |
| 293 | else |
| 294 | printf("<!-- %s -->\n", filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | exec_kernel_doc(vec); |
| 296 | fflush(stdout); |
| 297 | free(vec); |
| 298 | } |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 299 | static void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); } |
| 300 | static void extfunc(char * filename) { docfunctions(filename, FUNCTION); } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | /* |
Randy Dunlap | c612093 | 2006-11-02 22:07:01 -0800 | [diff] [blame] | 303 | * Document specific function(s) in a file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | * Call kernel-doc with the following parameters: |
| 305 | * kernel-doc -docbook -function function1 [-function function2] |
| 306 | */ |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 307 | static void singfunc(char * filename, char * line) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
| 309 | char *vec[200]; /* Enough for specific functions */ |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 310 | int i, idx = 0; |
| 311 | int startofsym = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | vec[idx++] = KERNELDOC; |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 313 | vec[idx++] = KERNELDOC_FORMAT; |
Johannes Berg | e946c43a | 2013-11-12 15:11:12 -0800 | [diff] [blame] | 314 | vec[idx++] = SHOWNOTFOUND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 316 | /* Split line up in individual parameters preceded by FUNCTION */ |
| 317 | for (i=0; line[i]; i++) { |
| 318 | if (isspace(line[i])) { |
| 319 | line[i] = '\0'; |
| 320 | startofsym = 1; |
| 321 | continue; |
| 322 | } |
| 323 | if (startofsym) { |
| 324 | startofsym = 0; |
| 325 | vec[idx++] = FUNCTION; |
| 326 | vec[idx++] = &line[i]; |
| 327 | } |
| 328 | } |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 329 | for (i = 0; i < idx; i++) { |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 330 | if (strcmp(vec[i], FUNCTION)) |
| 331 | continue; |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 332 | consume_symbol(vec[i + 1]); |
| 333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | vec[idx++] = filename; |
| 335 | vec[idx] = NULL; |
| 336 | exec_kernel_doc(vec); |
| 337 | } |
| 338 | |
| 339 | /* |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 340 | * Insert specific documentation section from a file. |
| 341 | * Call kernel-doc with the following parameters: |
| 342 | * kernel-doc -docbook -function "doc section" filename |
| 343 | */ |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 344 | static void docsect(char *filename, char *line) |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 345 | { |
Johannes Berg | e946c43a | 2013-11-12 15:11:12 -0800 | [diff] [blame] | 346 | /* kerneldoc -docbook -show-not-found -function "section" file NULL */ |
| 347 | char *vec[7]; |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 348 | char *s; |
| 349 | |
| 350 | for (s = line; *s; s++) |
| 351 | if (*s == '\n') |
| 352 | *s = '\0'; |
| 353 | |
Namhyung Kim | d0f95c7 | 2010-10-22 23:32:10 +0900 | [diff] [blame] | 354 | if (asprintf(&s, "DOC: %s", line) < 0) { |
| 355 | perror("asprintf"); |
| 356 | exit(1); |
| 357 | } |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 358 | consume_symbol(s); |
| 359 | free(s); |
| 360 | |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 361 | vec[0] = KERNELDOC; |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 362 | vec[1] = KERNELDOC_FORMAT; |
Johannes Berg | e946c43a | 2013-11-12 15:11:12 -0800 | [diff] [blame] | 363 | vec[2] = SHOWNOTFOUND; |
| 364 | vec[3] = FUNCTION; |
| 365 | vec[4] = line; |
| 366 | vec[5] = filename; |
| 367 | vec[6] = NULL; |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 368 | exec_kernel_doc(vec); |
| 369 | } |
| 370 | |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 371 | static void find_all_symbols(char *filename) |
| 372 | { |
| 373 | char *vec[4]; /* kerneldoc -list file NULL */ |
| 374 | pid_t pid; |
| 375 | int ret, i, count, start; |
| 376 | char real_filename[PATH_MAX + 1]; |
| 377 | int pipefd[2]; |
| 378 | char *data, *str; |
| 379 | size_t data_len = 0; |
| 380 | |
| 381 | vec[0] = KERNELDOC; |
| 382 | vec[1] = LIST; |
| 383 | vec[2] = filename; |
| 384 | vec[3] = NULL; |
| 385 | |
| 386 | if (pipe(pipefd)) { |
| 387 | perror("pipe"); |
| 388 | exit(1); |
| 389 | } |
| 390 | |
| 391 | switch (pid=fork()) { |
| 392 | case -1: |
| 393 | perror("fork"); |
| 394 | exit(1); |
| 395 | case 0: |
| 396 | close(pipefd[0]); |
| 397 | dup2(pipefd[1], 1); |
| 398 | memset(real_filename, 0, sizeof(real_filename)); |
| 399 | strncat(real_filename, kernsrctree, PATH_MAX); |
| 400 | strncat(real_filename, "/" KERNELDOCPATH KERNELDOC, |
| 401 | PATH_MAX - strlen(real_filename)); |
| 402 | execvp(real_filename, vec); |
| 403 | fprintf(stderr, "exec "); |
| 404 | perror(real_filename); |
| 405 | exit(1); |
| 406 | default: |
| 407 | close(pipefd[1]); |
| 408 | data = malloc(4096); |
| 409 | do { |
| 410 | while ((ret = read(pipefd[0], |
| 411 | data + data_len, |
| 412 | 4096)) > 0) { |
| 413 | data_len += ret; |
| 414 | data = realloc(data, data_len + 4096); |
| 415 | } |
| 416 | } while (ret == -EAGAIN); |
| 417 | if (ret != 0) { |
| 418 | perror("read"); |
| 419 | exit(1); |
| 420 | } |
| 421 | waitpid(pid, &ret ,0); |
| 422 | } |
| 423 | if (WIFEXITED(ret)) |
| 424 | exitstatus |= WEXITSTATUS(ret); |
| 425 | else |
| 426 | exitstatus = 0xff; |
| 427 | |
| 428 | count = 0; |
| 429 | /* poor man's strtok, but with counting */ |
| 430 | for (i = 0; i < data_len; i++) { |
| 431 | if (data[i] == '\n') { |
| 432 | count++; |
| 433 | data[i] = '\0'; |
| 434 | } |
| 435 | } |
| 436 | start = all_list_len; |
| 437 | all_list_len += count; |
| 438 | all_list = realloc(all_list, sizeof(char *) * all_list_len); |
| 439 | str = data; |
| 440 | for (i = 0; i < data_len && start != all_list_len; i++) { |
| 441 | if (data[i] == '\0') { |
| 442 | all_list[start] = str; |
| 443 | str = data + i + 1; |
| 444 | start++; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Jani Nikula | 1dcdad0 | 2016-05-12 16:15:42 +0300 | [diff] [blame] | 449 | /* |
| 450 | * Terminate s at first space, if any. If there was a space, return pointer to |
| 451 | * the character after that. Otherwise, return pointer to the terminating NUL. |
| 452 | */ |
| 453 | static char *chomp(char *s) |
| 454 | { |
| 455 | while (*s && !isspace(*s)) |
| 456 | s++; |
| 457 | |
| 458 | if (*s) |
| 459 | *s++ = '\0'; |
| 460 | |
| 461 | return s; |
| 462 | } |
| 463 | |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 464 | /* Return pointer to directive content, or NULL if not a directive. */ |
| 465 | static char *is_directive(char *line) |
| 466 | { |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 467 | if (file_format == FORMAT_DOCBOOK && line[0] == '!') |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 468 | return line + 1; |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 469 | else if (file_format == FORMAT_RST && !strncmp(line, ".. !", 4)) |
| 470 | return line + 4; |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 471 | |
| 472 | return NULL; |
| 473 | } |
| 474 | |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 475 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | * Parse file, calling action specific functions for: |
| 477 | * 1) Lines containing !E |
| 478 | * 2) Lines containing !I |
| 479 | * 3) Lines containing !D |
| 480 | * 4) Lines containing !F |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 481 | * 5) Lines containing !P |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 482 | * 6) Lines containing !C |
| 483 | * 7) Default lines - lines not matching the above |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | */ |
Trevor Keith | 4356f48 | 2009-09-18 12:49:23 -0700 | [diff] [blame] | 485 | static void parse_file(FILE *infile) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | { |
| 487 | char line[MAXLINESZ]; |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 488 | char *p, *s; |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 489 | while (fgets(line, MAXLINESZ, infile)) { |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 490 | p = is_directive(line); |
| 491 | if (!p) { |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 492 | defaultline(line); |
| 493 | continue; |
| 494 | } |
| 495 | |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 496 | switch (*p++) { |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 497 | case 'E': |
Jani Nikula | 1dcdad0 | 2016-05-12 16:15:42 +0300 | [diff] [blame] | 498 | chomp(p); |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 499 | externalfunctions(p); |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 500 | break; |
| 501 | case 'I': |
Jani Nikula | 1dcdad0 | 2016-05-12 16:15:42 +0300 | [diff] [blame] | 502 | chomp(p); |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 503 | internalfunctions(p); |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 504 | break; |
| 505 | case 'D': |
Jani Nikula | 1dcdad0 | 2016-05-12 16:15:42 +0300 | [diff] [blame] | 506 | chomp(p); |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 507 | symbolsonly(p); |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 508 | break; |
| 509 | case 'F': |
| 510 | /* filename */ |
Jani Nikula | 1dcdad0 | 2016-05-12 16:15:42 +0300 | [diff] [blame] | 511 | s = chomp(p); |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 512 | /* function names */ |
| 513 | while (isspace(*s)) |
| 514 | s++; |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 515 | singlefunctions(p, s); |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 516 | break; |
| 517 | case 'P': |
| 518 | /* filename */ |
Jani Nikula | 1dcdad0 | 2016-05-12 16:15:42 +0300 | [diff] [blame] | 519 | s = chomp(p); |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 520 | /* DOC: section name */ |
| 521 | while (isspace(*s)) |
| 522 | s++; |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 523 | docsection(p, s); |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 524 | break; |
| 525 | case 'C': |
Jani Nikula | 1dcdad0 | 2016-05-12 16:15:42 +0300 | [diff] [blame] | 526 | chomp(p); |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 527 | if (findall) |
Jani Nikula | a48dc45 | 2016-05-12 16:15:41 +0300 | [diff] [blame] | 528 | findall(p); |
Jani Nikula | 868fb19 | 2016-05-12 16:15:40 +0300 | [diff] [blame] | 529 | break; |
| 530 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | defaultline(line); |
| 532 | } |
| 533 | } |
| 534 | fflush(stdout); |
| 535 | } |
| 536 | |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 537 | /* |
| 538 | * Is this a RestructuredText template? Answer the question by seeing if its |
| 539 | * name ends in ".rst". |
| 540 | */ |
| 541 | static int is_rst(const char *file) |
| 542 | { |
| 543 | char *dot = strrchr(file, '.'); |
| 544 | |
| 545 | return dot && !strcmp(dot + 1, "rst"); |
| 546 | } |
| 547 | |
| 548 | enum opts { |
| 549 | OPT_DOCBOOK, |
| 550 | OPT_RST, |
| 551 | OPT_HELP, |
| 552 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
| 554 | int main(int argc, char *argv[]) |
| 555 | { |
Jani Nikula | 568fb2d | 2016-05-12 16:15:39 +0300 | [diff] [blame] | 556 | const char *subcommand, *filename; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | FILE * infile; |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 558 | int i; |
Rob Landley | bb13be5 | 2007-10-09 01:25:18 -0500 | [diff] [blame] | 559 | |
| 560 | srctree = getenv("SRCTREE"); |
| 561 | if (!srctree) |
| 562 | srctree = getcwd(NULL, 0); |
Jiri Slaby | 2d51005 | 2009-05-31 18:05:34 +0200 | [diff] [blame] | 563 | kernsrctree = getenv("KBUILD_SRC"); |
Amerigo Wang | b767b90 | 2009-06-19 03:06:54 -0400 | [diff] [blame] | 564 | if (!kernsrctree || !*kernsrctree) |
Jiri Slaby | 2d51005 | 2009-05-31 18:05:34 +0200 | [diff] [blame] | 565 | kernsrctree = srctree; |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 566 | |
| 567 | for (;;) { |
| 568 | int c; |
| 569 | struct option opts[] = { |
| 570 | { "docbook", no_argument, NULL, OPT_DOCBOOK }, |
| 571 | { "rst", no_argument, NULL, OPT_RST }, |
| 572 | { "help", no_argument, NULL, OPT_HELP }, |
| 573 | {} |
| 574 | }; |
| 575 | |
| 576 | c = getopt_long_only(argc, argv, "", opts, NULL); |
| 577 | if (c == -1) |
| 578 | break; |
| 579 | |
| 580 | switch (c) { |
| 581 | case OPT_DOCBOOK: |
| 582 | file_format = FORMAT_DOCBOOK; |
| 583 | break; |
| 584 | case OPT_RST: |
| 585 | file_format = FORMAT_RST; |
| 586 | break; |
| 587 | case OPT_HELP: |
| 588 | usage(); |
| 589 | return 0; |
| 590 | default: |
| 591 | case '?': |
| 592 | usage(); |
| 593 | return 1; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | argc -= optind; |
| 598 | argv += optind; |
| 599 | |
| 600 | if (argc != 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | usage(); |
| 602 | exit(1); |
| 603 | } |
Jani Nikula | 568fb2d | 2016-05-12 16:15:39 +0300 | [diff] [blame] | 604 | |
Jani Nikula | 064669b | 2016-05-12 16:15:43 +0300 | [diff] [blame] | 605 | subcommand = argv[0]; |
| 606 | filename = argv[1]; |
| 607 | |
| 608 | if (file_format == FORMAT_AUTO) |
| 609 | file_format = is_rst(filename) ? FORMAT_RST : FORMAT_DOCBOOK; |
Jani Nikula | 568fb2d | 2016-05-12 16:15:39 +0300 | [diff] [blame] | 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /* Open file, exit on error */ |
Jani Nikula | 568fb2d | 2016-05-12 16:15:39 +0300 | [diff] [blame] | 612 | infile = fopen(filename, "r"); |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 613 | if (infile == NULL) { |
| 614 | fprintf(stderr, "docproc: "); |
Jani Nikula | 568fb2d | 2016-05-12 16:15:39 +0300 | [diff] [blame] | 615 | perror(filename); |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 616 | exit(2); |
| 617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Jani Nikula | 568fb2d | 2016-05-12 16:15:39 +0300 | [diff] [blame] | 619 | if (strcmp("doc", subcommand) == 0) { |
Jani Nikula | 0e95abf | 2016-05-12 16:15:44 +0300 | [diff] [blame] | 620 | if (file_format == FORMAT_RST) { |
| 621 | time_t t = time(NULL); |
| 622 | printf(".. generated from %s by docproc %s\n", |
| 623 | filename, ctime(&t)); |
| 624 | } |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | /* Need to do this in two passes. |
| 627 | * First pass is used to collect all symbols exported |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 628 | * in the various files; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | * Second pass generate the documentation. |
Randy Dunlap | 6dd16f4 | 2007-09-04 21:23:22 -0700 | [diff] [blame] | 630 | * This is required because some functions are declared |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | * and exported in different files :-(( |
| 632 | */ |
| 633 | /* Collect symbols */ |
| 634 | defaultline = noaction; |
| 635 | internalfunctions = find_export_symbols; |
| 636 | externalfunctions = find_export_symbols; |
| 637 | symbolsonly = find_export_symbols; |
| 638 | singlefunctions = noaction2; |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 639 | docsection = noaction2; |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 640 | findall = find_all_symbols; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | parse_file(infile); |
| 642 | |
| 643 | /* Rewind to start from beginning of file again */ |
| 644 | fseek(infile, 0, SEEK_SET); |
| 645 | defaultline = printline; |
| 646 | internalfunctions = intfunc; |
| 647 | externalfunctions = extfunc; |
| 648 | symbolsonly = printline; |
| 649 | singlefunctions = singfunc; |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 650 | docsection = docsect; |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 651 | findall = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | parse_file(infile); |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 654 | |
| 655 | for (i = 0; i < all_list_len; i++) { |
| 656 | if (!all_list[i]) |
| 657 | continue; |
| 658 | fprintf(stderr, "Warning: didn't use docs for %s\n", |
| 659 | all_list[i]); |
| 660 | } |
Jani Nikula | 568fb2d | 2016-05-12 16:15:39 +0300 | [diff] [blame] | 661 | } else if (strcmp("depend", subcommand) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | /* Create first part of dependency chain |
| 663 | * file.tmpl */ |
Jani Nikula | 568fb2d | 2016-05-12 16:15:39 +0300 | [diff] [blame] | 664 | printf("%s\t", filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | defaultline = noaction; |
| 666 | internalfunctions = adddep; |
| 667 | externalfunctions = adddep; |
| 668 | symbolsonly = adddep; |
| 669 | singlefunctions = adddep2; |
Johannes Berg | e662af4 | 2007-10-24 15:08:48 -0700 | [diff] [blame] | 670 | docsection = adddep2; |
Johannes Berg | eda603f | 2010-09-11 15:55:22 -0700 | [diff] [blame] | 671 | findall = adddep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | parse_file(infile); |
| 673 | printf("\n"); |
Jesper Juhl | f0f3ca8 | 2011-06-15 11:53:13 +0200 | [diff] [blame] | 674 | } else { |
Jani Nikula | 568fb2d | 2016-05-12 16:15:39 +0300 | [diff] [blame] | 675 | fprintf(stderr, "Unknown option: %s\n", subcommand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | exit(1); |
| 677 | } |
| 678 | fclose(infile); |
| 679 | fflush(stdout); |
| 680 | return exitstatus; |
| 681 | } |