Subversion Repositories Spectranet

[/] [trunk/] [modules/] [basext/] [listdir.asm] - Blame information for rev 442

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 314 winston
;The MIT License
2
;
3
;Copyright (c) 2010 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 399 winston
.include        "stat.inc"
23
.include        "fcntl.inc"
24
.include        "spectranet.inc"
25
.include        "defs.inc"
26
.include        "zxrom.inc"
27
.include        "ctrlchars.inc"
28
.include        "sysvars.inc"
29 419 winston
.include        "zxsysvars.inc"
30 442 winston
.include        "errno.inc"
31 399 winston
.text
32 314 winston
 
33
; Show the directory listing. HL = directory to open.
34 399 winston
.globl F_listdir
35
F_listdir:
36 314 winston
        call OPENDIR                    ; open the directory
37
        jp c, J_tbas_error
38
        ld (v_vfs_dirhandle), a         ; save the directory handle
39 419 winston
        ld a, 22
40
        ld (SN_SCR_CT), a               ; and save it
41 314 winston
        ld a, 2
42
        rst CALLBAS                     ; set channel to 2
43
        defw 0x1601
44 434 winston
        call F_getfileaddr              ; get address to put filename
45
        ld (FILE_ADDR), hl
46 399 winston
.catloop1:
47 314 winston
        ld a, (v_vfs_dirhandle)         ; get the dir handle back
48 434 winston
        ld de, (FILE_ADDR)              ; location for result
49 314 winston
        call READDIR                    ; read dir
50 434 winston
        jr c, .readdone1                ; read is probably at EOF
51 314 winston
        call F_statentry                ; show some information about it
52 434 winston
        ld hl, (FILE_ADDR)
53 314 winston
        call F_tbas_zxprint             ; print a C string to #2
54 419 winston
        ld a, 2                         ; set SCR_CT > 1
55
        ld (ZX_SCR_CT), a               ; prevent ZX ROM from doing "Scroll"
56 399 winston
        ld a, ZXNEWLINE                 ; newline
57 314 winston
        rst CALLBAS
58
        defw 0x10
59 419 winston
        ld a, (SN_SCR_CT)
60
        dec a
61
        jr z, .scroll
62
        ld (SN_SCR_CT), a
63 399 winston
        jr .catloop1
64
.readdone1:
65 314 winston
        push af                         ; save error code while
66
        ld a, (v_vfs_dirhandle)         ; we close the dir handle
67
        call CLOSEDIR
68
        pop hl                          ; pop into hl to not disturb flags
69
        jp c, J_tbas_error              ; report any error
70
        ld a, h                         ; get original error code
71
        cp EOF                          ; EOF is good
72
        jp nz, J_tbas_error             ; everything else is bad, report it
73
        jp EXIT_SUCCESS
74
 
75 419 winston
        ; Ask the user if they want to scroll. We don't let the ZX ROM
76
        ; do it or we can end up leaking directory handles.
77
        ; Effectively mirror what the standard ROM does.
78
.scroll:
79
        rst CALLBAS
80
        defw ZX_CLS_LOWER
81
        ld a, 1
82
        rst CALLBAS
83
        defw ZX_CHAN_OPEN
84
        ld hl, STR_scroll               ; print "Scroll?" message
85
        call F_tbas_zxprint
86
        call GETKEY                     ; get a keystroke
87
        cp 0x20                         ; now test the keys that can
88
        jr z, .report_d                 ; interrupt the directory
89
        cp 0xE2                         ; listing
90
        jr z, .report_d
91
        or 0x20
92
        cp 0x6E
93
        jr z, .report_d
94
        rst CALLBAS
95
        defw ZX_CLS_LOWER
96
        ld a, 2                         ; back to main screen
97
        rst CALLBAS
98
        defw ZX_CHAN_OPEN
99
        ld a, 22                        ; reset our own scroll counter
100
        ld (SN_SCR_CT), a
101
        jp .catloop1                    ; back into dir listing routine
102
 
103
.report_d:
104
        ld a, (v_vfs_dirhandle)         ; get the current dirhandle
105
        call CLOSEDIR                   ; close the directory
106
        ld hl, STR_errorD
107
        jp REPORTERR
108
 
109 314 winston
; Expects the filename to be in INTERPWKSPC
110 399 winston
.globl F_statentry
111
F_statentry:
112 314 winston
        ld hl, INTERPWKSPC
113
        ld de, INTERPWKSPC+256          ; where to put the data
114
        call STAT
115 399 winston
        jr c, .staterr2
116 314 winston
        ld ix, INTERPWKSPC+256
117
        ld a, (ix+(STAT_MODE+1))        ; Check file mode MSB
118
        and S_IFDIR / 256               ; check directory flag
119 399 winston
        jr z, .isfile2
120 314 winston
        ld hl, STR_dir
121
        call F_tbas_zxprint
122 399 winston
        jr .continue2
123
.isfile2:
124 314 winston
        call F_showsize
125
        ld a, ' '
126
        rst CALLBAS
127
        defw 0x0010
128 399 winston
.continue2:
129 314 winston
        ret
130
 
