Subversion Repositories Spectranet

[/] [trunk/] [modules/] [basext/] [commands.asm] - Blame information for rev 288

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 221 winston
;The MIT License
2
;
3
;Copyright (c) 2009 Dylan Smith
4
;
5
;Permission is hereby granted, free of charge, to any person obtaining a copy
6
;of this software and associated documentation files (the "Software"), to deal
7
;in the Software without restriction, including without limitation the rights
8
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
;copies of the Software, and to permit persons to whom the Software is
10
;furnished to do so, subject to the following conditions:
11
;
12
;The above copyright notice and this permission notice shall be included in
13
;all copies or substantial portions of the Software.
14
;
15
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
;THE SOFTWARE.
22
 
23
; TNFS BASIC extensions
24
        include "../../rom/zxromdefs.asm"
25
 
26
;---------------------------------------------------------------------------
27
; F_tbas_mount
28
; BASIC interpreter for "mount"
29 233 winston
; Syntax: %mount mountpoint, "url"
30 221 winston
F_tbas_mount
31
        ; Syntax and runtime
32
        rst CALLBAS
33 233 winston
        defw ZX_EXPT1_NUM
34 221 winston
        cp ','                          ; comma
35
        jp nz, PARSE_ERROR
36
        rst CALLBAS
37
        defw ZX_NEXT_CHAR
38
 
39
        rst CALLBAS
40 238 winston
        defw ZX_EXPT_EXP                ; string parameter - an URL
41 221 winston
 
42
        call STATEMENT_END              ; followed by statement end
43
 
44
        ; -------- Runtime only ---------
45 233 winston
        ld hl, INTERPWKSPC              ; clear space for the
46
        ld de, INTERPWKSPC+1            ; mount argument structure
47
        ld bc, 9
48
        ld (hl), 0
49 221 winston
        ldir
50
 
51
        rst CALLBAS
52
        defw ZX_STK_FETCH               ; path string
53 233 winston
        ld hl, INTERPWKSPC+10
54 221 winston
        call F_basstrcpy                ; copy string from BASIC
55 233 winston
        ld ix, INTERPWKSPC              ; where to place the mount struct
56
        ld hl, INTERPWKSPC+10           ; location of the string to parse
57
        call F_parseurl
58
        jr c, .badurl
59
 
60
        rst CALLBAS                     ; fetch the mount point
61
        defw ZX_FIND_INT2
62 221 winston
 
63
.mount
64 233 winston
        ld a, c                         ; mount point in BC
65 221 winston
        call MOUNT
66
        jp c, J_tbas_error              ; display the error message
67
 
68
        jp EXIT_SUCCESS
69
 
70 233 winston
.badurl
71
        ld a, EBADURL
72
        jp J_tbas_error
73
 
74 221 winston
        ; Copy a BASIC string to a C string.
75
        ; BASIC string in DE, C string (dest) in HL
76
F_basstrcpy
77
        ld a, b                         ; end of string?
78
        or c
79
        jr z, .terminate
80
        ld a, (de)
81
        ld (hl), a
82
        inc hl
83
        inc de
84
        dec bc
85
        jr F_basstrcpy
86
.terminate
87
        xor a                           ; put the null on the end
88
        ld (hl), a
89
        inc hl
90
        ret
91
 
92
;----------------------------------------------------------------------------
93
; F_tbas_umount
94
; Umount the mounted filesystem.
95
F_tbas_umount
96 238 winston
        rst CALLBAS
97
        defw ZX_EXPT1_NUM               ; 1 parameter - mount point number
98
        call STATEMENT_END
99 221 winston
 
100
        ;--------- runtime ---------
101 238 winston
        rst CALLBAS
102
        defw ZX_FIND_INT2
103
        ld a, c                         ; mount point is in BC
104 221 winston
        call UMOUNT
105
        jp c, J_tbas_error
106
        jp EXIT_SUCCESS
107
 
108
;----------------------------------------------------------------------------
109
; F_tbas_chdir
110
; Handle changing directory
111
F_tbas_chdir
112
        rst CALLBAS
