| 1 |
(* StdChans.mod implement the ISO StdChans specification. |
| 2 |
|
| 3 |
Copyright (C) 2003-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. *) |
| 26 |
|
| 27 |
IMPLEMENTATION MODULE StdChans ; |
| 28 |
|
| 29 |
IMPORT FIO, IOLink, ChanConsts, SYSTEM, RTio ; |
| 30 |
|
| 31 |
FROM RTio IMPORT SetFile, GetFile, GetDevicePtr ; |
| 32 |
FROM IOConsts IMPORT ReadResults ; |
| 33 |
FROM ChanConsts IMPORT read, write, text, raw, FlagSet ; |
| 34 |
FROM RTgenif IMPORT GenDevIF, InitGenDevIF ; |
| 35 |
|
| 36 |
FROM RTfio IMPORT doreadchar, dounreadchar, |
| 37 |
dogeterrno, dorbytes, dowbytes, |
| 38 |
dowriteln, |
| 39 |
iseof, iseoln, iserror ; |
| 40 |
|
| 41 |
FROM RTgen IMPORT ChanDev, DeviceType, |
| 42 |
InitChanDev, doLook, doSkip, doSkipLook, doWriteLn, |
| 43 |
doReadText, doWriteText, doReadLocs, doWriteLocs, |
| 44 |
checkErrno ; |
| 45 |
|
| 46 |
|
| 47 |
VAR |
| 48 |
inch, |
| 49 |
outch, |
| 50 |
errch, |
| 51 |
stdin, |
| 52 |
stdout, |
| 53 |
stderr, |
| 54 |
stdnull: ChanId ; |
| 55 |
gen : GenDevIF ; |
| 56 |
dev : ChanDev ; |
| 57 |
did : IOLink.DeviceId ; |
| 58 |
|
| 59 |
|
| 60 |
PROCEDURE look (d: IOLink.DeviceTablePtr; |
| 61 |
VAR ch: CHAR; VAR r: ReadResults) ; |
| 62 |
BEGIN |
| 63 |
doLook(dev, d, ch, r) |
| 64 |
END look ; |
| 65 |
|
| 66 |
|
| 67 |
PROCEDURE skip (d: IOLink.DeviceTablePtr) ; |
| 68 |
BEGIN |
| 69 |
doSkip(dev, d) |
| 70 |
END skip ; |
| 71 |
|
| 72 |
|
| 73 |
PROCEDURE skiplook (d: IOLink.DeviceTablePtr; |
| 74 |
VAR ch: CHAR; VAR r: ReadResults) ; |
| 75 |
BEGIN |
| 76 |
doSkipLook(dev, d, ch, r) |
| 77 |
END skiplook ; |
| 78 |
|
| 79 |
|
| 80 |
PROCEDURE lnwrite (d: IOLink.DeviceTablePtr) ; |
| 81 |
BEGIN |
| 82 |
doWriteLn(dev, d) |
| 83 |
END lnwrite ; |
| 84 |
|
| 85 |
|
| 86 |
PROCEDURE textread (d: IOLink.DeviceTablePtr; |
| 87 |
to: SYSTEM.ADDRESS; |
| 88 |
maxChars: CARDINAL; |
| 89 |
VAR charsRead: CARDINAL) ; |
| 90 |
BEGIN |
| 91 |
doReadText(dev, d, to, maxChars, charsRead) |
| 92 |
END textread ; |
| 93 |
|
| 94 |
|
| 95 |
PROCEDURE textwrite (d: IOLink.DeviceTablePtr; |
| 96 |
from: SYSTEM.ADDRESS; |
| 97 |
charsToWrite: CARDINAL); |
| 98 |
BEGIN |
| 99 |
doWriteText(dev, d, from, charsToWrite) |
| 100 |
END textwrite ; |
| 101 |
|
| 102 |
|
| 103 |
PROCEDURE rawread (d: IOLink.DeviceTablePtr; |
| 104 |
to: SYSTEM.ADDRESS; |
| 105 |
maxLocs: CARDINAL; |
| 106 |
VAR locsRead: CARDINAL) ; |
| 107 |
BEGIN |
| 108 |
doReadLocs(dev, d, to, maxLocs, locsRead) |
| 109 |
END rawread ; |
| 110 |
|
| 111 |
|
| 112 |
PROCEDURE rawwrite (d: IOLink.DeviceTablePtr; |
| 113 |
from: SYSTEM.ADDRESS; |
| 114 |
locsToWrite: CARDINAL) ; |
| 115 |
BEGIN |
| 116 |
doWriteLocs(dev, d, from, locsToWrite) |
| 117 |
END rawwrite ; |
| 118 |
|
| 119 |
|
| 120 |
PROCEDURE getname (d: IOLink.DeviceTablePtr; |
| 121 |
VAR a: ARRAY OF CHAR) ; |
| 122 |
BEGIN |
| 123 |
FIO.GetFileName(GetFile(d^.cid), a) |
| 124 |
END getname ; |
| 125 |
|
| 126 |
|
| 127 |
PROCEDURE flush (d: IOLink.DeviceTablePtr) ; |
| 128 |
BEGIN |
| 129 |
FIO.FlushBuffer(GetFile(d^.cid)) |
| 130 |
END flush ; |
| 131 |
|
| 132 |
|
| 133 |
PROCEDURE StdInChan () : ChanId ; |
| 134 |
(* Returns the identity of the implementation-defined standard source for |
| 135 |
program input. |
| 136 |
*) |
| 137 |
BEGIN |
| 138 |
RETURN( stdin ) |
| 139 |
END StdInChan ; |
| 140 |
|
| 141 |
|
| 142 |
PROCEDURE StdOutChan () : ChanId ; |
| 143 |
(* Returns the identity of the implementation-defined standard source for program |
| 144 |
output. |
| 145 |
*) |
| 146 |
BEGIN |
| 147 |
RETURN( stdout ) |
| 148 |
END StdOutChan ; |
| 149 |
|
| 150 |
|
| 151 |
PROCEDURE StdErrChan () : ChanId ; |
| 152 |
(* Returns the identity of the implementation-defined standard destination for program |
| 153 |
error messages. |
| 154 |
*) |
| 155 |
BEGIN |
| 156 |
RETURN( stderr ) |
| 157 |
END StdErrChan ; |
| 158 |
|
| 159 |
|
| 160 |
PROCEDURE NullChan () : ChanId ; |
| 161 |
(* Returns the identity of a channel open to the null device. *) |
| 162 |
BEGIN |
| 163 |
RETURN( stdnull ) |
| 164 |
END NullChan ; |
| 165 |
|
| 166 |
|
| 167 |
(* The following functions return the default channel values *) |
| 168 |
|
| 169 |
PROCEDURE InChan () : ChanId ; |
| 170 |
(* Returns the identity of the current default input channel. *) |
| 171 |
BEGIN |
| 172 |
RETURN( inch ) |
| 173 |
END InChan ; |
| 174 |
|
| 175 |
|
| 176 |
PROCEDURE OutChan () : ChanId ; |
| 177 |
(* Returns the identity of the current default output channel. *) |
| 178 |
BEGIN |
| 179 |
RETURN( outch ) |
| 180 |
END OutChan ; |
| 181 |
|
| 182 |
|
| 183 |
PROCEDURE ErrChan () : ChanId ; |
| 184 |
(* Returns the identity of the current default error message channel. *) |
| 185 |
BEGIN |
| 186 |
RETURN( errch ) |
| 187 |
END ErrChan ; |
| 188 |
|
| 189 |
(* The following procedures allow for redirection of the default channels *) |
| 190 |
|
| 191 |
PROCEDURE SetInChan (cid: ChanId) ; |
| 192 |
(* Sets the current default input channel to that identified by cid. *) |
| 193 |
BEGIN |
| 194 |
inch := cid |
| 195 |
END SetInChan ; |
| 196 |
|
| 197 |
|
| 198 |
PROCEDURE SetOutChan (cid: ChanId) ; |
| 199 |
(* Sets the current default output channel to that identified by cid. *) |
| 200 |
BEGIN |
| 201 |
outch := cid |
| 202 |
END SetOutChan ; |
| 203 |
|
| 204 |
|
| 205 |
PROCEDURE SetErrChan (cid: ChanId) ; |
| 206 |
(* Sets the current default error channel to that identified by cid. *) |
| 207 |
BEGIN |
| 208 |
errch := cid |
| 209 |
END SetErrChan ; |
| 210 |
|
| 211 |
|
| 212 |
(* |
| 213 |
handlefree - |
| 214 |
*) |
| 215 |
|
| 216 |
PROCEDURE handlefree (d: IOLink.DeviceTablePtr) ; |
| 217 |
VAR |
| 218 |
f: FIO.File ; |
| 219 |
BEGIN |
| 220 |
WITH d^ DO |
| 221 |
doFlush(d) ; |
| 222 |
checkErrno(dev, d) ; |
| 223 |
f := RTio.GetFile(RTio.ChanId(cid)) ; |
| 224 |
IF FIO.IsNoError(f) |
| 225 |
THEN |
| 226 |
FIO.FlushBuffer(f) ; |
| 227 |
END ; |
| 228 |
checkErrno(dev, d) |
| 229 |
END |
| 230 |
END handlefree ; |
| 231 |
|
| 232 |
|
| 233 |
(* |
| 234 |
SafeClose - only closes a channel if it was a StdChan. |
| 235 |
*) |
| 236 |
|
| 237 |
PROCEDURE SafeClose (VAR cid: ChanId) ; |
| 238 |
BEGIN |
| 239 |
IF (cid#NIL) AND (cid#IOChan.InvalidChan()) AND IOLink.IsDevice(cid, did) |
| 240 |
THEN |
| 241 |
IOLink.UnMakeChan(did, cid) ; |
| 242 |
cid := IOChan.InvalidChan() |
| 243 |
END |
| 244 |
END SafeClose ; |
| 245 |
|
| 246 |
|
| 247 |
(* |
| 248 |
MapFile - |
| 249 |
*) |
| 250 |
|
| 251 |
PROCEDURE MapFile (f: FIO.File; fl: ChanConsts.FlagSet) : IOChan.ChanId ; |
| 252 |
VAR |
| 253 |
c: IOChan.ChanId ; |
| 254 |
d: IOLink.DeviceTablePtr ; |
| 255 |
BEGIN |
| 256 |
IOLink.MakeChan(did, c) ; |
| 257 |
d := GetDevicePtr(c) ; |
| 258 |
WITH d^ DO |
| 259 |
result := notKnown ; |
| 260 |
SetFile(c, f) ; |
| 261 |
flags := fl ; |
| 262 |
doLook := look ; |
| 263 |
doSkip := skip ; |
| 264 |
doSkipLook := skiplook ; |
| 265 |
doLnWrite := lnwrite ; |
| 266 |
doTextRead := textread ; |
| 267 |
doTextWrite := textwrite ; |
| 268 |
doRawRead := rawread ; |
| 269 |
doRawWrite := rawwrite ; |
| 270 |
doGetName := getname ; |
| 271 |
(* doReset := reset ; *) |
| 272 |
doFlush := flush ; |
| 273 |
doFree := handlefree |
| 274 |
END ; |
| 275 |
RETURN( c ) |
| 276 |
END MapFile ; |
| 277 |
|
| 278 |
|
| 279 |
(* |
| 280 |
Init - initializes the device and opens up the standard channels. |
| 281 |
*) |
| 282 |
|
| 283 |
PROCEDURE Init ; |
| 284 |
BEGIN |
| 285 |
IOLink.AllocateDeviceId(did) ; |
| 286 |
IOLink.MakeChan(did, stdnull) ; |
| 287 |
|
| 288 |
gen := InitGenDevIF(did, doreadchar, dounreadchar, |
| 289 |
dogeterrno, dorbytes, dowbytes, |
| 290 |
dowriteln, |
| 291 |
iseof, iseoln, iserror) ; |
| 292 |
dev := InitChanDev(stdchans, did, gen) ; |
| 293 |
|
| 294 |
stdin := MapFile(FIO.StdIn, read+text+raw) ; |
| 295 |
stdout := MapFile(FIO.StdOut, write+text+raw) ; |
| 296 |
stderr := MapFile(FIO.StdErr, write+text+raw) ; |
| 297 |
SetInChan(stdin) ; |
| 298 |
SetOutChan(stdout) ; |
| 299 |
SetErrChan(stderr) ; |
| 300 |
END Init ; |
| 301 |
|
| 302 |
|
| 303 |
BEGIN |
| 304 |
Init |
| 305 |
FINALLY |
| 306 |
SafeClose(inch) ; |
| 307 |
SafeClose(outch) ; |
| 308 |
SafeClose(errch) ; |
| 309 |
SafeClose(stdin) ; |
| 310 |
SafeClose(stdout) ; |
| 311 |
SafeClose(stderr) |
| 312 |
END StdChans. |