comparison lib-src/sorted-doc.c @ 428:3ecd8885ac67 r21-2-22

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