10 #include <sys/types.h>
12 #include <sys/ioctl.h>
21 /* the two exported functions */
22 void saveoldmap(int fd, char *omfil);
23 void loadnewmap(int fd, char *mfil);
25 static int ctoi (char *);
27 /* search for the map file in these directories (with trailing /) */
28 static char *mapdirpath[] = { "", DATADIR "/" TRANSDIR "/", 0 };
29 static char *mapsuffixes[] = { "", ".trans", "_to_uni.trans", ".acm", 0 };
39 main(int argc, char *argv[]) {
42 set_progname(argv[0]);
44 setlocale(LC_ALL, "");
45 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
46 textdomain(PACKAGE_NAME);
48 if (argc == 2 && !strcmp(argv[1], "-V"))
49 print_version_and_exit();
51 if (argc > 1 && !strcmp(argv[1], "-v")) {
59 if (argc >= 3 && !strcmp(argv[1], "-o")) {
60 saveoldmap(fd, argv[2]);
68 fprintf(stderr, _("usage: %s [-v] [-o map.orig] map-file\n"),
72 loadnewmap(fd, argv[1]);
78 * Read two-column file (index, value) with index in 0-255
79 * and value in 0-65535. Acceptable representations:
80 * decimal (nnn), octal (0nnn), hexadecimal (0xnnn), Unicode (U+xxxx),
82 * In the character encoding, x may be a single byte binary value
83 * (but not space, tab, comma, #) or a single Unicode value coded
84 * as a UTF-8 sequence.
86 * Store values in ubuf[], and small values also in buf[].
87 * Set u to 1 in case a value above 255 or a U+ occurs.
88 * Set lineno to line number of first error.
91 parsemap(FILE *fp, char *buf, unsigned short *ubuf, int *u, int *lineno) {
93 int in, on, ln, ret = 0;
97 while (fgets(buffer,sizeof(buffer)-1,fp)) {
99 if (!*u && strstr(buffer, "U+"))
101 p = strtok(buffer," \t\n");
102 if (p && *p != '#') {
103 q = strtok(NULL," \t\n#");
107 if (in >= 0 && in < 256 &&
108 on >= 0 && on < 65536) {
126 readnewmapfromfile(attr_unused int fd, char *mfil, char *buf, unsigned short *ubuf) {
132 if ((fp = findfile(mfil, mapdirpath, mapsuffixes)) == NULL) {
133 fprintf(stderr, _("mapscrn: cannot open map file _%s_\n"),
137 if (stat(pathname, &stbuf)) {
139 fprintf(stderr, _("Cannot stat map file"));
142 if (stbuf.st_size == E_TABSZ) {
144 printf(_("Loading binary direct-to-font screen map "
147 if (fread(buf,E_TABSZ,1,fp) != 1) {
149 _("Error reading map from file `%s'\n"),
153 } else if (stbuf.st_size == 2*E_TABSZ) {
155 printf(_("Loading binary unicode screen map "
158 if (fread(ubuf,2*E_TABSZ,1,fp) != 1) {
160 _("Error reading map from file `%s'\n"),
167 printf(_("Loading symbolic screen map from file %s\n"),
169 if (parsemap(fp,buf,ubuf,&u,&lineno)) {
171 _("Error parsing symbolic map "
172 "from `%s', line %d\n"),
182 loadnewmap(int fd, char *mfil) {
183 unsigned short ubuf[E_TABSZ];
187 /* default: trivial straight-to-font */
188 for (i=0; i<E_TABSZ; i++) {
190 ubuf[i] = (0xf000 + i);
195 u = readnewmapfromfile(fd, mfil, buf, ubuf);
197 /* do we need to use loaduniscrnmap() ? */
200 if (loaduniscrnmap(fd, ubuf))
204 if (loadscrnmap(fd,buf))
210 * Read decimal, octal, hexadecimal, Unicode (U+xxxx) or character
211 * ('x', x a single byte or a utf8 sequence).
217 if ((strncmp(s,"0x",2) == 0) &&
218 (strspn(s+2,"0123456789abcdefABCDEF") == strlen(s+2)))
219 (void)sscanf(s+2,"%x",&i);
221 else if ((*s == '0') &&
222 (strspn(s,"01234567") == strlen(s)))
223 (void)sscanf(s,"%o",&i);
225 else if (strspn(s,"0123456789") == strlen(s))
226 (void)sscanf(s,"%d",&i);
228 else if ((strncmp(s,"U+",2) == 0) && strlen(s) == 6 &&
229 (strspn(s+2,"0123456789abcdefABCDEF") == 4))
230 (void)sscanf(s+2,"%x",&i);
232 else if ((strlen(s) == 3) && (s[0] == '\'') && (s[2] == '\''))
235 else if (s[0] == '\'') {
239 i = from_utf8(&s1, 0, &err);
240 if (err || s1[0] != '\'' || s1[1] != 0)
250 saveoldmap(int fd, char *omfil) {
253 unsigned short ubuf[E_TABSZ];
254 int i, havemap, haveumap;
256 if ((fp = fopen(omfil, "w")) == NULL) {
260 havemap = haveumap = 1;
261 if (getscrnmap(fd,buf))
263 if (getuniscrnmap(fd,ubuf))
265 if (havemap && haveumap) {
266 for (i = 0; i < E_TABSZ; i++) {
267 if ((ubuf[i] & ~0xff) != 0xf000) {
274 if (fwrite(buf,sizeof(buf),1,fp) != 1) {
275 fprintf(stderr, _("Error writing map to file\n"));
278 } else if (haveumap) {
279 if (fwrite(ubuf,sizeof(ubuf),1,fp) != 1) {
280 fprintf(stderr, _("Error writing map to file\n"));
284 fprintf(stderr, _("Cannot read console map\n"));
290 printf(_("Saved screen map in `%s'\n"), omfil);