*** empty log message ***
[emacs.git] / lib-src / sorted-doc.c
blob5e98df2c0c0ce1d9de64c2b967b9ed3c208d6f56
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 <stdio.h>
9 #include <ctype.h>
11 extern char *malloc ();
12 char *xmalloc ();
14 #define NUL '\0'
15 #define MARKER '\037'
17 #define DEBUG 0
19 typedef struct line LINE;
21 struct line
23 LINE *next; /* ptr to next or NULL */
24 char *line; /* text of the line */
27 typedef struct docstr DOCSTR;
29 struct docstr /* Allocated thing for an entry. */
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 */
38 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
40 void
41 error (s1, s2)
42 char *s1, *s2;
44 fprintf (stderr, "sorted-doc: ");
45 fprintf (stderr, s1, s2);
46 fprintf (stderr, "\n");
49 /* Print error message and exit. */
51 void
52 fatal (s1, s2)
53 char *s1, *s2;
55 error (s1, s2);
56 exit (1);
59 /* Like malloc but get fatal error if memory is exhausted. */
61 char *
62 xmalloc (size)
63 int size;
65 char *result = malloc ((unsigned)size);
66 if (result == NULL)
67 fatal ("%s", "virtual memory exhausted");
68 return result;
71 char *
72 xstrdup (str)
73 char * str;
75 char *buf = xmalloc (strlen (str) + 1);
76 (void) strcpy (buf, str);
77 return (buf);
80 /* Comparison function for qsort to call. */
82 int
83 cmpdoc (a, b)
84 DOCSTR **a;
85 DOCSTR **b;
87 register int val = strcmp ((*a)->name, (*b)->name);
88 if (val) return val;
89 return (*a)->type - (*b)->type;
93 enum state
95 WAITING, BEG_NAME, NAME_GET, BEG_DESC, DESC_GET
98 char *states[] =
100 "WAITING", "BEG_NAME", "NAME_GET", "BEG_DESC", "DESC_GET"
104 main ()
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 */
112 DOCSTR *docs; /* chain of allocated DOCSTRS */
113 char buf[512]; /* line buffer */
115 while (1) /* process one char at a time */
117 /* this char from the DOCSTR file */
118 register int ch = getchar ();
120 /* Beginnings */
122 if (state == WAITING)
124 if (ch == MARKER)
125 state = BEG_NAME;
127 else if (state == BEG_NAME)
129 cnt++;
130 if (dp == NULL) /* first dp allocated */
132 docs = dp = (DOCSTR*) xmalloc (sizeof (DOCSTR));
134 else /* all the rest */
136 dp->next = (DOCSTR*) xmalloc (sizeof (DOCSTR));
137 dp = dp->next;
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 ();
147 else if (state == BEG_DESC)
149 if (lp == NULL) /* first line for dp */
151 dp->first = lp = (LINE*)xmalloc (sizeof (LINE));
153 else /* continuing lines */
155 lp->next = (LINE*)xmalloc (sizeof (LINE));
156 lp = lp->next;
158 lp->next = NULL;
159 bp = buf;
160 state = DESC_GET;
163 /* process gets */
165 if (state == NAME_GET || state == DESC_GET)
167 if (ch != MARKER && ch != '\n' && ch != EOF)
169 *bp++ = ch;
171 else /* saving and changing state */
173 *bp = NUL;
174 bp = xstrdup (buf);
176 if (state == NAME_GET)
177 dp->name = bp;
178 else
179 lp->line = bp;
181 bp = buf;
182 state = (ch == MARKER) ? BEG_NAME : BEG_DESC;
184 } /* NAME_GET || DESC_GET */
185 if (ch == EOF)
186 break;
190 DOCSTR **array;
191 register int i; /* counter */
193 /* build array of ptrs to DOCSTRs */
195 array = (DOCSTR**)xmalloc (cnt * sizeof (*array));
196 for (dp = docs, i = 0; dp != NULL ; dp = dp->next)
197 array[i++] = dp;
199 /* sort the array by name; within each name, by type */
201 qsort ((char*)array, cnt, sizeof (DOCSTR*), cmpdoc);
203 /* write the output header */
205 printf ("\\input texinfo @c -*-texinfo-*-\n");
206 printf ("@setfilename ../info/summary\n");
207 printf ("@settitle Command Summary for GNU Emacs\n");
208 printf ("@finalout\n");
209 printf ("@unnumbered Command Summary for GNU Emacs\n");
210 printf ("@table @asis\n");
211 printf ("\n");
212 printf ("@iftex\n");
213 printf ("@global@let@ITEM@item\n");
214 printf ("@def@item{@filbreak@vskip5pt@ITEM}\n");
215 printf ("@font@tensy cmsy10 scaled @magstephalf\n");
216 printf ("@font@teni cmmi10 scaled @magstephalf\n");
217 printf ("@def\\{{@tensy@char110}}\n"); /* this backslash goes with cmr10 */
218 printf ("@def|{{@tensy@char106}}\n");
219 printf ("@def@{{{@tensy@char102}}\n");
220 printf ("@def@}{{@tensy@char103}}\n");
221 printf ("@def<{{@teni@char62}}\n");
222 printf ("@def>{{@teni@char60}}\n");
223 printf ("@chardef@@64\n");
224 printf ("@catcode43=12\n");
225 printf ("@tableindent-0.2in\n");
226 printf ("@end iftex\n");
228 /* print each function from the array */
230 for (i = 0; i < cnt; i++)
232 printf ("\n@item %s @code{%s}\n@display\n",
233 array[i]->type == 'F' ? "Function" : "Variable",
234 array[i]->name);
236 for (lp = array[i]->first; lp != NULL ; lp = lp->next)
238 for (bp = lp->line; *bp; bp++)
240 /* the characters "@{}" need special treatment */
241 if (*bp == '@' || *bp == '{' || *bp == '}')
243 putchar('@');
245 putchar(*bp);
247 putchar ('\n');
249 printf("@end display\n");
250 /* Try to avoid a save size overflow in the TeX output
251 routine. */
252 if (i%100 == 0 && i > 0 && i != cnt)
253 printf("\n@end table\n@table @asis\n");
256 printf ("@end table\n");
257 printf ("@bye\n");
260 return 0;