diff options
author | Leah Rowe <leah@libreboot.org> | 2025-10-04 09:14:33 +0100 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2025-10-04 09:20:12 +0100 |
commit | e9a910b33c7837b4b868e3abda18eb4810df7f02 (patch) | |
tree | 749e1830cb0607952df1a1afc0ae09ec1db54140 /util/sbase/fold.c | |
parent | 2cfaba181b3c68761871fa47b32725c934423c14 (diff) |
config/git: import suckless sbase
i currently use the output of sha512sum in several
places of xbmk, which is a bit unreliable in case
output changes.
other cases where i use util outputs in variables
are probably reliable, because i'm using mostly
posix utilities in those.
to mitigate this, i now import suckless sbase, which
has a reasonable sha512sum implementation.
*every* binary it builds is being placed in build.list,
because i'll probably start using more of them.
for example, i may start modifying the "date"
implementation, adding the GNU-specific options that
i need as mentioned on init.sh
i'm importing it in util/ because the sha512sum
util is needed for verifying project sources, so
if sbase itself is a "project source", that means
we can into a chicken and egg bootstrapping problem.
this is sbase at revision:
055cc1ae1b3a13c3d8f25af0a4a3316590efcd48
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/sbase/fold.c')
-rw-r--r-- | util/sbase/fold.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/util/sbase/fold.c b/util/sbase/fold.c new file mode 100644 index 00000000..6c7b9e7b --- /dev/null +++ b/util/sbase/fold.c @@ -0,0 +1,130 @@ +/* See LICENSE file for copyright and license details. */ +#include <ctype.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "text.h" +#include "util.h" +#include "utf.h" + +static int bflag = 0; +static int sflag = 0; +static size_t width = 80; + +static void +foldline(struct line *l, const char *fname) { + size_t i, col, last, spacesect, len; + Rune r; + int runelen; + + for (i = 0, last = 0, col = 0, spacesect = 0; i < l->len; i += runelen) { + if (col >= width && ((l->data[i] != '\r' && l->data[i] != '\b') || bflag)) { + if (bflag && col > width) + i -= runelen; /* never split a character */ + len = ((sflag && spacesect) ? spacesect : i) - last; + if (fwrite(l->data + last, 1, len, stdout) != len) + eprintf("fwrite <stdout>:"); + if (l->data[i] != '\n') + putchar('\n'); + if (sflag && spacesect) + i = spacesect; + last = i; + col = 0; + spacesect = 0; + } + runelen = charntorune(&r, l->data + i, l->len - i); + if (!runelen || r == Runeerror) + eprintf("charntorune: %s: invalid utf\n", fname); + if (sflag && isblankrune(r)) + spacesect = i + runelen; + if (!bflag && iscntrl(l->data[i])) { + switch(l->data[i]) { + case '\b': + col -= (col > 0); + break; + case '\r': + col = 0; + break; + case '\t': + col += (8 - (col % 8)); + if (col >= width) + i--; + break; + } + } else { + col += bflag ? runelen : 1; + } + } + if (l->len - last) + fwrite(l->data + last, 1, l->len - last, stdout); +} + +static void +fold(FILE *fp, const char *fname) +{ + static struct line line; + static size_t size = 0; + ssize_t len; + + while ((len = getline(&line.data, &size, fp)) > 0) { + line.len = len; + foldline(&line, fname); + } + if (ferror(fp)) + eprintf("getline %s:", fname); +} + +static void +usage(void) +{ + eprintf("usage: %s [-bs] [-w num | -num] [FILE ...]\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + FILE *fp; + int ret = 0; + + ARGBEGIN { + case 'b': + bflag = 1; + break; + case 's': + sflag = 1; + break; + case 'w': + width = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX)); + break; + ARGNUM: + if (!(width = ARGNUMF())) + eprintf("illegal width value, too small\n"); + break; + default: + usage(); + } ARGEND + + if (!argc) { + fold(stdin, "<stdin>"); + } else { + for (; *argv; argc--, argv++) { + if (!strcmp(*argv, "-")) { + *argv = "<stdin>"; + fp = stdin; + } else if (!(fp = fopen(*argv, "r"))) { + weprintf("fopen %s:", *argv); + ret = 1; + continue; + } + fold(fp, *argv); + if (fp != stdin && fshut(fp, *argv)) + ret = 1; + } + } + + ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); + + return ret; +} |