X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=src%2Fhookmacros.h;h=35e64703ddcc1933891ea8645d155c6edea9282f;hb=9862b8c8f13fe3d5a6012a8d67562bdaef48b8af;hp=469886978e3a6048d8943f92f7a516c6bc7be4ae;hpb=fe3f7d4c635b265c58efd5bc420d32135ec7903e;p=coloredstderr%2Fcoloredstderr.git diff --git a/src/hookmacros.h b/src/hookmacros.h index 4698869..35e6470 100644 --- a/src/hookmacros.h +++ b/src/hookmacros.h @@ -22,46 +22,89 @@ /* 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; \ - int saved_errno = errno; \ - 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); \ - } \ - errno = saved_errno; + } #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); \ - } \ - errno = saved_errno; -/* Save and restore the errno to make sure we return the errno of the original - * function call. */ + } #define _HOOK_POST_FD_(fd) \ if (handle) { \ - saved_errno = errno; \ handle_fd_post(fd); \ - errno = saved_errno; \ } #define _HOOK_POST_FD(fd) \ _HOOK_POST_FD_(fd) \ return result; #define _HOOK_POST_FILE(file) \ if (handle) { \ - saved_errno = errno; \ handle_file_post(file); \ - errno = saved_errno; \ } \ return result;