#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int
main(int argc, char *argv[])
{
    void lower(char *);
    char buffer[BUFSIZ], *p;
    FILE *in, *out;
    int  first=0x7fffffff, last=0, n;

    if (argc != 3) {
	fprintf(stderr, "Usage: make_table input.h output.h\n");
	exit(1);
    }
    if ((in = fopen(argv[1], "r")) == NULL) {
	perror("fopen");
	exit(1);
    }
    if ((out = fopen(argv[2], "w")) == NULL) {
	perror("fopen");
	exit(1);
    }
    fprintf(out, "static void (*efun_table[])(int) = {\n");
    while (fgets(buffer, sizeof(buffer), in)) {
	if (buffer[0] != '#')
	    continue;
	p = strtok(buffer+1, " \t\n");
	if (strcmp(p, "define"))
	    continue;
	if ((p = strtok(NULL, " \t\n")) == NULL)
	    continue;
	lower(p);
	fprintf(out, "\t%s,\n", p);
	if ((p = strtok(NULL, " \t\n")) == NULL)
	    continue;
	n = atoi(p);
	if (n < first)
	    first = n;
	if (n > last)
	    last = n;
    }
    fprintf(out, "};\n\n");
    fprintf(out, "#define EFUN_FIRST %4d\n", first);
    fprintf(out, "#define EFUN_LAST  %4d\n", last);
    fclose(out);
    fclose(in);
    exit(0);
}

void
lower(char *string)
{
    while (*string) {
        if (isupper(*string))
	    *string = tolower(*string);
	string++;
    }
}