Back to index

tetex-bin  3.0
makedoc.c
Go to the documentation of this file.
00001 /* makedoc.c -- make doc.c and funs.h from input files.
00002    $Id: makedoc.c,v 1.4 2004/04/11 17:56:46 karl Exp $
00003 
00004    Copyright (C) 1993, 1997, 1998, 1999, 2001, 2002, 2003, 2004 Free Software
00005    Foundation, Inc.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2, or (at your option)
00010    any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 
00021    Originally written by Brian Fox (bfox@ai.mit.edu). */
00022 
00023 /* This program grovels the contents of the source files passed as arguments
00024    and writes out a file of function pointers and documentation strings, and
00025    a header file which describes the contents.  This only does the functions
00026    declared with DECLARE_INFO_COMMAND. */
00027 
00028 #include "info.h"
00029 #include "infokey.h"
00030 
00031 static void fatal_file_error (char *filename);
00032 
00033 /* Name of the header file which receives the declarations of functions. */
00034 static char *funs_filename = "funs.h";
00035 
00036 /* Name of the documentation to function pointer file. */
00037 static char *doc_filename = "doc.c";
00038 static char *key_filename = "key.c";
00039 
00040 static char *doc_header[] = {
00041   "/* doc.c -- Generated structure containing function names and doc strings.",
00042   "",
00043   "   This file was automatically made from various source files with the",
00044   "   command `%s'.  DO NOT EDIT THIS FILE, only `%s.c'.",
00045   (char *)NULL
00046 };
00047 
00048 static char *doc_header_1[] = {
00049   "   An entry in the array FUNCTION_DOC_ARRAY is made for each command",
00050   "   found in the above files; each entry consists of a function pointer,",
00051 #if defined (NAMED_FUNCTIONS)
00052   "   a string which is the user-visible name of the function,",
00053 #endif /* NAMED_FUNCTIONS */
00054   "   and a string which documents its purpose. */",
00055   "",
00056   "#include \"info.h\"",
00057   "#include \"funs.h\"",
00058   "",
00059   "FUNCTION_DOC function_doc_array[] = {",
00060   "",
00061   (char *)NULL
00062 };
00063 
00064 static char *key_header[] = {
00065   "/* key.c -- Generated array containing function names.",
00066   "",
00067   "   This file was automatically made from various source files with the",
00068   "   command \"%s\".  DO NOT EDIT THIS FILE, only \"%s.c\".",
00069   "",
00070   (char *)NULL
00071 };
00072 
00073 static char *key_header_1[] = {
00074   "   An entry in the array FUNCTION_KEY_ARRAY is made for each command",
00075   "   found in the above files; each entry consists of",
00076   "   a string which is the user-visible name of the function.  */",
00077   "",
00078   "#include \"key.h\"",
00079   "#include \"funs.h\"",
00080   "",
00081   "FUNCTION_KEY function_key_array[] = {",
00082   "",
00083   (char *)NULL
00084 };
00085 
00086 /* How to remember the locations of the functions found so that Emacs
00087    can use the information in a tag table. */
00088 typedef struct {
00089   char *name;                   /* Name of the tag. */
00090   int line;                     /* Line number at which it appears. */
00091   long char_offset;             /* Character offset at which it appears. */
00092 } EMACS_TAG;
00093 
00094 typedef struct {
00095   char *filename;               /* Name of the file containing entries. */
00096   long entrylen;                /* Total number of characters in tag block. */
00097   EMACS_TAG **entries;          /* Entries found in FILENAME. */
00098   int entries_index;
00099   int entries_slots;
00100 } EMACS_TAG_BLOCK;
00101 
00102 EMACS_TAG_BLOCK **emacs_tags = (EMACS_TAG_BLOCK **)NULL;
00103 int emacs_tags_index = 0;
00104 int emacs_tags_slots = 0;
00105 
00106 #define DECLARATION_STRING "\nDECLARE_INFO_COMMAND"
00107 
00108 static void process_one_file (char *filename, FILE *doc_stream,
00109     FILE *key_stream, FILE *funs_stream);
00110 static void maybe_dump_tags (FILE *stream);
00111 static FILE *must_fopen (char *filename, char *mode);
00112 static void init_func_key (unsigned int val);
00113 static unsigned int next_func_key (void);
00114 
00115 int
00116 main (int argc, char **argv)
00117 {
00118   register int i;
00119   int tags_only = 0;
00120   FILE *funs_stream, *doc_stream;
00121   FILE *key_stream;
00122 
00123 #if STRIP_DOT_EXE
00124   {
00125     char *dot = strrchr (argv[0], '.');
00126 
00127     if (dot && FILENAME_CMP (dot, ".exe") == 0)
00128       *dot = 0;
00129   }
00130 #endif
00131 
00132   for (i = 1; i < argc; i++)
00133     if (strcmp (argv[i], "-tags") == 0)
00134       {
00135         tags_only++;
00136         break;
00137       }
00138 
00139   if (tags_only)
00140     {
00141       funs_filename = NULL_DEVICE;
00142       doc_filename = NULL_DEVICE;
00143       key_filename = NULL_DEVICE;
00144     }
00145 
00146   funs_stream = must_fopen (funs_filename, "w");
00147   doc_stream = must_fopen (doc_filename, "w");
00148   key_stream = must_fopen (key_filename, "w");
00149 
00150   fprintf (funs_stream,
00151       "/* %s -- Generated declarations for Info commands. */\n\n\
00152 #include \"info.h\"\n",
00153       funs_filename);
00154 
00155   for (i = 0; doc_header[i]; i++)
00156     {
00157       fprintf (doc_stream, doc_header[i], argv[0], argv[0]);
00158       fprintf (doc_stream, "\n");
00159     }
00160 
00161   fprintf (doc_stream,
00162            _("   Source files groveled to make this file include:\n\n"));
00163 
00164   for (i = 0; key_header[i]; i++)
00165     {
00166       fprintf (key_stream, key_header[i], argv[0], argv[0]);
00167       fprintf (key_stream, "\n");
00168     }
00169   fprintf (key_stream,
00170            _("   Source files groveled to make this file include:\n\n"));
00171 
00172   for (i = 1; i < argc; i++)
00173     {
00174       fprintf (doc_stream, "\t%s\n", argv[i]);
00175       fprintf (key_stream, "\t%s\n", argv[i]);
00176     }
00177 
00178   fprintf (doc_stream, "\n");
00179   for (i = 0; doc_header_1[i]; i++)
00180     fprintf (doc_stream, "%s\n", doc_header_1[i]);
00181 
00182   fprintf (key_stream, "\n");
00183   for (i = 0; key_header_1[i]; i++)
00184     fprintf (key_stream, "%s\n", key_header_1[i]);
00185 
00186   init_func_key(0);
00187 
00188   for (i = 1; i < argc; i++)
00189     {
00190       char *curfile;
00191       curfile = argv[i];
00192 
00193       if (*curfile == '-')
00194         continue;
00195 
00196       fprintf (doc_stream, "/* Commands found in \"%s\". */\n", curfile);
00197       fprintf (key_stream, "/* Commands found in \"%s\". */\n", curfile);
00198       fprintf (funs_stream, "\n/* Functions declared in \"%s\". */\n",
00199                curfile);
00200 
00201       process_one_file (curfile, doc_stream, key_stream, funs_stream);
00202     }
00203 
00204 #if defined (INFOKEY)
00205 
00206 #if defined (NAMED_FUNCTIONS)
00207   fprintf (doc_stream,
00208            "   { (VFunction *)NULL, (char *)NULL, (FUNCTION_KEYSEQ *)NULL, (char *)NULL }\n};\n");
00209 #else /* !NAMED_FUNCTIONS */
00210   fprintf (doc_stream, "   { (VFunction *)NULL, (FUNCTION_KEYSEQ *)NULL, (char *)NULL }\n};\n");
00211 #endif /* !NAMED_FUNCTIONS */
00212 
00213 #else /* !INFOKEY */
00214 
00215 #if defined (NAMED_FUNCTIONS)
00216   fprintf (doc_stream,
00217            "   { (VFunction *)NULL, (char *)NULL, (char *)NULL }\n};\n");
00218 #else /* !NAMED_FUNCTIONS */
00219   fprintf (doc_stream, "   { (VFunction *)NULL, (char *)NULL }\n};\n");
00220 #endif /* !NAMED_FUNCTIONS */
00221 
00222 #endif /* !INFOKEY */
00223 
00224   fprintf (key_stream, "   { (char *)NULL, 0 }\n};\n");
00225   fprintf (funs_stream, "\n#define A_NCOMMANDS %u\n", next_func_key());
00226 
00227   fclose (funs_stream);
00228   fclose (doc_stream);
00229   fclose (key_stream);
00230 
00231   if (tags_only)
00232     maybe_dump_tags (stdout);
00233   return 0;
00234 }
00235 
00236 /* Dumping out the contents of an Emacs tags table. */
00237 static void
00238 maybe_dump_tags (FILE *stream)
00239 {
00240   register int i;
00241 
00242   /* Emacs needs its TAGS file to be in Unix text format (i.e., only
00243      newline at end of every line, no CR), so when we generate a
00244      TAGS table, we must switch the output stream to binary mode.
00245      (If the table is written to a terminal, this is obviously not needed.) */
00246   SET_BINARY (fileno (stream));
00247 
00248   /* Print out the information for each block. */
00249   for (i = 0; i < emacs_tags_index; i++)
00250     {
00251       register int j;
00252       register EMACS_TAG_BLOCK *block;
00253       register EMACS_TAG *etag;
00254       long block_len;
00255 
00256       block_len = 0;
00257       block = emacs_tags[i];
00258 
00259       /* Calculate the length of the dumped block first. */
00260       for (j = 0; j < block->entries_index; j++)
00261         {
00262           char digits[30];
00263           etag = block->entries[j];
00264           block_len += 3 + strlen (etag->name);
00265           sprintf (digits, "%d,%ld", etag->line, etag->char_offset);
00266           block_len += strlen (digits);
00267         }
00268 
00269       /* Print out the defining line. */
00270       fprintf (stream, "\f\n%s,%ld\n", block->filename, block_len);
00271 
00272       /* Print out the individual tags. */
00273       for (j = 0; j < block->entries_index; j++)
00274         {
00275           etag = block->entries[j];
00276 
00277           fprintf (stream, "%s,\177%d,%ld\n",
00278                    etag->name, etag->line, etag->char_offset);
00279         }
00280     }
00281 }
00282 
00283 /* Keeping track of names, line numbers and character offsets of functions
00284    found in source files. */
00285 static EMACS_TAG_BLOCK *
00286 make_emacs_tag_block (char *filename)
00287 {
00288   EMACS_TAG_BLOCK *block;
00289 
00290   block = (EMACS_TAG_BLOCK *)xmalloc (sizeof (EMACS_TAG_BLOCK));
00291   block->filename = xstrdup (filename);
00292   block->entrylen = 0;
00293   block->entries = (EMACS_TAG **)NULL;
00294   block->entries_index = 0;
00295   block->entries_slots = 0;
00296   return (block);
00297 }
00298 
00299 static void
00300 add_tag_to_block (EMACS_TAG_BLOCK *block,
00301     char *name, int line, long int char_offset)
00302 {
00303   EMACS_TAG *tag;
00304 
00305   tag = (EMACS_TAG *)xmalloc (sizeof (EMACS_TAG));
00306   tag->name = name;
00307   tag->line = line;
00308   tag->char_offset = char_offset;
00309   add_pointer_to_array (tag, block->entries_index, block->entries,
00310                         block->entries_slots, 50, EMACS_TAG *);
00311 }
00312 
00313 /* Read the file represented by FILENAME into core, and search it for Info
00314    function declarations.  Output the declarations in various forms to the
00315    DOC_STREAM, KEY_STREAM, and FUNS_STREAM. */
00316 static void
00317 process_one_file (char *filename, FILE *doc_stream,
00318     FILE *key_stream, FILE *funs_stream)
00319 {
00320   int descriptor, decl_len;
00321   char *buffer, *decl_str;
00322   struct stat finfo;
00323   long offset;
00324   long file_size;
00325   EMACS_TAG_BLOCK *block;
00326 
00327   if (stat (filename, &finfo) == -1)
00328     fatal_file_error (filename);
00329 
00330   descriptor = open (filename, O_RDONLY, 0666);
00331 
00332   if (descriptor == -1)
00333     fatal_file_error (filename);
00334 
00335   file_size = (long) finfo.st_size;
00336   buffer = (char *)xmalloc (1 + file_size);
00337   /* On some systems, the buffer will actually contain
00338      less characters than the full file's size, because
00339      the CR characters are removed from line endings.  */
00340   file_size = read (descriptor, buffer, file_size);
00341   close (descriptor);
00342 
00343   offset = 0;
00344   decl_str = DECLARATION_STRING;
00345   decl_len = strlen (decl_str);
00346 
00347   block = make_emacs_tag_block (filename);
00348 
00349   while (1)
00350     {
00351       long point = 0;
00352       long line_start = 0;
00353       int line_number = 0;
00354 
00355       char *func, *doc;
00356 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
00357       char *func_name;
00358 #endif /* INFOKEY || NAMED_FUNCTIONS */
00359 
00360       for (; offset < (file_size - decl_len); offset++)
00361         {
00362           if (buffer[offset] == '\n')
00363             {
00364               line_number++;
00365               line_start = offset + 1;
00366             }
00367 
00368           if (strncmp (buffer + offset, decl_str, decl_len) == 0)
00369             {
00370               offset += decl_len;
00371               point = offset;
00372               break;
00373             }
00374         }
00375 
00376       if (!point)
00377         break;
00378 
00379       /* Skip forward until we find the open paren. */
00380       while (point < file_size)
00381         {
00382           if (buffer[point] == '\n')
00383             {
00384               line_number++;
00385               line_start = point + 1;
00386             }
00387           else if (buffer[point] == '(')
00388             break;
00389 
00390           point++;
00391         }
00392 
00393       while (point++ < file_size)
00394         {
00395           if (!whitespace_or_newline (buffer[point]))
00396             break;
00397           else if (buffer[point] == '\n')
00398             {
00399               line_number++;
00400               line_start = point + 1;
00401             }
00402         }
00403 
00404       if (point >= file_size)
00405         break;
00406 
00407       /* Now looking at name of function.  Get it. */
00408       for (offset = point; buffer[offset] != ','; offset++);
00409       func = (char *)xmalloc (1 + (offset - point));
00410       strncpy (func, buffer + point, offset - point);
00411       func[offset - point] = '\0';
00412 
00413       /* Remember this tag in the current block. */
00414       {
00415         char *tag_name;
00416 
00417         tag_name = (char *)xmalloc (1 + (offset - line_start));
00418         strncpy (tag_name, buffer + line_start, offset - line_start);
00419         tag_name[offset - line_start] = '\0';
00420         add_tag_to_block (block, tag_name, line_number, point);
00421       }
00422 
00423 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
00424       /* Generate the user-visible function name from the function's name. */
00425       {
00426         register int i;
00427         char *name_start;
00428 
00429         name_start = func;
00430 
00431         if (strncmp (name_start, "info_", 5) == 0)
00432           name_start += 5;
00433 
00434         func_name = xstrdup (name_start);
00435 
00436         /* Fix up "ea" commands. */
00437         if (strncmp (func_name, "ea_", 3) == 0)
00438           {
00439             char *temp_func_name;
00440 
00441             temp_func_name = (char *)xmalloc (10 + strlen (func_name));
00442             strcpy (temp_func_name, "echo_area_");
00443             strcat (temp_func_name, func_name + 3);
00444             free (func_name);
00445             func_name = temp_func_name;
00446           }
00447 
00448         for (i = 0; func_name[i]; i++)
00449           if (func_name[i] == '_')
00450             func_name[i] = '-';
00451       }
00452 #endif /* INFOKEY || NAMED_FUNCTIONS */
00453 
00454       /* Find doc string. */
00455       point = offset + 1;
00456 
00457       while (point < file_size)
00458         {
00459           if (buffer[point] == '\n')
00460             {
00461               line_number++;
00462               line_start = point + 1;
00463             }
00464 
00465           if (buffer[point] == '"')
00466             break;
00467           else
00468             point++;
00469         }
00470 
00471       offset = point + 1;
00472 
00473       while (offset < file_size)
00474         {
00475           if (buffer[offset] == '\n')
00476             {
00477               line_number++;
00478               line_start = offset + 1;
00479             }
00480 
00481           if (buffer[offset] == '\\')
00482             offset += 2;
00483           else if (buffer[offset] == '"')
00484             break;
00485           else
00486             offset++;
00487         }
00488 
00489       offset++;
00490       if (offset >= file_size)
00491         break;
00492 
00493       doc = (char *)xmalloc (1 + (offset - point));
00494       strncpy (doc, buffer + point, offset - point);
00495       doc[offset - point] = '\0';
00496 
00497 #if defined (INFOKEY)
00498 
00499 #if defined (NAMED_FUNCTIONS)
00500       fprintf (doc_stream,
00501           "   { (VFunction *)%s, \"%s\", (FUNCTION_KEYSEQ *)0, %s },\n",
00502           func, func_name, doc);
00503 #else /* !NAMED_FUNCTIONS */
00504       fprintf (doc_stream,
00505           "   { (VFunction *) %s, (FUNCTION_KEYSEQ *)0, %s },\n", func, doc);
00506 #endif /* !NAMED_FUNCTIONS */
00507 
00508       fprintf (key_stream, "   { \"%s\", A_%s },\n", func_name, func);
00509 
00510 #else /* !INFOKEY */
00511 
00512 #if defined (NAMED_FUNCTIONS)
00513       fprintf (doc_stream, "   { %s, \"%s\", %s },\n", func, func_name, doc);
00514 #else /* !NAMED_FUNCTIONS */
00515       fprintf (doc_stream, "   { %s, %s },\n", func, doc);
00516 #endif /* !NAMED_FUNCTIONS */
00517 
00518 #endif /* !INFOKEY */
00519 
00520 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
00521       free (func_name);
00522 #endif /* INFOKEY || NAMED_FUNCTIONS */
00523 
00524 #if defined (INFOKEY)
00525       fprintf (funs_stream, "#define A_%s %u\n", func, next_func_key());
00526 #endif /* INFOKEY */
00527       fprintf (funs_stream,
00528           "extern void %s (WINDOW *window, int count, unsigned char key);\n",
00529           func);
00530       free (func);
00531       free (doc);
00532     }
00533   free (buffer);
00534 
00535   /* If we created any tags, remember this file on our global list.  Otherwise,
00536      free the memory already allocated to it. */
00537   if (block->entries)
00538     add_pointer_to_array (block, emacs_tags_index, emacs_tags,
00539                           emacs_tags_slots, 10, EMACS_TAG_BLOCK *);
00540   else
00541     {
00542       free (block->filename);
00543       free (block);
00544     }
00545 }
00546 
00547 static void
00548 fatal_file_error (char *filename)
00549 {
00550   fprintf (stderr, _("Couldn't manipulate the file %s.\n"), filename);
00551   xexit (2);
00552 }
00553 
00554 static FILE *
00555 must_fopen (char *filename, char *mode)
00556 {
00557   FILE *stream;
00558 
00559   stream = fopen (filename, mode);
00560   if (!stream)
00561     fatal_file_error (filename);
00562 
00563   return (stream);
00564 }
00565 
00566 static unsigned int func_key;
00567 
00568 static void
00569 init_func_key(unsigned int val)
00570 {
00571        func_key = val;
00572 }
00573 
00574 static unsigned int
00575 next_func_key(void)
00576 {
00577        return func_key++;
00578 }