char-io.red 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. %
  2. % CHAR-IO.RED - Bottom level character IO primitives
  3. %
  4. % Author: Eric Benson
  5. % Symbolic Computation Group
  6. % Computer Science Dept.
  7. % University of Utah
  8. % Date: 27 August 1981
  9. % Copyright (c) 1981 University of Utah
  10. %
  11. % Edit by Cris Perdue, 27 Jan 1983 1652-PST
  12. % ChannelReadChar and ChannelWriteChar now check the FileDes argument
  13. % <PERDUE.PSL>CHAR-IO.RED.2, 29-Dec-82 12:21:51, Edit by PERDUE
  14. % Added code to ChannelWriteChar to maintain PagePosition for LPOSN
  15. global '(IN!* % The current input channel
  16. OUT!*); % The current output channel
  17. on SysLisp;
  18. external WArray ReadFunction, % Indexed by channel # to read char
  19. WriteFunction, % Indexed by channel # to write char
  20. UnReadBuffer, % For input backup
  21. LinePosition, % For Posn()
  22. PagePosition; % For LPosn()
  23. syslsp procedure ChannelReadChar FileDes; %. Read one char from channel
  24. %
  25. % All channel input must pass through this function. When a channel is
  26. % open, its read function must be set up.
  27. %
  28. begin scalar Ch, FD;
  29. FD := IntInf FileDes; %/ Heuristic: don't do Int type test
  30. if not (0 <= FD and FD <= MaxChannels) then
  31. NonIOChannelError(FileDes, "ChannelReadChar");
  32. return if (Ch := UnReadBuffer[FD]) neq char NULL then
  33. << UnReadBuffer[FD] := char NULL;
  34. Ch >>
  35. else
  36. IDApply1(FD, ReadFunction[FD]);
  37. end;
  38. syslsp procedure ReadChar(); %. Read single char from current input
  39. ChannelReadChar LispVar IN!*;
  40. syslsp procedure ChannelWriteChar(FileDes, Ch); %. Write one char to channel
  41. %
  42. % All channel output must pass through this function. When a channel is
  43. % open, its write function must be set up, and line position set to zero.
  44. %
  45. begin scalar FD;
  46. FD := IntInf FileDes;
  47. if not (0 <= FD and FD <= MaxChannels) then
  48. NonIOChannelError(FileDes, "ChannelWriteChar");
  49. if Ch eq char EOL then
  50. << LinePosition[FD] := 0;
  51. PagePosition[FD] := PagePosition[FD] + 1 >>
  52. else if Ch eq char TAB then % LPos := (LPos + 8) - ((LPos + 8) MOD 8)
  53. LinePosition[FD] := LAND(LinePosition[FD] + 8, LNOT 7)
  54. else if Ch eq char FF then
  55. << PagePosition[FD] := 0;
  56. LinePosition[FD] := 0 >>
  57. else
  58. LinePosition[FD] := LinePosition[FD] + 1;
  59. IDApply2(FD, Ch, WriteFunction[FD]);
  60. end;
  61. syslsp procedure WriteChar Ch; %. Write single char to current output
  62. ChannelWriteChar(LispVar OUT!*, Ch);
  63. syslsp procedure ChannelUnReadChar(Channel, Ch); %. Input backup function
  64. %
  65. % Any channel input backup must pass through this function. The following
  66. % restrictions are made on input backup:
  67. % 1. Backing up without first doing input should cause an error, but
  68. % will probably cause unpredictable results.
  69. % 2. Only one character backup is supported.
  70. %
  71. UnReadBuffer[IntInf Channel] := Ch;
  72. syslsp procedure UnReadChar Ch; %. Backup on current input channel
  73. ChannelUnReadChar(LispVar IN!*, Ch);
  74. off SysLisp;
  75. END;