1 ;; -----------------------------------------------------------------------
3 ;; Copyright 1994-2007 H. Peter Anvin - All Rights Reserved
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 ;; -----------------------------------------------------------------------
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
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
36 getc_file_lg2 equ 4 ; Size of getc_file as a power of 2
39 %if (getc_file_size != (1 << getc_file_lg2))
40 %error "getc_file_size != (1 << getc_file_lg2)"
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
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
72 jb .stack_full ; Excessive nesting
75 mov [bx+gc_file],si ; File pointer
76 mov [bx+gc_bytes],ax ; Bytes available
77 mov [bx+gc_bytes+2],dx
79 mov [bx+gc_bufbytes],ax ; Buffer empty
80 mov [bx+gc_unget_cnt],al ; ungetc buffer empty
99 movzx bx,byte [di+gc_unget_cnt]
103 mov ax,real_mode_seg ; Borrow the real_mode_seg
106 sub word [di+gc_bufbytes],1
107 jc .get_data ; Was it zero already?
108 mov si,[di+gc_bufdata]
111 mov [di+gc_bufdata],si
122 mov al,[di+bx+gc_unget_buf]
123 mov [di+gc_unget_cnt],bl
128 ; Compute start of buffer
131 shl bx,bytes_per_getc_lg2-getc_file_lg2
133 mov [di+gc_bufdata],bx
135 mov ecx,[di+gc_bytes]
137 cmp ecx,bytes_per_getc
139 mov ecx,bytes_per_getc
141 mov [di+gc_bufbytes],cx
142 sub [di+gc_bytes],ecx
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
155 mov [di+gc_bufbytes],cx
166 add bx,getc_file_size
173 ; ungetc: Push a character (in AL) back into the getc buffer
174 ; Note: if more than MAX_UNGET bytes are pushed back, all
175 ; hell will break loose.
181 movzx bx,[di+gc_unget_cnt]
182 mov [bx+di+gc_unget_buf],al
184 mov [di+gc_unget_cnt],bl
190 ; skipspace: Skip leading whitespace using "getc". If we hit end-of-line
191 ; or end-of-file, return with carry set; ZF = true of EOF
192 ; ZF = false for EOLN; otherwise CF = ZF = 0.
194 ; Otherwise AL = first character after whitespace
206 .eof: cmp al,al ; Set ZF
209 .eoln: add al,0FFh ; Set CF, clear ZF
213 ; getint: Load an integer from the getc file.
214 ; Return CF if error; otherwise return integer in EBX
218 .getnum: cmp di,NumBufEnd ; Last byte in NumBuf
227 call ungetc ; Unget non-numeric
228 .loaded: mov byte [di],0
230 ; Fall through to parseint
233 ; parseint: Convert an integer to a number in EBX
234 ; Get characters from string in DS:SI
236 ; DS:SI points to first character after number
238 ; Syntaxes accepted: [-]dec, [-]0+oct, [-]0x+hex, val+[KMG]
244 xor eax,eax ; Current digit (keep eax == al)
245 mov ebx,eax ; Accumulator
247 xor bp,bp ; Used for negative flag
251 xor bp,1 ; Set unary minus flag
259 mov cl,10 ; Base = decimal
264 jb .km ; Value is zero
270 mov cl,8 ; Base = octal
273 mov al,'0' ; No numeric value accrued yet
274 mov cl,16 ; Base = hex
277 jc .km ; Not a (hex) digit
279 jae .km ; Invalid for base
280 imul ebx,ecx ; Multiply accumulated by base
281 add ebx,eax ; Add current digit
285 dec si ; Back up to last non-numeric
297 neg ebx ; Value was negative
305 .isg: shl ebx,10 ; * 2^30
306 .ism: shl ebx,10 ; * 2^20
307 .isk: shl ebx,10 ; * 2^10
312 NumBuf resb 15 ; Buffer to load number
313 NumBufEnd resb 1 ; Last byte in NumBuf
315 GetCStack resb getc_file_size*MAX_GETC
319 CurrentGetC dw GetCStack.end ; GetCStack empty
322 ; unhexchar: Convert a hexadecimal digit in AL to the equivalent number;
323 ; return CF=1 if not a hex digit
328 jb .ret ; If failure, CF == 1 already
333 .notdigit: or al,20h ; upper case -> lower case
335 jb .ret ; If failure, CF == 1 already
338 sub al,'a'-10 ; CF <- 0
345 ; getline: Get a command line, converting control characters to spaces
346 ; and collapsing streches to one; a space is appended to the
347 ; end of the string, unless the line is empty.
348 ; The line is terminated by ^J, ^Z or EOF and is written
349 ; to ES:DI. On return, DI points to first char after string.
350 ; CF is set if we hit EOF.
354 mov dl,1 ; Empty line -> empty string.
374 jnz .fillloop ; Ignore multiple spaces
375 mov al,' ' ; Ctrl -> space
378 .eoln: clc ; End of line is not end of file
381 .ret: pushf ; We want the last char to be space!