113
        defw ZX_EXPT_EXP                ; expect a string expression
114
        call STATEMENT_END
115
 
116
        ;-------- runtime --------
117
        rst CALLBAS
118
        defw ZX_STK_FETCH               ; get the string
119
        ld hl, INTERPWKSPC
120
        call F_basstrcpy                ; convert to a C string
121
        ld hl, INTERPWKSPC
122
        call CHDIR
123
        jp c, J_tbas_error              ; carry set = error
124
        jp EXIT_SUCCESS
125
 
126
;---------------------------------------------------------------------------
127
; F_tbas_aload: Loads an arbitary file from the TNFS filesystem.
128
; The syntax is %aload "filename" CODE address. This allows the user
129
; to load an arbitrary file with no ZX formatting into memory.
130
F_tbas_aload
131
        rst CALLBAS
132
        defw ZX_EXPT_EXP                ; expect a string expression
133
        cp TOKEN_CODE                   ; expect CODE
134
        jp nz, PARSE_ERROR
135
        rst CALLBAS                     ; fetch the next bit
136
        defw ZX_NEXT_CHAR               ; which must be a number
137
        rst CALLBAS
138
        defw ZX_EXPT1_NUM
139
        call STATEMENT_END              ; and then the end of statement
140
 
141
        ;------- runtime -------
142
        rst CALLBAS
143
        defw ZX_FIND_INT2               ; find the 16 bit int
144
        push bc                         ; save it
145
        rst CALLBAS
146
        defw ZX_STK_FETCH               ; get the filename
147
        ld hl, INTERPWKSPC              ; save it in workspace...
148
        call F_basstrcpy
149
 
150
        ; Now read the file into memory
151
        ld hl, INTERPWKSPC
152
        pop de                          ; retrieve address
153
        call F_tbas_readrawfile
154
        jp c, J_tbas_error
155
        jp EXIT_SUCCESS
156
 
157
;----------------------------------------------------------------------------
158
; F_tbas_load: Loads a ZX file (BASIC, CODE etc.)
159
; The syntax is as for ZX BASIC LOAD, except %load.
160
; TODO: CODE et al.
161
F_tbas_load
162
        rst CALLBAS
163
        defw ZX_EXPT_EXP                ; expect a string expression
164
        cp TOKEN_CODE                   ; Check for CODE...
165
        jr z, .loadcode
166
        call STATEMENT_END              ; If not, statment end for BASIC.
167
 
168
        ;------ runtime for BASIC ------
169
        xor a                           ; type 0x00 is BASIC
170
.loader
171
        push af                         ; save type
172
        rst CALLBAS
173
        defw ZX_STK_FETCH               ; fetch the filename
174
        ld hl, INTERPWKSPC
175
        call F_basstrcpy                ; copy + convert to C string
176
 
177
        ; Now call the loader routine with the filename in HL
178
        ld hl, INTERPWKSPC
179
        pop af                          ; get type id
180
        call F_tbas_loader
181
        jp c, J_tbas_error
182
        jp EXIT_SUCCESS
183
 
184
.loadcode
185
        ; TODO - code to a specific address.
186
        rst CALLBAS
187
        defw ZX_NEXT_CHAR
188
        call STATEMENT_END
189
 
190
        ;------ runtime for CODE with no addr -------
191
        ld a, 3                         ; type=CODE
192
        jr .loader                      ; get the filename then load.
193
 
194
;----------------------------------------------------------------------------
195
; F_tbas_save: Save a ZX file (BASIC, CODE etc.)
196
; The syntax is as for ZX BASIC SAVE.
197
; TODO: CODE et al.
198
F_tbas_save
199
        rst CALLBAS
200
        defw ZX_EXPT_EXP                ; fetch the file name
201
        cp TOKEN_CODE                   ; for CODE
202
        jr z, .savecode
203
        cp TOKEN_SCREEN                 ; for SCREEN$
204
        jr z, .savescreen
205 248 winston
        cp TOKEN_LINE                   ; then for LINE
206
        jr z, .savebasline
207 221 winston
 
208
        call STATEMENT_END              ; a basic BASIC save.
