xref: /haiku/src/bin/keymap/main.cpp (revision 746cac055adc6ac3308c7bc2d29040fb95689cc9)
1 /*
2  * Copyright (c) 2004-2006, Haiku, Inc.
3  *
4  * This software is part of the Haiku distribution and is covered
5  * by the MIT license.
6  *
7  * Author: Jérôme Duval
8  */
9 
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "Keymap.h"
16 
17 
18 #ifdef HAIKU_HOST_PLATFORM_SUNOS
19 static const char *sProgramName = "keymap";
20 #else
21 extern char *__progname;
22 static const char *sProgramName = __progname;
23 #endif
24 
25 
26 static void
27 usage(void)
28 {
29 	printf("usage: %s {-o output_file} -[d|l|r|c|b|h input_file]\n"
30 		"  -o  change output file to output_file (default:keymap.out)\n"
31 		"  -d  dump key map to standard output\n"
32 		"  -l  load key map from standard input\n"
33 		"  -b  load binary key map from file\n"
34 		"  -r  restore system default key map\n"
35 		"  -c  compile source keymap to binary\n"
36 		"  -h  compile source keymap to header\n",
37 		sProgramName);
38 }
39 
40 
41 static const char *
42 keymap_error(status_t status)
43 {
44 	if (status == KEYMAP_ERROR_UNKNOWN_VERSION)
45 		return "Unknown keymap version";
46 
47 	return strerror(status);
48 }
49 
50 
51 static void
52 load_keymap_source(Keymap& keymap, const char* name)
53 {
54 	entry_ref ref;
55 	get_ref_for_path(name, &ref);
56 
57 	status_t status = keymap.LoadSourceFromRef(ref);
58 	if (status != B_OK) {
59 		fprintf(stderr, "%s: error when loading the keymap: %s\n",
60 			sProgramName, keymap_error(status));
61 		exit(1);
62 	}
63 }
64 
65 
66 int
67 main(int argc, char **argv)
68 {
69 	char operation = ' ';
70 	entry_ref outputRef;
71 	get_ref_for_path("keymap.out", &outputRef);
72 
73 	for (int i = 1; i < argc; i++) {
74 		if (strncmp(argv[i], "-", 1) == 0) {
75 			if (strlen(argv[i]) > 1)
76 				operation = argv[i][1];
77 			else
78 				break;
79 
80 			if (operation == 'd') {
81 				Keymap keymap;
82 				if (keymap.LoadCurrent() != B_OK) {
83 					fprintf(stderr, "%s: error while getting current keymap!\n", sProgramName);
84 					return 1;
85 				}
86 				keymap.Dump();
87 				return 0;
88 			} else if (operation == 'r') {
89 				Keymap keymap;
90 				keymap.RestoreSystemDefault();
91 				printf("System default key map restored.\n");
92 				return 0;
93 			} else if (operation == 'l') {
94 				Keymap keymap;
95 				status_t status = keymap.LoadSource(stdin);
96 				if (status != B_OK) {
97 					fprintf(stderr, "%s: error when loading the keymap: %s\n",
98 						sProgramName, keymap_error(status));
99 					return 1;
100 				}
101 				keymap.SaveAsCurrent();
102 				printf("Key map loaded.\n");
103 				return 0;
104 			}
105 		} else {
106 			// TODO: the actual action should be issued *AFTER* the command line
107 			//	has been parsed, not mixed in.
108 			if (operation == 'o') {
109 				get_ref_for_path(argv[i], &outputRef);
110 			} else if (operation == 'c') {
111 				Keymap keymap;
112 				load_keymap_source(keymap, argv[i]);
113 
114 				status_t status = keymap.Save(outputRef);
115 				if (status < B_OK) {
116 					fprintf(stderr, "%s: error saving \"%s\": %s\n", sProgramName,
117 						argv[i], strerror(status));
118 					return 1;
119 				}
120 				return 0;
121 			} else if (operation == 'h') {
122 		        Keymap keymap;
123 				load_keymap_source(keymap, argv[i]);
124 				keymap.SaveAsHeader(outputRef, argv[i]);
125 				return 0;
126 			} else if (operation == 'b') {
127 				entry_ref ref;
128 				get_ref_for_path(argv[i], &ref);
129 				Keymap keymap;
130 				status_t status = keymap.Load(ref);
131 				if (status != B_OK) {
132 					fprintf(stderr, "%s: error when loading the keymap: %s\n",
133 						sProgramName, keymap_error(status));
134 					return 1;
135 				}
136 				keymap.SaveAsCurrent();
137 				printf("Key map loaded.\n");
138 				return 0;
139 			} else
140 				break;
141 		}
142 	}
143 
144 	usage();
145 	return 1;
146 }
147