-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.py
executable file
·266 lines (265 loc) · 7.67 KB
/
command.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python
import re
import string
import subprocess
import os
from os.path import expanduser
from getch import getch
import getpass
import time
from sys import stdin
import RPi.GPIO as GPIO
from RPLCD import CharLCD,CursorMode,cursor,ShiftMode,cleared
GPIO.setwarnings(False)
lcd = CharLCD(cols=20, rows=4,
pin_rw=None,
pin_rs=21,
pin_e=20,
pins_data=[18,23,24,25],
#d4, d5, d6, d7
numbering_mode=GPIO.BCM)
lcd.cursor_mode = CursorMode.blink
my_cmd = ""
my_username = getpass.getuser()
my_perl = ""
lcd.write_string("Press ENTER for LCD terminal")
print "\nPress ENTER for LCD terminal\n";
my_wait = subprocess.check_output("/etc/wait.pl ",shell=True)
if my_wait == "Timeout":
lcd.clear()
my_name = subprocess.check_output("hostname -A",shell=True)
lcd.cursor_pos = (0,0)
lcd.write_string(my_name)
my_ip = subprocess.check_output("hostname -I",shell=True)
lcd.cursor_pos = (2,0)
lcd.write_string(my_ip)
lcd.cursor_mode = CursorMode.hide
exit(0)
while my_perl != "Success!":
lcd.clear()
my_name = subprocess.check_output("hostname -A",shell=True)
lcd.write_string(my_name)
lcd.cursor_pos = (1,0)
my_ip = subprocess.check_output("hostname -I",shell=True)
lcd.write_string(my_ip)
lcd.cursor_pos = (3,0)
lcd.write_string(my_username)
lcd.write_string("\'s password:")
my_perl = subprocess.check_output("/etc/pass.pl ",shell=True)
lcd.clear()
lcd.write_string(my_perl)
my_char = getch()
my_history = list()
my_hist_file = expanduser("~") + "/.pi_history"
with open(my_hist_file, 'r+') as f:
my_history = [x.strip('\n') for x in f.readlines()]
my_history.append("")
#create a bashrc alias to run this script
#have it detect a device on the GPIO pins and boot with the
#LCD terminal only if a device is detected
while my_cmd != 'exit':
my_cmd = ""
lcd.clear();
my_pwd = os.getcwd()
lcd.cursor_pos = (0,0)
lcd.write_string(getpass.getuser())
lcd.write_string('@')
my_hostname = subprocess.check_output("hostname",shell=True)
hostname = my_hostname.split()
lcd.write_string(hostname[0])
lcd.write_string(':')
lcd.write_string(os.getcwd())
lcd.write_string('$')
my_char = 'c'
#history variables
hist_length = len(my_history)
my_place = hist_length -1
while my_char != '\r' and my_char != '\n':
my_char = getch()
#handle Backspace
if my_char == '\x7f':
my_cmd = my_cmd[:-1]
lcd.clear()
my_pwd = os.getcwd()
lcd.cursor_pos = (0,0)
lcd.write_string(getpass.getuser())
lcd.write_string('@')
my_hostname = subprocess.check_output("hostname",shell=True)
hostname = my_hostname.split()
lcd.write_string(hostname[0])
lcd.write_string(':')
lcd.write_string(os.getcwd())
lcd.write_string('$')
lcd.write_string(my_cmd)
#Go up and down through history
elif my_char == '\033':#the escape sequence
getch()#skip a token
my_arrow = getch()#read arrow key value
if (my_arrow == 'A' or my_arrow == 'C') and my_place > 0:#up/right
my_place -= 1
elif (my_arrow == 'B' or my_arrow == 'D') and my_place < hist_length-1:#down/left
my_place += 1
my_cmd = my_history[my_place]
lcd.clear()
my_pwd = os.getcwd()
lcd.cursor_pos = (0,0)
lcd.write_string(getpass.getuser())
lcd.write_string('@')
my_hostname = subprocess.check_output("hostname",shell=True)
hostname = my_hostname.split()
lcd.write_string(hostname[0])
lcd.write_string(':')
lcd.write_string(os.getcwd())
lcd.write_string('$')
lcd.write_string(my_cmd)
elif my_char != '\r' and my_char != '\n':
my_cmd += my_char
lcd.write_string(my_char)
my_history.insert(hist_length-2,my_cmd)#commands will be the second to last item
temp_cmd = "echo \"" + my_cmd + "\" >> ~/.pi_history"
try:
subprocess.check_output(temp_cmd, shell=True,stderr=subprocess.STDOUT)
except Exception as err:
print err.output
lcd.clear()
if not my_cmd:
lcd.write_string("no command entered")
elif re.compile("^\s*cd .+").match(my_cmd):
my_dir = re.sub("^\s*cd\s*","",my_cmd)
try:
os.chdir(my_dir)
except:
lcd.clear()
lcd.write_string("\"")
lcd.write_string(my_dir)
lcd.write_string("\" does not exist in this path")
getch()
elif re.compile("^\s*cd").match(my_cmd):
lcd.clear()
my_dir = "/"
my_dir += getpass.getuser()
try:
os.chdir(my_dir)
except:
lcd.clear()
lcd.write_string("\"")
lcd.write_string(my_dir)
lcd.write_string("\" does not exist in this path")
getch()
elif my_cmd !='exit':
if re.compile("\s*history\s*").match(my_cmd):
my_cmd ="cat " + my_hist_file
try:
my_output = subprocess.check_output(my_cmd,shell=True,stderr=subprocess.STDOUT)
except Exception as e:
#cut tabs and spaces down new lines
my_output = str(e.output)
#re.sub("\s+","\n",my_output)
my_words = my_output.split()
lines = list()
searched = list()
def chunkstring(string, length):
return (string[0+i:length+i] for i in range(0, len(string), length))
##also split strings longer than 20 characters into two strings
for item in my_words:
for chunk in chunkstring(item,20):
lines.append(chunk)
lines.insert(0,"\n")
lines.insert(0,"\n")
lines.insert(0,"\n")
my_length = len(lines)
for l,k in enumerate(lines):
if k != "\n":
searched.append(l)
search_length = len(searched)
search_pos = 0
# cover cases with short list
lines.append("\n")
lines.append("\n")
lines.append("\n")
lines.append("\n")
my_char = 'c'
#Starting point for a on lcd screen
a = 3
#Scroll through text using letters 'j' and 'k'
while my_char != '\r' and my_char != '\n' and my_char != 'q':
lcd.clear()
lcd.cursor_pos = (0,0)
lcd.write_string(lines[a])
lcd.cursor_pos = (1,0)
lcd.write_string(lines[a+1])
lcd.cursor_pos = (2,0)
lcd.write_string(lines[a+2])
lcd.cursor_pos = (3,0)
lcd.write_string(lines[a+3])
my_char = getch()
if my_char == 'j' and (a+3) < (my_length-1):
a += 1
elif my_char =='k' and a > 0:
a -= 1
elif my_char == 'G':
a = my_length-4
elif my_char == 'g':
a = 3
#search through the output and use 'n' and 'N' to jump to those lines
elif my_char == '/':
new_char = 'n'
lcd.clear()
lcd.write_string("pattern: ")
lcd.cursor_pos = (1,0)
my_search = ""
searched = list()
while new_char != '\r' and new_char != '\n':
new_char = getch()
#handle Backspace
if new_char == '\x7f':
my_search = my_search[:-1]
lcd.clear()
lcd.write_string("pattern: ")
lcd.cursor_pos = (1,0)
lcd.write_string(my_search)
if new_char != '\r' and new_char != '\n' and new_char != '\x7f':
my_search += new_char
lcd.write_string(new_char)
#Find that string in the array of output
for i, j in enumerate(lines):
matchobj = None
oust = None
try:
matchobj = re.match(my_search,j)
except:
lcd.clear()
lcd.write_string("invalid ")
lcd.write_string("regular ")
lcd.write_string("expression ")
lcd.write_string(my_search)
oust = "true"
getch()
break
if matchobj:
searched.append(i);
search_length = len(searched)
if search_length == 0:
searched.append(my_length+3)
search_pos = 0
if oust == "true":
a = 3
else:
a = searched[0]-3
elif my_char == 'n':
if search_pos < (search_length -1):
search_pos += 1
a = searched[search_pos]-3
elif my_char == 'N':
if search_pos > 0:
search_pos -= 1
a = searched[search_pos]-3
else:
lcd.clear()
my_name = subprocess.check_output("hostname -A",shell=True)
lcd.cursor_pos = (0,0)
lcd.write_string(my_name)
my_ip = subprocess.check_output("hostname -I",shell=True)
lcd.cursor_pos = (2,0)
lcd.write_string(my_ip)
lcd.cursor_mode = CursorMode.hide