209
 
210
        ;------- runtime for simple BASIC save -------
211 250 winston
        rst CALLBAS
212
        defw ZX_STK_FETCH               ; Fetch the file name
213
        push de                         ; save the filename
214
        push bc
215 248 winston
        xor a
216
        call F_tbas_mktapheader
217
        ld hl, 0xFFFF                   ; set param1 to >32767
218
        ld (INTERPWKSPC+OFFSET_PARAM1), hl
219
        jr .makebasicblock
220 221 winston
 
221
        ; Deal with SAVE "filename" CODE
222
.savecode
223
        rst CALLBAS
224
        defw ZX_NEXT2_NUM               ; check for 2 numbers
225
        call STATEMENT_END              ; then end of command.
226
 
227
        ; Runtime
228
        rst CALLBAS
229
        defw ZX_FIND_INT2               ; Get the length
230
        push bc                         ; into BC and save it
231
        rst CALLBAS
232
        defw ZX_FIND_INT2               ; and the start
233
        push bc                         ; and save that, too
234
.savecodemain
235
        rst CALLBAS
236
        defw ZX_STK_FETCH               ; and get the filename
237
        ld a, 3                         ; type = 3 - CODE
238
        call F_tbas_mktapheader         ; create the header template
239
        pop hl                          ; retrieve the start address
240
        ld (INTERPWKSPC+OFFSET_PARAM1), hl      ; and put it in the header
241
        pop hl                          ; and the length
242
        ld (INTERPWKSPC+OFFSET_LENGTH), hl      ; and put it in the header
243
        call F_tbas_writefile           ; finally write it out
244
        jp c, J_tbas_error
245
        jp EXIT_SUCCESS
246
 
247
.savescreen
248
        rst CALLBAS
249
        defw ZX_NEXT_CHAR               ; advance to the end of the line
250
        call STATEMENT_END
251
 
252
        ; Runtime
253
        ld hl, 6912                     ; Put the length of a SCREEN$
254
        push hl                         ; on the stack
255
        ld hl, 16384                    ; followed by the start address
256
        push hl
257
        jr .savecodemain                ; and do as for CODE
258 248 winston
 
259 221 winston
.savebasline
260 248 winston
        rst CALLBAS
261
        defw ZX_NEXT_CHAR
262
        rst CALLBAS
263
        defw ZX_EXPT1_NUM               ; 1 number - the line number
264
        call STATEMENT_END
265 221 winston
 
266 248 winston
        ; Runtime for save "x" LINE y
267
        rst CALLBAS
268
        defw ZX_FIND_INT2               ; Fetch the number
269 250 winston
        ld (v_bcsave), bc
270 248 winston
        rst CALLBAS
271
        defw ZX_STK_FETCH               ; Fetch the file name
272 250 winston
        push de
273
        push bc
274
        xor a                           ; type = 0
275
        call F_tbas_mktapheader         ; Create the header
276
        ld hl, (v_bcsave)               ; get LINE parameter
277
        ld (INTERPWKSPC+OFFSET_PARAM1), hl ; Put it into parameter 1
278 248 winston
.makebasicblock
279
 
280
        ; Fill in the header, length and length without vars
281
        ld hl, (ZX_E_LINE)              ; get the length of the BASIC prog
282
        ld de, (ZX_PROG)                ; by calculating it
283
        scf
284
        sbc hl, de
285
        ld (INTERPWKSPC+OFFSET_LENGTH), hl      ; prog + vars
286
        ld hl, (ZX_VARS)                ; now save the length - vars
287
        sbc hl, de                      ; calculate it...
288
        ld (INTERPWKSPC+OFFSET_PARAM2), hl
289 250 winston
        pop bc                          ; retrieve filename
290 248 winston
        pop de
291
        call F_tbas_writefile           ; Write it out.
292
        jp c, J_tbas_error
293
        jp EXIT_SUCCESS
294
 
295 221 winston
;----------------------------------------------------------------------------
296
; F_tbas_ls
297
; List a directory
298
; Two forms - either %cat or %cat "directory"
299
F_tbas_ls
300
        cp 0x0d
