1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
(* $Id: StdChannels.Mod,v 1.7 1999/10/31 13:56:13 ooc-devel Exp $ *)
MODULE StdChannels;
(* Defines the standard I/O channels.
Copyright (C) 1997-1999 Michael van Acken
This module is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This module is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with OOC. If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)
IMPORT
Channel, FD := PosixFileDescr, Time, Termination, Msg, SYSTEM;
(*
Standard channels do not have to be opened by a client program since they are
already open and ready for use. Under some operating system they may be
connected to sources and destinations specified before the program is run,
while on a stand-alone system they may be connected to a console terminal.
The standard channels (stdin, stdout, stderr) should never be closed and the
values used to identify standard channels should be constant throughout the
execution of the program.
*)
VAR (* standard channels *)
stdin-: Channel.Channel;
stdout-: Channel.Channel;
stderr-: Channel.Channel;
TYPE
NullChannel = POINTER TO NullChannelDesc;
Writer = POINTER TO WriterDesc;
NullChannelDesc = RECORD
(Channel.ChannelDesc)
writer: Writer
END;
WriterDesc = RECORD
(Channel.WriterDesc)
END;
VAR (* null channel; write only, accepts arbitrary input *)
null-: NullChannel;
VAR
fd: FD.Channel;
TYPE
ErrorContext = POINTER TO ErrorContextDesc;
ErrorContextDesc = RECORD
(Channel.ErrorContextDesc)
END;
VAR
errorContext: ErrorContext;
PROCEDURE GetError (code: Msg.Code): Msg.Msg;
BEGIN
RETURN Msg.New (errorContext, code)
END GetError;
(* Writer methods
------------------------------------------------------------------------ *)
PROCEDURE (w: Writer) Pos*(): LONGINT;
BEGIN
RETURN Channel.noPosition
END Pos;
PROCEDURE (w: Writer) SetPos* (newPos: LONGINT);
END SetPos;
PROCEDURE (w: Writer) WriteByte* (x: SYSTEM.BYTE);
BEGIN
w. bytesWritten := 1
END WriteByte;
PROCEDURE (w: Writer) WriteBytes* (VAR x: ARRAY OF SYSTEM.BYTE; start, n: LONGINT);
BEGIN
w. bytesWritten := n
END WriteBytes;
(* Channel methods
------------------------------------------------------------------------ *)
PROCEDURE (ch: NullChannel) Length*(): LONGINT;
BEGIN
RETURN Channel.noLength
END Length;
PROCEDURE (ch: NullChannel) GetModTime* (VAR mtime: Time.TimeStamp);
BEGIN
ch. res := GetError (Channel.noModTime)
END GetModTime;
PROCEDURE (ch: NullChannel) NewReader*(): Channel.Reader;
BEGIN
ch. res := GetError (Channel.noReadAccess);
RETURN NIL
END NewReader;
PROCEDURE (ch: NullChannel) NewWriter*(): Channel.Writer;
VAR
w: Writer;
BEGIN
ch. ClearError;
IF (ch. writer = NIL) THEN
NEW (w);
w. base := ch;
w. ClearError;
w. positionable := FALSE;
w. bytesWritten := -1;
ch. writer := w;
RETURN w
ELSE
RETURN ch. writer
END
END NewWriter;
PROCEDURE (ch: NullChannel) Flush*;
BEGIN
ch. ClearError
END Flush;
PROCEDURE (ch: NullChannel) Close*;
BEGIN
ch. ClearError
END Close;
PROCEDURE Flush;
BEGIN
stdout. Flush; stderr.Flush
END Flush;
BEGIN
NEW (errorContext);
Msg.InitContext (errorContext, "OOC:Core:StdChannels");
(* create standard channels stdin, stdout, stderr from the POSIX file
descriptors *)
NEW (fd);
FD.Init (fd, FD.stdinFileno, FD.readOnly);
stdin := fd;
NEW (fd);
FD.Init (fd, FD.stdoutFileno, FD.writeOnly);
stdout := fd;
NEW (fd);
FD.Init (fd, FD.stderrFileno, FD.writeOnly);
stderr := fd;
(* create null channel *)
NEW (null);
null. ClearError;
null. readable := FALSE;
null. writable := TRUE;
null. open := TRUE;
(* make sure that any output is flushed on program termination *)
Termination.RegisterProc (Flush)
END StdChannels.
|