2 * Copyright 2004-2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
7 * Axel Dörfler, axeld@pinc-software.de.
16 #include <Application.h>
22 #ifdef HAIKU_HOST_PLATFORM_SUNOS
23 static const char *sProgramName
= "keymap";
25 extern char *__progname
;
26 static const char *sProgramName
= __progname
;
33 printf("usage: %s {-o <output-file>} [-[l|r] | -[b|h|c|d <input-file>]]\n"
34 " -o, --output Change output file to output-file (default: "
36 " -d, --dump Decompile key map to standard output (can be "
39 " -l, --load Load key map. If no input-file is specified, it "
41 " read from standard input.\n"
42 " -s, --load-source Load source key map from standard input when no\n"
43 " input-file is specified.\n"
44 " -r, --restore Restore system default key map.\n"
45 " -c, --compile Compile source keymap to binary.\n"
46 " -h, --header Translate source keymap to C++ header.\n",
52 keymap_error(status_t status
)
54 if (status
== KEYMAP_ERROR_UNKNOWN_VERSION
)
55 return "Unknown keymap version";
57 return strerror(status
);
62 load_keymap(Keymap
& keymap
, const char* name
, bool source
)
67 status
= keymap
.LoadSource(name
);
69 status
= keymap
.LoadSource(stdin
);
72 status
= keymap
.SetTo(name
);
74 BFileIO
fileIO(stdin
);
75 status
= keymap
.SetTo(fileIO
);
80 fprintf(stderr
, "%s: error when loading the keymap: %s\n", sProgramName
,
81 keymap_error(status
));
88 main(int argc
, char** argv
)
90 const char* output
= NULL
;
91 const char* input
= NULL
;
100 } mode
= kUnspecified
;
102 static struct option
const kLongOptions
[] = {
103 {"output", required_argument
, 0, 'o'},
104 {"dump", optional_argument
, 0, 'd'},
105 {"load", optional_argument
, 0, 'l'},
106 {"load-source", optional_argument
, 0, 's'},
107 {"restore", no_argument
, 0, 'r'},
108 {"compile", optional_argument
, 0, 'c'},
109 {"header", optional_argument
, 0, 'h'},
110 {"help", no_argument
, 0, 'H'},
115 while ((c
= getopt_long(argc
, argv
, "o:dblsrchH", kLongOptions
,
155 if (argc
> optind
&& input
== NULL
)
156 input
= argv
[optind
];
158 BApplication
app("application/x-vnd.Haiku-keymap-cli");
169 load_keymap(keymap
, input
, mode
== kLoadText
);
171 status_t status
= keymap
.SaveAsCurrent();
172 if (status
!= B_OK
) {
173 fprintf(stderr
, "%s: error when saving as current: %s",
174 sProgramName
, strerror(status
));
178 printf("Key map loaded.\n");
185 status_t status
= keymap
.SetToCurrent();
186 if (status
!= B_OK
) {
187 fprintf(stderr
, "%s: error while getting keymap: %s!\n",
188 sProgramName
, keymap_error(status
));
192 load_keymap(keymap
, input
, false);
195 keymap
.SaveAsSource(output
);
197 keymap
.SaveAsSource(stdout
);
202 keymap
.RestoreSystemDefault();
207 load_keymap(keymap
, input
, true);
210 output
= "keymap.out";
212 status_t status
= keymap
.Save(output
);
213 if (status
!= B_OK
) {
214 fprintf(stderr
, "%s: error saving \"%s\": %s\n", sProgramName
,
215 output
, strerror(status
));
223 load_keymap(keymap
, input
, true);
228 status_t status
= keymap
.SaveAsCppHeader(output
, input
);
229 if (status
!= B_OK
) {
230 fprintf(stderr
, "%s: error saving \"%s\": %s\n", sProgramName
,
231 output
, strerror(status
));