301
        jr z, .noargs
302
        cp ':'
303
        jr z, .noargs
304
 
305
        ; we have an argument supplied.
306
        rst CALLBAS
307
        defw ZX_EXPT_EXP                ; expect a string
308
        call STATEMENT_END
309
 
310
        ; --------- runtime ----------
311
        rst CALLBAS
312
        defw ZX_STK_FETCH               ; get the directory arg
313
        ld hl, INTERPWKSPC
314
        call F_basstrcpy                ; convert it to a C string
315
        ld hl, INTERPWKSPC
316
        jr .makecat
317
 
318
.noargs
319
        call STATEMENT_END
320
 
321
        ; --------- runtime ----------
322
        ld a, '.'
323
        ld (INTERPWKSPC), a             ; default directory is CWD
324
        xor a
325
        ld (INTERPWKSPC+1), a
326
        ld hl, INTERPWKSPC
327
.makecat
328
        call OPENDIR                    ; open the directory
329
        jp c, J_tbas_error
330
        ld (v_vfs_dirhandle), a         ; save the directory handle
331
        ld a, 2
332
        rst CALLBAS                     ; set channel to 2
333
        defw 0x1601
334
.catloop
335
        ld a, (v_vfs_dirhandle)         ; get the dir handle back
336
        ld de, INTERPWKSPC              ; location for result
337
        call READDIR                    ; read dir
338
        jr c, .readdone                 ; read is probably at EOF
339
        ld hl, INTERPWKSPC
340
        call F_tbas_zxprint             ; print a C string to #2
341
        ld a, '\r'                      ; newline
342
        rst CALLBAS
343
        defw 0x10
344
        jr .catloop
345
.readdone
346
        push af                         ; save error code while
347
        ld a, (v_vfs_dirhandle)         ; we close the dir handle
348
        call CLOSEDIR
349
        pop hl                          ; pop into hl to not disturb flags
350
        jp c, J_tbas_error              ; report any error
351
        ld a, h                         ; get original error code
352
        cp EOF                          ; EOF is good
353
        jp nz, J_tbas_error             ; everything else is bad, report it
354
        jp EXIT_SUCCESS
355
 
356 222 winston
;---------------------------------------------------------------------------
357
; F_tbas_tapein
358
; Handle the %tapein command, which takes a filename as a parameter.
359
F_tbas_tapein
360
        rst CALLBAS
361
        defw ZX_EXPT_EXP                ; expect a string expression
362
        call STATEMENT_END
363
 
364
        ;-------- runtime --------
365
        rst CALLBAS
366
        defw ZX_STK_FETCH               ; get the string
367
        call F_settrap
368
        jp c, J_tbas_error              ; carry set = error
369
        jp EXIT_SUCCESS
370
 
371 221 winston
;----------------------------------------------------------------------------
372 225 winston
; F_tbas_info
373
; Handle the %info command
374
F_tbas_info
375
        rst CALLBAS
376
        defw ZX_EXPT_EXP                ; expect a string
377
        call STATEMENT_END
378
 
379
        ;-------- runtime ----------
380
        rst CALLBAS
381
        defw ZX_STK_FETCH
382
        call F_showfileinfo             ; Try to open the file and show
383
        jp c, J_tbas_error              ; the information.
384
        jp EXIT_SUCCESS
385 238 winston
 
386
;----------------------------------------------------------------------------
387
; F_tbas_fs
388
; Sets the current filesystem.
389
F_tbas_fs
390
        rst CALLBAS
391
        defw ZX_EXPT1_NUM               ; expect one number - the FS number
392
        call STATEMENT_END
393
 
394
        ;-------- runtime ----------
395
        rst CALLBAS
396
        defw ZX_FIND_INT2               ; get the fs number
397
        ld a, c                         ; get it from BC
398
        call SETMOUNTPOINT
399
        jp nc, EXIT_SUCCESS             ; No carry = FS change OK
400
        ld a, EBADFS
401
        jp J_tbas_error
402 255 winston
 
403
;----------------------------------------------------------------------------
404
; F_loadsnap
405
; Loads a snapshot file.
406
F_loadsnap
407
        rst CALLBAS
