diff options
Diffstat (limited to 'etc')
-rwxr-xr-x | etc/P | 5 | ||||
-rwxr-xr-x | etc/ce | 28 | ||||
-rwxr-xr-x | etc/child | 27 | ||||
-rwxr-xr-x | etc/colortest | 15 | ||||
-rwxr-xr-x | etc/cpy | 3 | ||||
-rwxr-xr-x | etc/cuthere | 2 | ||||
-rwxr-xr-x | etc/ep | 9 | ||||
-rwxr-xr-x | etc/f | 10 | ||||
-rwxr-xr-x | etc/flip | 3 | ||||
-rwxr-xr-x | etc/g | 30 | ||||
-rwxr-xr-x | etc/gspell | 18 | ||||
-rw-r--r-- | etc/imgur | 123 | ||||
-rwxr-xr-x | etc/inc | 3 | ||||
-rwxr-xr-x | etc/isort | 21 | ||||
-rwxr-xr-x | etc/lorem | 5 | ||||
-rwxr-xr-x | etc/mkmv | 23 | ||||
-rwxr-xr-x | etc/ord | 56 | ||||
-rwxr-xr-x | etc/p | 7 | ||||
-rwxr-xr-x | etc/pst | 17 | ||||
-rwxr-xr-x | etc/re! | 48 | ||||
-rwxr-xr-x | etc/wpdf | 76 | ||||
-rwxr-xr-x | etc/xtopen | 41 |
22 files changed, 570 insertions, 0 deletions
@@ -0,0 +1,5 @@ +#!/bin/sh + +# P -- open file processed with p in PAGER + +"$PAGER" /var/tmp/pg @@ -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 @@ -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<------------------------------------' @@ -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 @@ -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); @@ -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 <bart@tremby.net> +# Improvements by Tino Sino <robottinosino@gmail.com> +# Made POSIX-compliant by John Ankarström <john@ankarstrom.se> +# 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) [<filename|URL> [...]]" >&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##*<error>}" + echo "${msg%%</error>*}" >&2 + errors=true + continue + fi + + # Parse the response and output our stuff + url="${response##*<link>}" + url="${url%%</link>*}" + delete_hash="${response##*<deletehash>}" + delete_hash="${delete_hash%%</deletehash>*}" + 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 @@ -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 <<END +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +END diff --git a/etc/mkmv b/etc/mkmv new file mode 100755 index 0000000..0179bbc --- /dev/null +++ b/etc/mkmv @@ -0,0 +1,23 @@ +#!/usr/bin/perl -w + +# mkmv -- move files to new directory + +use Getopt::Std; +use File::Copy qw/mv/; + +getopts('t:', \%opt); +&usage if not $opt{t}; +&usage if not @ARGV; + +mkdir "$opt{t}.tmp" or die "Could not create directory $opt{t}.tmp: $!\n"; +mv $_, "$opt{t}.tmp/$_" or warn "Could not move $_ to $opt{t}/$_: $!\n" for @ARGV; +mv "$opt{t}.tmp", $opt{t} or warn "Could not move $opt{t}.tmp to $opt{t}: $!\n"; + +sub usage { &HELP_MESSAGE; exit 1; } +sub HELP_MESSAGE { + print STDERR <<OPT; +usage: $0 -t target file [...] + -t the target directory +OPT +} +sub VERSION_MESSAGE {} @@ -0,0 +1,56 @@ +#!/bin/sh + +# Order files by adding/changing numbers (like "01. ") in front of their names + +usage() { echo usage: $0 [-n number width] [-s separator] 1>&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 @@ -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 @@ -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 @@ -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 "$@" |