diff options
-rw-r--r-- | bonus/Makefile | 68 | ||||
-rw-r--r-- | bonus/README.md | 76 | ||||
-rwxr-xr-x | bonus/addurimethod | 53 | ||||
-rw-r--r-- | bonus/libfetch-http.c (renamed from bonus/libfetch/cha-http-libfetch.c) | 0 | ||||
-rw-r--r-- | bonus/libfetch/Makefile | 6 | ||||
-rw-r--r-- | bonus/stbir2/.gitignore | 1 | ||||
-rw-r--r-- | bonus/stbir2/stb_image_resize2.c | 2 | ||||
-rw-r--r-- | bonus/stbir2/stbir2.nim | 60 |
8 files changed, 260 insertions, 6 deletions
diff --git a/bonus/Makefile b/bonus/Makefile new file mode 100644 index 00000000..6365e7c0 --- /dev/null +++ b/bonus/Makefile @@ -0,0 +1,68 @@ +#TODO curse XDG +CHA_CGI_DIR ?= "$$HOME/cgi-bin" + +prefix = /usr/local + +bindir = $(DESTDIR)$(prefix)/bin + +.PHONY: none +none: + @echo "Run make install-{file} to install a script." + +stbir2_url=https://raw.githubusercontent.com/nothings/stb/master/stb_image_resize2.h + +stbir2/stb_image_resize2.h: + @printf 'stb_image_resize2.h not found.\n' + @printf 'Should I download it from $(stbir2_url)? (y/n) ' + @read -r res; if test "$$res" = "y"; then \ + cd stbir2; cha -d "$(stbir2_url)" >stb_image_resize2.h; \ + else exit 1; \ + fi + +.PHONY: install-stbir2 +install-stbir2: stbir2/stbir2.nim stbir2/stb_image_resize2.h stbir2/stb_image_resize2.c + mkdir -p $(CHA_CGI_DIR) + nim c -d:release -d:lto -o:$(CHA_CGI_DIR)/resize stbir2/stbir2.nim + +.PHONY: install-git.cgi +install-git.cgi: git.cgi + mkdir -p $(CHA_CGI_DIR) + cp git.cgi $(CHA_CGI_DIR) + ./addurimethod git git.cgi +#TODO this will almost never work unless the user is root... + ln -s $(CHA_CGI_DIR)/git.cgi $(bindir)/gitcha + +.PHONY: install-filei.cgi +install-filei.cgi: filei.cgi + mkdir -p $(CHA_CGI_DIR) + cp filei.cgi $(CHA_CGI_DIR) + +.PHONY: install-libfetch.c +install-libfetch-http.c: $(CHA_CGI_DIR)/libfetch-http + $(CC) -Wall -O2 -g -std=c89 $< -lfetch -o $@ + ./addurimethod http libfetch-http + ./addurimethod https libfetch-http + +install-magnet.cgi: magnet.cgi + mkdir -p $(CHA_CGI_DIR) + cp magnet.cgi $(CHA_CGI_DIR) + ./addurimethod magnet magnet.cgi + +install-trans.cgi: trans.cgi + mkdir -p $(CHA_CGI_DIR) + cp trans.cgi $(CHA_CGI_DIR) + @echo + @echo 'Add this to your config.toml:' + @echo + @echo '[page]' + @echo "gT = '''" + @echo 'async () => {' + @echo ' if (!pager.currentSelection) {' + @echo ' pager.alert("No selection to translate.");' + @echo ' return;' + @echo ' }' + @echo ' const text = await pager.getSelectionText(pager.currentSelection);' + @echo ' pager.cursorToggleSelection();' + @echo ' pager.load(`cgi-bin:trans.cgi?${encodeURIComponent(text)}\n`);' + @echo '}' + @echo "'''" diff --git a/bonus/README.md b/bonus/README.md new file mode 100644 index 00000000..b01c671c --- /dev/null +++ b/bonus/README.md @@ -0,0 +1,76 @@ +# Bonus + +Misc modules and configurations - mostly just personal scripts that I +don't want to copy around otherwise... + +## Install + +Run `make install-{filename}`. For example, to install git.cgi, you run +`make install-git.cgi`. + +Warning: some of the installers will modify your ~/.urimethodmap file. +Because the entries are inserted to the file's start, you can usually +just remove these if you no longer want to use the script. + +Also, the /cgi-bin/ directory is assumed to be configured to ~/cgi-bin. +Use `make CHA_CGI_DIR=...` to override this. + +If you haven't configured a CGI directory yet, then please add the +following to your ~/.config/chawan/config.toml: + +``` +[external] +cgi-dir = ["~/cgi-bin", "${%CHA_LIBEXEC_DIR}/cgi-bin"] +``` + +## Summary + +Additional documentation is embedded at the beginning of each file. +Please read it. (Note that the Makefile automates the installation +instructions, so you can skip those.) + +### [filei.cgi](filei.cgi) + +Album view of a directory. Requires `buffer.images = true`. + +### [git.cgi](git.cgi) + +Turns git command output into hypertext; quite useful, albeit a bit +slow. + +It's also a demonstration of a combined CLI command and CGI script. + +### [libfetch-http.c](libfetch-http.c) + +CGI script to replace the default http handler with FreeBSD libfetch. + +Just for fun; it's not very usable in practice, because libfetch is +designed to handle file downloads, not web browsing. + +### [magnet.cgi](magnet.cgi) + +A `magnet:` URL handler. It can forward magnet links to transmission. + +### [stbir2](stbir2/) + +By default, Chawan uses stb_image_resize for resizing images, but there +is a newer and improved (as well as much larger) version, called +`stb_image_resize2`. This script replaces the default image resizer +with that. + +To compile this, the Makefile will try download the header file from +GitHub: <https://raw.githubusercontent.com/nothings/stb/master/stb_image_resize2.h> + +but you can also just manually put the header in the directory, and then +nothing will be downloaded. + +### [trans.cgi](trans.cgi) + +Uses [translate-shell](https://github.com/soimort/translate-shell) to +translate words. + +### [w3m.toml](w3m.toml) + +A (somewhat) w3m-compatible keymap. Mainly for demonstration purposes. + +Note: this does not have an installer. Copy/include it manually. diff --git a/bonus/addurimethod b/bonus/addurimethod new file mode 100755 index 00000000..d6df13b6 --- /dev/null +++ b/bonus/addurimethod @@ -0,0 +1,53 @@ +#!/bin/sh + +if test $# -ne 2 +then printf 'Usage: addurimethod [scheme] [program name]' >&2 + exit 1 +fi + +urimethodmap=$HOME/.urimethodmap +scheme="$1:" +cgi="/cgi-bin/$2%s" + +if ! test -f "$urimethodmap" +then printf '%s: %s\n' "$scheme" "$cgi" > "$urimethodmap" + exit 0 +fi + +grep -- "^$1:" "$urimethodmap" >/dev/null && exit 0 + +space_kind=$(grep -oE '^[a-zA-Z+-]+:[[:space:]]+' "$urimethodmap" | \ + grep -oE '[[:space:]]+' | \ + tail -c2) +space_num=$(grep -oE '^[a-zA-Z+-]+:[[:space:]]+' "$urimethodmap" | \ + grep -oE '[[:space:]]+' | \ + while IFS= read -r line; do printf '%d\n' "${#line}"; done | \ + sort -r | \ + head -1) + +space_mul=1 +if test "$space_kind" = ' '; then space_mul=8; fi + +space_num=$((($space_num * $space_mul + ($space_mul - 1) - ${#scheme}) / $space_mul)) + +spaces= +if test "$space_num" -gt 0 +then spaces=$(printf "%${space_num}s" | sed "s/ /$space_kind/g") +fi + +tmpf=$(mktemp) + +printf "%s:${spaces}%s\n" "$1" "/cgi-bin/$2?%s" > "$tmpf" +cat "$urimethodmap" >> "$tmpf" + +{ + printf 'Updating %s to:\n\n' "$urimethodmap" + cat "$tmpf" +} | $PAGER +printf 'OK? (y/n) ' +read -r res + +if test "$res" = y +then cp "$tmpf" "$urimethodmap" +else printf 'Aborting. (temp file is %s)\n' "$tmpf" +fi diff --git a/bonus/libfetch/cha-http-libfetch.c b/bonus/libfetch-http.c index 4b824ced..4b824ced 100644 --- a/bonus/libfetch/cha-http-libfetch.c +++ b/bonus/libfetch-http.c diff --git a/bonus/libfetch/Makefile b/bonus/libfetch/Makefile deleted file mode 100644 index 304ac206..00000000 --- a/bonus/libfetch/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -cha-http-libfetch: cha-http-libfetch.c - $(CC) -Wall -O2 -g -std=c89 cha-http-libfetch.c -lfetch -o cha-http-libfetch - -.PHONY: clean -clean: - rm -f cha-http-libfetch diff --git a/bonus/stbir2/.gitignore b/bonus/stbir2/.gitignore new file mode 100644 index 00000000..bbc58636 --- /dev/null +++ b/bonus/stbir2/.gitignore @@ -0,0 +1 @@ +/stb_image_resize2.h diff --git a/bonus/stbir2/stb_image_resize2.c b/bonus/stbir2/stb_image_resize2.c new file mode 100644 index 00000000..f89c7eab --- /dev/null +++ b/bonus/stbir2/stb_image_resize2.c @@ -0,0 +1,2 @@ +#define STB_IMAGE_RESIZE_IMPLEMENTATION +#include "stb_image_resize2.h" diff --git a/bonus/stbir2/stbir2.nim b/bonus/stbir2/stbir2.nim new file mode 100644 index 00000000..8de0f92b --- /dev/null +++ b/bonus/stbir2/stbir2.nim @@ -0,0 +1,60 @@ +import std/options +import std/os +import std/posix +import std/strutils + +import io/dynstream +import utils/sandbox +import utils/twtstr + +{.compile("stb_image_resize2.c", "-O3").} + +{.push header: "stb_image_resize2.h".} +proc stbir_resize_uint8_linear(input_pixels: ptr uint8; + input_w, input_h, input_stride_in_bytes: cint; output_pixels: ptr uint8; + output_w, output_h, output_stride_in_bytes, num_channels: cint): ptr char + {.importc.} +{.pop.} + +proc die(s: string) {.noreturn.} = + let os = newPosixStream(STDOUT_FILENO) + os.sendDataLoop(s) + quit(1) + +proc main() = + var srcWidth = cint(-1) + var srcHeight = cint(-1) + var dstWidth = cint(-1) + var dstHeight = cint(-1) + for hdr in getEnv("REQUEST_HEADERS").split('\n'): + let k = hdr.until(':') + if k == "Cha-Image-Target-Dimensions" or k == "Cha-Image-Dimensions": + let v = hdr.after(':').strip() + let s = v.split('x') + if s.len != 2: + die("Cha-Control: ConnectionError 1 wrong dimensions\n") + let w = parseUInt32(s[0], allowSign = false) + let h = parseUInt32(s[1], allowSign = false) + if w.isNone or w.isNone: + die("Cha-Control: ConnectionError 1 wrong dimensions\n") + if k == "Cha-Image-Target-Dimensions": + dstWidth = cint(w.get) + dstHeight = cint(h.get) + else: + srcWidth = cint(w.get) + srcHeight = cint(h.get) + let ps = newPosixStream(STDIN_FILENO) + let os = newPosixStream(STDOUT_FILENO) + let src = ps.recvDataLoopOrMmap(int(srcWidth * srcHeight * 4)) + let dst = os.maybeMmapForSend(int(dstWidth * dstHeight * 4 + 1)) + if src == nil or dst == nil: + die("Cha-Control: ConnectionError 1 failed to open i/o\n") + dst.p[0] = uint8('\n') # for CGI + enterNetworkSandbox() + doAssert stbir_resize_uint8_linear(addr src.p[0], srcWidth, srcHeight, + 0, addr dst.p[1], dstWidth, dstHeight, 0, 4) != nil + os.sendDataLoop(dst) + deallocMem(src) + deallocMem(dst) + +main() |