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