diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-11-01 00:49:32 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-11-01 00:49:32 +0100 |
commit | 500892b2ae64676d1855d8a357cd39e8a9e7f6c2 (patch) | |
tree | 2ab4ea40675d15a66d4a0c4295818f9f83e8c1b1 /src | |
parent | 26a95b35414d0d4b729f3fd5da010debb70b412b (diff) |
Add support for $GRAB_TABSIZE
Diffstat (limited to 'src')
-rw-r--r-- | src/flags.h | 24 | ||||
-rw-r--r-- | src/globals.h | 51 | ||||
-rw-r--r-- | src/main.c | 13 | ||||
-rw-r--r-- | src/tpool.c | 35 | ||||
-rw-r--r-- | src/util.c | 20 | ||||
-rw-r--r-- | src/util.h | 1 | ||||
-rw-r--r-- | src/work.c | 9 | ||||
-rw-r--r-- | src/work.h | 12 |
8 files changed, 84 insertions, 81 deletions
diff --git a/src/flags.h b/src/flags.h deleted file mode 100644 index fbd957d..0000000 --- a/src/flags.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef GRAB_FLAGS_H -#define GRAB_FLAGS_H - -typedef struct { - bool b : 1; - bool c : 1; - bool i : 1; - bool l : 1; - bool p : 1; - bool s : 1; - bool U : 1; - bool z : 1; - -#if !GIT_GRAB - bool do_header : 1; -#endif -} flags_t; - -#if !MAIN_C -extern -#endif -flags_t flags; - -#endif /* !GRAB_FLAGS_H */ diff --git a/src/globals.h b/src/globals.h new file mode 100644 index 0000000..9475bc3 --- /dev/null +++ b/src/globals.h @@ -0,0 +1,51 @@ +#ifndef GRAB_GLOBALS_H +#define GRAB_GLOBALS_H + +#include <stdatomic.h> + +#include <pcre2.h> + +#include "exitcodes.h" + +typedef struct { + char c; + pcre2_code *re; +#if DEBUG + bool free_me; +#endif +} op_t; + +typedef struct { + bool b : 1; + bool c : 1; + bool i : 1; + bool l : 1; + bool p : 1; + bool s : 1; + bool U : 1; + bool z : 1; + +#if !GIT_GRAB + bool do_header : 1; +#endif +} flags_t; + +#if MAIN_C + #define maybe_extern +#else + #define maybe_extern extern +#endif + +maybe_extern flags_t flags; +maybe_extern int grab_tabsize; +maybe_extern op_t *ops; +maybe_extern typeof(pcre2_match) *pcre2_match_fn; +#if MAIN_C + atomic_int rv = EXIT_NOMATCH; + const char *lquot = "`", *rquot = "'"; +#else + extern atomic_int rv; + extern const char *lquot, *rquot; +#endif + +#endif /* !GRAB_GLOBALS_H */
\ No newline at end of file @@ -23,7 +23,7 @@ #include "work.h" #define MAIN_C 1 -#include "flags.h" +#include "globals.h" static bool use_color_p(void); static op_t *pattern_comp(u8view_t pat); @@ -31,15 +31,6 @@ static op_t *pattern_comp(u8view_t pat); static FILE *getfstream(int globc, char **globv); #endif -atomic_int rv = EXIT_NOMATCH; -op_t *ops; -/* For use in diagnostic messages */ -const char *lquot = "`", *rquot = "'"; - -/* We need to use different matching functions depending on if we’re using JIT - matching or not */ -typeof(pcre2_match) *pcre2_match_fn; - /* TODO: Use the LUT in work.c */ static const bool opchars[] = { ['g'] = true, @@ -72,6 +63,8 @@ main(int argc, char **argv) rquot = u8"’"; } + grab_tabsize = getenv_posnum("GRAB_TABSIZE", 8); + optparser_t parser = mkoptparser(argv); static const cli_opt_t opts[] = { {'b', U8C("byte-offset"), CLI_NONE}, diff --git a/src/tpool.c b/src/tpool.c index 7fe93b8..3e8a0de 100644 --- a/src/tpool.c +++ b/src/tpool.c @@ -13,7 +13,9 @@ #include <errors.h> #include <macros.h> +#include "globals.h" #include "tpool.h" +#include "util.h" #include "work.h" static int nproc(void); @@ -21,37 +23,12 @@ static void *tpwork(void *); static pthread_t thread_buffer[32]; -extern const char *lquot, *rquot; - int nproc(void) { - errno = 0; - - /* Grab the number of processors available on the users system. If we can - we query sysconf() but fallback to 1 for systems that don’t support the - sysconf() method. The user can also override this via the GRAB_NPROCS - environment variable, and if that’s invalid then we just issue a - diagnostic and default to 1. - - We don’t want to error on an invalid value for GRAB_NPROCS because we - might be running this tool as part of an editor plugin for example where - finding the root cause of your regexp-search failing may not be so - trivial. */ - - const char *ev = getenv("GRAB_NPROCS"); - if (ev != nullptr && *ev != 0) { - const char *endptr; - long n = strtol(ev, (char **)&endptr, 10); - if (errno == 0 && *endptr == 0) - return (int)n; - if (errno != 0) - warn("strtol: %s:", ev); - if (*endptr != 0) - warn("Invalid value for %s%s%s for GRAB_NPROCS", lquot, ev, rquot); - return 1; - } - + int np = getenv_posnum("GRAB_NPROCS", -1); + if (np != -1) + return np; #ifdef _SC_NPROCESSORS_ONLN return (int)sysconf(_SC_NPROCESSORS_ONLN); #else @@ -121,4 +98,4 @@ tpwork(void *arg) array_free(buf); #endif return nullptr; -} +}
\ No newline at end of file @@ -1,10 +1,12 @@ #include <stddef.h> #include <stdlib.h> +#include <errno.h> #include <errors.h> #include <pcre2.h> #include "exitcodes.h" +#include "globals.h" void pcre2_bitch_and_die(int ec, const char *fmt) @@ -21,4 +23,22 @@ pcre2_bitch_and_die(int ec, const char *fmt) } else cerr(EXIT_FATAL, fmt, buf); } +} + +int +getenv_posnum(const char *ev, int fallback) +{ + const char *s = getenv(ev); + if (s != nullptr && *s != 0) { + const char *endptr; + errno = 0; + long n = strtol(s, (char **)&endptr, 10); + if (errno != 0) + warn("strtol: %s:", s); + else if (*endptr != 0 || n <= 0) + warn("invalid value %s%s%s for %s", lquot, s, rquot, ev); + else + return (int)n; + } + return fallback; }
\ No newline at end of file @@ -1,6 +1,7 @@ #ifndef GRAB_UTIL_H #define GRAB_UTIL_H +int getenv_posnum(const char *ev, int fallback); void pcre2_bitch_and_die(int ec, const char *fmt); #endif /* !GRAB_UTIL_H */
\ No newline at end of file @@ -21,7 +21,7 @@ #include <unicode/string.h> #include "exitcodes.h" -#include "flags.h" +#include "globals.h" #include "util.h" #include "work.h" @@ -64,11 +64,6 @@ static typeof(operator_dispatch) *operators[] = { ['X'] = operator_X, }; -extern atomic_int rv; -extern op_t *ops; -extern bool cflag; -extern typeof(pcre2_match) *pcre2_match_fn; - void @@ -454,7 +449,7 @@ compute_pos(const char8_t *p, pos_state_t *ps) ps->row++; ps->col = 0; } else - ps->col = ucswdth(g, ps->col, 8); /* TODO: Configurable tabsize? */ + ps->col = ucswdth(g, ps->col, grab_tabsize); } } @@ -1,16 +1,6 @@ #ifndef GRAB_WORK_H #define GRAB_WORK_H -#include <pcre2.h> - -typedef struct { - char c; - pcre2_code *re; -#if DEBUG - bool free_me; -#endif -} op_t; - void process_file(const char *filename, unsigned char **buf); -#endif /* !GRAB_WORK_H */ +#endif /* !GRAB_WORK_H */
\ No newline at end of file |