OILS / builtin / pure_osh.py View on Github | oilshell.org

483 lines, 295 significant
1#!/usr/bin/env python2
2"""
3pure_osh.py - Builtins that don't do any I/O.
4
5If the OSH interpreter were embedded in another program, these builtins can be
6safely used, e.g. without worrying about modifying the file system.
7
8NOTE: There can be spew on stdout, e.g. for shopt -p and so forth.
9"""
10from __future__ import print_function
11
12from _devbuild.gen import arg_types
13from _devbuild.gen.syntax_asdl import loc
14from _devbuild.gen.types_asdl import opt_group_i
15
16from core import error
17from core.error import e_usage
18from core import state
19from core import ui
20from core import vm
21from data_lang import j8_lite
22from frontend import args
23from frontend import consts
24from frontend import flag_util
25from frontend import match
26from frontend import typed_args
27from mycpp import mylib
28from mycpp.mylib import print_stderr, log
29
30from typing import List, Dict, Tuple, Optional, TYPE_CHECKING
31if TYPE_CHECKING:
32 from _devbuild.gen.runtime_asdl import cmd_value
33 from core.state import MutableOpts, Mem, SearchPath
34 from osh.cmd_eval import CommandEvaluator
35
36_ = log
37
38
39class Boolean(vm._Builtin):
40 """For :, true, false."""
41
42 def __init__(self, status):
43 # type: (int) -> None
44 self.status = status
45
46 def Run(self, cmd_val):
47 # type: (cmd_value.Argv) -> int
48
49 # These ignore regular args, but shouldn't accept typed args.
50 typed_args.DoesNotAccept(cmd_val.typed_args)
51 return self.status
52
53
54class Alias(vm._Builtin):
55
56 def __init__(self, aliases, errfmt):
57 # type: (Dict[str, str], ui.ErrorFormatter) -> None
58 self.aliases = aliases
59 self.errfmt = errfmt
60
61 def Run(self, cmd_val):
62 # type: (cmd_value.Argv) -> int
63 _, arg_r = flag_util.ParseCmdVal('alias', cmd_val)
64 argv = arg_r.Rest()
65
66 if len(argv) == 0:
67 for name in sorted(self.aliases):
68 alias_exp = self.aliases[name]
69 # This is somewhat like bash, except we use %r for ''.
70 print('alias %s=%r' % (name, alias_exp))
71 return 0
72
73 status = 0
74 for i, arg in enumerate(argv):
75 name, alias_exp = mylib.split_once(arg, '=')
76 if alias_exp is None: # if we get a plain word without, print alias
77 alias_exp = self.aliases.get(name)
78 if alias_exp is None:
79 self.errfmt.Print_('No alias named %r' % name,
80 blame_loc=cmd_val.arg_locs[i])
81 status = 1
82 else:
83 print('alias %s=%r' % (name, alias_exp))
84 else:
85 self.aliases[name] = alias_exp
86
87 #print(argv)
88 #log('AFTER ALIAS %s', aliases)
89 return status
90
91
92class UnAlias(vm._Builtin):
93
94 def __init__(self, aliases, errfmt):
95 # type: (Dict[str, str], ui.ErrorFormatter) -> None
96 self.aliases = aliases
97 self.errfmt = errfmt
98
99 def Run(self, cmd_val):
100 # type: (cmd_value.Argv) -> int
101 _, arg_r = flag_util.ParseCmdVal('unalias', cmd_val)
102 argv = arg_r.Rest()
103
104 if len(argv) == 0:
105 e_usage('requires an argument', loc.Missing)
106
107 status = 0
108 for i, name in enumerate(argv):
109 if name in self.aliases:
110 mylib.dict_erase(self.aliases, name)
111 else:
112 self.errfmt.Print_('No alias named %r' % name,
113 blame_loc=cmd_val.arg_locs[i])
114 status = 1
115 return status
116
117
118def SetOptionsFromFlags(exec_opts, opt_changes, shopt_changes):
119 # type: (MutableOpts, List[Tuple[str, bool]], List[Tuple[str, bool]]) -> None
120 """Used by core/shell.py."""
121
122 # We can set ANY option with -o. -O is too annoying to type.
123 for opt_name, b in opt_changes:
124 exec_opts.SetAnyOption(opt_name, b)
125
126 for opt_name, b in shopt_changes:
127 exec_opts.SetAnyOption(opt_name, b)
128
129
130class Set(vm._Builtin):
131
132 def __init__(self, exec_opts, mem):
133 # type: (MutableOpts, Mem) -> None
134 self.exec_opts = exec_opts
135 self.mem = mem
136
137 def Run(self, cmd_val):
138 # type: (cmd_value.Argv) -> int
139
140 # TODO:
141 # - How to integrate this with auto-completion? Have to handle '+'.
142
143 if len(cmd_val.argv) == 1:
144 # 'set' without args shows visible variable names and values. According
145 # to POSIX:
146 # - the names should be sorted, and
147 # - the code should be suitable for re-input to the shell. We have a
148 # spec test for this.
149 # Also:
150 # - autoconf also wants them to fit on ONE LINE.
151 # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set
152 mapping = self.mem.GetAllVars()
153 for name in sorted(mapping):
154 str_val = mapping[name]
155 code_str = '%s=%s' % (name, j8_lite.MaybeShellEncode(str_val))
156 print(code_str)
157 return 0
158
159 arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
160 arg_r.Next() # skip 'set'
161 arg = flag_util.ParseMore('set', arg_r)
162
163 # 'set -o' shows options. This is actually used by autoconf-generated
164 # scripts!
165 if arg.show_options:
166 self.exec_opts.ShowOptions([])
167 return 0
168
169 # Note: set -o nullglob is not valid. The 'shopt' builtin is preferred in
170 # YSH, and we want code to be consistent.
171 for opt_name, b in arg.opt_changes:
172 self.exec_opts.SetOldOption(opt_name, b)
173
174 for opt_name, b in arg.shopt_changes:
175 self.exec_opts.SetAnyOption(opt_name, b)
176
177 # Hm do we need saw_double_dash?
178 if arg.saw_double_dash or not arg_r.AtEnd():
179 self.mem.SetArgv(arg_r.Rest())
180 return 0
181
182
183class Shopt(vm._Builtin):
184
185 def __init__(self, mutable_opts, cmd_ev):
186 # type: (MutableOpts, CommandEvaluator) -> None
187 self.mutable_opts = mutable_opts
188 self.cmd_ev = cmd_ev
189
190 def Run(self, cmd_val):
191 # type: (cmd_value.Argv) -> int
192 attrs, arg_r = flag_util.ParseCmdVal('shopt',
193 cmd_val,
194 accept_typed_args=True)
195
196 arg = arg_types.shopt(attrs.attrs)
197 opt_names = arg_r.Rest()
198
199 if arg.p: # print values
200 if arg.o: # use set -o names
201 self.mutable_opts.ShowOptions(opt_names)
202 else:
203 self.mutable_opts.ShowShoptOptions(opt_names)
204 return 0
205
206 if arg.q: # query values
207 for name in opt_names:
208 index = consts.OptionNum(name)
209 if index == 0:
210 return 2 # bash gives 1 for invalid option; 2 is better
211 if not self.mutable_opts.opt0_array[index]:
212 return 1 # at least one option is not true
213 return 0 # all options are true
214
215 if arg.s:
216 b = True
217 elif arg.u:
218 b = False
219 else:
220 # If no flags are passed, print the options. bash prints uses a
221 # different format for 'shopt', but we use the same format as 'shopt
222 # -p'.
223 self.mutable_opts.ShowShoptOptions(opt_names)
224 return 0
225
226 cmd = typed_args.OptionalBlock(cmd_val)
227 if cmd:
228 opt_nums = [] # type: List[int]
229 for opt_name in opt_names:
230 # TODO: could consolidate with checks in core/state.py and option
231 # lexer?
232 opt_group = consts.OptionGroupNum(opt_name)
233 if opt_group == opt_group_i.YshUpgrade:
234 opt_nums.extend(consts.YSH_UPGRADE)
235 continue
236
237 if opt_group == opt_group_i.YshAll:
238 opt_nums.extend(consts.YSH_ALL)
239 continue
240
241 if opt_group == opt_group_i.StrictAll:
242 opt_nums.extend(consts.STRICT_ALL)
243 continue
244
245 index = consts.OptionNum(opt_name)
246 if index == 0:
247 # TODO: location info
248 e_usage('got invalid option %r' % opt_name, loc.Missing)
249 opt_nums.append(index)
250
251 with state.ctx_Option(self.mutable_opts, opt_nums, b):
252 unused = self.cmd_ev.EvalCommand(cmd)
253 return 0 # cd also returns 0
254
255 # Otherwise, set options.
256 for opt_name in opt_names:
257 # We allow set -o options here
258 self.mutable_opts.SetAnyOption(opt_name, b)
259
260 return 0
261
262
263class Hash(vm._Builtin):
264
265 def __init__(self, search_path):
266 # type: (SearchPath) -> None
267 self.search_path = search_path
268
269 def Run(self, cmd_val):
270 # type: (cmd_value.Argv) -> int
271 attrs, arg_r = flag_util.ParseCmdVal('hash', cmd_val)
272 arg = arg_types.hash(attrs.attrs)
273
274 rest = arg_r.Rest()
275 if arg.r:
276 if len(rest):
277 e_usage('got extra arguments after -r', loc.Missing)
278 self.search_path.ClearCache()
279 return 0
280
281 status = 0
282 if len(rest):
283 for cmd in rest: # enter in cache
284 full_path = self.search_path.CachedLookup(cmd)
285 if full_path is None:
286 print_stderr('hash: %r not found' % cmd)
287 status = 1
288 else: # print cache
289 commands = self.search_path.CachedCommands()
290 commands.sort()
291 for cmd in commands:
292 print(cmd)
293
294 return status
295
296
297def _ParseOptSpec(spec_str):
298 # type: (str) -> Dict[str, bool]
299 spec = {} # type: Dict[str, bool]
300 i = 0
301 n = len(spec_str)
302 while True:
303 if i >= n:
304 break
305 ch = spec_str[i]
306 spec[ch] = False
307 i += 1
308 if i >= n:
309 break
310 # If the next character is :, change the value to True.
311 if spec_str[i] == ':':
312 spec[ch] = True
313 i += 1
314 return spec
315
316
317class GetOptsState(object):
318 """State persisted across invocations.
319
320 This would be simpler in GetOpts.
321 """
322
323 def __init__(self, mem, errfmt):
324 # type: (Mem, ui.ErrorFormatter) -> None
325 self.mem = mem
326 self.errfmt = errfmt
327 self._optind = -1
328 self.flag_pos = 1 # position within the arg, public var
329
330 def _OptInd(self):
331 # type: () -> int
332 """Returns OPTIND that's >= 1, or -1 if it's invalid."""
333 # Note: OPTIND could be value.Int?
334 try:
335 result = state.GetInteger(self.mem, 'OPTIND')
336 except error.Runtime as e:
337 self.errfmt.Print_(e.UserErrorString())
338 result = -1
339 return result
340
341 def GetArg(self, argv):
342 # type: (List[str]) -> Optional[str]
343 """Get the value of argv at OPTIND.
344
345 Returns None if it's out of range.
346 """
347
348 #log('_optind %d flag_pos %d', self._optind, self.flag_pos)
349
350 optind = self._OptInd()
351 if optind == -1:
352 return None
353 self._optind = optind # save for later
354
355 i = optind - 1 # 1-based index
356 #log('argv %s i %d', argv, i)
357 if 0 <= i and i < len(argv):
358 return argv[i]
359 else:
360 return None
361
362 def IncIndex(self):
363 # type: () -> None
364 """Increment OPTIND."""
365 # Note: bash-completion uses a *local* OPTIND ! Not global.
366 assert self._optind != -1
367 state.BuiltinSetString(self.mem, 'OPTIND', str(self._optind + 1))
368 self.flag_pos = 1
369
370 def SetArg(self, optarg):
371 # type: (str) -> None
372 """Set OPTARG."""
373 state.BuiltinSetString(self.mem, 'OPTARG', optarg)
374
375 def Fail(self):
376 # type: () -> None
377 """On failure, reset OPTARG."""
378 state.BuiltinSetString(self.mem, 'OPTARG', '')
379
380
381def _GetOpts(
382 spec, # type: Dict[str, bool]
383 argv, # type: List[str]
384 my_state, # type: GetOptsState
385 errfmt, # type: ui.ErrorFormatter
386):
387 # type: (...) -> Tuple[int, str]
388 current = my_state.GetArg(argv)
389 #log('current %s', current)
390
391 if current is None: # out of range, etc.
392 my_state.Fail()
393 return 1, '?'
394
395 if not current.startswith('-') or current == '-':
396 my_state.Fail()
397 return 1, '?'
398
399 flag_char = current[my_state.flag_pos]
400
401 if my_state.flag_pos < len(current) - 1:
402 my_state.flag_pos += 1 # don't move past this arg yet
403 more_chars = True
404 else:
405 my_state.IncIndex()
406 my_state.flag_pos = 1
407 more_chars = False
408
409 if flag_char not in spec: # Invalid flag
410 return 0, '?'
411
412 if spec[flag_char]: # does it need an argument?
413 if more_chars:
414 optarg = current[my_state.flag_pos:]
415 else:
416 optarg = my_state.GetArg(argv)
417 if optarg is None:
418 my_state.Fail()
419 # TODO: Add location info
420 errfmt.Print_('getopts: option %r requires an argument.' %
421 current)
422 tmp = [j8_lite.MaybeShellEncode(a) for a in argv]
423 print_stderr('(getopts argv: %s)' % ' '.join(tmp))
424
425 # Hm doesn't cause status 1?
426 return 0, '?'
427 my_state.IncIndex()
428 my_state.SetArg(optarg)
429 else:
430 my_state.SetArg('')
431
432 return 0, flag_char
433
434
435class GetOpts(vm._Builtin):
436 """
437 Vars used:
438 OPTERR: disable printing of error messages
439 Vars set:
440 The variable named by the second arg
441 OPTIND - initialized to 1 at startup
442 OPTARG - argument
443 """
444
445 def __init__(self, mem, errfmt):
446 # type: (Mem, ui.ErrorFormatter) -> None
447 self.mem = mem
448 self.errfmt = errfmt
449
450 # TODO: state could just be in this object
451 self.my_state = GetOptsState(mem, errfmt)
452 self.spec_cache = {} # type: Dict[str, Dict[str, bool]]
453
454 def Run(self, cmd_val):
455 # type: (cmd_value.Argv) -> int
456 arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
457 arg_r.Next()
458
459 # NOTE: If first char is a colon, error reporting is different. Alpine
460 # might not use that?
461 spec_str = arg_r.ReadRequired('requires an argspec')
462
463 var_name, var_loc = arg_r.ReadRequired2(
464 'requires the name of a variable to set')
465
466 spec = self.spec_cache.get(spec_str)
467 if spec is None:
468 spec = _ParseOptSpec(spec_str)
469 self.spec_cache[spec_str] = spec
470
471 user_argv = self.mem.GetArgv() if arg_r.AtEnd() else arg_r.Rest()
472 #log('user_argv %s', user_argv)
473 status, flag_char = _GetOpts(spec, user_argv, self.my_state,
474 self.errfmt)
475
476 if match.IsValidVarName(var_name):
477 state.BuiltinSetString(self.mem, var_name, flag_char)
478 else:
479 # NOTE: The builtin has PARTIALLY set state. This happens in all shells
480 # except mksh.
481 raise error.Usage('got invalid variable name %r' % var_name,
482 var_loc)
483 return status