#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <ctype.h>
#ifdef DOS_NT
#include <fcntl.h>
#include <io.h>
#endif
#ifndef HAVE_STDLIB_H
#ifndef WINDOWSNT
extern char *malloc ();
#endif
#endif
#define NUL '\0'
#define MARKER '\037'
#define DEBUG 0
typedef struct line LINE;
struct line
{
LINE *next;
char *line;
};
typedef struct docstr DOCSTR;
struct docstr
{
DOCSTR *next;
char *name;
LINE *first;
char type;
};
void
error (s1, s2)
char *s1, *s2;
{
fprintf (stderr, "sorted-doc: ");
fprintf (stderr, s1, s2);
fprintf (stderr, "\n");
}
void
fatal (s1, s2)
char *s1, *s2;
{
error (s1, s2);
exit (EXIT_FAILURE);
}
char *
xmalloc (size)
int size;
{
char *result = malloc ((unsigned)size);
if (result == NULL)
fatal ("%s", "virtual memory exhausted");
return result;
}
char *
xstrdup (str)
char * str;
{
char *buf = xmalloc (strlen (str) + 1);
(void) strcpy (buf, str);
return (buf);
}
int
cmpdoc (a, b)
DOCSTR **a;
DOCSTR **b;
{
register int val = strcmp ((*a)->name, (*b)->name);
if (val) return val;
return (*a)->type - (*b)->type;
}
enum state
{
WAITING, BEG_NAME, NAME_GET, BEG_DESC, DESC_GET
};
char *states[] =
{
"WAITING", "BEG_NAME", "NAME_GET", "BEG_DESC", "DESC_GET"
};
int
main ()
{
register DOCSTR *dp = NULL;
register LINE *lp = NULL;
register char *bp;
register enum state state = WAITING;
int cnt = 0;
DOCSTR *docs = NULL;
char buf[512];
#ifdef DOS_NT
if (!isatty (fileno (stdin)))
setmode (fileno (stdin), O_BINARY);
#endif
bp = buf;
while (1)
{
register int ch = getchar ();
if (state == WAITING)
{
if (ch == MARKER)
state = BEG_NAME;
}
else if (state == BEG_NAME)
{
cnt++;
if (dp == NULL)
{
docs = dp = (DOCSTR*) xmalloc (sizeof (DOCSTR));
}
else
{
dp->next = (DOCSTR*) xmalloc (sizeof (DOCSTR));
dp = dp->next;
}
lp = NULL;
dp->next = NULL;
bp = buf;
state = NAME_GET;
dp->type = ch;
ch = getchar ();
}
else if (state == BEG_DESC)
{
if (lp == NULL)
{
dp->first = lp = (LINE*)xmalloc (sizeof (LINE));
}
else
{
lp->next = (LINE*)xmalloc (sizeof (LINE));
lp = lp->next;
}
lp->next = NULL;
bp = buf;
state = DESC_GET;
}
if (state == NAME_GET || state == DESC_GET)
{
if (ch != MARKER && ch != '\n' && ch != EOF)
{
*bp++ = ch;
}
else
{
*bp = NUL;
bp = xstrdup (buf);
if (state == NAME_GET)
dp->name = bp;
else
lp->line = bp;
bp = buf;
state = (ch == MARKER) ? BEG_NAME : BEG_DESC;
}
}
if (ch == EOF)
break;
}
{
DOCSTR **array;
register int i;
array = (DOCSTR**)xmalloc (cnt * sizeof (*array));
for (dp = docs, i = 0; dp != NULL ; dp = dp->next)
array[i++] = dp;
qsort ((char*)array, cnt, sizeof (DOCSTR*), cmpdoc);
printf ("\\input texinfo @c -*-texinfo-*-\n");
printf ("@setfilename ../info/summary\n");
printf ("@settitle Command Summary for GNU Emacs\n");
printf ("@finalout\n");
printf ("@unnumbered Command Summary for GNU Emacs\n");
printf ("@table @asis\n");
printf ("\n");
printf ("@iftex\n");
printf ("@global@let@ITEM@item\n");
printf ("@def@item{@filbreak@vskip5pt@ITEM}\n");
printf ("@font@tensy cmsy10 scaled @magstephalf\n");
printf ("@font@teni cmmi10 scaled @magstephalf\n");
printf ("@def\\{{@tensy@char110}}\n");
printf ("@def|{{@tensy@char106}}\n");
printf ("@def@{{{@tensy@char102}}\n");
printf ("@def@}{{@tensy@char103}}\n");
printf ("@def<{{@teni@char62}}\n");
printf ("@def>{{@teni@char60}}\n");
printf ("@chardef@@64\n");
printf ("@catcode43=12\n");
printf ("@tableindent-0.2in\n");
printf ("@end iftex\n");
for (i = 0; i < cnt; i++)
{
printf ("\n@item %s @code{%s}\n@display\n",
array[i]->type == 'F' ? "Function" : "Variable",
array[i]->name);
for (lp = array[i]->first; lp != NULL ; lp = lp->next)
{
for (bp = lp->line; *bp; bp++)
{
if (*bp == '@' || *bp == '{' || *bp == '}')
{
putchar('@');
}
putchar(*bp);
}
putchar ('\n');
}
printf("@end display\n");
if (i%100 == 0 && i > 0 && i != cnt)
printf("\n@end table\n@table @asis\n");
}
printf ("@end table\n");
printf ("@bye\n");
}
return EXIT_SUCCESS;
}