diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-12-08 22:39:07 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-12-08 22:39:07 +0100 |
commit | 2064b68718ea5f15de76d661ae1f307d67a0cf26 (patch) | |
tree | 3b8f93932cb4a13ab6001f165de7714ec7e86848 /src/mds-kkbd.c | |
parent | update the texinfo manual for the new use of fail_if (diff) | |
download | mds-2064b68718ea5f15de76d661ae1f307d67a0cf26.tar.gz mds-2064b68718ea5f15de76d661ae1f307d67a0cf26.tar.bz2 mds-2064b68718ea5f15de76d661ae1f307d67a0cf26.tar.xz |
with a few exceptions and some remaining files, never return directly on failure, always goto fail by invoking fail_if
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/mds-kkbd.c')
-rw-r--r-- | src/mds-kkbd.c | 202 |
1 files changed, 112 insertions, 90 deletions
diff --git a/src/mds-kkbd.c b/src/mds-kkbd.c index 95b7f02..1823f5c 100644 --- a/src/mds-kkbd.c +++ b/src/mds-kkbd.c @@ -232,20 +232,12 @@ int initialise_server(void) "\n" KEYBOARD_ID "\n"; - fail_if (open_leds() < 0); - stage = 1; - fail_if (open_input() < 0); - stage = 2; - fail_if (pthread_mutex_init(&send_mutex, NULL)); - stage = 3; - fail_if (pthread_mutex_init(&mapping_mutex, NULL)); - stage = 4; - - if (full_send(message, strlen(message))) - return 1; - - fail_if (server_initialised()); - stage = 5; + fail_if (open_leds() < 0); stage++; + fail_if (open_input() < 0); stage++; + fail_if (pthread_mutex_init(&send_mutex, NULL)); stage++; + fail_if (pthread_mutex_init(&mapping_mutex, NULL)); stage++; + fail_if (full_send(message, strlen(message))); + fail_if (server_initialised()); stage++; fail_if (mds_message_initialise(&received)); return 0; @@ -257,11 +249,9 @@ int initialise_server(void) if (stage >= 2) close_input(); if (stage >= 1) close_leds(); } - if (stage >= 3) - pthread_mutex_destroy(&send_mutex); - if (stage >= 4) - pthread_mutex_destroy(&mapping_mutex); - mds_message_destroy(&received); + if (stage >= 3) pthread_mutex_destroy(&send_mutex); + if (stage >= 4) pthread_mutex_destroy(&mapping_mutex); + if (stage >= 5) mds_message_destroy(&received); return 1; } @@ -277,13 +267,12 @@ int postinitialise_server(void) if (connected) return 0; - if (reconnect_to_display()) - { - mds_message_destroy(&received); - return 1; - } + fail_if (reconnect_to_display()); connected = 1; return 0; + fail: + mds_message_destroy(&received); + return 1; } @@ -540,19 +529,23 @@ int handle_message(void) if (recv_command == NULL) return 0; /* How did that get here, not matter, just ignore it? */ - + +#define t(expr) do { fail_if (expr); return 0; } while (0) if (strequals(recv_command, "enumerate-keyboards")) - return handle_enumerate_keyboards(recv_client_id, recv_message_id, recv_modify_id); + t (handle_enumerate_keyboards(recv_client_id, recv_message_id, recv_modify_id)); if (strequals(recv_command, "keyboard-enumeration")) - return handle_keyboard_enumeration(recv_modify_id); + t (handle_keyboard_enumeration(recv_modify_id)); if (strequals(recv_command, "set-keyboard-leds")) - return handle_set_keyboard_leds(recv_active, recv_mask, recv_keyboard); + t (handle_set_keyboard_leds(recv_active, recv_mask, recv_keyboard)); if (strequals(recv_command, "get-keyboard-leds")) - return handle_get_keyboard_leds(recv_client_id, recv_message_id, recv_keyboard); + t (handle_get_keyboard_leds(recv_client_id, recv_message_id, recv_keyboard)); if (strequals(recv_command, "keycode-map")) - return handle_keycode_map(recv_client_id, recv_message_id, recv_action, recv_keyboard); + t (handle_keycode_map(recv_client_id, recv_message_id, recv_action, recv_keyboard)); +#undef t return 0; /* How did that get here, not matter, just ignore it? */ + fail: + return -1; } @@ -569,15 +562,13 @@ static int ensure_send_buffer_size(size_t size) if (send_buffer_size >= size) return 0; - if (xrealloc(send_buffer, size, char)) - { - send_buffer = old; - return -1; - } - else - send_buffer_size = size; + fail_if (xrealloc(send_buffer, size, char)); + send_buffer_size = size; return 0; + fail: + send_buffer = old; + return -1; } @@ -613,8 +604,7 @@ int handle_enumerate_keyboards(const char* recv_client_id, const char* recv_mess message_id = message_id == UINT32_MAX ? 0 : (message_id + 1); ); - if (ensure_send_buffer_size(48 + strlen(recv_modify_id) + 1) < 0) - return -1; + fail_if (ensure_send_buffer_size(48 + strlen(recv_modify_id) + 1) < 0); sprintf(send_buffer, "Modify: no\n" "Modify ID: %s\n" @@ -624,8 +614,10 @@ int handle_enumerate_keyboards(const char* recv_client_id, const char* recv_mess with_mutex (send_mutex, r = full_send(send_buffer, strlen(send_buffer)); + if (r) r = errno ? errno : -1; ); - return r; + fail_if (errno = (r == -1 ? 0 : r), r); + return 0; } with_mutex (send_mutex, @@ -636,8 +628,7 @@ int handle_enumerate_keyboards(const char* recv_client_id, const char* recv_mess n = 176 + 3 * sizeof(size_t) + strlen(KEYBOARD_ID); n += strlen(recv_modify_id) + strlen(recv_message_id); - if (ensure_send_buffer_size(n + 1) < 0) - return -1; + fail_if (ensure_send_buffer_size(n + 1) < 0); sprintf(send_buffer, "Modify: yes\n" "Modify ID: %s\n" @@ -656,8 +647,12 @@ int handle_enumerate_keyboards(const char* recv_client_id, const char* recv_mess with_mutex (send_mutex, r = full_send(send_buffer, strlen(send_buffer)); + if (r) r = errno ? errno : -1; ); - return r; + fail_if (errno = (r == -1 ? 0 : r), r); + return 0; + fail: + return -1; } @@ -687,8 +682,7 @@ int handle_keyboard_enumeration(const char* recv_modify_id) n += off = 64 + strlen(recv_modify_id) + 3 * sizeof(size_t); - if (ensure_send_buffer_size(n + 1) < 0) - return -1; + fail_if (ensure_send_buffer_size(n + 1) < 0); with_mutex (send_mutex, msgid = message_id; @@ -732,9 +726,13 @@ int handle_keyboard_enumeration(const char* recv_modify_id) with_mutex (send_mutex, r = full_send(send_buffer + off, n); + if (r) r = errno ? errno : -1; ); - return r; + fail_if (errno = (r == -1 ? 0 : r), r); + return 0; + fail: + return -1; } @@ -854,7 +852,7 @@ int handle_get_keyboard_leds(const char* recv_client_id, const char* recv_messag int error = errno; xperror(*argv); send_errno(error, recv_client_id, recv_message_id); - return -1; + fail_if (errno = error, 1); } with_mutex (send_mutex, @@ -864,8 +862,7 @@ int handle_get_keyboard_leds(const char* recv_client_id, const char* recv_messag n = 95 + 3 * sizeof(size_t) + 2 * strlen(PRESENT_LEDS); n += strlen(recv_client_id) + strlen(recv_message_id); - if (ensure_send_buffer_size(n + 1) < 0) - return -1; + fail_if (ensure_send_buffer_size(n + 1) < 0); sprintf(send_buffer, "To: %s\n" "In response to: %s\n" @@ -886,9 +883,14 @@ int handle_get_keyboard_leds(const char* recv_client_id, const char* recv_messag with_mutex (send_mutex, r = full_send(send_buffer, strlen(send_buffer)); + if (r) r = errno ? errno : -1; ); - return r; + fail_if (errno = (r == -1 ? 0 : r), r); + return 0; + fail: + xperror(*argv); + return -1; } @@ -913,10 +915,7 @@ static int parse_remap_line(char* begin, char* end, size_t n, int* restrict in, return 0; if (delimiter == NULL) - { - *in = -1, *out = -1; - return -1; - } + fail_if (*in = -1, *out = -1); *delimiter++ = '\0'; *in = atoi(begin); @@ -933,6 +932,8 @@ static int parse_remap_line(char* begin, char* end, size_t n, int* restrict in, } return 1; + fail: + return -1; } @@ -954,10 +955,7 @@ static int add_mapping(int in, int out) return 0; if (old = mapping, xrealloc(mapping, n, int)) - { - mapping = old; - return -1; - } + fail_if (mapping = old, 1); for (; mapping_size < n; mapping_size++) mapping[mapping_size] = (int)mapping_size; @@ -965,6 +963,8 @@ static int add_mapping(int in, int out) mapping[in] = out; return 0; + fail: + return -1; } @@ -997,8 +997,7 @@ static int remap(char* table, size_t n) if (in != out) greatest_remap = max(greatest_remap, in); else greatest_reset = max(greatest_reset, in); - if (add_mapping(in, out) < 0) - return -1; + fail_if (add_mapping(in, out) < 0); next: if (end == NULL) @@ -1012,6 +1011,8 @@ static int remap(char* table, size_t n) shrink_map(); return 0; + fail: + return -1; } @@ -1037,8 +1038,7 @@ static int mapping_query(const char* recv_client_id, const char* recv_message_id n *= 3 + (size_t)(greatest > 0x00FF ? 5 : 3); - if (ensure_send_buffer_size(top + n + 2) < 0) - return -1; + fail_if (ensure_send_buffer_size(top + n + 2) < 0); with_mutex (send_mutex, msgid = message_id; @@ -1069,9 +1069,13 @@ static int mapping_query(const char* recv_client_id, const char* recv_message_id with_mutex (send_mutex, r = full_send(send_buffer + off, top + n); + if (r) r = errno ? errno : -1; ); - return r; + fail_if (errno = (r == -1 ? 0 : r), r); + return 0; + fail: + return -1; } @@ -1088,8 +1092,7 @@ static int mapping_query(const char* recv_client_id, const char* recv_message_id int handle_keycode_map(const char* recv_client_id, const char* recv_message_id, const char* recv_action, const char* recv_keyboard) { - int r = 0; - + int r; if ((recv_keyboard != NULL) && !strequals(recv_keyboard, KEYBOARD_ID)) return 0; @@ -1105,7 +1108,9 @@ int handle_keycode_map(const char* recv_client_id, const char* recv_message_id, with_mutex (mapping_mutex, r = remap(received.payload, received.payload_size); + if (r) r = errno ? errno : -1; ); + fail_if (errno = (r == -1 ? 0 : r), r); } else if (strequals(recv_action, "reset")) { @@ -1122,12 +1127,14 @@ int handle_keycode_map(const char* recv_client_id, const char* recv_message_id, return 0; } - r = mapping_query(recv_client_id, recv_message_id); + fail_if (mapping_query(recv_client_id, recv_message_id)); } else eprint("received keycode map request with invalid action, ignoring."); - return r; + return 0; + fail: + return -1; } @@ -1168,15 +1175,15 @@ int full_send(const char* message, size_t length) eprint("Sent more of a message than exists in the message, aborting."); return -1; } - else if ((sent < length) && (errno != EINTR)) - { - xperror(*argv); - return -1; - } + else + fail_if ((sent < length) && (errno != EINTR)); message += sent; length -= sent; } return 0; + fail: + xperror(*argv); + return -1; } @@ -1188,17 +1195,19 @@ int full_send(const char* message, size_t length) int open_leds(void) { #ifdef __sparc__ - if ((ledfd = open(SPARC_KBD, O_RDONLY)) < 0) - return -1; + fail_if ((ledfd = open(SPARC_KBD, O_RDONLY)) < 0); if (ioctl(ledfd, GET_LED, &saved_leds) < 0) { close(ledfd); - return -1; + fail_if (1); } return 0; #else - return ioctl(ledfd, GET_LED, &saved_leds); + fail_if (ioctl(ledfd, GET_LED, &saved_leds)); #endif + return 0; + fail: + return -1; } @@ -1223,12 +1232,13 @@ void close_leds(void) int get_leds(void) { int leds; - if (ioctl(ledfd, GET_LED, &leds) < 0) - return -1; + fail_if (ioctl(ledfd, GET_LED, &leds) < 0); #ifdef __sparc__ leds &= 15; #endif return leds; + fail: + return -1; } @@ -1240,7 +1250,10 @@ int get_leds(void) */ int set_leds(int leds) { - return ioctl(ledfd, SET_LED, leds); + fail_if (ioctl(ledfd, SET_LED, leds)); + return 0; + fail: + return -1; } @@ -1257,16 +1270,15 @@ int open_input(void) stty = saved_stty; stty.c_lflag &= (tcflag_t)~(ECHO | ICANON | ISIG); stty.c_iflag = 0; - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &stty) < 0) - return -1; + fail_if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &stty) < 0); /* K_MEDIUMRAW: utilise keyboard drivers, but not layout */ if ((ioctl(STDIN_FILENO, KDGKBMODE, &saved_kbd_mode) < 0) || (ioctl(STDIN_FILENO, KDSKBMODE, K_MEDIUMRAW) < 0)) - { - xperror(*argv); - return tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_stty); - } + fail_if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_stty)); return 0; + fail: + xperror(*argv); + return -1; } @@ -1337,8 +1349,12 @@ int send_key(int* restrict scancode, int trio) with_mutex (send_mutex, r = full_send(key_send_buffer, strlen(key_send_buffer)); + if (r) r = errno ? errno : 0; ); - return r; + fail_if (errno = (r == -1 ? 0 : r), r); + return 0; + fail: + return -1; } @@ -1412,7 +1428,10 @@ int fetch_keys(void) } } - return errno == 0 ? 0 : -1; + fail_if (errno); + return 0; + fail: + return -1; } @@ -1429,8 +1448,7 @@ int send_errno(int error, const char* recv_client_id, const char* recv_message_i size_t n = 79 + strlen(recv_client_id) + strlen(recv_message_id) + 3 * sizeof(int); int r; - if (ensure_send_buffer_size(n + 1) < 0) - return -1; + fail_if (ensure_send_buffer_size(n + 1) < 0); with_mutex (send_mutex, sprintf(send_buffer, @@ -1444,8 +1462,12 @@ int send_errno(int error, const char* recv_client_id, const char* recv_message_i message_id = message_id == INT32_MAX ? 0 : (message_id + 1); r = full_send(send_buffer, strlen(send_buffer)); + if (r) r = errno ? errno : -1; ); - return r; + fail_if (errno = (r == -1 ? 0 : r), r); + return 0; + fail: + return -1; } |