You are not logged in.

#1 2013-08-01 09:29:13

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,716

wtf bash rewrite + wtf-vera acronyms database

I like to ask `wtf` what an acronym means, but the original wtf does not ship nearly enough of them.
I happened to discover the very great V.E.R.A. acronyms database, which I've converted to the wtf acronyms database format, i.e. the "WTF[tab]What the fuck" format. Just giving you an idea:

$ wc -l /usr/share/misc/acronyms*
   255 /usr/share/misc/acronyms
   353 /usr/share/misc/acronyms.comp
 11805 /usr/share/misc/acronyms.vera
 12413 total

I've uploaded the V.E.R.A. database for wtf as AUR/wtf-vera: https://aur.archlinux.org/packages/wtf-vera
I've also uploaded the V.E.R.A. database in Texinfo format as AUR/vera: https://aur.archlinux.org/packages/vera

I also happened to read the wtf code, which made me go "wtf". So I did it in bash, preserving mostly the same interface/behavior. Note that I removed falling back to whatis/pkg_info because it's one useless feature.
Anyway, here is my version of wtf:

#!/bin/bash

usage() {
  cat <<EOF
usage: wtf [-f dbfile] [is] <acronym>
default dbfiles: \$ACRONYMDB or /usr/share/misc/acronyms*
EOF
  exit $1
}

declare -a db_list db_files
db_list=($ACRONYMDB)

OPTIND=1
while getopts ':f:h' opt; do
  case $opt in
    h)
      usage 0
      ;;
    f)
      db_list=($OPTARG)
      ;;
    '?')
       printf "error: invalid option: \`%s'\n" "$OPTARG" >&2
       exit 1
       ;;
    ':')
       printf "error: option requires an argument: \`%s'\n" "$OPTARG" >&2
       exit 1
       ;;
  esac
done
shift $(( OPTIND -1 ))

if ! (( $# )); then
  usage 1
elif (( $# > 1 )) && [[ $1 == is ]]; then
  shift
fi

if ! (( ${#db_list[@]} )); then
  db_list=(/usr/share/misc/acronyms*)
fi

for (( i=0, j=0; i<${#db_list[@]}; i++ )); do
  f=${db_list[i]}
  if [[ -r $f ]] && [[ ! -d $f ]]; then
    db_files[j++]=$f
  else
    printf "warning: cannot open acronyms database file \`%s'\n" "$f" >&2
  fi
done

if ! (( j )); then
  printf "error: none of the acronyms database files are readable\n" >&2
  usage 1
fi

while (( $# )); do
  i=0
  for f in "${db_files[@]}"; do
#   printf "%% DB: %s\n" "${f##*/}" >&2
    grep -i "^$1	" "$f" && (( i++ )) # requires strictly well formatted db files
  done
  # TODO maybe fall back to whatis(1) / pkg_info(1) (?) like the original wtf does
  if ! (( i )); then
    printf "wtf, I don't know what %s means!\n" "$1" >&2
  fi
  shift;
done

# vim:ts=2:sw=2:et:

Most noticably I find the output of the original wtf really bad:

$ wtf msn
MSN: Microsoft Support Network (Internet, MS)
Monitoring cell Sequence Number (UNI, ATM)
Multiple Subscriber Number (Euro-, ISDN)

so I fixed it to do this:

$ wtf msn
MSN	Microsoft Support Network (Internet, MS)
MSN	Monitoring cell Sequence Number (UNI, ATM)
MSN	Multiple Subscriber Number (Euro-, ISDN)

Last edited by lolilolicon (2013-08-01 12:56:28)


This silver ladybug at line 28...

Offline

Board footer

Powered by FluxBB