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