aruu

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

commit 7c6c6a0dbb29180ae8b18145832ce6ddaa926f34
parent abd19f7e2d601e8272593483c103e35ae570fb4e
Author: xplshn <anto@xplshn.com.ar>
Date:   Mon, 23 Nov 2026 13:27:59 -0300

make: :=, $(@D), globs and g++ in names, blank lines in recipes. head: -n -num

Diffstat:
Mcmd/posix/head.c | 50++++++++++++++++++++++++++++++++++++++------------
Mcmd/posix/make/parser.c | 115++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 146 insertions(+), 19 deletions(-)

diff --git a/cmd/posix/head.c b/cmd/posix/head.c @@ -7,17 +7,34 @@ #include "util.h" +// with drop set, everything but the last n lines: a line is held back +// until n others came after it static void -head(FILE *fp, const char *fname, size_t n) +head(FILE *fp, const char *fname, size_t n, int drop) { - char *buf = NULL; - size_t i = 0, size = 0; + char *buf = NULL, **held = NULL; + size_t i = 0, size = 0, nheld = 0, at = 0; ssize_t len; - while (i < n && (len = getline(&buf, &size, fp)) > 0) { - fwrite(buf, 1, len, stdout); - i += (len && (buf[len - 1] == '\n')); + while ((drop || i < n) && (len = getline(&buf, &size, fp)) > 0) { + if (!drop) { + fwrite(buf, 1, len, stdout); + i += (len && (buf[len - 1] == '\n')); + continue; + } + if (nheld < n) { + held = ereallocarray(held, nheld + 1, sizeof(*held)); + held[nheld++] = estrdup(buf); + continue; + } + fputs(held[at], stdout); + free(held[at]); + held[at] = estrdup(buf); + at = (at + 1) % n; } + for (i = 0; i < nheld; i++) + free(held[i]); + free(held); free(buf); if (ferror(fp)) eprintf("getline %s:", fname); @@ -26,7 +43,7 @@ head(FILE *fp, const char *fname, size_t n) static void usage(void) { - eprintf("usage: %s [-num | -n num] [file ...]\n", argv0); + eprintf("usage: %s [-num | -n [-]num] [file ...]\n", argv0); } // ?man head: output first part of files @@ -37,14 +54,17 @@ main(int argc, char *argv[]) { size_t n = 10; FILE *fp; - int ret = 0, newline = 0, many = 0; + char *arg; + int ret = 0, newline = 0, many = 0, drop = 0; ARGBEGIN { // ?man -n:num: print line numbers or counts case 'n': - n = estrtonum( - EARGF(usage()), 0, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX) + arg = EARGF(usage()); + drop = arg[0] == '-'; + n = estrtonum( + arg + drop, 0, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX) ); break; // ?man ARGNUM: specify RGNUM option @@ -56,8 +76,14 @@ main(int argc, char *argv[]) } ARGEND + // dropping none is printing all + if (drop && !n) { + drop = 0; + n = SIZE_MAX; + } + if (!argc) { - head(stdin, "<stdin>", n); + head(stdin, "<stdin>", n, drop); } else { many = argc > 1; for (newline = 0; *argv; argc--, argv++) { @@ -75,7 +101,7 @@ main(int argc, char *argv[]) printf("==> %s <==\n", *argv); } newline = 1; - head(fp, *argv, n); + head(fp, *argv, n, drop); if (fp != stdin && fshut(fp, *argv)) ret = 1; } diff --git a/cmd/posix/make/parser.c b/cmd/posix/make/parser.c @@ -1,6 +1,8 @@ #include <assert.h> #include <ctype.h> #include <errno.h> +#include <glob.h> +#include <libgen.h> #include <limits.h> #include <stdarg.h> #include <stdio.h> @@ -13,6 +15,7 @@ #define TABSIZ 64 #define MAXTOKEN FILENAME_MAX #define ITEM 128 +#define IMMED 129 typedef struct macro Macro; @@ -732,6 +735,19 @@ validchar(int c) return c == '.' || c == '/' || c == '_' || c == '-' || isalnum(c); } +/* what a target, a dependency or a macro name in a rule line may hold. + * + and ? only end a name when they start += or ?=, so g++ and + * the glob patterns of other makes are names */ +static int +itemchar(int c) +{ + if (validchar(c)) + return 1; + if (c == '*' || c == '[' || c == ']' || c == '~' || c == ',') + return 1; + return (c == '+' || c == '?') && ahead() != '='; +} + static char * expandmacro(char *name) { @@ -885,6 +901,20 @@ internal(int ch) } } +/* $(@D) and $(@F) and their kin: the directory part of a name or the rest. + * the string given is freed */ +static char * +dirbase(char *name, int which) +{ + char *copy, *r; + + copy = estrdup(name); + r = estrdup(which == 'D' ? dirname(copy) : basename(copy)); + free(copy); + free(name); + return r; +} + static void expansion(Target *tp) { @@ -941,6 +971,10 @@ expansion(Target *tp) name[namei++] = c; break; case STINTERNAL: + if (c == 'D' || c == 'F') { + s = dirbase(s, c); + break; + } if (c == delim) goto no_replace; if (c != ':') @@ -1047,7 +1081,7 @@ item(void) c = nextc(); if (c == '$' && ahead() != '$') expansion(NULL); - else if (validchar(c)) + else if (itemchar(c)) *s++ = c; else break; @@ -1101,8 +1135,18 @@ repeat: comment(); c = '\n'; /* fallthrough */ - case ';': case ':': + /* := and ::= expand their text where they are read */ + if (ahead() == ':') + nextc(); + if (ahead() == '=') { + nextc(); + strcpy(token, ":="); + tok = IMMED; + break; + } + goto single; + case ';': case '=': case '\n': single: @@ -1111,23 +1155,25 @@ repeat: tok = c; break; case '+': - if (nextc() == '=') { + if (ahead() == '=') { + nextc(); token[0] = '+'; token[1] = '='; token[2] = '\0'; tok = '+'; break; } - error("unexpected character '+'"); + goto name; case '?': - if (nextc() == '=') { + if (ahead() == '=') { + nextc(); token[0] = '?'; token[1] = '='; token[2] = '\0'; tok = '?'; break; } - error("unexpected character '?'"); + goto name; case '!': if (nextc() == '=') { token[0] = '!'; @@ -1138,7 +1184,8 @@ repeat: } error("unexpected character '!'"); default: - if (!validchar(c)) + name: + if (!itemchar(c)) error("unexpected character '%c'", c); back(c); tok = item(); @@ -1220,6 +1267,34 @@ readcmd(void) return act; } +/* a dependency with glob characters stands for the files that match, + * as it does in the other makes. one that matches nothing stays as it + * is and fails as a missing file would */ +static int +globdeps(char ***deps, int n) +{ + char **out = NULL; + int i, j, m = 0; + glob_t g; + + for (i = 0; i < n; i++) { + if (strpbrk((*deps)[i], "*?[") && glob((*deps)[i], 0, NULL, &g) == 0) { + for (j = 0; j < (int)g.gl_pathc; j++) { + out = erealloc(out, (m + 1) * sizeof(char *)); + out[m++] = estrdup(g.gl_pathv[j]); + } + globfree(&g); + free((*deps)[i]); + continue; + } + out = erealloc(out, (m + 1) * sizeof(char *)); + out[m++] = (*deps)[i]; + } + free(*deps); + *deps = out; + return m; +} + static void rule(char *targets[], int ntargets) { @@ -1238,6 +1313,8 @@ rule(char *targets[], int ntargets) if (tok != '\n' && tok != ';') error("garbage at the end of the line"); + ndeps = globdeps(&deps, ndeps); + nactions = 0; acts = NULL; if (tok == ';') { @@ -1251,6 +1328,10 @@ rule(char *targets[], int ntargets) comment(); continue; } + /* a blank line does not end the commands: the makefiles of + * other makes put one between two of them */ + if (c == '\n') + continue; if (c != '\t') break; nactions++; @@ -1318,6 +1399,23 @@ assign_append(char *macros[], int where, int n) free(defs); } +/* := reads the text as it is now, so what a later line sets does not move it */ +static void +assign_immediate(char *macros[], int where, int n) +{ + char *defs, *text; + + if (n != 1) + error("invalid macro definition"); + + skipspaces(); + defs = readmacrodef(); + text = expandstring(defs, NULL, getloc()); + setmacro(*macros, text, where, NOEXPORT); + free(text); + free(defs); +} + /* ?= sets a macro only when nothing, the environment included, has */ static void assign_default(char *macros[], int where, int n) @@ -1402,6 +1500,9 @@ parseinput(int where) case '=': assign(targets, where, n); break; + case IMMED: + assign_immediate(targets, where, n); + break; case '+': assign_append(targets, where, n); break;