From 9d6b273cc39e9931f6f650ab348a85fe2ca0a00c Mon Sep 17 00:00:00 2001 From: Alexey Gladkov Date: Fri, 21 Oct 2011 02:58:30 +0400 Subject: [PATCH] Change the handling of -C option Signed-off-by: Alexey Gladkov --- man/man1/loadkeys.1.in | 8 +-- src/loadkeys.c | 176 +++++++++++++++++++++++-------------------------- src/loadkeys.y | 90 ++++++++++++------------- 3 files changed, 129 insertions(+), 145 deletions(-) diff --git a/man/man1/loadkeys.1.in b/man/man1/loadkeys.1.in index e39eb76..95e141a 100644 --- a/man/man1/loadkeys.1.in +++ b/man/man1/loadkeys.1.in @@ -9,9 +9,9 @@ loadkeys \- load keyboard translation tables ] [ .I -c --clearcompose ] [ -.I -C '' +.I -C '' | -.I --console=cons1,cons2,... +.I --console= ] [ .I -d --default ] [ @@ -38,11 +38,11 @@ The program reads the file or files specified by .IR filename... . Its main purpose is to load the kernel keymap for the console. -The affected console device or devices can be specified using the +You can specify console device by the .I -C (or .I --console -) option. This option supports a list of device names +) option. .SH "RESET TO DEFAULT" If the .I -d diff --git a/src/loadkeys.c b/src/loadkeys.c index 357463e..4ea48a4 100644 --- a/src/loadkeys.c +++ b/src/loadkeys.c @@ -155,8 +155,8 @@ static void attr_noreturn usage(void) " -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" + " -C --console=file\n" + " the console device 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" @@ -462,6 +462,15 @@ static int defkeys(int fd, int kbd_mode) return ct; } +static void freekeys(void) +{ + int i; + for (i = 0; i < MAX_NR_KEYMAPS; i++) { + xfree(keymap_was_set[i]); + xfree(key_map[i]); + } +} + static char *ostr(char *s) { int lth = strlen(s); @@ -492,27 +501,30 @@ static char *ostr(char *s) static int deffuncs(int fd) { int i, ct = 0; - char *ptr; + char *ptr, *s; 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)) + if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) { + s = ostr((char *)kbs_buf.kb_string); 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 + s, syms[KT_FN].table[kbs_buf.kb_func]); + xfree(s); + } else { ct++; + } } else if (opts) { kbs_buf.kb_string[0] = 0; - if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) + 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 + } else { ct++; + } } } return ct; @@ -629,12 +641,10 @@ static void do_constant(void) } } -static void loadkeys(char *console, int kbd_mode) +static void loadkeys(int fd, int kbd_mode) { - int fd; int keyct, funcct, diacct = 0; - fd = getfd(console); keyct = defkeys(fd, kbd_mode); funcct = deffuncs(fd); @@ -655,6 +665,8 @@ static void loadkeys(char *console, int kbd_mode) } else if (verbose) { printf(_("(No change in compose definitions.)\n")); } + + freekeys(); } static void strings_as_usual(void) @@ -895,6 +907,8 @@ static void attr_noreturn mktable(void) } printf("unsigned int accent_table_size = %d;\n", accent_table_size); + freekeys(); + exit(0); } @@ -922,16 +936,18 @@ static void attr_noreturn bkeymap(void) } } } + freekeys(); exit(0); fail: fprintf(stderr, _("Error writing map to file\n")); + freekeys(); exit(1); } /* Line 189 of yacc.c */ -#line 935 "loadkeys.c" +#line 951 "loadkeys.c" /* Enabling traces. */ #ifndef YYDEBUG @@ -1043,7 +1059,7 @@ typedef int YYSTYPE; /* Line 264 of yacc.c */ -#line 1047 "loadkeys.c" +#line 1063 "loadkeys.c" #ifdef short # undef short @@ -1348,12 +1364,12 @@ static const yytype_int8 yyrhs[] = /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ static const yytype_uint16 yyrline[] = { - 0, 875, 875, 876, 878, 879, 880, 881, 882, 883, - 884, 885, 886, 887, 889, 894, 899, 904, 908, 913, - 918, 919, 921, 927, 932, 941, 945, 950, 951, 953, - 953, 960, 965, 966, 968, 969, 970, 971, 972, 973, - 974, 975, 976, 978, 1017, 1018, 1020, 1027, 1028, 1029, - 1030, 1031, 1032 + 0, 891, 891, 892, 894, 895, 896, 897, 898, 899, + 900, 901, 902, 903, 905, 910, 915, 920, 924, 929, + 934, 935, 937, 943, 948, 957, 961, 966, 967, 969, + 969, 976, 981, 982, 984, 985, 986, 987, 988, 989, + 990, 991, 992, 994, 1033, 1034, 1036, 1043, 1044, 1045, + 1046, 1047, 1048 }; #endif @@ -2325,7 +2341,7 @@ yyreduce: case 14: /* Line 1464 of yacc.c */ -#line 890 "loadkeys.y" +#line 906 "loadkeys.y" { set_charset((char *) kbs_buf.kb_string); } @@ -2334,7 +2350,7 @@ yyreduce: case 15: /* Line 1464 of yacc.c */ -#line 895 "loadkeys.y" +#line 911 "loadkeys.y" { alt_is_meta = 1; } @@ -2343,7 +2359,7 @@ yyreduce: case 16: /* Line 1464 of yacc.c */ -#line 900 "loadkeys.y" +#line 916 "loadkeys.y" { strings_as_usual(); } @@ -2352,7 +2368,7 @@ yyreduce: case 17: /* Line 1464 of yacc.c */ -#line 905 "loadkeys.y" +#line 921 "loadkeys.y" { compose_as_usual((char *) kbs_buf.kb_string); } @@ -2361,7 +2377,7 @@ yyreduce: case 18: /* Line 1464 of yacc.c */ -#line 909 "loadkeys.y" +#line 925 "loadkeys.y" { compose_as_usual(0); } @@ -2370,7 +2386,7 @@ yyreduce: case 19: /* Line 1464 of yacc.c */ -#line 914 "loadkeys.y" +#line 930 "loadkeys.y" { keymaps_line_seen = 1; } @@ -2379,7 +2395,7 @@ yyreduce: case 22: /* Line 1464 of yacc.c */ -#line 922 "loadkeys.y" +#line 938 "loadkeys.y" { int i; for (i = (yyvsp[(1) - (3)]); i <= (yyvsp[(3) - (3)]); i++) @@ -2390,7 +2406,7 @@ yyreduce: case 23: /* Line 1464 of yacc.c */ -#line 928 "loadkeys.y" +#line 944 "loadkeys.y" { addmap((yyvsp[(1) - (1)]),1); } @@ -2399,7 +2415,7 @@ yyreduce: case 24: /* Line 1464 of yacc.c */ -#line 933 "loadkeys.y" +#line 949 "loadkeys.y" { if (KTYP((yyvsp[(2) - (5)])) != KT_FN) lkfatal(_("'%s' is not a function key symbol"), @@ -2412,7 +2428,7 @@ yyreduce: case 25: /* Line 1464 of yacc.c */ -#line 942 "loadkeys.y" +#line 958 "loadkeys.y" { compose((yyvsp[(2) - (6)]), (yyvsp[(3) - (6)]), (yyvsp[(5) - (6)])); } @@ -2421,7 +2437,7 @@ yyreduce: case 26: /* Line 1464 of yacc.c */ -#line 946 "loadkeys.y" +#line 962 "loadkeys.y" { compose((yyvsp[(2) - (6)]), (yyvsp[(3) - (6)]), (yyvsp[(5) - (6)])); } @@ -2430,21 +2446,21 @@ yyreduce: case 27: /* Line 1464 of yacc.c */ -#line 950 "loadkeys.y" +#line 966 "loadkeys.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 28: /* Line 1464 of yacc.c */ -#line 951 "loadkeys.y" +#line 967 "loadkeys.y" { (yyval) = (yyvsp[(1) - (1)]) ^ 0xf000; } break; case 29: /* Line 1464 of yacc.c */ -#line 953 "loadkeys.y" +#line 969 "loadkeys.y" { mod = 0; } @@ -2453,7 +2469,7 @@ yyreduce: case 30: /* Line 1464 of yacc.c */ -#line 957 "loadkeys.y" +#line 973 "loadkeys.y" { addkey((yyvsp[(4) - (7)]), mod, (yyvsp[(6) - (7)])); } @@ -2462,7 +2478,7 @@ yyreduce: case 31: /* Line 1464 of yacc.c */ -#line 961 "loadkeys.y" +#line 977 "loadkeys.y" { addkey((yyvsp[(3) - (6)]), 0, (yyvsp[(5) - (6)])); } @@ -2471,70 +2487,70 @@ yyreduce: case 34: /* Line 1464 of yacc.c */ -#line 968 "loadkeys.y" +#line 984 "loadkeys.y" { mod |= M_SHIFT; } break; case 35: /* Line 1464 of yacc.c */ -#line 969 "loadkeys.y" +#line 985 "loadkeys.y" { mod |= M_CTRL; } break; case 36: /* Line 1464 of yacc.c */ -#line 970 "loadkeys.y" +#line 986 "loadkeys.y" { mod |= M_ALT; } break; case 37: /* Line 1464 of yacc.c */ -#line 971 "loadkeys.y" +#line 987 "loadkeys.y" { mod |= M_ALTGR; } break; case 38: /* Line 1464 of yacc.c */ -#line 972 "loadkeys.y" +#line 988 "loadkeys.y" { mod |= M_SHIFTL; } break; case 39: /* Line 1464 of yacc.c */ -#line 973 "loadkeys.y" +#line 989 "loadkeys.y" { mod |= M_SHIFTR; } break; case 40: /* Line 1464 of yacc.c */ -#line 974 "loadkeys.y" +#line 990 "loadkeys.y" { mod |= M_CTRLL; } break; case 41: /* Line 1464 of yacc.c */ -#line 975 "loadkeys.y" +#line 991 "loadkeys.y" { mod |= M_CTRLR; } break; case 42: /* Line 1464 of yacc.c */ -#line 976 "loadkeys.y" +#line 992 "loadkeys.y" { mod |= M_CAPSSHIFT; } break; case 43: /* Line 1464 of yacc.c */ -#line 979 "loadkeys.y" +#line 995 "loadkeys.y" { int i, j; @@ -2576,7 +2592,7 @@ yyreduce: case 46: /* Line 1464 of yacc.c */ -#line 1021 "loadkeys.y" +#line 1037 "loadkeys.y" { if (rvalct >= MAX_NR_KEYMAPS) lkfatal(_("too many key definitions on one line")); @@ -2587,49 +2603,49 @@ yyreduce: case 47: /* Line 1464 of yacc.c */ -#line 1027 "loadkeys.y" +#line 1043 "loadkeys.y" { (yyval) = convert_code((yyvsp[(1) - (1)]), TO_AUTO); } break; case 48: /* Line 1464 of yacc.c */ -#line 1028 "loadkeys.y" +#line 1044 "loadkeys.y" { (yyval) = add_capslock((yyvsp[(2) - (2)])); } break; case 49: /* Line 1464 of yacc.c */ -#line 1029 "loadkeys.y" +#line 1045 "loadkeys.y" { (yyval) = convert_code((yyvsp[(1) - (1)])^0xf000, TO_AUTO); } break; case 50: /* Line 1464 of yacc.c */ -#line 1030 "loadkeys.y" +#line 1046 "loadkeys.y" { (yyval) = add_capslock((yyvsp[(2) - (2)])^0xf000); } break; case 51: /* Line 1464 of yacc.c */ -#line 1031 "loadkeys.y" +#line 1047 "loadkeys.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 52: /* Line 1464 of yacc.c */ -#line 1032 "loadkeys.y" +#line 1048 "loadkeys.y" { (yyval) = add_capslock((yyvsp[(2) - (2)])); } break; /* Line 1464 of yacc.c */ -#line 2633 "loadkeys.c" +#line 2649 "loadkeys.c" default: break; } YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); @@ -2841,7 +2857,7 @@ yyreturn: /* Line 1684 of yacc.c */ -#line 1034 "loadkeys.y" +#line 1050 "loadkeys.y" static void parse_keymap(FILE *fd) { @@ -2915,6 +2931,7 @@ int main(int argc, char *argv[]) break; case 'u': optu = 1; + prefer_unicode = 1; break; case 'q': quiet = 1; @@ -2937,15 +2954,14 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - prefer_unicode = optu; + /* get console */ + fd = getfd(console); if (!optm && !optb) { /* check whether the keyboard is in Unicode mode */ - fd = getfd(console); - - if (ioctl(fd, KDGKBMODE, &kbd_mode)) { - perror("KDGKBMODE"); - fprintf(stderr, _("%s: error reading keyboard mode\n"), + if (ioctl(fd, KDGKBMODE, &kbd_mode) || + ioctl(fd, KDGETMODE, &kd_mode)) { + fprintf(stderr, _("%s: error reading keyboard mode: %m\n"), progname); exit(EXIT_FAILURE); } @@ -2962,14 +2978,13 @@ int main(int argc, char *argv[]) /* reset -u option if keyboard is in K_UNICODE anyway */ optu = 0; - } else if (optu - && (ioctl(fd, KDGETMODE, &kd_mode) || (kd_mode != KD_GRAPHICS))) + + } else if (optu && 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); + } } for (i = optind; argv[i]; i++) { @@ -3015,39 +3030,16 @@ int main(int argc, char *argv[]) do_constant(); - if (optb) { + if (optb) bkeymap(); - } else if (optm) { + if (optm) mktable(); - } 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'; + loadkeys(fd, kbd_mode); - if (verbose) - printf("%s\n", s); + close(fd); - loadkeys(s, kbd_mode); - - *e = ch; - s = e; - } - free(buf); - - } else { - loadkeys(NULL, kbd_mode); - } exit(EXIT_SUCCESS); } diff --git a/src/loadkeys.y b/src/loadkeys.y index c45110d..b03ff51 100644 --- a/src/loadkeys.y +++ b/src/loadkeys.y @@ -96,8 +96,8 @@ static void attr_noreturn usage(void) " -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" + " -C --console=file\n" + " the console device 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" @@ -403,6 +403,15 @@ static int defkeys(int fd, int kbd_mode) return ct; } +static void freekeys(void) +{ + int i; + for (i = 0; i < MAX_NR_KEYMAPS; i++) { + xfree(keymap_was_set[i]); + xfree(key_map[i]); + } +} + static char *ostr(char *s) { int lth = strlen(s); @@ -433,27 +442,30 @@ static char *ostr(char *s) static int deffuncs(int fd) { int i, ct = 0; - char *ptr; + char *ptr, *s; 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)) + if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) { + s = ostr((char *)kbs_buf.kb_string); 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 + s, syms[KT_FN].table[kbs_buf.kb_func]); + xfree(s); + } else { ct++; + } } else if (opts) { kbs_buf.kb_string[0] = 0; - if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs_buf)) + 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 + } else { ct++; + } } } return ct; @@ -570,12 +582,10 @@ static void do_constant(void) } } -static void loadkeys(char *console, int kbd_mode) +static void loadkeys(int fd, int kbd_mode) { - int fd; int keyct, funcct, diacct = 0; - fd = getfd(console); keyct = defkeys(fd, kbd_mode); funcct = deffuncs(fd); @@ -596,6 +606,8 @@ static void loadkeys(char *console, int kbd_mode) } else if (verbose) { printf(_("(No change in compose definitions.)\n")); } + + freekeys(); } static void strings_as_usual(void) @@ -836,6 +848,8 @@ static void attr_noreturn mktable(void) } printf("unsigned int accent_table_size = %d;\n", accent_table_size); + freekeys(); + exit(0); } @@ -863,9 +877,11 @@ static void attr_noreturn bkeymap(void) } } } + freekeys(); exit(0); fail: fprintf(stderr, _("Error writing map to file\n")); + freekeys(); exit(1); } @@ -1104,6 +1120,7 @@ int main(int argc, char *argv[]) break; case 'u': optu = 1; + prefer_unicode = 1; break; case 'q': quiet = 1; @@ -1126,15 +1143,14 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - prefer_unicode = optu; + /* get console */ + fd = getfd(console); if (!optm && !optb) { /* check whether the keyboard is in Unicode mode */ - fd = getfd(console); - - if (ioctl(fd, KDGKBMODE, &kbd_mode)) { - perror("KDGKBMODE"); - fprintf(stderr, _("%s: error reading keyboard mode\n"), + if (ioctl(fd, KDGKBMODE, &kbd_mode) || + ioctl(fd, KDGETMODE, &kd_mode)) { + fprintf(stderr, _("%s: error reading keyboard mode: %m\n"), progname); exit(EXIT_FAILURE); } @@ -1151,14 +1167,13 @@ int main(int argc, char *argv[]) /* reset -u option if keyboard is in K_UNICODE anyway */ optu = 0; - } else if (optu - && (ioctl(fd, KDGETMODE, &kd_mode) || (kd_mode != KD_GRAPHICS))) + + } else if (optu && 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); + } } for (i = optind; argv[i]; i++) { @@ -1204,38 +1219,15 @@ int main(int argc, char *argv[]) do_constant(); - if (optb) { + if (optb) bkeymap(); - } else if (optm) { + if (optm) mktable(); - } else if (console) { - char *buf = strdup(console); /* make writable */ - char *e, *s = buf; - while (*s) { - char ch; + loadkeys(fd, kbd_mode); - while (*s == ' ' || *s == '\t' || *s == ',') - s++; - e = s; - while (*e && *e != ' ' && *e != '\t' && *e != ',') - e++; - ch = *e; - *e = '\0'; + close(fd); - if (verbose) - printf("%s\n", s); - - loadkeys(s, kbd_mode); - - *e = ch; - s = e; - } - free(buf); - - } else { - loadkeys(NULL, kbd_mode); - } exit(EXIT_SUCCESS); } -- 2.7.4