1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
* Copyright (c) 2008-2009 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <linux/kd.h>
#include "shvar.h"
static char *lang = NULL;
static char *font = NULL;
static char *acm = NULL;
static char *unimap = NULL;
static char *keymap = NULL;
static char *unikeytable = NULL;
static char *backspace = NULL;
static char *grp_toggle = NULL;
static int linux_console(int fd) {
unsigned char twelve = 12;
if (ioctl(fd, TIOCLINUX, &twelve) >= 0)
return 1;
return 0;
}
static int configured_as_utf8() {
shvarFile *i18nfile = NULL;
if ((i18nfile = svNewFile("/etc/sysconfig/i18n")) == NULL)
return 1; /* assume UTF-8 */
lang = svGetValue(i18nfile, "LANG");
font = svGetValue(i18nfile, "SYSFONT");
acm = svGetValue(i18nfile, "SYSFONTACM");
unimap = svGetValue(i18nfile, "UNIMAP");
svCloseFile(i18nfile);
if (!lang)
return 1;
if (g_str_has_suffix(lang,".utf8") || g_str_has_suffix(lang,".UTF-8"))
return 1;
return 0;
}
static int read_keymap(int utf8) {
shvarFile *keyboard = NULL;
char *tmp;
struct stat sb;
if (!stat("/etc/sysconfig/console/default.kmap",&sb)) {
keymap = "/etc/sysconfig/console/default.kmap";
}
if ((keyboard = svNewFile("/etc/sysconfig/keyboard")) == NULL)
return 0;
tmp = svGetValue(keyboard, "BACKSPACE");
if (tmp)
backspace = tmp;
tmp = svGetValue(keyboard, "GRP_TOGGLE");
if (tmp)
grp_toggle = tmp;
if (keymap)
return 0;
tmp = svGetValue(keyboard, "KEYMAP");
if (tmp)
keymap = tmp;
tmp = svGetValue(keyboard, "UNIKEYTABLE");
if (tmp)
unikeytable = tmp;
tmp = svGetValue(keyboard, "KEYTABLE");
if (tmp) {
if (keymap) free(keymap);
keymap = NULL;
if (utf8) {
if (g_str_has_suffix (tmp, "uni") || strstr(tmp,".uni."))
unikeytable = tmp;
else {
char * substring;
if ((substring = strstr(tmp, ".map"))) {
GString *new_string;
new_string = g_string_new_len (tmp, strlen(tmp)+4);
new_string = g_string_insert_len(new_string, substring-tmp,".uni",4);
unikeytable = g_string_free (new_string, FALSE);
}
else {
free (unikeytable);
asprintf (&unikeytable, "%s.uni", tmp);
}
}
}
asprintf(&keymap, "%s.map", tmp);
}
return 0;
}
static void set_font(char *device) {
int pid;
if ( (pid = fork()) == 0) {
char *args[] = { "setfont", "latarcyrheb-sun16", "-C", NULL,
NULL, NULL, NULL, NULL, NULL };
if (font)
args[1] = font;
args[3] = device;
if (acm) {
args[4] = "-m";
args[5] = acm;
if (unimap) {
args[6] = "-u";
args[7] = unimap;
}
} else if (unimap) {
args[4] = "-u";
args[5] = unimap;
}
execv("/bin/setfont", args);
exit(1);
}
}
static void set_keyboard(int fd, int utf8) {
if (ioctl(fd, KDSKBMODE, utf8 ? K_UNICODE : K_XLATE))
perror("could not set keyboard mode");
}
static void set_terminal(int fd, int utf8) {
if (utf8)
write(fd, "\033%G", 3);
else
write(fd, "\033%@", 3);
}
static void set_keymap(int fd, int utf8) {
int pid;
int status;
if ((pid = fork()) == 0) {
char *args[] = { "/bin/loadkeys", "-q", NULL, NULL, NULL };
dup2(fd, 0);
dup2(fd, 1);
if (utf8) {
if (unikeytable) {
args[2] = unikeytable;
if (g_spawn_sync (NULL, args, NULL, G_SPAWN_CHILD_INHERITS_STDIN , NULL, NULL, NULL, NULL, NULL, NULL))
exit(0);
}
args[2] = "-u";
args[3] = keymap;
} else {
args[2] = keymap;
}
execv("/bin/loadkeys", args);
exit(1);
}
waitpid(pid, &status, 0);
}
static void set_backspace(int fd) {
int pid;
int status;
if ((pid = fork()) == 0) {
char *args[] = { "/bin/loadkeys", "-q", NULL, NULL };
dup2(fd, 0);
dup2(fd, 1);
if (backspace && g_ascii_strcasecmp (backspace, "BackSpace")) {
args[2] = "backspace";
} else {
args[2] = "delete";
}
execv("/bin/loadkeys", args);
exit(1);
}
waitpid(pid, &status, 0);
}
static void set_grp_toggle(int fd) {
int pid;
int status;
if ((pid = fork()) == 0) {
char *args[] = { "/bin/loadkeys", "-q", NULL, NULL };
dup2(fd, 0);
dup2(fd, 1);
args[2] = grp_toggle;
execv("/bin/loadkeys", args);
exit(1);
}
waitpid(pid, &status, 0);
}
int main(int argc, char **argv) {
char *device;
int dev;
struct stat sb;
if (argc < 2) {
printf("usage: console_init <device>\n");
exit(1);
}
chdir("/dev");
device = argv[1];
dev = open(device, O_RDWR);
if (linux_console(dev)) {
int utf8 = configured_as_utf8();
set_keyboard(dev, utf8);
set_terminal(dev, utf8);
set_font(device);
read_keymap(utf8);
if (keymap || unikeytable)
set_keymap(dev,utf8);
set_backspace(dev);
if (grp_toggle)
set_grp_toggle(dev);
}
/* check if /usr is mounted */
return stat("/usr/bin",&sb) != 0;
}
|