dot

packages and services management
Log | Files | Refs | README

commit 4b74183a48d94ca0691671a7d0ef1934dbdd6aa3
parent aa47133184033a888dacbb262f7d7487f0c40405
Author: Josuah Demangeon <mail@josuah.net>
Date:   Mon,  5 Mar 2018 10:15:15 +0100

split xdg-open in more reasonable xdg-open, xdg-uri, xdg-store, xdg-connect files

Diffstat:
Tbin/o | 0
Abin/xdg-connect | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Mbin/xdg-open | 126+------------------------------------------------------------------------------
Mbin/xdg-store | 284++++++++++++++-----------------------------------------------------------------
Mbin/xdg-uri | 41+++++++++++++++++++++++------------------
5 files changed, 123 insertions(+), 378 deletions(-)

diff --git a/bin/o b/bin/o diff --git a/bin/xdg-connect b/bin/xdg-connect @@ -0,0 +1,50 @@ +#!/bin/sh + +# connect to the uri passed as argument + +# Call xdg-uri to parse the uri then open it with apropriate program +# and arguments. + +IFS=' ' read scheme user pass host port path << EOF +$(xdg-uri "$1") +EOF + +[ "${host##*.}" = "onion" ] && [ -z "$TOR" ] && TOR=1 exec torsocks "$0" "$@" + +case "$scheme" in +(9p) + dir="$HOME/var/9p/$host" + mkdir -p "$dir" + exec doas 9pfs "$host" "$dir" + ;; +(ftp) + exec ftp "$1" + ;; +(gopher) + exec sacc "$1" + ;; +(http|https) + exec firefox "$1" + ;; +(irc) + [ "$port" = . ] && port='6667' + cp -r "$HOME/etc/runit/ii" "$SVDIR/ii-$host" + [ "$path" = . ] && exit + sleep 3 + printf '/j #%s\n' "$path" > "$HOME/var/irc/$host/in" + ;; +(news) + exec retawq "$1" + ;; +(sftp) + [ "$port" = . ] && port='22' + exec sftp -P "$port" "$host" + ;; +(ssh) + [ "$port" = . ] && port='22' + exec ssh -p "$port" "${1#*://}" + ;; +(telnet) + exec telnet "$port" "$host" + ;; +esac diff --git a/bin/xdg-open b/bin/xdg-open @@ -1,130 +1,6 @@ #!/bin/sh -# override the xdg-open utility - -tty | grep -q -e '^/dev/pts/.*' -e '^/dev/tty.*' || - exec st -f terminus:pixelsize=16 -e "$0" "$@" - -[ -e "$1" ] && set -- "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" -printf '\033]0;%s\a' "$1" - -#--- hierarchical uri ---------------------------------------------------------- - -case "$host" in -(*.onion) - exec torsocks "$0" "$@" - ;; -esac -case "$scheme" in -(9p) - dir="$HOME/var/9p/$host" - mkdir -p "$dir" - exec doas 9pfs "$host" "$dir" - ;; -(ftp) - exec ftp "$1" - ;; -(git) - mkdir -p "$HOME/git/$host" - cd "$HOME/git/$host" || exit 1 - exec git clone "$1" - ;; -(gopher) - exec sacc "$1" - ;; -(http|https) - case "$host" in - (vimeo.com|*youtube.com|*youtu.be|*openload.co*) - mkdir -p "$HOME/lib/video/$host" - cd "$HOME/lib/video/$host" - youtube-dl "$1" - exec "$0" "$HOME/lib/video/$host"/*"${1##*[=/]}"* - ;; - (*bandcamp.com) - mkdir -p "$HOME/lib/music/$host" - cd "$HOME/lib/music/$host" - exec youtube-dl "$1" - ;; - esac - - case "$path" in - (*.torrent) - mkdir -p "$HOME/var/torrent" - curl -Lo "$HOME/var/torrent/${path##*/}" "$1" - exec "$0" "$HOME/var/torrent/${path##*/}" - ;; - (*.jpg|*.JPG|*.jpeg|*.JPEG|*.png|*.PNG|*.gif) - mkdir -p "$HOME/lib/image/$host/" - curl -Lo "$HOME/lib/image/$host/${path##*/}" "$1" - exec "$0" "$HOME/lib/image/$host/${path##*/}" - ;; - (*.opus|*.ogg|*.mp3|*.flac) - mkdir -p "$HOME/lib/music/$host" - curl -Lo "$HOME/lib/music/$host/${path##*/}" "$1" - exec "$0" "$HOME/lib/music/$host/${path##*/}" - ;; - (*.webm|*.mp4|*.mkv|*.ogv|*.avi) - mkdir -p "$HOME/lib/video/$host" - curl -Lo "$HOME/lib/video/$host/${path##*/}" "$1" - exec "$0" "$HOME/lib/video/$host/${path##*/}" - ;; - (*.pdf|*.ps) - mkdir -p "$HOME/lib/doc/$host" - curl -Lo "$HOME/lib/doc/$host/${path##*/}" "$1" - exec "$0" "$HOME/lib/doc/$host/${path##*/}" - ;; - (*.txt) - mkdir -p "$HOME/lib/text/$host" - curl -Lo "$HOME/lib/text/$host/${path##*/}" "$1" - exec "$0" "$HOME/lib/text/$host/${path##*/}" - ;; - (*.gz|*.bz2|*.xz|*.tgz|*.tgz|*.tbz2|*.txz|*.zip|*.rar) - mkdir -p "$HOME/tmp" - curl -Lo "$HOME/tmp/${path##*/}" "$1" - exec "$0" "$HOME/tmp/${path##*/}" - ;; - (*) - exec firefox "$1" - ;; - esac - - ;; -(irc) - [ "$port" = . ] && port='6667' - "$HOME/etc/sv/ii" "$host" "$port" - [ "$path" = . ] && exit - sleep 3 - printf '/j #%s\n' "$path" > "$HOME/var/irc/$host/in" - ;; -(news) - exec retawq "$1" - ;; -(sftp) - [ "$port" = . ] && port='22' - exec sftp -P "$port" "$host" - ;; -(ssh) - [ "$port" = . ] && port='22' - exec ssh -p "$port" "${1#*://}" - ;; -(telnet) - exec telnet "$port" "$host" - ;; -esac - -#--- short uri ----------------------------------------------------------------- - -case "$1" in -(mailto:*) - export EDITOR=vim - exec mcom "${1#mailto:}" - ;; -(magnet:*) - exec transmission-cli "$1" - ;; -esac - -#--- files --------------------------------------------------------------------- +# override the xdg-open utility case "$(uname)" in (Linux) diff --git a/bin/xdg-store b/bin/xdg-store @@ -1,62 +1,15 @@ -#!/bin/sh -# override the xdg-open utility +#!/bin/sh -e -tty | grep -q -e '^/dev/pts/.*' -e '^/dev/tty.*' || - exec st -f terminus:pixelsize=16 -e "$0" "$@" +# store a file from an uri scheme given as argument -[ -e "$1" ] && set -- "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" -printf '\033]0;%s\a' "$1" +# Call xdg-uri to parse the uri then call apropriate program to store +# the file, then print the path to the local file. The directory the file +# is saved to is determined according to its extension. -#--- parse uri ----------------------------------------------------------------- -# scheme://[user[:pass]@]host[:port][/path] -IFS=' ' read scheme user pass host port path << EOF -$(printf %s "$1" | awk '{ - gsub(" ", "%20"); - gsub("\t", "%0b"); - - scheme = $0; - if (sub("^[^:/]*://", "", $0) > 0) - sub("://.*", "", scheme); - else - scheme = "."; - - user = $0; - if (sub("^[^@/]*@", "", $0) > 0) - sub("@.*", "", user); - else - user = "."; - - pass = user; - if (sub(":.*", "", user) > 0) - sub("[^:]*:", "", pass) ; - else - pass = "."; - - host = $0 - if (sub("^[^:]*:", ":", $0) > 0) - sub(":.*", "", host); - else if (sub("[^/]*/", "/", $0) > 0) - sub("/.*", "", host); - - port = $0 - if (sub("^:[0-9]+:", "", $0) > 0 || sub("^:[0-9]+/", "/", $0) > 0) { - sub(":", "", port); - sub("[:/].*", "", port); - } else if (sub("^:[0-9]+$", "", $0) > 0) { - sub(":", "", port); - } else { - port = "."; - } - - path = $0; - sub("[#?].*", "", path); - - printf("%s\t%s\t%s\t%s\t%s\t%s", scheme, user, pass, host, port, path); -}') +IFS=' ' read scheme user pass host port path << EOF +$(xdg-uri "$1") EOF -#--- hierarchical uri ---------------------------------------------------------- - case "$host" in (*.onion) exec torsocks "$0" "$@" @@ -64,29 +17,13 @@ case "$host" in esac case "$scheme" in -(9p) - dir="$HOME/var/9p/$host" - mkdir -p "$dir" - exec doas 9pfs "$host" "$dir" - ;; -(ftp) - exec ftp "$1" - ;; -(git) - mkdir -p "$HOME/git/$host" - cd "$HOME/git/$host" || exit 1 - exec git clone "$1" - ;; -(gopher) - exec sacc "$1" - ;; (http|https) case "$host" in (vimeo.com|*youtube.com|*youtu.be|*openload.co*) mkdir -p "$HOME/lib/video/$host" cd "$HOME/lib/video/$host" youtube-dl "$1" - exec "$0" "$HOME/lib/video/$host"/*"${1##*[=/]}"* + exec printf '%s\n' "$HOME/lib/video/$host"/*"${1##*[=/]}"* ;; (*bandcamp.com) mkdir -p "$HOME/lib/music/$host" @@ -94,186 +31,63 @@ case "$scheme" in exec youtube-dl "$1" ;; esac - - case "$path" in - (*.torrent) - mkdir -p "$HOME/var/torrent" - curl -Lo "$HOME/var/torrent/${path##*/}" "$1" - exec "$0" "$HOME/var/torrent/${path##*/}" - ;; - (*.jpg|*.JPG|*.jpeg|*.JPEG|*.png|*.PNG|*.gif) - mkdir -p "$HOME/lib/image/$host/" - curl -Lo "$HOME/lib/image/$host/${path##*/}" "$1" - exec "$0" "$HOME/lib/image/$host/${path##*/}" - ;; - (*.opus|*.ogg|*.mp3|*.flac) - mkdir -p "$HOME/lib/music/$host" - curl -Lo "$HOME/lib/music/$host/${path##*/}" "$1" - exec "$0" "$HOME/lib/music/$host/${path##*/}" - ;; - (*.webm|*.mp4|*.mkv|*.ogv|*.avi) - mkdir -p "$HOME/lib/video/$host" - curl -Lo "$HOME/lib/video/$host/${path##*/}" "$1" - exec "$0" "$HOME/lib/video/$host/${path##*/}" - ;; - (*.pdf|*.ps) - mkdir -p "$HOME/lib/doc/$host" - curl -Lo "$HOME/lib/doc/$host/${path##*/}" "$1" - exec "$0" "$HOME/lib/doc/$host/${path##*/}" - ;; - (*.txt) - mkdir -p "$HOME/lib/text/$host" - curl -Lo "$HOME/lib/text/$host/${path##*/}" "$1" - exec "$0" "$HOME/lib/text/$host/${path##*/}" - ;; - (*.gz|*.bz2|*.xz|*.tgz|*.tgz|*.tbz2|*.txz|*.zip|*.rar) - mkdir -p "$HOME/tmp" - curl -Lo "$HOME/tmp/${path##*/}" "$1" - exec "$0" "$HOME/tmp/${path##*/}" - ;; - (*) - exec firefox "$1" - ;; - esac - - ;; -(irc) - [ "$port" = . ] && port='6667' - "$HOME/etc/sv/ii" "$host" "$port" - [ "$path" = . ] && exit - sleep 3 - printf '/j #%s\n' "$path" > "$HOME/var/irc/$host/in" - ;; -(news) - exec retawq "$1" - ;; -(sftp) - [ "$port" = . ] && port='22' - exec sftp -P "$port" "$host" - ;; -(ssh) - [ "$port" = . ] && port='22' - exec ssh -p "$port" "${1#*://}" ;; -(telnet) - exec telnet "$port" "$host" - ;; -esac - -#--- short uri ----------------------------------------------------------------- - -case "$1" in -(mailto:*) - export EDITOR=vim - exec mcom "${1#mailto:}" - ;; -(magnet:*) - exec transmission-cli "$1" - ;; -esac - -#--- files --------------------------------------------------------------------- - -case "$(uname)" in -(Linux) - play="aucat" +(git) + mkdir -p "$HOME/git/$host" + cd "$HOME/git/$host" || exit 1 + git clone "$1" + exec printf '%s\n' "$HOME/git/$host/${path%.git}" ;; -(*BSD) - play="aucat -r 44100 -i -" +(magnet) + transmission-cli "$1" ;; esac -[ -e "$1" ] && case "$1" in +case "$path" in (*.torrent) - exec transmission-cli "$1" - ;; -(*.img|*.iso) - exec qemu-system-x86_64 -m 500M "$1" - ;; -#--- media --------------------------------------------------------------------- -(*.pdf) - mupdf "$1" & - exit - ;; -(*.png|*.PNG) - png2ff < "$1" | lel & - exit - ;; -(*.jpg|*.jpeg|*.JPG|*.JPEG) - jpg2ff < "$1" | lel & - exit - ;; -(*.gif) - exec mplayer -loop 0 "$1" + file="$HOME/var/torrent/${path##*/}" ;; -(*.opus) - exec opusdec "$1" +(*.jpg|*.JPG|*.jpeg|*.JPEG|*.png|*.PNG|*.gif|*.GIF) + file="$HOME/lib/image/$host/${path##*/}" ;; -(*.webm|*.mp4|*.mkv|*.ogv|*.avi|*.mp3|*.flac|*.ogg) - exec ffplay "$1" +(*.opus|*.ogg|*.mp3|*.flac) + file="$HOME/lib/music/$host/${path##*/}" ;; -(*/music/*) - find "$1" -type f | sort | while IFS='' read -r song - do - printf '%s\n' "$song" - "$0" "$song" - done - exit +(*.webm|*.mp4|*.mkv|*.ogv|*.avi) + file="$HOME/lib/video/$host/${path##*/}" ;; -#--- pager --------------------------------------------------------------------- -(*.[0-9]|*.[0-9]p|*.[0-9].gz) - exec mandoc -a -T utf8 "$1" +(*.pdf|*.ps) + file="$HOME/lib/doc/$host/${path##*/}" ;; -(*.o|*.a) - exec radare2 "$1" +(*.txt) + file="$HOME/lib/text/$host/${path##*/}" ;; -(*.html) - exec retawq "$1" +(*.gz|*.bz2|*.xz|*.tgz|*.tgz|*.tbz2|*.txz|*.zip|*.rar) + file="$HOME/tmp/${path##*/}" ;; -#--- archive ------------------------------------------------------------------- -(*.tgz) - mv "$1" "${1%.tgz}.tar.gz" - exec "$0" "${1%.tgz}.tar.gz" - ;; -(*.tbz) - mv "$1" "${1%.tbz}.tar.bz2" - exec "$0" "${1%.tbz}.tar.bz2" - ;; -(*.txz) - mv "$1" "${1%.txz}.tar.xz" - exec "$0" "${1%.txz}.tar.xz" - ;; -(*.bz2) - bzip2 -ds "$1" - exec "$0" "${1%.bz2}" - ;; -(*.gz) - gzip -d "$1" - exec "$0" "${1%.gz}" - ;; -(*.xz) - xz -d "$1" - exec "$0" "${1%.xz}" - ;; -(*.tar) - exec tar -xf "$1" - ;; -(*.rar) - exec unrar x "$1" - ;; -(*.zip) - exec unzip "$1" +esac + +if [ "$file" ] +then + mkdir -p "$(dirname "$file")" + cd "$(dirname "$file")" +fi + +case "$scheme" in +(http|https) ;; -(*.Z) - exec uncompress "$1" +(sftp) + [ "$port" = . ] && port="22" + printf 'get %s\n' "$file" | sftp -P "$port" "$host" ;; -(*.7z) - exec 7z x "$1" +(ssh) + [ "$port" = . ] && port="22" + scp -p "$port" "${1#*://}" "$file" ;; -(*.deb) - exec ar vx "$1" +(gopher) + [ "$port" = . ] && port="70" + printf '%s\r\n' "$path" | nc "$host" "$port" > "$file" ;; esac -[ -d "$1" ] && exec io grep "$1" -[ -f "$1" ] && exec $EDITOR "$1" +printf '%s\n' "$file" diff --git a/bin/xdg-uri b/bin/xdg-uri @@ -1,19 +1,24 @@ -#!/bin/sh +#!/usr/bin/awk -f -# scheme://[user[:pass]@]host[:port][/path] -IFS=' ' read scheme user pass host port path << EOF -$(printf %s "$1" | awk '{ - gsub(" ", "%20"); - gsub("\t", "%0b"); +# uri parser - scheme = $0; - if (sub("^[^:/]*://", "", $0) > 0) +# [scheme://][user[:pass]@]host[:port][/path] + +BEGIN { + URI = ARGV[1]; + ARGC = 0; + + gsub(" ", "%20", URI); + gsub("\t", "%0b", URI); + + scheme = URI; + if (sub("^[^:/]*://", "", URI) > 0) sub("://.*", "", scheme); else scheme = "."; - user = $0; - if (sub("^[^@/]*@", "", $0) > 0) + user = URI; + if (sub("^[^@/]*@", "", URI) > 0) sub("@.*", "", user); else user = "."; @@ -24,24 +29,24 @@ $(printf %s "$1" | awk '{ else pass = "."; - host = $0 - if (sub("^[^:]*:", ":", $0) > 0) + host = URI + if (sub("^[^:]*:", ":", URI) > 0) sub(":.*", "", host); - else if (sub("[^/]*/", "/", $0) > 0) + else if (sub("[^/]*/", "/", URI) > 0) sub("/.*", "", host); - port = $0 - if (sub("^:[0-9]+:", "", $0) > 0 || sub("^:[0-9]+/", "/", $0) > 0) { + port = URI + if (sub("^:[0-9]+:", "", URI) > 0 || sub("^:[0-9]+/", "/", URI) > 0) { sub(":", "", port); sub("[:/].*", "", port); - } else if (sub("^:[0-9]+$", "", $0) > 0) { + } else if (sub("^:[0-9]+$", "", URI) > 0) { sub(":", "", port); } else { port = "."; } - path = $0; + path = URI; sub("[#?].*", "", path); printf("%s\t%s\t%s\t%s\t%s\t%s", scheme, user, pass, host, port, path); -}' +}