From 9862b8c8f13fe3d5a6012a8d67562bdaef48b8af Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Mon, 10 Jun 2013 05:55:51 +0200 Subject: [PATCH] Inline check_handle_fd() into hook macros. It's very small and prologue/epilogue have a lot of overhead. --- src/coloredstderr.c | 25 +------------------ src/hookmacros.h | 60 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 28 deletions(-) diff --git a/src/coloredstderr.c b/src/coloredstderr.c index 8775541..a95486d 100644 --- a/src/coloredstderr.c +++ b/src/coloredstderr.c @@ -60,31 +60,8 @@ static int force_write_to_non_tty; -/* Should the "action" handler be invoked for this file descriptor? */ -static int check_handle_fd(int fd) { - /* Load state from environment. Only necessary once per process. */ - if (!initialized) { - init_from_environment(); - } - - /* tracked_fds_find() is most likely faster than calling isatty(), - * therefore check if we are tracking this file descriptor first. */ - if (!tracked_fds_find(fd)) { - return 0; - } - - /* Never touch anything not going to a terminal - unless we are explicitly - * asked to do so. */ - if (force_write_to_non_tty) { - return 1; - } - - int saved_errno = errno; - int result = isatty(fd); - errno = saved_errno; +/* See hookmacros.h for the decision if a function call is colored. */ - return result; -} static void dup_fd(int oldfd, int newfd) { #ifdef DEBUG diff --git a/src/hookmacros.h b/src/hookmacros.h index f2a1fe8..35e6470 100644 --- a/src/hookmacros.h +++ b/src/hookmacros.h @@ -22,24 +22,76 @@ /* Hook the function by creating a function with the same name. With * LD_PRELOAD our function will be preferred. The original function is stored - * in a static variable (real_*). */ + * in a static variable (real_*). Any function called in these macros must + * make sure to restore the errno if it changes it. + * + * "Pseudo code" for the following macros. is the name of the hooked + * function, is either a file descriptor or a FILE pointer. + * + * if (!real_) { + * real_ = dlsym_function(); + * if (!initialized) { + * init_from_environment(); + * } + * } + * if (tracked_fds_find()) { + * if (force_write_to_non_tty) { + * handle = 1; + * } else { + * handle = isatty(); + * } + * } else { + * handle = 0; + * } + * + * if (handle) { + * handle__pre(); + * } + * result = real_(); + * if (handle) { + * handle__post(); + * } + * return result; + */ #define _HOOK_PRE(type, name) \ int handle; \ - DLSYM_FUNCTION(real_ ## name, #name); + if (!(real_ ## name )) { \ + *(void **) (&(real_ ## name)) = dlsym_function(#name); \ + /* Initialize our data while we're at it. */ \ + if (!initialized) { \ + init_from_environment(); \ + } \ + } #define _HOOK_PRE_FD(type, name, fd) \ type result; \ _HOOK_PRE_FD_(type, name, fd) #define _HOOK_PRE_FD_(type, name, fd) \ _HOOK_PRE(type, name) \ - handle = check_handle_fd(fd); \ + if (tracked_fds_find(fd)) { \ + if (force_write_to_non_tty) { \ + handle = 1; \ + } else { \ + handle = isatty(fd); \ + } \ + } else { \ + handle = 0; \ + } \ if (handle) { \ handle_fd_pre(fd); \ } #define _HOOK_PRE_FILE(type, name, file) \ type result; \ _HOOK_PRE(type, name) \ - handle = check_handle_fd(fileno(file)); \ + if (tracked_fds_find(fileno(file))) { \ + if (force_write_to_non_tty) { \ + handle = 1; \ + } else { \ + handle = isatty(fileno(file)); \ + } \ + } else { \ + handle = 0; \ + } \ if (handle) { \ handle_file_pre(file); \ } -- 2.45.2