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.
5 This version sorts the output by function name.
8 /* Synched up with: FSF 19.28. */
14 #include <stdlib.h> /* for qsort() and malloc() */
16 static void *xmalloc(size_t);
23 typedef struct LINE LINE;
26 LINE *next; /* ptr to next or NULL */
27 char *line; /* text of the line */
30 typedef struct docstr DOCSTR;
32 struct docstr { /* Allocated thing for an entry. */
33 DOCSTR *next; /* next in the chain */
34 char *name; /* name of the function or var */
35 LINE *first; /* first line of doc text. */
36 char type; /* 'F' for function, 'V' for variable */
39 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
41 static void error(char *s1, char *s2)
43 fprintf(stderr, "sorted-doc: ");
44 fprintf(stderr, s1, s2);
45 fprintf(stderr, "\n");
48 /* Print error message and exit. */
50 static void fatal(char *s1, char *s2)
56 /* Like malloc but get fatal error if memory is exhausted. */
58 static void *xmalloc(size_t size)
60 void *result = malloc(size);
62 fatal("%s", "virtual memory exhausted");
66 static char *strsav(char *str)
68 char *buf = (char *)xmalloc(strlen(str) + 1);
73 /* Comparison function for qsort to call. */
75 static int cmpdoc(DOCSTR ** a, DOCSTR ** b)
77 register int val = strcmp((*a)->name, (*b)->name);
80 return (*a)->type - (*b)->type;
84 WAITING, BEG_NAME, NAME_GET, BEG_DESC, DESC_GET
87 const char *states[] = {
88 "WAITING", "BEG_NAME", "NAME_GET", "BEG_DESC", "DESC_GET"
91 int main(int argc, char *argv[])
93 register DOCSTR *dp = NULL; /* allocated DOCSTR */
94 register LINE *lp = NULL; /* allocated line */
95 register char *bp = 0; /* ptr inside line buffer */
96 /* int notfirst = 0; / * set after read something */
97 register enum state state = WAITING; /* state at start */
98 int cnt = 0; /* number of DOCSTRs read */
100 DOCSTR *docs = 0; /* chain of allocated DOCSTRS */
101 char buf[512]; /* line buffer */
103 while (1) { /* process one char at a time */
104 /* this char from the DOCSTR file */
105 register int ch = getchar();
109 if (state == WAITING) {
112 } else if (state == BEG_NAME) {
114 if (dp == NULL) { /* first dp allocated */
115 docs = dp = (DOCSTR *) xmalloc(sizeof(DOCSTR));
116 } else { /* all the rest */
118 dp->next = (DOCSTR *) xmalloc(sizeof(DOCSTR));
125 /* Record whether function or variable. */
128 } else if (state == BEG_DESC) {
129 if (lp == NULL) { /* first line for dp */
130 dp->first = lp = (LINE *) xmalloc(sizeof(LINE));
131 } else { /* continuing lines */
133 lp->next = (LINE *) xmalloc(sizeof(LINE));
143 if (state == NAME_GET || state == DESC_GET) {
144 if (ch != MARKER && ch != '\n' && ch != EOF) {
146 } else { /* saving and changing state */
151 if (state == NAME_GET)
157 state = (ch == MARKER) ? BEG_NAME : BEG_DESC;
159 } /* NAME_GET || DESC_GET */
166 register int i; /* counter */
168 /* build array of ptrs to DOCSTRs */
170 array = (DOCSTR **) xmalloc(cnt * sizeof(*array));
171 for (dp = docs, i = 0; dp != NULL; dp = dp->next)
174 /* sort the array by name; within each name, by type */
176 qsort((char *)array, cnt, sizeof(DOCSTR *),
177 (int (*)(const void *, const void *))cmpdoc);
179 /* write the output header */
181 printf("\\input texinfo @c -*-texinfo-*-\n");
182 printf("@setfilename ../info/summary\n");
183 printf("@settitle Command Summary for GNU Emacs\n");
184 printf("@unnumbered Command Summary for GNU Emacs\n");
185 printf("@table @asis\n");
188 printf("@global@let@ITEM=@item\n");
189 printf("@def@item{@filbreak@vskip5pt@ITEM}\n");
190 printf("@font@tensy cmsy10 scaled @magstephalf\n");
191 printf("@font@teni cmmi10 scaled @magstephalf\n");
192 printf("@def\\{{@tensy@char110}}\n"); /* this backslash goes with cmr10 */
193 printf("@def|{{@tensy@char106}}\n");
194 printf("@def@{{{@tensy@char102}}\n");
195 printf("@def@}{{@tensy@char103}}\n");
196 printf("@def<{{@teni@char62}}\n");
197 printf("@def>{{@teni@char60}}\n");
198 printf("@chardef@@64\n");
199 printf("@catcode43=12\n");
200 printf("@tableindent-0.2in\n");
201 printf("@end iftex\n");
203 /* print each function from the array */
205 for (i = 0; i < cnt; i++) {
206 printf("\n@item %s @code{%s}\n@display\n",
207 array[i]->type == 'F' ? "Function" : "Variable",
210 for (lp = array[i]->first; lp != NULL; lp = lp->next) {
211 for (bp = lp->line; *bp; bp++) {
212 /* the characters "@{}" need special treatment */
213 if (*bp == '@' || *bp == '{'
221 printf("@end display\n");
222 if (i % 200 == 0 && i != 0)
223 printf("@end table\n\n@table @asis\n");
226 printf("@end table\n");