xref: /netbsd-src/sys/arch/atari/stand/loadkmap/loadkmap.c (revision 55f3b092cec81a6184f07a65dbbec4a076c1d106)
1 /*	$NetBSD: loadkmap.c,v 1.12 2019/06/29 16:29:24 tsutsui Exp $	*/
2 
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/ioctl.h>
6 #include "../../dev/iteioctl.h"
7 #include "../../dev/kbdmap.h"
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 
14 static int load_kmap(const char *, int);
15 static int dump_kmap(void);
16 
17 int
main(int argc,char * argv[])18 main(int argc, char *argv[])
19 {
20 	int	set_sysmap = 0;
21 	char	*mapfile;
22 	int	rc = 0;
23 
24 	if (argc > 2) {
25 		if ((argc == 3) && !strcmp(argv[1], "-f")) {
26 			mapfile = argv[2];
27 			set_sysmap = 1;
28 		} else {
29 			fprintf(stderr, "%s [-f] keymap\n", argv[0]);
30 			exit(1);
31 		}
32 	} else
33 		mapfile = argv[1];
34 
35 	if (argc == 1)
36 		rc = dump_kmap();
37 	else
38 		rc = load_kmap(mapfile, set_sysmap);
39 
40 	exit(rc);
41 }
42 
43 
44 static int
load_kmap(const char * file,int set_sysmap)45 load_kmap(const char *file, int set_sysmap)
46 {
47 	int	fd;
48 	char	buf[sizeof(struct kbdmap)];
49 	int	ioc;
50 
51 	ioc = set_sysmap ? ITEIOCSSKMAP : ITEIOCSKMAP;
52 
53 	if ((fd = open(file, 0)) >= 0) {
54 		if (read(fd, buf, sizeof(buf)) == sizeof(buf)) {
55 			if (ioctl(0, ioc, buf) == 0) {
56 				close(fd);
57 				return 0;
58 			} else
59 				perror("ITEIOCSKMAP");
60 		} else
61 			perror("read kmap");
62 
63 		close(fd);
64 	} else
65 		perror("open kmap");
66 	return 1;
67 }
68 
69 static int
dump_kmap(void)70 dump_kmap(void)
71 {
72 	char buf[sizeof(struct kbdmap)];
73 
74 	if (ioctl(0, ITEIOCGKMAP, buf) == 0) {
75 		write(1, buf, sizeof(buf));
76 		return 0;
77 	}
78 	perror("ITEIOCGKMAP");
79 	return 1;
80 }
81