/* vi:set sts=4 sw=4:
 *
 * VIM - Vi IMproved	by Bram Moolenaar
 *
 * Do ":help uganda"  in Vim to read copying and usage conditions.
 * Do ":help credits" in Vim to see a list of people who contributed.
 */

/*
 * mkcmdtab.c
 *
 * Standalone program that reads "ex_docmd.h" and produces "ex_cmds.h".
 *
 * Execute with: mkcmdtab ex_docmd.h ex_cmds.h
 */

#ifdef CROSS_COMPILING
# include <stdio.h>
#else
# include "vim.h"
#endif

#if defined(UTS4)
    int
#else
    void
#endif
main(argc, argv)
    int		argc;
    char	**argv;
{
    int		c;
    char	buffer[100];
    int		count;
    int		i;
    FILE	*ifp, *ofp;

    if (argc != 3)
    {
	fprintf(stderr, "Usage: mkcmdtab ex_docmd.h ex_cmds.h\n");
	exit(10);
    }
    ifp = fopen(argv[1], "r");
    if (ifp == NULL)
    {
	perror(argv[1]);
	exit(10);
    }
    ofp = fopen(argv[2], "w");
    if (ofp == NULL)
    {
	perror(argv[2]);
	exit(10);
    }

    fprintf(ofp, "/* THIS FILE IS AUTOMATICALLY PRODUCED - DO NOT EDIT */\n");

    /* Skip until the start of the table */
    while ((c = getc(ifp)) != '|' && c != EOF)
	;

    for (count = 0;; ++count)
    {
	/*
	 * Find the command name, inside quotes
	 * Copy it to buffer[].
	 */
	while ((c = getc(ifp)) != '"' && c != EOF)
	    ;
	if (c == EOF)
	    break;

	for (i = 0; (c = getc(ifp)) != '"' && c != EOF; ++i)
	    buffer[i] = c;
	buffer[i] = 0;

	/*
	 * Translate special-character commands to a name.
	 */
	switch (buffer[0])
	{
	    case '@':	strcpy(buffer, "at");
			break;
	    case '!':	strcpy(buffer, "bang");
			break;
	    case '<':	strcpy(buffer, "lshift");
			break;
	    case '>':	strcpy(buffer, "rshift");
			break;
	    case '=':	strcpy(buffer, "equal");
			break;
	    case '&':	strcpy(buffer, "and");
			break;
	    case '~':	strcpy(buffer, "tilde");
			break;
	    case '#':	strcpy(buffer, "pound");
			break;
	}

	fprintf(ofp, "#define CMD_%s %d\n", buffer, count);
    }

    fprintf(ofp, "#define CMD_SIZE %d\n", count);

    exit(0);
}
