[1108] | 1 | /* |
---|
| 2 | * Copyright (c) 2008 Red Hat, Inc. All rights reserved. |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License, version 2, |
---|
| 6 | * as published by the Free Software Foundation. |
---|
| 7 | * |
---|
| 8 | * This program is distributed in the hope that it will be useful, |
---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 11 | * GNU General Public License for more details. |
---|
| 12 | * |
---|
| 13 | * You should have received a copy of the GNU General Public License |
---|
| 14 | * along with this program; if not, write to the Free Software |
---|
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include <fcntl.h> |
---|
| 20 | #include <stdio.h> |
---|
| 21 | #include <stdlib.h> |
---|
| 22 | #include <unistd.h> |
---|
| 23 | |
---|
| 24 | #include <sys/ioctl.h> |
---|
| 25 | #include <sys/stat.h> |
---|
| 26 | #include <sys/types.h> |
---|
| 27 | #include <sys/wait.h> |
---|
| 28 | |
---|
| 29 | #include <linux/kd.h> |
---|
| 30 | |
---|
| 31 | #include "shvar.h" |
---|
| 32 | |
---|
| 33 | static char *lang = NULL; |
---|
| 34 | static char *font = NULL; |
---|
| 35 | static char *acm = NULL; |
---|
| 36 | static char *unimap = NULL; |
---|
| 37 | static char *keymap = NULL; |
---|
| 38 | |
---|
| 39 | static int linux_console(int fd) { |
---|
| 40 | unsigned char twelve = 12; |
---|
| 41 | |
---|
| 42 | if (ioctl(fd, TIOCLINUX, &twelve) >= 0) |
---|
| 43 | return 1; |
---|
| 44 | return 0; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | static int configured_as_utf8() { |
---|
| 48 | shvarFile *i18nfile = NULL; |
---|
| 49 | |
---|
| 50 | if ((i18nfile = svNewFile("/etc/sysconfig/i18n")) == NULL) |
---|
| 51 | return 1; /* assume UTF-8 */ |
---|
| 52 | |
---|
| 53 | lang = svGetValue(i18nfile, "LANG"); |
---|
| 54 | font = svGetValue(i18nfile, "SYSFONT"); |
---|
| 55 | acm = svGetValue(i18nfile, "SYSFONTACM"); |
---|
| 56 | unimap = svGetValue(i18nfile, "UNIMAP"); |
---|
| 57 | svCloseFile(i18nfile); |
---|
| 58 | if (!lang) |
---|
| 59 | return 1; |
---|
| 60 | if (g_str_has_suffix(lang,".utf8") || g_str_has_suffix(lang,".UTF-8")) |
---|
| 61 | return 1; |
---|
| 62 | return 0; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | static int read_keymap() { |
---|
| 66 | shvarFile *keyboard = NULL; |
---|
| 67 | char *tmp; |
---|
| 68 | struct stat sb; |
---|
| 69 | |
---|
| 70 | if (!stat("/etc/sysconfig/console/default.kmap",&sb)) { |
---|
| 71 | keymap = "/etc/sysconfig/console/default.kmap"; |
---|
| 72 | return 0; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | if ((keyboard = svNewFile("/etc/sysconfig/keyboard")) == NULL) |
---|
| 76 | return 0; |
---|
| 77 | |
---|
| 78 | tmp = svGetValue(keyboard, "KEYMAP"); |
---|
| 79 | if (tmp) |
---|
| 80 | keymap = tmp; |
---|
| 81 | tmp = svGetValue(keyboard, "KEYTABLE"); |
---|
| 82 | if (tmp) { |
---|
| 83 | if (keymap) free(keymap); |
---|
| 84 | asprintf(&keymap, "%s.map", tmp); |
---|
| 85 | } |
---|
| 86 | return 0; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | static void set_font(char *device) { |
---|
| 90 | int pid, status; |
---|
| 91 | |
---|
| 92 | if ( (pid = fork()) == 0) { |
---|
| 93 | char *args[] = { "setfont", "latarcyrheb-sun16", "-m", "none", |
---|
| 94 | "-u", "none", "-C", NULL, NULL }; |
---|
| 95 | |
---|
| 96 | if (font) |
---|
| 97 | args[1] = font; |
---|
| 98 | if (acm) { |
---|
| 99 | args[3] = acm; |
---|
| 100 | } |
---|
| 101 | if (unimap) { |
---|
| 102 | args[5] = unimap; |
---|
| 103 | } |
---|
| 104 | args[7] = device; |
---|
| 105 | execv("/bin/setfont", args); |
---|
| 106 | exit(1); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | static void set_keyboard(int fd, int utf8) { |
---|
| 111 | if (ioctl(fd, KDSKBMODE, utf8 ? K_UNICODE : K_XLATE)) |
---|
| 112 | perror("could not set keyboard mode"); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | static void set_terminal(int fd, int utf8) { |
---|
| 116 | if (utf8) |
---|
| 117 | write(fd, "\033%G", 3); |
---|
| 118 | else |
---|
| 119 | write(fd, "\033%@", 3); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | static void set_keymap(int fd, int utf8) { |
---|
| 123 | int pid, status; |
---|
| 124 | |
---|
| 125 | if ((pid = fork()) == 0) { |
---|
| 126 | char *args[] = { "loadkeys", NULL, NULL, NULL }; |
---|
| 127 | dup2(fd, 0); |
---|
| 128 | dup2(fd, 1); |
---|
| 129 | |
---|
| 130 | if (utf8) { |
---|
| 131 | args[1] = "-u"; |
---|
| 132 | args[2] = keymap; |
---|
| 133 | } else { |
---|
| 134 | args[1] = keymap; |
---|
| 135 | } |
---|
| 136 | execv("/bin/loadkeys", args); |
---|
| 137 | exit(1); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | int main(int argc, char **argv) { |
---|
| 142 | char *device; |
---|
| 143 | int dev; |
---|
| 144 | |
---|
| 145 | if (argc < 2) { |
---|
| 146 | printf("usage: console_init <device>\n"); |
---|
| 147 | exit(1); |
---|
| 148 | } |
---|
| 149 | chdir("/dev"); |
---|
| 150 | device = argv[1]; |
---|
| 151 | dev = open(device, O_RDWR); |
---|
| 152 | if (linux_console(dev)) { |
---|
| 153 | int utf8 = configured_as_utf8(); |
---|
| 154 | |
---|
| 155 | set_keyboard(dev, utf8); |
---|
| 156 | set_terminal(dev, utf8); |
---|
| 157 | set_font(device); |
---|
| 158 | read_keymap(); |
---|
| 159 | if (keymap) |
---|
| 160 | set_keymap(dev,utf8); |
---|
| 161 | } |
---|
| 162 | return 0; |
---|
| 163 | } |
---|