| 1 | """Methods on IO type"""
|
| 2 | from __future__ import print_function
|
| 3 |
|
| 4 | from _devbuild.gen.value_asdl import value, value_t
|
| 5 |
|
| 6 | from core import error
|
| 7 | from core import num
|
| 8 | from core import vm
|
| 9 | from mycpp.mylib import log
|
| 10 | from osh import prompt
|
| 11 |
|
| 12 | from typing import Dict, cast, TYPE_CHECKING
|
| 13 | if TYPE_CHECKING:
|
| 14 | from frontend import typed_args
|
| 15 |
|
| 16 | _ = log
|
| 17 |
|
| 18 |
|
| 19 | class Eval(vm._Callable):
|
| 20 |
|
| 21 | def __init__(self):
|
| 22 | # type: () -> None
|
| 23 | pass
|
| 24 |
|
| 25 | def Call(self, rd):
|
| 26 | # type: (typed_args.Reader) -> value_t
|
| 27 | return value.Null
|
| 28 |
|
| 29 |
|
| 30 | class CaptureStdout(vm._Callable):
|
| 31 |
|
| 32 | def __init__(self, shell_ex):
|
| 33 | # type: (vm._Executor) -> None
|
| 34 | self.shell_ex = shell_ex
|
| 35 |
|
| 36 | def Call(self, rd):
|
| 37 | # type: (typed_args.Reader) -> value_t
|
| 38 |
|
| 39 | io = rd.PosIO()
|
| 40 | cmd = rd.PosCommand()
|
| 41 | rd.Done() # no more args
|
| 42 |
|
| 43 | status, stdout_str = self.shell_ex.CaptureStdout(cmd)
|
| 44 | if status != 0:
|
| 45 | properties = {
|
| 46 | 'status': num.ToBig(status)
|
| 47 | } # type: Dict[str, value_t]
|
| 48 | raise error.Structured(
|
| 49 | 4, 'Captured command failed with status %d' % status,
|
| 50 | rd.LeftParenToken(), properties)
|
| 51 |
|
| 52 | return value.Str(stdout_str)
|
| 53 |
|
| 54 |
|
| 55 | class PromptVal(vm._Callable):
|
| 56 | """
|
| 57 | _io->promptVal('$') is like \$
|
| 58 | It expands to $ or # when root
|
| 59 | """
|
| 60 |
|
| 61 | def __init__(self):
|
| 62 | # type: () -> None
|
| 63 | pass
|
| 64 |
|
| 65 | def Call(self, rd):
|
| 66 | # type: (typed_args.Reader) -> value_t
|
| 67 |
|
| 68 | # "self" param is guaranteed to succeed
|
| 69 | io = rd.PosIO()
|
| 70 | what = rd.PosStr()
|
| 71 | rd.Done() # no more args
|
| 72 |
|
| 73 | # Bug fix: protect against crash later in PromptVal()
|
| 74 | if len(what) != 1:
|
| 75 | raise error.Expr(
|
| 76 | 'promptVal() expected a single char, got %r' % what,
|
| 77 | rd.LeftParenToken())
|
| 78 |
|
| 79 | prompt_ev = cast(prompt.Evaluator, io.prompt_ev)
|
| 80 | return value.Str(prompt_ev.PromptVal(what))
|
| 81 |
|
| 82 |
|
| 83 | class Time(vm._Callable):
|
| 84 |
|
| 85 | def __init__(self):
|
| 86 | # type: () -> None
|
| 87 | pass
|
| 88 |
|
| 89 | def Call(self, rd):
|
| 90 | # type: (typed_args.Reader) -> value_t
|
| 91 | return value.Null
|
| 92 |
|
| 93 |
|
| 94 | class Strftime(vm._Callable):
|
| 95 |
|
| 96 | def __init__(self):
|
| 97 | # type: () -> None
|
| 98 | pass
|
| 99 |
|
| 100 | def Call(self, rd):
|
| 101 | # type: (typed_args.Reader) -> value_t
|
| 102 | return value.Null
|