diff options
| author | Bruce Hill <bruce@stainless.com> | 2025-08-22 13:06:08 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@stainless.com> | 2025-08-22 13:06:08 -0400 |
| commit | 2d81f92fbadb0d60243cbcad7c4a5236815fdbbc (patch) | |
| tree | 99284bfcc320c102941a4d885b195d182899821f | |
| parent | 20c11b29b3a63c221cac942a17bf9abcf8b9bafe (diff) | |
Mac platform fixes
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | bp.c | 16 | ||||
| -rw-r--r-- | match.c | 3 |
3 files changed, 17 insertions, 6 deletions
@@ -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 \ @@ -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 @@ -10,6 +10,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/param.h> #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)); |
