aboutsummaryrefslogtreecommitdiff
path: root/repl
blob: f205b36fa8c74a4028c9b246b013f4324521b0ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
#
# Generic read-eval-print loop script.
#

# Parameters
BASENAME="`basename $0`"

# Shell
function _repl {
  local last_exit_code="0"
  local command="$1"

  shift

  # While Ctrl-C isn't typed, read STDIN and invoke a command
  while read -rep "${last_exit_code} ${command}> " STDIN; do
    history -s "$STDIN"
    ${command} $* ${STDIN[@]}
    last_exit_code="$?"
  done
}

# Check
if [ -z "$COMMAND" ]; then
  echo "usage: $BASENAME <command> [base-args]"
  exit 1
fi

# Dispatch
_repl $*