Skip to content

Commit

Permalink
[interactive] Initial implementation of PROMPT_COMMAND (#414)
Browse files Browse the repository at this point in the history
- Location information is wrong - PROMPT_COMMAND=';' gives a traceback pointing to ~/.local/oil/oshrc instead of [ interactive ] or [ PROMPT_COMMAND ]
- Don't let command change status

Addresses #400
  • Loading branch information
jyn514 authored and andychu committed Jul 17, 2019
1 parent c332b8b commit 54272e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
20 changes: 19 additions & 1 deletion core/main_loop.py
Expand Up @@ -18,9 +18,9 @@
command_t, command,
parse_result__EmptyLine, parse_result__Eof, parse_result__Node
)
from _devbuild.gen.runtime_asdl import value_e, arg_vector
from core import ui
from core import util
#from core.util import log

from typing import Any, Optional, List, TYPE_CHECKING
if TYPE_CHECKING:
Expand All @@ -31,6 +31,23 @@
#from osh.cmd_exec import Executor


def _ExecutePromptCommand(ex):
# type: (Any) -> None
prompt_var = ex.mem.GetVar('PROMPT_COMMAND')
# Undefined, Array, or AssociativeArray
if prompt_var.tag != value_e.Str:
return
command = prompt_var.s

# save this so PROMPT_COMMAND can't set $?
ex.mem.PushStatusFrame()
arg_vec = arg_vector(['PROMPT_COMMAND', command], [0, 0])
try:
ex._Eval(arg_vec)
finally:
ex.mem.PopStatusFrame()


def Interactive(opts, ex, c_parser, display, errfmt):
# type: (Any, Any, CommandParser, Any, ErrorFormatter) -> Any
status = 0
Expand All @@ -42,6 +59,7 @@ def Interactive(opts, ex, c_parser, display, errfmt):
# it appears in all branches.

while True: # ONLY EXECUTES ONCE
_ExecutePromptCommand(ex)
try:
# may raise HistoryError or ParseError
result = c_parser.ParseInteractiveLine()
Expand Down
16 changes: 15 additions & 1 deletion spec/interactive.test.sh
Expand Up @@ -20,7 +20,11 @@ one
#### fatal errors continue

# NOTE: tried here doc, but sys.stdin.isatty() fails. Could we fake it?
$SH -i -c '
case "$SH" in
*bash) FLAGS='--noprofile --norc';;
*osh) FLAGS='--rcfile /dev/null';;
esac
$SH $FLAGS -i -c '
echo $(( 1 / 0 ))
echo one
exit 42
Expand Down Expand Up @@ -51,3 +55,13 @@ RCFILE
## N-I dash status: 2
## N-I mksh status: 1

#### interactive shell runs PROMPT_COMMAND after each command
TEST_CASE='PROMPT_COMMAND="echo hi"'
case $SH in
*bash) echo "$TEST_CASE" | $SH --noprofile --norc -i;;
*osh) $SH --rcfile /dev/null -i -c "$TEST_CASE";;
*) $SH -i -c "$TEST_CASE";;
esac
## STDOUT:
hi
## N-I dash/mksh stdout-json: ""

0 comments on commit 54272e8

Please sign in to comment.