408
        defw ZX_EXPT_EXP                ; filename string
409
        call STATEMENT_END
410
 
411
        ;---------- runtime -----------
412
        rst CALLBAS
413
        defw ZX_STK_FETCH               ; get the filename
414 262 winston
        ld hl, INTERPWKSPC+256
415 255 winston
        call F_basstrcpy                ; copy filename as a C string
416 284 winston
        ld de, INTERPWKSPC+256
417
        ld hl, 0xFB01                   ; Module ID = 0xFB, call ID = 0x01
418
        rst MODULECALL_NOPAGE
419 255 winston
        jp J_tbas_error                 ; If we get here, an error occurred
420 288 winston
 
421 225 winston
;----------------------------------------------------------------------------
422 288 winston
; F_tbas_mv
423
; Moves (renames) a file.
424
F_tbas_mv
425
        rst CALLBAS
426
        defw ZX_EXPT_EXP                ; source filename
427
        cp ','                          ; and a comma
428
        jp nz, PARSE_ERROR
429
        rst CALLBAS
430
        defw ZX_NEXT_CHAR               ; advance past ,
431
 
432
        rst CALLBAS
433
        defw ZX_EXPT_EXP                ; destination filename
434
        call STATEMENT_END              ; then the end of statement
435
 
436
        ;------- runtime ---------
437
        rst CALLBAS
438
        defw ZX_STK_FETCH               ; destination filename
439
        ld hl, INTERPWKSPC+256
440
        call F_basstrcpy                ; copy to workspace as C string
441
        rst CALLBAS
442
        defw ZX_STK_FETCH               ; source filename
443
        ld hl, INTERPWKSPC
444
        call F_basstrcpy                ; copy to workspace
445
        ld hl, INTERPWKSPC              ; source and dest filename pointers
446
        ld de, INTERPWKSPC+256
447
        call RENAME
448
        jp nc, EXIT_SUCCESS
449
        jp J_tbas_error
450
 
451
;---------------------------------------------------------------------------
452
; F_tbas_rm: Removes a file
453
F_tbas_rm
454
        rst CALLBAS
455
        defw ZX_EXPT_EXP                ; file to remove
456
        call STATEMENT_END
457
 
458
        ;-------- runtime ---------
459
        rst CALLBAS
460
        defw ZX_STK_FETCH
461
        ld hl, INTERPWKSPC
462
        call F_basstrcpy
463
        ld hl, INTERPWKSPC
464
        call UNLINK                     ; remove the file
465
        jp nc, EXIT_SUCCESS
466
        jp J_tbas_error
467
 
468
;----------------------------------------------------------------------------
469 221 winston
; F_tbas_zxprint
470
; Prints a C string to the current ZX channel
471
; HL = pointer to string
472
F_tbas_zxprint
473
        ld a, (hl)
474
        and a
475
        ret z
476
        rst CALLBAS
477
        defw 0x10
478
        inc hl
479
        jr F_tbas_zxprint
480
 
481
;----------------------------------------------------------------------------
482
; handle errors and return control to BASIC.
483
; A=tnfs error number
484
J_tbas_error
485
        push af                         ; save error number
486
        call F_geterrstr                ; get the error string
487
        pop af
488
        jp REPORTERR                    ; exit to BASIC with error string
489
 
490
;----------------------------------------------------------------------------
491
; F_tnfs_geterrstr
492
; Enter with A=error number
493
; Exits with HL=pointer to null terminated error string
494
F_geterrstr
495 255 winston
        ld h, 0xFC              ; ID of messages module
496
        ld l, a                 ; Set the message ID to fetch
497
        ld de, 0x3000           ; buffer
498
        xor a
499
        ld (de), a              ; initialize with a NULL
500
        rst MODULECALL_NOPAGE
501
        ld hl, 0x3000           ; address of the message
502
        ret
503 221 winston
 
504
STR_proto       defb    "tnfs",0
505
 
506 233 winston
EBADURL         equ     0x28
507 238 winston
EBADFS          equ     0x29
508