You are not logged in.

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

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

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

#2 2025-08-28 12:55:13

Gwyneth Llewelyn
Member
From: Neufreistadt, CDS, Second Life
Registered: 2025-08-28
Posts: 1
Website

Re: wtf bash rewrite + wtf-vera acronyms database

I know, I know... Necroposting here. But there are still a few wtf fans around!

I've updated the VERA acronym database from the GNU sources to version 1.24: https://gist.github.com/GwynethLlewelyn … 040bb1a197

As for the "missing code" on @lolilolicon — you could do something like this:

Where you have:
  ...
  # 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
  ...
Replace with something like this:
  ...
  if ! (( i )); then
        # Try whatis(1) next
        ans="$(whatis "$1" 2>/dev/null)"
        if [ $? -eq 0 ]; then
                echo "$ans" | sort -u
                exit 0
        fi

        # Try pkg-info(1) next
        ans="$(pkginfo -qI "$1" 2> /dev/null)"
        if [ $? -eq 0 ]; then
                echo "$1: $ans"
                exit 0
        fi

        # If called from pkgsrc package directory,
        # try querying pkgsrc's help facility next
        if [ -f ../../mk/bsd.pkg.mk ]; then
                ans="$(make help topic="$1")"
                if [ "$ans" != "No help found for $1." ]; then
                        echo "$1: $ans"
                fi
                exit 0
        fi
    printf "wtf, I don't know what %s means!\n" "$1" >&2
  fi
  ...

The above snippet is based on the macOS version (which, in turn, comes from FreeBSD).
Paths may require adjustment, though.


I'm just a virtual girl in a virtual world...

Offline

Board footer

Powered by FluxBB