-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathedlin.py
376 lines (316 loc) · 12.3 KB
/
edlin.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
##Created by Joseph Long 2016
##Modified to resemble DOS edlin by David Hunter 2021
##First python project
##Simple Text Editor
import os
import sys
try:
from pydos_ui import input
except:
pass
def edlin(passedIn=""):
global filename
#global autosave
global lineNum
global parseMap
def chkPath(tstPath):
validPath = True
if tstPath == []:
validPath = True
else:
savDir = os.getcwd()
for path in tstPath:
if path == "":
os.chdir("/")
elif os.getcwd() == "/" and path == "..":
validPath = False
break
elif path == "." or path == "..":
os.chdir(path)
elif path in os.listdir() and (os.stat(path)[0] & (2**15) == 0):
os.chdir(path)
else:
validPath = False
break
os.chdir(savDir)
return(validPath)
def open_text(name):
if name == "":
name = input("Enter file name: ")
aPath = name.split("/")
newdir = aPath.pop(-1)
text = []
if chkPath(aPath) and newdir in os.listdir(name[0:len(name)-len(newdir)]) and os.stat(name)[0] & (2**15) != 0:
with open(name,"r") as f:
for line in f:
text.append(line.replace("\n",""))
else:
print("Unable to open: "+name+". File not found.")
name = ""
return(name,text)
def proc_text(lineNum,func,strt,end,text):
if len(text) > 0:
x = min(max(0,strt),len(text)-1)
popIndx = x
staticEnd = max(0,min(end,len(text)-1))
if x == staticEnd:
lineNum = staticEnd
while x <= staticEnd:
if func == "L":
if x == lineNum:
print(" *", x, text[x])
else:
print(" ", x, text[x])
elif func == "D":
text.pop(popIndx)
lineNum = popIndx
x+=1
return(lineNum,text)
def save_text(name,text):
if name == "":
name = input("Enter file name: ")
f = open(name, "w")
for line in text:
f.write(line + "\n")
f.close()
return(name)
def parseInput(command):
global parseMap
cmdList = []
parseMap = "C"
cmdFound = False
cnfrmFound = False
buildingNum = False
builtNum =""
txtParam = ""
for sChar in command:
if not cmdFound:
if sChar not in "0123456789,.+-$?":
cmd = sChar.upper()
cmdFound = True
if buildingNum:
cmdList.append(int(builtNum))
buildingNum = False
builtNum = ""
parseMap += "-"
elif sChar in "0123456789+-":
builtNum += sChar
buildingNum = True
elif sChar == "?" and not cmdFound:
if not cnfrmFound:
parseMap += sChar
cnfrmFound = True
else:
if buildingNum:
cmdList.append(int(builtNum))
buildingNum = False
builtNum = ""
parseMap += "-"
else:
txtParam += sChar
if txtParam != "":
cmdList.append(txtParam.strip())
parseMap += "+"
if not cmdFound:
if buildingNum:
cmdList.append(int(builtNum))
parseMap += "-"
cmd = "*"
else:
cmd = "#"
cmdList.insert(0,cmd)
return cmdList
def interperit(command,text):
global filename
#global autosave
global lineNum
global parseMap
confirmCmd = False
loop = True
command_list = parseInput(command)
#print (lineNum,command_list,parseMap)
if "?" in parseMap:
confirmCmd = True
parseMap = parseMap.replace("?","")
if command_list[0] == "O":
if parseMap == "C+":
(filename,text) = open_text(command_list[1])
else:
(filename,text) = open_text(filename)
elif command_list[0] == "W":
if parseMap == "C+":
filename = save_text(command_list[1],text)
else:
filename = save_text(filename,text)
elif command_list[0] == "E":
if parseMap == "C+":
filename = save_text(command_list[1],text)
else:
filename = save_text(filename,text)
loop = False
elif command_list[0] == "Q":
loop = False
#elif command_list[0] == "autosave":
# if autosave:
# autosave = False
# print("\tAutosave disabled")
# else:
# autosave = True
# print("\tAutosave enabled")
elif command_list[0] == "new":
if len(command_list) > 1:
filename = save_text(command_list[1][1:],text)
else:
filename = save_text(filename,text)
text[:] = []
elif command_list[0] == "H":
print(" ")
print(" ")
print("h - DISPLAY COMMANDS")
print("# - REPLACE A SINGLE LINE")
print("[#][,#]d - DELETE A BLOCK OF LINES")
print("[#]i - INSERT")
print("[#]a['str'] - APPEND LINES")
print("[#][,#]l - LIST LINES")
print("[#][,#][?]r'str','str' - REPLACE STRING")
print("[#][,#][?]s'str' - SEARCH FOR STRING")
print("o [filename] - OPEN FILE")
print("w [filename] - WRITE FILE")
print("e [filename] - SAVE AND QUIT")
print("q - QUIT")
elif command_list[0] == "L" or command_list[0] == "D":
if parseMap == "C-":
(lineNum,text) = proc_text(lineNum,command_list[0],int(command_list[1]),int(command_list[1]),text)
elif parseMap == "C--":
(lineNum,text) = proc_text(lineNum,command_list[0],int(command_list[1]),int(command_list[2]),text)
elif parseMap == "C":
(lineNum,text) = proc_text(lineNum,command_list[0],0,len(text)-1,text)
else:
print("* Wrong command format. use: [#][,#]"+command_list[0].lower())
elif command_list[0] == "I":
if parseMap == "C-":
text.insert(int(command_list[1]), input("."))
elif parseMap == "C":
text.insert(lineNum, input("."))
else:
print("* Wrong command format. use: [#]i")
elif command_list[0] == "A":
if parseMap == "C":
text.append(input("."))
elif parseMap == "C-":
for a in range(0, int(command_list[1])):
text.append(input("."))
elif parseMap == "C+" or parseMap == "C-+":
strngs = command_list[-1].strip()
if strngs.count(strngs[0]) !=2:
print("* Wrong command format. Use: [#]a 'str'")
else:
strngs = strngs.replace(strngs[0],"")
if parseMap == "C+":
text.append(strngs)
elif parseMap == "C-+":
for a in range(0, int(command_list[1])):
text.append(strngs)
else:
print("* Wrong command format, use: [#]a 'str'")
elif command_list[0] == "R":
strt = lineNum
if parseMap == "C+":
strt = lineNum
end = lineNum
elif parseMap == "C-+":
strt = int(command_list[1])
end = strt
elif parseMap == "C--+":
strt = int(command_list[1])
end = int(command_list[2])
else:
print("* Wrong command format. Use: [#][,#][?]r 'str1','str2'")
strt = -1
if strt != -1:
strngs = command_list[-1].strip()
delim = strngs[0]
if strngs.count(delim) != 4:
print("* Wrong command format. Use: [#][,#][?]r 'str1','str2'")
else:
x = 0
token = []
delimFound = True
for achar in strngs:
if achar == delim:
if delimFound:
strtTk = x+1
else:
endTk = x
token.append(strngs[strtTk:endTk])
delimFound = not delimFound
x += 1
if confirmCmd:
print("for lines:",strt,"to",end,"- replace:",token[0],"with:",token[1],":")
for b in range(strt, end+1):
if confirmCmd:
print(text[b].replace(token[0], token[1]))
if input("Confirm change (y/n): ")[0].upper() == "Y":
text[b] = text[b].replace(token[0], token[1])
else:
text[b] = text[b].replace(token[0], token[1])
elif command_list[0] == "S":
strt = lineNum
if parseMap == "C+":
strt = 0
end = len(text) - 1
elif parseMap == "C-+":
strt = int(command_list[1])
end = strt
elif parseMap == "C--+":
strt = int(command_list[1])
end = int(command_list[2])
else:
print("* Wrong command format. Use: [#][,#][?]s 'str'")
strt = -1
if strt != -1:
strngs = command_list[-1].strip()
if strngs.count(strngs[0]) !=2:
print("* Wrong command format. Use: [#][,#][?]s 'str'")
else:
strngs = strngs.replace(strngs[0],"")
for b in range(strt, end+1):
if text[b].count(strngs) > 0:
lineNum = b
proc_text(lineNum,"L",b,b,text)
if not confirmCmd:
break
else:
if input("Confirm find (y/n): ")[0].upper() == "Y":
break
elif command_list[0] in "*#":
if command_list[0] == "*":
lineNum = min(len(text)-1,max(0,command_list[1]))
proc_text(lineNum,"L",lineNum,lineNum,text)
strngs = input(".")
if strngs != "":
text.insert(lineNum,strngs)
if lineNum < len(text)-1:
text.pop(lineNum+1)
if lineNum < len(text):
lineNum += 1
return(loop,text)
#autosave = False
filename = ""
lineNum = 0
text = []
if passedIn != "":
(filename,text) = open_text(passedIn)
print("h for command list")
loop = True
while loop:
(loop,text) = interperit(input(filename+": "),text)
#if autosave and filename !="":
#filename = save_text(filename,text)
del filename
#del autosave
del lineNum
del parseMap
if __name__ != "PyDOS":
passedIn = ""
edlin(passedIn)