-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·67 lines (53 loc) · 1.51 KB
/
main.py
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
#!/usr/bin/env python3
import os
from buildin import build_in
from constants import HISTORY
from file import add_history
from process import sub_process
from utils import parse_input, parse_args, parse_prompt
import signal
def execute(args_list):
length = len(args_list)
std_in, std_out = os.dup(0), os.dup(1)
tmp_in = os.dup(std_in)
for i in range(length):
os.dup2(tmp_in, 0)
os.close(tmp_in)
if i == length - 1:
tmp_out = os.dup(std_out)
else:
tmp_in, tmp_out = os.pipe()
os.dup2(tmp_out, 1)
os.close(tmp_out)
args = args_list[i]
if not args[0]:
return True
# exec build in command first
if args[0] in build_in:
if not build_in[args[0]](args): return False
else:
sub_process(args)
os.dup2(std_in, 0)
os.dup2(std_out, 1)
os.close(std_in)
os.close(std_out)
return True
def loop():
status = True
while status:
prompt = parse_prompt()
line = parse_input(input(prompt).strip())
add_history(line)
args = line.split(' ') # args value sample: ['ls', '-la']
args_list = parse_args(args)
status = execute(args_list)
def init():
signal.signal(signal.SIGINT, signal.SIG_IGN)
history_path = os.path.join(os.environ['HOME'], HISTORY)
if not os.path.exists(history_path):
os.system(r'touch {}'.format(history_path))
def main():
init()
loop()
if __name__ == '__main__':
main()