From 5a8c7ef0c86885d241cbbcfe4cb9d38e084a731a Mon Sep 17 00:00:00 2001 From: Andy Chu Date: Sun, 26 Jul 2020 10:47:44 -0700 Subject: [PATCH] [bash] Support program input with the -c flag This helps testing with Oil, and makes the interpreter more consistent with bash, Python, etc. --- bash/bf.bash | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/bash/bf.bash b/bash/bf.bash index c03fe00..1cf7eed 100755 --- a/bash/bf.bash +++ b/bash/bf.bash @@ -8,15 +8,32 @@ die() exit 1 } -if (( $# )); then - if [[ -e $1 ]]; then - program=$(< "$1") +# program comes from -c flag +program='' +while getopts 'c:' flag; do + case $flag in + c) + program=$OPTARG + ;; + '?') + exit 1 # usage error occured, and bash printed a diagnostic + ;; + esac +done + +if test -z "$program"; then # corner case: -c '' gets here + if (( $# )); then + # program comes from filename argument + if [[ -e $1 ]]; then + program=$(< "$1") + else + die "file '$1' does not exist" + fi else - die "file '$1' does not exist" + # program comes from stdin + mapfile -t + program=${MAPFILE[*]} fi -else - mapfile -t - program=${MAPFILE[*]} fi program=${program//[^'><+-.,[]']}