| 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
 | 
| 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 log
 | 
| 23 | 
 | 
| 24 | from typing import TYPE_CHECKING, cast, Dict
 | 
| 25 | if TYPE_CHECKING:
 | 
| 26 |     from core.alloc import Arena
 | 
| 27 |     from core.ui import ErrorFormatter
 | 
| 28 |     from osh import cmd_eval
 | 
| 29 | 
 | 
| 30 | _ = log
 | 
| 31 | 
 | 
| 32 | 
 | 
| 33 | class _Builtin(vm._Builtin):
 | 
| 34 | 
 | 
| 35 |     def __init__(self, mem, errfmt):
 | 
| 36 |         # type: (state.Mem, 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__(self, mem, errfmt, procs, arena):
 | 
| 48 |         # type: (state.Mem, ErrorFormatter, Dict[str, value.Proc], Arena) -> None
 | 
| 49 |         _Builtin.__init__(self, mem, errfmt)
 | 
| 50 |         self.procs = procs
 | 
| 51 |         self.arena = arena
 | 
| 52 |         self.stdout_ = mylib.Stdout()
 | 
| 53 | 
 | 
| 54 |     def Run(self, cmd_val):
 | 
| 55 |         # type: (cmd_value.Argv) -> int
 | 
| 56 |         arg, arg_r = flag_util.ParseCmdVal('pp',
 | 
| 57 |                                            cmd_val,
 | 
| 58 |                                            accept_typed_args=True)
 | 
| 59 | 
 | 
| 60 |         action, action_loc = arg_r.ReadRequired2(
 | 
| 61 |             'expected an action (proc, cell, etc.)')
 | 
| 62 | 
 | 
| 63 |         # Actions that print unstable formats start with '.'
 | 
| 64 |         if action == 'cell':
 | 
| 65 |             argv, locs = arg_r.Rest2()
 | 
| 66 | 
 | 
| 67 |             status = 0
 | 
| 68 |             for i, name in enumerate(argv):
 | 
| 69 |                 if name.startswith(':'):
 | 
| 70 |                     name = name[1:]
 | 
| 71 | 
 | 
| 72 |                 if not match.IsValidVarName(name):
 | 
| 73 |                     raise error.Usage('got invalid variable name %r' % name,
 | 
| 74 |                                       locs[i])
 | 
| 75 | 
 | 
| 76 |                 cell = self.mem.GetCell(name)
 | 
| 77 |                 if cell is None:
 | 
| 78 |                     self.errfmt.Print_("Couldn't find a variable named %r" %
 | 
| 79 |                                        name,
 | 
| 80 |                                        blame_loc=locs[i])
 | 
| 81 |                     status = 1
 | 
| 82 |                 else:
 | 
| 83 |                     self.stdout_.write('%s = ' % name)
 | 
| 84 |                     pretty_f = fmt.DetectConsoleOutput(self.stdout_)
 | 
| 85 |                     fmt.PrintTree(cell.PrettyTree(), pretty_f)
 | 
| 86 |                     self.stdout_.write('\n')
 | 
| 87 | 
 | 
| 88 |         elif action == 'asdl':
 | 
| 89 |             # TODO: could be pp asdl (x, y, z)
 | 
| 90 |             rd = typed_args.ReaderForProc(cmd_val)
 | 
| 91 |             val = rd.PosValue()
 | 
| 92 |             rd.Done()
 | 
| 93 | 
 | 
| 94 |             tree = val.PrettyTree()
 | 
| 95 |             #tree = val.AbbreviatedTree()  # I used this to test cycle detection
 | 
| 96 | 
 | 
| 97 |             # TODO: ASDL should print the IDs.  And then they will be
 | 
| 98 |             # line-wrapped.
 | 
| 99 |             # The IDs should also be used to detect cycles, and omit values
 | 
| 100 |             # already printed.
 | 
| 101 |             #id_str = vm.ValueIdString(val)
 | 
| 102 |             #f.write('    <%s%s>\n' % (ysh_type, id_str))
 | 
| 103 | 
 | 
| 104 |             pretty_f = fmt.DetectConsoleOutput(self.stdout_)
 | 
| 105 |             fmt.PrintTree(tree, pretty_f)
 | 
| 106 |             self.stdout_.write('\n')
 | 
| 107 | 
 | 
| 108 |             status = 0
 | 
| 109 | 
 | 
| 110 |         elif action == 'line':
 | 
| 111 |             # Print format for unit tests
 | 
| 112 | 
 | 
| 113 |             # TODO: could be pp line (x, y, z)
 | 
| 114 |             rd = typed_args.ReaderForProc(cmd_val)
 | 
| 115 |             val = rd.PosValue()
 | 
| 116 |             rd.Done()
 | 
| 117 | 
 | 
| 118 |             ysh_type = ui.ValType(val)
 | 
| 119 |             self.stdout_.write('(%s)   ' % ysh_type)
 | 
| 120 | 
 | 
| 121 |             j8.PrintLine(val, self.stdout_)
 | 
| 122 | 
 | 
| 123 |             status = 0
 | 
| 124 | 
 | 
| 125 |         elif action == 'gc-stats':
 | 
| 126 |             print('TODO')
 | 
| 127 |             status = 0
 | 
| 128 | 
 | 
| 129 |         elif action == 'proc':
 | 
| 130 |             names, locs = arg_r.Rest2()
 | 
| 131 |             if len(names):
 | 
| 132 |                 for i, name in enumerate(names):
 | 
| 133 |                     node = self.procs.get(name)
 | 
| 134 |                     if node is None:
 | 
| 135 |                         self.errfmt.Print_('Invalid proc %r' % name,
 | 
| 136 |                                            blame_loc=locs[i])
 | 
