-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy.cmd.s
More file actions
340 lines (290 loc) · 7.88 KB
/
copy.cmd.s
File metadata and controls
340 lines (290 loc) · 7.88 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
;;; ============================================================
;;;
;;; COPY - Copy changing command for ProDOS-8
;;;
;;; Usage: COPY pathname1,pathname2
;;;
;;; Inspiration from COPY.ALL by Sandy Mossberg, Nibble 6/1987
;;;
;;; ============================================================
.include "apple2.inc"
.include "more_apple2.inc"
.include "prodos.inc"
;;; ============================================================
.org $4000
;; NOTE: Assumes XLEN is set by PATH
;; Point BI's parser at the command execution routine.
lda #<execute
sta XTRNADDR
lda #>execute
sta XTRNADDR+1
;; Mark command as external (zero).
lda #0
sta XCNUM
;; Set accepted parameter flags
lda #PBitsFlags::FN1 | PBitsFlags::FN2 ; Filenames
sta PBITS
lda #0 ; See below for why PBitsFlags::SD is not used
sta PBITS+1
clc ; Success (so far)
rts ; Return to BASIC.SYSTEM
;;; ============================================================
FN1REF := $D6
FN2REF := $D7
FN1INFO := $2EE
FN2BUF := $4200
DATABUF := $4600
DATALEN = $6000 - DATABUF
execute:
;; Fix relative paths. If PREFIX is not emptu, this is not needed.
;; If PREFIX is empty, then relative paths will fail. Specifying
;; PBitsFlags::SD is the usual fix for BI commands that take paths
;; but while it will make FN1 absolute if needed it applies the
;; same change to FN2 (nothing or prepending) without inspecting
;; FN2. So REL,/ABS becomes /PFX/REL,/PFX//ABS (oops!) and
;; /ABS,REL remains /ABS,REL (still relative!).
jsr GetPrefix
bcs rts1
lda VPATH1
ldx VPATH1+1
jsr FixPath
lda VPATH2
ldx VPATH2+1
jsr FixPath
;; Get FN1 info
lda #$A
sta SSGINFO
lda #GET_FILE_INFO
jsr GOSYSTEM
bcs rts1
;; Reject directory file
lda FIFILID
cmp #FT_DIR
bne :+
lda #BI_ERR_FILE_TYPE_MISMATCH
sec
rts1: rts
:
;; Save FN1 info
ldx #$11
: lda SSGINFO,x
sta FN1INFO,x
dex
bpl :-
;; Open FN1
lda HIMEM+1 ; Use BI's general purpose buffer (page aligned)
sta OSYSBUF+1
lda #OPEN
jsr GOSYSTEM
bcs rts1
lda OREFNUM
sta FN1REF
;; Copy FN2 to FN1
ptr1 := $06
ptr2 := $08
lda VPATH1
sta ptr1
lda VPATH1+1
sta ptr1+1
lda VPATH2
sta ptr2
lda VPATH2+1
sta ptr2+1
ldy #0
lda (ptr2),y
tay
: lda (ptr2),y
sta (ptr1),y
dey
bpl :-
;; Get FN2 info
lda #GET_FILE_INFO
jsr GOSYSTEM
bcs :+
lda #BI_ERR_DUPLICATE_FILE_NAME
err: pha
jsr CloseFiles
pla
sec
rts
: cmp #BI_ERR_PATH_NOT_FOUND
beq :+
cmp #BI_ERR_VOLUME_DIR_NOT_FOUND
bne err ; Otherwise - fail.
:
;; Create FN2
lda #$C3 ; Free access
sta CRACESS
ldx #3
: lda FN1INFO+4,x ; storage type, file type,
sta CRFKIND-3,x ; aux type, create date/time
lda FN1INFO+$E,x
sta CRFKIND+1,x
dex
bpl :-
lda #CREATE
jsr GOSYSTEM
bcs err
;; Open FN2
lda #<FN2BUF
sta OSYSBUF
lda #>FN2BUF
sta OSYSBUF+1
lda #OPEN
jsr GOSYSTEM
bcs err
lda OREFNUM
sta FN2REF
;; Read
read: lda FN1REF
sta RWREFNUM
lda #<DATABUF
sta RWDATA
lda #>DATABUF
sta RWDATA+1
lda #<DATALEN
sta RWCOUNT
lda #>DATALEN
sta RWCOUNT+1
lda #READ
jsr GOSYSTEM
bcc :+
cmp #BI_ERR_END_OF_DATA
beq finish
:
;; Write
lda FN2REF
sta RWREFNUM
lda #<DATABUF
sta RWDATA
lda #>DATABUF
sta RWDATA+1
lda RWTRANS
sta RWCOUNT
lda RWTRANS+1
sta RWCOUNT+1
lda #WRITE
jsr GOSYSTEM
bcc read
jmp err
finish: jsr CloseFiles
;; Apply FN1 info to FN2 to preserve modification date
ldx #$D - 3
: lda FN1INFO+3,x
sta SSGINFO+3,x
dex
bpl :-
lda #$7
sta SSGINFO
lda #SET_FILE_INFO
jsr GOSYSTEM
clc
rts
.proc CloseFiles
lda FN1REF
sta CFREFNUM
lda #CLOSE
jsr GOSYSTEM
lda FN2REF
sta CFREFNUM
lda #CLOSE
jsr GOSYSTEM
rts
.endproc
;;; Leave PREFIX at INBUF; infers it the same way as BI if empty.
;;; Returns with Carry set on failure.
.proc GetPrefix
;; Try fetching prefix
MLI_CALL GET_PREFIX, get_prefix_params
lda INBUF
bne done
;; Use BI's current slot and drive to construct unit number
lda DEFSLT ; xxxxxSSS
asl ; xxxxSSS0
asl ; xxxSSS00
asl ; xxSSS000
asl ; xSSS0000
asl ; SSS00000
ldx DEFDRV
cpx #2 ; C=0 if 1, C=1 if 2
ror ; DSSS0000
sta unit
;; Get volume name and convert to path
MLI_CALL ON_LINE, on_line_params
bcc :+
jsr BADCALL ; sets Carry
rts
: lda INBUF+1
and #$0F ; mask off name_len
tax
inx ; leading and trailing '/'
inx
stx INBUF
lda #'/'
sta INBUF+1 ; leading '/'
sta INBUF,x ; trailing '/'
done: clc
rts
get_prefix_params:
.byte 1
.addr INBUF
on_line_params:
.byte 2
unit: .byte 0
.addr INBUF+1 ; leave room to insert leading '/'
.endproc
;;; Fix path passed in A,X if it's relative. Uses prefix in INBUF
.proc FixPath
ptr := $06
ptr2 := $08
sta ptr
stx ptr+1
;; Already relative?
ldy #1
lda (ptr),y
cmp #'/'
beq done
;; Compute new length
ldy #0
lda (ptr),y
tay ; Y = current length
clc
adc INBUF ; add prefix length
pha ; stash for later
;; Shift path up to make room
lda INBUF
clc
adc ptr
sta ptr2
lda #0
adc ptr+1
sta ptr2+1
: lda (ptr),y
sta (ptr2),y
dey
bpl :-
;; Insert prefix
lda INBUF
tax
tay
: lda INBUF,x
sta (ptr),y
dex
dey
bne :-
;; Assign final length
pla
ldy #0
sta (ptr),y
done: rts
.endproc
.proc SkipSpaces
repeat: lda INBUF,x
cmp #' '|$80
beq :+
rts
: inx
jmp repeat
.endproc
cmd_length = .strlen("echo")
.assert * <= FN2BUF, error, "Too long"