NewUXIOTestImpl.mesa
Copyright Ó 1994 by Xerox Corporation. All rights reserved.
Willie-s, March 23, 1994 11:26 am PST
DIRECTORY
Atom,
Commander,
CStrings,
IO,
UXIO,
Rope,
UnixSysCalls,
UnixTypes,
UXStrings;
NewUXIOTestImpl: CEDAR PROGRAM
IMPORTS Atom, Commander, IO, UnixSysCalls, UXIO, UXStrings
~ BEGIN OPEN UnixTypes;
where: ARRAY[0..6) OF INT ¬ [446, 50264, 243144, 55468, 234330, 158217];
debug: BOOL ¬ FALSE;
DoInternalSetIndex: Commander.CommandProc ~ {
o: IO.STREAM ¬ UXIO.CreateStandardStream[output];
s: IO.STREAM ¬ NIL;
infile: Rope.ROPE ~ "/project/bonsai2.0/testlib/BlackWalnut.mailLog";
s ¬ UXIO.CreateFileStream[infile, read];
FOR i: INT IN [0..6) DO
s.SetIndex[where[i]];
o.PutF["GetIndex: %g, SetIndex was: %g\n", [integer[s.GetIndex[]]], [integer[where[i]]] ];
o.PutRope[s.GetLineRope[]];
o.PutChar['\n];
ENDLOOP;
IO.Close[o];
IO.Close[s];
};
DoGetTokenTest: Commander.CommandProc ~ {
o: IO.STREAM ¬ cmd.out;
s: IO.STREAM ¬ NIL;
pos: INT ¬ 0;
infile: Rope.ROPE ~ "/project/bonsai2.0/testlib/BlackWalnut.mailLog";
MyTokenProc: IO.BreakProc = {
cc: IO.CharClass;
what: Rope.ROPE;
SELECT char FROM
'[, '], '(, '), '{, '}, '", '+, '-, '*, '/, '@, '← => { cc ¬ break; what ¬ "break"};
IN [IO.NUL .. IO.SP] => { cc ¬ sepr; what ¬ "sepr" };
',, ':, '; => { cc ¬ sepr; what ¬ "sepr" };
ENDCASE => { cc ¬ other; what ¬ "other"};
IF debug THEN o.PutF[" <<%g, %g>> ", [character[char]], [rope[what]] ];
RETURN[cc];
};
s ¬ UXIO.CreateFileStream[infile, read];
o.PutF["GetIndex: %g, EndOf: %g\n", [integer[s.GetIndex[]]], [boolean[s.EndOf[]]] ];
FOR i: INT IN [0..5) DO
o.PutF["(%g) %g\n", [integer[i]], [rope[s.GetTokenRope[MyTokenProc].token] ]];
ENDLOOP;
pos ¬ s.GetIndex[];
o.PutF["GetIndex: %g, EndOf: %g\n", [integer[s.GetIndex[]]], [boolean[s.EndOf[]]] ];
FOR i: INT IN [6..9) DO
o.PutF["(%g) %g\n", [integer[i]], [rope[s.GetTokenRope[MyTokenProc].token] ]];
ENDLOOP;
o.PutF["GetIndex: %g, EndOf: %g\n", [integer[s.GetIndex[]]], [boolean[s.EndOf[]]] ];
o.PutF1["Now do SetIndex back to %g\n", [integer[pos]] ];
s.SetIndex[pos];
FOR i: INT IN [6..9) DO
o.PutF["(%g) %g\n", [integer[i]], [rope[s.GetTokenRope[MyTokenProc].token] ]];
ENDLOOP;
o.PutF["GetIndex: %g, EndOf: %g\n", [integer[s.GetIndex[]]], [boolean[s.EndOf[]]] ];
IO.Close[s];
};
mode: UnixTypes.Mode ¬ UXIO.defaultMode;
DoStat: Commander.CommandProc ~ {
o: IO.STREAM ¬ cmd.out;
infile: Rope.ROPE ~ "/project/bonsai2.0/testlib/BlackWalnut.mailLog";
stat: UnixTypes.Stat;
cName: CStrings.CString ~ UXStrings.Create[infile];
res: RES;
fd: UnixTypes.FD ¬ UnixSysCalls.Open[cName, [access: RDONLY], mode];
IF fd = error THEN { o.PutRope["UnixSysCalls.Open failed\n"]; RETURN };
TRUSTED { res ¬ UnixSysCalls.FStat[fd, @stat] };
IF res # success THEN { o.PutRope["FStat failed\n"]; RETURN };
o.PutFL["[ino: %g, mode: %g, uid: %g, gid: %g, size: %g]\n",
LIST[[integer[stat.ino]], [integer[LOOPHOLE[stat.mode]]], [integer[stat.uid]], [integer[stat.gid]], [integer[stat.size]] ]];
[] ¬ UnixSysCalls.Close[fd];
};
DoStream: PROC [s: IO.STREAM] ~ {
IO.PutRope[s, "Hello world; Cedar is alive on a Sun\L"];
IO.PutF1[s, "a rope: %g\L", [rope["rope"]]];
IO.PutF1[s, "an int: %g\L", [integer[LAST[INT]]]];
IO.PutF1[s, "an int: %g\L", [integer[FIRST[INT]]]];
IO.PutF1[s, "a char: %g\L", [character['c]]];
IO.PutF1[s, "an atom: %g\L", [atom[Atom.MakeAtom["atom"]]]];
IO.PutF1[s, "a bool: %g\L", [boolean[TRUE]]];
IO.PutF1[s, "a card: %g\L", [cardinal[LAST[CARD]]]];
IO.PutF1[s, "a card: %g\L", [cardinal[FIRST[CARD]]]];
IO.PutF1[s, "a time: %g\L", [time[BasicTime.nullGMT]]];
IO.PutF1[s, "a real: %g\L", [real[1.0]]];
};
DoStandard: Commander.CommandProc ~ {
s: IO.STREAM ¬ UXIO.CreateStandardStream[output];
DoStream[s];
};
DoFile: Commander.CommandProc ~ {
s: IO.STREAM ¬ UXIO.CreateFileStream["myfile", write];
DoStream[s];
cmd.out.PutRope["wrote file myfile\n"];
cmd.out.PutRope["now to append to myfile\n"];
s ¬ UXIO.CreateFileStream["myfile", append];
DoStream[s];
cmd.out.PutRope["finished appending to file myfile\n"];
};
magicFlags: ARRAY UXIO.Access OF UnixTypes.FileFlags ~ [
read: [access: RDONLY], -- read only
append: [creat: true, append: true, access: WRONLY], -- create, append, write only
write: [trunc: true, creat: true, access: WRONLY] -- truncate, create, write only
];
PosixFileFlags: TYPE ~ MACHINE DEPENDENT RECORD [
fill0: [0..0FFFFFH] ¬ 0,
fill1: Flag ¬ false,
excl: Flag ¬ false, -- error if already created
trunc: Flag ¬ false, -- truncate to zero length
creat: Flag ¬ false, -- create if nonexistant [sic]
nonblk: Flag ¬ false, -- signal pgrp when data ready
fill2: [0..7] ¬ 0,
append: Flag ¬ false, -- append on each write
ndelay: Flag ¬ false, -- non-blocking reads
access: MACHINE DEPENDENT { RDONLY(0), WRONLY(1), RDWR(2), (3)}
];
posixMagicFlags: ARRAY UXIO.Access OF PosixFileFlags ~ [
read: [access: RDONLY], -- read only
append: [creat: true, append: true, access: WRONLY], -- create, append, write only
write: [trunc: true, creat: true, access: WRONLY] -- truncate, create, write only
];
DoFlags: Commander.CommandProc ~ {
cmd.out.PutF["unixflags: read=%xH, write=%xH, append=%xH\n",
[integer[LOOPHOLE[magicFlags[read]]]],
[integer[LOOPHOLE[magicFlags[write]]]],
[integer[LOOPHOLE[magicFlags[append]]]] ];
cmd.out.PutF["posixflags: read=%xH, write=%xH, append=%xH\n",
[integer[LOOPHOLE[posixMagicFlags[read]]]],
[integer[LOOPHOLE[posixMagicFlags[write]]]],
[integer[LOOPHOLE[posixMagicFlags[append]]]] ];
};
Commander.Register["UXIODoStat", DoStat];
Commander.Register["UXIODoInternalSetIndex", DoInternalSetIndex];
Commander.Register["UXIODoGetTokenTest", DoGetTokenTest];
Commander.Register["UXIODoStandard", DoStandard];
Commander.Register["UXIODoFile", DoFile];
Commander.Register["UXIODoFlags", DoFlags];
END.
Commander.Register["UXIOXXX", yyy];