Revamp runkernel.inc for "lengthless" operation
[syslinux.git] / getc.inc
blobeddba8078fe6bbc6cc82821d0f6a8958c61547d3
1 ;; -----------------------------------------------------------------------
2 ;;
3 ;;   Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
4 ;;
5 ;;   This program is free software; you can redistribute it and/or modify
6 ;;   it under the terms of the GNU General Public License as published by
7 ;;   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 ;;   Boston MA 02111-1307, USA; either version 2 of the License, or
9 ;;   (at your option) any later version; incorporated herein by reference.
11 ;; -----------------------------------------------------------------------
14 ;; getc.inc
16 ;; Simple file handling library (open, getc, ungetc)
18 ;; WARNING: This interface uses the real_mode_seg/comboot_seg.
21 MAX_GETC_LG2    equ 4                   ; Max number of file nesting
22 MAX_GETC        equ (1 << MAX_GETC_LG2)
23 bytes_per_getc_lg2      equ 16-MAX_GETC_LG2
24 bytes_per_getc          equ (1 << bytes_per_getc_lg2)
25 secs_per_getc   equ bytes_per_getc/SECTOR_SIZE
26 MAX_UNGET       equ 5                   ; Max bytes that can be pushed back
28                 struc getc_file
29 gc_file         resw 1                  ; File pointer
30 gc_bufbytes     resw 1                  ; Bytes left in buffer
31 gc_bytes        resd 1                  ; Bytes left in file
32 gc_bufdata      resw 1                  ; Pointer to data in buffer
33 gc_unget_cnt    resb 1                  ; Character pushed back count
34 gc_unget_buf    resb MAX_UNGET          ; Character pushed back buffer
35                 endstruc
36 getc_file_lg2   equ 4                   ; Size of getc_file as a power of 2
38 %ifndef DEPEND
39 %if (getc_file_size != (1 << getc_file_lg2))
40 %error "getc_file_size != (1 << getc_file_lg2)"
41 %endif
42 %endif
45 ; open,getc:    Load a file a character at a time for parsing in a manner
46 ;               similar to the C library getc routine.
47 ;               Up to MAX_GETC files can be open at the same time,
48 ;               they are accessed in a stack-like fashion.
50 ;               All routines assume CS == DS.
52 ;               open:   Input:  mangled filename in DS:DI
53 ;                       Output: ZF set on file not found or zero length
55 ;               openfd: Input:  file handle in SI, file size in EAX
56 ;                       Output: ZF set on getc stack overflow
58 ;               getc:   Output: CF set on end of file
59 ;                               Character loaded in AL
61 ;               close:  Output: CF set if nothing open
63 open:
64                 call searchdir
65                 jz openfd.ret
66 openfd:
67                 push bx
69                 mov bx,[CurrentGetC]
70                 sub bx,getc_file_size
71                 cmp bx,GetCStack
72                 jb .stack_full          ; Excessive nesting
73                 mov [CurrentGetC],bx
75                 mov [bx+gc_file],si     ; File pointer
76                 mov [bx+gc_bytes],eax   ; Bytes available
77                 xor ax,ax
78                 mov [bx+gc_bufbytes],ax         ; Buffer empty
79                 mov [bx+gc_unget_cnt],al        ; ungetc buffer empty
81                 inc ax                  ; ZF <- 0
82                 pop bx
83 .ret:           ret
85 .stack_full:
86                 call close_file
87                 xor ax,ax               ; ZF <- 1
88                 pop bx
89                 ret
91 getc:
92                 push bx
93                 push si
94                 push di
95                 push es
97                 mov di,[CurrentGetC]
98                 movzx bx,byte [di+gc_unget_cnt]
99                 and bx,bx
100                 jnz .have_unget
102                 mov si,real_mode_seg    ; Borrow the real_mode_seg
103                 mov es,si
105 .got_data:
106                 sub word [di+gc_bufbytes],1
107                 jc .get_data            ; Was it zero already?
108                 mov si,[di+gc_bufdata]
109                 mov al,[es:si]
110                 inc si
111                 mov [di+gc_bufdata],si
112 .done:
113                 clc
114 .ret:
115                 pop es
116                 pop di
117                 pop si
118                 pop bx
119                 ret
120 .have_unget:
121                 dec bx
122                 mov al,[di+bx+gc_unget_buf]
123                 mov [di+gc_unget_cnt],bl
124                 jmp .done
126 .get_data:
127                 pushad
128                 ; Compute start of buffer
129                 mov bx,di
130                 sub bx,GetCStack
131                 shl bx,bytes_per_getc_lg2-getc_file_lg2
133                 mov [di+gc_bufdata],bx
134                 mov si,[di+gc_file]
135                 mov ecx,[di+gc_bytes]
136                 jecxz .empty
137                 cmp ecx,bytes_per_getc
138                 jna .sizeok
139                 mov ecx,bytes_per_getc
140 .sizeok:
141                 mov [di+gc_bufbytes],cx
142                 sub [di+gc_bytes],ecx
143                 add cx,SECTOR_SIZE-1
144                 shr cx,SECTOR_SHIFT
145                 call getfssec
146                 mov [di+gc_file],si
147                 popad
148                 jmp .got_data
150 .empty:
151                 ; CX == 0 at this point; gc_bufbytes was clobbered
152                 ; by the subtract; we need to restore it to zero so
153                 ; we will continue to get EOF on any further attempts
154                 ; to read the file.
155                 mov [di+gc_bufbytes],cx
156                 popad
157                 xor al,al               ; Return a predictable zero
158                 stc
159                 jmp .ret
161 close:
162                 push bx
163                 push si
164                 mov bx,[CurrentGetC]
165                 mov si,[bx+gc_file]
166                 call close_file
167                 add bx,getc_file_size
168                 mov [CurrentGetC],bx
169                 pop si
170                 pop bx
171                 ret
174 ; ungetc:       Push a character (in AL) back into the getc buffer
175 ;               Note: if more than MAX_UNGET bytes are pushed back, all
176 ;               hell will break loose.
178 ungetc:
179                 push di
180                 push bx
181                 mov di,[CurrentGetC]
182                 movzx bx,[di+gc_unget_cnt]
183                 mov [bx+di+gc_unget_buf],al
184                 inc bx
185                 mov [di+gc_unget_cnt],bl
186                 pop bx
187                 pop di
188                 ret
191 ; skipspace:    Skip leading whitespace using "getc".  If we hit end-of-line
192 ;               or end-of-file, return with carry set; ZF = true of EOF
193 ;               ZF = false for EOLN; otherwise CF = ZF = 0.
195 ;               Otherwise AL = first character after whitespace
197 skipspace:
198 .loop:          call getc
199                 jc .eof
200                 cmp al,1Ah                      ; DOS EOF
201                 je .eof
202                 cmp al,0Ah
203                 je .eoln
204                 cmp al,' '
205                 jbe .loop
206                 ret                             ; CF = ZF = 0
207 .eof:           cmp al,al                       ; Set ZF
208                 stc                             ; Set CF
209                 ret
210 .eoln:          add al,0FFh                     ; Set CF, clear ZF
211                 ret
214 ; getint:       Load an integer from the getc file.
215 ;               Return CF if error; otherwise return integer in EBX
217 getint:
218                 mov di,NumBuf
219 .getnum:        cmp di,NumBufEnd        ; Last byte in NumBuf
220                 jae .loaded
221                 push di
222                 call getc
223                 pop di
224                 jc .loaded
225                 stosb
226                 cmp al,'-'
227                 jnb .getnum
228                 call ungetc             ; Unget non-numeric
229 .loaded:        mov byte [di],0
230                 mov si,NumBuf
231                 ; Fall through to parseint
234 ; parseint:     Convert an integer to a number in EBX
235 ;               Get characters from string in DS:SI
236 ;               Return CF on error
237 ;               DS:SI points to first character after number
239 ;               Syntaxes accepted: [-]dec, [-]0+oct, [-]0x+hex, val+[KMG]
241 parseint:
242                 push eax
243                 push ecx
244                 push bp
245                 xor eax,eax             ; Current digit (keep eax == al)
246                 mov ebx,eax             ; Accumulator
247                 mov ecx,ebx             ; Base
248                 xor bp,bp               ; Used for negative flag
249 .begin:         lodsb
250                 cmp al,'-'
251                 jne .not_minus
252                 xor bp,1                ; Set unary minus flag
253                 jmp short .begin
254 .not_minus:
255                 cmp al,'0'
256                 jb .err
257                 je .octhex
258                 cmp al,'9'
259                 ja .err
260                 mov cl,10               ; Base = decimal
261                 jmp short .foundbase
262 .octhex:
263                 lodsb
264                 cmp al,'0'
265                 jb .km          ; Value is zero
266                 or al,20h               ; Downcase
267                 cmp al,'x'
268                 je .ishex
269                 cmp al,'7'
270                 ja .err
271                 mov cl,8                ; Base = octal
272                 jmp short .foundbase
273 .ishex:
274                 mov al,'0'              ; No numeric value accrued yet
275                 mov cl,16               ; Base = hex
276 .foundbase:
277                 call unhexchar
278                 jc .km                ; Not a (hex) digit
279                 cmp al,cl
280                 jae .km                 ; Invalid for base
281                 imul ebx,ecx            ; Multiply accumulated by base
282                 add ebx,eax             ; Add current digit
283                 lodsb
284                 jmp short .foundbase
285 .km:
286                 dec si                  ; Back up to last non-numeric
287                 lodsb
288                 or al,20h
289                 cmp al,'k'
290                 je .isk
291                 cmp al,'m'
292                 je .ism
293                 cmp al,'g'
294                 je .isg
295                 dec si                  ; Back up
296 .fini:          and bp,bp
297                 jz .ret         ; CF=0!
298                 neg ebx                 ; Value was negative
299 .done:          clc
300 .ret:           pop bp
301                 pop ecx
302                 pop eax
303                 ret
304 .err:           stc
305                 jmp short .ret
306 .isg:           shl ebx,10              ; * 2^30
307 .ism:           shl ebx,10              ; * 2^20
308 .isk:           shl ebx,10              ; * 2^10
309                 jmp .fini
311                 section .bss1
312                 alignb 4
313 NumBuf          resb 15                 ; Buffer to load number
314 NumBufEnd       resb 1                  ; Last byte in NumBuf
316 GetCStack       resb getc_file_size*MAX_GETC
317 .end            equ $
319                 section .data
320 CurrentGetC     dw GetCStack.end        ; GetCStack empty
323 ; unhexchar:    Convert a hexadecimal digit in AL to the equivalent number;
324 ;               return CF=1 if not a hex digit
326                 section .text
327 unhexchar:
328                 cmp al,'0'
329                 jb .ret                 ; If failure, CF == 1 already
330                 cmp al,'9'
331                 ja .notdigit
332                 sub al,'0'              ; CF <- 0
333                 ret
334 .notdigit:      or al,20h               ; upper case -> lower case
335                 cmp al,'a'
336                 jb .ret                 ; If failure, CF == 1 already
337                 cmp al,'f'
338                 ja .err
339                 sub al,'a'-10           ; CF <- 0
340                 ret
341 .err:           stc
342 .ret:           ret
346 ; getline:      Get a command line, converting control characters to spaces
347 ;               and collapsing streches to one; a space is appended to the
348 ;               end of the string, unless the line is empty.
349 ;               The line is terminated by ^J, ^Z or EOF and is written
350 ;               to ES:DI.  On return, DI points to first char after string.
351 ;               CF is set if we hit EOF.
353 getline:
354                 call skipspace
355                 mov dl,1                ; Empty line -> empty string.
356                 jz .eof               ; eof
357                 jc .eoln              ; eoln
358                 call ungetc
359 .fillloop:      push dx
360                 push di
361                 call getc
362                 pop di
363                 pop dx
364                 jc .ret         ; CF set!
365                 cmp al,' '
366                 jna .ctrl
367                 xor dx,dx
368 .store:         stosb
369                 jmp short .fillloop
370 .ctrl:          cmp al,10
371                 je .ret         ; CF clear!
372                 cmp al,26
373                 je .eof
374                 and dl,dl
375                 jnz .fillloop           ; Ignore multiple spaces
376                 mov al,' '              ; Ctrl -> space
377                 inc dx
378                 jmp short .store
379 .eoln:          clc                     ; End of line is not end of file
380                 jmp short .ret
381 .eof:           stc
382 .ret:           pushf                   ; We want the last char to be space!
383                 and dl,dl
384                 jnz .xret
385                 mov al,' '
386                 stosb
387 .xret:          popf
388                 ret