home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / editors / demacs-3.arj / MAKE-DOC.C < prev    next >
C/C++ Source or Header  |  1991-10-17  |  13KB  |  569 lines

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