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