comparison lib-src/sorted-doc.c @ 0:376386a54a3c r19-14

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