(lisp-mode-shared-map): Undo the change from 2001-01-12. It is not needed,
[emacs.git] / lib-src / sorted-doc.c
blob280460dfdaea89419f8fd773de96b69e175e639f
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.
6 */
8 #include "config.h"
9 #include <stdio.h>
10 #include <ctype.h>
11 #ifndef HAVE_STDLIB_H /* config.h includes stdlib. */
12 extern char *malloc ();
13 #endif
15 #define NUL '\0'
16 #define MARKER '\037'
18 #define DEBUG 0
20 typedef struct line LINE;
22 struct line
24 LINE *next; /* ptr to next or NULL */
25 char *line; /* text of the line */
28 typedef struct docstr DOCSTR;
30 struct docstr /* Allocated thing for an entry. */
32 DOCSTR *next; /* next in the chain */
33 char *name; /* name of the function or var */
34 LINE *first; /* first line of doc text. */
35 char type; /* 'F' for function, 'V' for variable */
39 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
41 void
42 error (s1, s2)
43 char *s1, *s2;
45 fprintf (stderr, "sorted-doc: ");
46 fprintf (stderr, s1, s2);
47 fprintf (stderr, "\n");
50 /* Print error message and exit. */
52 void
53 fatal (s1, s2)
54 char *s1, *s2;
56 error (s1, s2);
57 exit (1);
60 /* Like malloc but get fatal error if memory is exhausted. */
62 char *
63 xmalloc (size)
64 int size;
66 char *result = malloc ((unsigned)size);
67 if (result == NULL)
68 fatal ("%s", "virtual memory exhausted");
69 return result;
72 char *
73 xstrdup (str)
74 char * str;
76 char *buf = xmalloc (strlen (str) + 1);
77 (void) strcpy (buf, str);
78 return (buf);
81 /* Comparison function for qsort to call. */
83 int
84 cmpdoc (a, b)
85 DOCSTR **a;
86 DOCSTR **b;
88 register int val = strcmp ((*a)->name, (*b)->name);
89 if (val) return val;
90 return (*a)->type - (*b)->type;
94 enum state
96 WAITING, BEG_NAME, NAME_GET, BEG_DESC, DESC_GET
99 char *states[] =
101 "WAITING", "BEG_NAME", "NAME_GET", "BEG_DESC", "DESC_GET"
105 main ()
107 register DOCSTR *dp = NULL; /* allocated DOCSTR */
108 register LINE *lp = NULL; /* allocated line */
109 register char *bp; /* ptr inside line buffer */
110 register enum state state = WAITING; /* state at start */
111 int cnt = 0; /* number of DOCSTRs read */
113 DOCSTR *docs; /* chain of allocated DOCSTRS */
114 char buf[512]; /* line buffer */
116 while (1) /* process one char at a time */
118 /* this char from the DOCSTR file */
119 register int ch = getchar ();
121 /* Beginnings */
123 if (state == WAITING)
125 if (ch == MARKER)
126 state = BEG_NAME;
128 else if (state == BEG_NAME)
130 cnt++;
131 if (dp == NULL) /* first dp allocated */
133 docs = dp = (DOCSTR*) xmalloc (sizeof (DOCSTR));
135 else /* all the rest */
137 dp->next = (DOCSTR*) xmalloc (sizeof (DOCSTR));
138 dp = dp->next;
140 lp = NULL;
141 dp->next = NULL;
142 bp = buf;
143 state = NAME_GET;
144 /* Record whether function or variable. */
145 dp->type = ch;
146 ch = getchar ();
148 else if (state == BEG_DESC)
150 if (lp == NULL) /* first line for dp */
152 dp->first = lp = (LINE*)xmalloc (sizeof (LINE));
154 else /* continuing lines */
156 lp->next = (LINE*)xmalloc (sizeof (LINE));
157 lp = lp->next;
159 lp->next = NULL;
160 bp = buf;
161 state = DESC_GET;
164 /* process gets */
166 if (state == NAME_GET || state == DESC_GET)
168 if (ch != MARKER && ch != '\n' && ch != EOF)
170 *bp++ = ch;
172 else /* saving and changing state */
174 *bp = NUL;
175 bp = xstrdup (buf);
177 if (state == NAME_GET)
178 dp->name = bp;
179 else
180 lp->line = bp;
182 bp = buf;
183 state = (ch == MARKER) ? BEG_NAME : BEG_DESC;
185 } /* NAME_GET || DESC_GET */
186 if (ch == EOF)
187 break;
191 DOCSTR **array;
192 register int i; /* counter */
194 /* build array of ptrs to DOCSTRs */
196 array = (DOCSTR**)xmalloc (cnt * sizeof (*array));
197 for (dp = docs, i = 0; dp != NULL ; dp = dp->next)
198 array[i++] = dp;
200 /* sort the array by name; within each name, by type */
202 qsort ((char*)array, cnt, sizeof (DOCSTR*), cmpdoc);
204 /* write the output header */
206 printf ("\\input texinfo @c -*-texinfo-*-\n");
207 printf ("@setfilename ../info/summary\n");
208 printf ("@settitle Command Summary for GNU Emacs\n");
209 printf ("@finalout\n");
210 printf ("@unnumbered Command Summary for GNU Emacs\n");
211 printf ("@table @asis\n");
212 printf ("\n");
213 printf ("@iftex\n");
214 printf ("@global@let@ITEM@item\n");
215 printf ("@def@item{@filbreak@vskip5pt@ITEM}\n");
216 printf ("@font@tensy cmsy10 scaled @magstephalf\n");
217 printf ("@font@teni cmmi10 scaled @magstephalf\n");
218 printf ("@def\\{{@tensy@char110}}\n"); /* this backslash goes with cmr10 */
219 printf ("@def|{{@tensy@char106}}\n");
220 printf ("@def@{{{@tensy@char102}}\n");
221 printf ("@def@}{{@tensy@char103}}\n");
222 printf ("@def<{{@teni@char62}}\n");
223 printf ("@def>{{@teni@char60}}\n");
224 printf ("@chardef@@64\n");
225 printf ("@catcode43=12\n");
226 printf ("@tableindent-0.2in\n");
227 printf ("@end iftex\n");
229 /* print each function from the array */
231 for (i = 0; i < cnt; i++)
233 printf ("\n@item %s @code{%s}\n@display\n",
234 array[i]->type == 'F' ? "Function" : "Variable",
235 array[i]->name);
237 for (lp = array[i]->first; lp != NULL ; lp = lp->next)
239 for (bp = lp->line; *bp; bp++)
241 /* the characters "@{}" need special treatment */
242 if (*bp == '@' || *bp == '{' || *bp == '}')
244 putchar('@');
246 putchar(*bp);
248 putchar ('\n');
250 printf("@end display\n");
251 /* Try to avoid a save size overflow in the TeX output
252 routine. */
253 if (i%100 == 0 && i > 0 && i != cnt)
254 printf("\n@end table\n@table @asis\n");
257 printf ("@end table\n");
258 printf ("@bye\n");
261 return 0;