-
Notifications
You must be signed in to change notification settings - Fork 4
/
qbsh.bas
518 lines (480 loc) · 13.4 KB
/
qbsh.bas
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
_Title "QBSH - Quick Basic Shell"
_ConsoleTitle "QBSH - Quick Basic Shell"
$Console:Only
_Dest _Console
_Source _Console
Screen 0
GoSub INIT
MAIN:
script_mode = False
If _Trim$(Command$) = "" Then
GoSub WELCOME
Do
On Error GoTo GENERALERROR
GoSub PROMPT
Line Input ""; cmd$
GoSub ROUTECMD
Loop
ElseIf InStr(_Trim$(Command$), "-x") = 1 Then
cmd$ = Right$(_Trim$(Command$), Len(_Trim$(Command$)) - 2)
GoSub ROUTECMD
ElseIf _FileExists(_Trim$(Command$)) Then
args$ = _Trim$(Command$)
GoSub RUNSCRIPT
Else
Print "QBSH - Quick BASIC Shell"
Print
Print "Run without args to run qbsh interactively."
Print "Pass a script file as an argument to run a script."
Print "Run a single command with -x."
Print
GoSub HELP
End If
System
'Very important 8Ball function
EIGHTBALL:
Dim BALLREPLY$(1 To 20)
BALLREPLY$(1) = "It is certain."
BALLREPLY$(2) = "It is decidedly so."
BALLREPLY$(3) = "Without a doubt."
BALLREPLY$(4) = "Yes definitely."
BALLREPLY$(5) = "Very doubtful."
BALLREPLY$(6) = "You may rely on it."
BALLREPLY$(7) = "As I see it, yes."
BALLREPLY$(8) = "Most likely."
BALLREPLY$(9) = "Outlook good."
BALLREPLY$(10) = "Yes."
BALLREPLY$(11) = "Signs point to yes."
BALLREPLY$(12) = "Reply hazy, try again."
BALLREPLY$(13) = "Ask again later."
BALLREPLY$(14) = "Better not tell you now."
BALLREPLY$(15) = "Cannot predict now."
BALLREPLY$(16) = "Concentrate and ask again."
BALLREPLY$(17) = "Don't count on it."
BALLREPLY$(18) = "My reply is no."
BALLREPLY$(19) = "My sources say no."
BALLREPLY$(20) = "Outlook not so good."
BALLREPLYSEL% = Int(Rnd * 20) + 1
Print BALLREPLY$(BALLREPLYSEL%)
Return
'Add, Subtract, Multiply, and Divide
CALC:
baseval$ = args$
If InStr(baseval$, " ") < 2 Then
Print "Improper CALC Syntax. Ex: 1 + 2"
Return
End If
v1# = Val(Left$(baseval$, InStr(baseval$, " ")))
baseval2$ = Right$(baseval$, Len(baseval$) - InStr(baseval$, " "))
If InStr(baseval2$, " ") < 2 And InStr(baseval2$, "r") <> 1 Then
Print "Improper CALC Syntax. Ex: 1 + 2"
Return
End If
oper$ = Left$(baseval2$, 1)
If oper$ <> "+" And oper$ <> "-" And oper$ <> "*" And oper$ <> "x" And oper$ <> "/" And oper$ <> "%" And oper$ <> "^" And oper$ <> "r" Then
Print "Improper CALC Syntax. Ex: 1 + 2"
Return
End If
v2# = Val(Right$(baseval2$, Len(baseval2$) - 2))
Select Case oper$
Case "+"
Print v1# + v2#
Case "-"
Print v1# - v2#
Case "*", "x"
Print v1# * v2#
Case "/"
If v2# <> 0 Then
Print v1# / v2#
Else
Print "Divide By Zero? Are you insane?!?"
End If
Case "%"
If v2# >= -0.5 And v2# <= 0.5 Then
Print "Divide By Zero? Are you insane?!?"
Else
Print v1# Mod v2#
End If
Case "^"
Print v1# ^ v2#
Case "r"
Print Sqr(v1#)
End Select
Return
'Change working directory
CDIR:
dir$ = resolvePath$(args$)
If dir$ = "" Then
dir$ = Environ$("HOME")
End If
If _DirExists(dir$) Then
On Error GoTo CDIRERR
ChDir dir$
Else
Print "CHDIR <DIRECTORY>"
End If
Return
CDIRERR:
Print "Couldn't change directory to "; args$; ". It's likely that user doesn't have permissions."
Beep
Resume Next
Return
'Is it Easter or Christmas?
CHRISTMAS:
Play "t140o2p4g2e4.f8g4o3c2o2b8o3c8d4c4o2b4a8g2.o2b8o3c8d4c4o2b4a8a8g8o3c4o2e8e4g8a8g4f4e4f4g2.g2e4.f8g4o3c2o2b8o3c8d4c4o2b4a8g2.o2b8o3c8d4c4o2b4a8a8g8o3c4o2e8e4g8a8g4f4e4d4c2.c4a4a4o3c4c4o2b4a4g4e4f4a4g4f4e2.e8e8d4d4g4g4b4b4o3d4d8o2b8o3d4c4o2b4a4g4p4g2g2e4.f8g4o3c2o2b8o3c8d4c4o2b4a8g8g2.o2b8o3c8d4c4o2b4a8a8g8o3c4o2e8e4g8a8g4f4e4d4c2.p4t180g8g8g4g4g4a8g8g4g4g4a4g4e4g4d1t180g8g8g4g4g4a8g8g4g4g4g8g8g4a4b4o3c2c4p1"
Sleep 65
Return
'Clear the screen and display welcome text
CLEARSCR:
Cls
GoSub WELCOME
Return
'Offload unhandled call to legacy system shell. qbsh is the only shell of the future.
CMDOUT:
stdOut$ = Subprocess$(cmd$)
Return
'Delete a file path
DEL:
pathspec$ = resolvePath$(args$)
If _FileExists(pathspec$) Then
On Error GoTo DELERR
Kill pathspec$
ElseIf _DirExists(pathspec$) Then
Print "Path is a directory. Empty it and then use RMDIR instead."
Else
Print "File not found."
End If
Return
DELERR:
Print "Could not delete "; args$; ". It's likely that this user lacks permissions."
Beep
Resume Next
Return
'Take a look around at your environment. And then print that.
ENV:
If args$ = "" Then
I = 0
Do
I = I + 1
setting$ = Environ$(I)
If InStr(setting$, "SHELL=") = 1 Then
Print "SHELL="; SELFPATH$
Else
Print setting$
End If
Loop Until setting$ = ""
ElseIf args$ = "SHELL" Then
Print SELFPATH$
Else
Print Environ$(args$)
End If
Return
'General Error handler so xmessage doesn't get triggered
GENERALERROR:
Print "Something went terribly wrong. Here's all we know:"
Print "Error "; Err; " on program file line "; _ErrorLine
Beep
Resume MAIN
Return
'Tell users some of what we can do
HELP:
Print "Try One of These Commands:"
Print "8BALL - Answers to life's deepest questions."
Print "CALC - Add, Subtract, Multiply, and Divide"
Print "CHDIR - Change working directory. (Or CD)"
Print "CLEAR - Clear the current screen"
Print "DATE - Today's Date"
Print "DIR <Optional directory> - List files in directory"
Print "DELETE - Delete a file"
Print "DEVICES - Display info about attached input devices"
Print "ENV <Optional Key> - Print Environment"
Print "MAKEDIR <directory> - Make a new directory"
Print "OS - Display the Operating System type"
Print "PIP <DEST>=<SRC> - Copy a file to a new path, CP/M style."
Print "PLAY <Notes> - Play sounds and rock out!"
Print "PRINT - Output some text"
Print "RAND <Optional Limit> - Random number generator"
Print "RENAME <File/Dir> <New Name> - Rename a file or directory"
Print "READFILE <file> - Output some text file to terminal"
Print "RMDIR <Directory> - Delete a directory"
Print "RUN <file.qsh> - Run a qbsh script"
Print "TIME - Current time"
Print "XDIR <Optional directory> - List detailed directory info"
Print "WHO AM I - Sometimes we all forget, right?"
Print
Print "To exit the interactive shell, run `QUIT`"
Return
'Get current path and change to proper place
INIT:
Randomize Timer
SELFPATH$ = Environ$("_")
''Initial increment starting point for script read buffers
rs% = 5
If SELFPATH$ = "" Or InStr(SELFPATH$, ".") = 1 Then
If InStr(Command$(0), ".") = 1 Then
SELFPATH$ = _CWD$ + "/" + Right$(Command$(0), Len(Command$(0)) - 2)
Else
SELFPATH$ = _CWD$ + "/" + Command$(0)
End If
End If
ChDir (_StartDir$)
Return
'List current input devices
LSDEV:
For I = 1 To _Devices
Print _Device$(I)
Print "Buttons:"; _LastButton(I); "Axis:"; _LastAxis(I); "Wheels:"; _LastWheel(I)
Next
Return
'Make a new directory
MAKEDIR:
dir$ = resolvePath$(args$)
If dir$ <> "" Then
On Error GoTo MAKEDIRERR
MkDir dir$
Else
Print "MAKEDIR <New Directory Path>"
End If
Return
MAKEDIRERR:
Print "Unable to make "; args$; ". Likely user doesn't have perms or parent path doesn't exist."
Beep
Resume Next
Return
'Is there an echo in here?
OUTCMD:
If script_mode = True And refcmd$ = "LPRINT" Then
Print args$;
Else
Print args$
End If
Return
PIP:
If args$ = "" Or InStr(args$, "=") < 2 Then
Print "PIP <DEST>=<SRC>"
Return
End If
dest$ = resolvePath$(Left$(args$, InStr(args$, "=") - 1))
src$ = resolvePath$(Right$(args$, Len(args$) - InStr(args$, "=")))
If Not _FileExists(src$) Then
If _DirExists(src$) Then
Print src$; " is a directory. Only regular files are supported for the source."
Else
Print src$; " does not exist or isn't readable."
End If
Return
End If
If Not script_mode And _FileExists(dest$) Then
Print dest$; " exists."
Input "Overwrite? (Y/N) ", confirm$
If UCase$(confirm$) <> "YES" And UCase$(confirm$) <> "Y" Then
Return
End If
End If
If _DirExists(dest$) Then
srcfile$ = src$
If InStr(_OS$, "WINDOWS") > 0 Then
dirchar$ = "\"
Else
dirchar$ = "/"
End If
While InStr(srcfile$, dirchar$) > 0:
srcfile$ = Right$(srcfile$, Len(srcfile$) - InStr(srcfile$, dirchar$))
Wend
dest$ = dest$ + dirchar$ + srcfile$
End If
On Error GoTo PIPERR
Open src$ For Binary As #1
Open dest$ For Binary As #2
ffbc$ = Space$(LOF(1))
Get #1, , ffbc$
Put #2, , ffbc$
Close #1
Close #2
Return
PIPERR:
Print "Failed to copy "; src$; " to "; dest$; ". Likely user lacks permissions or target path doesn't exist."
Beep
Resume MAIN
Return
'Make tunes and rock out
PLAYSOUND:
If args$ <> "" Then
On Error GoTo PLAYSOUNDERR
Play args$
Else
Print "PLAY <NOTES>"
End If
Return
PLAYSOUNDERR:
Print "Could not parse the notes to play."
Beep
Resume Next
Return
'So user knows where they are and who they are
PROMPT:
Color 14
Print _CWD$ + " ";
user$ = Environ$("USER")
If user$ = "root" Then
Color 12
Print "root> # ";
Else
Color 10
Print user$ + "> $ ";
End If
Color 15
Return
'Return a Random Number
RANDNUM:
If args$ <> "" Then
randlimit = Int(Val(args$))
Else
randlimit = 10
End If
If randlimit < 0 Then
randlimit = randlimit * -1
posnegmod = Int(-1)
Else
posnegmod = 1
End If
Print Int(Rnd * (randlimit + 1) * posnegmod)
Return
'This sub reads a file.
READFILE:
tmpfileloc$ = resolvePath$(args$)
If Not _FileExists(tmpfileloc$) Then
Print "File not found."
Return
End If
On Error GoTo READFILEERR
Open tmpfileloc$ For Binary As #1
x$ = Space$(LOF(1))
Get #1, , x$
Close #1
Print x$
Return
READFILEERR:
Print "Could not read "; args$; ". Likely user lacks permissions."
Resume MAIN
Return
'Remove directory
REMDIR:
path$ = resolvePath$(args$)
If path$ <> "" And _DirExists(path$) Then
On Error GoTo REMDIRERR
RmDir path$
Else
Print "RMDIR must be called against an empty directory"
End If
Return
REMDIRERR:
Print "Couldn't remove "; args$; ". Likely the directory is not empty or user lacks permissions."
Beep
Resume Next
Return
'Rename/Move a file or folder
RENAME:
If args$ = "" Then
Print "RENAME <FILE/DIR> <NEW NAME>"
Return
End If
If InStr(UCase$(args$), " AS ") = 0 Then
sourcepath$ = _Trim$(Left$(args$, InStr(args$, " ")))
targetpath$ = _Trim$(Right$(args$, Len(args$) - Len(sourcepath$)))
Else
sourcepath$ = _Trim$(Left$(args$, InStr(UCase$(args$), " AS ")))
targetpath$ = _Trim$(Right$(args$, Len(args$) - (InStr(UCase$(args$), " AS ") + 3)))
End If
sourcepath$ = resolvePath$(sourcepath$)
targetpath$ = resolvePath$(targetpath$)
If InStr(UCase$(args$), " AS ") = 0 And InStr(targetpath$, " ") > 0 Then
Print "Syntax for paths with spaces: RENAME file/dir AS new name"
Return
ElseIf Not _FileExists(sourcepath$) And Not _DirExists(sourcepath$) Then
Print "Source path needs to be a file or directory."
Return
Else
On Error GoTo RENAMEERR
Name sourcepath$ As targetpath$
End If
Return
RENAMEERR:
Print "Failed to rename "; sourcepath$; "to "; targetpath$; ". Likely user lacks permissions or target path doesn't exist."
Beep
Resume MAIN
Return
'Route commands to right function
ROUTECMD:
cmd$ = _Trim$(cmd$)
If InStr(cmd$, " ") = 0 Then
refcmd$ = cmd$
args$ = ""
Else
refcmd$ = Left$(cmd$, InStr(cmd$, " ") - 1)
args$ = Right$(cmd$, Len(cmd$) - InStr(cmd$, " "))
End If
If cmd$ = "" Or InStr(cmd$, "'") = 1 Then Return
Select Case UCase$(refcmd$)
Case "EXIT", "QUIT": GoSub QUIT
Case "HELP": GoSub HELP
Case "8BALL": GoSub EIGHTBALL
Case "BEEP": Beep
Case "CD", "CHDIR": GoSub CDIR
Case "DEL", "DELETE", "RM", "ERA", "ERASE": GoSub DEL
Case "CALC": GoSub CALC
Case "CHRISTMAS": GoSub CHRISTMAS
Case "CLEAR", "CLS": GoSub CLEARSCR
Case "DATE": Print Date$
Case "DEVICES": GoSub LSDEV
Case "DIR": Print listDir$(args$)
Case "ENV": GoSub ENV
Case "MAKEDIR": GoSub MAKEDIR
Case "RENAME", "NAME", "MOVE", "REN": GoSub RENAME
Case "OS": Print _OS$
Case "PI": Print _Pi
Case "PIP": GoSub PIP
Case "LPRINT", "PRINT": GoSub OUTCMD
Case "PLAY": GoSub PLAYSOUND
Case "RAND": GoSub RANDNUM
Case "RMDIR": GoSub REMDIR
Case "RUN": GoSub RUNSCRIPT
Case "TIME": Print Time$
Case "USER", "WHO": Print Environ$("USER")
Case "XDIR": Print listXDir$(args$)
Case "READFILE", "CAT", "TYPE": GoSub READFILE
Case Else: GoSub CMDOUT
End Select
Return
RUNSCRIPT:
If _FileExists(args$) Then
rs% = rs% + 1
Open _Trim$(args$) For Input As #rs%
Do Until EOF(rs%)
On Error GoTo RUNSCRIPTERR
script_mode = True
Line Input #rs%, cmd$ 'read entire text file line
GoSub ROUTECMD
Loop
Close #rs%
rs% = rs% - 1
script_mode = False
Else
Print "Could not open script file at "; args$
End If
Return
RUNSCRIPTERR:
Print "Failed to open/run script at "; args$; ". Check that the file exists and you have permissions to read it."
Resume Next
Return
'Give a way to clo(se this because this isn't vim
QUIT:
System
'A friendly greeting
WELCOME:
Color 15
Print "WELCOME TO Quick Basic Shell, " + Environ$("USER")
Print "Type HELP to see a list of commands."
Print
Return
'$INCLUDE:'lib/io.bas'