-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.py
More file actions
143 lines (114 loc) · 3.72 KB
/
Copy pathshell.py
File metadata and controls
143 lines (114 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
import sys
from inspect import getmembers, isfunction
import multiprocessing
from multiprocessing import Process
import subprocess
from parse import AST, ParseError
from job import Job, JobList
import lib
class Shell(object):
prompt = '> '
functions = {n:f for n, f in getmembers(lib, isfunction)}
functions['help'] = functions['helpsh']
env_job_function_names = ['environ', 'set', 'unset', 'jobs']
def environ(self):
for k, v in self.env.items():
print(f'{k}={v}')
def set(self, var, val):
self.env[var] = val
def unset(self, var):
del self.env[var]
def jobs(self):
print(self.joblist)
def __init__(self, batch_file=None):
for n in self.env_job_function_names:
self.functions[n] = getattr(self, n)
self.env = {}
self.joblist = JobList()
if batch_file:
texts = open(batch_file, 'r').readlines()
for text in texts:
self.interpret(text)
else:
self.end = False
while not self.end:
# TODO: Notify job completion asynchronously
self.joblist.check()
text = input(f'[{os.getcwd()}]{self.prompt}')
self.interpret(text)
def interpret(self, text):
try:
ast = AST(text)
self.execute(ast.root)
except ParseError as err:
print(err)
def execute(self, root):
for pipe, bg in root:
if bg:
subp = Process(target=self.execute_pipe, args=(pipe,), daemon=True)
subp.start()
self.joblist.add_job(Job(pipe, subp))
else:
self.execute_pipe(pipe)
def execute_pipe(self, root):
# save for restoring later on
sin, sout = (0, 0)
sin = os.dup(0)
sout = os.dup(1)
# first command takes commandut from stdin
fdin = os.dup(sin)
pipe_len = len(root)
for i, command in enumerate(root):
# fdin will be stdin if it's the first iteration
# and the readable end of the pipe if not.
os.dup2(fdin, 0)
os.close(fdin)
# restore stdout if this is the last command
if i == pipe_len - 1:
fdout = os.dup(sout)
else:
fdin, fdout = os.pipe()
# redirect stdout to pipe
os.dup2(fdout, 1)
os.close(fdout)
self.execute_command(command)
# restore stdout and stdin
os.dup2(sin, 0)
os.dup2(sout, 1)
os.close(sin)
os.close(sout)
def execute_command(self, command):
stdin = sys.stdin
stdout = sys.stdout
if command.rin:
sys.stdin = open(command.rin, 'r')
if command.rout:
sys.stdout = open(command.rout, 'w')
elif command.rapp:
sys.stdout = open(command.rapp, 'a')
self.execute_args(command.args)
sys.stdin = stdin
sys.stdout = stdout
def execute_args(self, args):
head = args[0]
if head not in self.functions:
try:
subprocess.run(args)
except FileNotFoundError:
print(f'psh: no such command: {head}')
else:
try:
self.end = self.functions[head](*args[1:]) is not None
except TypeError:
print(f'{head}: invalid args length')
if __name__ == '__main__':
# Enforces fork as the start method on macOS
multiprocessing.set_start_method('fork')
argc = len(sys.argv)
if argc == 2:
Shell(sys.argv[1])
elif argc == 1:
Shell()
else:
print('unknown usage')