xref: /haiku/src/bin/keymap/main.cpp (revision 2ae568931fcac7deb9f1e6ff4e47213fbfe4029b)
1 /*
2  * Copyright (c) 2004-2005, Haiku
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 <string.h>
13 
14 #include "Keymap.h"
15 
16 
17 extern char *__progname;
18 static const char *sProgramName = __progname;
19 
20 
21 static void
22 usage(void)
23 {
24 	printf("usage: %s {-o output_file} -[d|l|r|c|b input_file]\n"
25 		"  -d  dump key map to standard output\n"
26 		"  -l  load key map from standard input\n"
27 		"  -b  load binary key map from file\n"
28 		"  -r  restore system default key map\n"
29 		"  -c  compile source keymap to binary\n"
30 		"  -h  compile source keymap to header\n"
31 		"  -o  change output file to output_file (default:keymap.out)\n",
32 		sProgramName);
33 }
34 
35 
36 int
37 main(int argc, char **argv)
38 {
39 	char operation = ' ';
40 	entry_ref outputRef;
41 	get_ref_for_path("keymap.out", &outputRef);
42 
43 	for (int i = 1; i < argc; i++) {
44 		if (strncmp(argv[i], "-", 1) == 0) {
45 			if (strlen(argv[i]) > 1)
46 				operation = argv[i][1];
47 			else
48 				break;
49 
50 			if (operation == 'd') {
51 				Keymap keymap;
52 				if (keymap.LoadCurrent() != B_OK)
53 					return 1;
54 				keymap.Dump();
55 				return 0;
56 			} else if (operation == 'r') {
57 				Keymap keymap;
58 				keymap.RestoreSystemDefault();
59 				printf("System default key map restored.\n");
60 				return 0;
61 			} else if (operation == 'l') {
62 				Keymap keymap;
63 				if (keymap.LoadSource(stdin)!=B_OK) {
64 					printf("error when loading the keymap\n");
65 					return 1;
66 				}
67 				keymap.SaveAsCurrent();
68 				printf("Key map loaded.\n");
69 				return 0;
70 			}
71 		} else {
72 			if (operation == 'o') {
73 				get_ref_for_path(argv[i], &outputRef);
74 			} else if (operation == 'c') {
75 				entry_ref ref;
76 				get_ref_for_path(argv[i], &ref);
77 				Keymap keymap;
78 				if (keymap.LoadSourceFromRef(ref)!=B_OK) {
79 					printf("error when loading the keymap\n");
80 					return 1;
81 				}
82 				keymap.Save(outputRef);
83 				return 0;
84 			} else if (operation == 'h') {
85 		        entry_ref ref;
86 		        get_ref_for_path(argv[i], &ref);
87 		        Keymap keymap;
88 		        if (keymap.LoadSourceFromRef(ref)!=B_OK) {
89 					printf("error when loading the keymap\n");
90 					return 1;
91 				}
92 				keymap.SaveAsHeader(outputRef);
93 				return 0;
94 			} else if (operation == 'b') {
95 				entry_ref ref;
96 				get_ref_for_path(argv[i], &ref);
97 				Keymap keymap;
98 				if (keymap.Load(ref)!=B_OK) {
99 					printf("error when loading the keymap\n");
100 					return 1;
101 				}
102 				keymap.SaveAsCurrent();
103 				printf("Key map loaded.\n");
104 				return 0;
105 			} else
106 				break;
107 		}
108 	}
109 
110 	usage();
111 	return 1;
112 }
113