Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.4KB

  1. ( requires core, parse, print )
  2. ( Managing variables in a core module is tricky. Sure, we
  3. have (sysv), but here we need to allocate a big buffer, and
  4. that cannot be done through (sysv). What we do is that we
  5. allocate that buffer at runtime and use (sysv) to point to
  6. it, a pointer that is set during the initialization
  7. routine. )
  8. 64 CONSTANT INBUFSZ
  9. : RDLNMEM+ 0x53 RAM+ @ + ;
  10. ( current position in INBUF )
  11. : IN> 0 RDLNMEM+ ;
  12. ( points to INBUF )
  13. : IN( 2 RDLNMEM+ ;
  14. ( points to INBUF's end )
  15. : IN) INBUFSZ 2 + RDLNMEM+ ;
  16. ( flush input buffer )
  17. ( set IN> to IN( and set IN> @ to null )
  18. : (infl) 0 IN( DUP IN> ! ! ;
  19. ( handle backspace: go back one char in IN>, if possible, then
  20. emit SPC + BS )
  21. : (inbs)
  22. ( already at IN( ? )
  23. IN> @ IN( = IF EXIT THEN
  24. IN> @ 1 - IN> !
  25. SPC BS
  26. ;
  27. ( read one char into input buffer and returns whether we
  28. should continue, that is, whether CR was not met. )
  29. : (rdlnc) ( -- f )
  30. ( buffer overflow? same as if we typed a newline )
  31. IN> @ IN) = IF 0x0a ELSE KEY THEN ( c )
  32. ( del? same as backspace )
  33. DUP 0x7f = IF DROP 0x8 THEN
  34. ( lf? same as cr )
  35. DUP 0x0a = IF DROP 0xd THEN
  36. ( echo back )
  37. DUP EMIT ( c )
  38. ( bacspace? handle and exit )
  39. DUP 0x8 = IF (inbs) EXIT THEN
  40. ( write and advance )
  41. DUP ( keep as result ) ( c c )
  42. ( Here, we take advantage of the fact that c's MSB is
  43. always zero and thus ! automatically null-terminates
  44. our string )
  45. IN> @ ! 1 IN> +! ( c )
  46. ( if newline, replace with zero to indicate EOL )
  47. DUP 0xd = IF DROP 0 THEN
  48. ;
  49. ( Read one line in input buffer and make IN> point to it )
  50. : (rdln)
  51. ( Should we prompt? if we're executing a word, FLAGS bit
  52. 0, then we shouldn't. )
  53. FLAGS @ 0x1 AND NOT IF '>' EMIT SPC THEN
  54. (infl)
  55. BEGIN (rdlnc) NOT UNTIL
  56. LF IN( IN> !
  57. ;
  58. ( And finally, implement a replacement for the (c<) routine )
  59. : (rdln<)
  60. IN> @ C@ ( c )
  61. ( not EOL? good, inc and return )
  62. DUP IF 1 IN> +! EXIT THEN ( c )
  63. ( EOL ? readline. we still return typed char though )
  64. (rdln) ( c )
  65. ;
  66. ( Initializes the readln subsystem )
  67. : RDLN$
  68. ( 53 == rdln's memory )
  69. H@ 0x53 RAM+ !
  70. ( 2 for IN>, plus 2 for extra bytes after buffer: 1 for
  71. the last typed 0x0a and one for the following NULL. )
  72. INBUFSZ 4 + ALLOT
  73. (infl)
  74. ['] (rdln<) 0x0c RAM+ !
  75. ;