home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / stdlib.zip / GETS.ASM < prev    next >
Assembly Source File  |  1991-02-23  |  2KB  |  116 lines

  1. stdlib        segment    para public 'slcode'
  2.         assume    cs:stdlib
  3. ;
  4.         extrn    sl_malloc:far, sl_realloc:far, sl_free:far
  5.         extrn    sl_putc:far, sl_getc:far
  6. ;
  7. ;
  8. ; GETS-    Reads a line of text from the user and returns a pointer to the
  9. ;    string read.  Returns the pointer in ES:DI.  Carry=0 if no error,
  10. ;    1 if heap overflow, EOF, or some other error (DOS).
  11. ;
  12. ;    The returned string is zero terminated and does not include the
  13. ;    carriage return (ENTER) key code.
  14. ;
  15. ; Note: This routine always allocates 256 bytes when you call
  16. ;    it.
  17. ;
  18. ; Released to the public domain.
  19. ; Created by: Randall Hyde
  20. ; Date: 7/90
  21. ; Updates:
  22. ;
  23. ;    8/11/90-    Modification to handle eof and other errors.
  24. ;
  25. ;
  26. ;
  27. ;
  28.         public    sl_gets
  29. sl_gets        proc    far
  30.         push    ax
  31.         push    bx
  32.         push    cx
  33.         pushf
  34. ;
  35. ; Allocate storage for return string:
  36. ;
  37.         mov    cx, 256
  38.         call    sl_malloc
  39.         jc    BadGETS
  40. ;
  41. ; Read data from keyboard until the user hits the enter key.
  42. ;
  43.         xor    bx, bx
  44. RdKbdLp:    call    sl_getc
  45.         jc    BadGetc
  46.         cmp    ah, 0
  47.         jz    EndString
  48.         cmp    al, 0                ;Scan code?
  49.         jnz    GotKey                ;If so, ignore it.
  50.         call    sl_getc
  51.         jmp    RdKbdLp
  52. ;
  53. GotKey:        cmp    al, 08                ;Backspace
  54.         jne    NotBS
  55.         or    bx, bx                 ;Don't do it if at
  56.         jz    RdKbdLp                ; beginning of line.
  57.         dec    bx
  58.         call    sl_putc
  59.         jmp    RdKbdLp
  60. ;
  61. NotBS:        cmp    al, 13                ;See if ENTER.
  62.         jnz    NotCR
  63.         call    sl_putc
  64.         mov    al, 0ah
  65.         call    sl_putc
  66.         mov    byte ptr es:[bx][di], 0
  67.         inc    bx
  68.         jmp    GetsDone
  69. ;
  70. NotCR:        cmp    al, 1bh                ;ESC
  71.         jne    NotESC
  72.         mov    al, 8
  73. EraseLn:    call    sl_putc
  74.         dec    bx
  75.         jne    EraseLn
  76.         jmp    RdKbdLp
  77. ;
  78. NotESC:        mov    es:[bx][di], al
  79.         call    sl_putc
  80.         inc    bx
  81.         cmp    bx, 255
  82.         jb    RdKbdLp
  83.         mov    al, 7                ;Bell
  84.         call    sl_putc
  85.         dec    bx
  86.         jmp    RdKbdLp
  87. ;
  88. ; Deallocate any left over storage:
  89. ;
  90. GetsDone:    mov    cx, bx
  91.         call    sl_realloc
  92.         popf
  93.         clc
  94.         pop     cx
  95.         pop    bx
  96.         pop    ax
  97.         ret
  98. ;
  99. EndString:    mov    ax, 0            ;End of file.
  100.         call    sl_free            ;Deallocate storage
  101.         jmp    short BadGetsx
  102. ;
  103. BadGetc:    call    sl_free
  104.         jmp    short BadGetsx
  105. ;
  106. BadGets:    mov    ax, 1            ;Memory allocation error.
  107. BadGetsx:    popf
  108.         pop    cx
  109.         pop    bx
  110.         add    sp, 2            ;Don't restore AX.
  111.         stc                ;Pass error status.
  112.         ret
  113. sl_gets        endp
  114. stdlib        ends
  115.         end
  116.