From 03d827e2fbc409ef97829f25b8eeca5204f81a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 12 Jul 2021 13:24:49 +0200 Subject: Re-organize files --- etc/P | 5 +++ etc/ce | 28 +++++++++++++ etc/child | 27 +++++++++++++ etc/colortest | 15 +++++++ etc/cpy | 3 ++ etc/cuthere | 2 + etc/ep | 9 +++++ etc/f | 10 +++++ etc/flip | 3 ++ etc/g | 30 ++++++++++++++ etc/gspell | 18 +++++++++ etc/imgur | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ etc/inc | 3 ++ etc/isort | 21 ++++++++++ etc/lorem | 5 +++ etc/mkmv | 23 +++++++++++ etc/ord | 56 ++++++++++++++++++++++++++ etc/p | 7 ++++ etc/pst | 17 ++++++++ etc/re! | 48 +++++++++++++++++++++++ etc/wpdf | 76 ++++++++++++++++++++++++++++++++++++ etc/xtopen | 41 ++++++++++++++++++++ 22 files changed, 570 insertions(+) create mode 100755 etc/P create mode 100755 etc/ce create mode 100755 etc/child create mode 100755 etc/colortest create mode 100755 etc/cpy create mode 100755 etc/cuthere create mode 100755 etc/ep create mode 100755 etc/f create mode 100755 etc/flip create mode 100755 etc/g create mode 100755 etc/gspell create mode 100644 etc/imgur create mode 100755 etc/inc create mode 100755 etc/isort create mode 100755 etc/lorem create mode 100755 etc/mkmv create mode 100755 etc/ord create mode 100755 etc/p create mode 100755 etc/pst create mode 100755 etc/re! create mode 100755 etc/wpdf create mode 100755 etc/xtopen (limited to 'etc') diff --git a/etc/P b/etc/P new file mode 100755 index 0000000..3c11b20 --- /dev/null +++ b/etc/P @@ -0,0 +1,5 @@ +#!/bin/sh + +# P -- open file processed with p in PAGER + +"$PAGER" /var/tmp/pg diff --git a/etc/ce b/etc/ce new file mode 100755 index 0000000..c00d1bf --- /dev/null +++ b/etc/ce @@ -0,0 +1,28 @@ +#!/bin/sh + +# ce -- center text + +usage() { echo usage: $0 [cols] 1>&2; exit 1; } + +[ $# -gt 1 ] && usage + +if [ $# -eq 1 ]; then + cols=${1#-} + [ "$cols" -gt 0 ] || usage + shift +else + cols=`tput cols` + [ "$cols" -le 80 ] || cols=80 +fi + +while read line; do + line=${line## } + line=${line%% } + len=`echo "$line" | wc -c` + max=$(((cols - len) / 2)) + i=0 + while [ $((i++)) -lt $max ]; do + printf ' ' + done + echo "$line" +done diff --git a/etc/child b/etc/child new file mode 100755 index 0000000..32dab60 --- /dev/null +++ b/etc/child @@ -0,0 +1,27 @@ +#!/bin/sh + +# child -- print pid of youngest grandchild + +opt= +for x in "$@"; do + case "$x" in + -*) opt="$opt $x"; shift ;; + *) ;; + esac +done + +[ $# -ne 1 ] && { echo usage: $0 [-?] pid 1>&2; exit 1; } + +ps -do pid,etime,comm -k etime $opt | +pid=$1 perl -lane ' + if (/^ *$ENV{pid} / .. 1) { + next if /^ *$ENV{pid} /; + last if not /[|`-]/; + $t = 0; $i = 0; + $t += $_*(60**$i++) for reverse split /:/, $F[1]; + print "$t $_" if $t > 2; + } +' | +sort -n | +head -1 | +cut -d\ -f2 diff --git a/etc/colortest b/etc/colortest new file mode 100755 index 0000000..24aa95c --- /dev/null +++ b/etc/colortest @@ -0,0 +1,15 @@ +#!/bin/sh + +printf " " +for b in 0 1 2 3 4 5 6 7; do printf " 4${b}m "; done +echo +for f in "" 30 31 32 33 34 35 36 37; do + for s in "" "1;"; do + printf "%4sm" "${s}${f}" + printf " \033[%sm%s\033[0m" "$s$f" "gYw " + for b in 0 1 2 3 4 5 6 7; do + printf " \033[4%s;%sm%s\033[0m" "$b" "$s$f" " gYw " + done + echo + done +done diff --git a/etc/cpy b/etc/cpy new file mode 100755 index 0000000..827d21c --- /dev/null +++ b/etc/cpy @@ -0,0 +1,3 @@ +#!/bin/sh + +tee /tmp/cpy"$1" diff --git a/etc/cuthere b/etc/cuthere new file mode 100755 index 0000000..01c1606 --- /dev/null +++ b/etc/cuthere @@ -0,0 +1,2 @@ +#!/bin/sh +echo '--------------------8<------------------------------------' diff --git a/etc/ep b/etc/ep new file mode 100755 index 0000000..4be6317 --- /dev/null +++ b/etc/ep @@ -0,0 +1,9 @@ +#!/bin/sh + +# ep -- edit pipe + +tmp=`mktemp /tmp/ep.XXXXXX` +cat > $tmp +${EDITOR:-vi} $tmp < /dev/tty > /dev/tty +cat $tmp +rm $tmp diff --git a/etc/f b/etc/f new file mode 100755 index 0000000..7f541d8 --- /dev/null +++ b/etc/f @@ -0,0 +1,10 @@ +#!/bin/sh + +# f -- fmt with new line for each sentence + +fmt "$@" | +sed 's/\([^ ]\)\. \([^ ]\)/\1.\ +\.temporary line for fmt\ +\2/g' | +fmt "$@" | +sed '/^\.temporary line for fmt$/d' diff --git a/etc/flip b/etc/flip new file mode 100755 index 0000000..614bc67 --- /dev/null +++ b/etc/flip @@ -0,0 +1,3 @@ +#!/usr/bin/perl + +exec (shift @ARGV, pop @ARGV, @ARGV); diff --git a/etc/g b/etc/g new file mode 100755 index 0000000..7c55707 --- /dev/null +++ b/etc/g @@ -0,0 +1,30 @@ +#!/bin/sh + +# g -- poor man's ack + +IFS=' +' + +pat= +opt= +i=0 + +while [ $((i++)) -lt $# ]; do + case "$1" in + -*) opt=`printf '%s%s\n' "$opt" $1` + shift ;; + *) break ;; + esac +done + +[ $# -eq 0 ] && { echo usage: $0 [-...] pattern [file ...] 1>&2; exit 1; } + +pat=$1 +shift + +files=`find . -name '*.c' -or -name '*.h' -or -name '*.cc' | sed s,./,,` +[ -z "$files" ] && [ -z "$*" ] && { echo no matching files found 1>&2; exit 1; } + +# todo (?): treat argument \*.x like -or -name \*.x + +p grep $opt -n "$pat" $files "$@" diff --git a/etc/gspell b/etc/gspell new file mode 100755 index 0000000..e931f3c --- /dev/null +++ b/etc/gspell @@ -0,0 +1,18 @@ +#!/bin/sh + +# gspell -- print locations of spelling errors in file + +f=`mktemp` +if [ $# -eq 0 ]; then + g=`mktemp` + cat > $g + set -- $g +fi + +spell "$@" > $f +fgrep -Hnof $f "$@" | +awk -F: 'NF > 2 { print $0; prefix = $1 ":" $2 ":"; next } { print prefix $0 }' +s=$? +rm $f +rm $g 2>/dev/null +exit $s diff --git a/etc/imgur b/etc/imgur new file mode 100644 index 0000000..f459fc3 --- /dev/null +++ b/etc/imgur @@ -0,0 +1,123 @@ +#!/bin/sh + +# Imgur script by Bart Nagel +# Improvements by Tino Sino +# Made POSIX-compliant by John Ankarström +# Version 6 or more +# I release this into the public domain. Do with it what you will. +# The latest version can be found at https://github.com/tremby/imgur.sh + +# API Key provided by Bart; +# replace with your own or specify yours as IMGUR_CLIENT_ID envionment variable +# to avoid limits +default_client_id=c9a6efb3d7932fd +client_id="${IMGUR_CLIENT_ID:=$default_client_id}" + +# Function to output usage instructions +usage() { + echo "Usage: $(basename $0) [ [...]]" >&2 + echo + echo "Upload images to imgur and output their new URLs to stdout. Each one's" >&2 + echo "delete page is output to stderr between the view URLs." >&2 + echo + echo "A filename can be - to read from stdin. If no filename is given, stdin is read." >&2 + echo + echo "If xsel, xclip, pbcopy, or clip is available," >&2 + echo "the URLs are put on the X selection or clipboard for easy pasting." >&2 +} + +# Function to upload a path +# First argument should be a content spec understood by curl's -F option +upload() { + curl -s -H "Authorization: Client-ID $client_id" -H "Expect: " -F "image=$1" https://api.imgur.com/3/image.xml + # The "Expect: " header is to get around a problem when using this through + # the Squid proxy. Not sure if it's a Squid bug or what. +} + +# Check arguments +if [ x"$1" = x"-h" -o x"$1" = x"--help" ]; then + usage + exit 0 +elif [ $# -eq 0 ]; then + echo "No file specified; reading from stdin" >&2 + exec "$0" - +fi + +# Check curl is available +type curl 2>&1 >/dev/null || { + echo "Couldn't find curl, which is required." >&2 + exit 17 +} + +clip="" +errors=false + +# Loop through arguments +while [ $# -gt 0 ]; do + file="$1" + shift + + # Upload the image + case "$file" + http://*|https://*) + # URL -> imgur + response=$(upload "$file") 2>/dev/null + ;; + *) + # File -> imgur + # Check file exists + if [ x"$file" != x"-" -a ! -f "$file" ]; then + echo "File '$file' doesn't exist; skipping" >&2 + errors=true + continue + fi + response=$(upload "@$file") 2>/dev/null + esac + + if [ $? -ne 0 ]; then + echo "Upload failed" >&2 + errors=true + continue + elif echo "$response" | grep -q 'success="0"'; then + echo "Error message from imgur:" >&2 + msg="${response##*}" + echo "${msg%%*}" >&2 + errors=true + continue + fi + + # Parse the response and output our stuff + url="${response##*}" + url="${url%%*}" + delete_hash="${response##*}" + delete_hash="${delete_hash%%*}" + echo $url | sed 's/^http:/https:/' + echo "Delete page: https://imgur.com/delete/$delete_hash" >&2 + + # Append the URL to a string so we can put them all on the clipboard later + clip+="$url" + if [ $# -gt 0 ]; then + clip+=$'\n' + fi +done + +# Put the URLs on the clipboard if we can +if type pbcopy 2>&1 >/dev/null; then + echo -n "$clip" | pbcopy +elif type clip 2>&1 >/dev/null; then + echo -n "$clip" | clip +elif [ $DISPLAY ]; then + if type xsel 2>&1 >/dev/null; then + echo -n "$clip" | xsel -i + elif type xclip 2>&1 >/dev/null; then + echo -n "$clip" | xclip + else + echo "Haven't copied to the clipboard: no xsel or xclip" >&2 + fi +else + echo "Haven't copied to the clipboard: no \$DISPLAY or pbcopy or clip" >&2 +fi + +if $errors; then + exit 1 +fi diff --git a/etc/inc b/etc/inc new file mode 100755 index 0000000..51345a5 --- /dev/null +++ b/etc/inc @@ -0,0 +1,3 @@ +#!/usr/bin/perl -p +# inc -- increment numbers in lines +s/\d+/$n=$& if!$n;$n+$i++/e diff --git a/etc/isort b/etc/isort new file mode 100755 index 0000000..eecde11 --- /dev/null +++ b/etc/isort @@ -0,0 +1,21 @@ +#!/usr/bin/perl -p + +# isort -- sort C variable declarations separated by commas + +# Extract words, save prefix. +s/([^,]*?)(\S+[,;])/$2/; +$prefix = $1; + +# Ensure all words end with comma. +$semicolon = s/;$/,/; + +# Sort words. +@words = sort { + ($x = $a) =~ s/^\*+//; + ($y = $b) =~ s/^\*+//; + $x cmp $y +} split /\s+/; + +# Join words, add prefix and semicolon. +$_ = $prefix . (join ' ', @words) . "\n"; +s/,$/;/ if $semicolon; diff --git a/etc/lorem b/etc/lorem new file mode 100755 index 0000000..3dc7042 --- /dev/null +++ b/etc/lorem @@ -0,0 +1,5 @@ +#!/bin/sh + +cat <&2; exit 1; } + +# Default values +n=2 +s='. ' + +# Parse options +while getopts n:s: o +do + case $o in + n) n=$OPTARG ;; + s) s=$OPTARG ;; + ?) usage ;; + esac +done +shift $((OPTIND-1)) + +# Validate options +case "$n" in +[1-9]) ;; +*) echo $0: n must be a number from 1 to 9 1>&2 + exit 1 ;; +esac +test -z "$1" && usage + +# Construct glob and regex substitution from -s and -n +i=0; while test $((i++)) -lt "$n"; do sub=$sub'[0-9]'; done +glob=$sub +sub="s/^$sub$(printf '%s\n' "$s" | sed 's/\([.*[\\]\|\]\)/\\&/g')//" + +totext() { + for f in "$@"; do printf '%s\n' "$f"; done | + sort | + sed "$sub" +} + +fromtext() { + nl -s.\ -w2 -nrz | { + i=1 + while read new + do + name=`printf '%s\n' "$new" | sed "$sub"` + if test -e "$name" + then mv "$name" "$new" + else mv $glob"$s$name" "$new" 2>&- + fi + done + } +} + +# Edit order +totext "$@" | ep | fromtext diff --git a/etc/p b/etc/p new file mode 100755 index 0000000..b145031 --- /dev/null +++ b/etc/p @@ -0,0 +1,7 @@ +#!/bin/sh + +# p -- maybe view command output in pager + +[ -t 0 ] && exec /bin/sh -c '"$@" 2>&1 | "$0"' "$0" "$@" + +cat | tee /var/tmp/pg diff --git a/etc/pst b/etc/pst new file mode 100755 index 0000000..dd6bd64 --- /dev/null +++ b/etc/pst @@ -0,0 +1,17 @@ +#!/bin/sh + +d= +while getopts d o +do + case $o in + d) d=1 ;; + ?) echo usage: $0 [-d] [suffix] 1>&2; exit 1 ;; + esac +done +shift $((OPTIND-1)) +s=$1 + +test ! -e /tmp/cpy"$s" && { echo clipboard not present 1>&2; exit 1; } +cat /tmp/cpy"$s" +test ! -z "$d" && rm /tmp/cpy"$s" +return 0 diff --git a/etc/re! b/etc/re! new file mode 100755 index 0000000..1314ae9 --- /dev/null +++ b/etc/re! @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +# re! -- rewrite shebangs + +use strict; + +my $test = 0; +sub usage { die "usage: $0 [-n] file ...\n" } + +if (@ARGV and $ARGV[0] eq '-n') { + $test = 1; + shift @ARGV; +} +usage if not @ARGV; + +for my $file (@ARGV) { + open my $o, '<', $file or die "could not open $file: $!\n"; + + # parse shebang + my $shebang = <$o>; + $shebang =~ /^#!/ or die "no shebang: $file\n"; + $shebang =~ /^#!\s*(\S+)\s*(.*)/; + my ($old, $args) = ($1, $2); + + # validate path + next if -x $old; + + # get new path + (my $basename = $old) =~ s,.*/,,; + chomp(my $new = `which $basename`); + $new or die "could not find $basename\n"; + next if $old eq $new; + + # print results if test + if ($test) { + print "$file: $old -> $new\n"; + next; + } + + # write new shebang + open my $n, '>', "$file.tmp" or die "could not open $file.tmp: $!\n"; + print $n "#!$new $args\n"; + print $n $_ while <$o>; + + close for ($o, $n); + system('mv', "$file.tmp", $file) == 0 + or die "could not overwrite $file\n"; +} diff --git a/etc/wpdf b/etc/wpdf new file mode 100755 index 0000000..94e5029 --- /dev/null +++ b/etc/wpdf @@ -0,0 +1,76 @@ +#!/usr/bin/perl + +# wpdf -- view, watch and remake pdf based on Makefile + +use strict; +use warnings; + +my $alive; # number of children alive +my %deptarget; # dependency => target +my $parent = $$; # pid of parent + +$SIG{USR1} = sub { kill 'HUP', 0 if not --$alive }; +$SIG{HUP} = sub { exit 0 }; + +# collect dependencies for given files +for my $target (@ARGV) { + # if pdf, collect from Makefile + if ($target =~ /\.pdf$/) { + (my $basename = $target) =~ s/\.pdf$//; + open my $f, '<', 'Makefile' + or die "could not open Makefile: $!\n"; + while (<$f>) { + # explicit + if (/^\Q$target\E:\s*(.*)/) { + $deptarget{$_} = $target for split /\s/, $1; + last; + } + # implicit + if (/^\.([^ .]+)\.pdf\s*:\s*(.*)/) { + $deptarget{"$basename.$1"} = $target; + $deptarget{$_} = $target for split /\s/, $2; + last; + } + } + close $f; + } + + # if not pdf, assume target is actually source file and collect from it + # (with build(1) syntax) + else { + my $i = 0; + my $source = $target; + $target =~ s,\.[^./]*$,,; $target .= '.pdf'; + $deptarget{$source} = $target; + + open my $f, '<', $source; + while (<$f>) { + last if ++$i > 20; + if (/\s% (.*)/) { + $deptarget{$_} = $target for split /\s/, $1; + last; + } + } + close $f; + } + + if (fork == 0) { + system 'xpdf', '-remote', "wpdf-$target", $target; + kill 'USR1', $parent; + exit; + } + $alive++; +} + +die "no dependencies found\n" if not keys %deptarget; + +# make pdf on demand +open my $p, '-|', 'watch', keys %deptarget + or die "could not start watch: $!\n"; +while (<$p>) { + chomp; + system('make', "$deptarget{$_}") == 0 + && system('xpdf', '-remote', "wpdf-$deptarget{$_}", '-reload'); +} +close $p; +exit($? != 0); diff --git a/etc/xtopen b/etc/xtopen new file mode 100755 index 0000000..ccda2e5 --- /dev/null +++ b/etc/xtopen @@ -0,0 +1,41 @@ +#!/bin/sh + +# xtopen -- run command in xterm + +abbr() { + case "$1" in + /home/$USER/*) a='~'${1#/home/$USER} ;; + /home/$USER) a='~' ;; + /home/*) a='~'${1#/home/} ;; + *) a=$1 ;; + esac + printf '%s\n' "$a" +} + +if [ $# -eq 0 ]; then + xmessage -buttons sorry:0 -default sorry -nearmouse \ + "usage: $0 [-o xterm-opt] [-p prefix] cmd ..." + exit 1 +fi + +o= +p= +while getopts o:p: opt; do + case $opt in + o) o=$OPTARG ;; + p) p=$OPTARG ;; + ?) xmessage -buttons sorry:0 -default sorry -nearmouse \ + error: "unknown flag $1" + exit 1 ;; + esac +done +shift $((OPTIND-1)) + +if ! which "$1" >/dev/null; then + xmessage -default okay -nearmouse \ + error: "program $1 not found" + exit 1 +fi + +export LC_ALL=en_US.ISO8859-1 # speed up xterm start +exec xterm -title "$* (`abbr "$PWD"`)" $o -e u $p "$@" -- cgit v1.2.3