* decl.c (pop_cp_function_context): Don't call free on a NULL
[official-gcc.git] / texinfo / info / makedoc.c
blobd0dc7a42f4870ba8db72fbf35665e72c6b43b689
1 /* makedoc.c -- Make doc.c and funs.h from input files.
2 $Id: makedoc.c,v 1.1.1.2 1998/03/22 20:42:43 law Exp $
4 This file is part of GNU Info, a program for reading online documentation
5 stored in Info format.
7 Copyright (C) 1993, 97 Free Software 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 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"
32 static void fatal_file_error ();
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";
40 static char *doc_header[] = {
41 "/* doc.c -- Generated structure containing function names and doc strings.",
42 "",
43 " This file was automatically made from various source files with the",
44 " command \"%s\". DO NOT EDIT THIS FILE, only \"%s.c\".",
45 (char *)NULL
48 static char *doc_header_1[] = {
49 " An entry in the array FUNCTION_DOC_ARRAY is made for each command",
50 " found in the above files; each entry consists of a function pointer,",
51 #if defined (NAMED_FUNCTIONS)
52 " a string which is the user-visible name of the function,",
53 #endif /* NAMED_FUNCTIONS */
54 " and a string which documents its purpose. */",
55 "",
56 "#include \"doc.h\"",
57 "#include \"funs.h\"",
58 "",
59 "FUNCTION_DOC function_doc_array[] = {",
60 "",
61 (char *)NULL
64 /* How to remember the locations of the functions found so that Emacs
65 can use the information in a tag table. */
66 typedef struct {
67 char *name; /* Name of the tag. */
68 int line; /* Line number at which it appears. */
69 long char_offset; /* Character offset at which it appears. */
70 } EMACS_TAG;
72 typedef struct {
73 char *filename; /* Name of the file containing entries. */
74 long entrylen; /* Total number of characters in tag block. */
75 EMACS_TAG **entries; /* Entries found in FILENAME. */
76 int entries_index;
77 int entries_slots;
78 } EMACS_TAG_BLOCK;
80 EMACS_TAG_BLOCK **emacs_tags = (EMACS_TAG_BLOCK **)NULL;
81 int emacs_tags_index = 0;
82 int emacs_tags_slots = 0;
84 #define DECLARATION_STRING "\nDECLARE_INFO_COMMAND"
86 static void process_one_file ();
87 static void maybe_dump_tags ();
88 static FILE *must_fopen ();
90 int
91 main (argc, argv)
92 int argc;
93 char **argv;
95 register int i;
96 int tags_only = 0;
97 FILE *funs_stream, *doc_stream;
99 for (i = 1; i < argc; i++)
100 if (strcmp (argv[i], "-tags") == 0)
102 tags_only++;
103 break;
106 if (tags_only)
108 funs_filename = "/dev/null";
109 doc_filename = "/dev/null";
112 funs_stream = must_fopen (funs_filename, "w");
113 doc_stream = must_fopen (doc_filename, "w");
115 fprintf (funs_stream,
116 "/* %s -- Generated declarations for Info commands. */\n",
117 funs_filename);
119 for (i = 0; doc_header[i]; i++)
121 fprintf (doc_stream, doc_header[i], argv[0], argv[0]);
122 fprintf (doc_stream, "\n");
125 fprintf (doc_stream,
126 _(" Source files groveled to make this file include:\n\n"));
128 for (i = 1; i < argc; i++)
129 fprintf (doc_stream, "\t%s\n", argv[i]);
131 fprintf (doc_stream, "\n");
133 for (i = 0; doc_header_1[i]; i++)
134 fprintf (doc_stream, "%s\n", doc_header_1[i]);
137 for (i = 1; i < argc; i++)
139 char *curfile;
140 curfile = argv[i];
142 if (*curfile == '-')
143 continue;
145 fprintf (doc_stream, "/* Commands found in \"%s\". */\n", curfile);
146 fprintf (funs_stream, "\n/* Functions declared in \"%s\". */\n",
147 curfile);
149 process_one_file (curfile, doc_stream, funs_stream);
152 fprintf (doc_stream,
153 " { (VFunction *)NULL, (char *)NULL, (char *)NULL }\n};\n");
155 fclose (funs_stream);
156 fclose (doc_stream);
158 if (tags_only)
159 maybe_dump_tags (stdout);
160 exit (0);
163 /* Dumping out the contents of an Emacs tags table. */
164 static void
165 maybe_dump_tags (stream)
166 FILE *stream;
168 register int i;
170 /* Print out the information for each block. */
171 for (i = 0; i < emacs_tags_index; i++)
173 register int j;
174 register EMACS_TAG_BLOCK *block;
175 register EMACS_TAG *etag;
176 long block_len;
178 block_len = 0;
179 block = emacs_tags[i];
181 /* Calculate the length of the dumped block first. */
182 for (j = 0; j < block->entries_index; j++)
184 char digits[30];
185 etag = block->entries[j];
186 block_len += 3 + strlen (etag->name);
187 sprintf (digits, "%d,%ld", etag->line, etag->char_offset);
188 block_len += strlen (digits);
191 /* Print out the defining line. */
192 fprintf (stream, "\f\n%s,%ld\n", block->filename, block_len);
194 /* Print out the individual tags. */
195 for (j = 0; j < block->entries_index; j++)
197 etag = block->entries[j];
199 fprintf (stream, "%s,\177%d,%ld\n",
200 etag->name, etag->line, etag->char_offset);
205 /* Keeping track of names, line numbers and character offsets of functions
206 found in source files. */
207 static EMACS_TAG_BLOCK *
208 make_emacs_tag_block (filename)
209 char *filename;
211 EMACS_TAG_BLOCK *block;
213 block = (EMACS_TAG_BLOCK *)xmalloc (sizeof (EMACS_TAG_BLOCK));
214 block->filename = xstrdup (filename);
215 block->entrylen = 0;
216 block->entries = (EMACS_TAG **)NULL;
217 block->entries_index = 0;
218 block->entries_slots = 0;
219 return (block);
222 static void
223 add_tag_to_block (block, name, line, char_offset)
224 EMACS_TAG_BLOCK *block;
225 char *name;
226 int line;
227 long char_offset;
229 EMACS_TAG *tag;
231 tag = (EMACS_TAG *)xmalloc (sizeof (EMACS_TAG));
232 tag->name = name;
233 tag->line = line;
234 tag->char_offset = char_offset;
235 add_pointer_to_array (tag, block->entries_index, block->entries,
236 block->entries_slots, 50, EMACS_TAG *);
239 /* Read the file represented by FILENAME into core, and search it for Info
240 function declarations. Output the declarations in various forms to the
241 DOC_STREAM and FUNS_STREAM. */
242 static void
243 process_one_file (filename, doc_stream, funs_stream)
244 char *filename;
245 FILE *doc_stream, *funs_stream;
247 int descriptor, decl_len;
248 char *buffer, *decl_str;
249 struct stat finfo;
250 long offset;
251 long file_size;
252 EMACS_TAG_BLOCK *block;
254 if (stat (filename, &finfo) == -1)
255 fatal_file_error (filename);
257 descriptor = open (filename, O_RDONLY, 0666);
259 if (descriptor == -1)
260 fatal_file_error (filename);
262 file_size = (long) finfo.st_size;
263 buffer = (char *)xmalloc (1 + file_size);
264 read (descriptor, buffer, file_size);
265 close (descriptor);
267 offset = 0;
268 decl_str = DECLARATION_STRING;
269 decl_len = strlen (decl_str);
271 block = make_emacs_tag_block (filename);
273 while (1)
275 long point = 0;
276 long line_start = 0;
277 int line_number = 0;
279 char *func, *doc;
280 #if defined (NAMED_FUNCTIONS)
281 char *func_name;
282 #endif /* NAMED_FUNCTIONS */
284 for (; offset < (file_size - decl_len); offset++)
286 if (buffer[offset] == '\n')
288 line_number++;
289 line_start = offset + 1;
292 if (strncmp (buffer + offset, decl_str, decl_len) == 0)
294 offset += decl_len;
295 point = offset;
296 break;
300 if (!point)
301 break;
303 /* Skip forward until we find the open paren. */
304 while (point < file_size)
306 if (buffer[point] == '\n')
308 line_number++;
309 line_start = point + 1;
311 else if (buffer[point] == '(')
312 break;
314 point++;
317 while (point++ < file_size)
319 if (!whitespace_or_newline (buffer[point]))
320 break;
321 else if (buffer[point] == '\n')
323 line_number++;
324 line_start = point + 1;
328 if (point >= file_size)
329 break;
331 /* Now looking at name of function. Get it. */
332 for (offset = point; buffer[offset] != ','; offset++);
333 func = (char *)xmalloc (1 + (offset - point));
334 strncpy (func, buffer + point, offset - point);
335 func[offset - point] = '\0';
337 /* Remember this tag in the current block. */
339 char *tag_name;
341 tag_name = (char *)xmalloc (1 + (offset - line_start));
342 strncpy (tag_name, buffer + line_start, offset - line_start);
343 tag_name[offset - line_start] = '\0';
344 add_tag_to_block (block, tag_name, line_number, point);
347 #if defined (NAMED_FUNCTIONS)
348 /* Generate the user-visible function name from the function's name. */
350 register int i;
351 char *name_start;
353 name_start = func;
355 if (strncmp (name_start, "info_", 5) == 0)
356 name_start += 5;
358 func_name = xstrdup (name_start);
360 /* Fix up "ea" commands. */
361 if (strncmp (func_name, "ea_", 3) == 0)
363 char *temp_func_name;
365 temp_func_name = (char *)xmalloc (10 + strlen (func_name));
366 strcpy (temp_func_name, "echo_area_");
367 strcat (temp_func_name, func_name + 3);
368 free (func_name);
369 func_name = temp_func_name;
372 for (i = 0; func_name[i]; i++)
373 if (func_name[i] == '_')
374 func_name[i] = '-';
376 #endif /* NAMED_FUNCTIONS */
378 /* Find doc string. */
379 point = offset + 1;
381 while (point < file_size)
383 if (buffer[point] == '\n')
385 line_number++;
386 line_start = point + 1;
389 if (buffer[point] == '"')
390 break;
391 else
392 point++;
395 offset = point + 1;
397 while (offset < file_size)
399 if (buffer[offset] == '\n')
401 line_number++;
402 line_start = offset + 1;
405 if (buffer[offset] == '\\')
406 offset += 2;
407 else if (buffer[offset] == '"')
408 break;
409 else
410 offset++;
413 offset++;
414 if (offset >= file_size)
415 break;
417 doc = (char *)xmalloc (1 + (offset - point));
418 strncpy (doc, buffer + point, offset - point);
419 doc[offset - point] = '\0';
421 #if defined (NAMED_FUNCTIONS)
422 fprintf (doc_stream, " { %s, \"%s\", %s },\n", func, func_name, doc);
423 free (func_name);
424 #else /* !NAMED_FUNCTIONS */
425 fprintf (doc_stream, " { %s, %s },\n", func, doc);
426 #endif /* !NAMED_FUNCTIONS */
428 fprintf (funs_stream, "extern void %s ();\n", func);
429 free (func);
430 free (doc);
432 free (buffer);
434 /* If we created any tags, remember this file on our global list. Otherwise,
435 free the memory already allocated to it. */
436 if (block->entries)
437 add_pointer_to_array (block, emacs_tags_index, emacs_tags,
438 emacs_tags_slots, 10, EMACS_TAG_BLOCK *);
439 else
441 free (block->filename);
442 free (block);
446 static void
447 fatal_file_error (filename)
448 char *filename;
450 fprintf (stderr, _("Couldn't manipulate the file %s.\n"), filename);
451 exit (2);
454 static FILE *
455 must_fopen (filename, mode)
456 char *filename, *mode;
458 FILE *stream;
460 stream = fopen (filename, mode);
461 if (!stream)
462 fatal_file_error (filename);
464 return (stream);