From d3ca906f8f3f57ba0b5d506ff33578a1834275ce Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 8 Nov 2022 01:05:10 -0500 Subject: [PATCH 01/14] rewrite all in one commit lol --- example-extensions/youtube-mime.toml | 7 ++ xdg-sanity | 116 +++++++++++++++++++ xdg-sanity/extensions/teddit-mime.sample | 12 -- xdg-sanity/extensions/youtube-replace | 3 - xdg-sanity/extensions/youtube-replace.sample | 12 -- xdg-sanity/xdg-sanity.conf | 1 - 6 files changed, 123 insertions(+), 28 deletions(-) create mode 100644 example-extensions/youtube-mime.toml create mode 100755 xdg-sanity delete mode 100755 xdg-sanity/extensions/teddit-mime.sample delete mode 100755 xdg-sanity/extensions/youtube-replace delete mode 100755 xdg-sanity/extensions/youtube-replace.sample delete mode 100644 xdg-sanity/xdg-sanity.conf diff --git a/example-extensions/youtube-mime.toml b/example-extensions/youtube-mime.toml new file mode 100644 index 0000000..0b6ac6b --- /dev/null +++ b/example-extensions/youtube-mime.toml @@ -0,0 +1,7 @@ +# xdg-sanity mime extension + +[replace] # replaces these urls +urls = [ "youtube.com", "youtu.be" ] + +[with] # replaces the mime type of the above +mime = "video/vnd.youtube.yt" diff --git a/xdg-sanity b/xdg-sanity new file mode 100755 index 0000000..9fc0c99 --- /dev/null +++ b/xdg-sanity @@ -0,0 +1,116 @@ +#!/bin/sh + +set -e + +argv0="$0" + +# grabs configuration files +CONFIG="$XDG_CONFIG_HOME"/xdg-sanity.toml +if ! test -e "$CONFIG"; then + touch "$CONFIG" + CONFIG=/etc/xdg-sanity.toml + if ! test -e "$CONFIG"; then + exit 66 # sysexits(3) EX_NOINPUT + fi +fi + + +# check if usage is valid +if ! test -n "$1"; then + printf "Usage: %s [resource...]\n" "$argv0" 1>&2 + exit 64 # sysexits(3) EX_USAGE +fi + +# check if we have curl(1) +if ! command -v curl >/dev/null 2>&1; then + printf "%s: Missing dependency: curl(1)\n" "$argv0" 1>&2 + exit 71 # sysexits(3) EX_OSERR +fi + +# check if we have tomcat(1) +if ! command -v tomcat >/dev/null 2>&1; then + printf "%s: Missing dependency: stoml(1)\n" "$argv0" 1>&2 + exit 71 # sysexits(3) EX_OSERR +fi + +# set the XDG_COMMAND +test -n "$XDG_COMMAND" || XDG_COMMAND="$(tomcat tools.xdg $CONFIG)" +! command -v handlr >/dev/null 2>&1 && ! command -v xdg-open >/dev/null 2>&1 \ + && XDG_COMMAND=false \ + || true + + +# check if we have a BROWSER +test -n "$BROWSER" || BROWSER="$(tomcat tools.browser $CONFIG)" +if ! test -n "$BROWSER"; then + printf "\ +%s: \$BROWSER not filled. + Please place the path to your preferred browser's executable in + $XDG_CONFIG_HOME/xdg-sanity.conf or /etc/xdg-sanity.conf +" "$argv0" 1>&2 + exit 71 # sysexits(3) EX_OSERR +fi + +while test -n "$1"; do + URL="$1" + + # use curl(1) to write out the request header's content_type, + # strip everything after the first semicolon, + # chop off any weird whitespace remnants + MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' |\ + xargs echo)" + + # get the pattern for the extensions to MATCH + MATCH=$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\ + sed -e 's/\/.*\+//g') + + # run through MIME extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ + /usr/share/xdg-sanity/*-mime.toml \ + /dev/null + do + i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | xargs wc -l) + while ! [ "$i" = 0 ]; do + if [ "$MATCH" = "$(tomcat replace.urls[$i] $file)" ]; then + MIME=$(tomcat with.mime "$file") + fi + i=$(dc -e "$i 1 - p") + done + done + + # and the replace extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ + /usr/share/xdg-sanity/*-replace.toml \ + /dev/null + do + i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | xargs wc -l) + while ! [ "$i" = 0 ]; do + if [ "$MATCH" = "$(tomcat replace.urls[$i] $file)" ]; then + URL=$(tomcat with.url "$file") + fi + i=$(dc -e "$i 1 - p") + done + done + + # these commands may fail; this is intentional + if [ "$MIME" = "text/html" ]; then + "$BROWSER" "$URL" + else + case "$(command -v $XDG_COMMAND)" in + */handlr ) + "XDG_COMMAND" "$MIME" -- "$URL" + ;; + */xdg-open ) + "$(xdg-mime query default $MIME)" "$URL" + ;; + false ) + exit 69 # sysexits(3) EX_UNAVAILABLE + ;; + esac + fi + shift +done + +exit 0 diff --git a/xdg-sanity/extensions/teddit-mime.sample b/xdg-sanity/extensions/teddit-mime.sample deleted file mode 100755 index 74908ee..0000000 --- a/xdg-sanity/extensions/teddit-mime.sample +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh - -# EXT-TYPE=mime - -TEST=$(echo $1 | sed -ne 's/^h.\+\/\///p' | sed -e 's/\/.*\+//g') - -if [ "$TEST" = "teddit.net" ] -then -echo "text/html" -else -echo $1 -fi \ No newline at end of file diff --git a/xdg-sanity/extensions/youtube-replace b/xdg-sanity/extensions/youtube-replace deleted file mode 100755 index e0276d2..0000000 --- a/xdg-sanity/extensions/youtube-replace +++ /dev/null @@ -1,3 +0,0 @@ -# EXT-TYPE=replace - -replace youtube.com or youtu.be with piped.mint.lgbt \ No newline at end of file diff --git a/xdg-sanity/extensions/youtube-replace.sample b/xdg-sanity/extensions/youtube-replace.sample deleted file mode 100755 index a1fd473..0000000 --- a/xdg-sanity/extensions/youtube-replace.sample +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh - -# EXT-TYPE=replace - -TEST=$(echo $1 | sed -ne 's/^h.\+\/\///p' | sed -e 's/\/.*\+//g') - -if [ "$TEST" = "youtube.com" ] -then -echo $1 | sed -ne 's/youtube.com/piped.mint.lgbt/p' -else -echo $1 -fi diff --git a/xdg-sanity/xdg-sanity.conf b/xdg-sanity/xdg-sanity.conf deleted file mode 100644 index 1acbb9a..0000000 --- a/xdg-sanity/xdg-sanity.conf +++ /dev/null @@ -1 +0,0 @@ -browser = From 39ebeccf9fe9b66c2ae7e44a3c75e421d877d710 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 8 Nov 2022 01:16:23 -0500 Subject: [PATCH 02/14] readme edits --- README.md | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 80f87be..d295e77 100644 --- a/README.md +++ b/README.md @@ -7,28 +7,32 @@ send `image/jpeg` MIME type files to your image viewer. ## Installation -### Arch +### Arch Linux I maintain a package [on the AUR](https://aur.archlinux.org/packages/xdg-sanity). ### From Source -First, make sure you have `curl(1)` and `handlr(1)` installed. Then, clone this -repository and move the `xdg-sanity/` folder into `/etc` and `xdg-sanity.sh` to -`xdg-sanity` wherever your operating system stores locally-installed binaries. -This is usually `/usr/local/bin`. Make sure the installation location is in your +First, make sure you have `curl(1)`, `xdg-utils(1)` (or an alternative like +[handlr](https://github.com/chmln/handlr), and +[tomcat](https://git.tebibyte.media/emma/tomcat) installed. Then, clone this +repository and move the `xdg-sanity` binary wherever your operating system +stores locally-installed binaries. This is usually `/usr/local/bin` or +`$HOME/.local/bin` for your user. Make sure the installation location is in your `$PATH`. -Create a `xdg-sanity.desktop` file either manually or with `gendesk(1)`, +Create an `xdg-sanity.desktop` file either manually or with `gendesk(1)`, placing it, also, where your OS stores locally-installed `.desktop` files, usually `/usr/local/applications`. Set your default web browser to that -`.desktop` file. +`.desktop` file with `xdg-settings(1)` or an equivalent. -Add your default web browser to `/etc/xdg-sanity/xdg-sanity.conf` so the +Add your default web browser to `$XDG_CONFIG_HOME/xdg-sanity.toml` so the script can forward links to it. ### Usage +`xdg-sanity [RESOURCE]` + Open links from applications outside your web browser as normal. Alternatively, -you can call `xdg-sanity` directly with the only argument accepted being a URI. \ No newline at end of file +you can call `xdg-sanity` directly with the only argument accepted being a URI. From 463ce6f9c027f52c3b942c18c5f54d6acb43f17f Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 8 Nov 2022 01:17:06 -0500 Subject: [PATCH 03/14] oops --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d295e77..1d5b144 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ AUR](https://aur.archlinux.org/packages/xdg-sanity). ### From Source First, make sure you have `curl(1)`, `xdg-utils(1)` (or an alternative like -[handlr](https://github.com/chmln/handlr), and +[handlr](https://github.com/chmln/handlr)), and [tomcat](https://git.tebibyte.media/emma/tomcat) installed. Then, clone this repository and move the `xdg-sanity` binary wherever your operating system stores locally-installed binaries. This is usually `/usr/local/bin` or From 4b51c0e3f87e354efe41c5fcbd480741ab9fcad3 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 8 Nov 2022 01:38:07 -0500 Subject: [PATCH 04/14] more README --- README.md | 70 ++++++++++++++++++++++++++++++++++++++++++++++-------- xdg-sanity | 2 ++ 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1d5b144..a190fcb 100644 --- a/README.md +++ b/README.md @@ -14,21 +14,38 @@ AUR](https://aur.archlinux.org/packages/xdg-sanity). ### From Source -First, make sure you have `curl(1)`, `xdg-utils(1)` (or an alternative like -[handlr](https://github.com/chmln/handlr)), and -[tomcat](https://git.tebibyte.media/emma/tomcat) installed. Then, clone this -repository and move the `xdg-sanity` binary wherever your operating system -stores locally-installed binaries. This is usually `/usr/local/bin` or +Dependencies: +- `curl(1)` +- `xdg-utils(1)` or `handlr(1)` +- `tomcat(1)` + +You can get `tomcat` [here](https://git.tebibyte.media/emma/tomcat) + +Instructions: +Clone this repository and move the `xdg-sanity` binary wherever your operating +system stores locally-installed binaries. This is usually `/usr/local/bin` or `$HOME/.local/bin` for your user. Make sure the installation location is in your `$PATH`. Create an `xdg-sanity.desktop` file either manually or with `gendesk(1)`, -placing it, also, where your OS stores locally-installed `.desktop` files, -usually `/usr/local/applications`. Set your default web browser to that -`.desktop` file with `xdg-settings(1)` or an equivalent. +placing it where your OS stores locally-installed `.desktop` files, which is +usually `/usr/local/share/applications` or `$XDG_DATA_HOME/applications` for +your user. Set your default web browser to that `.desktop` file with +`xdg-settings(1)` or an equivalent. -Add your default web browser to `$XDG_CONFIG_HOME/xdg-sanity.toml` so the -script can forward links to it. +### Configuration + +This program uses [TOML](https://toml.io/en/v1.0.0) for its configuration. The +configuration file is set up like this: +``` +$ cat $XDG_CONFIG_HOME/xdg-sanity.toml +[tools] +browser = "firefox" +xdg = "handlr launch" +``` + +The options available for `xdg` are `handlr launch` if you use `handlr(1)` and +`xdg-open` if you use `xdg-utils(1)`. ### Usage @@ -36,3 +53,36 @@ script can forward links to it. Open links from applications outside your web browser as normal. Alternatively, you can call `xdg-sanity` directly with the only argument accepted being a URI. + +#### Extensions + +Extensions are written using TOML and are stored in either +`/usr/share/xdg-sanity` when installed from a package manager, +`/usr/local/share/xdg-sanity` when installed locally, or +`$XDG_DATA_HOME/xdg-sanity` for your user. + +There are two types of extensions: MIME and replace. MIME extensions are parsed +first and replace the MIME type of the content being fetched. Replace extensions +change the URI passed to the command to another. + +The type of the extension depends on the file name. MIME extensions should have +a name ending in `-mime.toml` and replace extensions should have +`-replace.toml`. + +Here's what a MIME extension looks like: +``` +cat $XDG_DATA_HOME/xdg-sanity/youtube-mime.toml +[replace] +urls = [ "youtube.com", "youtu.be" ] + +[with] +mime = "video/vnd.youtube.yt" +``` +and here's what a replace extension looks like: +``` +cat $XDG_DATA_HOME/xdg-sanity/youtube-replace.toml +[replace] +urls = [ "youtube.com", "youtu.be" ] + +[with] +url = "https://piped.mint.lgbt/" diff --git a/xdg-sanity b/xdg-sanity index 9fc0c99..6df361a 100755 --- a/xdg-sanity +++ b/xdg-sanity @@ -68,6 +68,7 @@ while test -n "$1"; do for file in \ "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ /usr/share/xdg-sanity/*-mime.toml \ + /usr/local/share/xdg-sanity/*-mime.toml \ /dev/null do i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | xargs wc -l) @@ -83,6 +84,7 @@ while test -n "$1"; do for file in \ "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ /usr/share/xdg-sanity/*-replace.toml \ + /usr/local/share/xdg-sanity/*-replace.toml \ /dev/null do i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | xargs wc -l) From ad64911c7f17f89aa32c0c80cffdc69422d39a64 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 8 Nov 2022 01:48:56 -0500 Subject: [PATCH 05/14] $ --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a190fcb..be98648 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ a name ending in `-mime.toml` and replace extensions should have Here's what a MIME extension looks like: ``` -cat $XDG_DATA_HOME/xdg-sanity/youtube-mime.toml +$ cat $XDG_DATA_HOME/xdg-sanity/youtube-mime.toml [replace] urls = [ "youtube.com", "youtu.be" ] @@ -80,7 +80,7 @@ mime = "video/vnd.youtube.yt" ``` and here's what a replace extension looks like: ``` -cat $XDG_DATA_HOME/xdg-sanity/youtube-replace.toml +$ cat $XDG_DATA_HOME/xdg-sanity/youtube-replace.toml [replace] urls = [ "youtube.com", "youtu.be" ] From 5ca13548d737b8a54e566b2f1de6cb76f7ba79c4 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 8 Nov 2022 01:51:07 -0500 Subject: [PATCH 06/14] removed old file --- xdg-sanity.sh | 66 --------------------------------------------------- 1 file changed, 66 deletions(-) delete mode 100755 xdg-sanity.sh diff --git a/xdg-sanity.sh b/xdg-sanity.sh deleted file mode 100755 index 3ee015c..0000000 --- a/xdg-sanity.sh +++ /dev/null @@ -1,66 +0,0 @@ -#!/bin/sh - -INPUT=$(echo $1) -echo "Loading configuration..." -CONFIG=$(cat /etc/xdg-sanity/xdg-sanity.conf) -BROWSER=$(echo $CONFIG | sed -ne 's/^browser *= *//p') - -if [ "$BROWSER" = "" ] -then -echo "Please place the path to your default browser's executable in /etc/xdg-sanity/xdg-sanity.conf" -exit -else -echo "Found default browser $BROWSER" -fi - -echo "Loading extensions..." -for EXT in /etc/xdg-sanity/extensions/*.sh -do - -if [ "$EXT" = "/etc/xdg-sanity/extensions/*.sh" ] -then -echo "No extensions to load" -else - -for EXT in /etc/xdg-sanity/extensions/*.sh -do -TYPE=$(cat $EXT | sed -ne 's/^# EXT-TYPE=//p' | tr -d '\n') -echo "Found $TYPE extension $EXT" - -if [ "$TYPE" = "replace" ] -then -echo "Modifying $INPUT..." -INPUT=$($EXT "$INPUT") -echo "Got $INPUT" - -else -if [ "$TYPE" = "mime" ] -then -echo "Modifying MIME type..." -MIME=$($EXT "$INPUT") -echo "Got $MIME" - -fi -fi - -done -fi - -done - -if [ "$MIME" = "" ] || [ "$MIME" = "$1" ] || [ "$MIME" = "$INPUT" ] -then -echo "Determining MIME type of $INPUT:" -# Determines HTTP code, might use for something else? -# CODE=$(curl -fLIs "$INPUT" | sed -ne 's/ [[:space:]]*$//p' | sed -ne 's|^HTTP/.\+ ||p') -MIME=$(curl -fLIs "$INPUT" | sed -ne 's/^[cC]ontent-[tT]ype: //p' | sed -e 's/;.\+//p' | tail -n1 | tr -d '\r') -echo $MIME -fi - -if [ "$MIME" = "text/html" ] -then -$BROWSER $INPUT - -else -handlr launch "$MIME" -- "$INPUT" -fi \ No newline at end of file From 78e2bcf9ca4183499a02feb5d7024647016b290a Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 8 Nov 2022 10:16:20 -0500 Subject: [PATCH 07/14] fixed wc implementation --- xdg-sanity | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xdg-sanity b/xdg-sanity index 6df361a..b9ba416 100755 --- a/xdg-sanity +++ b/xdg-sanity @@ -71,7 +71,8 @@ while test -n "$1"; do /usr/local/share/xdg-sanity/*-mime.toml \ /dev/null do - i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | xargs wc -l) + i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) + printf "$i\n" while ! [ "$i" = 0 ]; do if [ "$MATCH" = "$(tomcat replace.urls[$i] $file)" ]; then MIME=$(tomcat with.mime "$file") @@ -87,7 +88,7 @@ while test -n "$1"; do /usr/local/share/xdg-sanity/*-replace.toml \ /dev/null do - i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | xargs wc -l) + i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) while ! [ "$i" = 0 ]; do if [ "$MATCH" = "$(tomcat replace.urls[$i] $file)" ]; then URL=$(tomcat with.url "$file") From e4501794f2b8f9fd6b022f8bcd03ef1307810a40 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 8 Nov 2022 19:38:27 -0500 Subject: [PATCH 08/14] extensions work (for real this time) --- xdg-sanity | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/xdg-sanity b/xdg-sanity index b9ba416..aa0dd04 100755 --- a/xdg-sanity +++ b/xdg-sanity @@ -29,19 +29,19 @@ fi # check if we have tomcat(1) if ! command -v tomcat >/dev/null 2>&1; then - printf "%s: Missing dependency: stoml(1)\n" "$argv0" 1>&2 + printf "%s: Missing dependency: tomcat(1)\n" "$argv0" 1>&2 exit 71 # sysexits(3) EX_OSERR fi # set the XDG_COMMAND -test -n "$XDG_COMMAND" || XDG_COMMAND="$(tomcat tools.xdg $CONFIG)" +test -n "$XDG_COMMAND" || XDG_COMMAND="$(tomcat tools.xdg "$CONFIG")" ! command -v handlr >/dev/null 2>&1 && ! command -v xdg-open >/dev/null 2>&1 \ && XDG_COMMAND=false \ || true # check if we have a BROWSER -test -n "$BROWSER" || BROWSER="$(tomcat tools.browser $CONFIG)" +test -n "$BROWSER" || BROWSER="$(tomcat tools.browser "$CONFIG")" if ! test -n "$BROWSER"; then printf "\ %s: \$BROWSER not filled. @@ -61,40 +61,41 @@ while test -n "$1"; do xargs echo)" # get the pattern for the extensions to MATCH - MATCH=$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\ - sed -e 's/\/.*\+//g') + MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\ + sed -e 's/\/.*\+//g')" # run through MIME extensions for file in \ "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ /usr/share/xdg-sanity/*-mime.toml \ - /usr/local/share/xdg-sanity/*-mime.toml \ - /dev/null + /usr/local/share/xdg-sanity/*-mime.toml do - i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) - printf "$i\n" - while ! [ "$i" = 0 ]; do - if [ "$MATCH" = "$(tomcat replace.urls[$i] $file)" ]; then - MIME=$(tomcat with.mime "$file") - fi - i=$(dc -e "$i 1 - p") - done + if test -e "$file"; then + i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) + while ! [ "$i" = 0 ]; do + if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then + MIME="$(tomcat with.mime "$file")" + fi + i="$(dc -e "$i 1 - p")" + done + fi done # and the replace extensions for file in \ "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ /usr/share/xdg-sanity/*-replace.toml \ - /usr/local/share/xdg-sanity/*-replace.toml \ - /dev/null + /usr/local/share/xdg-sanity/*-replace.toml do - i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) - while ! [ "$i" = 0 ]; do - if [ "$MATCH" = "$(tomcat replace.urls[$i] $file)" ]; then - URL=$(tomcat with.url "$file") - fi - i=$(dc -e "$i 1 - p") - done + if test -e "$file"; then + i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) + while ! [ "$i" = 0 ]; do + if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then + URL="$(tomcat with.url "$file")" + fi + i="$(dc -e "$i 1 - p")" + done + fi done # these commands may fail; this is intentional @@ -103,10 +104,10 @@ while test -n "$1"; do else case "$(command -v $XDG_COMMAND)" in */handlr ) - "XDG_COMMAND" "$MIME" -- "$URL" + handlr launch "$MIME" -- "$URL" ;; */xdg-open ) - "$(xdg-mime query default $MIME)" "$URL" + "$(xdg-mime query default "$MIME")" "$URL" ;; false ) exit 69 # sysexits(3) EX_UNAVAILABLE From 00bd98df9a3f55639a152f8282e738ea215a57ae Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 22 Nov 2022 00:02:30 -0500 Subject: [PATCH 09/14] removed tabs ;-; --- xdg-sanity | 146 +++++++++++++++++++++++++---------------------------- 1 file changed, 70 insertions(+), 76 deletions(-) diff --git a/xdg-sanity b/xdg-sanity index aa0dd04..f6eb1b2 100755 --- a/xdg-sanity +++ b/xdg-sanity @@ -7,114 +7,108 @@ argv0="$0" # grabs configuration files CONFIG="$XDG_CONFIG_HOME"/xdg-sanity.toml if ! test -e "$CONFIG"; then - touch "$CONFIG" - CONFIG=/etc/xdg-sanity.toml - if ! test -e "$CONFIG"; then - exit 66 # sysexits(3) EX_NOINPUT - fi + touch "$CONFIG" fi - # check if usage is valid if ! test -n "$1"; then - printf "Usage: %s [resource...]\n" "$argv0" 1>&2 - exit 64 # sysexits(3) EX_USAGE + printf "Usage: %s [resource...]\n" "$argv0" 1>&2 + exit 64 # sysexits(3) EX_USAGE fi # check if we have curl(1) if ! command -v curl >/dev/null 2>&1; then - printf "%s: Missing dependency: curl(1)\n" "$argv0" 1>&2 - exit 71 # sysexits(3) EX_OSERR + printf "%s: Missing dependency: curl(1)\n" "$argv0" 1>&2 + exit 69 # sysexits(3) EX_UNAVAILABLE fi # check if we have tomcat(1) if ! command -v tomcat >/dev/null 2>&1; then - printf "%s: Missing dependency: tomcat(1)\n" "$argv0" 1>&2 - exit 71 # sysexits(3) EX_OSERR + printf "%s: Missing dependency: tomcat(1)\n" "$argv0" 1>&2 + exit 69 # sysexits(3) EX_UNAVAILABLE fi # set the XDG_COMMAND test -n "$XDG_COMMAND" || XDG_COMMAND="$(tomcat tools.xdg "$CONFIG")" ! command -v handlr >/dev/null 2>&1 && ! command -v xdg-open >/dev/null 2>&1 \ && XDG_COMMAND=false \ - || true - + || true # check if we have a BROWSER test -n "$BROWSER" || BROWSER="$(tomcat tools.browser "$CONFIG")" if ! test -n "$BROWSER"; then - printf "\ + printf "\ %s: \$BROWSER not filled. - Please place the path to your preferred browser's executable in - $XDG_CONFIG_HOME/xdg-sanity.conf or /etc/xdg-sanity.conf + Please place the path to your preferred browser's executable in + $XDG_CONFIG_HOME/xdg-sanity.toml " "$argv0" 1>&2 - exit 71 # sysexits(3) EX_OSERR + exit 71 # sysexits(3) EX_OSERR fi while test -n "$1"; do - URL="$1" + URL="$1" - # use curl(1) to write out the request header's content_type, - # strip everything after the first semicolon, - # chop off any weird whitespace remnants - MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' |\ - xargs echo)" + # use curl(1) to write out the request header's content_type, + # strip everything after the first semicolon, + # chop off any weird whitespace remnants + MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' |\ + xargs echo)" - # get the pattern for the extensions to MATCH - MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\ - sed -e 's/\/.*\+//g')" - - # run through MIME extensions - for file in \ - "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ - /usr/share/xdg-sanity/*-mime.toml \ - /usr/local/share/xdg-sanity/*-mime.toml - do - if test -e "$file"; then - i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) - while ! [ "$i" = 0 ]; do - if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then - MIME="$(tomcat with.mime "$file")" - fi - i="$(dc -e "$i 1 - p")" - done - fi - done + # get the pattern for the extensions to MATCH + MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\ + sed -e 's/\/.*\+//g')" + + # run through MIME extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ + /usr/share/xdg-sanity/*-mime.toml \ + /usr/local/share/xdg-sanity/*-mime.toml + do + if test -e "$file"; then + i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) + while ! [ "$i" = 0 ]; do + if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then + MIME="$(tomcat with.mime "$file")" + fi + i="$(dc -e "$i 1 - p")" + done + fi + done - # and the replace extensions - for file in \ - "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ - /usr/share/xdg-sanity/*-replace.toml \ - /usr/local/share/xdg-sanity/*-replace.toml - do - if test -e "$file"; then - i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) - while ! [ "$i" = 0 ]; do - if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then - URL="$(tomcat with.url "$file")" - fi - i="$(dc -e "$i 1 - p")" - done - fi - done + # and the replace extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ + /usr/share/xdg-sanity/*-replace.toml \ + /usr/local/share/xdg-sanity/*-replace.toml + do + if test -e "$file"; then + i="$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l)" + while ! [ "$i" = 0 ]; do + if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then + URL="$(tomcat with.url "$file")" + fi + i="$(dc -e "$i 1 - p")" + done + fi + done # these commands may fail; this is intentional - if [ "$MIME" = "text/html" ]; then - "$BROWSER" "$URL" - else - case "$(command -v $XDG_COMMAND)" in - */handlr ) - handlr launch "$MIME" -- "$URL" - ;; - */xdg-open ) - "$(xdg-mime query default "$MIME")" "$URL" - ;; - false ) - exit 69 # sysexits(3) EX_UNAVAILABLE - ;; - esac - fi - shift + if [ "$MIME" = "text/html" ]; then + "$BROWSER" "$URL" + else + case "$(command -v $XDG_COMMAND)" in + */handlr ) + handlr launch "$MIME" -- "$URL" + ;; + */xdg-open ) + "$(xdg-mime query default "$MIME")" "$URL" + ;; + false ) + exit 69 # sysexits(3) EX_UNAVAILABLE + ;; + esac + fi + shift done exit 0 From b636494fd7a97dfdfade2041841bd16bf14753fa Mon Sep 17 00:00:00 2001 From: emma Date: Mon, 20 Mar 2023 19:12:06 -0400 Subject: [PATCH 10/14] good practice changes, removed config file, and replaced dc(1) with bc(1) --- LICENSE => COPYING | 0 xdg-sanity | 86 ++++++++++++++++++++-------------------------- 2 files changed, 37 insertions(+), 49 deletions(-) rename LICENSE => COPYING (100%) diff --git a/LICENSE b/COPYING similarity index 100% rename from LICENSE rename to COPYING diff --git a/xdg-sanity b/xdg-sanity index f6eb1b2..bea029a 100755 --- a/xdg-sanity +++ b/xdg-sanity @@ -1,47 +1,45 @@ #!/bin/sh +# Copyright (c) 2022–2023 Emma Tebibyte +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# This program is free software: you can redistribute it and/or modify it under +# the terms of the GNU Affero General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. +# +# You should have received a copy of the GNU Affero General Public License along +# with this program. If not, see https://www.gnu.org/licenses/. + set -e argv0="$0" -# grabs configuration files -CONFIG="$XDG_CONFIG_HOME"/xdg-sanity.toml -if ! test -e "$CONFIG"; then - touch "$CONFIG" -fi - # check if usage is valid -if ! test -n "$1"; then +if test -z "$1"; then printf "Usage: %s [resource...]\n" "$argv0" 1>&2 exit 64 # sysexits(3) EX_USAGE fi -# check if we have curl(1) -if ! command -v curl >/dev/null 2>&1; then - printf "%s: Missing dependency: curl(1)\n" "$argv0" 1>&2 - exit 69 # sysexits(3) EX_UNAVAILABLE -fi - -# check if we have tomcat(1) -if ! command -v tomcat >/dev/null 2>&1; then - printf "%s: Missing dependency: tomcat(1)\n" "$argv0" 1>&2 - exit 69 # sysexits(3) EX_UNAVAILABLE -fi - -# set the XDG_COMMAND -test -n "$XDG_COMMAND" || XDG_COMMAND="$(tomcat tools.xdg "$CONFIG")" -! command -v handlr >/dev/null 2>&1 && ! command -v xdg-open >/dev/null 2>&1 \ - && XDG_COMMAND=false \ - || true +for dep in \ + curl \ + handlr \ + tomcat +do + if ! command -v "$dep" >/dev/null 2>&1; then + printf "%s: Missing dependency: %s(1)\n" "$argv0" "$dep" 1>&2 + exit 69 # sysexits(3) EX_UNAVAILABLE + fi +done # check if we have a BROWSER -test -n "$BROWSER" || BROWSER="$(tomcat tools.browser "$CONFIG")" -if ! test -n "$BROWSER"; then - printf "\ -%s: \$BROWSER not filled. - Please place the path to your preferred browser's executable in - $XDG_CONFIG_HOME/xdg-sanity.toml -" "$argv0" 1>&2 +if test -z "$BROWSER"; then + printf "%s: \$BROWSER not filled.\n" "$argv0" 1>&2 exit 71 # sysexits(3) EX_OSERR fi @@ -51,12 +49,12 @@ while test -n "$1"; do # use curl(1) to write out the request header's content_type, # strip everything after the first semicolon, # chop off any weird whitespace remnants - MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' |\ - xargs echo)" + MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' \ + | xargs echo)" # get the pattern for the extensions to MATCH - MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\ - sed -e 's/\/.*\+//g')" + MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' \ + | sed -e 's/\/.*\+//g')" # run through MIME extensions for file in \ @@ -70,7 +68,7 @@ while test -n "$1"; do if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then MIME="$(tomcat with.mime "$file")" fi - i="$(dc -e "$i 1 - p")" + i="$(printf "%s-1\n" "$i" | bc)" done fi done @@ -85,9 +83,9 @@ while test -n "$1"; do i="$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l)" while ! [ "$i" = 0 ]; do if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then - URL="$(tomcat with.url "$file")" + URL="$(tomcat with.url "$file")" fi - i="$(dc -e "$i 1 - p")" + i="$(printf "%s-1\n" "$i" | bc)" done fi done @@ -96,17 +94,7 @@ while test -n "$1"; do if [ "$MIME" = "text/html" ]; then "$BROWSER" "$URL" else - case "$(command -v $XDG_COMMAND)" in - */handlr ) - handlr launch "$MIME" -- "$URL" - ;; - */xdg-open ) - "$(xdg-mime query default "$MIME")" "$URL" - ;; - false ) - exit 69 # sysexits(3) EX_UNAVAILABLE - ;; - esac + handlr launch "$MIME" -- "$URL" fi shift done From 494d9ad1405587fe8aefca2d12b3c74870254116 Mon Sep 17 00:00:00 2001 From: emma Date: Mon, 20 Mar 2023 19:22:14 -0400 Subject: [PATCH 11/14] added e-mail address to copyright header --- xdg-sanity | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xdg-sanity b/xdg-sanity index bea029a..7db0629 100755 --- a/xdg-sanity +++ b/xdg-sanity @@ -1,6 +1,6 @@ #!/bin/sh -# Copyright (c) 2022–2023 Emma Tebibyte +# Copyright (c) 2022–2023 Emma Tebibyte # SPDX-License-Identifier: AGPL-3.0-or-later # # This program is free software: you can redistribute it and/or modify it under From 265a1b9b95b4b363ba56168d2329791b2afeba01 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 22 Mar 2023 13:41:32 -0400 Subject: [PATCH 12/14] made tomcat extension parsing in-line with upcoming release of rust rewrite --- xdg-sanity | 86 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/xdg-sanity b/xdg-sanity index 7db0629..9a00659 100755 --- a/xdg-sanity +++ b/xdg-sanity @@ -20,6 +20,19 @@ set -e argv0="$0" +get_urls() { + i="0" + until ! tomcat replace.urls["$i"] "$file" >/dev/null 2>&1 + do + URLS="$(printf "%s\n%s" \ + "$URLS" \ + "$(tomcat replace.urls["$i"] "$file")" + )" + + i="$(printf "%s+1\n" "$i" | bc)" + done +} + # check if usage is valid if test -z "$1"; then printf "Usage: %s [resource...]\n" "$argv0" 1>&2 @@ -28,8 +41,7 @@ fi for dep in \ curl \ - handlr \ - tomcat + handlr do if ! command -v "$dep" >/dev/null 2>&1; then printf "%s: Missing dependency: %s(1)\n" "$argv0" "$dep" 1>&2 @@ -46,9 +58,8 @@ fi while test -n "$1"; do URL="$1" - # use curl(1) to write out the request header's content_type, - # strip everything after the first semicolon, - # chop off any weird whitespace remnants + # use curl(1) to write out the request header's content_type, strip everything + # after the first semicolon, chop off any weird whitespace remnants MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' \ | xargs echo)" @@ -56,39 +67,40 @@ while test -n "$1"; do MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' \ | sed -e 's/\/.*\+//g')" - # run through MIME extensions - for file in \ - "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ - /usr/share/xdg-sanity/*-mime.toml \ - /usr/local/share/xdg-sanity/*-mime.toml - do - if test -e "$file"; then - i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) - while ! [ "$i" = 0 ]; do - if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then - MIME="$(tomcat with.mime "$file")" - fi - i="$(printf "%s-1\n" "$i" | bc)" - done - fi - done + # only check for extensions if tomcat(1) is installed + if command -v tomcat >/dev/null 2>&1; then + # run through MIME extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ + /usr/share/xdg-sanity/*-mime.toml \ + /usr/local/share/xdg-sanity/*-mime.toml + do + if test -e "$file"; then + get_urls + for url in $URLS; do + if [ "$MATCH" = "$url" ]; then + MIME="$(tomcat with.mime "$file")" + fi + done + fi + done - # and the replace extensions - for file in \ - "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ - /usr/share/xdg-sanity/*-replace.toml \ - /usr/local/share/xdg-sanity/*-replace.toml - do - if test -e "$file"; then - i="$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l)" - while ! [ "$i" = 0 ]; do - if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then - URL="$(tomcat with.url "$file")" - fi - i="$(printf "%s-1\n" "$i" | bc)" - done - fi - done + # and the replace extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ + /usr/share/xdg-sanity/*-replace.toml \ + /usr/local/share/xdg-sanity/*-replace.toml + do + if test -e "$file"; then + get_urls + for url in $URLS; do + if [ "$MATCH" = "$url" ]; then + URL="$(tomcat with.url "$file")" + fi + done + fi + done + fi # these commands may fail; this is intentional if [ "$MIME" = "text/html" ]; then From da348f82f2eb8fc731777041dda7ebb4b06ede2c Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 22 Mar 2023 13:49:02 -0400 Subject: [PATCH 13/14] updated README --- README.md | 53 ++++++++++++++++------------------------------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index be98648..c9aa94e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -# xdg-sanity - The `xdg-sanity` script is built to replace your default web browser in your desktop/XDG settings. It intercepts http/s URIs sent to the default browser by `xdg-open` and sends it to the appropriate application. For example, it will @@ -16,51 +14,31 @@ AUR](https://aur.archlinux.org/packages/xdg-sanity). Dependencies: - `curl(1)` -- `xdg-utils(1)` or `handlr(1)` -- `tomcat(1)` - -You can get `tomcat` [here](https://git.tebibyte.media/emma/tomcat) +- `handlr(1)` +- [`tomcat(1)`](https://git.tebibyte.media/emma/tomcat) (optional; enables +[extensions](#extensions)) Instructions: Clone this repository and move the `xdg-sanity` binary wherever your operating -system stores locally-installed binaries. This is usually `/usr/local/bin` or -`$HOME/.local/bin` for your user. Make sure the installation location is in your -`$PATH`. +system stores locally-installed binaries. This is usually `/usr/local/bin/` or +`"$HOME/.local/bin/"` for your user. Make sure the installation location is in +your `$PATH`. -Create an `xdg-sanity.desktop` file either manually or with `gendesk(1)`, -placing it where your OS stores locally-installed `.desktop` files, which is -usually `/usr/local/share/applications` or `$XDG_DATA_HOME/applications` for -your user. Set your default web browser to that `.desktop` file with -`xdg-settings(1)` or an equivalent. - -### Configuration - -This program uses [TOML](https://toml.io/en/v1.0.0) for its configuration. The -configuration file is set up like this: -``` -$ cat $XDG_CONFIG_HOME/xdg-sanity.toml -[tools] -browser = "firefox" -xdg = "handlr launch" -``` - -The options available for `xdg` are `handlr launch` if you use `handlr(1)` and -`xdg-open` if you use `xdg-utils(1)`. - -### Usage - -`xdg-sanity [RESOURCE]` - -Open links from applications outside your web browser as normal. Alternatively, -you can call `xdg-sanity` directly with the only argument accepted being a URI. +Create an `xdg-sanity.desktop` file, placing it where your OS stores +locally-installed `.desktop` files, which is usually +`/usr/local/share/applications` or `$XDG_DATA_HOME/applications` for your user. +Set your default web browser to that `.desktop` file using `handlr set`. #### Extensions -Extensions are written using TOML and are stored in either +Extensions are written using TOML and are stored in either `/usr/share/xdg-sanity` when installed from a package manager, -`/usr/local/share/xdg-sanity` when installed locally, or +`/usr/local/share/xdg-sanity` when installed locally, or `$XDG_DATA_HOME/xdg-sanity` for your user. +Extension support requires the installation of the `tomcat(1)` tool, which +parses TOML for the command line. + There are two types of extensions: MIME and replace. MIME extensions are parsed first and replace the MIME type of the content being fetched. Replace extensions change the URI passed to the command to another. @@ -86,3 +64,4 @@ urls = [ "youtube.com", "youtu.be" ] [with] url = "https://piped.mint.lgbt/" +``` From ada7c6976f9a4fbc59ba49861415d8bf43c75889 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 22 Mar 2023 14:38:04 -0400 Subject: [PATCH 14/14] added scheme extensions and fixed URIs only remembering their domains when replaced. --- README.md | 26 +++++++++++++------ example-extensions/youtube-mime.toml | 7 ----- xdg-sanity | 39 +++++++++++++++++++++------- 3 files changed, 47 insertions(+), 25 deletions(-) delete mode 100644 example-extensions/youtube-mime.toml diff --git a/README.md b/README.md index c9aa94e..e31533e 100644 --- a/README.md +++ b/README.md @@ -39,29 +39,39 @@ Extensions are written using TOML and are stored in either Extension support requires the installation of the `tomcat(1)` tool, which parses TOML for the command line. -There are two types of extensions: MIME and replace. MIME extensions are parsed -first and replace the MIME type of the content being fetched. Replace extensions -change the URI passed to the command to another. +There are three kinds of extensions: MIME, replace, and scheme. MIME extensions +are parsed first and replace the MIME type of the content being fetched. Replace +extensions change the URI passed to the command to another. Scheme extensions +replace the protocol by which the URI hostname is being accessed. The type of the extension depends on the file name. MIME extensions should have -a name ending in `-mime.toml` and replace extensions should have -`-replace.toml`. +a name ending in `.mime.toml`, replace extensions should have +`.replace.toml`, and scheme extensions should end with `.scheme.toml`. Here's what a MIME extension looks like: ``` -$ cat $XDG_DATA_HOME/xdg-sanity/youtube-mime.toml +$ cat $XDG_DATA_HOME/xdg-sanity/youtube.mime.toml [replace] urls = [ "youtube.com", "youtu.be" ] [with] mime = "video/vnd.youtube.yt" ``` -and here's what a replace extension looks like: +what a replace extension looks like: ``` -$ cat $XDG_DATA_HOME/xdg-sanity/youtube-replace.toml +$ cat $XDG_DATA_HOME/xdg-sanity/youtube.replace.toml [replace] urls = [ "youtube.com", "youtu.be" ] [with] url = "https://piped.mint.lgbt/" ``` +and what a scheme extension looks like: +``` +$ cat $XDG_DATA_HOME/xdg-sanity/spotify.scheme.toml +[replace] +urls = [ "spotify.com" ] + +[with] +scheme = "spotify://" +``` diff --git a/example-extensions/youtube-mime.toml b/example-extensions/youtube-mime.toml deleted file mode 100644 index 0b6ac6b..0000000 --- a/example-extensions/youtube-mime.toml +++ /dev/null @@ -1,7 +0,0 @@ -# xdg-sanity mime extension - -[replace] # replaces these urls -urls = [ "youtube.com", "youtu.be" ] - -[with] # replaces the mime type of the above -mime = "video/vnd.youtube.yt" diff --git a/xdg-sanity b/xdg-sanity index 9a00659..8d4724f 100755 --- a/xdg-sanity +++ b/xdg-sanity @@ -63,17 +63,22 @@ while test -n "$1"; do MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' \ | xargs echo)" + # get the URL with no scheme + NO_SCHEME="$(printf "%s\n" "$URL" | sed -n 's/^h.\+\/\///p')" + # get the pattern for the extensions to MATCH - MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' \ - | sed -e 's/\/.*\+//g')" + MATCH="$(printf "%s\n" "$NO_SCHEME" | sed 's/\/.*\+//g')" + + # get the non-domain of the URI for tacking onto the end of replaced URIs + URI="$(printf "%s\n" "$NO_SCHEME" | sed -n 's/^[^/]*\///p')" # only check for extensions if tomcat(1) is installed if command -v tomcat >/dev/null 2>&1; then # run through MIME extensions for file in \ - "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ - /usr/share/xdg-sanity/*-mime.toml \ - /usr/local/share/xdg-sanity/*-mime.toml + "$XDG_DATA_HOME"/xdg-sanity/*.mime.toml \ + /usr/share/xdg-sanity/*.mime.toml \ + /usr/local/share/xdg-sanity/*.mime.toml do if test -e "$file"; then get_urls @@ -85,21 +90,35 @@ while test -n "$1"; do fi done - # and the replace extensions + # then replace extensions for file in \ - "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ - /usr/share/xdg-sanity/*-replace.toml \ - /usr/local/share/xdg-sanity/*-replace.toml + "$XDG_DATA_HOME"/xdg-sanity/*.replace.toml \ + /usr/share/xdg-sanity/*.replace.toml \ + /usr/local/share/xdg-sanity/*.replace.toml do if test -e "$file"; then get_urls for url in $URLS; do if [ "$MATCH" = "$url" ]; then - URL="$(tomcat with.url "$file")" + URL="$(tomcat with.url "$file")$URI" fi done fi done + + # and the scheme extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*.scheme.toml \ + /usr/share/xdg-sanity/*.scheme.toml \ + /usr/local/share/xdg-sanity/*.scheme.toml + do + get_urls + for url in $URLS; do + if [ "$URL" = "$url" ]; then + URL="$(tomcat with.scheme "$file")$MATCH" + fi + done + done fi # these commands may fail; this is intentional