(globals): Fix handling of namespace aliases.
[bpt/emacs.git] / lib-src / sorted-doc.c
1 /* Give this program DOCSTR.mm.nn as standard input and it outputs to
2 standard output a file of texinfo input containing the doc strings.
3
4 Copyright (C) 1989, 1992, 1994, 1996, 1999, 2000, 2001
5 Free Software Foundation Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs 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.
13
14 GNU Emacs 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.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 This version sorts the output by function name. */
24
25 #include "config.h"
26 #include <stdio.h>
27 #include <ctype.h>
28 #ifndef HAVE_STDLIB_H /* config.h includes stdlib. */
29 extern char *malloc ();
30 #endif
31
32 #define NUL '\0'
33 #define MARKER '\037'
34
35 #define DEBUG 0
36
37 typedef struct line LINE;
38
39 struct line
40 {
41 LINE *next; /* ptr to next or NULL */
42 char *line; /* text of the line */
43 };
44
45 typedef struct docstr DOCSTR;
46
47 struct docstr /* Allocated thing for an entry. */
48 {
49 DOCSTR *next; /* next in the chain */
50 char *name; /* name of the function or var */
51 LINE *first; /* first line of doc text. */
52 char type; /* 'F' for function, 'V' for variable */
53 };
54
55 \f
56 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
57
58 void
59 error (s1, s2)
60 char *s1, *s2;
61 {
62 fprintf (stderr, "sorted-doc: ");
63 fprintf (stderr, s1, s2);
64 fprintf (stderr, "\n");
65 }
66
67 /* Print error message and exit. */
68
69 void
70 fatal (s1, s2)
71 char *s1, *s2;
72 {
73 error (s1, s2);
74 exit (1);
75 }
76
77 /* Like malloc but get fatal error if memory is exhausted. */
78
79 char *
80 xmalloc (size)
81 int size;
82 {
83 char *result = malloc ((unsigned)size);
84 if (result == NULL)
85 fatal ("%s", "virtual memory exhausted");
86 return result;
87 }
88
89 char *
90 xstrdup (str)
91 char * str;
92 {
93 char *buf = xmalloc (strlen (str) + 1);
94 (void) strcpy (buf, str);
95 return (buf);
96 }
97
98 /* Comparison function for qsort to call. */
99
100 int
101 cmpdoc (a, b)
102 DOCSTR **a;
103 DOCSTR **b;
104 {
105 register int val = strcmp ((*a)->name, (*b)->name);
106 if (val) return val;
107 return (*a)->type - (*b)->type;
108 }
109
110
111 enum state
112 {
113 WAITING, BEG_NAME, NAME_GET, BEG_DESC, DESC_GET
114 };
115
116 char *states[] =
117 {
118 "WAITING", "BEG_NAME", "NAME_GET", "BEG_DESC", "DESC_GET"
119 };
120
121 int
122 main ()
123 {
124 register DOCSTR *dp = NULL; /* allocated DOCSTR */
125 register LINE *lp = NULL; /* allocated line */
126 register char *bp; /* ptr inside line buffer */
127 register enum state state = WAITING; /* state at start */
128 int cnt = 0; /* number of DOCSTRs read */
129
130 DOCSTR *docs; /* chain of allocated DOCSTRS */
131 char buf[512]; /* line buffer */
132
133 while (1) /* process one char at a time */
134 {
135 /* this char from the DOCSTR file */
136 register int ch = getchar ();
137
138 /* Beginnings */
139
140 if (state == WAITING)
141 {
142 if (ch == MARKER)
143 state = BEG_NAME;
144 }
145 else if (state == BEG_NAME)
146 {
147 cnt++;
148 if (dp == NULL) /* first dp allocated */
149 {
150 docs = dp = (DOCSTR*) xmalloc (sizeof (DOCSTR));
151 }
152 else /* all the rest */
153 {
154 dp->next = (DOCSTR*) xmalloc (sizeof (DOCSTR));
155 dp = dp->next;
156 }
157 lp = NULL;
158 dp->next = NULL;
159 bp = buf;
160 state = NAME_GET;
161 /* Record whether function or variable. */
162 dp->type = ch;
163 ch = getchar ();
164 }
165 else if (state == BEG_DESC)
166 {
167 if (lp == NULL) /* first line for dp */
168 {
169 dp->first = lp = (LINE*)xmalloc (sizeof (LINE));
170 }
171 else /* continuing lines */
172 {
173 lp->next = (LINE*)xmalloc (sizeof (LINE));
174 lp = lp->next;
175 }
176 lp->next = NULL;
177 bp = buf;
178 state = DESC_GET;
179 }
180
181 /* process gets */
182
183 if (state == NAME_GET || state == DESC_GET)
184 {
185 if (ch != MARKER && ch != '\n' && ch != EOF)
186 {
187 *bp++ = ch;
188 }
189 else /* saving and changing state */
190 {
191 *bp = NUL;
192 bp = xstrdup (buf);
193
194 if (state == NAME_GET)
195 dp->name = bp;
196 else
197 lp->line = bp;
198
199 bp = buf;
200 state = (ch == MARKER) ? BEG_NAME : BEG_DESC;
201 }
202 } /* NAME_GET || DESC_GET */
203 if (ch == EOF)
204 break;
205 }
206
207 {
208 DOCSTR **array;
209 register int i; /* counter */
210
211 /* build array of ptrs to DOCSTRs */
212
213 array = (DOCSTR**)xmalloc (cnt * sizeof (*array));
214 for (dp = docs, i = 0; dp != NULL ; dp = dp->next)
215 array[i++] = dp;
216
217 /* sort the array by name; within each name, by type */
218
219 qsort ((char*)array, cnt, sizeof (DOCSTR*), cmpdoc);
220
221 /* write the output header */
222
223 printf ("\\input texinfo @c -*-texinfo-*-\n");
224 printf ("@setfilename ../info/summary\n");
225 printf ("@settitle Command Summary for GNU Emacs\n");
226 printf ("@finalout\n");
227 printf ("@unnumbered Command Summary for GNU Emacs\n");
228 printf ("@table @asis\n");
229 printf ("\n");
230 printf ("@iftex\n");
231 printf ("@global@let@ITEM@item\n");
232 printf ("@def@item{@filbreak@vskip5pt@ITEM}\n");
233 printf ("@font@tensy cmsy10 scaled @magstephalf\n");
234 printf ("@font@teni cmmi10 scaled @magstephalf\n");
235 printf ("@def\\{{@tensy@char110}}\n"); /* this backslash goes with cmr10 */
236 printf ("@def|{{@tensy@char106}}\n");
237 printf ("@def@{{{@tensy@char102}}\n");
238 printf ("@def@}{{@tensy@char103}}\n");
239 printf ("@def<{{@teni@char62}}\n");
240 printf ("@def>{{@teni@char60}}\n");
241 printf ("@chardef@@64\n");
242 printf ("@catcode43=12\n");
243 printf ("@tableindent-0.2in\n");
244 printf ("@end iftex\n");
245
246 /* print each function from the array */
247
248 for (i = 0; i < cnt; i++)
249 {
250 printf ("\n@item %s @code{%s}\n@display\n",
251 array[i]->type == 'F' ? "Function" : "Variable",
252 array[i]->name);
253
254 for (lp = array[i]->first; lp != NULL ; lp = lp->next)
255 {
256 for (bp = lp->line; *bp; bp++)
257 {
258 /* the characters "@{}" need special treatment */
259 if (*bp == '@' || *bp == '{' || *bp == '}')
260 {
261 putchar('@');
262 }
263 putchar(*bp);
264 }
265 putchar ('\n');
266 }
267 printf("@end display\n");
268 /* Try to avoid a save size overflow in the TeX output
269 routine. */
270 if (i%100 == 0 && i > 0 && i != cnt)
271 printf("\n@end table\n@table @asis\n");
272 }
273
274 printf ("@end table\n");
275 printf ("@bye\n");
276 }
277
278 return 0;
279 }