stdlib		segment	para public 'slcode'
		assume	cs:stdlib
;
		extrn	sl_malloc:far, sl_realloc:far
		extrn	sl_putc:far, sl_getc:far
;
;
; GETS-	Reads a line of text from the user and returns a pointer to the
;	string read.  Returns the pointer in ES:DI.  Carry=0 if no error,
;	1 if heap overflow.
;
;	The returned string is zero terminated and does not include the
;	carriage return (ENTER) key code.
;
; Note: This routine always allocates 256 bytes when you call
;	it.  
;
;
;
;
		public	sl_gets
sl_gets		proc	far
		push	ax
		push	bx
		push	cx
		pushf
;
; Allocate storage for return string:
;
		mov	cx, 256
		call	sl_malloc
		jc	BadGETS
;
; Read data from keyboard until the user hits the enter key.
;
		xor	bx, bx
RdKbdLp:	call	sl_getc
		cmp	al, 08				;Backspace
		jne	NotBS
		or	bx, bx 				;Don't do it if at
		jz	RdKbdLp				; beginning of line.
		dec	bx
		call	sl_putc
		jmp	RdKbdLp
;
NotBS:		cmp	al, 13				;See if ENTER.
		jnz	NotCR
		call	sl_putc
		mov	al, 0ah
		call	sl_putc
		mov	byte ptr es:[bx][di], 0
		inc	bx
		jmp	GetsDone
;
NotCR:		cmp	al, 1bh				;ESC
		jne	NotESC
		mov	al, 8
EraseLn:	call	sl_putc
		dec	bx
		jne	EraseLn
		jmp	RdKbdLp
;
NotESC:		mov	es:[bx][di], al
		call	sl_putc
		inc	bx
		cmp	bx, 255
		jb	RdKbdLp
		mov	al, 7				;Bell
		call	sl_putc
		dec	bx
		jmp	RdKbdLp
;
; Deallocate any left over storage:
;
GetsDone:	mov	cx, bx
		call	sl_realloc
		popf
		clc
		pop     cx
		pop	bx
		pop	ax
		ret
;
BadGETS:	popf
		stc
		pop	cx
		pop	bx
		pop	ax
		ret
sl_gets		endp
stdlib		ends
		end
