Repository navigation
Expand file tree
/
Copy pathintvar.mac
More file actions
1341 lines (1266 loc) · 33.1 KB
/
Copy pathintvar.mac
File metadata and controls
1341 lines (1266 loc) · 33.1 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
name('intvar')
TITLE Nextor - COMMAND3.COM
SUBTTL Internal variables and variable functions
INCLUDE command.inc
;******************************************************************************
;
; Implementations of the internal variables (%_VAR%) and of the
; variable functions (%@FUN[parameters]%), both new in COMMAND2.COM 2.40.
;
; Based on COMMAND.COM 2.31, copyright (1986) IS Systems Ltd, and on
; the COMMAND2.COM 2.40/2.41 additions by Fokke Post, whose source code was
; released as freeware in 2003.
;
;******************************************************************************
; Handler conventions (see the dispatcher at RPC60/RPC100 in
; cli.mac): an IV_* routine implements the internal variable %_NAME% and is
; called with no arguments; an FN_* routine implements the variable
; function %@NAME[args]% and is called with HL -> the ASCIIZ argument text
; from between the brackets (nested %..% constructs already expanded). Both
; return DE -> the ASCIIZ result, usually built in ENVNAME or XBUF1; on an
; error DE is returned pointing to the empty string at CMDTEND (see FND90)
; so the construct expands to nothing. IX is preserved by the dispatcher;
; IY must be kept pointing at REDIRST.
; %_DIRBUFFER%: the directory currently stored in the directory
; buffer, which the CD - and CDD - commands return to. Case as per the
; LOWER environment item.
IV_DIRBUFFER:: LD DE,DIRBUF##
JR CHKLOW
; %_BOOT%: the boot drive letter, without a colon. Case as per the
; LOWER environment item.
; IVB10 is a common tail: it builds a one letter ASCIIZ string at
; ENVNAME from the 1-based drive number in A and falls into CHKLOW.
IV_BOOT:: LD A,(BOOTDRV)
IVB10: LD HL,ENVNAME##+1
LD (HL),#00
DEC HL
ADD A,"@" ; 1 => "A" etc.
LD (HL),A
EX DE,HL ; DE -> the string.
; Applies the LOWER environment item to a result string: when LOWER
; is set to ON, the ASCIIZ string at (DE) is lower-cased in place. Also
; used by the TAB filename completion in editor.mac; FN_LOWER enters the
; loop at CHK10 to lower-case unconditionally (string pointer pushed).
; On entry, DE points to the ASCIIZ string.
; On exit, DE is preserved; AF and HL are corrupted.
CHKLOW:: PUSH DE
LD HL,LOWER$##
CALL ON?##
POP DE
RET C ; Return unchanged if LOWER not ON.
PUSH DE
CHK10: LD A,(DE)
CALL MKLOWER##
LD (DE),A
INC DE
OR A
JR NZ,CHK10
POP DE
RET
; %_DISK%: the current drive letter, without a colon. Case as per
; the LOWER environment item.
IV_DISK:: LD A,(CURDRIVE##)
JR IVB10
; %_COLUMN%: the current cursor column, from the BIOS variable CSRX
; (1 = leftmost).
; IVC10 is a common tail: it returns the 8-bit value in A as a
; decimal ASCIIZ string at ENVNAME.
IV_COLUMN:: LD A,(CSRX)
IVC10: LD IX,ENVNAME##
PUSH IX
CALL D8##
POP DE
RET
; %_COLUMNS%: the screen width in columns, from the BIOS variable
; LINLEN.
IV_COLUMNS:: LD A,(LINLEN)
JR IVC10
; %_BG%: the current screen background colour number.
IV_BG:: LD A,(BAKCLR)
JR IVC10
; %_FG%: the current screen foreground colour number.
IV_FG:: LD A,(FORCLR)
JR IVC10
; %_ROW%: the current cursor row, from the BIOS variable CSRY
; (1 = top).
IV_ROW:: LD A,(CSRY)
JR IVC10
; %_ROWS%: the number of screen rows, from the BIOS variable CRTCNT.
IV_ROWS:: LD A,(CRTCNT)
JR IVC10
; %_ERROR%: the error code left by the previous command (0 = no
; error).
IV_ERROR:: LD A,(ABORTERR##)
JR IVC10
; %_DRIVEMAP%: the available drives as eight "0"/"1" digits, from
; the _LOGIN login vector; drive H: is the leftmost digit, drive A: the
; rightmost one.
IV_DRIVEMAP:: LD C,_LOGIN##
CALL BDOS ; L = login vector, b0 = A: etc.
LD C,L
JP FNP10 ; Render C as binary digits.
; %_SHELL%: the shell nesting level, "0" for the original COMMAND3.
; Whenever a transient command is run, the region EXITADDR to BUFFER1-1 is
; saved in the mapper segment below the save pointer kept at VAREND (see
; cli.mac); the level is recovered by dividing the amount of space in use
; by the size of one saved image.
IV_SHELL:: LD A,(SHRAMFL##) ; Without the mapper segment no
OR A ; saved images can be counted:
JR Z,IVSH0 ; report level 0.
XOR A
JR IVC10
IVSH0: CALL SETPG2## ; Map the mapper segment at page 2.
LD HL,#3FFF
LD DE,(VAREND+#8000) ; Saved state stack pointer.
RES 7,D ; Strip the page 2 address bit.
OR A
SBC HL,DE ; HL = bytes of saved state in use.
CALL RSTPG2##
LD DE,BUFFER1##-EXITADDR## ; Size of one saved image.
XOR A
IVS10: INC A
SBC HL,DE
JR NC,IVS10
DEC A ; A = number of saved images.
JR IVC10
; %_CPU%: the CPU type, "Z80" or "R800". On a Turbo-R the current
; mode is obtained from the BIOS GETCPU routine, on other machines it is
; always the Z80.
IV_CPU:: LD A,(MSXVER##)
LD DE,Z80$
CP 3 ; Turbo-R ?
RET C ; "Z80" if not.
LD IX,#0183 ; GETCPU.
CALL CALLSLOT## ; E = CPU mode.
LD A,E
LD DE,Z80$
OR A ; 0 => Z80 mode.
RET Z
LD DE,R800$
RET
Z80$: DEFB "Z80",0
R800$: DEFB "R800",0
; %_MSXVER%: the machine type as a string, from the MSX version byte
; (0 = MSX, 1 = MSX2, 2 = MSX2+, anything else = Turbo-R). The first
; generation MSX value is reported since 2.44.
IV_MSXVER:: LD A,(MSXVER##)
LD DE,MSX1$
OR A
RET Z
LD DE,MSX2$
DEC A
RET Z
LD DE,MSX2P$
DEC A
RET Z
LD DE,TURBOR$
RET
MSX1$: DEFB "MSX",0
MSX2$: DEFB "MSX-2",0
MSX2P$: DEFB "MSX-2+",0
TURBOR$: DEFB "Turbo-R",0
; %_CWD% and %_CWDS%: the current directory as "D:\PATH", %_CWDS%
; with a trailing backslash. Built at ENVNAME: "D:\" from the current
; drive, then the path from a guarded _GETCD call (a disk error truncates
; the value). For %_CWDS% the result is parsed to locate its end and a "\"
; is appended unless already there (root directory). Case as per the LOWER
; environment item.
IV_CWDS:: SCF ; Cy => append a trailing "\".
IV_CWD:: EX AF,AF' ; Save it.
CALL SETABT
LD HL,ENVNAME##
LD A,(CURDRIVE##)
LD B,A ; B = drive for _GETCD.
ADD A,"@" ; 1 => "A" etc.
LD (HL),A
INC HL
LD (HL),":"
INC HL
LD (HL),"\"
INC HL
LD (HL),#00
EX DE,HL ; Path goes after the "D:\".
LD C,_GETCD##
CALL BDOS
OR A
LD DE,ENVNAME##
PUSH DE
JR NZ,IVC20 ; Error => truncate the value.
EX AF,AF'
JR NC,IVC30 ; Plain %_CWD% is complete now.
LD BC,_PARSE##
CALL BDOS ; DE -> end of the string.
DEC DE
LD A,(DE)
CP "\" ; Root directory ?
INC DE
JR Z,IVC30
LD A,"\"
LD (DE),A
IVC20: XOR A
INC DE
LD (DE),A
IVC30: CALL RSTABT
POP DE
JP CHKLOW
; %_CWP% and %_CWPS%: as %_CWD%/%_CWDS% but without the leading
; "D:", i.e. "\PATH": the first two characters of the %_CWD% result are
; skipped (unless it was truncated by an error).
IV_CWPS:: SCF
IV_CWP:: CALL IV_CWD
INC DE
LD A,(DE)
OR A
RET Z ; Truncated by an error.
INC DE
RET
; %_DATE% and %_DATEF%: the current date, in the format set by the
; DATE environment item (DFORM); %_DATEF% with a four digit year. The
; _GDATE result is packed into a directory format date word at ENVNAME+0/1
; and rendered by DDATE from ENVNAME+2 on; DE returns ENVNAME+3, past the
; leading space that DDATE emits.
IV_DATEF:: SCF ; Cy => print the century.
IV_DATE:: EX AF,AF' ; Save it.
CALL DFORM## ; Set DATEFORM from the environment.
LD C,_GDATE##
CALL BDOS ; HL = year, D = month, E = day.
LD BC,#F844
ADD HL,BC ; L = year - 1980.
LD A,D ; Pack year/month/day into a
ADD A,A ; directory format date word:
ADD A,A ; b0-4 day, b5-8 month,
ADD A,A ; b9-15 year-1980.
ADD A,A
ADD A,A
RL L
OR E
LD D,L
LD HL,ENVNAME##+1
LD (HL),D ; MSB.
DEC HL
LD (HL),A ; LSB.
EX AF,AF'
SBC A,A
LD C,A ; C = FF => DDATE prints century.
LD IX,ENVNAME##+2 ; Text goes here.
PUSH IX
CALL DDATE##
POP DE
INC DE ; Skip the leading space.
RET
; %_DOSVER%: the COMMAND3.COM version, as a fixed string.
IV_DOSVER:: LD DE,DOSVER$
RET
DOSVER$: DEFB "3.0",0
; %_DOW%: the current day of the week as a full English name: _GDATE
; returns the day of the week in A (0 = Sunday); messages #17-#1D are the
; stems "Sun" to "Satur", to which message #1E ("day") is appended.
IV_DOW:: LD C,_GDATE##
CALL BDOS ; A = day of week, 0 = Sunday.
ADD A,#17 ; Messages #17-#1D = "Sun".."Satur".
CALL GETMSG##
EX DE,HL
LD DE,ENVNAME##
CALL NCOPY##
DEC DE ; Back onto the null.
PUSH DE
LD A,#1E ; Message #1E = "day".
CALL GETMSG##
EX DE,HL
POP DE
CALL NCOPY## ; Append it.
LD DE,ENVNAME##
RET
; %_TIME%: the current time as "HH:MM:SS", in 12 or 24 hour format
; as per the TIME environment item (TFORM); in 12 hour format an "a" or "p"
; is appended. Hours and minutes are packed into a directory format time
; word at ENVNAME+0/1 and rendered by DTIME, then the seconds from _GTIME
; are appended by hand. DE returns ENVNAME+3, past DTIME's leading space.
IV_TIME:: CALL TFORM## ; Set TIMEFORM from the environment.
LD C,_GTIME##
CALL BDOS ; H = hours, L = mins, D = secs.
PUSH DE ; Save seconds.
LD A,L ; Pack hours/minutes into a
ADD A,A ; directory format time word:
ADD A,A ; b5-10 minutes, b11-15 hours.
LD B,#03
IVT10: ADD A,A
RL H
DJNZ IVT10
LD C,H
LD HL,ENVNAME##
LD (HL),A ; LSB.
INC HL
LD (HL),C ; MSB.
LD IX,ENVNAME##+2 ; Text goes here.
CALL DTIME##
POP BC ; B = seconds.
PUSH AF ; Save trailing "a"/"p"/space.
LD (IX),":"
INC IX
LD A,B
LD D,#FF ; Always two digits.
CALL PRFIG2##
POP AF
CP " "
JR Z,IVT20 ; No am/pm indicator.
LD (IX),A
INC IX
IVT20: LD (IX),#00
LD DE,ENVNAME##+3
RET
; SETABT and RSTABT bracket a "guarded" BDOS call. SETABT installs
; DSKERR as the disk error handler and ABRT as the abort routine (both in
; cli.mac), so that a disk error or abort condition during the call makes
; it return to its caller with A = #FF instead of aborting COMMAND3. Both
; routines preserve all the main registers.
SETABT: PUSH BC
PUSH DE
PUSH HL
PUSH AF
LD DE,DSKERR##
LD C,_DEFER##
CALL BDOS
LD DE,ABRT##
LD C,_DEFAB##
CALL BDOS
POP AF
POP HL
POP DE
POP BC
RET
; Restores the default disk error handling and the normal CLI_ABORT
; abort routine; see SETABT above.
RSTABT: PUSH BC
PUSH DE
PUSH HL
PUSH AF
LD DE,#00
LD C,_DEFER##
CALL BDOS
LD DE,CLI_ABORT##
LD C,_DEFAB##
CALL BDOS
POP AF
POP HL
POP DE
POP BC
RET
; %_FNEXT%: the name of the next file matching the search started by
; %@FFIRST[filespec]%, continued with _FNEXT on the same FIB (FIB3). The
; file's size and count are added to the @FILETOTAL and %_FILECOUNT%
; accumulators. A null string is returned when no more files match.
IV_FNEXT:: CALL SETABT
LD IX,FIB3## ; The FIB set up by @FFIRST.
LD C,_FNEXT##
CALL BDOS
CALL RSTABT
OR A
JP NZ,FND90 ; No more files => null string.
LD HL,FIB3##+#15 ; Add the 32-bit file size to
LD DE,FILETOT1## ; the running total.
CALL ADD32##
LD HL,(FILECNT##)
INC HL
LD (FILECNT##),HL
LD DE,FIB3##+1 ; Return the ASCIIZ filename.
RET
; %_FILECOUNT%: the number of files found so far by the
; @FFIRST/_FNEXT walk, in decimal.
IV_FILECOUNT:: LD HL,(FILECNT##)
LD IX,ENVNAME##
PUSH IX
CALL D16##
POP DE
RET
SUBTTL Variable functions
; %@ALIAS[name]%: the value of the given alias, or a null string if
; it is not defined; GETALIAS (io.mac) copies it to ENVNAME.
FN_ALIAS:: LD DE,ENVNAME##
CALL GETALIAS##
RET
; %@ASCII[c]%: the ASCII value of the first argument character, in
; decimal.
FN_ASCII:: LD A,(HL)
LD IX,ENVNAME##
PUSH IX
CALL D8##
POP DE
RET
; %@ATTRIB[filespec,attribs]%: "1" if the file's attribute byte is
; exactly equal to the given combination (both set and reset bits must
; match), else "0". The letters R, H, S, D and A are OR-ed together via
; ATTRTAB, "N" (normal) resets the combination to no bits at all, and
; unknown letters are ignored. An inaccessible file never matches.
FN_ATTRIB:: EX DE,HL
PUSH DE
LD BC,_PARSE##
CALL BDOS
EX DE,HL ; HL -> filespec terminator.
XOR A
LD (HL),A ; Cut the filespec there.
LD (ATTRIB1##),A ; No attribute bits yet.
FNA10: INC HL
CALL GETCH## ; Next attribute letter.
JR Z,FNA40 ; End of the argument.
CALL UC##
CP "N"
LD B,A
LD A,#00
JR Z,FNA30 ; "N" => reset to no bits.
LD DE,ATTRTAB
FNA20: LD A,(DE)
OR A
JR Z,FNA10 ; Unknown letter, ignore it.
CP B
INC DE
LD A,(DE)
INC DE
JR NZ,FNA20
LD B,A ; B = the attribute bit.
LD A,(ATTRIB1##)
OR B
FNA30: LD (ATTRIB1##),A
JR FNA10
FNA40: CALL SETABT
POP DE
LD C,_ATTR##
XOR A ; Get attributes; L = result.
CALL BDOS
OR A
JR Z,FNA50
LD L,#FF ; Error => match nothing.
FNA50: CALL RSTABT
LD DE,ENVNAME##+1
LD (DE),A ; Terminator (A = 0 if no error).
DEC DE
LD A,"0"
LD (DE),A
LD A,(ATTRIB1##)
CP L ; Exact match of all bits ?
RET NZ
LD A,"1"
LD (DE),A
RET
; Attribute letter/attribute bit pairs for @ATTRIB, ended by an
; explicit zero byte: the FNA20 scan then ignores an unknown attribute
; letter instead of matching it against the code bytes that follow.
ATTRTAB: DEFB "R",#01
DEFB "H",#02
DEFB "S",#04
DEFB "D",#10
DEFB "A",#20
DEFB #00
; %@CHAR[n]%: a one character string containing the character with
; ASCII value n. A null string if n is not a valid number below 256.
FN_CHAR:: CALL GET8##
JP NZ,FND90
LD HL,ENVNAME##+1
LD (HL),#00
DEC HL
LD (HL),E
EX DE,HL
RET
; %@STRING[n,string]%: a string made of n copies of the first
; character of string, with n between 1 and 122 (so that the result fits
; in the expansion buffers). A null string if n is out of range, no comma
; follows it, or string is empty. New in 2.44.
FN_STRING:: CALL GET8## ; E = repeat count.
JP NZ,FND90
LD A,(HL)
CP "," ; A comma must follow the count.
JP NZ,FND90
INC HL
LD A,E
DEC A ; Reject 0 and anything above 122.
CP 122
JP NC,FND90
PUSH DE
CALL PRSSTR ; Copy the string argument to XBUF1
POP BC ; (unquoting it), C = repeat count.
LD A,(XBUF1##)
OR A
JP Z,FND90 ; Null result if string is empty.
LD B,C
LD HL,ENVNAME##
PUSH HL
FNS10: LD (HL),A ; Store n copies of the first character
INC HL ; of the string, plus the terminator.
DJNZ FNS10
LD (HL),#00
POP DE
RET
; %@HEX[n]%: the value of n as four hexadecimal digits (two per
; byte, via MKBYTE). A null string if n is not a valid 16-bit number.
FN_HEX:: CALL GET16##
JP NZ,FND90
LD HL,ENVNAME##
PUSH HL
LD A,D
CALL MKBYTE##
LD A,E
CALL MKBYTE##
LD (HL),#00
POP DE
RET
; FND50-FND80 are shared by the disk and file size functions: the
; 32-bit byte count in HL:DE is divided by 1024 once for "K" or twice for
; "M" (unit letter in AF') and rendered in decimal at ENVNAME.
FND50: LD (VALUEL##),DE ; Store the 32-bit byte count.
LD (VALUEH##),HL
EX AF,AF' ; Get the unit letter.
CP "B"
JR Z,FND80 ; Bytes.
LD B,10
FND60: SRL H ; Divide by 1024 => kilobytes.
RR L
RR D
RR E
DJNZ FND60
LD (VALUEL##),DE
LD (VALUEH##),HL
CP "K"
JR Z,FND80 ; Kilobytes.
LD B,10
FND70: SRL H ; Divide by 1024 => megabytes.
RR L
RR D
RR E
DJNZ FND70
LD (VALUEL##),DE
LD (VALUEH##),HL
FND80: LD DE,VALUEL##
XOR A ; No leading characters.
LD IX,ENVNAME## ; Render in decimal here.
PUSH IX
CALL D32##
POP DE
XOR A
RET
; Shared error exit: return a null string (CMDTEND is a zero byte,
; see tables.mac).
FND90: LD DE,CMDTEND##
RET
; %@FILETOTAL[b|k|m]%: the accumulated size of the files returned by
; the @FFIRST/_FNEXT walk, scaled by the unit letter.
FN_FILETOTAL:: LD A,(HL)
CALL MKUPPER##
EX AF,AF' ; Unit letter for FND50.
LD DE,(FILETOT1##)
LD HL,(FILETOT2##)
JR FND50
; Parses the "d:,b|k|m" argument of the @DISK* functions.
; On entry, HL points to the argument; the drive letter may be "@" for the
; current drive and must be followed by ":".
; On exit, Cy set => bad argument. Otherwise E = drive (0 = current,
; 1 = A:, the _DSPACE convention) and AF' holds the
; upper-cased unit letter.
GSPARSE: LD A,(HL)
CALL UC##
SUB "@" ; "@" => 0 = current drive.
RET C
CP 9 ; Only drives up to H: are valid.
CCF
RET C
LD E,A ; E = drive for _DSPACE.
INC HL
LD A,(HL)
CP ":"
SCF
RET NZ ; The colon is mandatory.
INC HL
CALL GETCH## ; Skip the separator.
SCF
RET Z ; Unit letter is missing.
CALL UC##
CP "B"
JR Z,GSP10
CP "K"
JR Z,GSP10
CP "M"
SCF
RET NZ ; Not a valid unit letter.
GSP10: EX AF,AF' ; Save the unit letter.
OR A ; NC => parsed successfully.
RET
; Gets the free (A = 0) or total (A = 1) space on drive E with a
; guarded _DSPACE call, which reports a 32-bit kilobyte count in HL:DE
; plus up to 512 extra bytes in BC when the allocation unit is not a
; whole number of kilobytes. IY is reloaded with REDIRST.
; On exit, Cy set => bad drive. Otherwise HL:DE = the space as a 32-bit
; byte count.
GSCALL: CALL SETABT
LD C,_DSPACE##
CALL BDOS
CALL RSTABT
LD IY,REDIRST##
OR A
SCF
RET NZ ; Error => Cy.
LD A,#0A ; HL:DE = KB * 1024 + the extra
GSC10: SLA E ; bytes in BC = the byte count.
RL D
ADC HL,HL
DEC A
JR NZ,GSC10
EX DE,HL
ADD HL,BC
EX DE,HL
RET NC
INC HL
OR A ; NC (A is 0 here).
RET
; %@DISKTOTAL[d:,b|k|m]%: the total size of a drive. The total
; reported by _DSPACE is always real: never affected by the reduced/zero
; allocation information mode, and correct beyond 32MB on FAT16.
FN_DISKTOTAL:: CALL GSPARSE
JP C,FND90 ; Bad argument => null string.
LD A,#01
CALL GSCALL
JP C,FND90
JP FND50
; %@DISKFREE[d:,b|k|m]%: the free space on a drive ("@:" = current
; drive) in bytes, kilobytes or megabytes. Under the zero allocation
; information mode this is 0, as reported by the kernel.
FN_DISKFREE:: CALL GSPARSE
JP C,FND90
XOR A
CALL GSCALL
JP C,FND90
JP FND50
; %@DISKUSED[d:,b|k|m]%: the space in use on a drive: total minus
; free bytes.
FN_DISKUSED:: CALL GSPARSE
JP C,FND90
PUSH DE ; Save the drive number.
LD A,#01
CALL GSCALL ; HL:DE = total bytes.
POP BC ; C = drive number.
JP C,FND90
PUSH HL
PUSH DE ; Stack: the total byte count.
LD E,C
XOR A
CALL GSCALL ; HL:DE = free bytes.
POP BC ; BC = total low word.
JR NC,FNDU10
POP HL ; Bad drive now (?): clean up the
JP FND90 ; stack and return a null string.
FNDU10: LD A,C ; Used = total - free: first the
SUB E ; low word into DE, keeping the
LD E,A ; borrow...
LD A,B
SBC A,D
LD D,A
EX (SP),HL ; HL = total high; stack: free high.
POP BC ; BC = free high word.
SBC HL,BC ; ...then the high word.
JP FND50
; %@FILEDATE[filespec]% and %@FILETIME[filespec]%: the date or time
; of last modification of a file (one shared body; Cy in AF' selects the
; date). The guarded _FTIME call returns DE = time and HL = date in
; directory format; the selected word is stored at ENVNAME+0/1 and rendered
; from ENVNAME+2 on by DTIME (seconds appended by hand, "a"/"p" in 12 hour
; format) or by DDATE (two digit year, format as per the DATE environment
; item). A null string is returned when the filespec is followed by garbage
; or the file is not accessible.
FN_FILEDATE:: SCF ; Cy => return the date.
FN_FILETIME:: EX AF,AF' ; Save it.
EX DE,HL
PUSH DE
CALL DFORM## ; Set the date and time formats
CALL TFORM## ; from the environment.
POP DE
PUSH DE
LD BC,_PARSE##
CALL BDOS
EX DE,HL ; HL -> filespec terminator.
POP DE
PUSH HL
CALL GETCH##
POP HL
JP NZ,FND90 ; Garbage after the filespec.
LD (HL),A ; Terminate the filespec.
CALL SETABT
LD C,_FTIME##
CALL BDOS ; DE = time, HL = date.
CALL RSTABT
OR A
JP NZ,FND90
EX AF,AF'
JR NC,FNF10
EX DE,HL ; Date wanted => DE = date.
FNF10: EX AF,AF'
LD HL,ENVNAME##
LD (HL),E ; Store the word for DTIME/DDATE.
INC HL
LD (HL),D
LD IX,ENVNAME##+2 ; Text goes here.
EX AF,AF'
JR C,FNF40
PUSH DE
CALL DTIME##
POP BC
PUSH AF ; Save trailing "a"/"p"/space.
LD (IX),":"
INC IX
LD A,C
AND %00011111 ; b0-4 of the word = seconds/2,
ADD A,A ; so times two.
LD D,#FF ; Always two digits.
CALL PRFIG2##
POP AF
CP " "
JR Z,FNF20 ; No am/pm indicator.
LD (IX),A
INC IX
FNF20: LD (IX),#00
FNF30: LD DE,ENVNAME##+3 ; Past DTIME/DDATE's lead space.
RET
FNF40: DEC HL ; HL -> the date word (LSB).
LD C,#00 ; Two digit year.
CALL DDATE##
JR FNF30
; %@FILESIZE[filespec,b|k|m]%: the size of a file, scaled by the
; unit letter. The filespec must not be ambiguous nor "." or ".."; the file
; is located with a guarded _FFIRST (attributes #16, so hidden and system
; files and directories are found too) and its 32-bit size is taken from
; the FIB. A null string is returned on any error.
FN_FILESIZE:: EX DE,HL
PUSH DE
LD BC,_PARSE##
CALL BDOS
EX DE,HL ; HL -> filespec terminator.
XOR A
LD (HL),A ; Cut the filespec there.
POP DE
LD A,B
AND %01100000 ; Reject ambiguous, "." and "..".
JP NZ,FND90
INC HL
CALL GETCH##
JP Z,FND90 ; Unit letter is missing.
CALL UC##
CP "B"
JR Z,FNF50
CP "K"
JR Z,FNF50
CP "M"
JP NZ,FND90 ; Not a valid unit letter.
FNF50: EX AF,AF' ; Unit letter for FND50.
CALL SETABT
LD C,_FFIRST##
LD B,#16 ; Include hidden/system/dirs.
LD IX,FIB1##
CALL BDOS
CALL RSTABT
OR A
JR Z,FNF60
JP FND90 ; Not found => null string.
FNF60: LD HL,FIB1##+#15 ; 32-bit file size from the FIB.
LD E,(HL)
INC HL
LD D,(HL)
INC HL
LD A,(HL)
INC HL
LD H,(HL)
LD L,A
JP FND50 ; Scale and render it.
; %@FILEATTR[filespec]%: the attributes of a file or directory as
; letters from ATTRLST$, scanning the guarded _ATTR result from bit 7
; down; only the letters of set bits appear, so the order is A D V S H R.
; A null string is returned on error or if garbage follows the filespec.
FN_FILEATTR:: EX DE,HL
PUSH DE
LD BC,_PARSE##
CALL BDOS
EX DE,HL ; HL -> filespec terminator.
POP DE
PUSH HL
CALL GETCH##
POP HL
JP NZ,FND90 ; Garbage after the filespec.
LD (HL),A ; Terminate the filespec.
CALL SETABT
LD C,_ATTR##
CALL BDOS ; A = 0: get; L = attributes.
CALL RSTABT
OR A
JP NZ,FND90
LD A,L
LD DE,ENVNAME##
PUSH DE
LD HL,ATTRLST$
LD B,#08 ; 8 attribute bits.
FNF70: RLA ; Attribute bit set ?
JR NC,FNF80
PUSH AF
LD A,(HL) ; Yes, emit its letter.
LD (DE),A
POP AF
INC DE
FNF80: INC HL
DJNZ FNF70
XOR A
LD (DE),A
POP DE
RET
; Attribute letters for @FILEATTR, bit 7 first; bits 7 and 6 are
; not used.
ATTRLST$: DEFB "--ADVSHR"
; %@LABEL[d:]%: the volume label of the drive (the current drive if
; omitted), found with a guarded volume name _FFIRST. A null string is
; returned for an invalid drive, when there is no label, or on a disk
; error (#FF from the guarded call).
FN_LABEL:: EX DE,HL
CALL SETABT
LD BC,#0840 ; _FFIRST, volume name bit set.
LD IX,FIB1##
CALL BDOS
CALL RSTABT
CP .IDRV##
JP Z,FND90
CP .NOFIL##
JP Z,FND90
CP #FF
JP Z,FND90
LD DE,FIB1##+1 ; Return the label from the FIB.
RET
; %@READY[d:]%: "1" if the drive is ready, "0" if not. Probed with a
; guarded volume name _FFIRST: success or "no files" mean the disk could
; be read, #FF (disk error) means not ready, and any other error (e.g. an
; invalid drive) yields a null string.
FN_READY:: PUSH HL
CALL PARSDR##
POP DE
LD B,#00 ; Default: return a null string.
JR NZ,FNR10 ; Not a valid drive letter.
CALL SETABT
LD BC,#0840 ; _FFIRST, volume name bit set.
LD IX,FIB1##
CALL BDOS
CALL RSTABT
LD B,"1"
OR A
JR Z,FNR10 ; Found a label => ready.
CP .NOFIL##
JR Z,FNR10 ; No label but readable => ready.
LD B,"0"
CP #FF
JR Z,FNR10 ; Disk error => not ready.
LD B,#00
FNR10: LD HL,ENVNAME##
PUSH HL
LD (HL),B
INC HL
LD (HL),#00
POP DE
RET
; %@EXT[filespec]%: the filename extension, without the dot. The
; last item of the filespec is expanded by _PFILE into the 11 byte
; blank-padded upper case form at ENVNAME; DE returns the 3 extension
; characters, null-terminated by _PFILE's A = 0 result.
FN_EXT:: EX DE,HL
LD BC,_PARSE##
CALL BDOS
EX DE,HL ; DE -> last item of the filespec.
LD HL,ENVNAME##
LD C,_PFILE##
CALL BDOS ; Expand it at ENVNAME.
LD (ENVNAME##+11),A ; A = 0: terminator.
LD DE,ENVNAME##+8 ; The 3 extension characters.
RET
; %@LEN[string]%: the length of the argument string, in decimal.
FN_LEN:: LD B,#FF
FNL10: LD A,(HL)
INC B
INC HL
OR A
JR NZ,FNL10
LD A,B ; B = length.
JP IVC10
; %@INSTR[n,string1,string2]%: the position of string2 inside
; string1, 1-based, "0" if it is not found; the search starts at position
; n (n and its comma may be omitted). String1 may be quoted to protect
; commas and is copied to XBUF1 by PRSSTR; the comparison (CHKEQ, cmd.mac)
; is case sensitive. A null string results if string2 is missing or n lies
; beyond the end of string1.
FN_INSTR:: LD E,#00
CALL GETDIG##
JR C,FNI10 ; No digit => n omitted.
CALL GET8## ; E = n.
JP NZ,FND90
FNI10: PUSH DE
CALL GETCH##
CALL PRSSTR ; String1 to XBUF1.
POP DE
JP Z,FND90 ; No comma => string2 missing.
CALL STRIP## ; HL -> string2.
PUSH HL
LD B,E ; B = start position.
LD E,#00 ; E = position counter.
LD HL,XBUF1##-1
DEC B
INC B
JR Z,FNI30 ; n = 0 => start at position 1.
FNI20: INC HL ; Advance to position n;
INC E
LD A,(HL)
OR A
JR Z,FNI60 ; past the end => null string.
DJNZ FNI20
DEC HL
DEC E
FNI30: LD B,H
LD C,L
POP HL ; HL -> string2.
FNI40: INC BC ; Try a match at each position.
INC E
LD A,(BC)
OR A
JR Z,FNI50 ; End of string1 => not found.
PUSH BC
PUSH HL
CALL CHKEQ## ; String2 matches here ?
POP HL
POP BC
JR NZ,FNI40
LD A,E ; Found: the 1-based position.
JP IVC10
FNI50: XOR A ; Not found => "0".
JP IVC10
FNI60: POP HL
JP FND90
; Parses one string argument for the functions that take a
; "string," parameter: copies characters from (HL) up to a comma or the
; end of the line into XBUF1 as an ASCIIZ string. Quote characters (" or
; ') make enclosed commas literal (RDCHAR in cli.mac keeps the quoting
; state in C).
; On exit, Z => no comma was found (this was the last argument);
; NZ => a comma was found and HL points just past it.
PRSSTR: EX DE,HL