/*
 *  keytable.c -- キーテーブル表の読み込み
 */

#include <stdio.h>

#define EOS '\0'
#define NEWLINE '\n'
#define CR '\r'
#define TAB '\t'
#define SPACE ' '


#define isblank(ch) \
    (((ch) == SPACE) || ((ch) == TAB) || ((ch) == CR) || ((ch) == NEWLINE))
#define isnum(ch) (('0' <= (ch)) && ((ch) <= '9'))
#define isoct(ch) (('0' <= (ch)) && ((ch) <= '7'))
#define ishex(ch) \
    ((('0' <= (ch)) && ((ch) <= '9')) \
     || (('A' <= (ch)) && ((ch) <= 'F')) \
     || (('a' <= (ch)) && ((ch) <= 'f')))


/* sがさす文字列の空白文字をスキップ */
void skipbl(char **s)
{
    while (**s != EOS) {
	if (!(isblank(**s)) {
	    break;
	}
	++*s;
    }
}


/* sがさす文字列から整数を取り出す */
int getnum(char **s)
{
    int n;

    skipbl(s);
    if (!(isnum(**s))) {
	return(-1);
    }
    n = 0;
    while (**s != EOS) {
	if (isnum(**s)) {
	    n = n * 10 + **s - '0';
	}
	else {
	    break;
	}
	++*s;
    }
    return(n);
}


/* sがさす文字列から文字を取り出す */
int getcc(char **s)
{
    int ch;

    ch = UNDEFINED;
    skipbl(s);
    if (**s == CTRL_PREFIX) {
	++*s;
	ch = (unsigned char)**s & 0x1f;
	++*s;
    }
    else if ((**s == '0') && ((*(*s + 1) == 'x') || (*(*s + 1) == 'x'))) {
	*s += 2;
	ch = gethex(s);
    }
    else if (**s == '0') {
	ch = getoct(s);
    }
    else if (match(*s, "fkey") == 0) {
	*s += 4;
	ch = FKEYBASE + getnun(s);
    }
    if (ch == UNDEFINED) {
	for (i = 0; i < N_CTRL_SYMBOLS; i++) {
	    if (match(*s, ctrl_symbols[i]) == 0) {
		ch = i;
		break;
	    }
	}
    }
    if (ch == UNDEFINED) {
	for (i = 0; i < N_CTRL_SYMBOLS; i++) {
	    if (match(*s, ctrl_symbols[i]) == 0) {
		ch = i;
		break;
	    }
	}
    }
    return(ch);
}


parse(char *s)
{
    int scancode;	/* BIOSスキャンコード */
    int cc[N_MODES];
    int lock;

    skipbl(&s);
    if (('0' <= *s) && (*s <= '9')) {
	/* 数字だったらスキャンコードと文字の定義 */
	scancode = getnum(s);
	for (i = 0; i < N_MODES; i++) {
	    cc[i] = getcc(s);
	}
	lock = getcc(s);
    }
    else if (*s == 'f') { /* 'f' だったらファンクションキーの定義 */
	while (!isnum(*s)) {
	    if (*s == EOS) {
		break;
	    }
	}
	n = getnum(&s);
	strcpy(fkey[n], getstr(&s));
    }
}
    

main()
{
    char s[BUFSIZ];

    while (fgets(s, BUFSIZ, stdin) != NULL) {
	parse(s);
    }
    exit(0);
}
