config

OpenBSD system configuration
git clone git://jacobedwards.org/config
Log | Files | Refs | README

commit ce800462d244a71c3f81c1eb60c3151d1354980a
parent ce5d851a7caa59e8c909bdd222bf4f8f35980a6d
Author: Jacob R. Edwards <n/a>
Date:   Mon, 28 Nov 2022 23:59:58 -0600

Add script to show OpenBSD package description

The reasoning for it's creation is explained in pkg_descr (basically,
the network is too slow). It's dependant on two new awk libraries:
1. openbsd-pkgspec.awk which handles with OpenBSD package specs and
2., unreg.awk which fully (to my knowledge, with reference to
re_format(7)) escapes EREs.

I plan to utilize the openbsd-pkgspec library for other purposes
too, perhaps integrating it into pkg_list, pkg_root, and pkg_tree.
(Really, I should just use the perl modules!)

Diffstat:
Alocal/awk/lib/openbsd-pkgspec.awk | 35+++++++++++++++++++++++++++++++++++
Alocal/awk/lib/unreg.awk | 8++++++++
Alocal/bin/bin/pkg_descr | 10++++++++++
Alocal/bin/bin/pkg_descr.awk | 42++++++++++++++++++++++++++++++++++++++++++
4 files changed, 95 insertions(+), 0 deletions(-)

diff --git a/local/awk/lib/openbsd-pkgspec.awk b/local/awk/lib/openbsd-pkgspec.awk @@ -0,0 +1,35 @@ +# Copyright 2022 Jacob R. Edwards +# +# These functions get various parts of an OpenBSD package spec (as +# defined in packages-specs(7)) + +function pkgflavor(pkg) +{ + if (match(pkg, "-[0-9]?[^-]*-")) + return substr(pkg, RSTART + RLENGTH) +} + +function pkgversion(pkg) +{ + if (match(pkg, "-[0-9][^-]*")) + return substr(pkg, RSTART + 1, RLENGTH - 1) +} + +function pkgstem(pkg) +{ + return substr(pkg, 1, + match(pkg, "-([0-9]|-)") ? (RSTART - 1) : length(pkg)) +} + + +# Generate a regex for matching complete package specs from incomplete +# package specs. (Requires unreg.awk.) +function pkgregex(pkg, _, s, v, f) +{ + if (!(s = pkgstem(pkg))) + return; + v = pkgversion(pkg); + f = pkgflavor(pkg); + return sprintf("^%s-%s%s$", unreg(s), (v ? unreg(v) : "[0-9][^-]+"), + (f ? ("-" unreg(f)) : "")); +} diff --git a/local/awk/lib/unreg.awk b/local/awk/lib/unreg.awk @@ -0,0 +1,8 @@ +# Copyright 2022 Jacob R. Edwards +# Escape regular expressions + +function unreg(reg) +{ + gsub("[[^$(*+?{|.]", "\\\\&", reg); + return reg; +} diff --git a/local/bin/bin/pkg_descr b/local/bin/bin/pkg_descr @@ -0,0 +1,10 @@ +#!/bin/sh +# Copyright 2022 Jacob R. Edwards +# Display OpenBSD package DESCR files from ports tree +# +# The purpose is display a package description in a reasonable time, +# where pkg_info takes a good few seconds on my connection, this +# does it in an instant on my hardware (0.00N seconds). + +exec awk -f "$HOME"/lib/unreg.awk -f "$HOME"/lib/openbsd-pkgspec.awk \ + -f "$HOME/bin/pkg_descr.awk" -- "$@" < /usr/local/share/ports-INDEX diff --git a/local/bin/bin/pkg_descr.awk b/local/bin/bin/pkg_descr.awk @@ -0,0 +1,42 @@ +# awk script, run like so (from sh(1)): +# +# awk -f unreg.awk -f openbsd-pkgspec.awk \ +# -f pkg_descr.awk -- "$@" < /usr/local/share/ports-INDEX + +# awk -f $lib/unreg.awk -f $lib/openbsd-pkgspec.awk + +BEGIN { + FS="|" + for (i = 1; i < ARGC; ++i) { + if (!(regs[i] = pkgregex(ARGV[i]))) { + printf "%s: Invalid package spec\n", ARGV[i] > "/dev/stderr"; + err=1 + exit 1; + } + } + ARGC = 0; +} + +{ + for (i in regs) { + if (match($1, regs[i])) { + if (path) + printf "\n" + printf "# %s\n\n", $1 + path = "/usr/ports/" $5 + while (getline < path > 0) + print; + close(path); + delete regs[i]; + next; + } + } +} + +END { + if (err) + exit 1 + for (i in regs) + printf "%s not found (%s)\n", ARGV[i], regs[i] > "/dev/stderr"; +} +