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);
26 typedef struct LINE LINE;
30 LINE *next; /* ptr to next or NULL */
31 char *line; /* text of the line */
34 typedef struct docstr DOCSTR;
36 struct docstr /* Allocated thing for an entry. */
38 DOCSTR *next; /* next in the chain */
39 char *name; /* name of the function or var */
40 LINE *first; /* first line of doc text. */
41 char type; /* 'F' for function, 'V' for variable */
45 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
48 error (char *s1, char *s2)
50 fprintf (stderr, "sorted-doc: ");
51 fprintf (stderr, s1, s2);
52 fprintf (stderr, "\n");
55 /* Print error message and exit. */
58 fatal (char *s1, char *s2)
64 /* Like malloc but get fatal error if memory is exhausted. */
69 void *result = malloc (size);
71 fatal ("%s", "virtual memory exhausted");
78 char *buf = (char *) xmalloc (strlen (str) + 1);
83 /* Comparison function for qsort to call. */
86 cmpdoc (DOCSTR **a, DOCSTR **b)
88 register int val = strcmp ((*a)->name, (*b)->name);
90 return (*a)->type - (*b)->type;
96 WAITING, BEG_NAME, NAME_GET, BEG_DESC, DESC_GET
99 CONST char *states[] =
101 "WAITING", "BEG_NAME", "NAME_GET", "BEG_DESC", "DESC_GET"
105 main (int argc, char *argv[])
107 register DOCSTR *dp = NULL; /* allocated DOCSTR */
108 register LINE *lp = NULL; /* allocated line */
109 register char *bp = 0; /* ptr inside line buffer */
110 /* int notfirst = 0; / * set after read something */
111 register enum state state = WAITING; /* state at start */
112 int cnt = 0; /* number of DOCSTRs read */
114 DOCSTR *docs = 0; /* chain of allocated DOCSTRS */
115 char buf[512]; /* line buffer */
117 while (1) /* process one char at a time */
119 /* this char from the DOCSTR file */
120 register int ch = getchar ();
124 if (state == WAITING)
129 else if (state == BEG_NAME)
132 if (dp == NULL) /* first dp allocated */
134 docs = dp = (DOCSTR*) xmalloc (sizeof (DOCSTR));
136 else /* all the rest */
138 dp->next = (DOCSTR*) xmalloc (sizeof (DOCSTR));
145 /* Record whether function or variable. */
149 else if (state == BEG_DESC)
151 if (lp == NULL) /* first line for dp */
153 dp->first = lp = (LINE*)xmalloc (sizeof (LINE));
155 else /* continuing lines */
157 lp->next = (LINE*)xmalloc (sizeof (LINE));
167 if (state == NAME_GET || state == DESC_GET)
169 if (ch != MARKER && ch != '\n' && ch != EOF)
173 else /* saving and changing state */
178 if (state == NAME_GET)
184 state = (ch == MARKER) ? BEG_NAME : BEG_DESC;
186 } /* NAME_GET || DESC_GET */
193 register int i; /* counter */
195 /* build array of ptrs to DOCSTRs */
197 array = (DOCSTR**)xmalloc (cnt * sizeof (*array));
198 for (dp = docs, i = 0; dp != NULL ; dp = dp->next)
201 /* sort the array by name; within each name, by type */
203 qsort ((char*)array, cnt, sizeof (DOCSTR*),
204 /* was cast to (int (*)(CONST void *, CONST void *))
205 but that loses on HP because CONST_IS_LOSING. */
206 /* This one loses too: (int (*)()) */
207 /* Ok, so let's try const instead of CONST. Fuck me!!! */
208 (int (*)(const void *, const void *))
211 /* write the output header */
213 printf ("\\input texinfo @c -*-texinfo-*-\n");
214 printf ("@setfilename ../info/summary\n");
215 printf ("@settitle Command Summary for GNU Emacs\n");
216 printf ("@unnumbered Command Summary for GNU Emacs\n");
217 printf ("@table @asis\n");
220 printf ("@global@let@ITEM=@item\n");
221 printf ("@def@item{@filbreak@vskip5pt@ITEM}\n");
222 printf ("@font@tensy cmsy10 scaled @magstephalf\n");
223 printf ("@font@teni cmmi10 scaled @magstephalf\n");
224 printf ("@def\\{{@tensy@char110}}\n"); /* this backslash goes with cmr10 */
225 printf ("@def|{{@tensy@char106}}\n");
226 printf ("@def@{{{@tensy@char102}}\n");
227 printf ("@def@}{{@tensy@char103}}\n");
228 printf ("@def<{{@teni@char62}}\n");
229 printf ("@def>{{@teni@char60}}\n");
230 printf ("@chardef@@64\n");
231 printf ("@catcode43=12\n");
232 printf ("@tableindent-0.2in\n");
233 printf ("@end iftex\n");
235 /* print each function from the array */
237 for (i = 0; i < cnt; i++)
239 printf ("\n@item %s @code{%s}\n@display\n",
240 array[i]->type == 'F' ? "Function" : "Variable",
243 for (lp = array[i]->first; lp != NULL ; lp = lp->next)
245 for (bp = lp->line; *bp; bp++)
247 /* the characters "@{}" need special treatment */
248 if (*bp == '@' || *bp == '{' || *bp == '}')
256 printf("@end display\n");
257 if ( i%200 == 0 && i != 0 ) printf("@end table\n\n@table @asis\n");
260 printf ("@end table\n");