(x-invocation-args): Add defvar.
[emacs.git] / lib-src / sorted-doc.c
blob129ce9c83b3da61fcba58503aa983b8ffa804e25
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 and exit. */
40 fatal (s1, s2)
41 char *s1, *s2;
43 error (s1, s2);
44 exit (1);
47 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
49 error (s1, s2)
50 char *s1, *s2;
52 fprintf (stderr, "sorted-doc: ");
53 fprintf (stderr, s1, s2);
54 fprintf (stderr, "\n");
57 /* Like malloc but get fatal error if memory is exhausted. */
59 char *
60 xmalloc (size)
61 int size;
63 char *result = malloc ((unsigned)size);
64 if (result == NULL)
65 fatal ("%s", "virtual memory exhausted");
66 return result;
69 char *
70 strsav (str)
71 char * str;
73 char *buf = xmalloc (strlen (str) + 1);
74 (void) strcpy (buf, str);
75 return (buf);
78 /* Comparison function for qsort to call. */
80 int
81 cmpdoc (a, b)
82 DOCSTR **a;
83 DOCSTR **b;
85 register int val = strcmp ((*a)->name, (*b)->name);
86 if (val) return val;
87 return (*a)->type - (*b)->type;
91 enum state
93 WAITING, BEG_NAME, NAME_GET, BEG_DESC, DESC_GET
96 char *states[] =
98 "WAITING", "BEG_NAME", "NAME_GET", "BEG_DESC", "DESC_GET"
101 main ()
103 register DOCSTR *dp = NULL; /* allocated DOCSTR */
104 register LINE *lp = NULL; /* allocated line */
105 register char *bp; /* ptr inside line buffer */
106 int notfirst = 0; /* set after read something */
107 register enum state state = WAITING; /* state at start */
108 int cnt = 0; /* number of DOCSTRs read */
110 DOCSTR *docs; /* chain of allocated DOCSTRS */
111 char buf[512]; /* line buffer */
113 while (1) /* process one char at a time */
115 /* this char from the DOCSTR file */
116 register int ch = getchar ();
118 /* Beginnings */
120 if (state == WAITING)
122 if (ch == MARKER)
123 state = BEG_NAME;
125 else if (state == BEG_NAME)
127 cnt++;
128 if (dp == NULL) /* first dp allocated */
130 docs = dp = (DOCSTR*) xmalloc (sizeof (DOCSTR));
132 else /* all the rest */
134 dp->next = (DOCSTR*) xmalloc (sizeof (DOCSTR));
135 dp = dp->next;
137 lp = NULL;
138 dp->next = NULL;
139 bp = buf;
140 state = NAME_GET;
141 /* Record whether function or variable. */
142 dp->type = ch;
143 ch = getchar ();
145 else if (state == BEG_DESC)
147 if (lp == NULL) /* first line for dp */
149 dp->first = lp = (LINE*)xmalloc (sizeof (LINE));
151 else /* continuing lines */
153 lp->next = (LINE*)xmalloc (sizeof (LINE));
154 lp = lp->next;
156 lp->next = NULL;
157 bp = buf;
158 state = DESC_GET;
161 /* process gets */
163 if (state == NAME_GET || state == DESC_GET)
165 if (ch != MARKER && ch != '\n' && ch != EOF)
167 *bp++ = ch;
169 else /* saving and changing state */
171 *bp = NUL;
172 bp = strsav (buf);
174 if (state == NAME_GET)
175 dp->name = bp;
176 else
177 lp->line = bp;
179 bp = buf;
180 state = (ch == MARKER) ? BEG_NAME : BEG_DESC;
182 } /* NAME_GET || DESC_GET */
183 if (ch == EOF)
184 break;
188 DOCSTR **array;
189 register int i; /* counter */
191 /* build array of ptrs to DOCSTRs */
193 array = (DOCSTR**)xmalloc (cnt * sizeof (*array));
194 for (dp = docs, i = 0; dp != NULL ; dp = dp->next)
195 array[i++] = dp;
197 /* sort the array by name; within each name, by type */
199 qsort ((char*)array, cnt, sizeof (DOCSTR*), cmpdoc);
201 /* write the output header */
203 printf ("\\input texinfo @c -*-texinfo-*-\n");
204 printf ("@setfilename ../info/summary\n");
205 printf ("@settitle Command Summary for GNU Emacs\n");
206 printf ("@unnumbered Command Summary for GNU Emacs\n");
207 printf ("@table @asis\n");
208 printf ("\n");
209 printf ("@let@ITEM@item\n");
210 printf ("@def@item{@filbreak@vskip5pt@ITEM}\n");
211 printf ("@font@tensy cmsy10 scaled @magstephalf\n");
212 printf ("@font@teni cmmi10 scaled @magstephalf\n");
213 printf ("@def\\{{@tensy@char110}}\n"); /* this backslash goes with cmr10 */
214 printf ("@def|{{@tensy@char106}}\n");
215 printf ("@def@{{{@tensy@char102}}\n");
216 printf ("@def@}{{@tensy@char103}}\n");
217 printf ("@def<{{@teni@char62}}\n");
218 printf ("@def>{{@teni@char60}}\n");
219 printf ("@chardef@@64\n");
220 printf ("@catcode43=12\n");
221 printf ("@tableindent-0.2in\n");
223 /* print each function from the array */
225 for (i = 0; i < cnt; i++)
227 printf ("\n@item %s @code{%s}\n@display\n",
228 array[i]->type == 'F' ? "Function" : "Variable",
229 array[i]->name);
231 for (lp = array[i]->first; lp != NULL ; lp = lp->next)
233 for (bp = lp->line; *bp; bp++)
235 /* the characters "@{}" need special treatment */
236 if (*bp == '@' || *bp == '{' || *bp == '}')
238 putchar('@');
240 putchar(*bp);
242 putchar ('\n');
244 printf("@end display\n");
247 printf ("@end table\n");
248 printf ("@bye\n");
251 return 0;