Mercurial > hg > xemacs-beta
comparison 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 |
comparison
equal
deleted
inserted
replaced
5405:2aa9cd456ae7 | 5406:061f4f90f874 |
---|---|
1 /* Give this program DOCSTR.mm.nn as standard input | 1 /* Give this program DOC-mm.nn.oo as standard input and it outputs to |
2 and it outputs to standard output | 2 standard output a file of texinfo input containing the doc strings. |
3 a file of texinfo input containing the doc strings. | 3 |
4 | 4 Copyright (C) 1989, 1992, 1994, 1996, 1999, 2000, 2001, 2002, 2003, |
5 This version sorts the output by function name. | 5 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
6 */ | 6 |
7 | 7 This file is part of XEmacs. |
8 /* Synched up with: FSF 19.28. */ | 8 |
9 | 9 XEmacs 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 3 of the License, or | |
12 (at your option) any later version. | |
13 | |
14 XEmacs 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 XEmacs. If not, see <http://www.gnu.org/licenses/>. */ | |
21 | |
22 | |
23 /* This version sorts the output by function name. */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
10 #include <config.h> | 26 #include <config.h> |
27 #endif | |
11 | 28 |
12 #include <stdio.h> | 29 #include <stdio.h> |
30 #include <stdlib.h> | |
31 #include <string.h> | |
13 #include <ctype.h> | 32 #include <ctype.h> |
14 #include <stdlib.h> /* for qsort() and malloc() */ | 33 #ifdef DOS_NT |
15 #include <string.h> | 34 #include <fcntl.h> /* for O_BINARY */ |
16 static void *xmalloc (size_t); | 35 #include <io.h> /* for setmode */ |
36 #endif | |
17 | 37 |
18 #define NUL '\0' | 38 #define NUL '\0' |
19 #define MARKER '\037' | 39 #define MARKER '\037' |
20 | 40 |
21 #define DEBUG 0 | 41 #define DEBUG 0 |
22 | 42 |
23 typedef struct LINE LINE; | 43 typedef struct line LINE; |
24 | 44 |
25 struct LINE | 45 struct line |
26 { | 46 { |
27 LINE *next; /* ptr to next or NULL */ | 47 LINE *next; /* ptr to next or NULL */ |
28 char *line; /* text of the line */ | 48 char *line; /* text of the line */ |
29 }; | 49 }; |
30 | 50 |
36 char *name; /* name of the function or var */ | 56 char *name; /* name of the function or var */ |
37 LINE *first; /* first line of doc text. */ | 57 LINE *first; /* first line of doc text. */ |
38 char type; /* 'F' for function, 'V' for variable */ | 58 char type; /* 'F' for function, 'V' for variable */ |
39 }; | 59 }; |
40 | 60 |
61 void error (char *s1, char *s2); | |
62 void fatal (char *s1, char *s2); | |
63 char *xmalloc (int size); | |
64 char *xstrdup (char *str); | |
65 int cmpdoc (DOCSTR **a, DOCSTR **b); | |
66 | |
67 typedef int (*sort_function_t)(const void *, const void *); | |
68 | |
41 | 69 |
42 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ | 70 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ |
43 | 71 |
44 static void | 72 void |
45 error (char *s1, char *s2) | 73 error (char *s1, char *s2) |
46 { | 74 { |
47 fprintf (stderr, "sorted-doc: "); | 75 fprintf (stderr, "sorted-doc: "); |
48 fprintf (stderr, s1, s2); | 76 fprintf (stderr, s1, s2); |
49 fprintf (stderr, "\n"); | 77 fprintf (stderr, "\n"); |
50 } | 78 } |
51 | 79 |
52 /* Print error message and exit. */ | 80 /* Print error message and exit. */ |
53 | 81 |
54 static void | 82 void |
55 fatal (char *s1, char *s2) | 83 fatal (char *s1, char *s2) |
56 { | 84 { |
57 error (s1, s2); | 85 error (s1, s2); |
58 exit (1); | 86 exit (EXIT_FAILURE); |
59 } | 87 } |
60 | 88 |
61 /* Like malloc but get fatal error if memory is exhausted. */ | 89 /* Like malloc but get fatal error if memory is exhausted. */ |
62 | 90 |
63 static void * | 91 char * |
64 xmalloc (size_t size) | 92 xmalloc (int size) |
65 { | 93 { |
66 void *result = malloc (size); | 94 char *result = malloc ((unsigned)size); |
67 if (result == NULL) | 95 if (result == NULL) |
68 fatal ("%s", "virtual memory exhausted"); | 96 fatal ("%s", "virtual memory exhausted"); |
69 return result; | 97 return result; |
70 } | 98 } |
71 | 99 |
72 static char * | 100 char * |
73 strsav (char *str) | 101 xstrdup (char *str) |
74 { | 102 { |
75 char *buf = (char *) xmalloc (strlen (str) + 1); | 103 char *buf = xmalloc (strlen (str) + 1); |
76 strcpy (buf, str); | 104 (void) strcpy (buf, str); |
77 return buf; | 105 return (buf); |
78 } | 106 } |
79 | 107 |
80 /* Comparison function for qsort to call. */ | 108 /* Comparison function for qsort to call. */ |
81 | 109 |
82 static int | 110 int |
83 cmpdoc (DOCSTR **a, DOCSTR **b) | 111 cmpdoc (DOCSTR **a, DOCSTR **b) |
84 { | 112 { |
85 register int val = strcmp ((*a)->name, (*b)->name); | 113 register int val = strcmp ((*a)->name, (*b)->name); |
86 if (val) return val; | 114 if (val) return val; |
87 return (*a)->type - (*b)->type; | 115 return (*a)->type - (*b)->type; |
91 enum state | 119 enum state |
92 { | 120 { |
93 WAITING, BEG_NAME, NAME_GET, BEG_DESC, DESC_GET | 121 WAITING, BEG_NAME, NAME_GET, BEG_DESC, DESC_GET |
94 }; | 122 }; |
95 | 123 |
96 const char *states[] = | 124 char *states[] = |
97 { | 125 { |
98 "WAITING", "BEG_NAME", "NAME_GET", "BEG_DESC", "DESC_GET" | 126 "WAITING", "BEG_NAME", "NAME_GET", "BEG_DESC", "DESC_GET" |
99 }; | 127 }; |
100 | 128 |
101 int | 129 int |
102 main (int argc, char **argv) | 130 main (void) |
103 { | 131 { |
104 register DOCSTR *dp = NULL; /* allocated DOCSTR */ | 132 register DOCSTR *dp = NULL; /* allocated DOCSTR */ |
105 register LINE *lp = NULL; /* allocated line */ | 133 register LINE *lp = NULL; /* allocated line */ |
106 register char *bp = 0; /* ptr inside line buffer */ | 134 register char *bp; /* ptr inside line buffer */ |
107 /* int notfirst = 0; / * set after read something */ | |
108 register enum state state = WAITING; /* state at start */ | 135 register enum state state = WAITING; /* state at start */ |
109 int cnt = 0; /* number of DOCSTRs read */ | 136 int cnt = 0; /* number of DOCSTRs read */ |
110 | 137 |
111 DOCSTR *docs = 0; /* chain of allocated DOCSTRS */ | 138 DOCSTR *docs = NULL; /* chain of allocated DOCSTRS */ |
112 char buf[512]; /* line buffer */ | 139 char buf[512]; /* line buffer */ |
113 | 140 |
141 #ifdef DOS_NT | |
142 /* DOC is a binary file. */ | |
143 if (!isatty (fileno (stdin))) | |
144 setmode (fileno (stdin), O_BINARY); | |
145 #endif | |
146 | |
147 bp = buf; | |
148 | |
114 while (1) /* process one char at a time */ | 149 while (1) /* process one char at a time */ |
115 { | 150 { |
116 /* this char from the DOCSTR file */ | 151 /* this char from the DOCSTR file */ |
117 register int ch = getchar (); | 152 register int ch = getchar (); |
118 | 153 |
156 } | 191 } |
157 lp->next = NULL; | 192 lp->next = NULL; |
158 bp = buf; | 193 bp = buf; |
159 state = DESC_GET; | 194 state = DESC_GET; |
160 } | 195 } |
161 | 196 |
162 /* process gets */ | 197 /* process gets */ |
163 | 198 |
164 if (state == NAME_GET || state == DESC_GET) | 199 if (state == NAME_GET || state == DESC_GET) |
165 { | 200 { |
166 if (ch != MARKER && ch != '\n' && ch != EOF) | 201 if (ch != MARKER && ch != '\n' && ch != EOF) |
168 *bp++ = ch; | 203 *bp++ = ch; |
169 } | 204 } |
170 else /* saving and changing state */ | 205 else /* saving and changing state */ |
171 { | 206 { |
172 *bp = NUL; | 207 *bp = NUL; |
173 bp = strsav (buf); | 208 bp = xstrdup (buf); |
174 | 209 |
175 if (state == NAME_GET) | 210 if (state == NAME_GET) |
176 dp->name = bp; | 211 dp->name = bp; |
177 else | 212 else |
178 lp->line = bp; | 213 lp->line = bp; |
195 for (dp = docs, i = 0; dp != NULL ; dp = dp->next) | 230 for (dp = docs, i = 0; dp != NULL ; dp = dp->next) |
196 array[i++] = dp; | 231 array[i++] = dp; |
197 | 232 |
198 /* sort the array by name; within each name, by type */ | 233 /* sort the array by name; within each name, by type */ |
199 | 234 |
200 qsort ((char*)array, cnt, sizeof (DOCSTR*), | 235 qsort ((char*)array, cnt, sizeof (DOCSTR*), (sort_function_t)cmpdoc); |
201 (int (*)(const void *, const void *)) cmpdoc); | |
202 | 236 |
203 /* write the output header */ | 237 /* write the output header */ |
204 | 238 |
205 printf ("\\input texinfo @c -*-texinfo-*-\n"); | 239 printf ("\\input texinfo @c -*-texinfo-*-\n"); |
206 printf ("@setfilename ../info/summary\n"); | 240 printf ("@setfilename ../info/summary\n"); |
207 printf ("@settitle Command Summary for XEmacs\n"); | 241 printf ("@settitle Command Summary for XEmacs\n"); |
242 printf ("@finalout\n"); | |
208 printf ("@unnumbered Command Summary for XEmacs\n"); | 243 printf ("@unnumbered Command Summary for XEmacs\n"); |
209 printf ("@table @asis\n"); | 244 printf ("@table @asis\n"); |
210 printf ("\n"); | 245 printf ("\n"); |
211 printf ("@iftex\n"); | 246 printf ("@iftex\n"); |
212 printf ("@global@let@ITEM=@item\n"); | 247 printf ("@global@let@ITEM@item\n"); |
213 printf ("@def@item{@filbreak@vskip5pt@ITEM}\n"); | 248 printf ("@def@item{@filbreak@vskip5pt@ITEM}\n"); |
214 printf ("@font@tensy cmsy10 scaled @magstephalf\n"); | 249 printf ("@font@tensy cmsy10 scaled @magstephalf\n"); |
215 printf ("@font@teni cmmi10 scaled @magstephalf\n"); | 250 printf ("@font@teni cmmi10 scaled @magstephalf\n"); |
216 printf ("@def\\{{@tensy@char110}}\n"); /* this backslash goes with cmr10 */ | 251 printf ("@def\\{{@tensy@char110}}\n"); /* this backslash goes with cmr10 */ |
217 printf ("@def|{{@tensy@char106}}\n"); | 252 printf ("@def|{{@tensy@char106}}\n"); |
244 putchar(*bp); | 279 putchar(*bp); |
245 } | 280 } |
246 putchar ('\n'); | 281 putchar ('\n'); |
247 } | 282 } |
248 printf("@end display\n"); | 283 printf("@end display\n"); |
249 if ( i%200 == 0 && i != 0 ) printf("@end table\n\n@table @asis\n"); | 284 /* Try to avoid a save size overflow in the TeX output |
285 routine. */ | |
286 if (i%100 == 0 && i > 0 && i != cnt) | |
287 printf("\n@end table\n@table @asis\n"); | |
250 } | 288 } |
251 | 289 |
252 printf ("@end table\n"); | 290 printf ("@end table\n"); |
253 printf ("@bye\n"); | 291 printf ("@bye\n"); |
254 } | 292 } |
255 | 293 |
256 return 0; | 294 return EXIT_SUCCESS; |
257 } | 295 } |
296 | |
297 /* sorted-doc.c ends here */ |