1 | """Methods on IO type"""
|
2 | from __future__ import print_function
|
3 |
|
4 | from _devbuild.gen.value_asdl import value, value_e, value_t
|
5 |
|
6 | from core import error
|
7 | from core import num
|
8 | from core import state
|
9 | from core import vm
|
10 | from frontend import typed_args
|
11 | from mycpp.mylib import log, NewDict
|
12 | from osh import prompt
|
13 |
|
14 | from typing import Dict, List, cast, TYPE_CHECKING
|
15 | if TYPE_CHECKING:
|
16 | from osh import cmd_eval
|
17 | from _devbuild.gen.runtime_asdl import Cell
|
18 |
|
19 | _ = log
|
20 |
|
21 | EVAL_NULL = 1
|
22 | EVAL_DICT = 2
|
23 |
|
24 |
|
25 | def _PrintFrame(prefix, frame):
|
26 | # type: (str, Dict[str, Cell]) -> None
|
27 | print('%s %s' % (prefix, ' '.join(frame.keys())))
|
28 |
|
29 | rear = frame.get('__E__')
|
30 | if rear:
|
31 | rear_val = rear.val
|
32 | if rear_val.tag() == value_e.Frame:
|
33 | r = cast(value.Frame, rear_val)
|
34 | _PrintFrame('--> ' + prefix, r.frame)
|
35 |
|
36 |
|
37 | class EvalInFrame(vm._Callable):
|
38 | """
|
39 | For making "inline procs"
|
40 | """
|
41 |
|
42 | def __init__(self, mem, cmd_ev):
|
43 | # type: (state.Mem, cmd_eval.CommandEvaluator) -> None
|
44 | self.mem = mem
|
45 | self.cmd_ev = cmd_ev
|
46 |
|
47 | def Call(self, rd):
|
48 | # type: (typed_args.Reader) -> value_t
|
49 | frag = rd.PosCommandFrag()
|
50 | bound = rd.PosFrame()
|
51 |
|
52 | # TODO: EvalCommandFrag()
|
53 |
|
54 | return value.Null
|
55 |
|
56 |
|
57 | class Eval(vm._Callable):
|
58 | """
|
59 | These are similar:
|
60 |
|
61 | var cmd = ^(echo hi)
|
62 | call io->eval(cmd)
|
63 |
|
64 | Also give the top namespace
|
65 |
|
66 | call io->evalToDict(cmd)
|
67 |
|
68 | The CALLER must handle errors.
|
69 | """
|
70 |
|
71 | def __init__(self, mem, cmd_ev, which):
|
72 | # type: (state.Mem, cmd_eval.CommandEvaluator, int) -> None
|
73 | self.mem = mem
|
74 | self.cmd_ev = cmd_ev
|
75 | self.which = which
|
76 |
|
77 | def Call(self, rd):
|
78 | # type: (typed_args.Reader) -> value_t
|
79 | unused = rd.PosValue()
|
80 | bound = rd.PosCommand()
|
81 |
|
82 | cmd = typed_args.GetCommandFrag(bound)
|
83 |
|
84 | dollar0 = rd.NamedStr("dollar0", None)
|
85 | pos_args_raw = rd.NamedList("pos_args", None)
|
86 | vars_ = rd.NamedDict("vars", None)
|
87 | rd.Done()
|
88 |
|
89 | pos_args = None # type: List[str]
|
90 | if pos_args_raw is not None:
|
91 | pos_args = []
|
92 | for arg in pos_args_raw:
|
93 | if arg.tag() != value_e.Str:
|
94 | raise error.TypeErr(
|
95 | arg, "Expected pos_args to be a List of Strs",
|
96 | rd.LeftParenToken())
|
97 |
|
98 | pos_args.append(cast(value.Str, arg).s)
|
99 |
|
100 | if self.which == EVAL_NULL:
|
101 | # _PrintFrame('[captured]', captured_frame)
|
102 | with state.ctx_EnclosedFrame(self.mem, bound.captured_frame,
|
103 | bound.module_frame, None):
|
104 | # _PrintFrame('[new]', self.cmd_ev.mem.var_stack[-1])
|
105 | with state.ctx_Eval(self.mem, dollar0, pos_args, vars_):
|
106 | unused_status = self.cmd_ev.EvalCommandFrag(cmd)
|
107 | return value.Null
|
108 |
|
109 | elif self.which == EVAL_DICT:
|
110 | # TODO: dollar0, pos_args, vars_ not supported
|
111 | # Does ctx_EnclosedFrame has different scoping rules? For "vars"?
|
112 |
|
113 | bindings = NewDict() # type: Dict[str, value_t]
|
114 | with state.ctx_EnclosedFrame(self.mem, bound.captured_frame,
|
115 | bound.module_frame, bindings):
|
116 | unused_status = self.cmd_ev.EvalCommandFrag(cmd)
|
117 | return value.Dict(bindings)
|
118 |
|
119 | else:
|
120 | raise AssertionError()
|
121 |
|
122 |
|
123 | class CaptureStdout(vm._Callable):
|
124 |
|
125 | def __init__(self, mem, shell_ex):
|
126 | # type: (state.Mem, vm._Executor) -> None
|
127 | self.mem = mem
|
128 | self.shell_ex = shell_ex
|
129 |
|
130 | def Call(self, rd):
|
131 | # type: (typed_args.Reader) -> value_t
|
132 |
|
133 | unused = rd.PosValue()
|
134 | cmd = rd.PosCommand()
|
135 | rd.Done() # no more args
|
136 |
|
137 | frag = typed_args.GetCommandFrag(cmd)
|
138 | with state.ctx_EnclosedFrame(self.mem, cmd.captured_frame,
|
139 | cmd.module_frame, None):
|
140 | status, stdout_str = self.shell_ex.CaptureStdout(frag)
|
141 | if status != 0:
|
142 | # Note that $() raises error.ErrExit with the status.
|
143 | # But I think that results in a more confusing error message, so we
|
144 | # "wrap" the errors.
|
145 | properties = {
|
146 | 'status': num.ToBig(status)
|
147 | } # type: Dict[str, value_t]
|
148 | raise error.Structured(
|
149 | 4, 'captureStdout(): command failed with status %d' % status,
|
150 | rd.LeftParenToken(), properties)
|
151 |
|
152 | return value.Str(stdout_str)
|
153 |
|
154 |
|
155 | class PromptVal(vm._Callable):
|
156 | """
|
157 | _io->promptVal('$') is like \$
|
158 | It expands to $ or # when root
|
159 | """
|
160 |
|
161 | def __init__(self, prompt_ev):
|
162 | # type: (prompt.Evaluator) -> None
|
163 | self.prompt_ev = prompt_ev
|
164 |
|
165 | def Call(self, rd):
|
166 | # type: (typed_args.Reader) -> value_t
|
167 |
|
168 | # "self" param is guaranteed to succeed
|
169 | unused = rd.PosValue()
|
170 | what = rd.PosStr()
|
171 | rd.Done() # no more args
|
172 |
|
173 | # Bug fix: protect against crash later in PromptVal()
|
174 | if len(what) != 1:
|
175 | raise error.Expr(
|
176 | 'promptVal() expected a single char, got %r' % what,
|
177 | rd.LeftParenToken())
|
178 |
|
179 | return value.Str(self.prompt_ev.PromptVal(what))
|
180 |
|
181 |
|
182 | # TODO: Implement these
|
183 |
|
184 |
|
185 | class Time(vm._Callable):
|
186 |
|
187 | def __init__(self):
|
188 | # type: () -> None
|
189 | pass
|
190 |
|
191 | def Call(self, rd):
|
192 | # type: (typed_args.Reader) -> value_t
|
193 | return value.Null
|
194 |
|
195 |
|
196 | class Strftime(vm._Callable):
|
197 |
|
198 | def __init__(self):
|
199 | # type: () -> None
|
200 | pass
|
201 |
|
202 | def Call(self, rd):
|
203 | # type: (typed_args.Reader) -> value_t
|
204 | return value.Null
|
205 |
|
206 |
|
207 | class Glob(vm._Callable):
|
208 |
|
209 | def __init__(self):
|
210 | # type: () -> None
|
211 | pass
|
212 |
|
213 | def Call(self, rd):
|
214 | # type: (typed_args.Reader) -> value_t
|
215 | return value.Null
|