From 37e0ed4ace909ec0236ca34949d52a73a7e7be42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 1 Jun 2006 10:05:56 +0000 Subject: [PATCH] * Improved error output. * Fixed some minor style issues. * fChars was allocated with new[] but deleted with free() in the Keymap destructor. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17686 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/bin/keymap/Keymap.cpp | 537 +++++++++++++++++++------------------- src/bin/keymap/Keymap.h | 84 +++--- src/bin/keymap/main.cpp | 55 ++-- 3 files changed, 349 insertions(+), 327 deletions(-) diff --git a/src/bin/keymap/Keymap.cpp b/src/bin/keymap/Keymap.cpp index d3571f0f27..5485021db1 100644 --- a/src/bin/keymap/Keymap.cpp +++ b/src/bin/keymap/Keymap.cpp @@ -1,32 +1,32 @@ -// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// -// Copyright (c) 2004, Haiku -// -// This software is part of the Haiku distribution and is covered -// by the Haiku license. -// -// -// File: Keymap.cpp -// Author: Jérôme Duval -// Description: keymap bin -// Created : July 29, 2004 -// -// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ +/* + * Copyright (c) 2004-2006, Haiku, Inc. + * + * This software is part of the Haiku distribution and is covered + * by the Haiku license. + * + * Author: Jérôme Duval + */ + #include "Keymap.h" -#include -#include -#include + #include #include #include #include #include +#include +#include +#include + + #define CHARS_TABLE_MAXSIZE 10000 + Keymap::Keymap() - : fChars(NULL), + : + fChars(NULL), fCharsSize(0) { } @@ -34,41 +34,40 @@ Keymap::Keymap() Keymap::~Keymap() { - if (fChars) - free(fChars); + delete[] fChars; } -void -Keymap::GetKey( char *chars, int32 offset, char* string) +void +Keymap::GetKey(char *chars, int32 offset, char* string) { int size = chars[offset++]; char str[32]; memset(str, 0, 32); memset(string, 0, 32); - - switch( size ) { - case 0: - // Not mapped - sprintf(str, "''"); - break; - - case 1: - // 1-byte UTF-8/ASCII character - if ((uint8)chars[offset] < 0x20 - || (uint8)chars[offset] > 0x7e) - sprintf(str, "0x%02x", (uint8)chars[offset]); - else - sprintf(str, "'%s%c'", - (chars[offset] == '\\' || chars[offset] == '\'') ? "\\" : "", chars[offset]); - break; - - default: - // n-byte UTF-8/ASCII character - sprintf(str, "0x"); - for (int i=0; i 0x7e) + sprintf(str, "0x%02x", (uint8)chars[offset]); + else + sprintf(str, "'%s%c'", + (chars[offset] == '\\' || chars[offset] == '\'') ? "\\" : "", chars[offset]); + break; + + default: + // n-byte UTF-8/ASCII character + sprintf(str, "0x"); + for (int i=0; i= 0) { sscanf(buffer + regs.start[1], "%ld", &fKeys.version); @@ -623,9 +618,10 @@ Keymap::LoadSource(FILE * f) sscanf(buffer + regs.start[1], "0x%lx", &fKeys.menu_key); } else if (re_search(&locksettingsBuf, buffer, strlen(buffer), 0, strlen(buffer), ®s) >= 0) { fKeys.lock_settings = 0; - for (int32 i=1; i<=3; i++) { + for (int32 i = 1; i <= 3; i++) { if (regs.end[i] - regs.start[i] <= 0) break; + if (strncmp(buffer + regs.start[i], "CapsLock", regs.end[i] - regs.start[i]) == 0) fKeys.lock_settings |= B_CAPS_LOCK; else if (strncmp(buffer + regs.start[i], "NumLock", regs.end[i] - regs.start[i]) == 0) @@ -636,37 +632,36 @@ Keymap::LoadSource(FILE * f) } else if (re_search(&keyBuf, buffer, strlen(buffer), 0, strlen(buffer), ®s) >= 0) { uint32 keyCode; if (sscanf(buffer + regs.start[1], "0x%lx", &keyCode) > 0) { - - for (int i=2; i<=10; i++) { - maps[i-2][keyCode] = offset; + for (int i = 2; i <= 10; i++) { + maps[i - 2][keyCode] = offset; ComputeChars(buffer, regs, i, offset); } } } else if (re_search(´Buf, buffer, strlen(buffer), 0, strlen(buffer), ®s) >= 0) { - for (int i=1; i<=2; i++) { + for (int i = 1; i <= 2; i++) { fKeys.acute_dead_key[acuteOffset++] = offset; ComputeChars(buffer, regs, i, offset); - } + } } else if (re_search(&graveBuf, buffer, strlen(buffer), 0, strlen(buffer), ®s) >= 0) { - for (int i=1; i<=2; i++) { + for (int i = 1; i <= 2; i++) { fKeys.grave_dead_key[graveOffset++] = offset; ComputeChars(buffer, regs, i, offset); - } + } } else if (re_search(&circumflexBuf, buffer, strlen(buffer), 0, strlen(buffer), ®s) >= 0) { - for (int i=1; i<=2; i++) { + for (int i = 1; i <= 2; i++) { fKeys.circumflex_dead_key[circumflexOffset++] = offset; ComputeChars(buffer, regs, i, offset); - } + } } else if (re_search(&diaeresisBuf, buffer, strlen(buffer), 0, strlen(buffer), ®s) >= 0) { - for (int i=1; i<=2; i++) { + for (int i = 1; i <= 2; i++) { fKeys.dieresis_dead_key[diaeresisOffset++] = offset; ComputeChars(buffer, regs, i, offset); - } + } } else if (re_search(&tildeBuf, buffer, strlen(buffer), 0, strlen(buffer), ®s) >= 0) { - for (int i=1; i<=2; i++) { + for (int i = 1; i <= 2; i++) { fKeys.tilde_dead_key[tildeOffset++] = offset; ComputeChars(buffer, regs, i, offset); - } + } } else if (re_search(´tabBuf, buffer, strlen(buffer), 0, strlen(buffer), ®s) >= 0) { ComputeTables(buffer, regs, fKeys.acute_tables); } else if (re_search(&gravetabBuf, buffer, strlen(buffer), 0, strlen(buffer), ®s) >= 0) { @@ -679,12 +674,12 @@ Keymap::LoadSource(FILE * f) ComputeTables(buffer, regs, fKeys.tilde_tables); } } - + fCharsSize = offset; if (fKeys.version != 3) - return B_ERROR; - + return KEYMAP_ERROR_UNKNOWN_VERSION; + return B_OK; } @@ -727,17 +722,17 @@ Keymap::Save(entry_ref &ref) const char header_header[] = -"/*\tHaiku \t*/\n" -"/*\n" -" This file is generated automatically. Don't edit ! \n" -"*/\n\n"; + "/*\tHaiku \t*/\n" + "/*\n" + " This file is generated automatically. Don't edit ! \n" + "*/\n\n"; void Keymap::SaveAsHeader(entry_ref &ref) { status_t err; - BFile file(&ref, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE ); + BFile file(&ref, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); if ((err = file.InitCheck()) != B_OK) { printf("error %s\n", strerror(err)); return; @@ -763,42 +758,46 @@ Keymap::SaveAsHeader(entry_ref &ref) fprintf(f, "\tright_option_key:0x%lx,\n", fKeys.right_option_key); fprintf(f, "\tmenu_key:0x%lx,\n", fKeys.menu_key); fprintf(f, "\tlock_settings:0x%lx,\n", fKeys.lock_settings); - + fprintf(f, "\tcontrol_map:{\n"); - for (uint32 i=0; i<128; i++) + for (uint32 i = 0; i < 128; i++) { fprintf(f, "\t\t%ld,\n", fKeys.control_map[i]); + } fprintf(f, "\t},\n"); - + fprintf(f, "\toption_caps_shift_map:{\n"); - for (uint32 i=0; i<128; i++) + for (uint32 i = 0; i < 128; i++) { fprintf(f, "\t\t%ld,\n", fKeys.option_caps_shift_map[i]); + } fprintf(f, "\t},\n"); - + fprintf(f, "\toption_caps_map:{\n"); - for (uint32 i=0; i<128; i++) + for (uint32 i = 0; i < 128; i++) { fprintf(f, "\t\t%ld,\n", fKeys.option_caps_map[i]); + } fprintf(f, "\t},\n"); - + fprintf(f, "\toption_shift_map:{\n"); - for (uint32 i=0; i<128; i++) + for (uint32 i = 0; i < 128; i++) { fprintf(f, "\t\t%ld,\n", fKeys.option_shift_map[i]); + } fprintf(f, "\t},\n"); - + fprintf(f, "\toption_map:{\n"); for (uint32 i=0; i<128; i++) fprintf(f, "\t\t%ld,\n", fKeys.option_map[i]); fprintf(f, "\t},\n"); - + fprintf(f, "\tcaps_shift_map:{\n"); for (uint32 i=0; i<128; i++) fprintf(f, "\t\t%ld,\n", fKeys.caps_shift_map[i]); fprintf(f, "\t},\n"); - + fprintf(f, "\tcaps_map:{\n"); for (uint32 i=0; i<128; i++) fprintf(f, "\t\t%ld,\n", fKeys.caps_map[i]); fprintf(f, "\t},\n"); - + fprintf(f, "\tshift_map:{\n"); for (uint32 i=0; i<128; i++) fprintf(f, "\t\t%ld,\n", fKeys.shift_map[i]); @@ -813,73 +812,77 @@ Keymap::SaveAsHeader(entry_ref &ref) for (uint32 i=0; i<32; i++) fprintf(f, "\t\t%ld,\n", fKeys.acute_dead_key[i]); fprintf(f, "\t},\n"); - + fprintf(f, "\tgrave_dead_key:{\n"); - for (uint32 i=0; i<32; i++) + for (uint32 i = 0; i < 32; i++) { fprintf(f, "\t\t%ld,\n", fKeys.grave_dead_key[i]); + } fprintf(f, "\t},\n"); - + fprintf(f, "\tcircumflex_dead_key:{\n"); - for (uint32 i=0; i<32; i++) + for (uint32 i = 0; i < 32; i++) { fprintf(f, "\t\t%ld,\n", fKeys.circumflex_dead_key[i]); + } fprintf(f, "\t},\n"); - + fprintf(f, "\tdieresis_dead_key:{\n"); - for (uint32 i=0; i<32; i++) + for (uint32 i = 0; i < 32; i++) { fprintf(f, "\t\t%ld,\n", fKeys.dieresis_dead_key[i]); + } fprintf(f, "\t},\n"); - + fprintf(f, "\ttilde_dead_key:{\n"); - for (uint32 i=0; i<32; i++) + for (uint32 i = 0; i < 32; i++) { fprintf(f, "\t\t%ld,\n", fKeys.tilde_dead_key[i]); + } fprintf(f, "\t},\n"); - + fprintf(f, "\tacute_tables:0x%lx,\n", fKeys.acute_tables); fprintf(f, "\tgrave_tables:0x%lx,\n", fKeys.grave_tables); fprintf(f, "\tcircumflex_tables:0x%lx,\n", fKeys.circumflex_tables); fprintf(f, "\tdieresis_tables:0x%lx,\n", fKeys.dieresis_tables); fprintf(f, "\ttilde_tables:0x%lx,\n", fKeys.tilde_tables); - + fprintf(f, "};\n\n"); - + fprintf(f, "const char sSystemKeyChars[] = {\n"); - for (uint32 i=0; i #include + #include -class Keymap -{ -public: - Keymap(); - ~Keymap(); - status_t LoadCurrent(); - status_t Load(entry_ref &ref); - status_t Save(entry_ref &ref); - status_t LoadSource(FILE *f); - status_t LoadSourceFromRef(entry_ref &ref); - void SaveAsCurrent(); - void SaveAsHeader(entry_ref &ref); - status_t Use(); - void Dump(); - bool IsModifierKey(uint32 keyCode); - uint8 IsDeadKey(uint32 keyCode, uint32 modifiers); - bool IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey); - void GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** chars, int32* numBytes); - void RestoreSystemDefault(); - static void GetKey( char *chars, int32 offset, char* string); -private: - void ComputeChars(const char *buffer, struct re_registers ®s, int i, int &offset); - void ComputeTables(const char *buffer, struct re_registers ®s, uint32 &table); - char *fChars; - key_map fKeys; - uint32 fCharsSize; + +class Keymap { + public: + Keymap(); + ~Keymap(); + + status_t LoadCurrent(); + status_t Load(entry_ref &ref); + status_t Save(entry_ref &ref); + status_t LoadSource(FILE *f); + status_t LoadSourceFromRef(entry_ref &ref); + void SaveAsCurrent(); + void SaveAsHeader(entry_ref &ref); + status_t Use(); + void Dump(); + bool IsModifierKey(uint32 keyCode); + uint8 IsDeadKey(uint32 keyCode, uint32 modifiers); + bool IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey); + void GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, + char** chars, int32* numBytes); + void RestoreSystemDefault(); + static void GetKey(char *chars, int32 offset, char* string); + + private: + void ComputeChars(const char *buffer, struct re_registers ®s, int i, int &offset); + void ComputeTables(const char *buffer, struct re_registers ®s, uint32 &table); + + char *fChars; + key_map fKeys; + uint32 fCharsSize; }; +#define KEYMAP_ERROR_UNKNOWN_VERSION (B_ERRORS_END + 1) -#endif //KEYMAP_H +#endif // KEYMAP_H diff --git a/src/bin/keymap/main.cpp b/src/bin/keymap/main.cpp index 7502506cba..88b84032f6 100644 --- a/src/bin/keymap/main.cpp +++ b/src/bin/keymap/main.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004-2005, Haiku + * Copyright (c) 2004-2006, Haiku, Inc. * * This software is part of the Haiku distribution and is covered * by the MIT license. @@ -33,6 +33,31 @@ usage(void) } +static const char * +keymap_error(status_t status) +{ + if (status == KEYMAP_ERROR_UNKNOWN_VERSION) + return "Unknown keymap version"; + + return strerror(status); +} + + +static void +load_keymap_source(Keymap& keymap, const char* name) +{ + entry_ref ref; + get_ref_for_path(name, &ref); + + status_t status = keymap.LoadSourceFromRef(ref); + if (status != B_OK) { + fprintf(stderr, "error when loading the keymap: %s\n", + keymap_error(status)); + exit(1); + } +} + + int main(int argc, char **argv) { @@ -60,8 +85,10 @@ main(int argc, char **argv) return 0; } else if (operation == 'l') { Keymap keymap; - if (keymap.LoadSource(stdin)!=B_OK) { - printf("error when loading the keymap\n"); + status_t status = keymap.LoadSource(stdin); + if (status != B_OK) { + fprintf(stderr, "error when loading the keymap: %s\n", + keymap_error(status)); return 1; } keymap.SaveAsCurrent(); @@ -69,34 +96,28 @@ main(int argc, char **argv) return 0; } } else { + // TODO: the actual action should be issued *AFTER* the command line + // has been parsed, not mixed in. if (operation == 'o') { get_ref_for_path(argv[i], &outputRef); } else if (operation == 'c') { - entry_ref ref; - get_ref_for_path(argv[i], &ref); Keymap keymap; - if (keymap.LoadSourceFromRef(ref)!=B_OK) { - printf("error when loading the keymap\n"); - return 1; - } + load_keymap_source(keymap, argv[i]); keymap.Save(outputRef); return 0; } else if (operation == 'h') { - entry_ref ref; - get_ref_for_path(argv[i], &ref); Keymap keymap; - if (keymap.LoadSourceFromRef(ref)!=B_OK) { - printf("error when loading the keymap\n"); - return 1; - } + load_keymap_source(keymap, argv[i]); keymap.SaveAsHeader(outputRef); return 0; } else if (operation == 'b') { entry_ref ref; get_ref_for_path(argv[i], &ref); Keymap keymap; - if (keymap.Load(ref)!=B_OK) { - printf("error when loading the keymap\n"); + status_t status = keymap.Load(ref); + if (status != B_OK) { + fprintf(stderr, "error when loading the keymap: %s\n", + keymap_error(status)); return 1; } keymap.SaveAsCurrent();