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. */
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.",
44 " This file was automatically made from various source files with the",
45 " command `%s'. DO NOT EDIT THIS FILE, only `%s.c'.",
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. */",
57 "#include \"info.h\"",
58 "#include \"funs.h\"",
60 "FUNCTION_DOC function_doc_array[] = {",
65 static char *key_header
[] = {
66 "/* key.c -- Generated array containing function names.",
68 " This file was automatically made from various source files with the",
69 " command \"%s\". DO NOT EDIT THIS FILE, only \"%s.c\".",
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. */",
80 "#include \"funs.h\"",
82 "FUNCTION_KEY function_key_array[] = {",
87 /* How to remember the locations of the functions found so that Emacs
88 can use the information in a tag table. */
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. */
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. */
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
)
121 FILE *funs_stream
, *doc_stream
;
126 char *dot
= strrchr (argv
[0], '.');
128 if (dot
&& FILENAME_CMP (dot
, ".exe") == 0)
133 for (i
= 1; i
< argc
; i
++)
134 if (strcmp (argv
[i
], "-tags") == 0)
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",
159 for (i
= 0; doc_header
[i
]; i
++)
161 fprintf (doc_stream
, doc_header
[i
], argv
[0], argv
[0]);
162 fprintf (doc_stream
, "\n");
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");
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
]);
192 for (i
= 1; i
< argc
; i
++)
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",
205 process_one_file (curfile
, doc_stream
, key_stream
, funs_stream
);
208 #if defined (INFOKEY)
210 #if defined (NAMED_FUNCTIONS)
212 " { NULL, NULL, NULL, NULL }\n};\n");
213 #else /* !NAMED_FUNCTIONS */
214 fprintf (doc_stream
, " { NULL, NULL, NULL }\n};\n");
215 #endif /* !NAMED_FUNCTIONS */
219 #if defined (NAMED_FUNCTIONS)
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
);
238 maybe_dump_tags (stdout
);
242 /* Dumping out the contents of an Emacs tags table. */
244 maybe_dump_tags (FILE *stream
)
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
++)
258 register EMACS_TAG_BLOCK
*block
;
259 register EMACS_TAG
*etag
;
263 block
= emacs_tags
[i
];
265 /* Calculate the length of the dumped block first. */
266 for (j
= 0; j
< block
->entries_index
; j
++)
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
);
299 block
->entries
= NULL
;
300 block
->entries_index
= 0;
301 block
->entries_slots
= 0;
306 add_tag_to_block (EMACS_TAG_BLOCK
*block
,
307 char *name
, int line
, long int char_offset
)
311 tag
= xmalloc (sizeof (EMACS_TAG
));
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. */
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
;
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
);
350 decl_str
= DECLARATION_STRING
;
351 decl_len
= strlen (decl_str
);
353 block
= make_emacs_tag_block (filename
);
362 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
364 #endif /* INFOKEY || NAMED_FUNCTIONS */
366 for (; offset
< (file_size
- decl_len
); offset
++)
368 if (buffer
[offset
] == '\n')
371 line_start
= offset
+ 1;
374 if (strncmp (buffer
+ offset
, decl_str
, decl_len
) == 0)
385 /* Skip forward until we find the open paren. */
386 while (point
< file_size
)
388 if (buffer
[point
] == '\n')
391 line_start
= point
+ 1;
393 else if (buffer
[point
] == '(')
399 while (point
++ < file_size
)
401 if (!whitespace_or_newline (buffer
[point
]))
403 else if (buffer
[point
] == '\n')
406 line_start
= point
+ 1;
410 if (point
>= file_size
)
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. */
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. */
437 if (strncmp (name_start
, "info_", 5) == 0)
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);
451 func_name
= temp_func_name
;
454 for (i
= 0; func_name
[i
]; i
++)
455 if (func_name
[i
] == '_')
458 #endif /* INFOKEY || NAMED_FUNCTIONS */
460 /* Find doc string. */
463 while (point
< file_size
)
465 if (buffer
[point
] == '\n')
468 line_start
= point
+ 1;
471 if (buffer
[point
] == '"')
479 while (offset
< file_size
)
481 if (buffer
[offset
] == '\n')
484 line_start
= offset
+ 1;
487 if (buffer
[offset
] == '\\')
489 else if (buffer
[offset
] == '"')
496 if (offset
>= file_size
)
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)
507 " { (VFunction *)%s, \"%s\", (FUNCTION_KEYSEQ *)0, %s },\n",
508 func
, func_name
, doc
);
509 #else /* !NAMED_FUNCTIONS */
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
);
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)
528 #endif /* INFOKEY || NAMED_FUNCTIONS */
530 #if defined (INFOKEY)
531 fprintf (funs_stream
, "#define A_%s %u\n", func
, next_func_key());
533 fprintf (funs_stream
,
534 "extern void %s (WINDOW *window, int count, unsigned char key);\n",
541 /* If we created any tags, remember this file on our global list. Otherwise,
542 free the memory already allocated to it. */
544 add_pointer_to_array (block
, emacs_tags_index
, emacs_tags
,
545 emacs_tags_slots
, 10, EMACS_TAG_BLOCK
*);
548 free (block
->filename
);
554 fatal_file_error (char *filename
)
556 fprintf (stderr
, _("Couldn't manipulate the file %s.\n"), filename
);
561 must_fopen (char *filename
, char *mode
)
565 stream
= fopen (filename
, mode
);
567 fatal_file_error (filename
);
572 static unsigned int func_key
;
575 init_func_key(unsigned int val
)