~mia/psh-fc

cc0d5d97a9f09ff8c13be75179c28f360bd26182 — mia 3 months ago fd8f2fe
add -o/-a & !; fix -fl; fix *.0 bug
2 files changed, 82 insertions(+), 31 deletions(-)

M README
M com
M README => README +5 -0
@@ 8,6 8,7 @@ Usage
-- $3 = number
-- eg:
---- 1.89 ^~ 2
---- 1.89 ~ 1
---- 1.5 >/-gt 1.04
---- 1 </-lt 3.14122
---- 1.0051 -ne 1


@@ 24,6 25,10 @@ Operator support:
---- -le/<=/=<: less than or equal to
---- -cl/^~/~^/^=: equal after ceiling
---- -fl/~/≈/≅: equal after floor
-- Additonal support for -a/-o and ! is now present
---- -a: literal and
---- -o: literal or
---- !: literal false
---
Caveats
-- note that while no math is used here

M com => com +77 -31
@@ 7,22 7,21 @@ set -e # exit 1 if any error
ceil() {
  # ceiling $1, compare to $2; return 1 if not the same
  [ -z "${1##*.*}" -o ! -z "${2##*.*}" ] && {
    [ ! -z "${1##*.*}" -a ! -z "${2##*.*}" -a "$1" -eq "$2" ] 2>/dev/null && exit 0
    [ ! -z "${1##*.*}" -a ! -z "${2##*.*}" -a "$1" -eq "$2" ] 2>/dev/null && return 0
    set -- "${1%.*}" "${2%.*}"
    set -- "$(($1+1))" "$2"
    [ "$1" -eq "$2" ] 2>/dev/null && {
      exit 0
      return 0
    } || return 1
  } || return 1
} # ceiling
floor() {
  # floor $1. compare to $2; return 1 if not the same
  [ -z "${1##*.*}" -o ! -z "${2##*.*}" ] && {
    [ ! -z "${1##*.*}" -a ! -z "${2##*.*}" -a "$1" -eq "$2" ] 2>/dev/null && exit 0
    [ ! -z "${1##*.*}" -a ! -z "${2##*.*}" -a "$1" -eq "$2" ] 2>/dev/null && return 0
    set -- "${1%.*}" "${2%.*}"
    set -- "$(($1-1))" "$2"
    [ "$1" -eq "$2" ] 2>/dev/null && {
      exit 0
      return 0
    } || return 1
  } || return 1
} # floor


@@ 40,12 39,12 @@ gt() {
    d2="${d2#0}"; d1="${d1}0"
  done
  [ "$3" ] && {
    [ "$d1" -gt "$d2" -o "$d1" -eq "$d2" -a "$w1" -eq "$w2" ] && {
      exit 0
    [ "${d1:-0}" -gt "${d2:-0}" -o "${d1:-0}" -eq "${d2:-0}" -a "${w1:-0}" -eq "${w2:-0}" ] && {
      return 0
    } || return 1
  } || {
    [ "$d1" -gt "$d2" ] && { 
      exit 0
    [ "${d1:-0}" -gt "${d2:-0}" ] && { 
      return 0
    } || return 1
  } # or equal to (-ge)
} # test if $1 is > $2


@@ 60,12 59,12 @@ lt() {
    d2="${d2#0}"; d1="${d1}0"
  done
  [ "$3" ] && {
    [ "$d1" -lt "$d2" -o "$d1" -eq "$d2" -a "$w1" -eq "$w2" ] && {
      exit 0
    [ "${d1:-0}" -lt "${d2:-0}" -o "${d1:-0}" -eq "${d2:-0}" -a "${w1:-0}" -eq "${w2:-0}" ] && {
      return 0
    } || return 1
  } || {
    [ "$d1" -lt "$d2" ] && {
      exit 0
    [ "${d1:-0}" -lt "${d2:-0}" ] && {
      return 0
    } || return 1
  }
} # test if $1 is < $2


@@ 79,26 78,73 @@ eq() { # use $3 for -ne
    d2="${d2#0}"; d1="${d1}0"
  done
  [ "$3" ] && {
    [ "$w1" -ne "$w2" -a "$d1" -ne "$d2" ] && {
      exit 0
    [ "${w1:-0}" -ne "${w2:-0}" -a "${d1:-0}" -ne "${d2:-0}" ] && {
      return 0
    } || return 1
  } || {
    [ "$w1" -eq "$w2" -a "$d1" -eq "$d2" ] && { 
      exit 0
    [ "${w1:-0}" -eq "${w2:-0}" -a "${d1:-0}" -eq "${d2:-0}" ] && { 
      return 0
    } || return 1
  }
}
n1="$1"; op="$2"; n2="$3"
case "$op" in
  '^~'|'~^'|'^='|'-cl') ceil "$n1" "$n2" || exit 1 ;;
  '~'|'≈'|'≅'|'-fl') floor "$n1" "$n2" || exit 1 ;;
  '>'|'-gt') gt "$n1" "$n2" || exit 1 ;;
  '<'|'-lt') lt "$n1" "$n2" || exit 1 ;;
  '-ge'|'>='|'=>') gt "$n1" "$n2" "-" || exit 1 ;;
  '-le'|'<='|'=<') lt "$n1" "$n2" "-" || exit 1 ;;
  # or equal to is set via $3
  '-eq'|'=='|'=') eq "$n1" "$n2" || exit 1 ;;
  '-ne'|'!=='|'!=') eq "$n1" "$n2" "-" || exit 1
  # -ne uses $3 with -eq
