-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPyDOS.py
1269 lines (1103 loc) · 49 KB
/
PyDOS.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
try:
from os import sep
except:
sep = os.getcwd()[0]
from time import localtime
from sys import stdin,implementation,path
try:
from traceback import print_exception,format_exception
except:
from sys import print_exception as sysprexcept
print_exception = lambda err,value=None,tb=None: sysprexcept(err)
format_exception = lambda err,value=None,tb=None: [err]
if not sep+'lib' in path:
path.insert(1,sep+'lib')
path.append(sep+'PyBasic')
try:
from pydos_ui import Pydos_ui
except ImportError:
Pydos_ui = None
try:
from pydos_ui import input
except ImportError:
pass
import gc
imp = "B"
if implementation.name.upper() == "MICROPYTHON":
from micropython import mem_info
imp = "M"
elif implementation.name.upper() == "CIRCUITPYTHON":
if not Pydos_ui:
from supervisor import runtime
imp = "C"
gc.collect()
if 'threshold' in dir(gc):
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
# The first string may contain wildcards
def _match(first, second):
# If we reach end of both strings, we are done
if len(first) == 0 and len(second) == 0:
return True
# Make sure that the characters after '*' are present
# in second string. Can't contain two consecutive '*'
if len(first) > 1 and first[0] == '*' and len(second) == 0:
return False
if (len(first) > 1 and first[0] == '?') or (len(first) != 0
and len(second) !=0 and first[0] == second[0]):
return _match(first[1:],second[1:])
if first[:1] == '*':
return _match(first[1:],second) or _match(first,second[1:])
return False
def calcWildCardLen(wldCLen,recursiveFail):
wldCLen += 1
if not recursiveFail and wldCLen < 115:
try:
(wldCLen,recursiveFail) = calcWildCardLen(wldCLen,recursiveFail)
except:
recursiveFail = True
return (wldCLen,recursiveFail)
def PyDOS():
global envVars
if "envVars" not in globals().keys():
envVars = {}
_VER = "1.46"
prmpVals = ['>','(',')','&','|','\x1b','\b','<','=',' ',_VER,'\n','$','']
print("Starting Py-DOS...")
envVars["PATH"] = sep+";/PyBasic"
envVars["PROMPT"] = "$C$R$F$P$G"
envVars["LIB"] = ";".join(path[1:])
envVars["DIRSEP"] = sep
if Pydos_ui:
(envVars["_scrHeight"],envVars["_scrWidth"]) = Pydos_ui.get_screensize()
else:
envVars["_scrHeight"] = 24
envVars["_scrWidth"] = 80
scrWdth = int(envVars["_scrWidth"])
wldCLen = 0
recursiveFail = False
(wldCLen,recursiveFail) = calcWildCardLen(wldCLen,recursiveFail)
wldCAdj = int(1+.2*wldCLen)
if imp == "C":
wldCAdj += 5
wldCLen = max(1,wldCLen-wldCAdj)
if wldCLen < 60:
print("Wild card length set to: ",wldCLen)
gc.collect()
aFile = lambda dPth: bool(os.stat(dPth)[0]&(32768))
def anyKey():
print("Press any key to continue . . . ."[:scrWdth],end="")
if Pydos_ui:
while not Pydos_ui.serial_bytes_available():
pass
keyIn = Pydos_ui.read_keyboard(1)
else:
if imp == "C":
while not runtime.serial_bytes_available:
pass
keyIn = stdin.read(1)
print("")
return(keyIn)
def scrnPause(swPause,nLines,scrnLine,scrnEnd=None):
quit = False
i = 0
for sLine in scrnLine:
i += 1
if swPause and nLines >= int(envVars["_scrHeight"])-1:
key = anyKey()
nLines = 0
if key in "QqCc":
quit = True
break
if sLine is not None:
if scrnEnd is None or i<len(scrnLine):
print(sLine)
nLines += 1
else:
print(sLine,end="")
return (quit,nLines)
def exCmd(cFile,passedIn):
try:
with open(cFile) as cf:
if passedIn.find("'") > -1:
exec(f'passedIn = "{passedIn}"\n{cf.read()}')
else:
exec(f"passedIn = '{passedIn}'\n{cf.read()}")
except Exception as err:
print_exception(err,err, \
err.__traceback__ if hasattr(err,'__traceback__') else None)
envVars['lasterror'] = format_exception(err,err, \
err.__traceback__ if hasattr(err,'__traceback__') else None)
except KeyboardInterrupt:
print("^C")
return
def chkPath(tstPath):
validPath = True
simpPath = ""
if tstPath != []:
savDir = os.getcwd()
for path in tstPath:
if path[1:2] == ":":
path = path[2:]
if path == "":
os.chdir(sep)
elif os.getcwd() == sep and path == "..":
validPath = False
break
elif path == ".":
continue
elif path == ".." and len(os.getcwd().split(sep)) == 2:
os.chdir(sep)
elif path == "..":
os.chdir("..")
elif path in os.listdir() and not aFile(path):
os.chdir(path)
else:
validPath = False
break
if validPath:
simpPath = os.getcwd()
os.chdir(savDir)
return((validPath,simpPath))
def pFmt(dPath,trailsep=True):
if dPath == "":
return sep
elif dPath == sep:
return dPath
elif trailsep:
return dPath+(sep if dPath[-1]!=sep else "")
else:
return dPath[:(-1 if dPath[-1] == sep else None)]
def absolutePath(argPath,currDir):
if argPath[:1] == sep:
fullPath = argPath
elif currDir == sep:
fullPath = sep+argPath
else:
fullPath = currDir+sep+argPath
fullPath = pFmt(fullPath,False)
return(fullPath)
srtFnc = lambda v,dP: f"{str(os.stat(dP+v)[0]&(32768))[0]}{v.lower()}*{v}"
def dirLoop(tmpDir,lastDir,isFile,swPause,swWide,swRecur,prSum, \
nLines=0,nFiles=0,tFSize=0,nDirs=0):
wideCols = scrWdth//16
wideCount = 0
dirHeadPrntd = False
quit = False
if "*" in lastDir or "?" in lastDir or isFile:
dirPat = lastDir
lastDir = ""
else:
dirPat = None
dPath = pFmt(pFmt(tmpDir)+lastDir,False)
if dPath == sep+".":
dPath = sep
lastDir = ""
if dirPat is None:
(quit,nLines) = scrnPause(swPause,nLines,["","Directory of "+dPath.replace('/',('\\' if envVars.get('DIRSEP','/') == '\\' else '/'))])
dirHeadPrntd = True
nDirs += 2
if swWide:
if wideCols > 1:
(quit,nLines) = scrnPause(swPause,nLines, \
["[.] [..] "],"")
wideCount += 2
else:
(quit,nLines) = scrnPause(swPause,nLines,["[.]","[..]"],"")
wideCount = 1
else:
scrAdj1 = 52 - min(scrWdth,52)
(quit,nLines) = scrnPause(swPause,nLines, \
[f'.{" "*(23-scrAdj1)}<DIR>',f'..{" "*(22-scrAdj1)}<DIR>'])
for _dir in sorted([srtFnc(x,pFmt(dPath)) for x in os.listdir(dPath)]):
_dir = _dir.split('*')[1]
if (dirPat is None or _match(dirPat,_dir[:wldCLen])) and not quit:
dStat = os.stat(f"{pFmt(dPath)}{_dir}")
ForD = aFile(f"{pFmt(dPath)}{_dir}")
if not dirHeadPrntd:
(quit,nLines) = scrnPause(swPause,nLines,["","Directory of "+dPath.replace('/',('\\' if envVars.get('DIRSEP','/') == '\\' else '/'))])
if quit:
break
dirHeadPrntd = True
if not ForD:
fSize = 0
nDirs += 1
else:
fSize = str(dStat[6])
tFSize += int(fSize)
nFiles += 1
if swWide:
if wideCount >= wideCols:
wideCount = 0
print()
nLines += 1
wideCount += 1
if not ForD:
(quit,nLines) = scrnPause(swPause,nLines, \
[f'[{_dir[:13]}]{" "*(14-len(_dir[:13]))}'],"")
else:
(quit,nLines) = scrnPause(swPause,nLines, \
[_dir[:15]+" "*(16-len(_dir[:15]))],"")
else:
fTime = localtime(max(min(2145916800,dStat[9]),946684800))
if not ForD:
scrAdj1 = 52 - min(scrWdth,52)
scrAdj2 = min(13,65-min(scrWdth,65))
(quit,nLines) = scrnPause(swPause,nLines, \
[f'{_dir[:max(8,scrWdth-26)]}{" "*(24-len(_dir)-scrAdj1)}<DIR>{" "*(18-scrAdj2)}{fTime[1]:02}-{fTime[2]:02}-{fTime[0]:04} {fTime[3]:02}:{fTime[4]:02}'])
else:
scrAdj1 = 65 - min(scrWdth,65)
(quit,nLines) = scrnPause(swPause,nLines, \
[f'{_dir[:max(8,scrWdth-20-len(fSize))]}{" "*(36-len(_dir)+10-len(fSize)-scrAdj1)}{fSize} {fTime[1]:02}-{fTime[2]:02}-{fTime[0]:04} {fTime[3]:02}:{fTime[4]:02}'])
if quit:
break
if not quit:
if swWide:
if dirHeadPrntd:
print()
nLines += 1
if swRecur:
for _dir in sorted(os.listdir(dPath), key=str.upper):
dStat = pFmt(dPath)+_dir
if not aFile(dStat):
try:
(nLines,nFiles,tFSize,nDirs,quit) = \
dirLoop(dStat,(dirPat if dirPat is not None else ""), \
isFile,swPause,swWide,swRecur,False, \
nLines,nFiles,tFSize,nDirs)
except:
print("Recursion limit exceeded, Pystack too small")
quit = True
if quit:
break
if prSum and not quit:
try:
availDisk = os.statvfs(dPath)[1]*os.statvfs(dPath)[4]
except:
availDisk = 0
scrAdj1 = 65 - min(scrWdth,65)
(quit,nLines) = scrnPause(swPause,nLines, \
[(f'{" "*(4-len(str(nFiles)))} {nFiles} File(s){" "*(32-len(str(tFSize))-scrAdj1)} {tFSize} Bytes.')[:scrWdth], \
(f'{" "*(4-len(str(nDirs)))} {nDirs} Dir(s){" "*(33-len(str(availDisk))-scrAdj1)} {availDisk} Bytes free.')[:scrWdth],""],"")
return (nLines,nFiles,tFSize,nDirs,quit)
def prDir(dirPath,swBits):
if swBits & (swAllB-int('010110',2)):
print("Illegal switch, Command Format: DIR[/p][/w][/s] [path][file]")
return
savDir = os.getcwd()
fullPath = absolutePath(dirPath,savDir)
pathDirs = fullPath.split(sep)
lastDir = pathDirs.pop(-1)
(validPath, tmpDir) = chkPath(pathDirs)
if validPath:
os.chdir(tmpDir)
if tmpDir == sep:
pathDirs = [""]
else:
pathDirs = tmpDir.split(sep)
# Check for relative directory from possible mount point root
if len(pathDirs) == 2:
if lastDir == ".":
os.chdir(sep)
lastDir = tmpDir[1:]
elif lastDir == "..":
os.chdir(sep)
lastDir = ""
tmpDir = os.getcwd()
if lastDir in os.listdir() or lastDir in ".." or "*" in lastDir or "?" in lastDir or \
swBits & int('000010',2):
if lastDir in os.listdir():
if aFile(pFmt(lastDir,False)):
isFile = True
else:
isFile = False
else:
if lastDir in ".." or (tmpDir == sep and lastDir == ""):
isFile = False
else:
isFile = True
dirLoop(tmpDir,lastDir,isFile,bool(swBits&int('000100',2)),bool(swBits&int('010000',2)),bool(swBits&int('000010',2)),True)
else:
print("File",dirPath,"not found. (1)")
os.chdir(savDir)
else:
print("File",dirPath,"not found. (2)")
return
def filecpy(file1,file2):
gc.collect()
with open(file2, "wb") as fCopy:
with open(file1, 'rb') as fOrig:
for line in fOrig:
fCopy.write(line)
return
def delFiles(Dir,File,Recurs,removDirs):
for _dir in os.listdir(pFmt(Dir,False)):
if _match(File,_dir[:wldCLen]):
if File == "*" or File == "*.*" or _dir == _dir[:wldCLen]:
if aFile(Dir+_dir):
try:
os.remove(Dir+_dir)
print((Dir+_dir).replace('/',('\\' if envVars.get('DIRSEP','/') == '\\' else '/')),"deleted.")
except Exception as err:
print(f"Unable to delete: {Dir}{_dir}, Exception: {err}")
break
elif Recurs:
delFiles(Dir+_dir+sep,'*',Recurs,removDirs)
if removDirs:
if os.getcwd() == Dir+_dir:
os.chdir("..")
try:
os.rmdir(Dir+_dir)
except Exception as err:
print(f"Unable to remove: {Dir}{_dir}, Exception: {err}")
break
else:
print("Unable to delete: "+Dir+_dir+". Filename too long for wildcard operation.")
elif Recurs and not removDirs and not aFile(Dir+_dir):
delFiles(Dir+_dir+sep,File,Recurs,removDirs)
def setCondCmd(args,i,condResult):
condCmd = ""
foundElse = False
for _ in args[i:]:
if condResult:
if _.upper() == "ELSE":
break
condCmd += (_+" ")
else:
if foundElse:
condCmd += (_+" ")
else:
if _.upper() == "ELSE":
foundElse = True
return condCmd
def readBATFile(BATfile):
batIndex = [0]
batLabels = {}
batLineNo = 0
for batLine in BATfile:
batIndex.append(batIndex[batLineNo]+len(batLine))
batLineNo += 1
if batLine.strip()[:1] == ":" and len(batLine.strip().split(" ")[0]) > 1:
batLabels[batLine.strip().split(" ")[0][1:]] = [batLineNo,batIndex[batLineNo]]
BATfile.seek(0)
del batIndex,batLineNo
return batLabels
batEcho = True
cmd = ""
condCmd = ""
os.chdir(sep)
if "autoexec.bat" in ",".join(os.listdir()).lower().split(','):
activeBAT = True
BATfile = open(os.listdir()[(",".join(os.listdir()).lower().split(",")).index("autoexec.bat")])
batLabels = readBATFile(BATfile)
batLineNo = 0
batParams = []
else:
activeBAT = False
gc.collect()
while True:
scrWdth = int(envVars["_scrWidth"])
if condCmd != "":
cmdLine = condCmd
condCmd = ""
else:
if activeBAT:
cmdLine = BATfile.readline()
i=1
for param in batParams:
cmdLine = cmdLine.replace(f'%{i}',param)
i+=1
if i>9:
break
batLineNo += 1
if cmdLine == "":
activeBAT = False
batEcho = True
BATfile.close()
gc.collect()
elif batEcho and cmdLine[0] !="@":
print(cmdLine,end="")
elif cmdLine[0] == "@":
cmdLine = cmdLine[1:]
else:
prompt = "\n"
prmpLitrl = True
for prmpToken in envVars.get('PROMPT','$C$R$F$P$G').replace("$$","$."):
if prmpToken == '$':
prmpLitrl = False
continue
if prmpLitrl:
prompt += prmpToken
else:
prmpToken = prmpToken.upper()
prmpLitrl = True
if prmpToken == 'R':
if 'mem_free' in dir(gc):
prompt += str(gc.mem_free())
elif prmpToken == 'D':
prompt += f"{localtime()[1]:02}/{localtime()[2]:02}/{localtime()[0]:04}"
elif prmpToken == 'T':
prompt += f"{localtime()[3]:02}:{localtime()[4]:02}:{localtime()[5]:02}"
elif prmpToken == 'P':
prompt += os.getcwd().replace('/',('\\' if envVars.get('DIRSEP','/') == '\\' else '/'))
else:
prompt += prmpVals['GCFABEHLQSV_.'.find(prmpToken)]
cmdLine = input(prompt)
cmdLine = cmdLine.strip()
envFound = False
fndVar = ""
newCmdLine = ""
for _ in cmdLine:
if not envFound:
if _ == "%":
envFound = True
doublepct = True
else:
newCmdLine += _
else:
if _ == "%":
if doublepct:
newCmdLine += "%"
envFound = False
newCmdLine += str(envVars.get(fndVar,""))
fndVar = ""
else:
doublepct = False
fndVar += _
if envVars.get('DIRSEP','/') == '\\':
switches = []
switch = ""
nxt = False
cmdLine = newCmdLine[:1].replace('\\','/')
for _ in newCmdLine[1:]:
if nxt and _ not in " /":
switch += _.upper()
elif _ == '/':
nxt = True
if switch != "":
switches.append(switch)
switch = ""
elif nxt and _ == " ":
cmdLine += _
nxt = False
switches.append(switch)
switch = ""
elif _ == "\\":
cmdLine += sep
else:
cmdLine += _
if switch != "":
switches.append(switch)
else:
cmdLine = newCmdLine
args = cmdLine.split(" ")
quotedArg = False
if len(args) > 1:
i = 0
iEnd = len(args)
for _ in range(0, iEnd):
if args[i].strip() == "":
args.pop(i)
elif quotedArg:
if args[i].find('"') > -1 and args[i].find('"') != len(args[i])-1:
break
elif args[i][-1] == '"':
args[i-1] = f"{args[i-1]} {args.pop(i)[:-1]}"
quotedArg = False
else:
args[i-1] = f"{args[i-1]} {args.pop(i)}"
elif args[i][0] == '"':
if args[i][-1] != '"':
args[i] = args[i][1:]
i += 1
quotedArg = True
else:
args[i] = args[i][1:-1]
i += 1
else:
i += 1
if cmdLine != "" and cmdLine[0] != '/':
tmp = (args[0].upper()).split('/')
if envVars.get('DIRSEP','/') != '\\':
switches = tmp
cmd = tmp.pop(0)
else:
if envVars.get('DIRSEP','/') != '\\':
switches = []
cmd = args[0]
# Error=1, (S)Recur=2, (P)Pause=4, (Y)Conf=8, (W)Wide=16, (D)debug=32, (Q)uiet=64
# (V)erify=128
swBits = 0
swAllB = int('11111111',2)
for i in range(len(switches)):
swBits = swBits | (2**('SPYWDQV'.find(switches[i])+1))
if quotedArg:
print("Mismatched quotes.")
cmd = ""
if cmd in ["DELETE","DEL","TYPE","MORE","MKDIR","MD","RMDIR","RD","COPY", \
"CHDIR","CD","RENAME","REN","MOVE","DELTREE"]:
if len(args) > 1:
savDir = os.getcwd()
args[1] = absolutePath(args[1],savDir)
aPath = args[1].split(sep)
if cmd not in ["RMDIR","RD","CHDIR","CD","DELTREE"]:
newdir = aPath.pop(-1)
(validPath,tmpDir) = chkPath(aPath)
if cmd in ["DELETE","DEL","TYPE","MORE","MKDIR","MD"]:
tmpDir = pFmt(tmpDir)
if cmd == "" or cmd == "REM":
continue
elif cmd == "DIR":
if len(args) == 1:
prDir(os.getcwd()[(2 if os.getcwd()[1:2]==":" else 0):],swBits)
elif len(args) == 2:
prDir(args[1],swBits)
else:
print("Too many arguments. Command Format: DIR/p/w/s [path][file]")
elif cmd == "DATE":
i = localtime()[6]*3
print(f'The current date is: {"MonTueWedThuFriSatSun"[i:i+3]} {localtime()[1]:02}/{localtime()[2]:02}/{localtime()[0]:04}')
elif cmd == "TIME":
print(f'The current time is: {localtime()[3]%12}:{localtime()[4]:02}:{localtime()[5]:02} {["AM","PM"][localtime()[3]//12]}')
elif cmd == "MEM":
gc.collect()
print(f"\n{gc.mem_free()/1024:10.1f} Kb free conventional memory")
print(f"{gc.mem_alloc()/1024:10.1f} Kb used conventional memory")
if imp == "M":
if 'threshold' in dir(gc):
print(f"{gc.threshold()/1024:10.1f} Kb current threshold value")
if swBits & int('010000',2):
print(mem_info(1))
elif swBits:
print("Illegal switch, Command Format: mem[/d]")
elif cmd == "VER":
print(f"PyDOS [Version {_VER}]")
elif cmd == "ECHO":
if len(args) == 1:
print("Echo is "+("on." if batEcho else "off."))
else:
if args[1].upper() == 'ON':
batEcho = True
elif args[1].upper() == 'OFF':
batEcho = False
else:
print(cmdLine[5:].replace("\e",chr(27)).replace('\x1b',chr(27)).replace("\E",chr(27)).replace('\X1B',chr(27)))
elif cmd == "PAUSE":
anyKey()
elif cmd[0] == ":" and activeBAT:
if len(args[0]) <= 1 or len(args) != 1:
print("Invalid batch label")
condCmd = "exit"
elif cmd == "GOTO" and activeBAT:
if len(args) == 2:
try:
BATfile.seek(batLabels[args[1]][1])
except:
print("Invalid Goto label:",args[1])
condCmd = "exit"
batLineNo = batLabels.get(args[1],[batLineNo+1,0])[0]
else:
print("Invalid Goto label:",cmdLine)
condCmd = "exit"
elif cmd == "IF" and activeBAT:
condResult = False
if len(args) < 3:
print("Invalid command format:",cmdLine)
condCmd = "exit"
else:
i = 1
notlogic = False
if args[1].upper() == "NOT":
notlogic = True
i = 2
if args[i].strip().upper() == 'ERRORLEVEL':
i += 1
if len(args) > i and args[i].isdigit():
if str(envVars.get('errorlevel')).isdigit() and int(envVars.get('errorlevel')) == int(args[i]):
condResult = True
if notlogic:
condResult = not condResult
i += 1
condCmd = setCondCmd(args,i,condResult)
else:
print("Invalid conditional ERRORLEVEL:",cmdLine)
condCmd = "exit"
elif args[i].strip().upper() == 'EXIST':
i += 1
if len(args) > i:
savDir = os.getcwd()
args[i] = absolutePath(args[i],savDir)
aPath = args[i].split(sep)
newdir = aPath.pop(-1)
(validPath, tmpDir) = chkPath(aPath)
tmpDir = pFmt(tmpDir)
if validPath and newdir in os.listdir(pFmt(tmpDir,False)):
condResult = True
if notlogic:
condResult = not condResult
i += 1
condCmd = setCondCmd(args,i,condResult)
else:
print("Invalid conditional EXIST:",cmdLine)
condCmd = "exit"
else:
# string comparison
if len(args) > i:
string1 = args[i]
if "==" in args[i]:
string1 = args[i].split("==")[0]
if args[i][-1] != "=":
string2 = args[i].split("==")[-1]
else:
i += 1
if len(args) > i:
string2 = args[i]
else:
print("Invalid string conditional:",cmdLine)
condCmd = "exit"
elif len(args) > i+1:
i += 1
if "==" in args[i]:
if len(args[i]) > 2:
string2 = args[i].split("==")[-1]
else:
i+=1
if len(args) > i:
string2 = args[i]
else:
print("Invalid string conditional:",cmdLine)
condCmd = "exit"
else:
print("Invalid string conditional:",cmdLine)
condCmd = "exit"
else:
print("Invalid string conditional:",cmdLine)
condCmd = "exit"
if condCmd != "exit":
if string1 == string2:
condResult = True
if notlogic:
condResult = not condResult
i += 1
condCmd = setCondCmd(args,i,condResult)
else:
print("Invalid string conditional:",cmdLine)
condCmd = "exit"
elif cmd == "SET":
if len(args) == 1:
for _ in sorted(envVars):
print(f"{_}={envVars[_]}")
else:
args = cmdLine.split(" ")
args.pop(0)
envCmd = (" ".join(args)).split("=")
envCmdVar = envCmd.pop(0).strip()
if len(switches) <= 1:
if len(switches) == 0:
tmp = "=".join(envCmd).strip()
elif switches[0] == 'A':
# Replace all possible environment variables with their values
envCmd = "=".join(envCmd)
for _ in " %*()-+/":
envCmd = envCmd.replace(_," ")
envCmd = envCmd.split(" ")
for _ in envCmd:
if _[:1].isalpha() or _[:1] == "_":
cmdLine = cmdLine.replace(_.strip(),str(envVars.get(_,0)))
# Evaluate right sight of = after value substituion
args = cmdLine.split(" ")
args.pop(0)
envCmd = (" ".join(args)).split("=")
envCmdVar = envCmd.pop(0).strip()
try:
envVars[envCmdVar] = str(eval("=".join(envCmd).strip()))
except:
envVars[envCmdVar] = "0"
elif switches[0] == "P":
tmp = input("=".join(envCmd).strip()+" ")
else:
print("Illegal switch, Command Format: SET[/a|/p] [variable = [string|expression]]")
if len(switches) == 0 or switches[0] == "P":
if tmp != "":
envVars[envCmdVar] = tmp
elif envCmdVar == "_scrHeight" or envCmdVar == "_scrWidth":
if Pydos_ui:
(tHeight,tWidth) = Pydos_ui.get_screensize(envVars.get('_display'))
else:
tHeight = 24
tWidth = 80
if envCmdVar == "_scrWidth":
envVars[envCmdVar] = tWidth
else:
envVars[envCmdVar] = tHeight
elif envVars.get(envCmdVar) != None:
envVars.pop(envCmdVar)
scrWdth = int(envVars["_scrWidth"])
if envCmdVar == 'LIB':
path.clear()
path.extend(['']+envVars.get("LIB","").split(';'))
else:
print("Illegal switch, Command Format: SET[/a|/p] [variable = [string|expression]]")
elif cmd in ["PROMPT","PATH"]:
if len(args) == 1:
print(cmd+"="+envVars.get(cmd,""))
else:
envVars[cmd] = args[1]
elif cmd in ["RENAME","REN","MOVE"]:
# todo: allow source to be file and target directory?
# Wildcard renames
# renames across sd mount points
if len(args) == 3:
# Check that first argument has a valid path and exists
if validPath and newdir in os.listdir(tmpDir) and args[1][-1] != sep:
args[2] = absolutePath(args[2],savDir)
aPath2 = args[2].split(sep)
newdir2 = aPath2.pop(-1)
(validPath, tmpDir2) = chkPath(aPath2)
if newdir2 == '*':
newdir2 = newdir
# Second argument has valid path
if validPath and args[2][-1] != sep and '?' not in newdir2 and \
'*' not in newdir2 and newdir2 !="." and newdir2 != "..":
# second argument doesn't specify an existing target
if newdir2 not in os.listdir(tmpDir2):
currDRen = False
if not aFile(tmpDir+sep+newdir):
if tmpDir+sep+newdir == os.getcwd():
currDRen = True
os.rename(pFmt(tmpDir)+newdir,pFmt(tmpDir2)+newdir2)
if currDRen:
os.chdir(tmpDir2)
else:
print("Target file exists")
else:
print("Invalid target:",args[2])
else:
print("Invalid source:",args[1])
else:
print("Wrong number of arguments")
elif cmd in ["DELETE","DEL"]:
if len(args) == 2:
if validPath:
if not (swAllB-int('000010',2)) & swBits:
if "*" in newdir or "?" in newdir:
ans = "Y"
if newdir == "*" or newdir == "*.*":
ans = input(tmpDir+newdir+", Are you sure (y/n)? ").upper()
if ans == "Y":
delFiles(tmpDir,newdir,bool(swBits&int('000010',2)),False)
else:
if newdir in os.listdir(pFmt(tmpDir,False)):
if aFile(tmpDir+newdir):
os.remove(tmpDir+newdir)
print((tmpDir+newdir).replace('/',('\\' if envVars.get('DIRSEP','/') == '\\' else '/')),"deleted.")
else:
ans = input(tmpDir+newdir+sep+"*, Are you sure (y/n)? ").upper()
if ans == "Y":
delFiles(tmpDir+newdir+sep,'*',bool(swBits&int('000010',2)),False)
else:
if swBits & int('000010',2):
ans = input(tmpDir+newdir+sep+"*, Are you sure (y/n)? ").upper()
if ans == "Y":
delFiles(tmpDir,newdir,True,False)
else:
print(f"Unable to delete: {tmpDir}{newdir}. File not found.")
else:
print("Illegal switch, Command Format: DEL[/s] [path][file]")
else:
print(f"Unable to delete: {tmpDir}{newdir}. File not found.")
else:
print("Illegal Path.")
elif cmd in ["TYPE","MORE"]:
if len(args) == 2:
if validPath and newdir in os.listdir(pFmt(tmpDir,False)) and aFile(tmpDir+newdir):
if cmd == "MORE":
swBits = swBits | int('000100',2)
if not ((swAllB-int('000100',2)) & swBits):
swPause = bool(swBits & int('000100',2))
key = " "
with open(tmpDir+newdir, "rb") as f:
nLines = 0
for line in f:
istrt = 0
while istrt+scrWdth < len(line):
(quit,nLines) = scrnPause(swPause,nLines,[None])
try:
print(line[istrt:istrt+scrWdth].decode())
except:
print(chr(65534)*scrWdth)
nLines += 1
istrt += scrWdth
(quit,nLines) = scrnPause(swPause,nLines,[None])
if quit:
break
try:
print(line[istrt:len(line)].decode(),end="")
except:
print(chr(65534)*(len(line)-istrt))
nLines += 1
else:
print("Illegal switch, Command Format: TYPE[/p] [path][file]")
else:
print(f"Unable to display: {tmpDir}{newdir}. File not found.")
else:
print("Illegal Path.")
elif cmd in ["CHDIR","CD"]:
if len(args) == 1:
print(os.getcwd().replace('/',('\\' if envVars.get('DIRSEP','/') == '\\' else '/')))
else:
if validPath:
os.chdir(tmpDir)
else:
print("Unable to change to:",args[1])
elif cmd in ["MKDIR","MD"]:
if len(args) == 1:
print("Unable to make .")
elif len(args) > 2:
print("Too many arguments")
else:
if validPath:
if newdir not in os.listdir(pFmt(tmpDir,False)):
os.mkdir(tmpDir+newdir)
else:
print("Target name already exists")
else:
print("Invalid path")
elif cmd in ["RMDIR","RD","DELTREE"]:
if len(args) == 2 and validPath:
if cmd == "DELTREE":