From 51e1803bc3ef14c2200ba69e61f0933bc484c1dd Mon Sep 17 00:00:00 2001 From: Emil Williams Date: Sat, 4 Jan 2025 13:09:04 -0700 Subject: [PATCH] Awake! Documented! --- README | 13 +++++++++++++ awake | 15 +++++++++------ bake.1 | 0 bake.l | 41 ++++++++++++++++++++++++++++------------- install.sh | 41 ++++++++++++++++++++++++++++++++++------- shake | 6 +++--- test.a.txt | Bin 7 files changed, 87 insertions(+), 29 deletions(-) mode change 100644 => 100755 README mode change 100644 => 100755 bake.1 mode change 100644 => 100755 bake.l mode change 100644 => 100755 test.a.txt diff --git a/README b/README old mode 100644 new mode 100755 index dfd1299..53e7498 --- a/README +++ b/README @@ -54,6 +54,8 @@ bootstrapper. The modified version includes all features of Bake with the exceptions of multi-line commands. It is not a general replacement for Bake. +Shake doesn't support multi-line commands. + Changelog Bake was created on 2023/09/13, and complete as of 2024/03/02. @@ -62,3 +64,14 @@ Bake was created on 2023/09/13, and complete as of 2024/03/02. Lex. As adviced by the original creator, I learned and implemented a Bake with lex. It's fully featured, and has new stuff. + +2024-10-24 - Awake + +Egor (some dead guy) made a AWK implementation of Bake, it's POSIX. +It doesn't support multi-line, expunge, parses options after filename, +or changing directory to file's directory - it's compatible with Bake +otherwise. + + cd /dir/of/file; awake file + +See ./awake --help diff --git a/awake b/awake index 1bd350e..82a01a4 100755 --- a/awake +++ b/awake @@ -1,7 +1,8 @@ #!/bin/sh # vim:set ft=awk: +# Written by Egor, modified for compatibility by Emil. # SPDX-License-Identifier: Unlicense -"exec" "${AWK:-awk}" "-f" "$0" "--" "$@" && 0 +"exec" "${AWK:-awk}" "-f" "$(basename $0)" "--" "$@" && 0 function usage() { print "awake: usage: awake [-nl] [-s num] filename [args...]" exit @@ -68,13 +69,15 @@ BEGIN { if (list) { print bakenum": "$0 } else if (select == bakenum) { - gsub(/@FILENAME/, filename) - gsub(/@NAME/, filename) - gsub(/@SHORT/, shortname) - gsub(/@ARGS/, extraargs) - print + gsub(/\$@|@(FILE|FILENAME|NAME)/, filename) + gsub(/\$\*|@SHORT/, shortname) + gsub(/\$\+|@ARGS/, extraargs) + gsub(/@RECURS/, ARGV[0]) + print "Awake: " $0 + printf "Output: " if (!dryrun) { system($0) + print "" } } } diff --git a/bake.1 b/bake.1 old mode 100644 new mode 100755 diff --git a/bake.l b/bake.l old mode 100644 new mode 100755 index 6c61bab..d38b089 --- a/bake.l +++ b/bake.l @@ -1,5 +1,4 @@ -/* cbake.l @BAKE flex @FILE && cc -Wall -Wextra -std=c99 -D_GNU_SOURCE -o @SHORT lex.yy.c @ARGS @STOP */ -/* expunge @BAKE flex @FILE && cc -Wall -Wextra -std=c99 -D_GNU_SOURCE -o @{@SHORT} lex.yy.c @ARGS @STOP */ +/* @BAKE flex @FILE && cc -Wall -Wextra -Wwrite-strings -std=c99 -D_GNU_SOURCE -o @SHORT lex.yy.c @ARGS @STOP */ /* Licensed under the public domain. */ %{ @@ -25,7 +24,6 @@ int line = 1, expunge_depth, first_nl, tmpline; extern void root(char * filename); extern void args(int n, int rest); extern void shorten(char * filename, int n); -extern void pipeopen(char * filename, char * mode); %} SPACE [ \t\r\v\f] @@ -99,6 +97,7 @@ MACROS ({CMD}|@STOP|{FILENAME}|@SHORT|@ARGS|@LINE|@RECURS|$@|$*|$+|@\{) %% #include +#include # define RED "\033[91m" # define GREEN "\033[92m" @@ -125,19 +124,33 @@ void shorten(char * filename, int n) { char * end = filename + strlen(filename); while (n && (end = memrchr(filename, '.', end - filename))) { --n; } if (!end) { - fprintf(stderr, " context error: Argument out of range.\n"); - /* Ensures consistency. @SHORT will always return *something* that isn't filename */ - STRING("idiot"); - return; + fprintf(stderr, "%s: Cannot shorten '%s' any further, aborting due to paranoia\n", av0, filename); + abort(); } FWRITE(filename, end - filename); } void help(void) { fprintf(stderr, g_color ? BOLD "%s" RESET : "%s", "see bake(1) - \"Buy high. Sell low.\"\n"); } -void pipeopen(char * filename, char * mode) { - g_pipe = popen(filename, mode); - if (!g_pipe) { fprintf(stderr, "%s: %s\n", av0, strerror(errno)); exit(1); } +FILE * pipeopen(char * filename, char * mode) { + FILE * pipe = popen(filename, mode); + if (!pipe) { fprintf(stderr, "%s: %s\n", av0, strerror(errno)); exit(1); } + return pipe; +} + +void print(FILE * fp, char * color_prefix, char * format, ...) { + va_list ap; + size_t len = strlen(format); + g_color ? fprintf(fp, "%s%s: ", color_prefix, av0) + : fprintf(fp, "%s: ", av0); + va_start(ap, format); + for (size_t i = 0, f = 0; i < len; ++i) { + if (!(format[i] ^ '\033')) { + while (format[i] && format[i] != 'm') ++i; + } else format[f++] = format[i]; + } + vfprintf(fp, format, ap); + va_end(ap); } int main (int ac, char ** av) { @@ -202,7 +215,8 @@ int main (int ac, char ** av) { /* setup pipe and output */ if (run) { - pipeopen("/bin/sh -e /dev/stdin", "w"); +#define pipeopen(a,b) pipeopen((char*)(a), (char*)(b)) + g_pipe = pipeopen("/bin/sh -e /dev/stdin", "w"); } if (g_rm) { g_restore = stdout; @@ -215,7 +229,7 @@ int main (int ac, char ** av) { yylex(); fflush(stdout); if (g_rm) { - fputs("\n", g_restore); + fputc('\n', g_restore); stdout = g_restore; g_pipe = g_expunge; } @@ -229,7 +243,8 @@ int main (int ac, char ** av) { run = pclose(g_pipe); /* repurposed run */ if (!g_rm) { putchar('\n'); } - if (run) { fprintf(stderr, "%s: Exit code %d\n", av0, run); } + if (run < 0) { fprintf(stderr, "%s: Exit failure\n", av0); } + if (run > 0) { fprintf(stderr, "%s: Exit code %d\n", av0, run); } return run; out_of_range: fprintf(stderr, "%s: <%d> Out of range\n", av0, g_select); return 1; } diff --git a/install.sh b/install.sh index f591f0e..e2668c3 100755 --- a/install.sh +++ b/install.sh @@ -1,13 +1,40 @@ #!/bin/sh # source install -TARGET=${TARGET:-/usr/local} -INSTALL=${INSTALL:-bake} +TARGET="${TARGET:-/usr/local}" +INSTALL="bake" + +usage() { + echo "compiles and installs Bake into /usr/local (or TARGET) in bin/" + echo "" + echo "--alternatives Includes awake and shake into the build" + echo "--target=DIRECTORY Target directory like /usr or /usr/local" + echo "" + exit 1 +} + +while [ ! -z $1 ]; do + case $(echo "$1" | cut -d= -f1) in + "--target") + TARGET=$(echo "$1" | cut -d= -f2) + ;; + "--alternatives") + INSTALL="bake shake awake" + ;; + "--help") + usage + ;; + *) + echo "Unknown option: " $1 + usage + ;; + esac + shift +done cd "$(dirname "$(readlink -f $0)")" -./shake bake.l -s $@ && \ -mkdir $TARGET/bin $TARGET/man/man1 -p && \ -install -m 755 $INSTALL $TARGET/bin - -gzip -c bake.1 > $TARGET/man/man1/bake.1.gz +./awake bake.l +mkdir -p "$TARGET/bin" "$TARGET/man/man1" +install -m 755 $INSTALL "$TARGET/bin" +gzip -c bake.1 > "$TARGET/man/man1/bake.1.gz" diff --git a/shake b/shake index 8407a54..51d3b5c 100755 --- a/shake +++ b/shake @@ -1,8 +1,8 @@ #!/bin/bash -# Originally written by Anon, modified by Emil to better match Bake functionality - -# Issues: sloooow, fails to handle multi-line statements +# Originally written by Anon, modified by Emil to better match Bake. +# Please don't use this to compile Bake, use Awake instead. +# Licensed under the public domain. VERSION="20240408" diff --git a/test.a.txt b/test.a.txt old mode 100644 new mode 100755