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