Repository navigation
Expand file tree
/
Copy pathline.asm
More file actions
1612 lines (1519 loc) · 29.6 KB
/
Copy pathline.asm
File metadata and controls
1612 lines (1519 loc) · 29.6 KB
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
keep obj/line
mcopy line.macros
****************************************************************
*
* Line
*
* This module contains the subroutines used to directly
* manipulate the buffer containing the current command line.
*
****************************************************************
*
copy sh.dp
LineCommon privdata
buffSize ds 4 size of the line buffer
ch ds 2 next EXEC character
firstToken ds 2 disp to token (for ExpandAlias)
line dstr ,255 user input line
pipenum dc c'0' pipe number (as a character)
shellPtr dc a4'0' ptr to the first char in a shell variable
shellLen dc i'0' # chars left in the shell variable
lineLen equ 256 length of a line
lLine ds lineLen logical line buffer for short lines
eLine ds lineLen exec file line buffer for short lines
lAllocated ds 2 was lp^ allocated? (default is @lLine)
eAllocated ds 2 was ep^ allocated? (default is @eLine)
end
****************************************************************
*
* ChangeChar - change a character
*
* Inputs:
* X - index into the line
* A character to change to
*
* Notes:
* 1. The A register may be long or short on entry
* 2. X is not changed
*
****************************************************************
*
ChangeChar start
debug ChangeChar
php
phx
tay
txa
clc
adc lDisp
cmp lLength
bge lb1
tyx
tay
txa
short M
sta [lp],Y
long M
lb1 plx
plp
rts
end
****************************************************************
*
* ClearLine - clear any information from the current line
*
* Inputs:
* lp - pointer to the current line
*
****************************************************************
*
ClearLine start
debug ClearLine
using LineCommon
lda lAllocated if there is a line then
beq lb1
free lp dispose of it
lb1 stz lp set the pointer to NULL
stz lp+2
stz lAllocated note that nothing is allocated
lda eAllocated if there is an exec line then
beq lb2
free ep dispose of it
lb2 stz ep set the pointer to NULL
stz ep+2
stz eAllocated note that nothing is allocated
stz line+1 clear user inputs, too
rts
end
****************************************************************
*
* Continuation - check for a continuation line
*
* Inputs:
* exec_disp - disp into exec file
* r0 - ptr to exec file
*
* Outputs:
* exec_disp - points to 1st char in next line if the line
* was continued
* C - set for a continuation, else clear
*
****************************************************************
*
Continuation private
using LineCommon
using Common
debug Continuation
using RecCom
ldy exec_disp scan for non-whitespace before a CR
short M
lb1 lda [r0],Y
beq lb3
cmp #RETURN
beq found
jsl IsWhite
bcc lb3
iny
bra lb1
lb3 long M
clc none found - return
rts
found long M
iny found - update exec_disp
sty exec_disp
sec
rts
end
****************************************************************
*
* ExpandAlias - if the last token in the line is an alias, expand it
*
* Inputs:
* lp - line pointer
* lLength - # chars in the line
* firstToken - disp to the start of the token
*
* Outputs:
* C - set for error; clear if no error
*
****************************************************************
*
ExpandAlias private
using Common
using LineCommon
debug ExpandAlias
ph4 r0
sec quit if there are no characters
lda eLength
sbc firstToken
beq lb3
sta r16 save the alias length
inc A reserve space for the OS name
inc A
jsl Malloc
sta r12
stx r14
ora r14
beq lb3
lda r16 set the string length
sta [r12]
clc get the address of the characters -2
lda firstToken
adc ep
sta r16
lda ep+2
adc #0
sta r18
sub4 r16,#2 copy over the alias name
lda [r12] copy over the characters
tax
ldy #2
short M
lb1 lda [r16],Y
sta [r12],Y
iny
dex
bne lb1
long M
ph4 r4 find the alias
jsr FindAlias
pl4 r4
free r12 free the os string buffer
lda r0 quit if there is no alias
ora r2
beq lb3
lda firstToken erase the alias
sta eLength
ldy #2 put the characters
lda [r8]
tax
lb2 lda [r8],Y
and #$00FF
phy
jsr PutChar
ply
bcs lb4
iny
dex
bne lb2
lb3 clc
lb4 pl4 r0
rts
end
****************************************************************
*
* ExpandPipe - expand a pipe in the current line buffer
*
* Inputs:
* lp - line pointer
* lLength - # chars in the line
*
* Outputs:
* C - set for error; clear if no error
*
****************************************************************
*
ExpandPipe private
using LineCommon
using Common
debug ExpandPipe
lda #' ' write a padd char
jsr PutChar
bcs rts
lda #'>' write the output redirection char
jsr PutChar
bcs rts
jsr WritePipe write '3/syspipe'
bcs rts
lda #RETURN write the line feed
jsr PutChar
lda #'<' write the input redirection char
jsr PutChar
bcs rts
jsr WritePipe write '3/syspipe'
bcs rts
lda #' ' write a padd char
jsr PutChar
bcs rts
inc pipenum increment the pipe number for next time
clc
rts rts
;
; WritePipe - write '14:syspipe[pipenum]'
;
WritePipe ldx #0
wp1 lda pipeName,X
and #$00FF
phx
jsr PutChar
plx
bcs rts
inx
cpx #l:pipeName
bne wp1
lda pipenum
brl PutChar
pipeName dc c'14:SYSPIPE'
end
****************************************************************
*
* ExpandShellVariable - expand a shell variable
*
* Outputs:
* shellPtr - pointer to the first char of the var; NULL
* for none
* shellLen - # chars left in the variable; 0 for none
*
****************************************************************
*
ExpandShellVariable private
using LineCommon
using Common
debug ExpandShellVariable
;
; Collect the shell name in the user input line buffer. Continue
; collecting characters until:
;
; 1. The terminating '}' is found.
; 2. 255 characters have been found, in which case you still scan
; for the terminating '}', but ignore all characters past the
; 255th.
; 3. The end of the line or exec file is reached, in which case
; a '}' is implied.
;
stz line+1
cn1 jsr NextExecChar
lda ch
cmp #'}'
beq fv1
cmp #RETURN
beq fv1
short I,M
ldx line+1
inx
beq cn2
stx line+1
sta line+1,X
cn2 long I,M
bra cn1
;
; Find the shell variable
;
fv1 stz shellPtr assume there is no shell variable
stz shellPtr+2
stz shellLen
short I,M null terminate the shell var name
ldx line+1
beq fv4
lda #0
sta line+2,X
long I,M
ph4 r0 save critical variables
ph4 r4
ph4 #line+2 get the variable value
jsl CtoOSString
sta r12
stx r14
ora r14
beq fv2
jsr FindVariable
free r12 free(r12)
lda r0 if there is a value then
ora r2
beq fv2
lda [r8] set the length
sta shellLen
add4 r8,#2,shellPtr set the address
fv2 pl4 r4 restore variables
pl4 r0
fv4 long I,M
stz line+1 no user line...
rts
end
****************************************************************
*
* GetChar - get a character from the command line
*
* Inputs:
* lp - line pointer
* lDisp - disp in line buffer
* line - user input line
*
* Outputs:
* lDisp - updated
* A - character; RETURN if line was empty
*
* Notes:
* 1. X, Y preserved.
* 2. Register sizes on entry can be anything.
* 3. Direct page varables are not disturbed.
*
****************************************************************
*
GetChar start
debug GetChar
using LineCommon
using Common
;
; Initialization
;
php save register size
long I switch to the size we want
short M
phx save registers
phy
lda line+1 branch if input is from the user's line
bne rl1
;
; Handle input from the normal line
;
ldy lDisp return RETURN if past the end of line
cpy lLength
blt in2
lda #RETURN
bra tr1
in2 lda [lp],Y get the character
cmp #RETURN if it is not a RETURN then
beq tr1
iny increment the character counter
sty lDisp
bra tr1
;
; Read a character from the user line
;
rl1 short I get the character
lda line+2
pha
dec line+1 remove it from the line
beq rl3
ldx line+1
ldy #0
rl2 lda line+3,Y
sta line+2,Y
iny
dex
bne rl2
rl3 pla
long I
;
; Terminal processing
;
tr1 long M zero the high byte
and #$00FF
ply restore regs & return
plx
plp
rts
end
****************************************************************
*
* GetLine - read a line from stdin
*
* Outputs:
* line - line read
*
* Notes:
* Skips any remaining characters in the current command
* line.
*
****************************************************************
*
GetLine start
using LineCommon
using Common
debug GetLine
stz line+1 make sure there are no characters in
! an old line
lb1 jsr GetChar eat characters until the end of line
cmp #RETURN is reached
bne lb1
gets line,cr=t get the line
rts
end
****************************************************************
*
* GetString - read a (possibly quoted) string from the command line
*
* Outputs:
* X-A - pointer to an OS output string; null for error
* C - set for error, else clear
*
* Notes:
* 1. If no error is flagged, the caller is responsible
* for doing a dispose on the pointer.
*
****************************************************************
*
GetString start
debug GetString
using LineCommon
using Common
jsr LineLength reserve space for the variable
inc A
inc A
inc A
jsl Malloc
sta r12
stx r14
ora r14
beq err1
stz quote assume that there is no quote
ldy #2 no characters read so far
jsr GetChar get the first character from the line
cmp #'"' branch if it is not a quote
bne lb2
inc quote note that there was an opening quote
lb1 jsr GetChar get the next character
lb2 cmp #RETURN branch if all characters have been read
beq lb4
ldx quote if there was a starting quote then
beq lb3
cmp #'"' if it is a quote then...
bne lb3
lb2a jsr GetChar get the next char, too.
jsl IsWhite skip any white space
bcs lb2a
cmp #RETURN if there are no more chars then
beq lb5 we are finished
cmp #'"' if it is not a quote then
bne err2 flag the error
lb3 sta [r12],Y save the character in the string
iny
bra lb1
lb4 ldx quote make sure we were not expecting a
bne err2 closing quote
lb5 tya set the length
dec A
dec A
sta [r12]
lda r12 return the pointer
ldx r12+2
clc
rts
err1 lda #outOfMem could not allocate memory
jsl SysError
sec
rts
err2 free r12 dispose of the work area
puts #'Mismatched quotes',cr=t,errout=t
sec
rts
quote ds 2 quote flag
end
****************************************************************
*
* GetToken - read a (possibly quoted) string from the command line
*
* Outputs:
* X-A - pointer to a GS/OS input string; null for error
* C - set for error, else clear
*
* Notes:
* 1. If no error is flagged, the caller is responsible
* for doing a dispose on the pointer.
*
****************************************************************
*
GetToken start
debug GetToken
using LineCommon
using Common
jsr LineLength reserve space for the variable
inc A
inc A
jsl Malloc
sta r12
stx r14
ora r14
beq err1
stz quote assume that there is no quote
ldy #2 no characters read so far
lb0 jsr GetChar get the first character from the line
jsl IsWhite
bcs lb0
cmp #'"' branch if it is not a quote
bne lb2
inc quote note that there was an opening quote
lb1 jsr GetChar get the next character
lb2 cmp #RETURN branch if all characters have been read
beq lb4
ldx quote if there was a starting quote then
beq lb2a
cmp #'"' if it is a quote then...
bne lb3
jsr GetChar get the next char, too.
cmp #RETURN if there are no more chars then
beq lb5 we are finished
cmp #'"' if it is a quote then
beq lb3 include it as a character
bra lb5 else we are done
lb2a jsl IsWhite else if this is whitespace then
bcs lb5 we are done
lb3 short M save the character in the string
sta [r12],Y
long M
iny
bra lb1
lb4 ldx quote make sure we were not expecting a
bne err2 closing quote
lb5 tya set the token length
dec A
dec A
sta [r12]
lda r12 return the pointer
ldx r12+2
clc
rts
err1 lda #outOfMem could not allocate memory
jsl SysError
sec
rts
err2 free r12 dispose of the work area
puts #'Mismatched quotes',cr=t,errout=t
sec
rts
quote ds 2 quote flag
end
****************************************************************
*
* LastCLine - Get the last line
*
* Outputs:
* C - set if error, else clear
*
****************************************************************
*
LastCLine start
debug LastCLine
using Common
using LineCommon
lb1 jsr Pause check for early exit
bcs err2
jsr LastCommand last command on this line
bcc lb2
clc
rts
lb2 jsr ClearLine dispose of old line
jsr LastLine last line in EXEC file
bcs err
lda eLength skip to last command in line
sta eDisp
bra lb1
err puts #'Error during line skip',cr=t,errout=t
err2 lda #true
sta exec_exit
sec
rts
end
****************************************************************
*
* LastCommand - last command to LINE
*
* Inputs:
* ep - pointer to exec file line
* eDisp - disp in exec file
*
* Outputs:
* eDisp - points to next command line
* lp - pointes to expanded command line
* lLength - length of command line
* lDisp - set to 0
* C - set if command found
*
****************************************************************
*
LastCommand private
debug LastCommand
using LineCommon
using Common
;
; Initialization
;
lda ep quit if there is no line
ora ep+2
beq no1
;
; Skip back to the start of the previous line
;
short M
ldy eDisp branch if we are at the start
beq no1
dey branch if the start is a blank line
beq lb4
lda #RETURN skip back to the start or the revious
lb1 dey RETURN
beq lb2
cmp [ep],Y
bne lb1
bra lb3
lb2 cmp [ep],Y
bne lb4
lb3 iny
lb4 sty eDisp set the displacement
long M
phy
jsr NextCommand expand shell variables
pl2 eDisp
sec
rts
;
; Exit here if no previous line exists
;
no1 long M
clc
rts
end
****************************************************************
*
* LastLine - Last line in EXEC list
*
* Inputs:
* EXEC_PTR - pointer to exec file
* EXEC_DISP - displacement into file
*
* Outputs:
* lp - pointer to line read
* lDisp - 0
* lLength - length of the line
* C - set if error, else clear if line read
*
****************************************************************
*
LastLine private
debug LastLine
using RecCom
using Common
d_addr equ 0 disp to pointer to EXEC file
r_next equ 0 disp to handle of next entry
r_addr equ 4 disp to pointer to EXEC file
r_disp equ 8 disp to disp into EXEC file
r equ r0 pointer to current entry
next equ r4+r_next handle of next entry
addr equ r4+r_addr addr of EXEC file
disp equ r4+r_disp disp into EXEC file
move4 exec_ptr,r4 get the exec file pointer
ldy exec_disp
jsr Back back up two lines
bcs err
jsr Back
bcs err
sty exec_disp save disp into exec file
jsr ClearLine clear the old line
jmp NextLine read the line
err sec no line found
rts
;
; Back - back up one line
;
Back tya error if at start of list
beq err
bk0 dey error if last char is not RETURN
lda [r4],Y
and #$007F
cmp #RETURN
bne err
bk1 dey back up to last RETURN or to start of
bmi bk2 list
lda [r4],Y
and #$007F
cmp #RETURN
bne bk1
bk2 iny
beq bk5 if not at the start of the list then
short M see if this line was continued
phy
dey
beq bk4
bk3 dey
beq bk4
lda [r4],Y
cmp #' '
beq bk3
cmp #TAB
beq bk3
cmp #'\'
bne bk4
long M
ply
bra bk0
bk4 long M
ply
bk5 clc
rts
end
****************************************************************
*
* LineAddr - return a pointer to the command line
*
* Note: this command line is a separately allocated dynamic
* variable. The pointer must be disposed of by the caller
* when the need for the line is exhausted. There must be a
* line when this routine is called.
*
* Inputs:
* lp - line pointer
* lDisp - disp in lp
* quote - processing a quoted string?
*
* Outputs:
* X-A - address
*
****************************************************************
*
LineAddr start
debug LineAddr
ph2 lDisp save volitile labels
stz lDisp go back to the start of the line
jsr LineLength get the length of the line
sta len
inc A reserve space for it
jsl Malloc
sta r12
stx r14
ora r14
beq lb3
ldy len move in the line
short M
lda #0
sta [r12],Y
tyx
beq lb3
dey
beq lb2
lb1 lda [lp],Y
sta [r12],Y
dey
bne lb1
lb2 lda [lp]
sta [r12]
lb3 long M
pl2 lDisp restore volitile variables
lda r12 return the result
ldx r14
rts
len ds 2
end
****************************************************************
*
* LineLength - return the number of characters left in the
* current line.
*
* Inputs:
* lp - pointer to the current line
* lDisp - disp in lp for next character
* quote - processing a quoted string?
*
* Outputs:
* A - # bytes left
*
****************************************************************
*
LineLength start
debug LineLength
using Common
using LineCommon
;
; Handle a line in the GetLine line buffer
;
lda line+1 if there is a line in the GetLine buffer
and #$00FF
bne lb3 return it's length
;
; Handle a line in the input line buffer
;
lda lp quit if there is no line
ora lp+2
beq lb3
short M use short A to scan the line
ldy lDisp get the current disp
lb1 lda [lp],Y skip to the RETURN
cmp #RETURN
beq lb2
iny
bra lb1
lb2 long M compute the line length
tya
sec
sbc lDisp
lb3 rts
end
****************************************************************
*
* NextChar - return a character and increment the index
*
* Inputs:
* X - index into the line
* lp - command line
*
* Outputs:
* A - character read; RETURN if at end of line
* X - incremented
*
* Notes:
* 1. Y is not changed.
* 2. Direct page is not changed.
* 3. The A register may be long or short on entry.
*
****************************************************************
*
NextChar start
using Common
using LineCommon
debug NextChar
;
; Handle the user line
;
php save the caller's reg size
long M
lda line+1 branch if there is no user line
and #$00FF
beq lb1
sta length return $0D if the disp is past the end
cpx length of the user line
blt ul1
lda #$000D
bra rts
ul1 inx return the character
lda line+1,X
and #$00FF
bra rts
;
; Handle input from the line buffer
;
lb1 phy get the disp into the line buffer
txa
clc
adc lDisp
cmp lLength if past the line
blt lb2
lda #RETURN return RETURN
bra none
lb2 tay get the character
lda [lp],Y
and #$00FF
cmp #RETURN branch if at end of line
beq none
inx inc the index
none ply restore caller's Y
rts plp
rts
;
; local data
;
length ds 2
end
****************************************************************
*
* NextCLine - Get the next line
*
* Outputs:
* lDisp,lStart - disp to next command line
* C - set if error, else clear
*
****************************************************************
*
NextCLine start
debug NextCLine
using LineCommon
lb1 jsr Pause check for exit
bcs rts
jsr NextCommand next command on this line
bcc lb2
clc there was one - return
rts rts
lb2 jsr ClearLine dispose of old line
jsr NextLine next line in EXEC file
bcc lb1 (condition code set by NextLine)
rts
end
****************************************************************
*
* NextCommand - next command in the line buffer
*
* Inputs:
* ep - pointer to exec file line
* eDisp - disp in exec file
*
* Outputs:
* eDisp - points to next command line
* lp - pointes to expanded command line
* lLength - length of command line
* lDisp - set to 0
* C - set if command found
*
****************************************************************
*
NextCommand private