esac
exit 0 # exit 0 if done
hexit() { # handle exits 
# allow for ! handling
  ecode="$1"
  [ "$FALSE" ] && case "$ecode" in
      0) ecode=1 ;;
      1) ecode=0
    esac
  unset FALSE
  case "$@" in
    *'-a'*) [ "$ecode" -eq 0 ] && {
      args="${@}"; args="${args##*:}"
      ${0} ${args##? $op ? -a }; ecode=$? 
    } || exit $ecode ;;
    # if exit code is != 0 then all other calls for -a don't matter
    # as a single 1 will cause the final result to be 1
    *'-o'*) [ "$ecode" -eq 1 ] && {
      args="${@}"; args="${args##*:}" 
      ${0} ${args##? $op ? -o }; ecode=$?
    } || exit $ecode
    # as above
    ##
    # this could easily cause a runaway loop if one tried to use an excessive amount of -a/-o calls
    # but I don't think thats really worth creating an exception for
  esac
  exit $ecode
}
com() { # this allows for recusion for -a/-o handling
  n1="$1"; op="$2"; n2="$3"
  [ "$1" ] || exit 1
  if [ "$1" = '!' ] && [ "$2" ]; then
    FALSE=0; shift 1
    n1="$1"; op="$2"; n2="$3"
    [ "$1" ] || exit 1
  fi # -a breaks here ??
  [ ! "$3" ] && {
    [ "$FALSE" ] && {
      [ "$2" ] && hexit 0 ":$@"
      hexit 1 ":$@"
    } || {
      [ "$1" ] && hexit 0 ":$@"
      hexit 1 ":$@"
    }
  }
  # [ ! ] causes exit code to swap
  #echo "$@"
  case "$op" in
    '^~'|'~^'|'^='|'-cl') ceil "$n1" "$n2" || hexit 1 ":$@";;
    '~'|'≈'|'≅'|'-fl') floor "$n1" "$n2" || hexit 1 ":$@";;
    '>'|'-gt') gt "$n1" "$n2" || hexit 1 ":$@";;
    '<'|'-lt') lt "$n1" "$n2" || hexit 1 ":$@";;
    '-ge'|'>='|'=>') gt "$n1" "$n2" "-" || hexit 1 ":$@";;
    '-le'|'<='|'=<') lt "$n1" "$n2" "-" || hexit 1 ":$@";;
    # or equal to is set via $3
    '-eq'|'=='|'=') eq "$n1" "$n2" || hexit 1 ":$@";;
    '-ne'|'!=='|'!=') eq "$n1" "$n2" "-" || hexit 1 ":$@"
    # -ne uses $3 with -eq
  esac
  hexit 0 ":$@" # exit 0 if done
  echo "$? --"
}
com "$@"