1 | #!/usr/bin/env python2
|
2 | """
|
3 | builtin/io_ysh.py - YSH builtins that perform I/O
|
4 | """
|
5 | from __future__ import print_function
|
6 |
|
7 | from _devbuild.gen import arg_types
|
8 | from _devbuild.gen.runtime_asdl import cmd_value
|
9 | from _devbuild.gen.syntax_asdl import command_e, BraceGroup, loc
|
10 | from _devbuild.gen.value_asdl import value, value_e, value_t
|
11 | from asdl import format as fmt
|
12 | from core import error
|
13 | from core.error import e_usage
|
14 | from core import state
|
15 | from display import ui
|
16 | from core import vm
|
17 | from data_lang import j8
|
18 | from frontend import flag_util
|
19 | from frontend import match
|
20 | from frontend import typed_args
|
21 | from mycpp import mylib
|
22 | from mycpp.mylib import tagswitch, log, iteritems
|
23 |
|
24 | from typing import TYPE_CHECKING, cast
|
25 | if TYPE_CHECKING:
|
26 | from core.alloc import Arena
|
27 | from osh import cmd_eval
|
28 | from ysh import expr_eval
|
29 |
|
30 | _ = log
|
31 |
|
32 |
|
33 | class _Builtin(vm._Builtin):
|
34 |
|
35 | def __init__(self, mem, errfmt):
|
36 | # type: (state.Mem, ui.ErrorFormatter) -> None
|
37 | self.mem = mem
|
38 | self.errfmt = errfmt
|
39 |
|
40 |
|
41 | class Pp(_Builtin):
|
42 | """Given a list of variable names, print their values.
|
43 |
|
44 | 'pp cell a' is a lot easier to type than 'argv.py "${a[@]}"'.
|
45 | """
|
46 |
|
47 | def __init__(
|
48 | self,
|
49 | expr_ev, # type: expr_eval.ExprEvaluator
|
50 | mem, # type: state.Mem
|
51 | errfmt, # type: ui.ErrorFormatter
|
52 | procs, # type: state.Procs
|
53 | arena, # type: Arena
|
54 | ):
|
55 | # type: (...) -> None
|
56 | _Builtin.__init__(self, mem, errfmt)
|
57 | self.expr_ev = expr_ev
|
58 | self.procs = procs
|
59 | self.arena = arena
|
60 | self.stdout_ = mylib.Stdout()
|
61 |
|
62 | def _PrettyPrint(self, cmd_val):
|
63 | # type: (cmd_value.Argv) -> int
|
64 | rd = typed_args.ReaderForProc(cmd_val)
|
65 | val = rd.PosValue()
|
66 | rd.Done()
|
67 |
|
68 | blame_tok = rd.LeftParenToken()
|
69 |
|
70 | # It might be nice to add a string too, like
|
71 | # pp 'my annotation' (actual)
|
72 | # But the var name should meaningful in most cases
|
73 |
|
74 | UP_val = val
|
75 | result = None # type: value_t
|
76 | with tagswitch(val) as case:
|
77 | if case(value_e.Expr): # Destructured assert [true === f()]
|
78 | val = cast(value.Expr, UP_val)
|
79 |
|
80 | # In this case, we could get the unevaluated code string and
|
81 | # print it. Although quoting the line seems enough.
|
82 | result = self.expr_ev.EvalExpr(val.e, blame_tok)
|
83 | else:
|
84 | result = val
|
85 |
|
86 | # Show it with location
|
87 | self.stdout_.write('\n')
|
88 | excerpt, prefix = ui.CodeExcerptAndPrefix(blame_tok)
|
89 | self.stdout_.write(excerpt)
|
90 | ui.PrettyPrintValue(prefix, result, self.stdout_)
|
91 |
|
92 | return 0
|
93 |
|
94 | def Run(self, cmd_val):
|
95 | # type: (cmd_value.Argv) -> int
|
96 | arg, arg_r = flag_util.ParseCmdVal('pp',
|
97 | cmd_val,
|
98 | accept_typed_args=True)
|
99 |
|
100 | action, action_loc = arg_r.Peek2()
|
101 |
|
102 | # Special cases
|
103 | # pp (x) quotes its code location
|
104 | # pp [x] also evaluates
|
105 | if action is None:
|
106 | return self._PrettyPrint(cmd_val)
|
107 |
|
108 | arg_r.Next()
|
109 |
|
110 | if action == 'value':
|
111 | # pp value (x) prints in the same way that '= x' does
|
112 | rd = typed_args.ReaderForProc(cmd_val)
|
113 | val = rd.PosValue()
|
114 | rd.Done()
|
115 |
|
116 | ui.PrettyPrintValue('', val, self.stdout_)
|
117 | return 0
|
118 |
|
119 | if action == 'asdl_':
|
120 | # TODO: could be pp asdl_ (x, y, z)
|
121 | rd = typed_args.ReaderForProc(cmd_val)
|
122 | val = rd.PosValue()
|
123 | rd.Done()
|
124 |
|
125 | tree = val.PrettyTree()
|
126 | #tree = val.AbbreviatedTree() # I used this to test cycle detection
|
127 |
|
128 | # TODO: ASDL should print the IDs. And then they will be
|
129 | # line-wrapped.
|
130 | # The IDs should also be used to detect cycles, and omit values
|
131 | # already printed.
|
132 | #id_str = vm.ValueIdString(val)
|
133 | #f.write(' <%s%s>\n' % (ysh_type, id_str))
|
134 |
|
135 | pretty_f = fmt.DetectConsoleOutput(self.stdout_)
|
136 | fmt.PrintTree(tree, pretty_f)
|
137 | self.stdout_.write('\n')
|
138 |
|
139 | return 0
|
140 |
|
141 | if action == 'test_': # Print format for spec tests
|
142 | # TODO: could be pp test_ (x, y, z)
|
143 | rd = typed_args.ReaderForProc(cmd_val)
|
144 | val = rd.PosValue()
|
145 | rd.Done()
|
146 |
|
147 | if ui.TypeNotPrinted(val):
|
148 | ysh_type = ui.ValType(val)
|
149 | self.stdout_.write('(%s) ' % ysh_type)
|
150 |
|
151 | j8.PrintLine(val, self.stdout_)
|
152 |
|
153 | return 0
|
154 |
|
155 | if action == 'cell_': # Format may change
|
156 | argv, locs = arg_r.Rest2()
|
157 |
|
158 | status = 0
|
159 | for i, name in enumerate(argv):
|
160 |
|
161 | if not match.IsValidVarName(name):
|
162 | raise error.Usage('got invalid variable name %r' % name,
|
163 | locs[i])
|
164 |
|
165 | cell = self.mem.GetCell(name)
|
166 | if cell is None:
|
167 | self.errfmt.Print_("Couldn't find a variable named %r" %
|
168 | name,
|
169 | blame_loc=locs[i])
|
170 | status = 1
|
171 | else:
|
172 | self.stdout_.write('%s = ' % name)
|
173 | pretty_f = fmt.DetectConsoleOutput(self.stdout_)
|
174 | fmt.PrintTree(cell.PrettyTree(), pretty_f)
|
175 | self.stdout_.write('\n')
|
176 | return status
|
177 |
|
178 | if action == 'stacks_': # Format may change
|
179 | if mylib.PYTHON:
|
180 | var_stack, argv_stack, unused = self.mem.Dump()
|
181 | print(var_stack)
|
182 | print('===')
|
183 | print(argv_stack)
|
184 | if 0:
|
185 | var_stack = self.mem.var_stack
|
186 | for i, frame in enumerate(var_stack):
|
187 | print('=== Frame %d' % i)
|
188 | for name, cell in iteritems(frame):
|
189 | print('%s = %s' % (name, cell))
|
190 |
|
191 | return 0
|
192 |
|
193 | if action == 'gc-stats_':
|
194 | print('TODO')
|
195 | return 0
|
196 |
|
197 | if action == 'proc':
|
198 | names, locs = arg_r.Rest2()
|
199 | if len(names):
|
200 | for i, name in enumerate(names):
|
201 | node = self.procs.Get(name)
|
202 | if node is None:
|
203 | self.errfmt.Print_('Invalid proc %r' % name,
|
204 | blame_loc=locs[i])
|
205 | return 1
|
206 | else:
|
207 | names = self.procs.GetNames()
|
208 |
|
209 | # TSV8 header
|
210 | print('proc_name\tdoc_comment')
|
211 | for name in names:
|
212 | proc = self.procs.Get(name) # must exist
|
213 | #log('Proc %s', proc)
|
214 | body = proc.body
|
215 |
|
216 | # TODO: not just command.ShFunction, but command.Proc!
|
217 | doc = ''
|
218 | if body.tag() == command_e.BraceGroup:
|
219 | bgroup = cast(BraceGroup, body)
|
220 | if bgroup.doc_token:
|
221 | token = bgroup.doc_token
|
222 | # 1 to remove leading space
|
223 | doc = token.line.content[token.col + 1:token.col +
|
224 | token.length]
|
225 |
|
226 | # Note: these should be attributes on value.Proc
|
227 | buf = mylib.BufWriter()
|
228 | j8.EncodeString(name, buf, unquoted_ok=True)
|
229 | buf.write('\t')
|
230 | j8.EncodeString(doc, buf, unquoted_ok=True)
|
231 | print(buf.getvalue())
|
232 |
|
233 | return 0
|
234 |
|
235 | e_usage('got invalid action %r' % action, action_loc)
|
236 | #return status
|
237 |
|
238 |
|
239 | class Write(_Builtin):
|
240 | """
|
241 | write -- @strs
|
242 | write --sep ' ' --end '' -- @strs
|
243 | write -n -- @
|
244 | write --j8 -- @strs # argv serialization
|
245 | write --j8 --sep $'\t' -- @strs # this is like TSV8
|
246 | """
|
247 |
|
248 | def __init__(self, mem, errfmt):
|
249 | # type: (state.Mem, ui.ErrorFormatter) -> None
|
250 | _Builtin.__init__(self, mem, errfmt)
|
251 | self.stdout_ = mylib.Stdout()
|
252 |
|
253 | def Run(self, cmd_val):
|
254 | # type: (cmd_value.Argv) -> int
|
255 | attrs, arg_r = flag_util.ParseCmdVal('write', cmd_val)
|
256 | arg = arg_types.write(attrs.attrs)
|
257 | #print(arg)
|
258 |
|
259 | i = 0
|
260 | while not arg_r.AtEnd():
|
261 | if i != 0:
|
262 | self.stdout_.write(arg.sep)
|
263 | s = arg_r.Peek()
|
264 |
|
265 | if arg.json:
|
266 | s = j8.MaybeEncodeJsonString(s)
|
267 |
|
268 | elif arg.j8:
|
269 | s = j8.MaybeEncodeString(s)
|
270 |
|
271 | self.stdout_.write(s)
|
272 |
|
273 | arg_r.Next()
|
274 | i += 1
|
275 |
|
276 | if arg.n:
|
277 | pass
|
278 | elif len(arg.end):
|
279 | self.stdout_.write(arg.end)
|
280 |
|
281 | return 0
|
282 |
|
283 |
|
284 | class Fopen(vm._Builtin):
|
285 | """fopen does nothing but run a block.
|
286 |
|
287 | It's used solely for its redirects.
|
288 | fopen >out.txt { echo hi }
|
289 |
|
290 | It's a subset of eval
|
291 | eval >out.txt { echo hi }
|
292 | """
|
293 |
|
294 | def __init__(self, mem, cmd_ev):
|
295 | # type: (state.Mem, cmd_eval.CommandEvaluator) -> None
|
296 | self.mem = mem
|
297 | self.cmd_ev = cmd_ev # To run blocks
|
298 |
|
299 | def Run(self, cmd_val):
|
300 | # type: (cmd_value.Argv) -> int
|
301 | _, arg_r = flag_util.ParseCmdVal('fopen',
|
302 | cmd_val,
|
303 | accept_typed_args=True)
|
304 |
|
305 | cmd = typed_args.OptionalBlock(cmd_val)
|
306 | if not cmd:
|
307 | raise error.Usage('expected a block', loc.Missing)
|
308 |
|
309 | unused = self.cmd_ev.EvalCommand(cmd)
|
310 | return 0
|