%{
#include <stdlib.h>
#include <linux/kd.h>

int ksymtocode(char *s);

int	line_nr=1;
int	yylval;
struct kbsentry kbs_buf;
char *p;

#undef yywrap
extern int yywrap(void);
extern int yyerror(char *s);

%}

%s RVALUE
%x STR
Eol			\n
Blank			[ \t]
Decimal			[1-9][0-9]*
Octal			0[0-7]*
Hex			0[xX][0-9a-fA-F]+
Literal			[a-zA-Z][a-zA-Z_0-9]*
Octa			([0-7]){1,3}
Charset			[cC][hH][aA][rR][sS][eE][tT]
Keycode			[kK][eE][yY][cC][oO][dD][eE]
String			[sS][tT][rR][iI][nN][gG]
Compose                 [cC][oO][mM][pP][oO][sS][eE]
To                      [tT][oO]
Equals			=
Shift			[sS][hH][iI][fF][tT]
Control			[cC][oO][nN][tT][rR][oO][lL]
Alt			[aA][lL][tT]
AltGr			[aA][lL][tT][gG][rR]
Comment			#|!
Continuation		\\\n
%%
{Continuation}		{line_nr++;}
{Eol}			{line_nr++;BEGIN(0);return(EOL);}
{Blank}+		; /* do nothing */
{Comment}.*/{Eol}	; /* do nothing */
{Equals}		{BEGIN(RVALUE);return(EQUALS);}
\+			{return(PLUS);}
{Decimal}|{Octal}|{Hex}	{yylval=strtol(yytext,NULL,0);return(NUMBER);}
<RVALUE>{Literal}	{return((yylval=ksymtocode(yytext))==-1?ERROR:LITERAL);}
{Charset}		{return(CHARSET);}
{Keycode}		{return(KEYCODE);}
{Compose}               {return(COMPOSE);}
{To}                    {return(TO);}
{String}		{BEGIN(RVALUE);return(STRING);}
{Shift}			{return(SHIFT);}
{Control}		{return(CONTROL);}
{Alt}			{return(ALT);}
{AltGr}			{return(ALTGR);}
'\\{Octa}'              {yylval = strtol(yytext+2,NULL,8); return(CCHAR);}
'\\.'                   {yylval = yytext[2]; return(CCHAR);}
'.'                     {yylval = yytext[1]; return(CCHAR);}
\"			{p=kbs_buf.kb_string;BEGIN(STR);}
<STR>\\{Octa}		{*p++=strtol(yytext+1,NULL,8);}
<STR>\\\"               {*p++='"';}
<STR>\\\\               {*p++='\\';}
<STR>\\n		{*p++='\n';}
<STR>[^"\\]*		{strcpy(p,yytext);p+=strlen(yytext);}
<STR>\"			{*p='\0';BEGIN(0);return(STRLITERAL);}
.			{return(ERROR); /* report any unknown characters */}
%%
#include "ksyms.h"
#include <linux/keyboard.h>

static struct sym_code {
	char *keysym;
	int code;
}
depr[] = {
	{ "Shift_L",	K_SHIFT },
	{ "Shift_R",	K_SHIFT },
	{ "Control_L",	K_CTRL },
	{ "Control_R",	K_CTRL },
        { "Alt_L",      K_ALT },
        { "Alt_R",      K_ALTGR },
	{ "AltGr_L",	K_ALTGR }
};

int
ksymtocode(char *s)
{
	int i;
	int j;
	static warn = 0;

	for (i = 0; i < syms_size; i++)
		for (j = 0; j < syms[i].size; j++)
			if (!strcmp(s,syms[i].table[j]))
				return K(i, j);

	for (i = 0; i < syn_size; i++)
		if (!strcmp(s, synonyms[i].synonym))
			return ksymtocode(synonyms[i].official_name);

	for (i = 0; i < sizeof(depr) / sizeof(depr[0]); i++)
		if (!strcmp(s, depr[i].keysym)) {
			j = depr[i].code;
#if 0
			/* Soon these symbols will be back */
			printf("symbol `%s' depreciated, use `%s' instead\n",
				s, syms[KTYP(j)].table[KVAL(j)]);
#endif
			return j;
		}

	for (i = 0; i < 256 - 160; i++)
		if (!strcmp(s, latin2_syms[i])) {
			if(!warn++)
				fprintf(stderr, "assuming iso-8859-2\n");
			return K(KT_LATIN, 160 + i);
		}

	for (i = 0; i < 256 - 160; i++)
		if (!strcmp(s, latin3_syms[i])) {
			if(!warn++)
				fprintf(stderr, "assuming iso-8859-3\n");
			return K(KT_LATIN, 160 + i);
		}

	for (i = 0; i < 256 - 160; i++)
		if (!strcmp(s, latin4_syms[i])) {
			if(!warn++)
				fprintf(stderr, "assuming iso-8859-4\n");
			return K(KT_LATIN, 160 + i);
		}

	fprintf(stderr, "unknown keysym '%s'\n", s);

	return -1;
}
