home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-18.59-bin.lha / lib / emacs / 18.59 / etc / make-docfile.c < prev    next >
C/C++ Source or Header  |  1993-05-09  |  12KB  |  548 lines

  1. /* Generate doc-string file for GNU Emacs from source files.
  2.    Copyright (C) 1985, 1986 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* The arguments given to this program are all the C and Lisp source files
  21.  of GNU Emacs.  .elc and .el and .c files are allowed.
  22.  A .o file can also be specified; the .c file it was made from is used.
  23.  This helps the makefile pass the correct list of files.
  24.  
  25.  The results, which go to standard output or to a file
  26.  specified with -a or -o (-a to append, -o to start from nothing),
  27.  are entries containing function or variable names and their documentation.
  28.  Each entry starts with a ^_ character.
  29.  Then comes F for a function or V for a variable.
  30.  Then comes the function or variable name, terminated with a newline.
  31.  Then comes the documentation for that function or variable.
  32.  */
  33.  
  34. #include <stdio.h>
  35.  
  36. FILE *outfile;
  37.  
  38. main (argc, argv)
  39.      int argc;
  40.      char **argv;
  41. {
  42.   int i;
  43.   int err_count = 0;
  44.  
  45.   outfile = stdout;
  46.  
  47.   /* If first two args are -o FILE, output to FILE.  */
  48.   i = 1;
  49.   if (argc > i + 1 && !strcmp (argv[i], "-o"))
  50.     {
  51.       outfile = fopen (argv[i + 1], "w");
  52.       i += 2;
  53.     }
  54.   if (argc > i + 1 && !strcmp (argv[i], "-a"))
  55.     {
  56.       outfile = fopen (argv[i + 1], "a");
  57.       i += 2;
  58.     }
  59.  
  60. #ifdef AMIGA
  61.   {
  62.     char fn[512], *fpos;
  63.     int c;
  64.  
  65.     c = getchar();
  66.     do
  67.       {
  68.     while (c == ' ' || c == '\n' || c == '\t') c = getchar();
  69.     if (c == EOF) break;
  70.     fpos = fn;
  71.     do *fpos++ = c;
  72.     while ((c = getchar()) != EOF && c != ' ' && c != '\t' && c != '\n');
  73.     *fpos = 0;
  74.  
  75.     fprintf(stderr, "doc file %s\n", fn);
  76.     err_count += scan_file (fn); /* err_count seems to be {mis,un}used */
  77.       }
  78.     while (1);
  79.   }
  80. #else
  81.   for (; i < argc; i++)
  82.     err_count += scan_file (argv[i]);    /* err_count seems to be {mis,un}used */
  83. #endif
  84. #ifndef VMS
  85.   exit (err_count);            /* see below - shane */
  86. #endif /* VMS */
  87. }
  88.  
  89. /* Read file FILENAME and output its doc strings to stdout.  */
  90. /* Return 1 if file is not found, 0 if it is found.  */
  91.  
  92. scan_file (filename)
  93.      char *filename;
  94. {
  95.   int len = strlen (filename);
  96.   if (!strcmp (filename + len - 4, ".elc"))
  97.     return scan_lisp_file (filename);
  98.   else if (!strcmp (filename + len - 3, ".el"))
  99.     return scan_lisp_file (filename);
  100.   else
  101.     return scan_c_file (filename);
  102. }
  103.  
  104. char buf[128];
  105.  
  106. /* Skip a C string from INFILE,
  107.  and return the character that follows the closing ".
  108.  If printflag is positive, output string contents to stdout.
  109.  If it is negative, store contents in buf.
  110.  Convert escape sequences \n and \t to newline and tab;
  111.  discard \ followed by newline.  */
  112.  
  113. read_c_string (infile, printflag)
  114.      FILE *infile;
  115.      int printflag;
  116. {
  117.   register int c;
  118.   char *p = buf;
  119.  
  120.   c = getc (infile);
  121.   while (c != EOF)
  122.     {
  123.       while (c != '"' && c != EOF)
  124.     {
  125.       if (c == '\\')
  126.         {
  127.           c = getc (infile);
  128.           if (c == '\n')
  129.         {
  130.           c = getc (infile);
  131.           continue;
  132.         }
  133.           if (c == 'n')
  134.         c = '\n';
  135.           if (c == 't')
  136.         c = '\t';
  137.         }
  138.       if (printflag > 0)
  139.         putc (c, outfile);
  140.       else if (printflag < 0)
  141.         *p++ = c;
  142.       c = getc (infile);
  143.     }
  144.       c = getc (infile);
  145.       if (c != '"')
  146.     break;
  147.       if (printflag > 0)
  148.     putc (c, outfile);
  149.       else if (printflag < 0)
  150.     *p++ = c;
  151.       c = getc (infile);
  152.     }
  153.  
  154.   if (printflag < 0)
  155.     *p = 0;
  156.  
  157.   return c;
  158. }
  159.  
  160. /* Read through a c file.  If a .o file is named,
  161.  the corresponding .c file is read instead.
  162.  Looks for DEFUN constructs such as are defined in ../src/lisp.h.
  163.  Accepts any word starting DEF... so it finds DEFSIMPLE and DEFPRED.  */
  164.  
  165. scan_c_file (filename)
  166.      char *filename;
  167. {
  168.   FILE *infile;
  169.   register int c;
  170.   register int commas;
  171.   register int defunflag;
  172.   register int defvarflag;
  173.   
  174.   if (filename[strlen (filename) - 1] == 'o')
  175.     filename[strlen (filename) - 1] = 'c';
  176.  
  177.   infile = fopen (filename, "r");
  178.  
  179.   /* No error if non-ex input file */
  180.   if (infile == NULL)
  181.     {
  182.       perror (filename);
  183.       return 0;
  184.     }
  185.  
  186.   c = '\n';
  187.   while (!feof (infile))
  188.     {
  189.       if (c != '\n')
  190.     {
  191.       c = getc (infile);
  192.       continue;
  193.     }
  194.       c = getc (infile);
  195.       if (c == ' ')
  196.     {
  197.       while (c == ' ')
  198.         c = getc (infile);
  199.       if (c != 'D')
  200.         continue;
  201.       c = getc (infile);
  202.       if (c != 'E')
  203.         continue;
  204.       c = getc (infile);
  205.       if (c != 'F')
  206.         continue;
  207.       c = getc (infile);
  208.       if (c != 'V')
  209.         continue;
  210.       defvarflag = 1;
  211.       defunflag = 0;
  212.       c = getc (infile);
  213.     }
  214.       else if (c == 'D')
  215.     {
  216.       c = getc (infile);
  217.       if (c != 'E')
  218.         continue;
  219.       c = getc (infile);
  220.       if (c != 'F')
  221.         continue;
  222.       c = getc (infile);
  223.       defunflag = c == 'U';
  224.       defvarflag = 0;
  225.     }
  226.       else continue;
  227.  
  228.       while (c != '(')
  229.     {
  230.       if (c < 0)
  231.         return 0;
  232.       c = getc (infile);
  233.     }
  234.  
  235.       c = getc (infile);
  236.       if (c != '"')
  237.     continue;
  238.       c = read_c_string (infile, -1);
  239.  
  240.       if (defunflag)
  241.     commas = 5;
  242.       else if (defvarflag)
  243.     commas = 1;
  244.       else  /* For DEFSIMPLE and DEFPRED */
  245.     commas = 2;
  246.  
  247.       while (commas)
  248.     {
  249.       if (c == ',') commas --;
  250.       if (c < 0)
  251.         return 0;
  252.       c = getc (infile);
  253.     }
  254.       while (c == ' ' || c == '\n' || c == '\t')
  255.     c = getc (infile);
  256.       if (c == '"')
  257.     c = read_c_string (infile, 0);
  258.       while (c != ',')
  259.     c = getc (infile);
  260.       c = getc (infile);
  261.       while (c == ' ' || c == '\n' || c == '\t')
  262.     c = getc (infile);
  263.  
  264.       if (c == '"')
  265.     {
  266.       putc (037, outfile);
  267.       putc (defvarflag ? 'V' : 'F', outfile);
  268.       fprintf (outfile, "%s\n", buf);
  269.       read_c_string (infile, 1);
  270.     }
  271.     }
  272.   fclose (infile);
  273.   return 0;
  274. }
  275.  
  276. /* Read a file of Lisp code, compiled or interpreted.
  277.  Looks for
  278.   (defun NAME ARGS DOCSTRING ...)
  279.   (autoload 'NAME FILE DOCSTRING ...)
  280.   (defvar NAME VALUE DOCSTRING)
  281.   (defconst NAME VALUE DOCSTRING)
  282.  starting in column zero.
  283.  ARGS, FILE or VALUE is ignored.  We do not know how to parse Lisp code
  284.  so we use a kludge to skip them:
  285.   In a function definition, the form of ARGS of FILE is known, and we
  286.   can skip it.
  287.   In a variable definition, we use a formatting convention:
  288.   the DOCSTRING, if present, must be followed by a closeparen and a newline,
  289.   and no newline must appear between the defvar or defconst and the docstring,
  290.   The only source file that must follow this convention is loaddefs.el;
  291.   aside from that, it is always the .elc file that we look at, and
  292.   they are no problem because byte-compiler output follows this convention.
  293.  The NAME and DOCSTRING are output.
  294.  NAME is preceded by `F' for a function or `V' for a variable.
  295.  An entry is output only if DOCSTRING has \ newline just after the opening "
  296.  */
  297.  
  298. scan_lisp_file (filename)
  299.      char *filename;
  300. {
  301.   FILE *infile;
  302.   register int c;
  303.   register int commas;
  304.   register char *p;
  305.   int defvarflag;
  306.  
  307.   infile = fopen (filename, "r");
  308.   if (infile == NULL)
  309.     {
  310.       perror (filename);
  311.       return 0;                /* No error */
  312.     }
  313.  
  314.   c = '\n';
  315.   while (!feof (infile))
  316.     {
  317.       if (c != '\n')
  318.     {
  319.       c = getc (infile);
  320.       continue;
  321.     }
  322.       c = getc (infile);
  323.       if (c != '(')
  324.     continue;
  325.       c = getc (infile);
  326.       if (c == 'a')
  327.     {
  328.       c = getc (infile);
  329.       if (c != 'u')
  330.         continue;
  331.       c = getc (infile);
  332.       if (c != 't')
  333.         continue;
  334.       c = getc (infile);
  335.       if (c != 'o')
  336.         continue;
  337.       c = getc (infile);
  338.       if (c != 'l')
  339.         continue;
  340.       c = getc (infile);
  341.       if (c != 'o')
  342.         continue;
  343.       c = getc (infile);
  344.       if (c != 'a')
  345.         continue;
  346.       c = getc (infile);
  347.       if (c != 'd')
  348.         continue;
  349.  
  350.       c = getc (infile);
  351.       while (c == ' ')
  352.         c = getc (infile);
  353.  
  354.       if (c == '\'')
  355.         {
  356.           c = getc (infile);
  357.         }
  358.       else
  359.         {
  360.           if (c != '(')
  361.         continue;
  362.           c = getc (infile);
  363.           if (c != 'q')
  364.         continue;
  365.           c = getc (infile);
  366.           if (c != 'u')
  367.         continue;
  368.           c = getc (infile);
  369.           if (c != 'o')
  370.         continue;
  371.           c = getc (infile);
  372.           if (c != 't')
  373.         continue;
  374.           c = getc (infile);
  375.           if (c != 'e')
  376.         continue;
  377.           c = getc (infile);
  378.           if (c != ' ')
  379.         continue;
  380.           while (c == ' ')
  381.         c = getc (infile);
  382.         }
  383.  
  384.       p = buf;
  385.       while (c != ' ' && c != ')')
  386.         {
  387.           if (c == EOF)
  388.         return 1;
  389.           if (c == '\\')
  390.         c = getc (infile);
  391.           *p++ = c;
  392.           c = getc (infile);
  393.         }
  394.       *p = 0;
  395.  
  396.       while (c != '"')
  397.         {
  398.           if (c == EOF)
  399.         return 1;
  400.           c = getc (infile);
  401.         }
  402.       c = read_c_string (infile, 0);
  403.     }
  404.       else if (c == 'd')
  405.     {
  406.       c = getc (infile);
  407.       if (c != 'e')
  408.         continue;
  409.       c = getc (infile);
  410.       if (c != 'f')
  411.         continue;
  412.       c = getc (infile);
  413.       if (c == 'u')
  414.         {
  415.           c = getc (infile);
  416.           if (c != 'n')
  417.         continue;
  418.           defvarflag = 0;
  419.         }
  420.       else if (c == 'v')
  421.         {
  422.           c = getc (infile);
  423.           if (c != 'a')
  424.         continue;
  425.           c = getc (infile);
  426.           if (c != 'r')
  427.         continue;
  428.           defvarflag = 1;
  429.         }
  430.       else if (c == 'c')
  431.         {
  432.           c = getc (infile);
  433.           if (c != 'o')
  434.         continue;
  435.           c = getc (infile);
  436.           if (c != 'n')
  437.         continue;
  438.           c = getc (infile);
  439.           if (c != 's')
  440.         continue;
  441.           c = getc (infile);
  442.           if (c != 't')
  443.         continue;
  444.           defvarflag = 1;
  445.         }
  446.       else
  447.         continue;
  448.  
  449.       /* Now we have seen "defun" or "defvar" or "defconst".  */
  450.  
  451.       while (c != ' ' && c != '\n' && c != '\t')
  452.         c = getc (infile);
  453.  
  454.       while (c == ' ' || c == '\n' || c == '\t')
  455.         c = getc (infile);
  456.  
  457.       /* Read and store name of function or variable being defined
  458.          Discard backslashes that are for quoting.  */
  459.       p = buf;
  460.       while (c != ' ' && c != '\n' && c != '\t')
  461.         {
  462.           if (c == '\\')
  463.         c = getc (infile);
  464.           *p++ = c;
  465.           c = getc (infile);
  466.         }
  467.       *p = 0;
  468.  
  469.       while (c == ' ' || c == '\n' || c == '\t')
  470.         c = getc (infile);
  471.  
  472.       if (! defvarflag)
  473.         {
  474.           /* A function: */
  475.           /* Skip the arguments: either "nil" or a list in parens */
  476.           if (c == 'n')
  477.         {
  478.           while (c != ' ' && c != '\n' && c != '\t')
  479.             c = getc (infile);
  480.         }
  481.           else
  482.         {
  483.           while (c != '(')
  484.             c = getc (infile);
  485.           while (c != ')')
  486.             c = getc (infile);
  487.         }
  488.           c = getc (infile);
  489.         }
  490.       else
  491.         {
  492.           /* A variable:  */
  493.  
  494.           /* Skip until the first newline; remember
  495.          the two previous characters.  */
  496.           char c1 = 0, c2 = 0;
  497.  
  498.           while (c != '\n' && c >= 0)
  499.         {
  500.           c2 = c1;
  501.           c1 = c;
  502.           c = getc (infile);
  503.         }
  504.  
  505.           /* If two previous characters were " and \,
  506.          this is a doc string.  Otherwise, there is none.  */
  507.           if (c2 == '"' && c1 == '\\')
  508.         {
  509.           putc (037, outfile);
  510.           putc ('V', outfile);
  511.           fprintf (outfile, "%s\n", buf);
  512.           read_c_string (infile, 1);
  513.         }
  514.           continue;
  515.         }
  516.     }
  517.       else
  518.     continue;
  519.  
  520.       /* Here for a function definition.
  521.      We have skipped the file name or arguments
  522.      and arrived at where the doc string is,
  523.      if there is a doc string.  */
  524.  
  525.       /* Skip whitespace */
  526.  
  527.       while (c == ' ' || c == '\n' || c == '\t')
  528.     c = getc (infile);
  529.  
  530.       /* " followed by \ and newline means a doc string we should gobble */
  531.       if (c != '"')
  532.     continue;
  533.       c = getc (infile);
  534.       if (c != '\\')
  535.     continue;
  536.       c = getc (infile);
  537.       if (c != '\n')
  538.     continue;
  539.  
  540.       putc (037, outfile);
  541.       putc ('F', outfile);
  542.       fprintf (outfile, "%s\n", buf);
  543.       read_c_string (infile, 1);
  544.     }
  545.   fclose (infile);
  546.   return 0;
  547. }
  548.