| 137 |                         return 1
 | 
| 138 |             else:
 | 
| 139 |                 names = sorted(self.procs)
 | 
| 140 | 
 | 
| 141 |             # TSV8 header
 | 
| 142 |             print('proc_name\tdoc_comment')
 | 
| 143 |             for name in names:
 | 
| 144 |                 proc = self.procs[name]  # must exist
 | 
| 145 |                 #log('Proc %s', proc)
 | 
| 146 |                 body = proc.body
 | 
| 147 | 
 | 
| 148 |                 # TODO: not just command.ShFunction, but command.Proc!
 | 
| 149 |                 doc = ''
 | 
| 150 |                 if body.tag() == command_e.BraceGroup:
 | 
| 151 |                     bgroup = cast(BraceGroup, body)
 | 
| 152 |                     if bgroup.doc_token:
 | 
| 153 |                         token = bgroup.doc_token
 | 
| 154 |                         # 1 to remove leading space
 | 
| 155 |                         doc = token.line.content[token.col + 1:token.col +
 | 
| 156 |                                                  token.length]
 | 
| 157 | 
 | 
| 158 |                 # Note: these should be attributes on value.Proc
 | 
| 159 |                 buf = mylib.BufWriter()
 | 
| 160 |                 j8.EncodeString(name, buf, unquoted_ok=True)
 | 
| 161 |                 buf.write('\t')
 | 
| 162 |                 j8.EncodeString(doc, buf, unquoted_ok=True)
 | 
| 163 |                 print(buf.getvalue())
 | 
| 164 | 
 | 
| 165 |             status = 0
 | 
| 166 | 
 | 
| 167 |         else:
 | 
| 168 |             e_usage('got invalid action %r' % action, action_loc)
 | 
| 169 | 
 | 
| 170 |         return status
 | 
| 171 | 
 | 
| 172 | 
 | 
| 173 | class Write(_Builtin):
 | 
| 174 |     """
 | 
| 175 |     write -- @strs
 | 
| 176 |     write --sep ' ' --end '' -- @strs
 | 
| 177 |     write -n -- @
 | 
| 178 |     write --j8 -- @strs   # argv serialization
 | 
| 179 |     write --j8 --sep $'\t' -- @strs   # this is like TSV8
 | 
| 180 |     """
 | 
| 181 | 
 | 
| 182 |     def __init__(self, mem, errfmt):
 | 
| 183 |         # type: (state.Mem, ErrorFormatter) -> None
 | 
| 184 |         _Builtin.__init__(self, mem, errfmt)
 | 
| 185 |         self.stdout_ = mylib.Stdout()
 | 
| 186 | 
 | 
| 187 |     def Run(self, cmd_val):
 | 
| 188 |         # type: (cmd_value.Argv) -> int
 | 
| 189 |         attrs, arg_r = flag_util.ParseCmdVal('write', cmd_val)
 | 
| 190 |         arg = arg_types.write(attrs.attrs)
 | 
| 191 |         #print(arg)
 | 
| 192 | 
 | 
| 193 |         i = 0
 | 
| 194 |         while not arg_r.AtEnd():
 | 
| 195 |             if i != 0:
 | 
| 196 |                 self.stdout_.write(arg.sep)
 | 
| 197 |             s = arg_r.Peek()
 | 
| 198 | 
 | 
| 199 |             if arg.json:
 | 
| 200 |                 s = j8.MaybeEncodeJsonString(s)
 | 
| 201 | 
 | 
| 202 |             elif arg.j8:
 | 
| 203 |                 s = j8.MaybeEncodeString(s)
 | 
| 204 | 
 | 
| 205 |             self.stdout_.write(s)
 | 
| 206 | 
 | 
| 207 |             arg_r.Next()
 | 
| 208 |             i += 1
 | 
| 209 | 
 | 
| 210 |         if arg.n:
 | 
| 211 |             pass
 | 
| 212 |         elif len(arg.end):
 | 
| 213 |             self.stdout_.write(arg.end)
 | 
| 214 | 
 | 
| 215 |         return 0
 | 
| 216 | 
 | 
| 217 | 
 | 
| 218 | class Fopen(vm._Builtin):
 | 
| 219 |     """fopen does nothing but run a block.
 | 
| 220 | 
 | 
| 221 |     It's used solely for its redirects.
 | 
| 222 |         fopen >out.txt { echo hi }
 | 
| 223 | 
 | 
| 224 |     It's a subset of eval
 | 
| 225 |         eval >out.txt { echo hi }
 | 
| 226 |     """
 | 
| 227 | 
 | 
| 228 |     def __init__(self, mem, cmd_ev):
 | 
| 229 |         # type: (state.Mem, cmd_eval.CommandEvaluator) -> None
 | 
| 230 |         self.mem = mem
 | 
| 231 |         self.cmd_ev = cmd_ev  # To run blocks
 | 
| 232 | 
 | 
| 233 |     def Run(self, cmd_val):
 | 
| 234 |         # type: (cmd_value.Argv) -> int
 | 
| 235 |         _, arg_r = flag_util.ParseCmdVal('fopen',
 | 
| 236 |                                          cmd_val,
 | 
| 237 |                                          accept_typed_args=True)
 | 
| 238 | 
 | 
| 239 |         cmd = typed_args.OptionalBlock(cmd_val)
 | 
| 240 |         if not cmd:
 | 
| 241 |             raise error.Usage('expected a block', loc.Missing)
 | 
| 242 | 
 | 
| 243 |         unused = self.cmd_ev.EvalCommand(cmd)
 | 
| 244 |         return 0
 |