home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / OS2 / gnuinfo.zip / info / makedoc.c < prev    next >
C/C++ Source or Header  |  1997-07-15  |  12KB  |  467 lines

  1. /* makedoc.c -- Make doc.c and funs.h from input files.
  2.    $Id: makedoc.c,v 1.4 1997/07/15 18:35:59 karl Exp $
  3.  
  4.    This file is part of GNU Info, a program for reading online documentation
  5.    stored in Info format.
  6.  
  7.    Copyright (C) 1993, 97 Free Software Foundation, Inc.
  8.  
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 2, or (at your option)
  12.    any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  
  23.    Written by Brian Fox (bfox@ai.mit.edu). */
  24.  
  25. /* This program grovels the contents of the source files passed as arguments
  26.    and writes out a file of function pointers and documentation strings, and
  27.    a header file which describes the contents.  This only does the functions
  28.    declared with DECLARE_INFO_COMMAND. */
  29.  
  30. #include "info.h"
  31.  
  32. static void fatal_file_error ();
  33.  
  34. /* Name of the header file which receives the declarations of functions. */
  35. static char *funs_filename = "funs.h";
  36.  
  37. /* Name of the documentation to function pointer file. */
  38. static char *doc_filename = "doc.c";
  39.  
  40. static char *doc_header[] = {
  41.   "/* doc.c -- Generated structure containing function names and doc strings.",
  42.   "",
  43.   "   This file was automatically made from various source files with the",
  44.   "   command \"%s\".  DO NOT EDIT THIS FILE, only \"%s.c\".",
  45.   (char *)NULL
  46. };
  47.  
  48. static char *doc_header_1[] = {
  49.   "   An entry in the array FUNCTION_DOC_ARRAY is made for each command",
  50.   "   found in the above files; each entry consists of a function pointer,",
  51. #if defined (NAMED_FUNCTIONS)
  52.   "   a string which is the user-visible name of the function,",
  53. #endif /* NAMED_FUNCTIONS */
  54.   "   and a string which documents its purpose. */",
  55.   "",
  56.   "#include \"doc.h\"",
  57.   "#include \"funs.h\"",
  58.   "",
  59.   "FUNCTION_DOC function_doc_array[] = {",
  60.   "",
  61.   (char *)NULL
  62. };
  63.  
  64. /* How to remember the locations of the functions found so that Emacs
  65.    can use the information in a tag table. */
  66. typedef struct {
  67.   char *name;                   /* Name of the tag. */
  68.   int line;                     /* Line number at which it appears. */
  69.   long char_offset;             /* Character offset at which it appears. */
  70. } EMACS_TAG;
  71.  
  72. typedef struct {
  73.   char *filename;               /* Name of the file containing entries. */
  74.   long entrylen;                /* Total number of characters in tag block. */
  75.   EMACS_TAG **entries;          /* Entries found in FILENAME. */
  76.   int entries_index;
  77.   int entries_slots;
  78. } EMACS_TAG_BLOCK;
  79.  
  80. EMACS_TAG_BLOCK **emacs_tags = (EMACS_TAG_BLOCK **)NULL;
  81. int emacs_tags_index = 0;
  82. int emacs_tags_slots = 0;
  83.  
  84. #define DECLARATION_STRING "\nDECLARE_INFO_COMMAND"
  85.  
  86. static void process_one_file ();
  87. static void maybe_dump_tags ();
  88. static FILE *must_fopen ();
  89.  
  90. int
  91. main (argc, argv)
  92.      int argc;
  93.      char **argv;
  94. {
  95.   register int i;
  96.   int tags_only = 0;
  97.   FILE *funs_stream, *doc_stream;
  98.  
  99.   for (i = 1; i < argc; i++)
  100.     if (strcmp (argv[i], "-tags") == 0)
  101.       {
  102.         tags_only++;
  103.         break;
  104.       }
  105.  
  106.   if (tags_only)
  107.     {
  108.       funs_filename = "/dev/null";
  109.       doc_filename = "/dev/null";
  110.     }
  111.   
  112.   funs_stream = must_fopen (funs_filename, "w");
  113.   doc_stream = must_fopen (doc_filename, "w");
  114.  
  115.   fprintf (funs_stream,
  116.            "/* %s -- Generated declarations for Info commands. */\n",
  117.            funs_filename);
  118.  
  119.   for (i = 0; doc_header[i]; i++)
  120.     {
  121.       fprintf (doc_stream, doc_header[i], argv[0], argv[0]);
  122.       fprintf (doc_stream, "\n");
  123.     }
  124.  
  125.   fprintf (doc_stream,
  126.            _("   Source files groveled to make this file include:\n\n"));
  127.  
  128.   for (i = 1; i < argc; i++)
  129.     fprintf (doc_stream, "\t%s\n", argv[i]);
  130.  
  131.   fprintf (doc_stream, "\n");
  132.  
  133.   for (i = 0; doc_header_1[i]; i++)
  134.     fprintf (doc_stream, "%s\n", doc_header_1[i]);
  135.  
  136.  
  137.   for (i = 1; i < argc; i++)
  138.     {
  139.       char *curfile;
  140.       curfile = argv[i];
  141.  
  142.       if (*curfile == '-')
  143.         continue;
  144.  
  145.       fprintf (doc_stream, "/* Commands found in \"%s\". */\n", curfile);
  146.       fprintf (funs_stream, "\n/* Functions declared in \"%s\". */\n",
  147.                curfile);
  148.  
  149.       process_one_file (curfile, doc_stream, funs_stream);
  150.     }
  151.  
  152.   fprintf (doc_stream,
  153.            "   { (VFunction *)NULL, (char *)NULL, (char *)NULL }\n};\n");
  154.  
  155.   fclose (funs_stream);
  156.   fclose (doc_stream);
  157.  
  158.   if (tags_only)
  159.     maybe_dump_tags (stdout);
  160.   exit (0);
  161. }
  162.  
  163. /* Dumping out the contents of an Emacs tags table. */
  164. static void
  165. maybe_dump_tags (stream)
  166.      FILE *stream;
  167. {
  168.   register int i;
  169.  
  170.   /* Print out the information for each block. */
  171.   for (i = 0; i < emacs_tags_index; i++)
  172.     {
  173.       register int j;
  174.       register EMACS_TAG_BLOCK *block;
  175.       register EMACS_TAG *etag;
  176.       long block_len;
  177.  
  178.       block_len = 0;
  179.       block = emacs_tags[i];
  180.  
  181.       /* Calculate the length of the dumped block first. */
  182.       for (j = 0; j < block->entries_index; j++)
  183.         {
  184.           char digits[30];
  185.           etag = block->entries[j];
  186.           block_len += 3 + strlen (etag->name);
  187.           sprintf (digits, "%d,%ld", etag->line, etag->char_offset);
  188.           block_len += strlen (digits);
  189.         }
  190.  
  191.       /* Print out the defining line. */
  192.       fprintf (stream, "\f\n%s,%ld\n", block->filename, block_len);
  193.  
  194.       /* Print out the individual tags. */
  195.       for (j = 0; j < block->entries_index; j++)
  196.         {
  197.           etag = block->entries[j];
  198.  
  199.           fprintf (stream, "%s,\177%d,%ld\n",
  200.                    etag->name, etag->line, etag->char_offset);
  201.         }
  202.     }
  203. }
  204.  
  205. /* Keeping track of names, line numbers and character offsets of functions
  206.    found in source files. */
  207. static EMACS_TAG_BLOCK *
  208. make_emacs_tag_block (filename)
  209.      char *filename;
  210. {
  211.   EMACS_TAG_BLOCK *block;
  212.  
  213.   block = (EMACS_TAG_BLOCK *)xmalloc (sizeof (EMACS_TAG_BLOCK));
  214.   block->filename = xstrdup (filename);
  215.   block->entrylen = 0;
  216.   block->entries = (EMACS_TAG **)NULL;
  217.   block->entries_index = 0;
  218.   block->entries_slots = 0;
  219.   return (block);
  220. }
  221.  
  222. static void
  223. add_tag_to_block (block, name, line, char_offset)
  224.      EMACS_TAG_BLOCK *block;
  225.      char *name;
  226.      int line;
  227.      long char_offset;
  228. {
  229.   EMACS_TAG *tag;
  230.  
  231.   tag = (EMACS_TAG *)xmalloc (sizeof (EMACS_TAG));
  232.   tag->name = name;
  233.   tag->line = line;
  234.   tag->char_offset = char_offset;
  235.   add_pointer_to_array (tag, block->entries_index, block->entries,
  236.                         block->entries_slots, 50, EMACS_TAG *);
  237. }
  238.  
  239. /* Read the file represented by FILENAME into core, and search it for Info
  240.    function declarations.  Output the declarations in various forms to the
  241.    DOC_STREAM and FUNS_STREAM. */
  242. static void
  243. process_one_file (filename, doc_stream, funs_stream)
  244.      char *filename;
  245.      FILE *doc_stream, *funs_stream;
  246. {
  247.   int descriptor, decl_len;
  248.   char *buffer, *decl_str;
  249.   struct stat finfo;
  250.   long offset;
  251.   long file_size;
  252.   EMACS_TAG_BLOCK *block;
  253.  
  254.   if (stat (filename, &finfo) == -1)
  255.     fatal_file_error (filename);
  256.  
  257.   descriptor = open (filename, O_RDONLY, 0666);
  258.  
  259.   if (descriptor == -1)
  260.     fatal_file_error (filename);
  261.  
  262.   file_size = (long) finfo.st_size;
  263.   buffer = (char *)xmalloc (1 + file_size);
  264.   read (descriptor, buffer, file_size);
  265.   close (descriptor);
  266.  
  267.   offset = 0;
  268.   decl_str = DECLARATION_STRING;
  269.   decl_len = strlen (decl_str);
  270.  
  271.   block = make_emacs_tag_block (filename);
  272.  
  273.   while (1)
  274.     {
  275.       long point = 0;
  276.       long line_start = 0;
  277.       int line_number = 0;
  278.  
  279.       char *func, *doc;
  280. #if defined (NAMED_FUNCTIONS)
  281.       char *func_name;
  282. #endif /* NAMED_FUNCTIONS */
  283.  
  284.       for (; offset < (file_size - decl_len); offset++)
  285.         {
  286.           if (buffer[offset] == '\n')
  287.             {
  288.               line_number++;
  289.               line_start = offset + 1;
  290.             }
  291.  
  292.           if (strncmp (buffer + offset, decl_str, decl_len) == 0)
  293.             {
  294.               offset += decl_len;
  295.               point = offset;
  296.               break;
  297.             }
  298.         }
  299.  
  300.       if (!point)
  301.         break;
  302.  
  303.       /* Skip forward until we find the open paren. */
  304.       while (point < file_size)
  305.         {
  306.           if (buffer[point] == '\n')
  307.             {
  308.               line_number++;
  309.               line_start = point + 1;
  310.             }
  311.           else if (buffer[point] == '(')
  312.             break;
  313.  
  314.           point++;
  315.         }
  316.  
  317.       while (point++ < file_size)
  318.         {
  319.           if (!whitespace_or_newline (buffer[point]))
  320.             break;
  321.           else if (buffer[point] == '\n')
  322.             {
  323.               line_number++;
  324.               line_start = point + 1;
  325.             }
  326.         }
  327.  
  328.       if (point >= file_size)
  329.         break;
  330.  
  331.       /* Now looking at name of function.  Get it. */
  332.       for (offset = point; buffer[offset] != ','; offset++);
  333.       func = (char *)xmalloc (1 + (offset - point));
  334.       strncpy (func, buffer + point, offset - point);
  335.       func[offset - point] = '\0';
  336.  
  337.       /* Remember this tag in the current block. */
  338.       {
  339.         char *tag_name;
  340.  
  341.         tag_name = (char *)xmalloc (1 + (offset - line_start));
  342.         strncpy (tag_name, buffer + line_start, offset - line_start);
  343.         tag_name[offset - line_start] = '\0';
  344.         add_tag_to_block (block, tag_name, line_number, point);
  345.       }
  346.  
  347. #if defined (NAMED_FUNCTIONS)
  348.       /* Generate the user-visible function name from the function's name. */
  349.       {
  350.         register int i;
  351.         char *name_start;
  352.  
  353.         name_start = func;
  354.  
  355.         if (strncmp (name_start, "info_", 5) == 0)
  356.           name_start += 5;
  357.  
  358.         func_name = xstrdup (name_start);
  359.  
  360.         /* Fix up "ea" commands. */
  361.         if (strncmp (func_name, "ea_", 3) == 0)
  362.           {
  363.             char *temp_func_name;
  364.  
  365.             temp_func_name = (char *)xmalloc (10 + strlen (func_name));
  366.             strcpy (temp_func_name, "echo_area_");
  367.             strcat (temp_func_name, func_name + 3);
  368.             free (func_name);
  369.             func_name = temp_func_name;
  370.           }
  371.  
  372.         for (i = 0; func_name[i]; i++)
  373.           if (func_name[i] == '_')
  374.             func_name[i] = '-';
  375.       }
  376. #endif /* NAMED_FUNCTIONS */
  377.  
  378.       /* Find doc string. */
  379.       point = offset + 1;
  380.  
  381.       while (point < file_size)
  382.         {
  383.           if (buffer[point] == '\n')
  384.             {
  385.               line_number++;
  386.               line_start = point + 1;
  387.             }
  388.  
  389.           if (buffer[point] == '"')
  390.             break;
  391.           else
  392.             point++;
  393.         }
  394.  
  395.       offset = point + 1;
  396.  
  397.       while (offset < file_size)
  398.         {
  399.           if (buffer[offset] == '\n')
  400.             {
  401.               line_number++;
  402.               line_start = offset + 1;
  403.             }
  404.  
  405.           if (buffer[offset] == '\\')
  406.             offset += 2;
  407.           else if (buffer[offset] == '"')
  408.             break;
  409.           else
  410.             offset++;
  411.         }
  412.  
  413.       offset++;
  414.       if (offset >= file_size)
  415.         break;
  416.  
  417.       doc = (char *)xmalloc (1 + (offset - point));
  418.       strncpy (doc, buffer + point, offset - point);
  419.       doc[offset - point] = '\0';
  420.  
  421. #if defined (NAMED_FUNCTIONS)
  422.       fprintf (doc_stream, "   { %s, \"%s\", %s },\n", func, func_name, doc);
  423.       free (func_name);
  424. #else /* !NAMED_FUNCTIONS */
  425.       fprintf (doc_stream, "   { %s, %s },\n", func, doc);
  426. #endif /* !NAMED_FUNCTIONS */
  427.  
  428.       fprintf (funs_stream, "extern void %s ();\n", func);
  429.       free (func);
  430.       free (doc);
  431.     }
  432.   free (buffer);
  433.  
  434.   /* If we created any tags, remember this file on our global list.  Otherwise,
  435.      free the memory already allocated to it. */
  436.   if (block->entries)
  437.     add_pointer_to_array (block, emacs_tags_index, emacs_tags,
  438.                           emacs_tags_slots, 10, EMACS_TAG_BLOCK *);
  439.   else
  440.     {
  441.       free (block->filename);
  442.       free (block);
  443.     }
  444. }
  445.  
  446. static void
  447. fatal_file_error (filename)
  448.      char *filename;
  449. {
  450.   fprintf (stderr, _("Couldn't manipulate the file %s.\n"), filename);
  451.   exit (2);
  452. }
  453.  
  454. static FILE *
  455. must_fopen (filename, mode)
  456.      char *filename, *mode;
  457. {
  458.   FILE *stream;
  459.  
  460.   stream = fopen (filename, mode);
  461.   if (!stream)
  462.     fatal_file_error (filename);
  463.  
  464.   return (stream);
  465. }
  466.  
  467.