10 #include <sys/types.h>
12 #include <sys/ioctl.h>
20 /* the two exported functions */
21 void saveoldmap(int fd, char *omfil);
22 void loadnewmap(int fd, char *mfil);
24 static int ctoi (unsigned char *);
26 /* search for the map file in these directories (with trailing /) */
27 static char *mapdirpath[] = { "", DATADIR "/" TRANSDIR "/", 0 };
28 static char *mapsuffixes[] = { "", ".trans", "_to_uni.trans", 0 };
37 main(int argc, char *argv[]) {
40 set_progname(argv[0]);
42 setlocale(LC_ALL, "");
43 bindtextdomain(PACKAGE, LOCALEDIR);
46 if (argc == 2 && !strcmp(argv[1], "-V"))
47 print_version_and_exit();
49 if (argc > 1 && !strcmp(argv[1], "-v")) {
57 if (argc >= 3 && !strcmp(argv[1], "-o")) {
58 saveoldmap(fd, argv[2]);
66 fprintf(stderr, _("usage: %s [-v] [-o map.orig] map-file\n"),
70 loadnewmap(fd, argv[1]);
76 * Read two-column file (index, value) with index in 0-255
77 * and value in 0-65535. Acceptable representations:
78 * decimal (nnn), octal (0nnn), hexadecimal (0xnnn), Unicode (U+xxxx),
80 * In the character encoding, x may be a single byte binary value
81 * (but not space, tab, comma, #) or a single Unicode value coded
82 * as a UTF-8 sequence.
84 * Store values in ubuf[], and small values also in buf[].
85 * Set u to 1 in case a value above 255 or a U+ occurs.
86 * Set lineno to line number of first error.
89 parsemap(FILE *fp, char *buf, unsigned short *ubuf, int *u, int *lineno) {
91 int in, on, ln, ret = 0;
95 while (fgets(buffer,sizeof(buffer)-1,fp)) {
97 if (!*u && strstr(buffer, "U+"))
99 p = strtok(buffer," \t\n");
100 if (p && *p != '#') {
101 q = strtok(NULL," \t\n#");
105 if (in >= 0 && in < 256 &&
106 on >= 0 && on < 65536) {
124 readnewmapfromfile(int fd, char *mfil, char *buf, unsigned short *ubuf) {
130 if ((fp = findfile(mfil, mapdirpath, mapsuffixes)) == NULL) {
131 fprintf(stderr, _("mapscrn: cannot open map file _%s_\n"),
135 if (stat(pathname, &stbuf)) {
137 fprintf(stderr, _("Cannot stat map file"));
140 if (stbuf.st_size == E_TABSZ) {
142 printf(_("Loading binary direct-to-font screen map "
145 if (fread(buf,E_TABSZ,1,fp) != 1) {
147 _("Error reading map from file `%s'\n"),
151 } else if (stbuf.st_size == 2*E_TABSZ) {
153 printf(_("Loading binary unicode screen map "
156 if (fread(ubuf,2*E_TABSZ,1,fp) != 1) {
158 _("Error reading map from file `%s'\n"),
165 printf(_("Loading symbolic screen map from file %s\n"),
167 if (parsemap(fp,buf,ubuf,&u,&lineno)) {
169 _("Error parsing symbolic map "
170 "from `%s', line %d\n"),
180 loadnewmap(int fd, char *mfil) {
181 unsigned short ubuf[E_TABSZ];
185 /* default: trivial straight-to-font */
186 for (i=0; i<E_TABSZ; i++) {
188 ubuf[i] = (0xf000 + i);
193 u = readnewmapfromfile(fd, mfil, buf, ubuf);
195 /* do we need to use loaduniscrnmap() ? */
198 if (loaduniscrnmap(fd, ubuf))
202 if (loadscrnmap(fd,buf))
208 * Read decimal, octal, hexadecimal, Unicode (U+xxxx) or character
209 * ('x', x a single byte or a utf8 sequence).
212 ctoi(unsigned char *s) {
215 if ((strncmp(s,"0x",2) == 0) &&
216 (strspn(s+2,"0123456789abcdefABCDEF") == strlen(s+2)))
219 else if ((*s == '0') &&
220 (strspn(s,"01234567") == strlen(s)))
223 else if (strspn(s,"0123456789") == strlen(s))
226 else if ((strncmp(s,"U+",2) == 0) && strlen(s) == 6 &&
227 (strspn(s+2,"0123456789abcdefABCDEF") == 4))
230 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
233 else if (s[0] == '\'') {
237 i = from_utf8(&s1, 0, &err);
238 if (err || s1[0] != '\'' || s1[1] != 0)
248 saveoldmap(int fd, char *omfil) {
251 unsigned short ubuf[E_TABSZ];
252 int i, havemap, haveumap;
254 if ((fp = fopen(omfil, "w")) == NULL) {
258 havemap = haveumap = 1;
259 if (getscrnmap(fd,buf))
261 if (getuniscrnmap(fd,ubuf))
263 if (havemap && haveumap) {
264 for (i = 0; i < E_TABSZ; i++) {
265 if ((ubuf[i] & ~0xff) != 0xf000) {
272 if (fwrite(buf,sizeof(buf),1,fp) != 1) {
273 fprintf(stderr, _("Error writing map to file\n"));
276 } else if (haveumap) {
277 if (fwrite(ubuf,sizeof(ubuf),1,fp) != 1) {
278 fprintf(stderr, _("Error writing map to file\n"));
282 fprintf(stderr, _("Cannot read console map\n"));
288 printf(_("Saved screen map in `%s'\n"), omfil);