| 1 | /* Generate doc-string file for GNU Emacs from source files. |
| 2 | Copyright (C) 1985, 1986, 92, 93, 94, 1997 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Emacs. |
| 5 | |
| 6 | GNU Emacs is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to |
| 18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | /* The arguments given to this program are all the C and Lisp source files |
| 22 | of GNU Emacs. .elc and .el and .c files are allowed. |
| 23 | A .o file can also be specified; the .c file it was made from is used. |
| 24 | This helps the makefile pass the correct list of files. |
| 25 | |
| 26 | The results, which go to standard output or to a file |
| 27 | specified with -a or -o (-a to append, -o to start from nothing), |
| 28 | are entries containing function or variable names and their documentation. |
| 29 | Each entry starts with a ^_ character. |
| 30 | Then comes F for a function or V for a variable. |
| 31 | Then comes the function or variable name, terminated with a newline. |
| 32 | Then comes the documentation for that function or variable. |
| 33 | */ |
| 34 | |
| 35 | #define NO_SHORTNAMES /* Tell config not to load remap.h */ |
| 36 | #include <../src/config.h> |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #ifdef MSDOS |
| 40 | #include <fcntl.h> |
| 41 | #endif /* MSDOS */ |
| 42 | #ifdef WINDOWSNT |
| 43 | #include <stdlib.h> |
| 44 | #include <fcntl.h> |
| 45 | #include <direct.h> |
| 46 | #endif /* WINDOWSNT */ |
| 47 | |
| 48 | #ifdef DOS_NT |
| 49 | #define READ_TEXT "rt" |
| 50 | #define READ_BINARY "rb" |
| 51 | #else /* not DOS_NT */ |
| 52 | #define READ_TEXT "r" |
| 53 | #define READ_BINARY "r" |
| 54 | #endif /* not DOS_NT */ |
| 55 | |
| 56 | int scan_file (); |
| 57 | int scan_lisp_file (); |
| 58 | int scan_c_file (); |
| 59 | |
| 60 | #ifdef MSDOS |
| 61 | /* s/msdos.h defines this as sys_chdir, but we're not linking with the |
| 62 | file where that function is defined. */ |
| 63 | #undef chdir |
| 64 | #endif |
| 65 | |
| 66 | #ifdef HAVE_UNISTD_H |
| 67 | #include <unistd.h> |
| 68 | #endif |
| 69 | |
| 70 | /* Stdio stream for output to the DOC file. */ |
| 71 | FILE *outfile; |
| 72 | |
| 73 | /* Name this program was invoked with. */ |
| 74 | char *progname; |
| 75 | |
| 76 | /* Print error message. `s1' is printf control string, `s2' is arg for it. */ |
| 77 | |
| 78 | /* VARARGS1 */ |
| 79 | void |
| 80 | error (s1, s2) |
| 81 | char *s1, *s2; |
| 82 | { |
| 83 | fprintf (stderr, "%s: ", progname); |
| 84 | fprintf (stderr, s1, s2); |
| 85 | fprintf (stderr, "\n"); |
| 86 | } |
| 87 | |
| 88 | /* Print error message and exit. */ |
| 89 | |
| 90 | /* VARARGS1 */ |
| 91 | void |
| 92 | fatal (s1, s2) |
| 93 | char *s1, *s2; |
| 94 | { |
| 95 | error (s1, s2); |
| 96 | exit (1); |
| 97 | } |
| 98 | |
| 99 | /* Like malloc but get fatal error if memory is exhausted. */ |
| 100 | |
| 101 | long * |
| 102 | xmalloc (size) |
| 103 | unsigned int size; |
| 104 | { |
| 105 | long *result = (long *) malloc (size); |
| 106 | if (result == NULL) |
| 107 | fatal ("virtual memory exhausted", 0); |
| 108 | return result; |
| 109 | } |
| 110 | \f |
| 111 | int |
| 112 | main (argc, argv) |
| 113 | int argc; |
| 114 | char **argv; |
| 115 | { |
| 116 | int i; |
| 117 | int err_count = 0; |
| 118 | int first_infile; |
| 119 | |
| 120 | progname = argv[0]; |
| 121 | |
| 122 | outfile = stdout; |
| 123 | |
| 124 | /* Don't put CRs in the DOC file. */ |
| 125 | #ifdef MSDOS |
| 126 | _fmode = O_BINARY; |
| 127 | #if 0 /* Suspicion is that this causes hanging. |
| 128 | So instead we require people to use -o on MSDOS. */ |
| 129 | (stdout)->_flag &= ~_IOTEXT; |
| 130 | _setmode (fileno (stdout), O_BINARY); |
| 131 | #endif |
| 132 | outfile = 0; |
| 133 | #endif /* MSDOS */ |
| 134 | #ifdef WINDOWSNT |
| 135 | _fmode = O_BINARY; |
| 136 | _setmode (fileno (stdout), O_BINARY); |
| 137 | #endif /* WINDOWSNT */ |
| 138 | |
| 139 | /* If first two args are -o FILE, output to FILE. */ |
| 140 | i = 1; |
| 141 | if (argc > i + 1 && !strcmp (argv[i], "-o")) |
| 142 | { |
| 143 | outfile = fopen (argv[i + 1], "w"); |
| 144 | i += 2; |
| 145 | } |
| 146 | if (argc > i + 1 && !strcmp (argv[i], "-a")) |
| 147 | { |
| 148 | outfile = fopen (argv[i + 1], "a"); |
| 149 | i += 2; |
| 150 | } |
| 151 | if (argc > i + 1 && !strcmp (argv[i], "-d")) |
| 152 | { |
| 153 | chdir (argv[i + 1]); |
| 154 | i += 2; |
| 155 | } |
| 156 | |
| 157 | if (outfile == 0) |
| 158 | fatal ("No output file specified", ""); |
| 159 | |
| 160 | first_infile = i; |
| 161 | for (; i < argc; i++) |
| 162 | { |
| 163 | int j; |
| 164 | /* Don't process one file twice. */ |
| 165 | for (j = first_infile; j < i; j++) |
| 166 | if (! strcmp (argv[i], argv[j])) |
| 167 | break; |
| 168 | if (j == i) |
| 169 | err_count += scan_file (argv[i]); |
| 170 | } |
| 171 | #ifndef VMS |
| 172 | exit (err_count > 0); |
| 173 | #endif /* VMS */ |
| 174 | return err_count > 0; |
| 175 | } |
| 176 | |
| 177 | /* Read file FILENAME and output its doc strings to outfile. */ |
| 178 | /* Return 1 if file is not found, 0 if it is found. */ |
| 179 | |
| 180 | int |
| 181 | scan_file (filename) |
| 182 | char *filename; |
| 183 | { |
| 184 | int len = strlen (filename); |
| 185 | if (len > 4 && !strcmp (filename + len - 4, ".elc")) |
| 186 | return scan_lisp_file (filename, READ_BINARY); |
| 187 | else if (len > 3 && !strcmp (filename + len - 3, ".el")) |
| 188 | return scan_lisp_file (filename, READ_TEXT); |
| 189 | else |
| 190 | return scan_c_file (filename, READ_TEXT); |
| 191 | } |
| 192 | \f |
| 193 | char buf[128]; |
| 194 | |
| 195 | /* Skip a C string from INFILE, |
| 196 | and return the character that follows the closing ". |
| 197 | If printflag is positive, output string contents to outfile. |
| 198 | If it is negative, store contents in buf. |
| 199 | Convert escape sequences \n and \t to newline and tab; |
| 200 | discard \ followed by newline. */ |
| 201 | |
| 202 | int |
| 203 | read_c_string (infile, printflag) |
| 204 | FILE *infile; |
| 205 | int printflag; |
| 206 | { |
| 207 | register int c; |
| 208 | char *p = buf; |
| 209 | |
| 210 | c = getc (infile); |
| 211 | while (c != EOF) |
| 212 | { |
| 213 | while (c != '"' && c != EOF) |
| 214 | { |
| 215 | if (c == '\\') |
| 216 | { |
| 217 | c = getc (infile); |
| 218 | if (c == '\n') |
| 219 | { |
| 220 | c = getc (infile); |
| 221 | continue; |
| 222 | } |
| 223 | if (c == 'n') |
| 224 | c = '\n'; |
| 225 | if (c == 't') |
| 226 | c = '\t'; |
| 227 | } |
| 228 | if (printflag > 0) |
| 229 | putc (c, outfile); |
| 230 | else if (printflag < 0) |
| 231 | *p++ = c; |
| 232 | c = getc (infile); |
| 233 | } |
| 234 | c = getc (infile); |
| 235 | if (c != '"') |
| 236 | break; |
| 237 | /* If we had a "", concatenate the two strings. */ |
| 238 | c = getc (infile); |
| 239 | } |
| 240 | |
| 241 | if (printflag < 0) |
| 242 | *p = 0; |
| 243 | |
| 244 | return c; |
| 245 | } |
| 246 | \f |
| 247 | /* Write to file OUT the argument names of function FUNC, whose text is in BUF. |
| 248 | MINARGS and MAXARGS are the minimum and maximum number of arguments. */ |
| 249 | |
| 250 | void |
| 251 | write_c_args (out, func, buf, minargs, maxargs) |
| 252 | FILE *out; |
| 253 | char *func, *buf; |
| 254 | int minargs, maxargs; |
| 255 | { |
| 256 | register char *p; |
| 257 | int in_ident = 0; |
| 258 | int just_spaced = 0; |
| 259 | int need_space = 1; |
| 260 | |
| 261 | fprintf (out, "(%s", func); |
| 262 | |
| 263 | if (*buf == '(') |
| 264 | ++buf; |
| 265 | |
| 266 | for (p = buf; *p; p++) |
| 267 | { |
| 268 | char c = *p; |
| 269 | int ident_start = 0; |
| 270 | |
| 271 | /* Notice when we start printing a new identifier. */ |
| 272 | if ((('A' <= c && c <= 'Z') |
| 273 | || ('a' <= c && c <= 'z') |
| 274 | || ('0' <= c && c <= '9') |
| 275 | || c == '_') |
| 276 | != in_ident) |
| 277 | { |
| 278 | if (!in_ident) |
| 279 | { |
| 280 | in_ident = 1; |
| 281 | ident_start = 1; |
| 282 | |
| 283 | if (need_space) |
| 284 | putc (' ', out); |
| 285 | |
| 286 | if (minargs == 0 && maxargs > 0) |
| 287 | fprintf (out, "&optional "); |
| 288 | just_spaced = 1; |
| 289 | |
| 290 | minargs--; |
| 291 | maxargs--; |
| 292 | } |
| 293 | else |
| 294 | in_ident = 0; |
| 295 | } |
| 296 | |
| 297 | /* Print the C argument list as it would appear in lisp: |
| 298 | print underscores as hyphens, and print commas as spaces. |
| 299 | Collapse adjacent spaces into one. */ |
| 300 | if (c == '_') c = '-'; |
| 301 | if (c == ',') c = ' '; |
| 302 | |
| 303 | /* In C code, `default' is a reserved word, so we spell it |
| 304 | `defalt'; unmangle that here. */ |
| 305 | if (ident_start |
| 306 | && strncmp (p, "defalt", 6) == 0 |
| 307 | && ! (('A' <= p[6] && p[6] <= 'Z') |
| 308 | || ('a' <= p[6] && p[6] <= 'z') |
| 309 | || ('0' <= p[6] && p[6] <= '9') |
| 310 | || p[6] == '_')) |
| 311 | { |
| 312 | fprintf (out, "DEFAULT"); |
| 313 | p += 5; |
| 314 | in_ident = 0; |
| 315 | just_spaced = 0; |
| 316 | } |
| 317 | else if (c != ' ' || ! just_spaced) |
| 318 | { |
| 319 | if (c >= 'a' && c <= 'z') |
| 320 | /* Upcase the letter. */ |
| 321 | c += 'A' - 'a'; |
| 322 | putc (c, out); |
| 323 | } |
| 324 | |
| 325 | just_spaced = (c == ' '); |
| 326 | need_space = 0; |
| 327 | } |
| 328 | } |
| 329 | \f |
| 330 | /* Read through a c file. If a .o file is named, |
| 331 | the corresponding .c file is read instead. |
| 332 | Looks for DEFUN constructs such as are defined in ../src/lisp.h. |
| 333 | Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED. */ |
| 334 | |
| 335 | int |
| 336 | scan_c_file (filename, mode) |
| 337 | char *filename, *mode; |
| 338 | { |
| 339 | FILE *infile; |
| 340 | register int c; |
| 341 | register int commas; |
| 342 | register int defunflag; |
| 343 | register int defvarperbufferflag; |
| 344 | register int defvarflag; |
| 345 | int minargs, maxargs; |
| 346 | int extension = filename[strlen (filename) - 1]; |
| 347 | |
| 348 | if (extension == 'o') |
| 349 | filename[strlen (filename) - 1] = 'c'; |
| 350 | |
| 351 | infile = fopen (filename, mode); |
| 352 | |
| 353 | /* No error if non-ex input file */ |
| 354 | if (infile == NULL) |
| 355 | { |
| 356 | perror (filename); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | /* Reset extension to be able to detect duplicate files. */ |
| 361 | filename[strlen (filename) - 1] = extension; |
| 362 | |
| 363 | c = '\n'; |
| 364 | while (!feof (infile)) |
| 365 | { |
| 366 | if (c != '\n') |
| 367 | { |
| 368 | c = getc (infile); |
| 369 | continue; |
| 370 | } |
| 371 | c = getc (infile); |
| 372 | if (c == ' ') |
| 373 | { |
| 374 | while (c == ' ') |
| 375 | c = getc (infile); |
| 376 | if (c != 'D') |
| 377 | continue; |
| 378 | c = getc (infile); |
| 379 | if (c != 'E') |
| 380 | continue; |
| 381 | c = getc (infile); |
| 382 | if (c != 'F') |
| 383 | continue; |
| 384 | c = getc (infile); |
| 385 | if (c != 'V') |
| 386 | continue; |
| 387 | c = getc (infile); |
| 388 | if (c != 'A') |
| 389 | continue; |
| 390 | c = getc (infile); |
| 391 | if (c != 'R') |
| 392 | continue; |
| 393 | c = getc (infile); |
| 394 | if (c != '_') |
| 395 | continue; |
| 396 | |
| 397 | defvarflag = 1; |
| 398 | defunflag = 0; |
| 399 | |
| 400 | c = getc (infile); |
| 401 | defvarperbufferflag = (c == 'P'); |
| 402 | |
| 403 | c = getc (infile); |
| 404 | } |
| 405 | else if (c == 'D') |
| 406 | { |
| 407 | c = getc (infile); |
| 408 | if (c != 'E') |
| 409 | continue; |
| 410 | c = getc (infile); |
| 411 | if (c != 'F') |
| 412 | continue; |
| 413 | c = getc (infile); |
| 414 | defunflag = c == 'U'; |
| 415 | defvarflag = 0; |
| 416 | } |
| 417 | else continue; |
| 418 | |
| 419 | while (c != '(') |
| 420 | { |
| 421 | if (c < 0) |
| 422 | goto eof; |
| 423 | c = getc (infile); |
| 424 | } |
| 425 | |
| 426 | c = getc (infile); |
| 427 | if (c != '"') |
| 428 | continue; |
| 429 | c = read_c_string (infile, -1); |
| 430 | |
| 431 | if (defunflag) |
| 432 | commas = 5; |
| 433 | else if (defvarperbufferflag) |
| 434 | commas = 2; |
| 435 | else if (defvarflag) |
| 436 | commas = 1; |
| 437 | else /* For DEFSIMPLE and DEFPRED */ |
| 438 | commas = 2; |
| 439 | |
| 440 | while (commas) |
| 441 | { |
| 442 | if (c == ',') |
| 443 | { |
| 444 | commas--; |
| 445 | if (defunflag && (commas == 1 || commas == 2)) |
| 446 | { |
| 447 | do |
| 448 | c = getc (infile); |
| 449 | while (c == ' ' || c == '\n' || c == '\t'); |
| 450 | if (c < 0) |
| 451 | goto eof; |
| 452 | ungetc (c, infile); |
| 453 | if (commas == 2) /* pick up minargs */ |
| 454 | fscanf (infile, "%d", &minargs); |
| 455 | else /* pick up maxargs */ |
| 456 | if (c == 'M' || c == 'U') /* MANY || UNEVALLED */ |
| 457 | maxargs = -1; |
| 458 | else |
| 459 | fscanf (infile, "%d", &maxargs); |
| 460 | } |
| 461 | } |
| 462 | if (c < 0) |
| 463 | goto eof; |
| 464 | c = getc (infile); |
| 465 | } |
| 466 | while (c == ' ' || c == '\n' || c == '\t') |
| 467 | c = getc (infile); |
| 468 | if (c == '"') |
| 469 | c = read_c_string (infile, 0); |
| 470 | while (c != ',') |
| 471 | c = getc (infile); |
| 472 | c = getc (infile); |
| 473 | while (c == ' ' || c == '\n' || c == '\t') |
| 474 | c = getc (infile); |
| 475 | |
| 476 | if (c == '"') |
| 477 | { |
| 478 | putc (037, outfile); |
| 479 | putc (defvarflag ? 'V' : 'F', outfile); |
| 480 | fprintf (outfile, "%s\n", buf); |
| 481 | c = read_c_string (infile, 1); |
| 482 | |
| 483 | /* If this is a defun, find the arguments and print them. If |
| 484 | this function takes MANY or UNEVALLED args, then the C source |
| 485 | won't give the names of the arguments, so we shouldn't bother |
| 486 | trying to find them. */ |
| 487 | if (defunflag && maxargs != -1) |
| 488 | { |
| 489 | char argbuf[1024], *p = argbuf; |
| 490 | while (c != ')') |
| 491 | { |
| 492 | if (c < 0) |
| 493 | goto eof; |
| 494 | c = getc (infile); |
| 495 | } |
| 496 | /* Skip into arguments. */ |
| 497 | while (c != '(') |
| 498 | { |
| 499 | if (c < 0) |
| 500 | goto eof; |
| 501 | c = getc (infile); |
| 502 | } |
| 503 | /* Copy arguments into ARGBUF. */ |
| 504 | *p++ = c; |
| 505 | do |
| 506 | *p++ = c = getc (infile); |
| 507 | while (c != ')'); |
| 508 | *p = '\0'; |
| 509 | /* Output them. */ |
| 510 | fprintf (outfile, "\n\n"); |
| 511 | write_c_args (outfile, buf, argbuf, minargs, maxargs); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | eof: |
| 516 | fclose (infile); |
| 517 | return 0; |
| 518 | } |
| 519 | \f |
| 520 | /* Read a file of Lisp code, compiled or interpreted. |
| 521 | Looks for |
| 522 | (defun NAME ARGS DOCSTRING ...) |
| 523 | (defmacro NAME ARGS DOCSTRING ...) |
| 524 | (autoload (quote NAME) FILE DOCSTRING ...) |
| 525 | (defvar NAME VALUE DOCSTRING) |
| 526 | (defconst NAME VALUE DOCSTRING) |
| 527 | (fset (quote NAME) (make-byte-code ... DOCSTRING ...)) |
| 528 | (fset (quote NAME) #[... DOCSTRING ...]) |
| 529 | (defalias (quote NAME) #[... DOCSTRING ...]) |
| 530 | (custom-declare-variable (quote NAME) VALUE DOCSTRING ...) |
| 531 | starting in column zero. |
| 532 | (quote NAME) may appear as 'NAME as well. |
| 533 | |
| 534 | We also look for #@LENGTH CONTENTS^_ at the beginning of the line. |
| 535 | When we find that, we save it for the following defining-form, |
| 536 | and we use that instead of reading a doc string within that defining-form. |
| 537 | |
| 538 | For defun, defmacro, and autoload, we know how to skip over the arglist. |
| 539 | For defvar, defconst, and fset we skip to the docstring with a kludgy |
| 540 | formatting convention: all docstrings must appear on the same line as the |
| 541 | initial open-paren (the one in column zero) and must contain a backslash |
| 542 | and a double-quote immediately after the initial double-quote. No newlines |
| 543 | must appear between the beginning of the form and the first double-quote. |
| 544 | The only source file that must follow this convention is loaddefs.el; aside |
| 545 | from that, it is always the .elc file that we look at, and they are no |
| 546 | problem because byte-compiler output follows this convention. |
| 547 | The NAME and DOCSTRING are output. |
| 548 | NAME is preceded by `F' for a function or `V' for a variable. |
| 549 | An entry is output only if DOCSTRING has \ newline just after the opening " |
| 550 | */ |
| 551 | |
| 552 | void |
| 553 | skip_white (infile) |
| 554 | FILE *infile; |
| 555 | { |
| 556 | char c = ' '; |
| 557 | while (c == ' ' || c == '\t' || c == '\n') |
| 558 | c = getc (infile); |
| 559 | ungetc (c, infile); |
| 560 | } |
| 561 | |
| 562 | void |
| 563 | read_lisp_symbol (infile, buffer) |
| 564 | FILE *infile; |
| 565 | char *buffer; |
| 566 | { |
| 567 | char c; |
| 568 | char *fillp = buffer; |
| 569 | |
| 570 | skip_white (infile); |
| 571 | while (1) |
| 572 | { |
| 573 | c = getc (infile); |
| 574 | if (c == '\\') |
| 575 | *(++fillp) = getc (infile); |
| 576 | else if (c == ' ' || c == '\t' || c == '\n' || c == '(' || c == ')') |
| 577 | { |
| 578 | ungetc (c, infile); |
| 579 | *fillp = 0; |
| 580 | break; |
| 581 | } |
| 582 | else |
| 583 | *fillp++ = c; |
| 584 | } |
| 585 | |
| 586 | if (! buffer[0]) |
| 587 | fprintf (stderr, "## expected a symbol, got '%c'\n", c); |
| 588 | |
| 589 | skip_white (infile); |
| 590 | } |
| 591 | |
| 592 | int |
| 593 | scan_lisp_file (filename, mode) |
| 594 | char *filename, *mode; |
| 595 | { |
| 596 | FILE *infile; |
| 597 | register int c; |
| 598 | char *saved_string = 0; |
| 599 | |
| 600 | infile = fopen (filename, mode); |
| 601 | if (infile == NULL) |
| 602 | { |
| 603 | perror (filename); |
| 604 | return 0; /* No error */ |
| 605 | } |
| 606 | |
| 607 | c = '\n'; |
| 608 | while (!feof (infile)) |
| 609 | { |
| 610 | char buffer[BUFSIZ]; |
| 611 | char type; |
| 612 | |
| 613 | if (c != '\n') |
| 614 | { |
| 615 | c = getc (infile); |
| 616 | continue; |
| 617 | } |
| 618 | c = getc (infile); |
| 619 | /* Detect a dynamic doc string and save it for the next expression. */ |
| 620 | if (c == '#') |
| 621 | { |
| 622 | c = getc (infile); |
| 623 | if (c == '@') |
| 624 | { |
| 625 | int length = 0; |
| 626 | int i; |
| 627 | |
| 628 | /* Read the length. */ |
| 629 | while ((c = getc (infile), |
| 630 | c >= '0' && c <= '9')) |
| 631 | { |
| 632 | length *= 10; |
| 633 | length += c - '0'; |
| 634 | } |
| 635 | |
| 636 | /* The next character is a space that is counted in the length |
| 637 | but not part of the doc string. |
| 638 | We already read it, so just ignore it. */ |
| 639 | length--; |
| 640 | |
| 641 | /* Read in the contents. */ |
| 642 | if (saved_string != 0) |
| 643 | free (saved_string); |
| 644 | saved_string = (char *) malloc (length); |
| 645 | for (i = 0; i < length; i++) |
| 646 | saved_string[i] = getc (infile); |
| 647 | /* The last character is a ^_. |
| 648 | That is needed in the .elc file |
| 649 | but it is redundant in DOC. So get rid of it here. */ |
| 650 | saved_string[length - 1] = 0; |
| 651 | /* Skip the newline. */ |
| 652 | c = getc (infile); |
| 653 | while (c != '\n') |
| 654 | c = getc (infile); |
| 655 | } |
| 656 | continue; |
| 657 | } |
| 658 | |
| 659 | if (c != '(') |
| 660 | continue; |
| 661 | |
| 662 | read_lisp_symbol (infile, buffer); |
| 663 | |
| 664 | if (! strcmp (buffer, "defun") || |
| 665 | ! strcmp (buffer, "defmacro")) |
| 666 | { |
| 667 | type = 'F'; |
| 668 | read_lisp_symbol (infile, buffer); |
| 669 | |
| 670 | /* Skip the arguments: either "nil" or a list in parens */ |
| 671 | |
| 672 | c = getc (infile); |
| 673 | if (c == 'n') /* nil */ |
| 674 | { |
| 675 | if ((c = getc (infile)) != 'i' || |
| 676 | (c = getc (infile)) != 'l') |
| 677 | { |
| 678 | fprintf (stderr, "## unparsable arglist in %s (%s)\n", |
| 679 | buffer, filename); |
| 680 | continue; |
| 681 | } |
| 682 | } |
| 683 | else if (c != '(') |
| 684 | { |
| 685 | fprintf (stderr, "## unparsable arglist in %s (%s)\n", |
| 686 | buffer, filename); |
| 687 | continue; |
| 688 | } |
| 689 | else |
| 690 | while (c != ')') |
| 691 | c = getc (infile); |
| 692 | skip_white (infile); |
| 693 | |
| 694 | /* If the next three characters aren't `dquote bslash newline' |
| 695 | then we're not reading a docstring. |
| 696 | */ |
| 697 | if ((c = getc (infile)) != '"' || |
| 698 | (c = getc (infile)) != '\\' || |
| 699 | (c = getc (infile)) != '\n') |
| 700 | { |
| 701 | #ifdef DEBUG |
| 702 | fprintf (stderr, "## non-docstring in %s (%s)\n", |
| 703 | buffer, filename); |
| 704 | #endif |
| 705 | continue; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | else if (! strcmp (buffer, "defvar") || |
| 710 | ! strcmp (buffer, "defconst")) |
| 711 | { |
| 712 | char c1 = 0, c2 = 0; |
| 713 | type = 'V'; |
| 714 | read_lisp_symbol (infile, buffer); |
| 715 | |
| 716 | if (saved_string == 0) |
| 717 | { |
| 718 | |
| 719 | /* Skip until the first newline; remember the two previous chars. */ |
| 720 | while (c != '\n' && c >= 0) |
| 721 | { |
| 722 | c2 = c1; |
| 723 | c1 = c; |
| 724 | c = getc (infile); |
| 725 | } |
| 726 | |
| 727 | /* If two previous characters were " and \, |
| 728 | this is a doc string. Otherwise, there is none. */ |
| 729 | if (c2 != '"' || c1 != '\\') |
| 730 | { |
| 731 | #ifdef DEBUG |
| 732 | fprintf (stderr, "## non-docstring in %s (%s)\n", |
| 733 | buffer, filename); |
| 734 | #endif |
| 735 | continue; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | else if (! strcmp (buffer, "custom-declare-variable")) |
| 741 | { |
| 742 | char c1 = 0, c2 = 0; |
| 743 | type = 'V'; |
| 744 | |
| 745 | c = getc (infile); |
| 746 | if (c == '\'') |
| 747 | read_lisp_symbol (infile, buffer); |
| 748 | else |
| 749 | { |
| 750 | if (c != '(') |
| 751 | { |
| 752 | fprintf (stderr, |
| 753 | "## unparsable name in custom-declare-variable in %s\n", |
| 754 | filename); |
| 755 | continue; |
| 756 | } |
| 757 | read_lisp_symbol (infile, buffer); |
| 758 | if (strcmp (buffer, "quote")) |
| 759 | { |
| 760 | fprintf (stderr, |
| 761 | "## unparsable name in custom-declare-variable in %s\n", |
| 762 | filename); |
| 763 | continue; |
| 764 | } |
| 765 | read_lisp_symbol (infile, buffer); |
| 766 | c = getc (infile); |
| 767 | if (c != ')') |
| 768 | { |
| 769 | fprintf (stderr, |
| 770 | "## unparsable quoted name in custom-declare-variable in %s\n", |
| 771 | filename); |
| 772 | continue; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | if (saved_string == 0) |
| 777 | { |
| 778 | /* Skip until the first newline; remember the two previous |
| 779 | chars. */ |
| 780 | while (c != '\n' && c >= 0) |
| 781 | { |
| 782 | c2 = c1; |
| 783 | c1 = c; |
| 784 | c = getc (infile); |
| 785 | } |
| 786 | |
| 787 | /* If two previous characters were " and \, |
| 788 | this is a doc string. Otherwise, there is none. */ |
| 789 | if (c2 != '"' || c1 != '\\') |
| 790 | { |
| 791 | #ifdef DEBUG |
| 792 | fprintf (stderr, "## non-docstring in %s (%s)\n", |
| 793 | buffer, filename); |
| 794 | #endif |
| 795 | continue; |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | else if (! strcmp (buffer, "fset") || ! strcmp (buffer, "defalias")) |
| 801 | { |
| 802 | char c1 = 0, c2 = 0; |
| 803 | type = 'F'; |
| 804 | |
| 805 | c = getc (infile); |
| 806 | if (c == '\'') |
| 807 | read_lisp_symbol (infile, buffer); |
| 808 | else |
| 809 | { |
| 810 | if (c != '(') |
| 811 | { |
| 812 | fprintf (stderr, "## unparsable name in fset in %s\n", |
| 813 | filename); |
| 814 | continue; |
| 815 | } |
| 816 | read_lisp_symbol (infile, buffer); |
| 817 | if (strcmp (buffer, "quote")) |
| 818 | { |
| 819 | fprintf (stderr, "## unparsable name in fset in %s\n", |
| 820 | filename); |
| 821 | continue; |
| 822 | } |
| 823 | read_lisp_symbol (infile, buffer); |
| 824 | c = getc (infile); |
| 825 | if (c != ')') |
| 826 | { |
| 827 | fprintf (stderr, |
| 828 | "## unparsable quoted name in fset in %s\n", |
| 829 | filename); |
| 830 | continue; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | if (saved_string == 0) |
| 835 | { |
| 836 | /* Skip until the first newline; remember the two previous chars. */ |
| 837 | while (c != '\n' && c >= 0) |
| 838 | { |
| 839 | c2 = c1; |
| 840 | c1 = c; |
| 841 | c = getc (infile); |
| 842 | } |
| 843 | |
| 844 | /* If two previous characters were " and \, |
| 845 | this is a doc string. Otherwise, there is none. */ |
| 846 | if (c2 != '"' || c1 != '\\') |
| 847 | { |
| 848 | #ifdef DEBUG |
| 849 | fprintf (stderr, "## non-docstring in %s (%s)\n", |
| 850 | buffer, filename); |
| 851 | #endif |
| 852 | continue; |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | else if (! strcmp (buffer, "autoload")) |
| 858 | { |
| 859 | type = 'F'; |
| 860 | c = getc (infile); |
| 861 | if (c == '\'') |
| 862 | read_lisp_symbol (infile, buffer); |
| 863 | else |
| 864 | { |
| 865 | if (c != '(') |
| 866 | { |
| 867 | fprintf (stderr, "## unparsable name in autoload in %s\n", |
| 868 | filename); |
| 869 | continue; |
| 870 | } |
| 871 | read_lisp_symbol (infile, buffer); |
| 872 | if (strcmp (buffer, "quote")) |
| 873 | { |
| 874 | fprintf (stderr, "## unparsable name in autoload in %s\n", |
| 875 | filename); |
| 876 | continue; |
| 877 | } |
| 878 | read_lisp_symbol (infile, buffer); |
| 879 | c = getc (infile); |
| 880 | if (c != ')') |
| 881 | { |
| 882 | fprintf (stderr, |
| 883 | "## unparsable quoted name in autoload in %s\n", |
| 884 | filename); |
| 885 | continue; |
| 886 | } |
| 887 | } |
| 888 | skip_white (infile); |
| 889 | if ((c = getc (infile)) != '\"') |
| 890 | { |
| 891 | fprintf (stderr, "## autoload of %s unparsable (%s)\n", |
| 892 | buffer, filename); |
| 893 | continue; |
| 894 | } |
| 895 | read_c_string (infile, 0); |
| 896 | skip_white (infile); |
| 897 | |
| 898 | if (saved_string == 0) |
| 899 | { |
| 900 | /* If the next three characters aren't `dquote bslash newline' |
| 901 | then we're not reading a docstring. */ |
| 902 | if ((c = getc (infile)) != '"' || |
| 903 | (c = getc (infile)) != '\\' || |
| 904 | (c = getc (infile)) != '\n') |
| 905 | { |
| 906 | #ifdef DEBUG |
| 907 | fprintf (stderr, "## non-docstring in %s (%s)\n", |
| 908 | buffer, filename); |
| 909 | #endif |
| 910 | continue; |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | #ifdef DEBUG |
| 916 | else if (! strcmp (buffer, "if") || |
| 917 | ! strcmp (buffer, "byte-code")) |
| 918 | ; |
| 919 | #endif |
| 920 | |
| 921 | else |
| 922 | { |
| 923 | #ifdef DEBUG |
| 924 | fprintf (stderr, "## unrecognised top-level form, %s (%s)\n", |
| 925 | buffer, filename); |
| 926 | #endif |
| 927 | continue; |
| 928 | } |
| 929 | |
| 930 | /* At this point, we should either use the previous |
| 931 | dynamic doc string in saved_string |
| 932 | or gobble a doc string from the input file. |
| 933 | |
| 934 | In the latter case, the opening quote (and leading |
| 935 | backslash-newline) have already been read. */ |
| 936 | |
| 937 | putc (037, outfile); |
| 938 | putc (type, outfile); |
| 939 | fprintf (outfile, "%s\n", buffer); |
| 940 | if (saved_string) |
| 941 | { |
| 942 | fputs (saved_string, outfile); |
| 943 | /* Don't use one dynamic doc string twice. */ |
| 944 | free (saved_string); |
| 945 | saved_string = 0; |
| 946 | } |
| 947 | else |
| 948 | read_c_string (infile, 1); |
| 949 | } |
| 950 | fclose (infile); |
| 951 | return 0; |
| 952 | } |