131 399 winston
.staterr2:
132 314 winston
        ld hl, STR_staterr
133
        jp F_tbas_zxprint               ; print it and return.
134
 
135 399 winston
STR_staterr:    defb "  Err ",0
136
STR_dir:                defb "  Dir ",0
137 314 winston
 
138
;----------------------------------------------------------------------
139
; F_showsize
140
; Create a 4 digit decimal with the correct ending (b, k, M or G)
141
; TODO: This routine is particularly naive and hardly optimized
142
; for space (or anything else). (Low priority TODO).
143 399 winston
.globl F_showsize
144
F_showsize:
145 314 winston
        ld a, (ix+(STAT_SIZE+3))        ; MSB of size
146
        ld b, a
147
        and 0xC0                        ; >= 2^30
148 399 winston
        jr nz, .gigs3
149 314 winston
        cpl                             ; check lower half
150
        and b                           ; of 4th stat byte for megs
151 399 winston
        jr nz, .megs3
152 314 winston
        ld a, (ix+(STAT_SIZE+2))
153
        ld b, a
154
        and 0xF0                        ; >= 2^20
155 399 winston
        jr nz, .megs3
156 314 winston
        cpl                             ; check lower half of
157
        and b                           ; 3rd stat byte for kilos
158 399 winston
        jr nz, .kilos3
159 314 winston
        ld a, (ix+(STAT_SIZE+1))
160
        and 0xFC                        ; >= 2^10
161 399 winston
        jr nz, .kilos3
162 314 winston
        ld l, (ix+STAT_SIZE)            ; less than 1K
163
        ld h, (ix+(STAT_SIZE+1))
164
        call F_decimal
165
        ld a, 'b'
166
        rst CALLBAS
167
        defw 0x0010
168
        ret
169 399 winston
.kilos3:
170 314 winston
        ld l, (ix+(STAT_SIZE+1))        ; 1K to 1023K
171
        ld h, (ix+(STAT_SIZE+2))
172
        srl h
173
        rr l
174
        srl h
175
        rr l
176
        call F_decimal
177
        ld a, 'k'
178
        rst CALLBAS
179
        defw 0x0010
180
        ret
181 399 winston
.megs3:
182 314 winston
        ld l, (ix+(STAT_SIZE+2))        ; 1M to 1023M
183
        ld h, (ix+(STAT_SIZE+3))
184
        ld b, 4
185 399 winston
.megloop3:
186 314 winston
        srl h
187
        rr l
188 399 winston
        djnz .megloop3
189 314 winston
        call F_decimal
190
        ld a, 'M'
191
        rst CALLBAS
192
        defw 0x0010
193
        ret
194 399 winston
.gigs3:
195 314 winston
        ld l, (ix+(STAT_SIZE+4))        ; 1G to 4G
196
        ld h, 0
197
        ld b, 6
198 399 winston
.gigloop3:
199 314 winston
        srl l
200 399 winston
        djnz .gigloop3
201 314 winston
        call F_decimal
202
        ld a, 'G'
203
        rst CALLBAS
204
        defw 0x0010
205
        ret
206
 
207
;----------------------------------------------------------------------
208
; F_decimal
209 399 winston
; Modified version of http://baze.au3.com3/misc/z80bits.html3#5.13
210 314 winston
; by baze.
211
; HL = number to convert
212 399 winston
.globl F_decimal
213
F_decimal:
214 314 winston
        ld e, 0
215
        ld bc, -1000            ; maximum value passed will be 9999
216 399 winston
        call .num14
217 314 winston
        ld bc, -100
218 399 winston
        call .num14
219 314 winston
        ld c, -10
220 399 winston
        call .num14
221 314 winston
        ld c, b
222
        ld e, 1                 ; print a zero if it's the last digit
223 399 winston
.num14:
224 314 winston
        ld a, '0'-1
225 399 winston
.num24:
226 314 winston
        inc a
227
        add hl, bc
228 399 winston
        jr c, .num24
229 314 winston
        sbc hl, bc
230
 
231
        bit 0, e
232 399 winston
        jr nz, .zerocont4
233 314 winston
        cp '0'
234 399 winston
        jr nz, .nonz4
235 314 winston
        ld a, ' '
236 399 winston
        jr .zerocont4
237
.nonz4:
238 314 winston
        ld e, 1
239 399 winston
.zerocont4:
240 314 winston
        rst CALLBAS
241
        defw 0x0010
242
        ret
243 419 winston
 
244 434 winston
;-------------------------------------------------------------------------
245
; F_getfileaddr
246
; Makes the address for the file name and returns it in HL.
247
; Expects the path at INTERPWKSPC.
248
; Returns with Z set if terminator was found.
249
F_getfileaddr:
250
        xor a                   ; look for the NULL at the end
251
        ld hl, INTERPWKSPC
252
        ld bc, 512              ; TODO: pathmax
253
        cpir
254
        dec hl
255
        ld (hl), '/'
256
        inc hl
257
        ret
258
 
259 419 winston
.data
260
STR_errorD:     defb    "D BREAK into listing",0
261
STR_scroll:     defb    "scroll?",0
262