File: define.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # define [options...] [words...]
  27 #
  28 # DEFine/lookup words locally using `dict`. The non-default online mode can
  29 # search words not found locally via dict://dict.org, using a non-encrypted
  30 # (not private) protocol via `curl`.
  31 #
  32 # The options are, available both in single and double-dash versions
  33 #
  34 #   -h, -help    show this help message
  35 #   -online      allow searching dict://dict.org for words not found locally
  36 
  37 
  38 online=0
  39 
  40 case "$1" in
  41     -h|--h|-help|--help)
  42         awk '/^# +define /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  43         exit 0
  44     ;;
  45 
  46     -online|--online) online=1; shift ;;
  47 
  48     --) shift ;;
  49 
  50     -*)
  51         printf "unsupported option '%s'\n" "$1" >&2
  52         exit 1
  53     ;;
  54 esac
  55 
  56 if [ $# -eq 0 ]; then
  57     awk '/^# +define /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
  58     exit 1
  59 fi
  60 
  61 gap=0
  62 options='-MKiCRS'
  63 if [ $# -eq 1 ]; then
  64     options="${options} --header=1"
  65 fi
  66 
  67 for word in "$@"; do
  68     [ "${gap}" -gt 0 ] && printf "\n"
  69     gap=1
  70     printf "%s\n" "${word}"
  71 
  72     if dict "${word}" 2> /dev/null; then
  73         continue
  74     fi
  75 
  76     [ "${online}" -eq 0 ] && continue
  77     curl --silent --show-error "dict://dict.org/d:${word}" 2>&1 | grep '^ '
  78 done | { less ${options} 2> /dev/null || less -RIMS 2> /dev/null || cat; }