]> git.xolatile.top Git - emil-bake.git/commitdiff
awk implementation provided by the greatest man to ever live
authorEmil Williams <emilemilemil@cock.li>
Thu, 5 Dec 2024 21:12:07 +0000 (21:12 +0000)
committerEmil Williams <emilemilemil@cock.li>
Thu, 5 Dec 2024 21:12:07 +0000 (21:12 +0000)
awake [new file with mode: 0755]

diff --git a/awake b/awake
new file mode 100755 (executable)
index 0000000..1bd350e
--- /dev/null
+++ b/awake
@@ -0,0 +1,80 @@
+#!/bin/sh
+# vim:set ft=awk:
+# SPDX-License-Identifier: Unlicense
+"exec" "${AWK:-awk}" "-f" "$0" "--" "$@" && 0
+function usage() {
+       print "awake: usage: awake [-nl] [-s num] filename [args...]"
+       exit
+}
+BEGIN {
+       RS = "\r?\n"
+       FS = "\n"
+       select = 1
+       for (i = 1; i < ARGC; i++) {
+               if (!optsdone && substr(ARGV[i], 1, 1) == "-") {
+                       if (ARGV[i] == "--") {
+                               optsdone = 1
+                       } else {
+                               for (j = 2; j <= length(ARGV[i]); j++) {
+                                       c = substr(ARGV[i], j, 1)
+                                       if (c == "s") {
+                                               select = substr(ARGV[i], j+1)
+                                               if (!select) {
+                                                       ARGV[i] = ""
+                                                       select = ARGV[++i]
+                                               }
+                                               if (select !~ /^[0-9]+$/) {
+                                                       print "awake: junk -s argument: "select
+                                                       usage()
+                                               }
+                                               break
+                                       } else if (c == "n") {
+                                               dryrun = 1
+                                       } else if (c == "l") {
+                                               list = 1
+                                       } else {
+                                               print "awake: unknown option: -"c
+                                               usage()
+                                       }
+                               }
+                       }
+                       ARGV[i] = ""
+               } else if (hasfilename) {
+                       if (extraargs) {
+                               extraargs = extraargs" "ARGV[i]
+                       } else {
+                               extraargs = ARGV[i]
+                       }
+                       ARGV[i] = ""
+               } else {
+                       if (index(ARGV[i], "=")) {
+                               print "awake: bad filename: "ARGV[i]
+                               usage()
+                       }
+                       hasfilename = 1
+                       shortname = filename = ARGV[i]
+                       sub(/.*\//, "", shortname)
+                       sub(/[.][^.]*/, "", shortname)
+               }
+       }
+       if (!hasfilename) {
+               usage()
+       }
+}
+/@BAKE[ ]/ {
+       bakenum++
+       sub(/.*@BAKE[ ]/, "")
+       sub(/ *@STOP.*/, "")
+       if (list) {
+               print bakenum": "$0
+       } else if (select == bakenum) {
+               gsub(/@FILENAME/, filename)
+               gsub(/@NAME/, filename)
+               gsub(/@SHORT/, shortname)
+               gsub(/@ARGS/, extraargs)
+               print
+               if (!dryrun) {
+                       system($0)
+               }
+       }
+}