/************************************************************************
 * This program is Copyright (C) 1986 by Jonathan Payne.  JOVE is       *
 * provided to you without charge, and with no warranty.  You may give  *
 * away copies of JOVE, including sources, provided that this notice is *
 * included in all the files.                                           *
 ************************************************************************/

#define NOT_JOVE

#include "tune.h"

RCS("$Id: setmaps.c,v 14.28 1992/10/23 17:15:59 tom Exp tom $")

#include <stdio.h>

/* et voila! le trick salaud: */

#define DEF_CMD(name,f,opt)	name,
#define DEF_MAJ(name,n,desc)	name,
#define DEF_MIN(name,n,desc)	name,
#define DEF_MAP(name,map,n)	name,

#define DEF_INT(name,v,opt)	name,
#define DEF_STR(name,v,l,opt)	name,


const char * const commands[] = {
#	include "funcs.inc"
	NULL
};

const char * const variables[] = {
#	include "vars.inc"
	NULL
};

const char * const allowed[] = {
	"unbound",
#if (BPC != 8)
	"accent",
#endif
#ifndef MOUSE
	"handle-mouse",
#endif
	NULL
};

char	line[130];
char	replbuf[40];

int match __(( const char * const *_(choices), const char *_(what),
	       size_t _(len) ));

int
match(choices, what, len)
const char		* const *choices;
register const char	*what;
size_t			len;
{
	register const char	* const *cp;

	if (len <= 0)
		return -1;

	for (cp = choices; *cp;  cp++) {
		if (*what != **cp)
			continue;
		if (strncmp(what, *cp, len) == 0)
			return (int)(cp - choices);
	}
	return -1;
}

void
main(argc, argv)
int	argc;
char	*argv[];
{
	register char	*lp,
			*name;
	register int	i,
			lineno = 0,
			status = 0;
	const char	*filename = argv[1];

	if (filename == NULL)
		filename = "<stdin>";

	while (fgets(lp = line, sizeof line, stdin) != NULL) {
		lineno++;

		if ((*lp++ != '\t') || (*lp++ != '"') || /* not an entry */
		    (lp = index(name = lp, '"')) == NULL) { /* single quote */
			fputs(line, stdout);
			continue;
		}

		*lp = '\0';	/* zero-terminate name */

		replbuf[0] = '0', replbuf[1] = '\0';	/* suitable default */

		if ((i = match(commands, name, lp - name)) >= 0) {
			sprintf(replbuf, "&commands[%d]", i);
		}
		else if (strncmp(name, "set ", 4) == 0) {
			if ((i = match(variables, name+4, lp - name-4)) >= 0) {
				sprintf(replbuf, "&variables[%d]", i);
			}
			else {
				++status;
				fprintf(stderr,
					"%s:%d: Cannot find variable \"%s\"\n",
					filename, lineno, name+4);
			}
		}
		else if (match(allowed, name, lp - name) < 0) {
			++status;
			fprintf(stderr, "%s:%d: Cannot find command \"%s\"\n",
				filename, lineno, name);
		}
		printf("\t(data_obj *) %-15s/* %s */%s", replbuf, name, ++lp);
	}
	exit(status);
}

/*======================================================================
 * $Log: setmaps.c,v $
 * Revision 14.28  1992/10/23  17:15:59  tom
 * convert to "port{ansi,defs}.h" conventions; ditch DEF_SPARSE().
 *
 * Revision 14.27  1992/09/21  23:52:31  tom
 * make error messages conform to JOVEs "parse-errors" format.
 *
 * Revision 14.26  1992/08/26  23:57:06  tom
 * add RCS directives.
 *
 */
