Upgrade Texinfo from 4.8 to 4.13 on the vendor branch
[dragonfly.git] / contrib / texinfo / info / makedoc.c
1 /* makedoc.c -- make doc.c and funs.h from input files.
2    $Id: makedoc.c,v 1.10 2008/06/11 09:55:42 gray Exp $
3
4    Copyright (C) 1993, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2007,
5    2008 Free Software Foundation, Inc.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20    Originally written by Brian Fox (bfox@ai.mit.edu). */
21
22 /* This program grovels the contents of the source files passed as arguments
23    and writes out a file of function pointers and documentation strings, and
24    a header file which describes the contents.  This only does the functions
25    declared with DECLARE_INFO_COMMAND. */
26
27 #include "info.h"
28 #include "infokey.h"
29
30 char *program_name = "makedoc";
31
32 static void fatal_file_error (char *filename);
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 static char *key_filename = "key.c";
40
41 static char *doc_header[] = {
42   "/* doc.c -- Generated structure containing function names and doc strings.",
43   "",
44   "   This file was automatically made from various source files with the",
45   "   command `%s'.  DO NOT EDIT THIS FILE, only `%s.c'.",
46   NULL
47 };
48
49 static char *doc_header_1[] = {
50   "   An entry in the array FUNCTION_DOC_ARRAY is made for each command",
51   "   found in the above files; each entry consists of a function pointer,",
52 #if defined (NAMED_FUNCTIONS)
53   "   a string which is the user-visible name of the function,",
54 #endif /* NAMED_FUNCTIONS */
55   "   and a string which documents its purpose. */",
56   "",
57   "#include \"info.h\"",
58   "#include \"funs.h\"",
59   "",
60   "FUNCTION_DOC function_doc_array[] = {",
61   "",
62   NULL
63 };
64
65 static char *key_header[] = {
66   "/* key.c -- Generated array containing function names.",
67   "",
68   "   This file was automatically made from various source files with the",
69   "   command \"%s\".  DO NOT EDIT THIS FILE, only \"%s.c\".",
70   "",
71   NULL
72 };
73
74 static char *key_header_1[] = {
75   "   An entry in the array FUNCTION_KEY_ARRAY is made for each command",
76   "   found in the above files; each entry consists of",
77   "   a string which is the user-visible name of the function.  */",
78   "",
79   "#include \"key.h\"",
80   "#include \"funs.h\"",
81   "",
82   "FUNCTION_KEY function_key_array[] = {",
83   "",
84   NULL
85 };
86
87 /* How to remember the locations of the functions found so that Emacs
88    can use the information in a tag table. */
89 typedef struct {
90   char *name;                   /* Name of the tag. */
91   int line;                     /* Line number at which it appears. */
92   long char_offset;             /* Character offset at which it appears. */
93 } EMACS_TAG;
94
95 typedef struct {
96   char *filename;               /* Name of the file containing entries. */
97   long entrylen;                /* Total number of characters in tag block. */
98   EMACS_TAG **entries;          /* Entries found in FILENAME. */
99   int entries_index;
100   int entries_slots;
101 } EMACS_TAG_BLOCK;
102
103 EMACS_TAG_BLOCK **emacs_tags = NULL;
104 int emacs_tags_index = 0;
105 int emacs_tags_slots = 0;
106
107 #define DECLARATION_STRING "\nDECLARE_INFO_COMMAND"
108
109 static void process_one_file (char *filename, FILE *doc_stream,
110     FILE *key_stream, FILE *funs_stream);
111 static void maybe_dump_tags (FILE *stream);
112 static FILE *must_fopen (char *filename, char *mode);
113 static void init_func_key (unsigned int val);
114 static unsigned int next_func_key (void);
115
116 int
117 main (int argc, char **argv)
118 {
119   register int i;
120   int tags_only = 0;
121   FILE *funs_stream, *doc_stream;
122   FILE *key_stream;
123
124 #if STRIP_DOT_EXE
125   {
126     char *dot = strrchr (argv[0], '.');
127
128     if (dot && FILENAME_CMP (dot, ".exe") == 0)
129       *dot = 0;
130   }
131 #endif
132
133   for (i = 1; i < argc; i++)
134     if (strcmp (argv[i], "-tags") == 0)
135       {
136         tags_only++;
137         break;
138       }
139
140   if (tags_only)
141     {
142       funs_filename = NULL_DEVICE;
143       doc_filename = NULL_DEVICE;
144       key_filename = NULL_DEVICE;
145     }
146
147   /* The order of these calls depends exactly on the order in the
148      Makefile.{in,am}, or they might fail on filesystems with
149      high-precision times; see also the fclose calls below.  */
150   funs_stream = must_fopen (funs_filename, "w");
151   key_stream = must_fopen (key_filename, "w");
152   doc_stream = must_fopen (doc_filename, "w");
153
154   fprintf (funs_stream,
155       "/* %s -- Generated declarations for Info commands. */\n\n\
156 #include \"info.h\"\n",
157       funs_filename);
158
159   for (i = 0; doc_header[i]; i++)
160     {
161       fprintf (doc_stream, doc_header[i], argv[0], argv[0]);
162       fprintf (doc_stream, "\n");
163     }
164
165   fprintf (doc_stream,
166            _("   Source files groveled to make this file include:\n\n"));
167
168   for (i = 0; key_header[i]; i++)
169     {
170       fprintf (key_stream, key_header[i], argv[0], argv[0]);
171       fprintf (key_stream, "\n");
172     }
173   fprintf (key_stream,
174            _("   Source files groveled to make this file include:\n\n"));
175
176   for (i = 1; i < argc; i++)
177     {
178       fprintf (doc_stream, "\t%s\n", argv[i]);
179       fprintf (key_stream, "\t%s\n", argv[i]);
180     }
181
182   fprintf (doc_stream, "\n");
183   for (i = 0; doc_header_1[i]; i++)
184     fprintf (doc_stream, "%s\n", doc_header_1[i]);
185
186   fprintf (key_stream, "\n");
187   for (i = 0; key_header_1[i]; i++)
188     fprintf (key_stream, "%s\n", key_header_1[i]);
189
190   init_func_key(0);
191
192   for (i = 1; i < argc; i++)
193     {
194       char *curfile;
195       curfile = argv[i];
196
197       if (*curfile == '-')
198         continue;
199
200       fprintf (doc_stream, "/* Commands found in \"%s\". */\n", curfile);
201       fprintf (key_stream, "/* Commands found in \"%s\". */\n", curfile);
202       fprintf (funs_stream, "\n/* Functions declared in \"%s\". */\n",
203                curfile);
204
205       process_one_file (curfile, doc_stream, key_stream, funs_stream);
206     }
207
208 #if defined (INFOKEY)
209
210 #if defined (NAMED_FUNCTIONS)
211   fprintf (doc_stream,
212            "   { NULL, NULL, NULL, NULL }\n};\n");
213 #else /* !NAMED_FUNCTIONS */
214   fprintf (doc_stream, "   { NULL, NULL, NULL }\n};\n");
215 #endif /* !NAMED_FUNCTIONS */
216
217 #else /* !INFOKEY */
218
219 #if defined (NAMED_FUNCTIONS)
220   fprintf (doc_stream,
221            "   { NULL, NULL, NULL }\n};\n");
222 #else /* !NAMED_FUNCTIONS */
223   fprintf (doc_stream, "   { NULL, NULL }\n};\n");
224 #endif /* !NAMED_FUNCTIONS */
225
226 #endif /* !INFOKEY */
227
228   fprintf (key_stream, "   { NULL, 0 }\n};\n");
229   fprintf (funs_stream, "\n#define A_NCOMMANDS %u\n", next_func_key());
230
231   /* The order of these calls also depends exactly on the order in the
232    * Makefile.{in,am}; see the must_fopen calls above.  */
233   fclose (funs_stream);
234   fclose (key_stream);
235   fclose (doc_stream);
236
237   if (tags_only)
238     maybe_dump_tags (stdout);
239   return 0;
240 }
241
242 /* Dumping out the contents of an Emacs tags table. */
243 static void
244 maybe_dump_tags (FILE *stream)
245 {
246   register int i;
247
248   /* Emacs needs its TAGS file to be in Unix text format (i.e., only
249      newline at end of every line, no CR), so when we generate a
250      TAGS table, we must switch the output stream to binary mode.
251      (If the table is written to a terminal, this is obviously not needed.) */
252   SET_BINARY (fileno (stream));
253
254   /* Print out the information for each block. */
255   for (i = 0; i < emacs_tags_index; i++)
256     {
257       register int j;
258       register EMACS_TAG_BLOCK *block;
259       register EMACS_TAG *etag;
260       long block_len;
261
262       block_len = 0;
263       block = emacs_tags[i];
264
265       /* Calculate the length of the dumped block first. */
266       for (j = 0; j < block->entries_index; j++)
267         {
268           char digits[30];
269           etag = block->entries[j];
270           block_len += 3 + strlen (etag->name);
271           sprintf (digits, "%d,%ld", etag->line, etag->char_offset);
272           block_len += strlen (digits);
273         }
274
275       /* Print out the defining line. */
276       fprintf (stream, "\f\n%s,%ld\n", block->filename, block_len);
277
278       /* Print out the individual tags. */
279       for (j = 0; j < block->entries_index; j++)
280         {
281           etag = block->entries[j];
282
283           fprintf (stream, "%s,\177%d,%ld\n",
284                    etag->name, etag->line, etag->char_offset);
285         }
286     }
287 }
288
289 /* Keeping track of names, line numbers and character offsets of functions
290    found in source files. */
291 static EMACS_TAG_BLOCK *
292 make_emacs_tag_block (char *filename)
293 {
294   EMACS_TAG_BLOCK *block;
295
296   block = xmalloc (sizeof (EMACS_TAG_BLOCK));
297   block->filename = xstrdup (filename);
298   block->entrylen = 0;
299   block->entries = NULL;
300   block->entries_index = 0;
301   block->entries_slots = 0;
302   return block;
303 }
304
305 static void
306 add_tag_to_block (EMACS_TAG_BLOCK *block,
307     char *name, int line, long int char_offset)
308 {
309   EMACS_TAG *tag;
310
311   tag = xmalloc (sizeof (EMACS_TAG));
312   tag->name = name;
313   tag->line = line;
314   tag->char_offset = char_offset;
315   add_pointer_to_array (tag, block->entries_index, block->entries,
316                         block->entries_slots, 50, EMACS_TAG *);
317 }
318
319 /* Read the file represented by FILENAME into core, and search it for Info
320    function declarations.  Output the declarations in various forms to the
321    DOC_STREAM, KEY_STREAM, and FUNS_STREAM. */
322 static void
323 process_one_file (char *filename, FILE *doc_stream,
324     FILE *key_stream, FILE *funs_stream)
325 {
326   int descriptor, decl_len;
327   char *buffer, *decl_str;
328   struct stat finfo;
329   long offset;
330   long file_size;
331   EMACS_TAG_BLOCK *block;
332
333   if (stat (filename, &finfo) == -1)
334     fatal_file_error (filename);
335
336   descriptor = open (filename, O_RDONLY, 0666);
337
338   if (descriptor == -1)
339     fatal_file_error (filename);
340
341   file_size = (long) finfo.st_size;
342   buffer = xmalloc (1 + file_size);
343   /* On some systems, the buffer will actually contain
344      less characters than the full file's size, because
345      the CR characters are removed from line endings.  */
346   file_size = read (descriptor, buffer, file_size);
347   close (descriptor);
348
349   offset = 0;
350   decl_str = DECLARATION_STRING;
351   decl_len = strlen (decl_str);
352
353   block = make_emacs_tag_block (filename);
354
355   while (1)
356     {
357       long point = 0;
358       long line_start = 0;
359       int line_number = 0;
360
361       char *func, *doc;
362 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
363       char *func_name;
364 #endif /* INFOKEY || NAMED_FUNCTIONS */
365
366       for (; offset < (file_size - decl_len); offset++)
367         {
368           if (buffer[offset] == '\n')
369             {
370               line_number++;
371               line_start = offset + 1;
372             }
373
374           if (strncmp (buffer + offset, decl_str, decl_len) == 0)
375             {
376               offset += decl_len;
377               point = offset;
378               break;
379             }
380         }
381
382       if (!point)
383         break;
384
385       /* Skip forward until we find the open paren. */
386       while (point < file_size)
387         {
388           if (buffer[point] == '\n')
389             {
390               line_number++;
391               line_start = point + 1;
392             }
393           else if (buffer[point] == '(')
394             break;
395
396           point++;
397         }
398
399       while (point++ < file_size)
400         {
401           if (!whitespace_or_newline (buffer[point]))
402             break;
403           else if (buffer[point] == '\n')
404             {
405               line_number++;
406               line_start = point + 1;
407             }
408         }
409
410       if (point >= file_size)
411         break;
412
413       /* Now looking at name of function.  Get it. */
414       for (offset = point; buffer[offset] != ','; offset++);
415       func = xmalloc (1 + (offset - point));
416       strncpy (func, buffer + point, offset - point);
417       func[offset - point] = '\0';
418
419       /* Remember this tag in the current block. */
420       {
421         char *tag_name;
422
423         tag_name = xmalloc (1 + (offset - line_start));
424         strncpy (tag_name, buffer + line_start, offset - line_start);
425         tag_name[offset - line_start] = '\0';
426         add_tag_to_block (block, tag_name, line_number, point);
427       }
428
429 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
430       /* Generate the user-visible function name from the function's name. */
431       {
432         register int i;
433         char *name_start;
434
435         name_start = func;
436
437         if (strncmp (name_start, "info_", 5) == 0)
438           name_start += 5;
439
440         func_name = xstrdup (name_start);
441
442         /* Fix up "ea" commands. */
443         if (strncmp (func_name, "ea_", 3) == 0)
444           {
445             char *temp_func_name;
446
447             temp_func_name = xmalloc (10 + strlen (func_name));
448             strcpy (temp_func_name, "echo_area_");
449             strcat (temp_func_name, func_name + 3);
450             free (func_name);
451             func_name = temp_func_name;
452           }
453
454         for (i = 0; func_name[i]; i++)
455           if (func_name[i] == '_')
456             func_name[i] = '-';
457       }
458 #endif /* INFOKEY || NAMED_FUNCTIONS */
459
460       /* Find doc string. */
461       point = offset + 1;
462
463       while (point < file_size)
464         {
465           if (buffer[point] == '\n')
466             {
467               line_number++;
468               line_start = point + 1;
469             }
470
471           if (buffer[point] == '"')
472             break;
473           else
474             point++;
475         }
476
477       offset = point + 1;
478
479       while (offset < file_size)
480         {
481           if (buffer[offset] == '\n')
482             {
483               line_number++;
484               line_start = offset + 1;
485             }
486
487           if (buffer[offset] == '\\')
488             offset += 2;
489           else if (buffer[offset] == '"')
490             break;
491           else
492             offset++;
493         }
494
495       offset++;
496       if (offset >= file_size)
497         break;
498
499       doc = xmalloc (1 + (offset - point));
500       strncpy (doc, buffer + point, offset - point);
501       doc[offset - point] = '\0';
502
503 #if defined (INFOKEY)
504
505 #if defined (NAMED_FUNCTIONS)
506       fprintf (doc_stream,
507           "   { (VFunction *)%s, \"%s\", (FUNCTION_KEYSEQ *)0, %s },\n",
508           func, func_name, doc);
509 #else /* !NAMED_FUNCTIONS */
510       fprintf (doc_stream,
511           "   { (VFunction *) %s, (FUNCTION_KEYSEQ *)0, %s },\n", func, doc);
512 #endif /* !NAMED_FUNCTIONS */
513
514       fprintf (key_stream, "   { \"%s\", A_%s },\n", func_name, func);
515
516 #else /* !INFOKEY */
517
518 #if defined (NAMED_FUNCTIONS)
519       fprintf (doc_stream, "   { %s, \"%s\", %s },\n", func, func_name, doc);
520 #else /* !NAMED_FUNCTIONS */
521       fprintf (doc_stream, "   { %s, %s },\n", func, doc);
522 #endif /* !NAMED_FUNCTIONS */
523
524 #endif /* !INFOKEY */
525
526 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
527       free (func_name);
528 #endif /* INFOKEY || NAMED_FUNCTIONS */
529
530 #if defined (INFOKEY)
531       fprintf (funs_stream, "#define A_%s %u\n", func, next_func_key());
532 #endif /* INFOKEY */
533       fprintf (funs_stream,
534           "extern void %s (WINDOW *window, int count, unsigned char key);\n",
535           func);
536       free (func);
537       free (doc);
538     }
539   free (buffer);
540
541   /* If we created any tags, remember this file on our global list.  Otherwise,
542      free the memory already allocated to it. */
543   if (block->entries)
544     add_pointer_to_array (block, emacs_tags_index, emacs_tags,
545                           emacs_tags_slots, 10, EMACS_TAG_BLOCK *);
546   else
547     {
548       free (block->filename);
549       free (block);
550     }
551 }
552
553 static void
554 fatal_file_error (char *filename)
555 {
556   fprintf (stderr, _("Couldn't manipulate the file %s.\n"), filename);
557   xexit (2);
558 }
559
560 static FILE *
561 must_fopen (char *filename, char *mode)
562 {
563   FILE *stream;
564
565   stream = fopen (filename, mode);
566   if (!stream)
567     fatal_file_error (filename);
568
569   return stream;
570 }
571
572 static unsigned int func_key;
573
574 static void
575 init_func_key(unsigned int val)
576 {
577         func_key = val;
578 }
579
580 static unsigned int
581 next_func_key(void)
582 {
583         return func_key++;
584 }