| 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
|
| 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 core 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
|
| 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 | UP_val = val
|
| 69 | with tagswitch(val) as case:
|
| 70 | if case(value_e.Expr): # Destructured assert [true === f()]
|
| 71 | val = cast(value.Expr, UP_val)
|
| 72 | blame_tok = rd.LeftParenToken()
|
| 73 | result = self.expr_ev.EvalExpr(val.e, blame_tok)
|
| 74 |
|
| 75 | # Show it with location
|
| 76 | excerpt, prefix = ui.CodeExcerptAndPrefix(blame_tok)
|
| 77 | self.stdout_.write(excerpt)
|
| 78 | ui.PrettyPrintValue(prefix, result, self.stdout_)
|
| 79 | else:
|
| 80 | # IOError caught by caller
|
| 81 | ui.PrettyPrintValue('', val, self.stdout_)
|
| 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 | # pp (x) prints in the same way that '= x' does
|
| 93 | # TODO: We also need pp [x], which shows the expression
|
| 94 | if action is None:
|
| 95 | return self._PrettyPrint(cmd_val)
|
| 96 |
|
| 97 | arg_r.Next()
|
| 98 |
|
| 99 | # Actions that print unstable formats start with '.'
|
| 100 | if action == 'cell':
|
| 101 | argv, locs = arg_r.Rest2()
|
| 102 |
|
| 103 | status = 0
|
| 104 | for i, name in enumerate(argv):
|
| 105 | if name.startswith(':'):
|
| 106 | name = name[1:]
|
| 107 |
|
| 108 | if not match.IsValidVarName(name):
|
| 109 | raise error.Usage('got invalid variable name %r' % name,
|
| 110 | locs[i])
|
| 111 |
|
| 112 | cell = self.mem.GetCell(name)
|
| 113 | if cell is None:
|
| 114 | self.errfmt.Print_("Couldn't find a variable named %r" %
|
| 115 | name,
|
| 116 | blame_loc=locs[i])
|
| 117 | status = 1
|
| 118 | else:
|
| 119 | self.stdout_.write('%s = ' % name)
|
| 120 | pretty_f = fmt.DetectConsoleOutput(self.stdout_)
|
| 121 | fmt.PrintTree(cell.PrettyTree(), pretty_f)
|
| 122 | self.stdout_.write('\n')
|
| 123 |
|
| 124 | elif action == 'asdl':
|
| 125 | # TODO: could be pp asdl (x, y, z)
|
| 126 | rd = typed_args.ReaderForProc(cmd_val)
|
| 127 | val = rd.PosValue()
|
| 128 | rd.Done()
|
| 129 |
|
| 130 | tree = val.PrettyTree()
|
| 131 | #tree = val.AbbreviatedTree() # I used this to test cycle detection
|
| 132 |
|
| 133 | # TODO: ASDL should print the IDs. And then they will be
|
| 134 | # line-wrapped.
|
| 135 | # The IDs should also be used to detect cycles, and omit values
|
| 136 | # already printed.
|
| 137 | #id_str = vm.ValueIdString(val)
|
| 138 | #f.write(' <%s%s>\n' % (ysh_type, id_str))
|
| 139 |
|
| 140 | pretty_f = fmt.DetectConsoleOutput(self.stdout_)
|
| 141 | fmt.PrintTree(tree, pretty_f)
|
| 142 | self.stdout_.write('\n')
|
| 143 |
|
| 144 | status = 0
|
| 145 |
|
| 146 | elif action == 'line':
|
| 147 | # Print format for unit tests
|
| 148 |
|
| 149 | # TODO: could be pp line (x, y, z)
|
| 150 | rd = typed_args.ReaderForProc(cmd_val)
|
| 151 | val = rd.PosValue()
|
| 152 | rd.Done()
|
| 153 |
|
| 154 | if ui.TypeNotPrinted(val):
|
| 155 | ysh_type = ui.ValType(val)
|
| 156 | self.stdout_.write('(%s) ' % ysh_type)
|
| 157 |
|
| 158 | j8.PrintLine(val, self.stdout_)
|
| 159 |
|
| 160 | status = 0
|
| 161 |
|
| 162 | elif action == 'gc-stats':
|
| 163 | print('TODO')
|
| 164 | status = 0
|
| 165 |
|
| 166 | elif action == 'proc':
|
| 167 | names, locs = arg_r.Rest2()
|
| 168 | if len(names):
|
| 169 | for i, name in enumerate(names):
|
| 170 | node = self.procs.Get(name)
|
| 171 | if node is None:
|
| 172 | self.errfmt.Print_('Invalid proc %r' % name,
|
| 173 | blame_loc=locs[i])
|
| 174 | return 1
|
| 175 | else:
|
| 176 | names = self.procs.GetNames()
|
| 177 |
|
| 178 | # TSV8 header
|
| 179 | print('proc_name\tdoc_comment')
|
| 180 | for name in names:
|
| 181 | proc = self.procs.Get(name) # must exist
|
| 182 | #log('Proc %s', proc)
|
| 183 | body = proc.body
|
| 184 |
|
| 185 | # TODO: not just command.ShFunction, but command.Proc!
|
| 186 | doc = ''
|
| 187 | if body.tag() == command_e.BraceGroup:
|
| 188 | bgroup = cast(BraceGroup, body)
|
| 189 | if bgroup.doc_token:
|
| 190 | token = bgroup.doc_token
|
| 191 | # 1 to remove leading space
|
| 192 | doc = token.line.content[token.col + 1:token.col +
|
| 193 | token.length]
|
| 194 |
|
| 195 | # Note: these should be attributes on value.Proc
|
| 196 | buf = mylib.BufWriter()
|
| 197 | j8.EncodeString(name, buf, unquoted_ok=True)
|
| 198 | buf.write('\t')
|
| 199 | j8.EncodeString(doc, buf, unquoted_ok=True)
|
| 200 | print(buf.getvalue())
|
| 201 |
|
| 202 | status = 0
|
| 203 |
|
| 204 | else:
|
| 205 | e_usage('got invalid action %r' % action, action_loc)
|
| 206 |
|
| 207 | return status
|
| 208 |
|
| 209 |
|
| 210 | class Write(_Builtin):
|
| 211 | """
|
| 212 | write -- @strs
|
| 213 | write --sep ' ' --end '' -- @strs
|
| 214 | write -n -- @
|
| 215 | write --j8 -- @strs # argv serialization
|
| 216 | write --j8 --sep $'\t' -- @strs # this is like TSV8
|
| 217 | """
|
| 218 |
|
| 219 | def __init__(self, mem, errfmt):
|
| 220 | # type: (state.Mem, ui.ErrorFormatter) -> None
|
| 221 | _Builtin.__init__(self, mem, errfmt)
|
| 222 | self.stdout_ = mylib.Stdout()
|
| 223 |
|
| 224 | def Run(self, cmd_val):
|
| 225 | # type: (cmd_value.Argv) -> int
|
| 226 | attrs, arg_r = flag_util.ParseCmdVal('write', cmd_val)
|
| 227 | arg = arg_types.write(attrs.attrs)
|
| 228 | #print(arg)
|
| 229 |
|
| 230 | i = 0
|
| 231 | while not arg_r.AtEnd():
|
| 232 | if i != 0:
|
| 233 | self.stdout_.write(arg.sep)
|
| 234 | s = arg_r.Peek()
|
| 235 |
|
| 236 | if arg.json:
|
| 237 | s = j8.MaybeEncodeJsonString(s)
|
| 238 |
|
| 239 | elif arg.j8:
|
| 240 | s = j8.MaybeEncodeString(s)
|
| 241 |
|
| 242 | self.stdout_.write(s)
|
| 243 |
|
| 244 | arg_r.Next()
|
| 245 | i += 1
|
| 246 |
|
| 247 | if arg.n:
|
| 248 | pass
|
| 249 | elif len(arg.end):
|
| 250 | self.stdout_.write(arg.end)
|
| 251 |
|
| 252 | return 0
|
| 253 |
|
| 254 |
|
| 255 | class Fopen(vm._Builtin):
|
| 256 | """fopen does nothing but run a block.
|
| 257 |
|
| 258 | It's used solely for its redirects.
|
| 259 | fopen >out.txt { echo hi }
|
| 260 |
|
| 261 | It's a subset of eval
|
| 262 | eval >out.txt { echo hi }
|
| 263 | """
|
| 264 |
|
| 265 | def __init__(self, mem, cmd_ev):
|
| 266 | # type: (state.Mem, cmd_eval.CommandEvaluator) -> None
|
| 267 | self.mem = mem
|
| 268 | self.cmd_ev = cmd_ev # To run blocks
|
| 269 |
|
| 270 | def Run(self, cmd_val):
|
| 271 | # type: (cmd_value.Argv) -> int
|
| 272 | _, arg_r = flag_util.ParseCmdVal('fopen',
|
| 273 | cmd_val,
|
| 274 | accept_typed_args=True)
|
| 275 |
|
| 276 | cmd = typed_args.OptionalBlock(cmd_val)
|
| 277 | if not cmd:
|
| 278 | raise error.Usage('expected a block', loc.Missing)
|
| 279 |
|
| 280 | unused = self.cmd_ev.EvalCommand(cmd)
|
| 281 | return 0
|