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