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
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)
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. */
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.",
45 " This file was automatically made from various source files with the",
46 " command `%s'. DO NOT EDIT THIS FILE, only `%s.c'.",
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. */",
58 "#include \"info.h\"",
59 "#include \"funs.h\"",
61 "FUNCTION_DOC function_doc_array[] = {",
66 static char *key_header
[] = {
67 "/* key.c -- Generated array containing function names.",
69 " This file was automatically made from various source files with the",
70 " command \"%s\". DO NOT EDIT THIS FILE, only \"%s.c\".",
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. */",
81 "#include \"funs.h\"",
83 "FUNCTION_KEY function_key_array[] = {",
88 /* How to remember the locations of the functions found so that Emacs
89 can use the information in a tag table. */
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. */
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. */
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
)
122 FILE *funs_stream
, *doc_stream
;
127 char *dot
= strrchr (argv
[0], '.');
129 if (dot
&& FILENAME_CMP (dot
, ".exe") == 0)
134 for (i
= 1; i
< argc
; i
++)
135 if (strcmp (argv
[i
], "-tags") == 0)
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",
157 for (i
= 0; doc_header
[i
]; i
++)
159 fprintf (doc_stream
, doc_header
[i
], argv
[0], argv
[0]);
160 fprintf (doc_stream
, "\n");
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");
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
]);
190 for (i
= 1; i
< argc
; i
++)
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",
203 process_one_file (curfile
, doc_stream
, key_stream
, funs_stream
);
206 #if defined (INFOKEY)
208 #if defined (NAMED_FUNCTIONS)
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 */
217 #if defined (NAMED_FUNCTIONS)
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
);
234 maybe_dump_tags (stdout
);
238 /* Dumping out the contents of an Emacs tags table. */
240 maybe_dump_tags (FILE *stream
)
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
++)
254 register EMACS_TAG_BLOCK
*block
;
255 register EMACS_TAG
*etag
;
259 block
= emacs_tags
[i
];
261 /* Calculate the length of the dumped block first. */
262 for (j
= 0; j
< block
->entries_index
; j
++)
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
);
295 block
->entries
= (EMACS_TAG
**)NULL
;
296 block
->entries_index
= 0;
297 block
->entries_slots
= 0;
302 add_tag_to_block (EMACS_TAG_BLOCK
*block
,
303 char *name
, int line
, long int char_offset
)
307 tag
= (EMACS_TAG
*)xmalloc (sizeof (EMACS_TAG
));
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. */
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
;
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
);
346 decl_str
= DECLARATION_STRING
;
347 decl_len
= strlen (decl_str
);
349 block
= make_emacs_tag_block (filename
);
358 #if defined (INFOKEY) || defined (NAMED_FUNCTIONS)
360 #endif /* INFOKEY || NAMED_FUNCTIONS */
362 for (; offset
< (file_size
- decl_len
); offset
++)
364 if (buffer
[offset
] == '\n')
367 line_start
= offset
+ 1;
370 if (strncmp (buffer
+ offset
, decl_str
, decl_len
) == 0)
381 /* Skip forward until we find the open paren. */
382 while (point
< file_size
)
384 if (buffer
[point
] == '\n')
387 line_start
= point
+ 1;
389 else if (buffer
[point
] == '(')
395 while (point
++ < file_size
)
397 if (!whitespace_or_newline (buffer
[point
]))
399 else if (buffer
[point
] == '\n')
402 line_start
= point
+ 1;
406 if (point
>= file_size
)
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. */
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. */
433 if (strncmp (name_start
, "info_", 5) == 0)
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);
447 func_name
= temp_func_name
;
450 for (i
= 0; func_name
[i
]; i
++)
451 if (func_name
[i
] == '_')
454 #endif /* INFOKEY || NAMED_FUNCTIONS */
456 /* Find doc string. */
459 while (point
< file_size
)
461 if (buffer
[point
] == '\n')
464 line_start
= point
+ 1;
467 if (buffer
[point
] == '"')
475 while (offset
< file_size
)
477 if (buffer
[offset
] == '\n')
480 line_start
= offset
+ 1;
483 if (buffer
[offset
] == '\\')
485 else if (buffer
[offset
] == '"')
492 if (offset
>= file_size
)
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)
503 " { (VFunction *)%s, \"%s\", (FUNCTION_KEYSEQ *)0, %s },\n",
504 func
, func_name
, doc
);
505 #else /* !NAMED_FUNCTIONS */
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
);
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)
524 #endif /* INFOKEY || NAMED_FUNCTIONS */
526 #if defined (INFOKEY)
527 fprintf (funs_stream
, "#define A_%s %u\n", func
, next_func_key());
529 fprintf (funs_stream
,
530 "extern void %s (WINDOW *window, int count, unsigned char key);\n",
537 /* If we created any tags, remember this file on our global list. Otherwise,
538 free the memory already allocated to it. */
540 add_pointer_to_array (block
, emacs_tags_index
, emacs_tags
,
541 emacs_tags_slots
, 10, EMACS_TAG_BLOCK
*);
544 free (block
->filename
);
550 fatal_file_error (char *filename
)
552 fprintf (stderr
, _("Couldn't manipulate the file %s.\n"), filename
);
557 must_fopen (char *filename
, char *mode
)
561 stream
= fopen (filename
, mode
);
563 fatal_file_error (filename
);
568 static unsigned int func_key
;
571 init_func_key(unsigned int val
)