-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.py
93 lines (80 loc) · 6.11 KB
/
builtins.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
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
# Builtin functions for sip.
import os, signal, errno, sys
from sip import run
from parser import parse
###########################################
# BUILT IN FUNCTIONS
###########################################
#Change directory
def cd(state, cmd):
if len(cmd.args) == 0:
os.chdir(os.environ['HOME'])
elif cmd.args[0] == '~':
os.chdir(os.environ['HOME'])
else:
os.chdir(str(cmd.args[0]))
#Print current working directory
def pwd(state, cmd):
print os.getcwd()
#Print history
def history(state, cmd):
for index, item in enumerate(state.hist):
print ("%d %s" %(index, item))
#Clear history
def rmhistory(state, cm):
state.clearhistory()
print "History cleared!"
def clear(state, cm):
sys.stderr.write("\x1b[2J\x1b[H")
#Repeat an item from history
def repeat(state,cmd):
num = int(cmd.name[1:])
if((len(state.hist)-1) <num):
print "No such command in history"
else:
if num < 0:
ref = (len(state.hist)-1) + num
line = state.hist[ref]
else:
ref = num
line = state.hist[ref]
state.updatehistory(line)
command = parse(line)
run(state, command)
#list current jobs
def jobs(state, cmd):
state.updatejobs()
print "# PID COMMAND"
for index, item in enumerate(state.jobs):
print ("%d %s %s" %(index, item[0], item[1]))
#Kill numbered process
def kill(state, cmd):
try:
job = cmd.args[0]
if job[0] == '%':
os.kill(int(job[1:]), signal.SIGKILL)
else:
os.kill(int(job), signal.SIGKILL)
except OSError as e:
print e.strerror
#Print help
def help(state, cmd):
print "\n SHELL IN PYTHON"
print "-------------------------------------------------------------------------"
print " cd -- Changes directory"
print " pwd -- Print current working directory to std out"
print " history -- Print current history"
print " !n -- repate command n from history"
print " rmhistory -- Clear history"
print " jobs -- List currently background jobs"
print " kill -- Kill the process with the PID given, or the position n on the list if the input is %n"
print " clear -- Clear terminal"
print " exit -- Exit the shell, assuming that no background processes are currently running"
#Exit
def exit(state, cmd):
state.updatejobs()
if len(state.jobs) > 0:
print "There are jobs running in the background. Please kill them!"
else:
print "Thanks having a sip."
sys.exit()