Browse Source

version 3 (bug fixes)

main
Blau Araujo 11 months ago
parent
commit
3737391d37
  1. 1000
      -1
  2. 53
      cawk
  3. 2
      cawk.1

1000
-1

File diff suppressed because it is too large

53
cawk

@ -2,7 +2,7 @@
# -----------------------------------------------------------------------------
# Script : cawk
# Descrição: REPL calculator in bash and GNU AWK.
# Versão : 0.0.1
# Versão : 0.0.3
# Data : 09/03/2022
# Licença : GNU/GPL v3.0
# -----------------------------------------------------------------------------
@ -27,7 +27,7 @@
# COMMAND | cawk [-f PRECISION] - Eval expressions from COMMAND output.
# -----------------------------------------------------------------------------
version="0.0.1"
version="0.0.3"
error[0]="Exiting..."
error[1]="Invalid expression!"
@ -76,29 +76,26 @@ die() {
}
calc() {
local exp fix
local exp fix res
exp="$1"
fix=$2
awk -M 'BEGIN {
printf "%.'$fix'f\n", ('"$exp"')
}' 2> /dev/null || die 1
res=$(awk -M 'BEGIN { printf "%.'$fix'f\n", ('"$exp"') }' 2> /dev/null)
[[ $res ]] && EXPRESSION_VAL="$res" || EXPRESSION_ERROR=1
}
repl() {
while :; do
read -erp ': '
history -s "$REPLY"
history -s -- "$REPLY"
case "${REPLY,,}" in
f*( )+([0-9])) PRECISION=${REPLY:1}
;;
help|h|'?') echo "$help"
;;
q|quit) die 0
;;
*) local r=$(calc "$REPLY" $PRECISION)
echo "$r"
history -s "$r"
;;
(f*( )+([0-9])) PRECISION=${REPLY:1}
;;
help|h|'?') echo "$help"
;;
q|quit) die 0
;;
*) eval_expression "$REPLY" $PRECISION IS_REPL || continue
;;
esac
done
}
@ -117,6 +114,19 @@ opts() {
done
}
eval_expression() {
# Reset globals...
EXPRESSION_ERROR=0
EXPRESSION_VAL=''
calc "$1" $2
if (( EXPRESSION_ERROR )); then
printf '\e[33;1m%s\e[m\n' "${error[1]}"
return 1
fi
[[ $3 = 'IS_REPL' ]] && history -s -- "$EXPRESSION_VAL"
echo "$EXPRESSION_VAL"
}
# main ------------------------------------------------------------------------
unset EXPRESSION
@ -125,10 +135,13 @@ PRECISION=2
opts "$@"
if [[ -t 0 ]];then
[[ $EXPRESSION ]] && { calc "$EXPRESSION" $PRECISION; exit; }
repl $PRECISION
if [[ $EXPRESSION ]]; then
eval_expression "$EXPRESSION" $PRECISION || exit
else
repl $PRECISION
fi
else
while read; do
[[ $REPLY ]] && calc "$REPLY" $PRECISION
eval_expression "$REPLY" $PRECISION || exit
done
fi

2
cawk.1

@ -1,4 +1,4 @@
.TH CAWK "1" "Mar 2022" "CAWK 0.0.1" "REPL calculator in Bash and GNU awk"
.TH CAWK "1" "Mar 2022" "CAWK 0.0.3" "REPL calculator in Bash and GNU awk"
.\" Text automatically generated by txt2man
.RS
.SH NAME

Loading…
Cancel
Save