From 549a021d0aec8b88735baae98d2b357ef9722f9a Mon Sep 17 00:00:00 2001 From: Alexey Gladkov Date: Tue, 1 Mar 2011 02:43:36 +0300 Subject: [PATCH] loadkeys: Use Linux coding style Signed-off-by: Alexey Gladkov --- src/loadkeys.c | 1051 ++++++++++++++++++++++++++++++-------------------------- src/loadkeys.y | 966 +++++++++++++++++++++++++++------------------------ 2 files changed, 1083 insertions(+), 934 deletions(-) diff --git a/src/loadkeys.c b/src/loadkeys.c index e421bd6..16bd20b 100644 --- a/src/loadkeys.c +++ b/src/loadkeys.c @@ -104,7 +104,7 @@ /* What keymaps are we defining? */ char defining[MAX_NR_KEYMAPS]; char keymaps_line_seen = 0; -int max_keymap = 0; /* from here on, defining[] is false */ +int max_keymap = 0; /* from here on, defining[] is false */ int alt_is_meta = 0; /* the kernel structures we want to set or print */ @@ -116,7 +116,7 @@ unsigned int accent_table_size = 0; char key_is_constant[NR_KEYS]; char *keymap_was_set[MAX_NR_KEYMAPS]; -char func_buf[4096]; /* should be allocated dynamically */ +char func_buf[4096]; /* should be allocated dynamically */ char *fp = func_buf; #undef ECHO @@ -127,7 +127,7 @@ static void addfunc(struct kbsentry kbs_buf); static void killkey(int index, int table); static void compose(int diacr, int base, int res); static void do_constant(void); -static void do_constant_key (int, u_short); +static void do_constant_key(int, u_short); static void loadkeys(char *console, int kbd_mode); static void strings_as_usual(void); static void compose_as_usual(char *charset); @@ -143,6 +143,7 @@ int private_error_ct = 0; extern int rvalct; extern struct kbsentry kbs_buf; + int yyerror(const char *s); FILE *find_incl_file_near_fn(char *s, char *fn); @@ -153,16 +154,16 @@ void open_include(char *s); extern char *filename; extern int line_nr; -extern void stack_push(FILE *fd, int ispipe, char *filename); +extern void stack_push(FILE * fd, int ispipe, char *filename); extern int stack_pop(void); - #include "ksyms.h" int yylex(void); + /* Line 189 of yacc.c */ -#line 166 "loadkeys.c" +#line 167 "loadkeys.c" /* Enabling traces. */ #ifndef YYDEBUG @@ -274,7 +275,7 @@ typedef int YYSTYPE; /* Line 264 of yacc.c */ -#line 278 "loadkeys.c" +#line 279 "loadkeys.c" #ifdef short # undef short @@ -579,12 +580,12 @@ static const yytype_int8 yyrhs[] = /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ static const yytype_uint16 yyrline[] = { - 0, 106, 106, 107, 109, 110, 111, 112, 113, 114, - 115, 116, 117, 118, 120, 125, 130, 135, 139, 144, - 149, 150, 152, 158, 163, 172, 176, 181, 183, 186, - 186, 191, 196, 197, 199, 200, 201, 202, 203, 204, - 205, 206, 207, 209, 242, 243, 245, 252, 254, 256, - 258, 260, 262 + 0, 107, 107, 108, 110, 111, 112, 113, 114, 115, + 116, 117, 118, 119, 121, 126, 131, 136, 140, 145, + 150, 151, 153, 159, 164, 173, 177, 182, 186, 191, + 191, 198, 203, 204, 206, 207, 208, 209, 210, 211, + 212, 213, 214, 216, 254, 255, 257, 264, 265, 266, + 267, 268, 269 }; #endif @@ -1556,303 +1557,314 @@ yyreduce: case 14: /* Line 1464 of yacc.c */ -#line 121 "loadkeys.y" +#line 122 "loadkeys.y" { - set_charset((char *) kbs_buf.kb_string); + set_charset((char *) kbs_buf.kb_string); } break; case 15: /* Line 1464 of yacc.c */ -#line 126 "loadkeys.y" +#line 127 "loadkeys.y" { - alt_is_meta = 1; + alt_is_meta = 1; } break; case 16: /* Line 1464 of yacc.c */ -#line 131 "loadkeys.y" +#line 132 "loadkeys.y" { - strings_as_usual(); + strings_as_usual(); } break; case 17: /* Line 1464 of yacc.c */ -#line 136 "loadkeys.y" +#line 137 "loadkeys.y" { - compose_as_usual((char *) kbs_buf.kb_string); + compose_as_usual((char *) kbs_buf.kb_string); } break; case 18: /* Line 1464 of yacc.c */ -#line 140 "loadkeys.y" +#line 141 "loadkeys.y" { - compose_as_usual(0); + compose_as_usual(0); } break; case 19: /* Line 1464 of yacc.c */ -#line 145 "loadkeys.y" +#line 146 "loadkeys.y" { - keymaps_line_seen = 1; + keymaps_line_seen = 1; } break; case 22: /* Line 1464 of yacc.c */ -#line 153 "loadkeys.y" +#line 154 "loadkeys.y" { - int i; - for (i = (yyvsp[(1) - (3)]); i<= (yyvsp[(3) - (3)]); i++) - addmap(i,1); + int i; + for (i = (yyvsp[(1) - (3)]); i <= (yyvsp[(3) - (3)]); i++) + addmap(i,1); } break; case 23: /* Line 1464 of yacc.c */ -#line 159 "loadkeys.y" +#line 160 "loadkeys.y" { - addmap((yyvsp[(1) - (1)]),1); + addmap((yyvsp[(1) - (1)]),1); } break; case 24: /* Line 1464 of yacc.c */ -#line 164 "loadkeys.y" +#line 165 "loadkeys.y" { - if (KTYP((yyvsp[(2) - (5)])) != KT_FN) - lkfatal(_("'%s' is not a function key symbol"), - syms[KTYP((yyvsp[(2) - (5)]))].table[KVAL((yyvsp[(2) - (5)]))]); - kbs_buf.kb_func = KVAL((yyvsp[(2) - (5)])); - addfunc(kbs_buf); + if (KTYP((yyvsp[(2) - (5)])) != KT_FN) + lkfatal(_("'%s' is not a function key symbol"), + syms[KTYP((yyvsp[(2) - (5)]))].table[KVAL((yyvsp[(2) - (5)]))]); + kbs_buf.kb_func = KVAL((yyvsp[(2) - (5)])); + addfunc(kbs_buf); } break; case 25: /* Line 1464 of yacc.c */ -#line 173 "loadkeys.y" +#line 174 "loadkeys.y" { - compose((yyvsp[(2) - (6)]), (yyvsp[(3) - (6)]), (yyvsp[(5) - (6)])); + compose((yyvsp[(2) - (6)]), (yyvsp[(3) - (6)]), (yyvsp[(5) - (6)])); } break; case 26: /* Line 1464 of yacc.c */ -#line 177 "loadkeys.y" +#line 178 "loadkeys.y" { - compose((yyvsp[(2) - (6)]), (yyvsp[(3) - (6)]), (yyvsp[(5) - (6)])); + compose((yyvsp[(2) - (6)]), (yyvsp[(3) - (6)]), (yyvsp[(5) - (6)])); } break; case 27: /* Line 1464 of yacc.c */ -#line 182 "loadkeys.y" - { (yyval) = (yyvsp[(1) - (1)]); } +#line 183 "loadkeys.y" + { + (yyval) = (yyvsp[(1) - (1)]); + } break; case 28: /* Line 1464 of yacc.c */ -#line 184 "loadkeys.y" - { (yyval) = (yyvsp[(1) - (1)]) ^ 0xf000; } +#line 187 "loadkeys.y" + { + (yyval) = (yyvsp[(1) - (1)]) ^ 0xf000; + } break; case 29: /* Line 1464 of yacc.c */ -#line 186 "loadkeys.y" - { mod = 0; } +#line 191 "loadkeys.y" + { + mod = 0; + } break; case 30: /* Line 1464 of yacc.c */ -#line 188 "loadkeys.y" +#line 195 "loadkeys.y" { - addkey((yyvsp[(4) - (7)]), mod, (yyvsp[(6) - (7)])); + addkey((yyvsp[(4) - (7)]), mod, (yyvsp[(6) - (7)])); } break; case 31: /* Line 1464 of yacc.c */ -#line 192 "loadkeys.y" +#line 199 "loadkeys.y" { - addkey((yyvsp[(3) - (6)]), 0, (yyvsp[(5) - (6)])); + addkey((yyvsp[(3) - (6)]), 0, (yyvsp[(5) - (6)])); } break; case 34: /* Line 1464 of yacc.c */ -#line 199 "loadkeys.y" +#line 206 "loadkeys.y" { mod |= M_SHIFT; } break; case 35: /* Line 1464 of yacc.c */ -#line 200 "loadkeys.y" +#line 207 "loadkeys.y" { mod |= M_CTRL; } break; case 36: /* Line 1464 of yacc.c */ -#line 201 "loadkeys.y" +#line 208 "loadkeys.y" { mod |= M_ALT; } break; case 37: /* Line 1464 of yacc.c */ -#line 202 "loadkeys.y" +#line 209 "loadkeys.y" { mod |= M_ALTGR; } break; case 38: /* Line 1464 of yacc.c */ -#line 203 "loadkeys.y" +#line 210 "loadkeys.y" { mod |= M_SHIFTL; } break; case 39: /* Line 1464 of yacc.c */ -#line 204 "loadkeys.y" +#line 211 "loadkeys.y" { mod |= M_SHIFTR; } break; case 40: /* Line 1464 of yacc.c */ -#line 205 "loadkeys.y" +#line 212 "loadkeys.y" { mod |= M_CTRLL; } break; case 41: /* Line 1464 of yacc.c */ -#line 206 "loadkeys.y" +#line 213 "loadkeys.y" { mod |= M_CTRLR; } break; case 42: /* Line 1464 of yacc.c */ -#line 207 "loadkeys.y" +#line 214 "loadkeys.y" { mod |= M_CAPSSHIFT; } break; case 43: /* Line 1464 of yacc.c */ -#line 210 "loadkeys.y" +#line 217 "loadkeys.y" { - int i, j; - - if (rvalct == 1) { - /* Some files do not have a keymaps line, and - we have to wait until all input has been read - before we know which maps to fill. */ - key_is_constant[(yyvsp[(2) - (5)])] = 1; - - /* On the other hand, we now have include files, - and it should be possible to override lines - from an include file. So, kill old defs. */ - for (j = 0; j < max_keymap; j++) - if (defining[j]) - killkey((yyvsp[(2) - (5)]), j); - } - if (keymaps_line_seen) { - i = 0; - for (j = 0; j < max_keymap; j++) - if (defining[j]) { - if (rvalct != 1 || i == 0) - addkey((yyvsp[(2) - (5)]), j, (i < rvalct) ? key_buf[i] : K_HOLE); - i++; - } - if (i < rvalct) - lkfatal(_("too many (%d) entries on one line"), rvalct); - } else - for (i = 0; i < rvalct; i++) - addkey((yyvsp[(2) - (5)]), i, key_buf[i]); - } + int i, j; + + if (rvalct == 1) { + /* Some files do not have a keymaps line, and + * we have to wait until all input has been read + * before we know which maps to fill. */ + key_is_constant[(yyvsp[(2) - (5)])] = 1; + + /* On the other hand, we now have include files, + * and it should be possible to override lines + * from an include file. So, kill old defs. */ + for (j = 0; j < max_keymap; j++) { + if (defining[j]) + killkey((yyvsp[(2) - (5)]), j); + } + } + + if (keymaps_line_seen) { + i = 0; + for (j = 0; j < max_keymap; j++) { + if (defining[j]) { + if (rvalct != 1 || i == 0) + addkey((yyvsp[(2) - (5)]), j, (i < rvalct) ? key_buf[i] : K_HOLE); + i++; + } + } + + if (i < rvalct) + lkfatal(_("too many (%d) entries on one line"), rvalct); + } else + for (i = 0; i < rvalct; i++) { + addkey((yyvsp[(2) - (5)]), i, key_buf[i]); + } + } break; case 46: /* Line 1464 of yacc.c */ -#line 246 "loadkeys.y" +#line 258 "loadkeys.y" { - if (rvalct >= MAX_NR_KEYMAPS) - lkfatal(_("too many key definitions on one line")); - key_buf[rvalct++] = (yyvsp[(1) - (1)]); + if (rvalct >= MAX_NR_KEYMAPS) + lkfatal(_("too many key definitions on one line")); + key_buf[rvalct++] = (yyvsp[(1) - (1)]); } break; case 47: /* Line 1464 of yacc.c */ -#line 253 "loadkeys.y" - {(yyval)=convert_code((yyvsp[(1) - (1)]), TO_AUTO);} +#line 264 "loadkeys.y" + { (yyval) = convert_code((yyvsp[(1) - (1)]), TO_AUTO); } break; case 48: /* Line 1464 of yacc.c */ -#line 255 "loadkeys.y" - {(yyval)=add_capslock((yyvsp[(2) - (2)]));} +#line 265 "loadkeys.y" + { (yyval) = add_capslock((yyvsp[(2) - (2)])); } break; case 49: /* Line 1464 of yacc.c */ -#line 257 "loadkeys.y" - {(yyval)=convert_code((yyvsp[(1) - (1)])^0xf000, TO_AUTO);} +#line 266 "loadkeys.y" + { (yyval) = convert_code((yyvsp[(1) - (1)])^0xf000, TO_AUTO); } break; case 50: /* Line 1464 of yacc.c */ -#line 259 "loadkeys.y" - {(yyval)=add_capslock((yyvsp[(2) - (2)])^0xf000);} +#line 267 "loadkeys.y" + { (yyval) = add_capslock((yyvsp[(2) - (2)])^0xf000); } break; case 51: /* Line 1464 of yacc.c */ -#line 261 "loadkeys.y" - {(yyval)=(yyvsp[(1) - (1)]);} +#line 268 "loadkeys.y" + { (yyval) = (yyvsp[(1) - (1)]); } break; case 52: /* Line 1464 of yacc.c */ -#line 263 "loadkeys.y" - {(yyval)=add_capslock((yyvsp[(2) - (2)]));} +#line 269 "loadkeys.y" + { (yyval) = add_capslock((yyvsp[(2) - (2)])); } break; /* Line 1464 of yacc.c */ -#line 1856 "loadkeys.c" +#line 1868 "loadkeys.c" default: break; } YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); @@ -2064,33 +2076,34 @@ yyreturn: /* Line 1684 of yacc.c */ -#line 265 "loadkeys.y" +#line 271 "loadkeys.y" -static void attr_noreturn -usage(void) { +static void attr_noreturn usage(void) +{ fprintf(stderr, _("loadkeys version %s\n" -"\n" -"Usage: loadkeys [option...] [mapfile...]\n" -"\n" -"Valid options are:\n" -"\n" -" -a --ascii force conversion to ASCII\n" -" -b --bkeymap output a binary keymap to stdout\n" -" -c --clearcompose clear kernel compose table\n" -" -C --console=\n" -" the console device(s) to be used\n" -" -d --default load \"%s\"\n" -" -h --help display this help text\n" -" -m --mktable output a \"defkeymap.c\" to stdout\n" -" -q --quiet suppress all normal output\n" -" -s --clearstrings clear kernel string table\n" -" -u --unicode force conversion to Unicode\n" -" -v --verbose report the changes\n"), PACKAGE_VERSION, DEFMAP); + "\n" + "Usage: loadkeys [option...] [mapfile...]\n" + "\n" + "Valid options are:\n" + "\n" + " -a --ascii force conversion to ASCII\n" + " -b --bkeymap output a binary keymap to stdout\n" + " -c --clearcompose clear kernel compose table\n" + " -C --console=\n" + " the console device(s) to be used\n" + " -d --default load \"%s\"\n" + " -h --help display this help text\n" + " -m --mktable output a \"defkeymap.c\" to stdout\n" + " -q --quiet suppress all normal output\n" + " -s --clearstrings clear kernel string table\n" + " -u --unicode force conversion to Unicode\n" + " -v --verbose report the changes\n"), + PACKAGE_VERSION, DEFMAP); exit(1); } -char *dirpath[] = { "", DATADIR "/" KEYMAPDIR "/**", KERNDIR "/", 0 }; +char *dirpath[] = { "", DATADIR "/" KEYMAPDIR "/**", KERNDIR "/", 0 }; char *suffixes[] = { "", ".kmap", ".map", 0 }; char **args; @@ -2104,22 +2117,22 @@ int verbose = 0; int quiet = 0; int nocompose = 0; -int -main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ const char *short_opts = "abcC:dhmsuqvV"; const struct option long_opts[] = { - { "ascii", no_argument, NULL, 'a' }, - { "bkeymap", no_argument, NULL, 'b' }, - { "clearcompose", no_argument, NULL, 'c' }, - { "console", 1, NULL, 'C' }, - { "default", no_argument, NULL, 'd' }, - { "help", no_argument, NULL, 'h' }, - { "mktable", no_argument, NULL, 'm' }, - { "clearstrings", no_argument, NULL, 's' }, - { "unicode", no_argument, NULL, 'u' }, - { "quiet", no_argument, NULL, 'q' }, - { "verbose", no_argument, NULL, 'v' }, - { "version", no_argument, NULL, 'V' }, + { "console", required_argument, NULL, 'C'}, + { "ascii", no_argument, NULL, 'a' }, + { "bkeymap", no_argument, NULL, 'b' }, + { "clearcompose", no_argument, NULL, 'c' }, + { "default", no_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, + { "mktable", no_argument, NULL, 'm' }, + { "clearstrings", no_argument, NULL, 's' }, + { "unicode", no_argument, NULL, 'u' }, + { "quiet", no_argument, NULL, 'q' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; int c, i; @@ -2134,80 +2147,83 @@ main(int argc, char *argv[]) { bindtextdomain(PACKAGE_NAME, LOCALEDIR); textdomain(PACKAGE_NAME); - while ((c = getopt_long(argc, argv, - short_opts, long_opts, NULL)) != -1) { + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { - case 'a': - opta = 1; - break; - case 'b': - optb = 1; - break; - case 'c': - nocompose = 1; - break; - case 'C': - console = optarg; - break; - case 'd': - optd = 1; - break; - case 'm': - optm = 1; - break; - case 's': - opts = 1; - break; - case 'u': - optu = 1; - break; - case 'q': - quiet = 1; - break; - case 'v': - verbose++; - break; - case 'V': - print_version_and_exit(); - case 'h': - case '?': - usage(); + case 'a': + opta = 1; + break; + case 'b': + optb = 1; + break; + case 'c': + nocompose = 1; + break; + case 'C': + console = optarg; + break; + case 'd': + optd = 1; + break; + case 'm': + optm = 1; + break; + case 's': + opts = 1; + break; + case 'u': + optu = 1; + break; + case 'q': + quiet = 1; + break; + case 'v': + verbose++; + break; + case 'V': + print_version_and_exit(); + case 'h': + case '?': + usage(); } } if (optu && opta) { - fprintf(stderr, _("%s: Options --unicode and --ascii are mutually exclusive\n"), - progname); + fprintf(stderr, + _("%s: Options --unicode and --ascii are mutually exclusive\n"), + progname); exit(1); } prefer_unicode = optu; + if (!optm && !optb) { /* check whether the keyboard is in Unicode mode */ fd = getfd(NULL); + if (ioctl(fd, KDGKBMODE, &kbd_mode)) { perror("KDGKBMODE"); - fprintf(stderr, _("%s: error reading keyboard mode\n"), progname); + fprintf(stderr, _("%s: error reading keyboard mode\n"), + progname); exit(1); } if (kbd_mode == K_UNICODE) { if (opta) { fprintf(stderr, - _("%s: warning: loading non-Unicode keymap on Unicode console\n" + _("%s: warning: loading non-Unicode keymap on Unicode console\n" " (perhaps you want to do `kbd_mode -a'?)\n"), - progname); - } - else { + progname); + } else { prefer_unicode = 1; } /* reset -u option if keyboard is in K_UNICODE anyway */ optu = 0; - } - else if (optu && (ioctl(fd, KDGETMODE, &kd_mode) || (kd_mode != KD_GRAPHICS))) - fprintf(stderr, _("%s: warning: loading Unicode keymap on non-Unicode console\n" - " (perhaps you want to do `kbd_mode -u'?)\n"), + } else if (optu + && (ioctl(fd, KDGETMODE, &kd_mode) || (kd_mode != KD_GRAPHICS))) + fprintf(stderr, + _("%s: warning: loading Unicode keymap on non-Unicode console\n" + " (perhaps you want to do `kbd_mode -u'?)\n"), progname); close(fd); @@ -2217,11 +2233,11 @@ main(int argc, char *argv[]) { FILE *f; if (optd) { - /* first read default map - search starts in . */ - optd = 0; - if((f = findfile(DEFMAP, dirpath, suffixes)) == NULL) { - fprintf(stderr, _("Cannot find %s\n"), DEFMAP); - exit(1); + /* first read default map - search starts in . */ + optd = 0; + if ((f = findfile(DEFMAP, dirpath, suffixes)) == NULL) { + fprintf(stderr, _("Cannot find %s\n"), DEFMAP); + exit(1); } goto gotf; } @@ -2235,7 +2251,7 @@ main(int argc, char *argv[]) { exit(1); } -gotf: + gotf: if (!quiet && !optm) fprintf(stdout, _("Loading %s\n"), pathname); @@ -2245,50 +2261,59 @@ gotf: fprintf(stderr, _("syntax error in map file\n")); if (!optm) - fprintf(stderr, _("key bindings not changed\n")); + fprintf(stderr, + _("key bindings not changed\n")); exit(1); } } - //exit(0); - do_constant(); - if(optb) { + + if (optb) { bkeymap(key_map); - } else if(optm) { - mktable(key_map, func_table); - } else if (console) - { - char *buf = strdup(console); /* make writable */ - char *e, *s = buf; - while (*s) - { - while ( *s == ' ' || *s == '\t' || *s == ',') s++; - e = s; - while (*e && *e != ' ' && *e != '\t' && *e != ',') e++; - char ch = *e; - *e = '\0'; - if (verbose) printf("%s\n", s); - loadkeys(s, kbd_mode); - *e = ch; - s = e; - } - free(buf); - } - else - loadkeys(NULL, kbd_mode); + + } else if (optm) { + mktable(key_map, func_table); + + } else if (console) { + char *buf = strdup(console); /* make writable */ + char *e, *s = buf; + while (*s) { + char ch; + + while (*s == ' ' || *s == '\t' || *s == ',') + s++; + e = s; + while (*e && *e != ' ' && *e != '\t' && *e != ',') + e++; + ch = *e; + *e = '\0'; + + if (verbose) + printf("%s\n", s); + + loadkeys(s, kbd_mode); + + *e = ch; + s = e; + } + free(buf); + + } else { + loadkeys(NULL, kbd_mode); + } exit(0); } -int -yyerror(const char *s) { +int yyerror(const char *s) +{ fprintf(stderr, "%s:%d: %s\n", filename, line_nr, s); private_error_ct++; - return(0); + return (0); } -void attr_noreturn attr_format_1_2 -lkfatal(const char *fmt, ...) { +void attr_noreturn attr_format_1_2 lkfatal(const char *fmt, ...) +{ va_list ap; va_start(ap, fmt); fprintf(stderr, "%s: %s:%d: ", progname, filename, line_nr); @@ -2327,12 +2352,16 @@ lkfatal(const char *fmt, ...) { char *include_dirpath0[] = { "", 0 }; char *include_dirpath1[] = { "", "../include/", "../../include/", 0 }; char *include_dirpath2[] = { 0, 0, 0, 0 }; -char *include_dirpath3[] = { DATADIR "/" KEYMAPDIR "/include/", - DATADIR "/" KEYMAPDIR "/i386/include/", - DATADIR "/" KEYMAPDIR "/mac/include/", 0 }; +char *include_dirpath3[] = { + DATADIR "/" KEYMAPDIR "/include/", + DATADIR "/" KEYMAPDIR "/i386/include/", + DATADIR "/" KEYMAPDIR "/mac/include/", 0 +}; + char *include_suffixes[] = { "", ".inc", 0 }; -FILE *find_incl_file_near_fn(char *s, char *fn) { +FILE *find_incl_file_near_fn(char *s, char *fn) +{ FILE *f = NULL; char *t, *te, *t1, *t2; int len; @@ -2360,7 +2389,8 @@ FILE *find_incl_file_near_fn(char *s, char *fn) { return f; } -FILE *find_standard_incl_file(char *s) { +FILE *find_standard_incl_file(char *s) +{ FILE *f; f = findfile(s, include_dirpath1, include_suffixes); @@ -2374,38 +2404,42 @@ FILE *find_standard_incl_file(char *s) { n = readlink(filename, buf, sizeof(buf)); if (n > 0 && n < sizeof(buf)) { - buf[n] = 0; - if (buf[0] == '/') - f = find_incl_file_near_fn(s, buf); - else if (strlen(filename) + n < sizeof(path)) { - strcpy(path, filename); - path[sizeof(path)-1] = 0; - ptr = strrchr(path, '/'); - if (ptr) - ptr[1] = 0; - strcat(path, buf); - f = find_incl_file_near_fn(s, path); - } + buf[n] = 0; + if (buf[0] == '/') { + f = find_incl_file_near_fn(s, buf); + + } else if (strlen(filename) + n < sizeof(path)) { + strcpy(path, filename); + path[sizeof(path) - 1] = 0; + ptr = strrchr(path, '/'); + if (ptr) + ptr[1] = 0; + strcat(path, buf); + f = find_incl_file_near_fn(s, path); + } } } if (!f) - f = findfile(s, include_dirpath3, include_suffixes); + f = findfile(s, include_dirpath3, include_suffixes); return f; } -FILE *find_incl_file(char *s) { +FILE *find_incl_file(char *s) +{ FILE *f; char *ev; + if (!s || !*s) return NULL; + if (*s == '/') /* no path required */ return (findfile(s, include_dirpath0, include_suffixes)); - if((ev = getenv("LOADKEYS_INCLUDE_PATH")) != NULL) { + if ((ev = getenv("LOADKEYS_INCLUDE_PATH")) != NULL) { /* try user-specified path */ char *user_dir[2] = { 0, 0 }; - while(ev) { + while (ev) { char *t = strchr(ev, ':'); char sv = 0; if (t) { @@ -2428,8 +2462,8 @@ FILE *find_incl_file(char *s) { return find_standard_incl_file(s); } -void -open_include(char *s) { +void open_include(char *s) +{ FILE *fd; if (verbose) @@ -2444,133 +2478,147 @@ open_include(char *s) { stack_push(fd, ispipe, pathname); } -static void -addmap(int i, int explicit) { +static void addmap(int i, int explicit) +{ if (i < 0 || i >= MAX_NR_KEYMAPS) - lkfatal(_("addmap called with bad index %d"), i); + lkfatal(_("addmap called with bad index %d"), i); if (!defining[i]) { - if (keymaps_line_seen && !explicit) - lkfatal(_("adding map %d violates explicit keymaps line"), i); + if (keymaps_line_seen && !explicit) + lkfatal(_("adding map %d violates explicit keymaps line"), i); - defining[i] = 1; - if (max_keymap <= i) - max_keymap = i+1; + defining[i] = 1; + if (max_keymap <= i) + max_keymap = i + 1; } } /* unset a key */ -static void -killkey(int k_index, int k_table) { +static void killkey(int k_index, int k_table) +{ /* roughly: addkey(k_index, k_table, K_HOLE); */ - if (k_index < 0 || k_index >= NR_KEYS) - lkfatal(_("killkey called with bad index %d"), k_index); - if (k_table < 0 || k_table >= MAX_NR_KEYMAPS) - lkfatal(_("killkey called with bad table %d"), k_table); + if (k_index < 0 || k_index >= NR_KEYS) + lkfatal(_("killkey called with bad index %d"), k_index); + + if (k_table < 0 || k_table >= MAX_NR_KEYMAPS) + lkfatal(_("killkey called with bad table %d"), k_table); + if (key_map[k_table]) (key_map[k_table])[k_index] = K_HOLE; + if (keymap_was_set[k_table]) (keymap_was_set[k_table])[k_index] = 0; } -static void -addkey(int k_index, int k_table, int keycode) { +static void addkey(int k_index, int k_table, int keycode) +{ int i; if (keycode == CODE_FOR_UNKNOWN_KSYM) - /* is safer not to be silent in this case, - * it can be caused by coding errors as well. */ - lkfatal(_("addkey called with bad keycode %d"), keycode); - if (k_index < 0 || k_index >= NR_KEYS) - lkfatal(_("addkey called with bad index %d"), k_index); - if (k_table < 0 || k_table >= MAX_NR_KEYMAPS) - lkfatal(_("addkey called with bad table %d"), k_table); + /* is safer not to be silent in this case, + * it can be caused by coding errors as well. */ + lkfatal(_("addkey called with bad keycode %d"), keycode); + + if (k_index < 0 || k_index >= NR_KEYS) + lkfatal(_("addkey called with bad index %d"), k_index); + + if (k_table < 0 || k_table >= MAX_NR_KEYMAPS) + lkfatal(_("addkey called with bad table %d"), k_table); if (!defining[k_table]) addmap(k_table, 0); + if (!key_map[k_table]) { - key_map[k_table] = (u_short *)xmalloc(NR_KEYS * sizeof(u_short)); + key_map[k_table] = + (u_short *) xmalloc(NR_KEYS * sizeof(u_short)); for (i = 0; i < NR_KEYS; i++) - (key_map[k_table])[i] = K_HOLE; + (key_map[k_table])[i] = K_HOLE; } + if (!keymap_was_set[k_table]) { - keymap_was_set[k_table] = (char *) xmalloc(NR_KEYS); + keymap_was_set[k_table] = (char *)xmalloc(NR_KEYS); for (i = 0; i < NR_KEYS; i++) - (keymap_was_set[k_table])[i] = 0; + (keymap_was_set[k_table])[i] = 0; } - if (alt_is_meta && keycode == K_HOLE && (keymap_was_set[k_table])[k_index]) + if (alt_is_meta && keycode == K_HOLE + && (keymap_was_set[k_table])[k_index]) return; (key_map[k_table])[k_index] = keycode; (keymap_was_set[k_table])[k_index] = 1; if (alt_is_meta) { - int alttable = k_table | M_ALT; - int type = KTYP(keycode); - int val = KVAL(keycode); - if (alttable != k_table && defining[alttable] && - (!keymap_was_set[alttable] || - !(keymap_was_set[alttable])[k_index]) && - (type == KT_LATIN || type == KT_LETTER) && val < 128) - addkey(k_index, alttable, K(KT_META, val)); + int alttable = k_table | M_ALT; + int type = KTYP(keycode); + int val = KVAL(keycode); + + if (alttable != k_table && defining[alttable] && + (!keymap_was_set[alttable] || + !(keymap_was_set[alttable])[k_index]) && + (type == KT_LATIN || type == KT_LETTER) && val < 128) + addkey(k_index, alttable, K(KT_META, val)); } } -static void -addfunc(struct kbsentry kbs) { +static void addfunc(struct kbsentry kbs) +{ int sh, i, x; char *ptr, *q, *r; x = kbs.kb_func; - if (x >= MAX_NR_FUNC) { - fprintf(stderr, _("%s: addfunc called with bad func %d\n"), + if (x >= MAX_NR_FUNC) { + fprintf(stderr, _("%s: addfunc called with bad func %d\n"), progname, kbs.kb_func); exit(1); } q = func_table[x]; - if (q) { /* throw out old previous def */ - sh = strlen(q) + 1; + if (q) { /* throw out old previous def */ + sh = strlen(q) + 1; ptr = q + sh; while (ptr < fp) - *q++ = *ptr++; + *q++ = *ptr++; fp -= sh; - for (i = x + 1; i < MAX_NR_FUNC; i++) - if (func_table[i]) - func_table[i] -= sh; + for (i = x + 1; i < MAX_NR_FUNC; i++) { + if (func_table[i]) + func_table[i] -= sh; + } } - ptr = func_buf; /* find place for new def */ - for (i = 0; i < x; i++) - if (func_table[i]) { - ptr = func_table[i]; - while(*ptr++); + ptr = func_buf; /* find place for new def */ + for (i = 0; i < x; i++) { + if (func_table[i]) { + ptr = func_table[i]; + while (*ptr++) ; } + } + func_table[x] = ptr; - sh = strlen((char *) kbs.kb_string) + 1; + sh = strlen((char *)kbs.kb_string) + 1; + if (fp + sh > func_buf + sizeof(func_buf)) { - fprintf(stderr, - _("%s: addfunc: func_buf overflow\n"), progname); + fprintf(stderr, _("%s: addfunc: func_buf overflow\n"), progname); exit(1); } q = fp; fp += sh; r = fp; while (q > ptr) - *--r = *--q; - strcpy(ptr, (char *) kbs.kb_string); - for (i = x + 1; i < MAX_NR_FUNC; i++) - if (func_table[i]) - func_table[i] += sh; + *--r = *--q; + strcpy(ptr, (char *)kbs.kb_string); + for (i = x + 1; i < MAX_NR_FUNC; i++) { + if (func_table[i]) + func_table[i] += sh; + } } -static void -compose(int diacr, int base, int res) { - accent_entry *ptr; +static void compose(int diacr, int base, int res) +{ + accent_entry *ptr; int direction; #ifdef KDSKBDIACRUC @@ -2580,21 +2628,22 @@ compose(int diacr, int base, int res) { #endif direction = TO_8BIT; - if (accent_table_size == MAX_DIACR) { - fprintf(stderr, _("compose table overflow\n")); + if (accent_table_size == MAX_DIACR) { + fprintf(stderr, _("compose table overflow\n")); exit(1); } + ptr = &accent_table[accent_table_size++]; ptr->diacr = convert_code(diacr, direction); ptr->base = convert_code(base, direction); ptr->result = convert_code(res, direction); } -static int -defkeys(int fd, int kbd_mode) { +static int defkeys(int fd, int kbd_mode) +{ struct kbentry ke; int ct = 0; - int i,j,fail; + int i, j, fail; if (optu) { /* temporarily switch to K_UNICODE while defining keys */ @@ -2605,76 +2654,85 @@ defkeys(int fd, int kbd_mode) { } } - for(i=0; i 1) - printf(_("deallocate keymap %d\n"), i); - - if(ioctl(fd, KDSKBENT, (unsigned long)&ke)) { - if (errno != EINVAL) { - perror("KDSKBENT"); - fprintf(stderr, - _("%s: could not deallocate keymap %d\n"), - progname, i); - exit(1); - } - /* probably an old kernel */ - /* clear keymap by hand */ - for (j = 0; j < NR_KEYS; j++) { - ke.kb_index = j; + } else if (keymaps_line_seen && !defining[i]) { + /* deallocate keymap */ + ke.kb_index = 0; ke.kb_table = i; - ke.kb_value = K_HOLE; - if(ioctl(fd, KDSKBENT, (unsigned long)&ke)) { - if (errno == EINVAL && i >= 16) - break; /* old kernel */ - perror("KDSKBENT"); - fprintf(stderr, - _("%s: cannot deallocate or clear keymap\n"), - progname); - exit(1); + ke.kb_value = K_NOSUCHMAP; + + if (verbose > 1) + printf(_("deallocate keymap %d\n"), i); + + if (ioctl(fd, KDSKBENT, (unsigned long)&ke)) { + if (errno != EINVAL) { + perror("KDSKBENT"); + fprintf(stderr, + _("%s: could not deallocate keymap %d\n"), + progname, i); + exit(1); + } + /* probably an old kernel */ + /* clear keymap by hand */ + for (j = 0; j < NR_KEYS; j++) { + ke.kb_index = j; + ke.kb_table = i; + ke.kb_value = K_HOLE; + if (ioctl + (fd, KDSKBENT, + (unsigned long)&ke)) { + if (errno == EINVAL && i >= 16) + break; /* old kernel */ + perror("KDSKBENT"); + fprintf(stderr, + _("%s: cannot deallocate or clear keymap\n"), + progname); + exit(1); + } + } } - } } - } } if (optu && ioctl(fd, KDSKBMODE, kbd_mode)) { perror("KDSKBMODE"); - fprintf(stderr, _("%s: could not return to original keyboard mode\n"), + fprintf(stderr, + _("%s: could not return to original keyboard mode\n"), progname); exit(1); } @@ -2682,61 +2740,65 @@ defkeys(int fd, int kbd_mode) { return ct; } -static char * -ostr(char *s) { +static char *ostr(char *s) +{ int lth = strlen(s); - char *ns0 = xmalloc(4*lth + 1); + char *ns0 = xmalloc(4 * lth + 1); char *ns = ns0; - while(*s) { - switch(*s) { - case '\n': - *ns++ = '\\'; - *ns++ = 'n'; - break; - case '\033': - *ns++ = '\\'; - *ns++ = '0'; - *ns++ = '3'; - *ns++ = '3'; - break; - default: - *ns++ = *s; - } - s++; + while (*s) { + switch (*s) { + case '\n': + *ns++ = '\\'; + *ns++ = 'n'; + break; + case '\033': + *ns++ = '\\'; + *ns++ = '0'; + *ns++ = '3'; + *ns++ = '3'; + break; + default: + *ns++ = *s; + } + s++; } *ns = 0; return ns0; } -static int -deffuncs(int fd){ - int i, ct = 0; +static int deffuncs(int fd) +{ + int i, ct = 0; char *ptr; - for (i = 0; i < MAX_NR_FUNC; i++) { - kbs_buf.kb_func = i; - if ((ptr = func_table[i])) { - strcpy((char *) kbs_buf.kb_string, ptr); - if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) - fprintf(stderr, _("failed to bind string '%s' to function %s\n"), - ostr((char *) kbs_buf.kb_string), syms[KT_FN].table[kbs_buf.kb_func]); - else - ct++; - } else if (opts) { - kbs_buf.kb_string[0] = 0; - if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) - fprintf(stderr, _("failed to clear string %s\n"), - syms[KT_FN].table[kbs_buf.kb_func]); - else - ct++; - } - } + for (i = 0; i < MAX_NR_FUNC; i++) { + kbs_buf.kb_func = i; + + if ((ptr = func_table[i])) { + strcpy((char *)kbs_buf.kb_string, ptr); + if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) + fprintf(stderr, + _("failed to bind string '%s' to function %s\n"), + ostr((char *)kbs_buf.kb_string), + syms[KT_FN].table[kbs_buf.kb_func]); + else + ct++; + } else if (opts) { + kbs_buf.kb_string[0] = 0; + if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) + fprintf(stderr, + _("failed to clear string %s\n"), + syms[KT_FN].table[kbs_buf.kb_func]); + else + ct++; + } + } return ct; } -static int -defdiacs(int fd){ +static int defdiacs(int fd) +{ unsigned int i, count; struct kbdiacrs kd; #ifdef KDSKBDIACRUC @@ -2745,154 +2807,174 @@ defdiacs(int fd){ count = accent_table_size; if (count > MAX_DIACR) { - count = MAX_DIACR; - fprintf(stderr, _("too many compose definitions\n")); + count = MAX_DIACR; + fprintf(stderr, _("too many compose definitions\n")); } - #ifdef KDSKBDIACRUC if (prefer_unicode) { kdu.kb_cnt = count; + for (i = 0; i < kdu.kb_cnt; i++) { - kdu.kbdiacruc[i].diacr = accent_table[i].diacr; - kdu.kbdiacruc[i].base = accent_table[i].base; - kdu.kbdiacruc[i].result = accent_table[i].result; + kdu.kbdiacruc[i].diacr = accent_table[i].diacr; + kdu.kbdiacruc[i].base = accent_table[i].base; + kdu.kbdiacruc[i].result = accent_table[i].result; } - if(ioctl(fd, KDSKBDIACRUC, (unsigned long) &kdu)) { - perror("KDSKBDIACRUC"); - exit(1); + + if (ioctl(fd, KDSKBDIACRUC, (unsigned long)&kdu)) { + perror("KDSKBDIACRUC"); + exit(1); } - } - else + } else #endif { kd.kb_cnt = count; for (i = 0; i < kd.kb_cnt; i++) { - kd.kbdiacr[i].diacr = accent_table[i].diacr; - kd.kbdiacr[i].base = accent_table[i].base; - kd.kbdiacr[i].result = accent_table[i].result; + kd.kbdiacr[i].diacr = accent_table[i].diacr; + kd.kbdiacr[i].base = accent_table[i].base; + kd.kbdiacr[i].result = accent_table[i].result; } - if(ioctl(fd, KDSKBDIACR, (unsigned long) &kd)) { - perror("KDSKBDIACR"); - exit(1); + + if (ioctl(fd, KDSKBDIACR, (unsigned long)&kd)) { + perror("KDSKBDIACR"); + exit(1); } } return kd.kb_cnt; } -void -do_constant_key (int i, u_short key) { +void do_constant_key(int i, u_short key) +{ int typ, val, j; typ = KTYP(key); val = KVAL(key); + if ((typ == KT_LATIN || typ == KT_LETTER) && - ((val >= 'a' && val <= 'z') || - (val >= 'A' && val <= 'Z'))) { + ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'))) { u_short defs[16]; defs[0] = K(KT_LETTER, val); defs[1] = K(KT_LETTER, val ^ 32); defs[2] = defs[0]; defs[3] = defs[1]; - for(j=4; j<8; j++) + + for (j = 4; j < 8; j++) defs[j] = K(KT_LATIN, val & ~96); - for(j=8; j<16; j++) - defs[j] = K(KT_META, KVAL(defs[j-8])); - for(j=0; j 0 && keymap_was_set[j] && (keymap_was_set[j])[i]) continue; - addkey(i, j, defs[j%16]); + + addkey(i, j, defs[j % 16]); } + } else { /* do this also for keys like Escape, as promised in the man page */ - for (j=1; j 0 || nocompose) { - diacct = defdiacs(fd); - if (verbose) { - printf(_("Loaded %d compose %s.\n"), diacct, - (diacct == 1) ? _("definition") : _("definitions")); - } + diacct = defdiacs(fd); + + if (verbose) { + printf(_("Loaded %d compose %s.\n"), + diacct, (diacct == 1) ? _("definition") : _("definitions")); + } + + } else if (verbose) { + printf(_("(No change in compose definitions.)\n")); } - else - if (verbose) - printf(_("(No change in compose definitions.)\n")); } - -static void strings_as_usual(void) { +static void strings_as_usual(void) +{ /* * 26 strings, mostly inspired by the VT100 family */ char *stringvalues[30] = { /* F1 .. F20 */ - "\033[[A", "\033[[B", "\033[[C", "\033[[D", "\033[[E", + "\033[[A", "\033[[B", "\033[[C", "\033[[D", "\033[[E", "\033[17~", "\033[18~", "\033[19~", "\033[20~", "\033[21~", "\033[23~", "\033[24~", "\033[25~", "\033[26~", "\033[28~", "\033[29~", "\033[31~", "\033[32~", "\033[33~", "\033[34~", - /* Find, Insert, Remove, Select, Prior */ - "\033[1~", "\033[2~", "\033[3~", "\033[4~", "\033[5~", - /* Next, Macro, Help, Do, Pause */ - "\033[6~", 0, 0, 0, 0 + /* Find, Insert, Remove, Select, Prior */ + "\033[1~", "\033[2~", "\033[3~", "\033[4~", "\033[5~", + /* Next, Macro, Help, Do, Pause */ + "\033[6~", 0, 0, 0, 0 }; int i; - for (i=0; i<30; i++) if(stringvalues[i]) { - struct kbsentry ke; - ke.kb_func = i; - strncpy((char *) ke.kb_string, stringvalues[i], sizeof(ke.kb_string)); - ke.kb_string[sizeof(ke.kb_string)-1] = 0; - addfunc(ke); + + for (i = 0; i < 30; i++) { + if (stringvalues[i]) { + struct kbsentry ke; + ke.kb_func = i; + strncpy((char *)ke.kb_string, stringvalues[i], + sizeof(ke.kb_string)); + ke.kb_string[sizeof(ke.kb_string) - 1] = 0; + addfunc(ke); + } } } -static void -compose_as_usual(char *charset) { +static void compose_as_usual(char *charset) +{ if (charset && strcmp(charset, "iso-8859-1")) { - fprintf(stderr, _("loadkeys: don't know how to compose for %s\n"), + fprintf(stderr, + _("loadkeys: don't know how to compose for %s\n"), charset); exit(1); + } else { struct ccc { unsigned char c1, c2, c3; @@ -2933,11 +3015,10 @@ compose_as_usual(char *charset) { { 's', 'z', 0337 }, { 'i', 'j', 0377 } }; int i; - for(i=0; i<68; i++) { + for (i = 0; i < 68; i++) { struct ccc ptr = def_latin1_composes[i]; compose(ptr.c1, ptr.c2, ptr.c3); } } } - diff --git a/src/loadkeys.y b/src/loadkeys.y index b8940b1..555309a 100644 --- a/src/loadkeys.y +++ b/src/loadkeys.y @@ -45,7 +45,7 @@ /* What keymaps are we defining? */ char defining[MAX_NR_KEYMAPS]; char keymaps_line_seen = 0; -int max_keymap = 0; /* from here on, defining[] is false */ +int max_keymap = 0; /* from here on, defining[] is false */ int alt_is_meta = 0; /* the kernel structures we want to set or print */ @@ -57,7 +57,7 @@ unsigned int accent_table_size = 0; char key_is_constant[NR_KEYS]; char *keymap_was_set[MAX_NR_KEYMAPS]; -char func_buf[4096]; /* should be allocated dynamically */ +char func_buf[4096]; /* should be allocated dynamically */ char *fp = func_buf; #undef ECHO @@ -68,7 +68,7 @@ static void addfunc(struct kbsentry kbs_buf); static void killkey(int index, int table); static void compose(int diacr, int base, int res); static void do_constant(void); -static void do_constant_key (int, u_short); +static void do_constant_key(int, u_short); static void loadkeys(char *console, int kbd_mode); static void strings_as_usual(void); static void compose_as_usual(char *charset); @@ -84,6 +84,7 @@ int private_error_ct = 0; extern int rvalct; extern struct kbsentry kbs_buf; + int yyerror(const char *s); FILE *find_incl_file_near_fn(char *s, char *fn); @@ -97,9 +98,9 @@ extern int line_nr; extern void stack_push(FILE *fd, int ispipe, char *filename); extern int stack_pop(void); - #include "ksyms.h" int yylex(void); + %} %% @@ -119,31 +120,31 @@ line : EOL ; charsetline : CHARSET STRLITERAL EOL { - set_charset((char *) kbs_buf.kb_string); + set_charset((char *) kbs_buf.kb_string); } ; altismetaline : ALT_IS_META EOL { - alt_is_meta = 1; + alt_is_meta = 1; } ; usualstringsline: STRINGS AS USUAL EOL { - strings_as_usual(); + strings_as_usual(); } ; usualcomposeline: COMPOSE AS USUAL FOR STRLITERAL EOL { - compose_as_usual((char *) kbs_buf.kb_string); + compose_as_usual((char *) kbs_buf.kb_string); } | COMPOSE AS USUAL EOL { - compose_as_usual(0); + compose_as_usual(0); } ; keymapline : KEYMAPS range EOL { - keymaps_line_seen = 1; + keymaps_line_seen = 1; } ; range : range COMMA range0 @@ -151,46 +152,46 @@ range : range COMMA range0 ; range0 : NUMBER DASH NUMBER { - int i; - for (i = $1; i<= $3; i++) - addmap(i,1); + int i; + for (i = $1; i <= $3; i++) + addmap(i,1); } | NUMBER { - addmap($1,1); + addmap($1,1); } ; strline : STRING LITERAL EQUALS STRLITERAL EOL { - if (KTYP($2) != KT_FN) - lkfatal(_("'%s' is not a function key symbol"), - syms[KTYP($2)].table[KVAL($2)]); - kbs_buf.kb_func = KVAL($2); - addfunc(kbs_buf); + if (KTYP($2) != KT_FN) + lkfatal(_("'%s' is not a function key symbol"), + syms[KTYP($2)].table[KVAL($2)]); + kbs_buf.kb_func = KVAL($2); + addfunc(kbs_buf); } ; compline : COMPOSE compsym compsym TO compsym EOL { - compose($2, $3, $5); + compose($2, $3, $5); } | COMPOSE compsym compsym TO rvalue EOL { - compose($2, $3, $5); + compose($2, $3, $5); } ; -compsym : CCHAR - { $$ = $1; } - | UNUMBER - { $$ = $1 ^ 0xf000; } +compsym : CCHAR { $$ = $1; } + | UNUMBER { $$ = $1 ^ 0xf000; } ; -singleline : { mod = 0; } +singleline : { + mod = 0; + } modifiers KEYCODE NUMBER EQUALS rvalue EOL { - addkey($4, mod, $6); + addkey($4, mod, $6); } | PLAIN KEYCODE NUMBER EQUALS rvalue EOL { - addkey($3, 0, $5); + addkey($3, 0, $5); } ; modifiers : modifiers modifier @@ -207,36 +208,42 @@ modifier : SHIFT { mod |= M_SHIFT; } | CAPSSHIFT { mod |= M_CAPSSHIFT; } ; fullline : KEYCODE NUMBER EQUALS rvalue0 EOL - { - int i, j; - - if (rvalct == 1) { - /* Some files do not have a keymaps line, and - we have to wait until all input has been read - before we know which maps to fill. */ - key_is_constant[$2] = 1; - - /* On the other hand, we now have include files, - and it should be possible to override lines - from an include file. So, kill old defs. */ - for (j = 0; j < max_keymap; j++) - if (defining[j]) - killkey($2, j); - } - if (keymaps_line_seen) { - i = 0; - for (j = 0; j < max_keymap; j++) - if (defining[j]) { - if (rvalct != 1 || i == 0) - addkey($2, j, (i < rvalct) ? key_buf[i] : K_HOLE); - i++; - } - if (i < rvalct) - lkfatal(_("too many (%d) entries on one line"), rvalct); - } else - for (i = 0; i < rvalct; i++) - addkey($2, i, key_buf[i]); - } + { + int i, j; + + if (rvalct == 1) { + /* Some files do not have a keymaps line, and + * we have to wait until all input has been read + * before we know which maps to fill. */ + key_is_constant[$2] = 1; + + /* On the other hand, we now have include files, + * and it should be possible to override lines + * from an include file. So, kill old defs. */ + for (j = 0; j < max_keymap; j++) { + if (defining[j]) + killkey($2, j); + } + } + + if (keymaps_line_seen) { + i = 0; + + for (j = 0; j < max_keymap; j++) { + if (defining[j]) { + if (rvalct != 1 || i == 0) + addkey($2, j, (i < rvalct) ? key_buf[i] : K_HOLE); + i++; + } + } + + if (i < rvalct) + lkfatal(_("too many (%d) entries on one line"), rvalct); + } else { + for (i = 0; i < rvalct; i++) + addkey($2, i, key_buf[i]); + } + } ; rvalue0 : @@ -244,50 +251,45 @@ rvalue0 : ; rvalue1 : rvalue { - if (rvalct >= MAX_NR_KEYMAPS) - lkfatal(_("too many key definitions on one line")); - key_buf[rvalct++] = $1; + if (rvalct >= MAX_NR_KEYMAPS) + lkfatal(_("too many key definitions on one line")); + key_buf[rvalct++] = $1; } ; -rvalue : NUMBER - {$$=convert_code($1, TO_AUTO);} - | PLUS NUMBER - {$$=add_capslock($2);} - | UNUMBER - {$$=convert_code($1^0xf000, TO_AUTO);} - | PLUS UNUMBER - {$$=add_capslock($2^0xf000);} - | LITERAL - {$$=$1;} - | PLUS LITERAL - {$$=add_capslock($2);} +rvalue : NUMBER { $$ = convert_code($1, TO_AUTO); } + | PLUS NUMBER { $$ = add_capslock($2); } + | UNUMBER { $$ = convert_code($1^0xf000, TO_AUTO); } + | PLUS UNUMBER { $$ = add_capslock($2^0xf000); } + | LITERAL { $$ = $1; } + | PLUS LITERAL { $$ = add_capslock($2); } ; %% -static void attr_noreturn -usage(void) { +static void attr_noreturn usage(void) +{ fprintf(stderr, _("loadkeys version %s\n" -"\n" -"Usage: loadkeys [option...] [mapfile...]\n" -"\n" -"Valid options are:\n" -"\n" -" -a --ascii force conversion to ASCII\n" -" -b --bkeymap output a binary keymap to stdout\n" -" -c --clearcompose clear kernel compose table\n" -" -C --console=\n" -" the console device(s) to be used\n" -" -d --default load \"%s\"\n" -" -h --help display this help text\n" -" -m --mktable output a \"defkeymap.c\" to stdout\n" -" -q --quiet suppress all normal output\n" -" -s --clearstrings clear kernel string table\n" -" -u --unicode force conversion to Unicode\n" -" -v --verbose report the changes\n"), PACKAGE_VERSION, DEFMAP); + "\n" + "Usage: loadkeys [option...] [mapfile...]\n" + "\n" + "Valid options are:\n" + "\n" + " -a --ascii force conversion to ASCII\n" + " -b --bkeymap output a binary keymap to stdout\n" + " -c --clearcompose clear kernel compose table\n" + " -C --console=\n" + " the console device(s) to be used\n" + " -d --default load \"%s\"\n" + " -h --help display this help text\n" + " -m --mktable output a \"defkeymap.c\" to stdout\n" + " -q --quiet suppress all normal output\n" + " -s --clearstrings clear kernel string table\n" + " -u --unicode force conversion to Unicode\n" + " -v --verbose report the changes\n"), + PACKAGE_VERSION, DEFMAP); exit(1); } -char *dirpath[] = { "", DATADIR "/" KEYMAPDIR "/**", KERNDIR "/", 0 }; +char *dirpath[] = { "", DATADIR "/" KEYMAPDIR "/**", KERNDIR "/", 0 }; char *suffixes[] = { "", ".kmap", ".map", 0 }; char **args; @@ -301,22 +303,22 @@ int verbose = 0; int quiet = 0; int nocompose = 0; -int -main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ const char *short_opts = "abcC:dhmsuqvV"; const struct option long_opts[] = { - { "ascii", no_argument, NULL, 'a' }, - { "bkeymap", no_argument, NULL, 'b' }, - { "clearcompose", no_argument, NULL, 'c' }, - { "console", 1, NULL, 'C' }, - { "default", no_argument, NULL, 'd' }, - { "help", no_argument, NULL, 'h' }, - { "mktable", no_argument, NULL, 'm' }, - { "clearstrings", no_argument, NULL, 's' }, - { "unicode", no_argument, NULL, 'u' }, - { "quiet", no_argument, NULL, 'q' }, - { "verbose", no_argument, NULL, 'v' }, - { "version", no_argument, NULL, 'V' }, + { "console", required_argument, NULL, 'C'}, + { "ascii", no_argument, NULL, 'a' }, + { "bkeymap", no_argument, NULL, 'b' }, + { "clearcompose", no_argument, NULL, 'c' }, + { "default", no_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, + { "mktable", no_argument, NULL, 'm' }, + { "clearstrings", no_argument, NULL, 's' }, + { "unicode", no_argument, NULL, 'u' }, + { "quiet", no_argument, NULL, 'q' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; int c, i; @@ -331,80 +333,83 @@ main(int argc, char *argv[]) { bindtextdomain(PACKAGE_NAME, LOCALEDIR); textdomain(PACKAGE_NAME); - while ((c = getopt_long(argc, argv, - short_opts, long_opts, NULL)) != -1) { + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { - case 'a': - opta = 1; - break; - case 'b': - optb = 1; - break; - case 'c': - nocompose = 1; - break; - case 'C': - console = optarg; - break; - case 'd': - optd = 1; - break; - case 'm': - optm = 1; - break; - case 's': - opts = 1; - break; - case 'u': - optu = 1; - break; - case 'q': - quiet = 1; - break; - case 'v': - verbose++; - break; - case 'V': - print_version_and_exit(); - case 'h': - case '?': - usage(); + case 'a': + opta = 1; + break; + case 'b': + optb = 1; + break; + case 'c': + nocompose = 1; + break; + case 'C': + console = optarg; + break; + case 'd': + optd = 1; + break; + case 'm': + optm = 1; + break; + case 's': + opts = 1; + break; + case 'u': + optu = 1; + break; + case 'q': + quiet = 1; + break; + case 'v': + verbose++; + break; + case 'V': + print_version_and_exit(); + case 'h': + case '?': + usage(); } } if (optu && opta) { - fprintf(stderr, _("%s: Options --unicode and --ascii are mutually exclusive\n"), - progname); + fprintf(stderr, + _("%s: Options --unicode and --ascii are mutually exclusive\n"), + progname); exit(1); } prefer_unicode = optu; + if (!optm && !optb) { /* check whether the keyboard is in Unicode mode */ fd = getfd(NULL); + if (ioctl(fd, KDGKBMODE, &kbd_mode)) { perror("KDGKBMODE"); - fprintf(stderr, _("%s: error reading keyboard mode\n"), progname); + fprintf(stderr, _("%s: error reading keyboard mode\n"), + progname); exit(1); } if (kbd_mode == K_UNICODE) { if (opta) { fprintf(stderr, - _("%s: warning: loading non-Unicode keymap on Unicode console\n" + _("%s: warning: loading non-Unicode keymap on Unicode console\n" " (perhaps you want to do `kbd_mode -a'?)\n"), - progname); - } - else { + progname); + } else { prefer_unicode = 1; } /* reset -u option if keyboard is in K_UNICODE anyway */ optu = 0; - } - else if (optu && (ioctl(fd, KDGETMODE, &kd_mode) || (kd_mode != KD_GRAPHICS))) - fprintf(stderr, _("%s: warning: loading Unicode keymap on non-Unicode console\n" - " (perhaps you want to do `kbd_mode -u'?)\n"), + } else if (optu + && (ioctl(fd, KDGETMODE, &kd_mode) || (kd_mode != KD_GRAPHICS))) + fprintf(stderr, + _("%s: warning: loading Unicode keymap on non-Unicode console\n" + " (perhaps you want to do `kbd_mode -u'?)\n"), progname); close(fd); @@ -414,11 +419,11 @@ main(int argc, char *argv[]) { FILE *f; if (optd) { - /* first read default map - search starts in . */ - optd = 0; - if((f = findfile(DEFMAP, dirpath, suffixes)) == NULL) { - fprintf(stderr, _("Cannot find %s\n"), DEFMAP); - exit(1); + /* first read default map - search starts in . */ + optd = 0; + if ((f = findfile(DEFMAP, dirpath, suffixes)) == NULL) { + fprintf(stderr, _("Cannot find %s\n"), DEFMAP); + exit(1); } goto gotf; } @@ -432,7 +437,7 @@ main(int argc, char *argv[]) { exit(1); } -gotf: + gotf: if (!quiet && !optm) fprintf(stdout, _("Loading %s\n"), pathname); @@ -442,50 +447,59 @@ gotf: fprintf(stderr, _("syntax error in map file\n")); if (!optm) - fprintf(stderr, _("key bindings not changed\n")); + fprintf(stderr, + _("key bindings not changed\n")); exit(1); } } - //exit(0); - do_constant(); - if(optb) { + + if (optb) { bkeymap(key_map); - } else if(optm) { - mktable(key_map, func_table); - } else if (console) - { - char *buf = strdup(console); /* make writable */ - char *e, *s = buf; - while (*s) - { - while ( *s == ' ' || *s == '\t' || *s == ',') s++; - e = s; - while (*e && *e != ' ' && *e != '\t' && *e != ',') e++; - char ch = *e; - *e = '\0'; - if (verbose) printf("%s\n", s); - loadkeys(s, kbd_mode); - *e = ch; - s = e; - } - free(buf); - } - else - loadkeys(NULL, kbd_mode); + + } else if (optm) { + mktable(key_map, func_table); + + } else if (console) { + char *buf = strdup(console); /* make writable */ + char *e, *s = buf; + while (*s) { + char ch; + + while (*s == ' ' || *s == '\t' || *s == ',') + s++; + e = s; + while (*e && *e != ' ' && *e != '\t' && *e != ',') + e++; + ch = *e; + *e = '\0'; + + if (verbose) + printf("%s\n", s); + + loadkeys(s, kbd_mode); + + *e = ch; + s = e; + } + free(buf); + + } else { + loadkeys(NULL, kbd_mode); + } exit(0); } -int -yyerror(const char *s) { +int yyerror(const char *s) +{ fprintf(stderr, "%s:%d: %s\n", filename, line_nr, s); private_error_ct++; - return(0); + return (0); } -void attr_noreturn attr_format_1_2 -lkfatal(const char *fmt, ...) { +void attr_noreturn attr_format_1_2 lkfatal(const char *fmt, ...) +{ va_list ap; va_start(ap, fmt); fprintf(stderr, "%s: %s:%d: ", progname, filename, line_nr); @@ -524,12 +538,16 @@ lkfatal(const char *fmt, ...) { char *include_dirpath0[] = { "", 0 }; char *include_dirpath1[] = { "", "../include/", "../../include/", 0 }; char *include_dirpath2[] = { 0, 0, 0, 0 }; -char *include_dirpath3[] = { DATADIR "/" KEYMAPDIR "/include/", - DATADIR "/" KEYMAPDIR "/i386/include/", - DATADIR "/" KEYMAPDIR "/mac/include/", 0 }; +char *include_dirpath3[] = { + DATADIR "/" KEYMAPDIR "/include/", + DATADIR "/" KEYMAPDIR "/i386/include/", + DATADIR "/" KEYMAPDIR "/mac/include/", 0 +}; + char *include_suffixes[] = { "", ".inc", 0 }; -FILE *find_incl_file_near_fn(char *s, char *fn) { +FILE *find_incl_file_near_fn(char *s, char *fn) +{ FILE *f = NULL; char *t, *te, *t1, *t2; int len; @@ -557,7 +575,8 @@ FILE *find_incl_file_near_fn(char *s, char *fn) { return f; } -FILE *find_standard_incl_file(char *s) { +FILE *find_standard_incl_file(char *s) +{ FILE *f; f = findfile(s, include_dirpath1, include_suffixes); @@ -571,38 +590,42 @@ FILE *find_standard_incl_file(char *s) { n = readlink(filename, buf, sizeof(buf)); if (n > 0 && n < sizeof(buf)) { - buf[n] = 0; - if (buf[0] == '/') - f = find_incl_file_near_fn(s, buf); - else if (strlen(filename) + n < sizeof(path)) { - strcpy(path, filename); - path[sizeof(path)-1] = 0; - ptr = strrchr(path, '/'); - if (ptr) - ptr[1] = 0; - strcat(path, buf); - f = find_incl_file_near_fn(s, path); - } + buf[n] = 0; + if (buf[0] == '/') { + f = find_incl_file_near_fn(s, buf); + + } else if (strlen(filename) + n < sizeof(path)) { + strcpy(path, filename); + path[sizeof(path) - 1] = 0; + ptr = strrchr(path, '/'); + if (ptr) + ptr[1] = 0; + strcat(path, buf); + f = find_incl_file_near_fn(s, path); + } } } if (!f) - f = findfile(s, include_dirpath3, include_suffixes); + f = findfile(s, include_dirpath3, include_suffixes); return f; } -FILE *find_incl_file(char *s) { +FILE *find_incl_file(char *s) +{ FILE *f; char *ev; + if (!s || !*s) return NULL; + if (*s == '/') /* no path required */ return (findfile(s, include_dirpath0, include_suffixes)); - if((ev = getenv("LOADKEYS_INCLUDE_PATH")) != NULL) { + if ((ev = getenv("LOADKEYS_INCLUDE_PATH")) != NULL) { /* try user-specified path */ char *user_dir[2] = { 0, 0 }; - while(ev) { + while (ev) { char *t = strchr(ev, ':'); char sv = 0; if (t) { @@ -625,8 +648,8 @@ FILE *find_incl_file(char *s) { return find_standard_incl_file(s); } -void -open_include(char *s) { +void open_include(char *s) +{ FILE *fd; if (verbose) @@ -641,133 +664,147 @@ open_include(char *s) { stack_push(fd, ispipe, pathname); } -static void -addmap(int i, int explicit) { +static void addmap(int i, int explicit) +{ if (i < 0 || i >= MAX_NR_KEYMAPS) - lkfatal(_("addmap called with bad index %d"), i); + lkfatal(_("addmap called with bad index %d"), i); if (!defining[i]) { - if (keymaps_line_seen && !explicit) - lkfatal(_("adding map %d violates explicit keymaps line"), i); + if (keymaps_line_seen && !explicit) + lkfatal(_("adding map %d violates explicit keymaps line"), i); - defining[i] = 1; - if (max_keymap <= i) - max_keymap = i+1; + defining[i] = 1; + if (max_keymap <= i) + max_keymap = i + 1; } } /* unset a key */ -static void -killkey(int k_index, int k_table) { +static void killkey(int k_index, int k_table) +{ /* roughly: addkey(k_index, k_table, K_HOLE); */ - if (k_index < 0 || k_index >= NR_KEYS) - lkfatal(_("killkey called with bad index %d"), k_index); - if (k_table < 0 || k_table >= MAX_NR_KEYMAPS) - lkfatal(_("killkey called with bad table %d"), k_table); + if (k_index < 0 || k_index >= NR_KEYS) + lkfatal(_("killkey called with bad index %d"), k_index); + + if (k_table < 0 || k_table >= MAX_NR_KEYMAPS) + lkfatal(_("killkey called with bad table %d"), k_table); + if (key_map[k_table]) (key_map[k_table])[k_index] = K_HOLE; + if (keymap_was_set[k_table]) (keymap_was_set[k_table])[k_index] = 0; } -static void -addkey(int k_index, int k_table, int keycode) { +static void addkey(int k_index, int k_table, int keycode) +{ int i; if (keycode == CODE_FOR_UNKNOWN_KSYM) - /* is safer not to be silent in this case, - * it can be caused by coding errors as well. */ - lkfatal(_("addkey called with bad keycode %d"), keycode); - if (k_index < 0 || k_index >= NR_KEYS) - lkfatal(_("addkey called with bad index %d"), k_index); - if (k_table < 0 || k_table >= MAX_NR_KEYMAPS) - lkfatal(_("addkey called with bad table %d"), k_table); + /* is safer not to be silent in this case, + * it can be caused by coding errors as well. */ + lkfatal(_("addkey called with bad keycode %d"), keycode); + + if (k_index < 0 || k_index >= NR_KEYS) + lkfatal(_("addkey called with bad index %d"), k_index); + + if (k_table < 0 || k_table >= MAX_NR_KEYMAPS) + lkfatal(_("addkey called with bad table %d"), k_table); if (!defining[k_table]) addmap(k_table, 0); + if (!key_map[k_table]) { - key_map[k_table] = (u_short *)xmalloc(NR_KEYS * sizeof(u_short)); + key_map[k_table] = + (u_short *) xmalloc(NR_KEYS * sizeof(u_short)); for (i = 0; i < NR_KEYS; i++) - (key_map[k_table])[i] = K_HOLE; + (key_map[k_table])[i] = K_HOLE; } + if (!keymap_was_set[k_table]) { - keymap_was_set[k_table] = (char *) xmalloc(NR_KEYS); + keymap_was_set[k_table] = (char *)xmalloc(NR_KEYS); for (i = 0; i < NR_KEYS; i++) - (keymap_was_set[k_table])[i] = 0; + (keymap_was_set[k_table])[i] = 0; } - if (alt_is_meta && keycode == K_HOLE && (keymap_was_set[k_table])[k_index]) + if (alt_is_meta && keycode == K_HOLE + && (keymap_was_set[k_table])[k_index]) return; (key_map[k_table])[k_index] = keycode; (keymap_was_set[k_table])[k_index] = 1; if (alt_is_meta) { - int alttable = k_table | M_ALT; - int type = KTYP(keycode); - int val = KVAL(keycode); - if (alttable != k_table && defining[alttable] && - (!keymap_was_set[alttable] || - !(keymap_was_set[alttable])[k_index]) && - (type == KT_LATIN || type == KT_LETTER) && val < 128) - addkey(k_index, alttable, K(KT_META, val)); + int alttable = k_table | M_ALT; + int type = KTYP(keycode); + int val = KVAL(keycode); + + if (alttable != k_table && defining[alttable] && + (!keymap_was_set[alttable] || + !(keymap_was_set[alttable])[k_index]) && + (type == KT_LATIN || type == KT_LETTER) && val < 128) + addkey(k_index, alttable, K(KT_META, val)); } } -static void -addfunc(struct kbsentry kbs) { +static void addfunc(struct kbsentry kbs) +{ int sh, i, x; char *ptr, *q, *r; x = kbs.kb_func; - if (x >= MAX_NR_FUNC) { - fprintf(stderr, _("%s: addfunc called with bad func %d\n"), + if (x >= MAX_NR_FUNC) { + fprintf(stderr, _("%s: addfunc called with bad func %d\n"), progname, kbs.kb_func); exit(1); } q = func_table[x]; - if (q) { /* throw out old previous def */ - sh = strlen(q) + 1; + if (q) { /* throw out old previous def */ + sh = strlen(q) + 1; ptr = q + sh; while (ptr < fp) - *q++ = *ptr++; + *q++ = *ptr++; fp -= sh; - for (i = x + 1; i < MAX_NR_FUNC; i++) - if (func_table[i]) - func_table[i] -= sh; + for (i = x + 1; i < MAX_NR_FUNC; i++) { + if (func_table[i]) + func_table[i] -= sh; + } } - ptr = func_buf; /* find place for new def */ - for (i = 0; i < x; i++) - if (func_table[i]) { - ptr = func_table[i]; - while(*ptr++); + ptr = func_buf; /* find place for new def */ + for (i = 0; i < x; i++) { + if (func_table[i]) { + ptr = func_table[i]; + while (*ptr++) ; } + } + func_table[x] = ptr; - sh = strlen((char *) kbs.kb_string) + 1; + sh = strlen((char *)kbs.kb_string) + 1; + if (fp + sh > func_buf + sizeof(func_buf)) { - fprintf(stderr, - _("%s: addfunc: func_buf overflow\n"), progname); + fprintf(stderr, _("%s: addfunc: func_buf overflow\n"), progname); exit(1); } q = fp; fp += sh; r = fp; while (q > ptr) - *--r = *--q; - strcpy(ptr, (char *) kbs.kb_string); - for (i = x + 1; i < MAX_NR_FUNC; i++) - if (func_table[i]) - func_table[i] += sh; + *--r = *--q; + strcpy(ptr, (char *)kbs.kb_string); + for (i = x + 1; i < MAX_NR_FUNC; i++) { + if (func_table[i]) + func_table[i] += sh; + } } -static void -compose(int diacr, int base, int res) { - accent_entry *ptr; +static void compose(int diacr, int base, int res) +{ + accent_entry *ptr; int direction; #ifdef KDSKBDIACRUC @@ -777,21 +814,22 @@ compose(int diacr, int base, int res) { #endif direction = TO_8BIT; - if (accent_table_size == MAX_DIACR) { - fprintf(stderr, _("compose table overflow\n")); + if (accent_table_size == MAX_DIACR) { + fprintf(stderr, _("compose table overflow\n")); exit(1); } + ptr = &accent_table[accent_table_size++]; ptr->diacr = convert_code(diacr, direction); ptr->base = convert_code(base, direction); ptr->result = convert_code(res, direction); } -static int -defkeys(int fd, int kbd_mode) { +static int defkeys(int fd, int kbd_mode) +{ struct kbentry ke; int ct = 0; - int i,j,fail; + int i, j, fail; if (optu) { /* temporarily switch to K_UNICODE while defining keys */ @@ -802,76 +840,85 @@ defkeys(int fd, int kbd_mode) { } } - for(i=0; i 1) - printf(_("deallocate keymap %d\n"), i); - - if(ioctl(fd, KDSKBENT, (unsigned long)&ke)) { - if (errno != EINVAL) { - perror("KDSKBENT"); - fprintf(stderr, - _("%s: could not deallocate keymap %d\n"), - progname, i); - exit(1); - } - /* probably an old kernel */ - /* clear keymap by hand */ - for (j = 0; j < NR_KEYS; j++) { - ke.kb_index = j; + } else if (keymaps_line_seen && !defining[i]) { + /* deallocate keymap */ + ke.kb_index = 0; ke.kb_table = i; - ke.kb_value = K_HOLE; - if(ioctl(fd, KDSKBENT, (unsigned long)&ke)) { - if (errno == EINVAL && i >= 16) - break; /* old kernel */ - perror("KDSKBENT"); - fprintf(stderr, - _("%s: cannot deallocate or clear keymap\n"), - progname); - exit(1); + ke.kb_value = K_NOSUCHMAP; + + if (verbose > 1) + printf(_("deallocate keymap %d\n"), i); + + if (ioctl(fd, KDSKBENT, (unsigned long)&ke)) { + if (errno != EINVAL) { + perror("KDSKBENT"); + fprintf(stderr, + _("%s: could not deallocate keymap %d\n"), + progname, i); + exit(1); + } + /* probably an old kernel */ + /* clear keymap by hand */ + for (j = 0; j < NR_KEYS; j++) { + ke.kb_index = j; + ke.kb_table = i; + ke.kb_value = K_HOLE; + if (ioctl + (fd, KDSKBENT, + (unsigned long)&ke)) { + if (errno == EINVAL && i >= 16) + break; /* old kernel */ + perror("KDSKBENT"); + fprintf(stderr, + _("%s: cannot deallocate or clear keymap\n"), + progname); + exit(1); + } + } } - } } - } } if (optu && ioctl(fd, KDSKBMODE, kbd_mode)) { perror("KDSKBMODE"); - fprintf(stderr, _("%s: could not return to original keyboard mode\n"), + fprintf(stderr, + _("%s: could not return to original keyboard mode\n"), progname); exit(1); } @@ -879,61 +926,64 @@ defkeys(int fd, int kbd_mode) { return ct; } -static char * -ostr(char *s) { +static char *ostr(char *s) +{ int lth = strlen(s); - char *ns0 = xmalloc(4*lth + 1); + char *ns0 = xmalloc(4 * lth + 1); char *ns = ns0; - while(*s) { - switch(*s) { - case '\n': - *ns++ = '\\'; - *ns++ = 'n'; - break; - case '\033': - *ns++ = '\\'; - *ns++ = '0'; - *ns++ = '3'; - *ns++ = '3'; - break; - default: - *ns++ = *s; - } - s++; + while (*s) { + switch (*s) { + case '\n': + *ns++ = '\\'; + *ns++ = 'n'; + break; + case '\033': + *ns++ = '\\'; + *ns++ = '0'; + *ns++ = '3'; + *ns++ = '3'; + break; + default: + *ns++ = *s; + } + s++; } *ns = 0; return ns0; } -static int -deffuncs(int fd){ - int i, ct = 0; +static int deffuncs(int fd) +{ + int i, ct = 0; char *ptr; - for (i = 0; i < MAX_NR_FUNC; i++) { - kbs_buf.kb_func = i; - if ((ptr = func_table[i])) { - strcpy((char *) kbs_buf.kb_string, ptr); - if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) - fprintf(stderr, _("failed to bind string '%s' to function %s\n"), - ostr((char *) kbs_buf.kb_string), syms[KT_FN].table[kbs_buf.kb_func]); - else - ct++; - } else if (opts) { - kbs_buf.kb_string[0] = 0; - if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) - fprintf(stderr, _("failed to clear string %s\n"), - syms[KT_FN].table[kbs_buf.kb_func]); - else - ct++; - } - } + for (i = 0; i < MAX_NR_FUNC; i++) { + kbs_buf.kb_func = i; + + if ((ptr = func_table[i])) { + strcpy((char *)kbs_buf.kb_string, ptr); + if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) + fprintf(stderr, + _("failed to bind string '%s' to function %s\n"), + ostr((char *)kbs_buf.kb_string), + syms[KT_FN].table[kbs_buf.kb_func]); + else + ct++; + } else if (opts) { + kbs_buf.kb_string[0] = 0; + if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) + fprintf(stderr, _("failed to clear string %s\n"), + syms[KT_FN].table[kbs_buf.kb_func]); + else + ct++; + } + } return ct; } -static int -defdiacs(int fd){ +static int defdiacs(int fd) +{ unsigned int i, count; struct kbdiacrs kd; #ifdef KDSKBDIACRUC @@ -942,154 +992,173 @@ defdiacs(int fd){ count = accent_table_size; if (count > MAX_DIACR) { - count = MAX_DIACR; - fprintf(stderr, _("too many compose definitions\n")); + count = MAX_DIACR; + fprintf(stderr, _("too many compose definitions\n")); } - #ifdef KDSKBDIACRUC if (prefer_unicode) { kdu.kb_cnt = count; + for (i = 0; i < kdu.kb_cnt; i++) { - kdu.kbdiacruc[i].diacr = accent_table[i].diacr; - kdu.kbdiacruc[i].base = accent_table[i].base; - kdu.kbdiacruc[i].result = accent_table[i].result; + kdu.kbdiacruc[i].diacr = accent_table[i].diacr; + kdu.kbdiacruc[i].base = accent_table[i].base; + kdu.kbdiacruc[i].result = accent_table[i].result; } - if(ioctl(fd, KDSKBDIACRUC, (unsigned long) &kdu)) { - perror("KDSKBDIACRUC"); - exit(1); + + if (ioctl(fd, KDSKBDIACRUC, (unsigned long)&kdu)) { + perror("KDSKBDIACRUC"); + exit(1); } - } - else + } else #endif { kd.kb_cnt = count; for (i = 0; i < kd.kb_cnt; i++) { - kd.kbdiacr[i].diacr = accent_table[i].diacr; - kd.kbdiacr[i].base = accent_table[i].base; - kd.kbdiacr[i].result = accent_table[i].result; + kd.kbdiacr[i].diacr = accent_table[i].diacr; + kd.kbdiacr[i].base = accent_table[i].base; + kd.kbdiacr[i].result = accent_table[i].result; } - if(ioctl(fd, KDSKBDIACR, (unsigned long) &kd)) { - perror("KDSKBDIACR"); - exit(1); + + if (ioctl(fd, KDSKBDIACR, (unsigned long)&kd)) { + perror("KDSKBDIACR"); + exit(1); } } return kd.kb_cnt; } -void -do_constant_key (int i, u_short key) { +void do_constant_key(int i, u_short key) +{ int typ, val, j; typ = KTYP(key); val = KVAL(key); + if ((typ == KT_LATIN || typ == KT_LETTER) && - ((val >= 'a' && val <= 'z') || - (val >= 'A' && val <= 'Z'))) { + ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'))) { u_short defs[16]; defs[0] = K(KT_LETTER, val); defs[1] = K(KT_LETTER, val ^ 32); defs[2] = defs[0]; defs[3] = defs[1]; - for(j=4; j<8; j++) + + for (j = 4; j < 8; j++) defs[j] = K(KT_LATIN, val & ~96); - for(j=8; j<16; j++) - defs[j] = K(KT_META, KVAL(defs[j-8])); - for(j=0; j 0 && keymap_was_set[j] && (keymap_was_set[j])[i]) continue; - addkey(i, j, defs[j%16]); + + addkey(i, j, defs[j % 16]); } + } else { /* do this also for keys like Escape, as promised in the man page */ - for (j=1; j 0 || nocompose) { - diacct = defdiacs(fd); - if (verbose) { - printf(_("Loaded %d compose %s.\n"), diacct, - (diacct == 1) ? _("definition") : _("definitions")); - } + diacct = defdiacs(fd); + + if (verbose) { + printf(_("Loaded %d compose %s.\n"), + diacct, (diacct == 1) ? _("definition") : _("definitions")); + } + + } else if (verbose) { + printf(_("(No change in compose definitions.)\n")); } - else - if (verbose) - printf(_("(No change in compose definitions.)\n")); } - -static void strings_as_usual(void) { +static void strings_as_usual(void) +{ /* * 26 strings, mostly inspired by the VT100 family */ char *stringvalues[30] = { /* F1 .. F20 */ - "\033[[A", "\033[[B", "\033[[C", "\033[[D", "\033[[E", + "\033[[A", "\033[[B", "\033[[C", "\033[[D", "\033[[E", "\033[17~", "\033[18~", "\033[19~", "\033[20~", "\033[21~", "\033[23~", "\033[24~", "\033[25~", "\033[26~", "\033[28~", "\033[29~", "\033[31~", "\033[32~", "\033[33~", "\033[34~", - /* Find, Insert, Remove, Select, Prior */ - "\033[1~", "\033[2~", "\033[3~", "\033[4~", "\033[5~", - /* Next, Macro, Help, Do, Pause */ - "\033[6~", 0, 0, 0, 0 + /* Find, Insert, Remove, Select, Prior */ + "\033[1~", "\033[2~", "\033[3~", "\033[4~", "\033[5~", + /* Next, Macro, Help, Do, Pause */ + "\033[6~", 0, 0, 0, 0 }; int i; - for (i=0; i<30; i++) if(stringvalues[i]) { - struct kbsentry ke; - ke.kb_func = i; - strncpy((char *) ke.kb_string, stringvalues[i], sizeof(ke.kb_string)); - ke.kb_string[sizeof(ke.kb_string)-1] = 0; - addfunc(ke); + + for (i = 0; i < 30; i++) { + if (stringvalues[i]) { + struct kbsentry ke; + ke.kb_func = i; + strncpy((char *)ke.kb_string, stringvalues[i], + sizeof(ke.kb_string)); + ke.kb_string[sizeof(ke.kb_string) - 1] = 0; + addfunc(ke); + } } } -static void -compose_as_usual(char *charset) { +static void compose_as_usual(char *charset) +{ if (charset && strcmp(charset, "iso-8859-1")) { fprintf(stderr, _("loadkeys: don't know how to compose for %s\n"), charset); exit(1); + } else { struct ccc { unsigned char c1, c2, c3; @@ -1130,10 +1199,9 @@ compose_as_usual(char *charset) { { 's', 'z', 0337 }, { 'i', 'j', 0377 } }; int i; - for(i=0; i<68; i++) { + for (i = 0; i < 68; i++) { struct ccc ptr = def_latin1_composes[i]; compose(ptr.c1, ptr.c2, ptr.c3); } } } - -- 2.7.4