test - Little program to list processes from coredumps or kmem.
[dragonfly.git] / contrib / texinfo / info / makedoc.c
blob21fcb3b7f2ff6566ff87c6846638c7f43b245503
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 $
4 Copyright (C) 1993, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2007,
5 2008 Free Software Foundation, Inc.
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.
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.
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/>.
20 Originally written by Brian Fox (bfox@ai.mit.edu). */
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. */
27 #include "info.h"
28 #include "infokey.h"
30 char *program_name = "makedoc";
32 static void fatal_file_error (char *filename);
34 /* Name of the header file which receives the declarations of functions. */
35 static char *funs_filename = "funs.h";
37 /* Name of the documentation to function pointer file. */
38 static char *doc_filename = "doc.c";
39 static char *key_filename = "key.c";
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
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
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
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
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;
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;
103 EMACS_TAG_BLOCK **emacs_tags = NULL;
104 int emacs_tags_index = 0;
105 int emacs_tags_slots = 0;
107 #define DECLARATION_STRING "\nDECLARE_INFO_COMMAND"
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);
117 main (int argc, char **argv)
119 register int i;
120 int tags_only = 0;
121 FILE *funs_stream, *doc_stream;
122 FILE *key_stream;
124 #if STRIP_DOT_EXE
126 char *dot = strrchr (argv[0], '.');
128 if (dot && FILENAME_CMP (dot, ".exe") == 0)
129 *dot = 0;
131 #endif
133 for (i = 1; i < argc; i++)
134 if (strcmp (argv[i], "-tags") == 0)
136 tags_only++;
137 break;
140 if (tags_only)
142 funs_filename = NULL_DEVICE;
143 doc_filename = NULL_DEVICE;
144 key_filename = NULL_DEVICE;
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");
154 fprintf (funs_stream,
155 "/* %s -- Generated declarations for Info commands. */\n\n\
156 #include \"info.h\"\n",
157 funs_filename);
159 for (i = 0; doc_header[i]; i++)
161 fprintf (doc_stream, doc_header[i], argv[0], argv[0]);
162 fprintf (doc_stream, "\n");
165 fprintf (doc_stream,
166 _(" Source files groveled to make this file include:\n\n"));
168 for (i = 0; key_header[i]; i++)
170 fprintf (key_stream, key_header[i], argv[0], argv[0]);
171 fprintf (key_stream, "\n");
173 fprintf (key_stream,
174 _(" Source files groveled to make this file include:\n\n"));
176 for (i = 1; i < argc; i++)
178 fprintf (doc_stream, "\t%s\n", argv[i]);
179 fprintf (key_stream, "\t%s\n", argv[i]);
182 fprintf (doc_stream, "\n");
183 for (i = 0; doc_header_1[i]; i++)
184 fprintf (doc_stream, "%s\n", doc_header_1[i]);
186 fprintf (key_stream, "\n");
187 for (i = 0; key_header_1[i]; i++)
188 fprintf (key_stream, "%s\n", key_header_1[i]);
190 init_func_key(0);
192 for (i = 1; i < argc; i++)
194 char *curfile;
195 curfile = argv[i];
197 if (*curfile == '-')
198 continue;
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);
205 process_one_file (curfile, doc_stream, key_stream, funs_stream);
208 #if defined (INFOKEY)
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 */
217 #else /* !INFOKEY */
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 */
226 #endif /* !INFOKEY */
228 fprintf (key_stream, " { NULL, 0 }\n};\n");
229 fprintf (funs_stream, "\n#define A_NCOMMANDS %u\n", next_func_key());
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);
237 if (tags_only)
238 maybe_dump_tags (stdout);
239 return 0;
242 /* Dumping out the contents of an Emacs tags table. */
243 static void
244 maybe_dump_tags (FILE *stream)
246 register int i;
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));
254 /* Print out the information for each block. */
255 for (i = 0; i < emacs_tags_index; i++)
257 register int j;
258 register EMACS_TAG_BLOCK *block;
259 register EMACS_TAG *etag;
260 long block_len;
262 block_len = 0;
263 block = emacs_tags[i];
265 /* Calculate the length of the dumped block first. */
266 for (j = 0; j < block->entries_index; j++)
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);
275 /* Print out the defining line. */
276 fprintf (stream, "\f\n%s,%ld\n", block->filename, block_len);
278 /* Print out the individual tags. */
279 for (j = 0; j < block->entries_index; j++)
281 etag = block->entries[j];
283 fprintf (stream, "%s,\177%d,%ld\n",
284 etag->name, etag->line, etag->char_offset);
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)
294 EMACS_TAG_BLOCK *block;
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;
305 static void
306 add_tag_to_block (EMACS_TAG_BLOCK *block,
307 char *name, int line, long int char_offset)
309 EMACS_TAG *tag;
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 *);
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)
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;
333 if (stat (filename, &finfo) == -1)
334 fatal_file_error (filename);
336 descriptor = open (filename, O_RDONLY, 0666);
338 if (descriptor == -1)
339 fatal_file_error (filename);
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);
349 offset = 0;
350 decl_str = DECLARATION_STRING;
351 decl_len = strlen (decl_str);
353 block = make_emacs_tag_block (filename);
355 while (1)
357 long point = 0;
358 long line_start = 0;
359 int line_number = 0;
361 char *func, *doc;
362 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
363 char *func_name;
364 #endif /* INFOKEY || NAMED_FUNCTIONS */
366 for (; offset < (file_size - decl_len); offset++)
368 if (buffer[offset] == '\n')
370 line_number++;
371 line_start = offset + 1;
374 if (strncmp (buffer + offset, decl_str, decl_len) == 0)
376 offset += decl_len;
377 point = offset;
378 break;
382 if (!point)
383 break;
385 /* Skip forward until we find the open paren. */
386 while (point < file_size)
388 if (buffer[point] == '\n')
390 line_number++;
391 line_start = point + 1;
393 else if (buffer[point] == '(')
394 break;
396 point++;
399 while (point++ < file_size)
401 if (!whitespace_or_newline (buffer[point]))
402 break;
403 else if (buffer[point] == '\n')
405 line_number++;
406 line_start = point + 1;
410 if (point >= file_size)
411 break;
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';
419 /* Remember this tag in the current block. */
421 char *tag_name;
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);
429 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
430 /* Generate the user-visible function name from the function's name. */
432 register int i;
433 char *name_start;
435 name_start = func;
437 if (strncmp (name_start, "info_", 5) == 0)
438 name_start += 5;
440 func_name = xstrdup (name_start);
442 /* Fix up "ea" commands. */
443 if (strncmp (func_name, "ea_", 3) == 0)
445 char *temp_func_name;
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;
454 for (i = 0; func_name[i]; i++)
455 if (func_name[i] == '_')
456 func_name[i] = '-';
458 #endif /* INFOKEY || NAMED_FUNCTIONS */
460 /* Find doc string. */
461 point = offset + 1;
463 while (point < file_size)
465 if (buffer[point] == '\n')
467 line_number++;
468 line_start = point + 1;
471 if (buffer[point] == '"')
472 break;
473 else
474 point++;
477 offset = point + 1;
479 while (offset < file_size)
481 if (buffer[offset] == '\n')
483 line_number++;
484 line_start = offset + 1;
487 if (buffer[offset] == '\\')
488 offset += 2;
489 else if (buffer[offset] == '"')
490 break;
491 else
492 offset++;
495 offset++;
496 if (offset >= file_size)
497 break;
499 doc = xmalloc (1 + (offset - point));
500 strncpy (doc, buffer + point, offset - point);
501 doc[offset - point] = '\0';
503 #if defined (INFOKEY)
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 */
514 fprintf (key_stream, " { \"%s\", A_%s },\n", func_name, func);
516 #else /* !INFOKEY */
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 */
524 #endif /* !INFOKEY */
526 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
527 free (func_name);
528 #endif /* INFOKEY || NAMED_FUNCTIONS */
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);
539 free (buffer);
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
548 free (block->filename);
549 free (block);
553 static void
554 fatal_file_error (char *filename)
556 fprintf (stderr, _("Couldn't manipulate the file %s.\n"), filename);
557 xexit (2);
560 static FILE *
561 must_fopen (char *filename, char *mode)
563 FILE *stream;
565 stream = fopen (filename, mode);
566 if (!stream)
567 fatal_file_error (filename);
569 return stream;
572 static unsigned int func_key;
574 static void
575 init_func_key(unsigned int val)
577 func_key = val;
580 static unsigned int
581 next_func_key(void)
583 return func_key++;