* lisp/help.el (finder-by-keyword): Remove unnecessary autoload.
[bpt/emacs.git] / lib-src / sorted-doc.c
CommitLineData
a43dbef8 1/* Give this program DOC-mm.nn.oo as standard input and it outputs to
b3ae7a0a 2 standard output a file of texinfo input containing the doc strings.
80b2cbf2 3
294981c7 4Copyright (C) 1989, 1992, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
114f9c96 5 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
b3ae7a0a 6
294981c7 7This file is part of GNU Emacs.
b3ae7a0a 8
294981c7
GM
9GNU Emacs is free software: you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation, either version 3 of the License, or
12(at your option) any later version.
b3ae7a0a 13
294981c7
GM
14GNU Emacs is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
b3ae7a0a 18
294981c7
GM
19You should have received a copy of the GNU General Public License
20along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
cdbf5964 21
294981c7
GM
22
23/* This version sorts the output by function name. */
b54b38e6 24
2f8fe2f4
PJ
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
b54b38e6
JB
29#include <stdio.h>
30#include <ctype.h>
e75dcee3
EZ
31#ifdef DOS_NT
32#include <fcntl.h> /* for O_BINARY */
33#include <io.h> /* for setmode */
34#endif
13688fb9 35#ifndef HAVE_STDLIB_H /* config.h includes stdlib. */
e75dcee3 36#ifndef WINDOWSNT /* src/s/ms-w32.h includes stdlib.h */
b54b38e6 37extern char *malloc ();
13688fb9 38#endif
e75dcee3 39#endif
b54b38e6
JB
40
41#define NUL '\0'
42#define MARKER '\037'
43
44#define DEBUG 0
45
46typedef struct line LINE;
47
48struct line
49{
50 LINE *next; /* ptr to next or NULL */
51 char *line; /* text of the line */
52};
53
54typedef struct docstr DOCSTR;
55
56struct docstr /* Allocated thing for an entry. */
57{
58 DOCSTR *next; /* next in the chain */
59 char *name; /* name of the function or var */
60 LINE *first; /* first line of doc text. */
61 char type; /* 'F' for function, 'V' for variable */
62};
63
64\f
b54b38e6
JB
65/* Print error message. `s1' is printf control string, `s2' is arg for it. */
66
340ff9de 67void
988e88ab 68error (const char *s1, const char *s2)
b54b38e6
JB
69{
70 fprintf (stderr, "sorted-doc: ");
71 fprintf (stderr, s1, s2);
72 fprintf (stderr, "\n");
73}
74
340ff9de
DM
75/* Print error message and exit. */
76
77void
988e88ab 78fatal (const char *s1, const char *s2)
340ff9de
DM
79{
80 error (s1, s2);
65396510 81 exit (EXIT_FAILURE);
340ff9de
DM
82}
83
b54b38e6
JB
84/* Like malloc but get fatal error if memory is exhausted. */
85
86char *
873fbd0b 87xmalloc (int size)
b54b38e6
JB
88{
89 char *result = malloc ((unsigned)size);
90 if (result == NULL)
91 fatal ("%s", "virtual memory exhausted");
92 return result;
93}
94
95char *
728a982d 96xstrdup (const char *str)
b54b38e6
JB
97{
98 char *buf = xmalloc (strlen (str) + 1);
99 (void) strcpy (buf, str);
100 return (buf);
101}
102
103/* Comparison function for qsort to call. */
104
105int
a52a00a3 106cmpdoc (const void *va, const void *vb)
b54b38e6 107{
a52a00a3
AS
108 DOCSTR *const *a = va;
109 DOCSTR *const *b = vb;
b54b38e6
JB
110 register int val = strcmp ((*a)->name, (*b)->name);
111 if (val) return val;
112 return (*a)->type - (*b)->type;
113}
114
b54b38e6
JB
115enum state
116{
117 WAITING, BEG_NAME, NAME_GET, BEG_DESC, DESC_GET
118};
119
988e88ab 120const char *states[] =
b54b38e6
JB
121{
122 "WAITING", "BEG_NAME", "NAME_GET", "BEG_DESC", "DESC_GET"
123};
80b2cbf2 124
340ff9de 125int
873fbd0b 126main (void)
b54b38e6
JB
127{
128 register DOCSTR *dp = NULL; /* allocated DOCSTR */
129 register LINE *lp = NULL; /* allocated line */
130 register char *bp; /* ptr inside line buffer */
b54b38e6
JB
131 register enum state state = WAITING; /* state at start */
132 int cnt = 0; /* number of DOCSTRs read */
133
7fe8b491 134 DOCSTR *docs = NULL; /* chain of allocated DOCSTRS */
b54b38e6 135 char buf[512]; /* line buffer */
80b2cbf2 136
e75dcee3
EZ
137#ifdef DOS_NT
138 /* DOC is a binary file. */
139 if (!isatty (fileno (stdin)))
140 setmode (fileno (stdin), O_BINARY);
141#endif
142
37c49f02
EZ
143 bp = buf;
144
b54b38e6
JB
145 while (1) /* process one char at a time */
146 {
147 /* this char from the DOCSTR file */
148 register int ch = getchar ();
149
150 /* Beginnings */
151
152 if (state == WAITING)
153 {
154 if (ch == MARKER)
155 state = BEG_NAME;
156 }
157 else if (state == BEG_NAME)
158 {
159 cnt++;
160 if (dp == NULL) /* first dp allocated */
161 {
162 docs = dp = (DOCSTR*) xmalloc (sizeof (DOCSTR));
163 }
164 else /* all the rest */
165 {
166 dp->next = (DOCSTR*) xmalloc (sizeof (DOCSTR));
167 dp = dp->next;
168 }
169 lp = NULL;
170 dp->next = NULL;
171 bp = buf;
172 state = NAME_GET;
173 /* Record whether function or variable. */
174 dp->type = ch;
175 ch = getchar ();
176 }
177 else if (state == BEG_DESC)
178 {
179 if (lp == NULL) /* first line for dp */
180 {
181 dp->first = lp = (LINE*)xmalloc (sizeof (LINE));
182 }
183 else /* continuing lines */
184 {
185 lp->next = (LINE*)xmalloc (sizeof (LINE));
186 lp = lp->next;
187 }
188 lp->next = NULL;
189 bp = buf;
190 state = DESC_GET;
191 }
80b2cbf2 192
b54b38e6
JB
193 /* process gets */
194
195 if (state == NAME_GET || state == DESC_GET)
196 {
197 if (ch != MARKER && ch != '\n' && ch != EOF)
198 {
199 *bp++ = ch;
200 }
201 else /* saving and changing state */
202 {
203 *bp = NUL;
340ff9de 204 bp = xstrdup (buf);
b54b38e6
JB
205
206 if (state == NAME_GET)
207 dp->name = bp;
208 else
209 lp->line = bp;
210
211 bp = buf;
212 state = (ch == MARKER) ? BEG_NAME : BEG_DESC;
213 }
214 } /* NAME_GET || DESC_GET */
215 if (ch == EOF)
216 break;
217 }
218
219 {
220 DOCSTR **array;
221 register int i; /* counter */
222
223 /* build array of ptrs to DOCSTRs */
224
225 array = (DOCSTR**)xmalloc (cnt * sizeof (*array));
226 for (dp = docs, i = 0; dp != NULL ; dp = dp->next)
227 array[i++] = dp;
228
229 /* sort the array by name; within each name, by type */
230
a52a00a3 231 qsort ((char*)array, cnt, sizeof (DOCSTR*), cmpdoc);
b54b38e6
JB
232
233 /* write the output header */
234
235 printf ("\\input texinfo @c -*-texinfo-*-\n");
236 printf ("@setfilename ../info/summary\n");
237 printf ("@settitle Command Summary for GNU Emacs\n");
df0ffd25 238 printf ("@finalout\n");
b54b38e6
JB
239 printf ("@unnumbered Command Summary for GNU Emacs\n");
240 printf ("@table @asis\n");
4dcb1d26 241 printf ("\n");
df0ffd25
DL
242 printf ("@iftex\n");
243 printf ("@global@let@ITEM@item\n");
4dcb1d26
RS
244 printf ("@def@item{@filbreak@vskip5pt@ITEM}\n");
245 printf ("@font@tensy cmsy10 scaled @magstephalf\n");
246 printf ("@font@teni cmmi10 scaled @magstephalf\n");
247 printf ("@def\\{{@tensy@char110}}\n"); /* this backslash goes with cmr10 */
248 printf ("@def|{{@tensy@char106}}\n");
249 printf ("@def@{{{@tensy@char102}}\n");
250 printf ("@def@}{{@tensy@char103}}\n");
251 printf ("@def<{{@teni@char62}}\n");
252 printf ("@def>{{@teni@char60}}\n");
253 printf ("@chardef@@64\n");
254 printf ("@catcode43=12\n");
255 printf ("@tableindent-0.2in\n");
df0ffd25 256 printf ("@end iftex\n");
b54b38e6
JB
257
258 /* print each function from the array */
259
260 for (i = 0; i < cnt; i++)
261 {
262 printf ("\n@item %s @code{%s}\n@display\n",
263 array[i]->type == 'F' ? "Function" : "Variable",
264 array[i]->name);
265
266 for (lp = array[i]->first; lp != NULL ; lp = lp->next)
267 {
268 for (bp = lp->line; *bp; bp++)
269 {
270 /* the characters "@{}" need special treatment */
271 if (*bp == '@' || *bp == '{' || *bp == '}')
272 {
273 putchar('@');
274 }
275 putchar(*bp);
276 }
277 putchar ('\n');
278 }
279 printf("@end display\n");
df0ffd25
DL
280 /* Try to avoid a save size overflow in the TeX output
281 routine. */
282 if (i%100 == 0 && i > 0 && i != cnt)
283 printf("\n@end table\n@table @asis\n");
b54b38e6
JB
284 }
285
286 printf ("@end table\n");
287 printf ("@bye\n");
288 }
289
65396510 290 return EXIT_SUCCESS;
b54b38e6 291}
ab5796a9
MB
292
293/* arch-tag: ce28f204-1e70-4b34-8210-3d54a5662071
294 (do not change this comment) */
65396510
TTN
295
296/* sorted-doc.c ends here */