Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / texinfo / info / makedoc.c
blobbeefcd5110817e121264b3ecbea3f88e7d6e80f1
1 /* $NetBSD$ */
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
6 Copyright (C) 1993, 1997, 1998, 1999, 2001, 2002, 2003, 2004 Free Software
7 Foundation, Inc.
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.
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.
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.
23 Originally written by Brian Fox (bfox@ai.mit.edu). */
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. */
30 #include "info.h"
31 #include "infokey.h"
33 static void fatal_file_error (char *filename);
35 /* Name of the header file which receives the declarations of functions. */
36 static char *funs_filename = "funs.h";
38 /* Name of the documentation to function pointer file. */
39 static char *doc_filename = "doc.c";
40 static char *key_filename = "key.c";
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
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
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
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
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;
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;
104 EMACS_TAG_BLOCK **emacs_tags = (EMACS_TAG_BLOCK **)NULL;
105 int emacs_tags_index = 0;
106 int emacs_tags_slots = 0;
108 #define DECLARATION_STRING "\nDECLARE_INFO_COMMAND"
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);
118 main (int argc, char **argv)
120 register int i;
121 int tags_only = 0;
122 FILE *funs_stream, *doc_stream;
123 FILE *key_stream;
125 #if STRIP_DOT_EXE
127 char *dot = strrchr (argv[0], '.');
129 if (dot && FILENAME_CMP (dot, ".exe") == 0)
130 *dot = 0;
132 #endif
134 for (i = 1; i < argc; i++)
135 if (strcmp (argv[i], "-tags") == 0)
137 tags_only++;
138 break;
141 if (tags_only)
143 funs_filename = NULL_DEVICE;
144 doc_filename = NULL_DEVICE;
145 key_filename = NULL_DEVICE;
148 funs_stream = must_fopen (funs_filename, "w");
149 doc_stream = must_fopen (doc_filename, "w");
150 key_stream = must_fopen (key_filename, "w");
152 fprintf (funs_stream,
153 "/* %s -- Generated declarations for Info commands. */\n\n\
154 #include \"info.h\"\n",
155 funs_filename);
157 for (i = 0; doc_header[i]; i++)
159 fprintf (doc_stream, doc_header[i], argv[0], argv[0]);
160 fprintf (doc_stream, "\n");
163 fprintf (doc_stream,
164 _(" Source files groveled to make this file include:\n\n"));
166 for (i = 0; key_header[i]; i++)
168 fprintf (key_stream, key_header[i], argv[0], argv[0]);
169 fprintf (key_stream, "\n");
171 fprintf (key_stream,
172 _(" Source files groveled to make this file include:\n\n"));
174 for (i = 1; i < argc; i++)
176 fprintf (doc_stream, "\t%s\n", argv[i]);
177 fprintf (key_stream, "\t%s\n", argv[i]);
180 fprintf (doc_stream, "\n");
181 for (i = 0; doc_header_1[i]; i++)
182 fprintf (doc_stream, "%s\n", doc_header_1[i]);
184 fprintf (key_stream, "\n");
185 for (i = 0; key_header_1[i]; i++)
186 fprintf (key_stream, "%s\n", key_header_1[i]);
188 init_func_key(0);
190 for (i = 1; i < argc; i++)
192 char *curfile;
193 curfile = argv[i];
195 if (*curfile == '-')
196 continue;
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);
203 process_one_file (curfile, doc_stream, key_stream, funs_stream);
206 #if defined (INFOKEY)
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 */
215 #else /* !INFOKEY */
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 */
224 #endif /* !INFOKEY */
226 fprintf (key_stream, " { (char *)NULL, 0 }\n};\n");
227 fprintf (funs_stream, "\n#define A_NCOMMANDS %u\n", next_func_key());
229 fclose (funs_stream);
230 fclose (doc_stream);
231 fclose (key_stream);
233 if (tags_only)
234 maybe_dump_tags (stdout);
235 return 0;
238 /* Dumping out the contents of an Emacs tags table. */
239 static void
240 maybe_dump_tags (FILE *stream)
242 register int i;
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));
250 /* Print out the information for each block. */
251 for (i = 0; i < emacs_tags_index; i++)
253 register int j;
254 register EMACS_TAG_BLOCK *block;
255 register EMACS_TAG *etag;
256 long block_len;
258 block_len = 0;
259 block = emacs_tags[i];
261 /* Calculate the length of the dumped block first. */
262 for (j = 0; j < block->entries_index; j++)
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);
271 /* Print out the defining line. */
272 fprintf (stream, "\f\n%s,%ld\n", block->filename, block_len);
274 /* Print out the individual tags. */
275 for (j = 0; j < block->entries_index; j++)
277 etag = block->entries[j];
279 fprintf (stream, "%s,\177%d,%ld\n",
280 etag->name, etag->line, etag->char_offset);
285 /* Keeping track of names, line numbers and character offsets of functions
286 found in source files. */
287 static EMACS_TAG_BLOCK *
288 make_emacs_tag_block (char *filename)
290 EMACS_TAG_BLOCK *block;
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);
301 static void
302 add_tag_to_block (EMACS_TAG_BLOCK *block,
303 char *name, int line, long int char_offset)
305 EMACS_TAG *tag;
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 *);
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
319 process_one_file (char *filename, FILE *doc_stream,
320 FILE *key_stream, FILE *funs_stream)
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;
329 if (stat (filename, &finfo) == -1)
330 fatal_file_error (filename);
332 descriptor = open (filename, O_RDONLY, 0666);
334 if (descriptor == -1)
335 fatal_file_error (filename);
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);
345 offset = 0;
346 decl_str = DECLARATION_STRING;
347 decl_len = strlen (decl_str);
349 block = make_emacs_tag_block (filename);
351 while (1)
353 long point = 0;
354 long line_start = 0;
355 int line_number = 0;
357 char *func, *doc;
358 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
359 char *func_name;
360 #endif /* INFOKEY || NAMED_FUNCTIONS */
362 for (; offset < (file_size - decl_len); offset++)
364 if (buffer[offset] == '\n')
366 line_number++;
367 line_start = offset + 1;
370 if (strncmp (buffer + offset, decl_str, decl_len) == 0)
372 offset += decl_len;
373 point = offset;
374 break;
378 if (!point)
379 break;
381 /* Skip forward until we find the open paren. */
382 while (point < file_size)
384 if (buffer[point] == '\n')
386 line_number++;
387 line_start = point + 1;
389 else if (buffer[point] == '(')
390 break;
392 point++;
395 while (point++ < file_size)
397 if (!whitespace_or_newline (buffer[point]))
398 break;
399 else if (buffer[point] == '\n')
401 line_number++;
402 line_start = point + 1;
406 if (point >= file_size)
407 break;
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';
415 /* Remember this tag in the current block. */
417 char *tag_name;
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);
425 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
426 /* Generate the user-visible function name from the function's name. */
428 register int i;
429 char *name_start;
431 name_start = func;
433 if (strncmp (name_start, "info_", 5) == 0)
434 name_start += 5;
436 func_name = xstrdup (name_start);
438 /* Fix up "ea" commands. */
439 if (strncmp (func_name, "ea_", 3) == 0)
441 char *temp_func_name;
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;
450 for (i = 0; func_name[i]; i++)
451 if (func_name[i] == '_')
452 func_name[i] = '-';
454 #endif /* INFOKEY || NAMED_FUNCTIONS */
456 /* Find doc string. */
457 point = offset + 1;
459 while (point < file_size)
461 if (buffer[point] == '\n')
463 line_number++;
464 line_start = point + 1;
467 if (buffer[point] == '"')
468 break;
469 else
470 point++;
473 offset = point + 1;
475 while (offset < file_size)
477 if (buffer[offset] == '\n')
479 line_number++;
480 line_start = offset + 1;
483 if (buffer[offset] == '\\')
484 offset += 2;
485 else if (buffer[offset] == '"')
486 break;
487 else
488 offset++;
491 offset++;
492 if (offset >= file_size)
493 break;
495 doc = (char *)xmalloc (1 + (offset - point));
496 strncpy (doc, buffer + point, offset - point);
497 doc[offset - point] = '\0';
499 #if defined (INFOKEY)
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 */
510 fprintf (key_stream, " { \"%s\", A_%s },\n", func_name, func);
512 #else /* !INFOKEY */
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 */
520 #endif /* !INFOKEY */
522 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
523 free (func_name);
524 #endif /* INFOKEY || NAMED_FUNCTIONS */
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);
535 free (buffer);
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
544 free (block->filename);
545 free (block);
549 static void
550 fatal_file_error (char *filename)
552 fprintf (stderr, _("Couldn't manipulate the file %s.\n"), filename);
553 xexit (2);
556 static FILE *
557 must_fopen (char *filename, char *mode)
559 FILE *stream;
561 stream = fopen (filename, mode);
562 if (!stream)
563 fatal_file_error (filename);
565 return (stream);
568 static unsigned int func_key;
570 static void
571 init_func_key(unsigned int val)
573 func_key = val;
576 static unsigned int
577 next_func_key(void)
579 return func_key++;