From 2d81f92fbadb0d60243cbcad7c4a5236815fdbbc Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 22 Aug 2025 13:06:08 -0400 Subject: Mac platform fixes --- Makefile | 4 ++-- bp.c | 16 +++++++++++++--- match.c | 3 ++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 9c646bb..b10125b 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ NAME=bp CC=cc PREFIX=/usr/local SYSCONFDIR=/etc -CFLAGS=-std=c11 -Werror -D_XOPEN_SOURCE=800 -D_POSIX_C_SOURCE=200809L -fPIC -flto=auto -fvisibility=hidden \ - -fsanitize=signed-integer-overflow -fno-sanitize-recover +CFLAGS=-std=c2x -Werror -D_GNU_SOURCE -fPIC -flto=auto -fvisibility=hidden \ + -fsanitize=signed-integer-overflow -fno-sanitize-recover -DSYSCONFDIR='"$(SYSCONFDIR)"' CWARN=-Wall -Wextra -Wno-format -Wshadow # -Wpedantic -Wsign-conversion -Wtype-limits -Wunused-result -Wnull-dereference \ # -Waggregate-return -Walloc-zero -Walloca -Warith-conversion -Wcast-align -Wcast-align=strict \ diff --git a/bp.c b/bp.c index 236aab0..c67669f 100644 --- a/bp.c +++ b/bp.c @@ -98,6 +98,16 @@ static inline void fprint_filename(FILE *out, const char *filename) else fprintf(out, "%s:\n", filename); } +static void *portable_memrchr(const void *s, int c, size_t n) +{ + const unsigned char *p = (const unsigned char *)s + n; + while (n--) { + if (*--p == (unsigned char)c) + return (void *)p; + } + return NULL; +} + // // If there was a parse error while building a pattern, print an error message and exit. // @@ -109,7 +119,7 @@ static inline bp_pat_t *assert_pat(const char *start, const char *end, maybe_pat *err_end = maybe_pat.value.error.end, *err_msg = maybe_pat.value.error.msg; - const char *nl = memrchr(start, '\n', (size_t)(err_start - start)); + const char *nl = portable_memrchr(start, '\n', (size_t)(err_start - start)); const char *sol = nl ? nl+1 : start; nl = memchr(err_start, '\n', (size_t)(end - err_start)); const char *eol = nl ? nl : end; @@ -547,7 +557,7 @@ int main(int argc, char *argv[]) bp_pat_t *pattern = NULL; // Load builtins: - file_t *builtins_file = load_file(&loaded_files, "/etc/"BP_NAME"/builtins.bp"); + file_t *builtins_file = load_file(&loaded_files, SYSCONFDIR"/"BP_NAME"/builtins.bp"); if (builtins_file) defs = load_grammar(defs, builtins_file); file_t *local_file = load_filef(&loaded_files, "%s/.config/"BP_NAME"/builtins.bp", getenv("HOME")); if (local_file) defs = load_grammar(defs, local_file); @@ -595,7 +605,7 @@ int main(int argc, char *argv[]) if (f == NULL) f = load_filef(&loaded_files, "%s/.config/"BP_NAME"/%s.bp", getenv("HOME"), flag); if (f == NULL) - f = load_filef(&loaded_files, "/etc/"BP_NAME"/%s.bp", flag); + f = load_filef(&loaded_files, SYSCONFDIR"/"BP_NAME"/%s.bp", flag); if (f == NULL) errx(EXIT_FAILURE, "Couldn't find grammar: %s", flag); defs = load_grammar(defs, f); // Keep in memory for debug output diff --git a/match.c b/match.c index e714a45..85cbccc 100644 --- a/match.c +++ b/match.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "match.h" #include "pattern.h" @@ -339,7 +340,7 @@ static bp_match_t *_next_match(match_ctx_t *ctx, const char *str, bp_pat_t *pat, if (!skip && first->type == BP_STRING && first->min_matchlen > 0) { char *found = ctx->ignorecase ? strcasestr(str, When(first, BP_STRING)->string) - : memmem(str, (size_t)(ctx->end - str), When(first, BP_STRING)->string, first->min_matchlen); + : strnstr(str, When(first, BP_STRING)->string, MIN((size_t)(ctx->end - str), first->min_matchlen)); str = found ? found : ctx->end; } else if (!skip && str > ctx->start && (first->type == BP_START_OF_LINE || first->type == BP_END_OF_LINE)) { char *found = memchr(str, '\n', (size_t)(ctx->end - str)); -- cgit v1.2.3