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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
* mapscrn.c
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include "kbd.h"
#include "paths.h"
#include "findfile.h"
#include "kdmapop.h"
#include "utf8.h"
#include "nls.h"
#include "kbd_error.h"
/* the two exported functions */
void saveoldmap(int fd, char *omfil);
void loadnewmap(int fd, char *mfil);
static int ctoi(char *);
/* search for the map file in these directories (with trailing /) */
static const char *const mapdirpath[] = { "", DATADIR "/" TRANSDIR "/", 0 };
static const char *const mapsuffixes[] = { "", ".trans", "_to_uni.trans", ".acm", 0 };
#ifdef MAIN
#include "getfd.h"
#include "version.h"
int verbose = 0;
int debug = 0;
int main(int argc, char *argv[])
{
int fd;
set_progname(argv[0]);
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE_NAME, LOCALEDIR);
textdomain(PACKAGE_NAME);
if (argc == 2 && !strcmp(argv[1], "-V"))
print_version_and_exit();
if (argc > 1 && !strcmp(argv[1], "-v")) {
verbose = 1;
argc--;
argv++;
}
if ((fd = getfd(NULL)) < 0)
kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console"));
if (argc >= 3 && !strcmp(argv[1], "-o")) {
saveoldmap(fd, argv[2]);
argc -= 2;
argv += 2;
if (argc == 1)
exit(EXIT_SUCCESS);
}
if (argc != 2) {
fprintf(stderr, _("usage: %s [-V] [-v] [-o map.orig] map-file\n"),
progname);
exit(EXIT_FAILURE);
}
loadnewmap(fd, argv[1]);
exit(EXIT_SUCCESS);
}
#endif
/*
* Read two-column file (index, value) with index in 0-255
* and value in 0-65535. Acceptable representations:
* decimal (nnn), octal (0nnn), hexadecimal (0xnnn), Unicode (U+xxxx),
* character ('x').
* In the character encoding, x may be a single byte binary value
* (but not space, tab, comma, #) or a single Unicode value coded
* as a UTF-8 sequence.
*
* Store values in ubuf[], and small values also in buf[].
* Set u to 1 in case a value above 255 or a U+ occurs.
* Set lineno to line number of first error.
*/
static int
parsemap(FILE *fp, char *buf, unsigned short *ubuf, int *u, int *lineno)
{
char buffer[256];
int in, on, ln, ret = 0;
char *p, *q;
ln = 0;
while (fgets(buffer, sizeof(buffer) - 1, fp)) {
ln++;
if (!*u && strstr(buffer, "U+"))
*u = 1;
p = strtok(buffer, " \t\n");
if (p && *p != '#') {
q = strtok(NULL, " \t\n#");
if (q) {
in = ctoi(p);
on = ctoi(q);
if (in >= 0 && in < 256 &&
on >= 0 && on < 65536) {
ubuf[in] = on;
if (on < 256)
buf[in] = on;
else
*u = 1;
} else {
if (!ret)
*lineno = ln;
ret = -1;
}
}
}
}
return ret;
}
static int
readnewmapfromfile(char *mfil, char *buf, unsigned short *ubuf)
{
struct stat stbuf;
int u = 0;
int lineno = 0;
lkfile_t fp;
if (lk_findfile(mfil, mapdirpath, mapsuffixes, &fp)) {
fprintf(stderr, _("mapscrn: cannot open map file _%s_\n"),
mfil);
exit(1);
}
if (stat(fp.pathname, &stbuf)) {
perror(fp.pathname);
fprintf(stderr, _("Cannot stat map file"));
exit(1);
}
if (stbuf.st_size == E_TABSZ) {
if (verbose)
printf(_("Loading binary direct-to-font screen map "
"from file %s\n"),
fp.pathname);
if (fread(buf, E_TABSZ, 1, fp.fd) != 1) {
fprintf(stderr,
_("Error reading map from file `%s'\n"),
fp.pathname);
exit(1);
}
} else if (stbuf.st_size == 2 * E_TABSZ) {
if (verbose)
printf(_("Loading binary unicode screen map "
"from file %s\n"),
fp.pathname);
if (fread(ubuf, 2 * E_TABSZ, 1, fp.fd) != 1) {
fprintf(stderr,
_("Error reading map from file `%s'\n"),
fp.pathname);
exit(1);
}
u = 1;
} else {
if (verbose)
printf(_("Loading symbolic screen map from file %s\n"),
fp.pathname);
if (parsemap(fp.fd, buf, ubuf, &u, &lineno)) {
fprintf(stderr,
_("Error parsing symbolic map "
"from `%s', line %d\n"),
fp.pathname, lineno);
exit(1);
}
}
lk_fpclose(&fp);
return u;
}
void loadnewmap(int fd, char *mfil)
{
unsigned short ubuf[E_TABSZ];
char buf[E_TABSZ];
int i, u;
/* default: trivial straight-to-font */
for (i = 0; i < E_TABSZ; i++) {
buf[i] = i;
ubuf[i] = (0xf000 + i);
}
u = 0;
if (mfil)
u = readnewmapfromfile(mfil, buf, ubuf);
/* do we need to use loaduniscrnmap() ? */
if (u) {
/* yes */
if (loaduniscrnmap(fd, ubuf))
exit(1);
} else {
/* no */
if (loadscrnmap(fd, buf))
exit(1);
}
}
/*
* Read decimal, octal, hexadecimal, Unicode (U+xxxx) or character
* ('x', x a single byte or a utf8 sequence).
*/
int ctoi(char *s)
{
int i;
if ((strncmp(s, "0x", 2) == 0) &&
(strspn(s + 2, "0123456789abcdefABCDEF") == strlen(s + 2)))
(void)sscanf(s + 2, "%x", &i);
else if ((*s == '0') &&
(strspn(s, "01234567") == strlen(s)))
(void)sscanf(s, "%o", &i);
else if (strspn(s, "0123456789") == strlen(s))
(void)sscanf(s, "%d", &i);
else if ((strncmp(s, "U+", 2) == 0) && strlen(s) == 6 &&
(strspn(s + 2, "0123456789abcdefABCDEF") == 4))
(void)sscanf(s + 2, "%x", &i);
else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
i = s[1];
else if (s[0] == '\'') {
int err;
char *s1 = s + 1;
i = from_utf8(&s1, 0, &err);
if (err || s1[0] != '\'' || s1[1] != 0)
return -1;
}
else
return -1;
return i;
}
void saveoldmap(int fd, char *omfil)
{
FILE *fp;
char buf[E_TABSZ];
unsigned short ubuf[E_TABSZ];
int i, havemap, haveumap;
if ((fp = fopen(omfil, "w")) == NULL) {
perror(omfil);
exit(1);
}
havemap = haveumap = 1;
if (getscrnmap(fd, buf))
havemap = 0;
if (getuniscrnmap(fd, ubuf))
haveumap = 0;
if (havemap && haveumap) {
for (i = 0; i < E_TABSZ; i++) {
if ((ubuf[i] & ~0xff) != 0xf000) {
havemap = 0;
break;
}
}
}
if (havemap) {
if (fwrite(buf, sizeof(buf), 1, fp) != 1) {
fprintf(stderr, _("Error writing map to file\n"));
exit(1);
}
} else if (haveumap) {
if (fwrite(ubuf, sizeof(ubuf), 1, fp) != 1) {
fprintf(stderr, _("Error writing map to file\n"));
exit(1);
}
} else {
fprintf(stderr, _("Cannot read console map\n"));
exit(1);
}
fclose(fp);
if (verbose)
printf(_("Saved screen map in `%s'\n"), omfil);
}
|