%{
/*
 * klex.l -- lexical analyzer for the keybind mini-language interpreter
 *
 * Copyright 1989 by Eric S. Raymond <esr@snark.thyrsus.com>, all rights
 * reserved.  You may use, modify or redistribute this code freely, but do not
 * delete this notice. Check with me before you make money with it.
 */
/*LINTLIBRARY*/
#ifndef lint
static char *sccsid = "@(#)klex1.l	1.3";
#endif /* lint */

#include <sys/types.h>
#include <sys/at_ansi.h>
#include <sys/kd.h>
#include <string.h>
#include <ctype.h>
#include "ascii.h"
#include "keybind.h"
#include "y.tab.h"

extern yylval;	/* used to pass token values to YACC */

static int yytokno = 0;
static int smashcase = 1;
static int interactive = 0;

#undef input
int input()
{
    static int doprompt;
    int c;

    if (doprompt-- == 1)
	(void) fputs("*", stdout);

    if ((c = getc(yyin)) == '\n')
    {
	yylineno++;
	if (interactive)
	    doprompt = 1;
    }

    if (feof(yyin))
	return(0);
    else if (smashcase && isupper(c))    
	return(tolower(c));
    else
	return(c);
}

#undef unput
#define unput(c)	ungetc(c, yyin)

extern void dump_string();
static void getstring();
%}

%e	1700
%p	3700
%n	500
%k	50
%a	900
%o	800

%%

clearstrings|cl	{yytokno++; return(CLEARSTRINGS);}
disabled|di	{yytokno++; return(DISABLED);}
dump|du		{yytokno++; return(DUMP);}
enabled|en	{yytokno++; return(ENABLED);}
execute|ex	{yytokno++; return(EXECUTE);}
funkeys|fu	{yytokno++; return(FUNKEYS);}
help|he		{yytokno++; interactive++; return(HELP);}
meta|me		{yytokno++; return(META);}
restore|re	{yytokno++; return(RESTORE);}

shift-?		{yylval = SHIFTED; yytokno++; return(PREFIX);}
ctrl-?		{yylval = CTRLED; yytokno++; return(PREFIX);}
alt-?		{yylval = ALTED; yytokno++; return(PREFIX);}

capslock|ca	{yylval = KMF_CLOCK; yytokno++; return(SHIFTER);}
shiftlock|sh	{yylval = KMF_CLOCK; yytokno++; return(SHIFTER);}
numlock|nu	{yylval = KMF_NLOCK; yytokno++; return(SHIFTER);}

\"		{getstring(yytext, '"'); yytokno++; return(STRING);}
'		{
		    getstring(yytext, '\'');
		    if (strlen(yytext) == 1)
		    {
			yylval = yytext[0];
			yytokno++;
			return(CHARKEY);
		    }
	    	    else
		    {
			(void) fprintf(stderr,
			    "keybind: bad character syntax, line %d token %d\n",
			     yylineno + 1, yytokno);
			interactive--;
			while (input() != '\n')
			    continue;
			interactive++;
			return(LEXERR);
		    }
		}

@[0-9][0-9][0-9]	{yylval=atoi(yytext+1); yytokno++; return(SCANCODE);}
