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
|
10 | from _devbuild.gen.value_asdl import value, value_e
|
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 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 | # Show it with location
|
71 | # It looks like
|
72 | # pp (42)
|
73 | # ^
|
74 | # [ stdin ]:5: (Int) 42
|
75 | # We could also print with ! or -^-
|
76 |
|
77 | self.stdout_.write('\n')
|
78 | excerpt, prefix = ui.CodeExcerptAndPrefix(blame_tok)
|
79 | self.stdout_.write(excerpt)
|
80 | ui.PrettyPrintValue(prefix, val, self.stdout_)
|
81 |
|
82 | return 0
|
83 |
|
84 | def Run(self, cmd_val):
|
85 | # type: (cmd_value.Argv) -> int
|
86 | arg, arg_r = flag_util.ParseCmdVal('pp',
|
87 | cmd_val,
|
88 | accept_typed_args=True)
|
89 |
|
90 | action, action_loc = arg_r.Peek2()
|
91 |
|
92 | # Special cases
|
93 | # pp (x) quotes its code location, can also be pp [x]
|
94 | if action is None:
|
95 | return self._PrettyPrint(cmd_val)
|
96 |
|
97 | arg_r.Next()
|
98 |
|
99 | if action == 'value':
|
100 | # pp value (x) prints in the same way that '= x' does
|
101 | rd = typed_args.ReaderForProc(cmd_val)
|
102 | val = rd.PosValue()
|
103 | rd.Done()
|
104 |
|
105 | ui.PrettyPrintValue('', val, self.stdout_)
|
106 | return 0
|
107 |
|
108 | if action == 'asdl_':
|
109 | # TODO: could be pp asdl_ (x, y, z)
|
110 | rd = typed_args.ReaderForProc(cmd_val)
|
111 | val = rd.PosValue()
|
112 | rd.Done()
|
113 |
|
114 | tree = val.PrettyTree()
|
115 | #tree = val.AbbreviatedTree() # I used this to test cycle detection
|
116 |
|
117 | # TODO: ASDL should print the IDs. And then they will be
|
118 | # line-wrapped.
|
119 | # The IDs should also be used to detect cycles, and omit values
|
120 | # already printed.
|
121 | #id_str = vm.ValueIdString(val)
|
122 | #f.write(' <%s%s>\n' % (ysh_type, id_str))
|
123 |
|
124 | pretty_f = fmt.DetectConsoleOutput(self.stdout_)
|
125 | fmt.PrintTree(tree, pretty_f)
|
126 | self.stdout_.write('\n')
|
127 |
|
128 | return 0
|
129 |
|
130 | if action == 'test_': # Print format for spec tests
|
131 | # TODO: could be pp test_ (x, y, z)
|
132 | rd = typed_args.ReaderForProc(cmd_val)
|
133 | val = rd.PosValue()
|
134 | rd.Done()
|
135 |
|
136 | if ui.TypeNotPrinted(val):
|
137 | ysh_type = ui.ValType(val)
|
138 | self.stdout_.write('(%s) ' % ysh_type)
|
139 |
|
140 | j8.PrintLine(val, self.stdout_)
|
141 |
|
142 | return 0
|
143 |
|
144 | if action == 'cell_': # Format may change
|
145 | argv, locs = arg_r.Rest2()
|
146 |
|
147 | status = 0
|
148 | for i, name in enumerate(argv):
|
149 |
|
150 | if not match.IsValidVarName(name):
|
151 | raise error.Usage('got invalid variable name %r' % name,
|
152 | locs[i])
|
153 |
|
154 | cell = self.mem.GetCell(name)
|
155 | if cell is None:
|
156 | self.errfmt.Print_("Couldn't find a variable named %r" %
|
157 | name,
|
158 | blame_loc=locs[i])
|
159 | status = 1
|
160 | else:
|
161 | self.stdout_.write('%s = ' % name)
|
162 | pretty_f = fmt.DetectConsoleOutput(self.stdout_)
|
163 | fmt.PrintTree(cell.PrettyTree(), pretty_f)
|
164 | self.stdout_.write('\n')
|
165 | return status
|
166 |
|
167 | if action == 'stacks_': # Format may change
|
168 | if mylib.PYTHON:
|
169 | var_stack, argv_stack, unused = self.mem.Dump()
|
170 | print(var_stack)
|
171 | print('===')
|
172 | print(argv_stack)
|
173 | if 0:
|
174 | var_stack = self.mem.var_stack
|
175 | for i, frame in enumerate(var_stack):
|
176 | print('=== Frame %d' % i)
|
177 | for name, cell in iteritems(frame):
|
178 | print('%s = %s' % (name, cell))
|
179 |
|
180 | return 0
|
181 |
|
182 | if action == 'frame_vars_': # Print names in current frame, for testing
|
183 | top = self.mem.var_stack[-1]
|
184 | print(' [frame_vars_] %s' % ' '.join(top.keys()))
|
185 | return 0
|
186 |
|
187 | if action == 'gc-stats_':
|
188 | print('TODO')
|
189 | return 0
|
190 |
|
191 | if action == 'proc':
|
192 | names, locs = arg_r.Rest2()
|
193 | if len(names):
|
194 | for i, name in enumerate(names):
|
195 | node, _ = self.procs.GetInvokable(name)
|
196 | if node is None:
|
197 | self.errfmt.Print_('Invalid proc %r' % name,
|
198 | blame_loc=locs[i])
|
199 | return 1
|
200 | else:
|
201 | names = self.procs.InvokableNames()
|
202 |
|
203 | # TSV8 header
|
204 | print('proc_name\tdoc_comment')
|
205 | for name in names:
|
206 | proc_val, _ = self.procs.GetInvokable(name) # must exist
|
207 | if proc_val.tag() != value_e.Proc:
|
208 | continue # can't be value.BuiltinProc
|
209 | user_proc = cast(value.Proc, proc_val)
|
210 |
|
211 | body = user_proc.body
|
212 |
|
213 | # TODO: not just command.ShFunction, but command.Proc!
|
214 | doc = ''
|
215 | if body.tag() == command_e.BraceGroup:
|
216 | bgroup = cast(BraceGroup, body)
|
217 | if bgroup.doc_token:
|
218 | token = bgroup.doc_token
|
219 | # 1 to remove leading space
|
220 | doc = token.line.content[token.col + 1:token.col +
|
221 | token.length]
|
222 |
|
223 | # Note: these should be attributes on value.Proc
|
224 | buf = mylib.BufWriter()
|
225 | j8.EncodeString(name, buf, unquoted_ok=True)
|
226 | buf.write('\t')
|
227 | j8.EncodeString(doc, buf, unquoted_ok=True)
|
228 | print(buf.getvalue())
|
229 |
|
230 | return 0
|
231 |
|
232 | e_usage('got invalid action %r' % action, action_loc)
|
233 | #return status
|
234 |
|
235 |
|
236 | class Write(_Builtin):
|
237 | """
|
238 | write -- @strs
|
239 | write --sep ' ' --end '' -- @strs
|
240 | write -n -- @
|
241 | write --j8 -- @strs # argv serialization
|
242 | write --j8 --sep $'\t' -- @strs # this is like TSV8
|
243 | """
|
244 |
|
245 | def __init__(self, mem, errfmt):
|
246 | # type: (state.Mem, ui.ErrorFormatter) -> None
|
247 | _Builtin.__init__(self, mem, errfmt)
|
248 | self.stdout_ = mylib.Stdout()
|
249 |
|
250 | def Run(self, cmd_val):
|
251 | # type: (cmd_value.Argv) -> int
|
252 | attrs, arg_r = flag_util.ParseCmdVal('write', cmd_val)
|
253 | arg = arg_types.write(attrs.attrs)
|
254 | #print(arg)
|
255 |
|
256 | i = 0
|
257 | while not arg_r.AtEnd():
|
258 | if i != 0:
|
259 | self.stdout_.write(arg.sep)
|
260 | s = arg_r.Peek()
|
261 |
|
262 | if arg.json:
|
263 | s = j8.MaybeEncodeJsonString(s)
|
264 |
|
265 | elif arg.j8:
|
266 | s = j8.MaybeEncodeString(s)
|
267 |
|
268 | self.stdout_.write(s)
|
269 |
|
270 | arg_r.Next()
|
271 | i += 1
|
272 |
|
273 | if arg.n:
|
274 | pass
|
275 | elif len(arg.end):
|
276 | self.stdout_.write(arg.end)
|
277 |
|
278 | return 0
|
279 |
|
280 |
|
281 | class RunBlock(vm._Builtin):
|
282 | """Used for 'redir' builtin
|
283 |
|
284 | It's used solely for its redirects.
|
285 | redir >out.txt { echo hi }
|
286 | """
|
287 |
|
288 | def __init__(self, mem, cmd_ev):
|
289 | # type: (state.Mem, cmd_eval.CommandEvaluator) -> None
|
290 | self.mem = mem
|
291 | self.cmd_ev = cmd_ev # To run blocks
|
292 |
|
293 | def Run(self, cmd_val):
|
294 | # type: (cmd_value.Argv) -> int
|
295 | _, arg_r = flag_util.ParseCmdVal('redir',
|
296 | cmd_val,
|
297 | accept_typed_args=True)
|
298 |
|
299 | cmd_frag = typed_args.RequiredBlockAsFrag(cmd_val)
|
300 | unused = self.cmd_ev.EvalCommandFrag(cmd_frag)
|
301 | return 0
|