aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

commit 452ab77014edd523564b76cfe58b411b832ac12f
parent 7c6c6a0dbb29180ae8b18145832ce6ddaa926f34
Author: xplshn <anto@xplshn.com.ar>
Date:   Mon, 23 Nov 2026 13:35:49 -0300

make: macros after targets, escaped MAKEFLAGS, nested names, empty rules. grep: -o

Diffstat:
Mcmd/posix/grep.c | 71++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mcmd/posix/make/main.c | 100++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
Mcmd/posix/make/parser.c | 69++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
3 files changed, 213 insertions(+), 27 deletions(-)

diff --git a/cmd/posix/grep.c b/cmd/posix/grep.c @@ -31,6 +31,7 @@ static int eflag; static int fflag; static int hflag; static int iflag; +static int oflag; static int sflag; static int vflag; static int wflag; @@ -107,6 +108,64 @@ print_line(const char *str, const char *line, long line_no, char sep) } #endif +// the leftmost longest match of any pattern in text, its offset and length. +// notbol is set once part of the line is behind us, so ^ matches nothing +// in the middle of it +static int +firstmatch(const char *text, int notbol, size_t *at, size_t *len) +{ + struct pattern *pnode; + regmatch_t pm; + const char *hit; + size_t o, l; + int found = 0; + + SLIST_FOREACH(pnode, &phead, entry) + { + if (Fflag) { + if (!(hit = (iflag ? strcasestr : strstr)(text, pnode->pattern))) + continue; + o = hit - text; + l = strlen(pnode->pattern); + } else { + if (regexec(&pnode->preg, text, 1, &pm, notbol ? REG_NOTBOL : 0)) + continue; + o = pm.rm_so; + l = pm.rm_eo - pm.rm_so; + } + if (!found || o < *at || (o == *at && l > *len)) { + *at = o; + *len = l; + found = 1; + } + } + return found; +} + +// -o: each match on a line of its own, with the prefix a whole line has +static void +print_matches(const char *str, const char *line, long line_no) +{ + size_t at, len; + int notbol = 0; + + while (*line && firstmatch(line, notbol, &at, &len)) { + if (!len) { + // an empty match says nothing, step over a character + line++; + notbol = 1; + continue; + } + if (!hflag && (many || Hflag)) + printf("%s:", str); + if (mode == 'n') + printf("%ld:", line_no); + printf("%.*s\n", (int)len, line + at); + line += at + len; + notbol = 1; + } +} + static int grep(FILE *fp, const char *str) { @@ -190,6 +249,10 @@ grep(FILE *fp, const char *str) last_printed_line = n; } else { #endif + if (oflag && !vflag) { + print_matches(str, buf, n); + break; + } if (!hflag && (many || Hflag)) printf("%s:", str); if (mode == 'n') @@ -248,7 +311,7 @@ usage(void) { enprintf( Error, - "usage: %s [-EFHchilnqsvwx]" + "usage: %s [-EFHchilnoqsvwx]" #if FEATURE_GREP_CONTEXT " [-A num] [-B num] [-C num]" #endif @@ -274,6 +337,7 @@ main(int argc, char *argv[]) char *arg; SLIST_INIT(&phead); + argv0 = argv[0]; // egrep/fgrep: legacy invocation names many upstream scripts still // assume exist as their own commands rather than as grep -E/-F @@ -369,6 +433,11 @@ main(int argc, char *argv[]) flags |= REG_ICASE; iflag = 1; break; + case 'o': + // ?man -o: print only the matching parts of a line, each on a line of its own + oflag = 1; + flags &= ~REG_NOSUB; + break; case 's': // ?man -s: suppress error messages about nonexistent or unreadable files sflag = 1; diff --git a/cmd/posix/make/main.c b/cmd/posix/make/main.c @@ -1,3 +1,4 @@ +#include <ctype.h> #include <errno.h> #include <signal.h> #include <stdarg.h> @@ -93,33 +94,77 @@ getarg(char **args, char ***argv) return **argv; } +// MAKEFLAGS is a list of words, so a space inside one, as in CFLAGS=-O2 -g, +// goes in behind a backslash, and so does a backslash static void appendmakeflags(char *text) { - int n; - char *s, *t, *fmt; - - s = getmacro("MAKEFLAGS"); - fmt = *s ? "%s %s" : "%s%s"; - n = snprintf(NULL, 0, fmt, s, text); - - t = emalloc(n + 1); - snprintf(t, n + 1, fmt, s, text); + size_t n; + char *s, *t, *w; + + s = getmacro("MAKEFLAGS"); + n = strlen(s); + t = emalloc(n + 2 * strlen(text) + 2); + memcpy(t, s, n); + w = t + n; + if (n) + *w++ = ' '; + for (; *text; text++) { + if (*text == ' ' || *text == '\t' || *text == '\\') + *w++ = '\\'; + *w++ = *text; + } + *w = '\0'; setmacro("MAKEFLAGS", t, MAKEFLAGS, EXPORT); free(t); } +// the next word of a MAKEFLAGS list, cut out in place and its escapes +// undone. NULL at the end +static char * +flagword(char **list) +{ + char *start, *w; + + while (**list == ' ' || **list == '\t') + (*list)++; + if (!**list) + return NULL; + start = w = *list; + while (**list && **list != ' ' && **list != '\t') { + if (**list == '\\' && (*list)[1]) + (*list)++; + *w++ = *(*list)++; + } + if (**list) + (*list)++; + *w = '\0'; + return start; +} + static int hasargs(int c) { return c == 'f' || c == 'j'; } +// a bare -j, with nothing after it or something that is not a count, asks +// for as many jobs as there can be and takes no argument +static int +takesarg(int c, char **args, char ***argv) +{ + if (c != 'j') + return hasargs(c); + if ((*args)[1]) + return 1; + return argv && (*argv)[1] && isdigit((unsigned char)(*argv)[1][0]); +} + static void parseflag(int flag, char **args, char ***argv) { - if (hasargs(flag)) + if (takesarg(flag, args, argv)) getarg(args, argv); switch (flag) { @@ -211,11 +256,16 @@ assign(char *s, int where, int export) return 1; } +// macros may come after targets too, as they do in "make all CC=cc", so a +// command line is read to its end and what is not an option or a macro is a +// target. the options of MAKEFLAGS have no targets to collect static void parseargv(char **argv, char ***targets, int where, int export) { - char *s; - char *hm = NULL; + char *s; + char *hm = NULL; + char **found = NULL; + int n = 0; for (; *argv; ++argv) { s = *argv; @@ -224,16 +274,26 @@ parseargv(char **argv, char ***targets, int where, int export) } else if (hm && s[0] == '-') { break; } else if (s[0] != '-') { - if (!assign(s, where, export)) + if (assign(s, where, export)) + continue; + if (!targets) break; + found = erealloc(found, (n + 2) * sizeof(*found)); + found[n++] = s; + found[n] = NULL; continue; } while (hm == NULL && *++s) parseflag(*s, &s, &argv); } - if (targets) - *targets = argv; + if (targets) { + if (!found) { + found = emalloc(sizeof(*found)); + found[0] = NULL; + } + *targets = found; + } } static void @@ -258,7 +318,7 @@ parsemakeflags(void) } else { n = 0; arr = NULL; - for (s = strtok(flags, " \t"); s; s = strtok(NULL, " \t")) { + while ((s = flagword(&flags))) { n++; arr = erealloc(arr, sizeof(char *) * (n + 1)); arr[n - 1] = s; @@ -278,9 +338,11 @@ parsemakefiles(char **argv) int c, hasmake; hasmake = 0; - for (; *argv && **argv == '-'; ++argv) { + for (; *argv; ++argv) { + if (**argv != '-') + continue; for (s = *argv; (c = *s); ++s) { - if (hasargs(c)) + if (takesarg(c, &s, &argv)) arg = getarg(&s, &argv); if (c == 'f') { @@ -315,7 +377,7 @@ enadebug(char *argv[]) for (; *argv && **argv == '-'; ++argv) { p = *argv; for (++p; (c = *p); ++p) { - if (hasargs(c)) + if (takesarg(c, &p, &argv)) getarg(&p, &argv); if (c == 'd') dflag = 1; diff --git a/cmd/posix/make/parser.c b/cmd/posix/make/parser.c @@ -11,7 +11,7 @@ #include "make.h" -#define MAXREPL 30 +#define MAXREPL 256 #define TABSIZ 64 #define MAXTOKEN FILENAME_MAX #define ITEM 128 @@ -915,12 +915,54 @@ dirbase(char *name, int which) return r; } +/* a macro name that holds a macro, as in $(tag-static-$(static)): the inner + * one is read to its closing bracket and its value joins the name. the + * pieces of a $(name:from=to) hold them too, kept as text until used */ +static int +rawref(char *dst, int di, int max) +{ + int c, open, close, depth = 1; + + dst[di++] = '$'; + open = nextc(); + if (open != '(' && open != '{') + error("invalid macro name in expansion"); + close = open == '(' ? ')' : '}'; + dst[di++] = open; + while (depth && (c = nextc()) != EOF) { + if (di >= max - 2) + error("expansion text too long"); + depth += (c == open) - (c == close); + dst[di++] = c; + } + if (depth) + error("found eof while parsing expansion"); + dst[di] = '\0'; + return di; +} + +static int +nested(char *name, int namei, Target *tp) +{ + char ref[MAXTOKEN], *val; + int n; + + rawref(ref, 0, MAXTOKEN); + val = expandstring(ref, tp, getloc()); + n = strlen(val); + if (namei + n >= MAXTOKEN - 1) + error("expansion text too long"); + memcpy(name + namei, val, n); + free(val); + return namei + n; +} + static void expansion(Target *tp) { int delim, c, repli, toi, namei, st; char name[MAXTOKEN], repl[MAXREPL], to[MAXREPL]; - char *s, *erepl; + char *s, *erepl, *eto; c = nextc(); if (c == '(') @@ -966,7 +1008,11 @@ expansion(Target *tp) break; } - if (!validchar(c)) + if (c == '$') { + namei = nested(name, namei, tp); + break; + } + if (!validchar(c) && c != '+') error("invalid macro name in expansion"); name[namei++] = c; break; @@ -991,6 +1037,10 @@ expansion(Target *tp) "invalid replacement pattern in " "expansion" ); + if (c == '$') { + repli = rawref(repl, repli, MAXREPL); + break; + } if (repli == MAXREPL - 1) error("macro replacement too big"); repl[repli++] = c; @@ -1001,6 +1051,10 @@ expansion(Target *tp) break; } + if (c == '$') { + toi = rawref(to, toi, MAXREPL); + break; + } if (toi == MAXREPL - 1) error("macro substiturion too big"); to[toi++] = c; @@ -1015,8 +1069,10 @@ expansion(Target *tp) to[toi] = '\0'; erepl = expandstring(repl, tp, getloc()); - replace(s, erepl, to); + eto = expandstring(to, tp, getloc()); + replace(s, erepl, eto); + free(eto); free(erepl); free(s); return; @@ -1302,9 +1358,8 @@ rule(char *targets[], int ntargets) struct action *acts; char **deps = NULL; - if (ntargets == 0) - error("missing target"); - + // a name that expands to nothing, as ${SO}: does when SO is unset, makes + // a rule that is not there for (ndeps = 0; next() == ITEM; ++ndeps) { deps = erealloc(deps, (ndeps + 1) * sizeof(char *)); deps[ndeps] = estrdup(token);