| 1 | #!/usr/bin/env python2
 | 
| 2 | """osh_eval.py."""
 | 
| 3 | from __future__ import print_function
 | 
| 4 | 
 | 
| 5 | import sys
 | 
| 6 | 
 | 
| 7 | from _devbuild.gen.syntax_asdl import CompoundWord
 | 
| 8 | from core import shell
 | 
| 9 | from core import pyos
 | 
| 10 | from core import pyutil
 | 
| 11 | from frontend import args
 | 
| 12 | from frontend import flag_def  # side effect: flags are defined!
 | 
| 13 | from mycpp.mylib import print_stderr
 | 
| 14 | 
 | 
| 15 | unused = flag_def
 | 
| 16 | 
 | 
| 17 | from typing import List
 | 
| 18 | 
 | 
| 19 | 
 | 
| 20 | def main(argv):
 | 
| 21 |     # type: (List[str]) -> int
 | 
| 22 |     loader = pyutil.GetResourceLoader()
 | 
| 23 | 
 | 
| 24 |     login_shell = False
 | 
| 25 | 
 | 
| 26 |     environ = pyos.Environ()
 | 
| 27 | 
 | 
| 28 |     missing = None  # type: CompoundWord
 | 
| 29 |     arg_r = args.Reader(argv, [missing] * len(argv))
 | 
| 30 | 
 | 
| 31 |     try:
 | 
| 32 |         status = shell.Main('osh', arg_r, environ, login_shell, loader, None)
 | 
| 33 |         return status
 | 
| 34 |     except RuntimeError as e:
 | 
| 35 |         if 0:
 | 
| 36 |             import traceback
 | 
| 37 |             traceback.print_exc()
 | 
| 38 |         # NOTE: The Python interpreter can cause this, e.g. on stack overflow.
 | 
| 39 |         # f() { f; }; f will cause this
 | 
| 40 |         msg = e.message  # type: str
 | 
| 41 |         print_stderr('osh fatal error: %s' % msg)
 | 
| 42 |         return 1
 | 
| 43 | 
 | 
| 44 |     # Note: This doesn't happen in C++.
 | 
| 45 |     except KeyboardInterrupt:
 | 
| 46 |         print('')
 | 
| 47 |         return 130  # 128 + 2
 | 
| 48 | 
 | 
| 49 |     except (IOError, OSError) as e:
 | 
| 50 |         if 0:
 | 
| 51 |             import traceback
 | 
| 52 |             traceback.print_exc()
 | 
| 53 | 
 | 
| 54 |         # test this with prlimit --nproc=1 --pid=$$
 | 
| 55 |         print_stderr('oils I/O error: %s' % pyutil.strerror(e))
 | 
| 56 |         return 2  # dash gives status 2
 | 
| 57 | 
 | 
| 58 | 
 | 
| 59 | if __name__ == '__main__':
 | 
| 60 |     sys.exit(main(sys.argv))
 |