STPsD.mesa - Simple/Stream Transfer Protocol (Common and protocol)
Copyright © 1985 by Xerox Corporation. All rights reserved.
Mark, Feb 12, 1981 11:39 PM
Smokey, 17-Jul-81 7:20:16
Karlton, 17-Jul-81 17:45:12
JGS, 14-Aug-81 10:35:26
Loretta, 27-Oct-82 10:45:58
Daniels, 21-Sep-82 13:07:12
Davirro, 15-Dec-82 15:32:52
Andrew Birrell, June 1, 1983 5:39 pm
Levin, August 9, 1983 9:59 am
Schroeder, August 10, 1983 5:33 pm, but I only changed one character!
MBrown, September 17, 1983 8:14 pm
HGM, February 20, 1984 10:29:03 pm PST
Russ Atkinson (RRA) April 4, 1985 11:40:25 am PST
DIRECTORY
BasicTime USING[ GMT, MonthOfYear, Unpack, Unpacked ],
Convert USING[ IntFromRope ],
IO USING[ EndOf, EndOfStream, GetBlock, GetChar, STREAM, PutChar, PutF, PutRope, ROS, RopeFromROS ],
PupStream USING[ConsumeMark, SendMark, StreamClosing, TimeOut],
Rope USING [Cat, Equal, Fetch, Find, FromRefText, Length, ROPE, Substr],
STP USING [DesiredProperties, Error, ErrorCode, FileInfo, Open, Type],
STPOps USING [DestroyPList, ErrorCodeToSTPErrorCode, FileProperties, FilenameType, GenerateErrorString, GenerateProtocolError, Handle, markAbort, markComment, markEOC, markHereIsPList, markIAmVersion, markNo, markYes, markYouAreUser, Object, PList, PListArray, ServerType, SmashClosed, UserProperties, ValidProperties];
STPsD:
CEDAR
PROGRAM
IMPORTS BasicTime, Convert, IO, PupStream, Rope, STP, STPOps
EXPORTS STP, STPOps =
{ OPEN STPOps;
Exported Types
Object: PUBLIC TYPE = STPOps.Object;
Global Data
MarkEncountered: PUBLIC ERROR = CODE;
BadProperty: PUBLIC ERROR = CODE;
propertyStrings: PListArray = [
userName: "User-Name", userPassword: "User-Password",
connectName: "Connect-Name", connectPassword: "Connect-Password",
byteSize: "Byte-Size", type: "Type", size: "Size", directory: "Directory",
nameBody: "Name-Body", version: "Version", createDate: "Creation-Date",
readDate: "Read-Date", writeDate: "Write-Date", author: "Author",
eolConversion: "End-of-Line-Convention", account: "Account",
userAccount: "User-Account", device: "Device",
serverName: "Server-Filename"];
desiredPropString: Rope.ROPE = "Desired-property";
Connect:
PUBLIC
PROC [stp: STPOps.Handle, name, password: Rope.
ROPE] = {
stp.userState[connectName] ← name;
stp.userState[connectPassword] ← password;
};
IsOpen:
PUBLIC
PROC [stp: STPOps.Handle]
RETURNS [yes:
BOOL] = {
yes ← stp.byteStream # NIL};
GetProperty:
PUBLIC
PROC [stp: STPOps.Handle, prop: ValidProperties]
RETURNS [Rope.
ROPE] = {
RETURN[stp.plist[prop]]};
Login:
PUBLIC
PROC [stp: STPOps.Handle, name, password: Rope.
ROPE] = {
stp.userState[userName] ← name;
stp.userState[userPassword] ← password;
};
SetHost:
PUBLIC
PROC [stp: STPOps.Handle, host: Rope.
ROPE] = {
stp.host ← host };
SetDirectory:
PUBLIC
PROC [stp: STPOps.Handle, directory: Rope.
ROPE] = {
stp.userState[directory] ← directory };
GetCommand:
PUBLIC
PROC [stp: STPOps.Handle]
RETURNS [mark: [0..256), code:
CHARACTER, ps: Rope.
ROPE] = {
code ← 0C;
CheckConnection[stp];
mark ← MyGetMark[stp];
SELECT mark
FROM
markAbort, markComment, markYouAreUser => ps ← CollectString[stp];
markIAmVersion, markNo, markYes => {code ← CollectCode[stp]; ps ← CollectString[stp]};
ENDCASE => GenerateProtocolError[stp, badMark, mark, code];
};
GetHereIsAndPList:
PUBLIC
PROC [stp: STPOps.Handle, gobbleEOC:
BOOL ←
TRUE] = {
mark: [0..256);
DO
SELECT (mark ← MyGetMark[stp])
FROM
markComment => stp.remoteString ← CollectString[stp];
markHereIsPList => {GetPList[stp, gobbleEOC, FALSE]; EXIT};
markNo =>
{
code: CHARACTER = CollectCode[stp];
errorCode: STP.ErrorCode = ErrorCodeToSTPErrorCode[requestRefused, code];
stp.remoteString ← CollectString[stp];
ErrorIfNextNotEOC[stp];
GenerateErrorString[stp, errorCode, stp.remoteString, code];
};
ENDCASE => GenerateProtocolError[stp, badMark, mark, 0C];
ENDLOOP;
};
GetPList:
PUBLIC
PROC [stp: STPOps.Handle, gobbleEOC:
BOOL, propertiesOk:
BOOL] = {
buffer: REF TEXT = NEW[TEXT[100]]; -- arbitrary choice of length
Record:
PROC = {
IF buffer.length # 0
THEN {
fragment: Rope.ROPE = Rope.FromRefText[buffer];
buffer.length ← 0;
IF which = property
THEN property ← property.Cat[fragment]
ELSE value ← value.Cat[fragment];
};
};
Append:
PROC[c:
CHAR] = {
IF buffer.length = buffer.maxLength THEN Record[];
buffer[buffer.length] ← c; buffer.length ← buffer.length+1;
};
property: Rope.ROPE;
value: Rope.ROPE;
parens: INT ← 0;
char: CHARACTER;
which: {property, value} ← property;
CheckConnection[stp];
stp.plist[nameBody] ← NIL;
DO
char ← MyGetChar[stp ! MarkEncountered => IF NOT propertiesOk THEN GOTO BadPList];
SELECT char
FROM
'' =>
Append[MyGetChar[stp ! MarkEncountered => GOTO BadPList]];
'( =>
{ parens ← parens + 1; Record[]; which ← property; property ← NIL; };
' =>
IF which = property THEN { Record[]; which ← value; value ← NIL; }
ELSE Append[char];
') =>
{
Record[];
IF property.Length[] # 0
AND value.Length[] # 0
THEN
{
SetPListItem[
stp.plist, property, value ! BadProperty => CONTINUE];
property ← value ← NIL;
};
IF (parens ← parens - 1) = 0 THEN EXIT;
};
ENDCASE => Append[char];
ENDLOOP;
IF (property.Length[] # 0 AND value.Length[] # 0) THEN GOTO BadPList;
IF gobbleEOC AND MyGetMark[stp] # markEOC THEN GOTO BadPList;
EXITS
BadPList => {ResetPList[stp.plist]; GenerateProtocolError[stp, badPList, stp.mark]};
};
PutCommand:
PUBLIC
PROC [stp: STPOps.Handle, mark: [0..256), code:
CHARACTER, string: Rope.
ROPE, sendEOC:
BOOL ←
TRUE] = {
CheckConnection[stp];
PupStream.SendMark[stp.byteStream, mark];
stp.byteStream.PutChar[code];
IF string.Length[] # 0 THEN MyPutString[stp.byteStream, string];
IF sendEOC THEN PupStream.SendMark[stp.byteStream, markEOC];
};
PutPList:
PUBLIC
PROC [stp: STPOps.Handle, mark: [0..256), sendEOC:
BOOL ←
TRUE] = {
state: {okay, tryOpen, triedOpen} ← okay;
DoIt:
PROC = {
CheckConnection[stp];
IF mark # 0B THEN PupStream.SendMark[stp.byteStream, mark];
stp.byteStream.PutChar['(];
FOR i: ValidProperties
IN ValidProperties
DO
IF stp.plist[i].Length[] # 0
THEN
PutPListItem[stp.byteStream, i, stp.plist[i]];
ENDLOOP;
IF stp.desiredProps # ALL[TRUE] THEN PutDesiredProps[stp];
stp.byteStream.PutChar[')];
IF sendEOC THEN PupStream.SendMark[stp.byteStream, markEOC];
};
DO
DoIt[! PupStream.StreamClosing, PupStream.TimeOut =>
IF state = okay
AND stp.host.Length[] # 0
THEN
{state ← tryOpen; CONTINUE}];
IF state # tryOpen THEN EXIT;
{
savedPList: PList ← stp.plist;
stp.plist ← NIL;
SmashClosed[stp]; -- Call outside catch so Pup monitor unlocked
[] ← STP.Open[stp, stp.host];
stp.plist ← DestroyPList[stp.plist];
stp.plist ← savedPList;
state ← triedOpen;
};
ENDLOOP;
};
CollectString:
PUBLIC
PROC [stp: STPOps.Handle]
RETURNS[ r: Rope.
ROPE ←
NIL] = {
buffer: REF TEXT = NEW[TEXT[100]]; -- arbitrary choice of length
DO buffer.length ← stp.byteStream.GetBlock[buffer, 0, buffer.maxLength];
r ← r.Cat[Rope.FromRefText[buffer]];
IF stp.byteStream.EndOf[] THEN EXIT;
ENDLOOP;
stp.mark ← PupStream.ConsumeMark[stp.byteStream];
stp.gotMark ← TRUE;
SELECT stp.mark
FROM
markHereIsPList, markEOC => NULL;
ENDCASE => GenerateProtocolError[stp, eocExpected, stp.mark];
};
CollectCode:
PUBLIC
PROC [stp: STPOps.Handle]
RETURNS [code:
CHARACTER] = {
code ← 0C;
CheckConnection[stp];
code ← MyGetChar[stp ! MarkEncountered =>
GenerateProtocolError[stp, noCode, MyGetMark[stp]]];
};
CheckConnection:
PUBLIC
PROC [stp: STPOps.Handle] = {
WHILE stp.byteStream =
NIL
DO
SIGNAL STP.Error[stp, noConnection, "Please open a connection!", 0C] ENDLOOP
};
ErrorIfNextNotYes:
PUBLIC
PROC [stp: STPOps.Handle] = {
code: CHARACTER ← 0C;
mark: [0..256);
[mark, code, stp.remoteString] ← GetCommand[stp];
IF mark # markYes THEN GenerateErrorString[stp, requestRefused, stp.remoteString, code];
};
ErrorIfNextNotEOC:
PUBLIC
PROC [stp: STPOps.Handle] = {
mark: [0..256) ← MyGetMark[stp];
IF mark # markEOC THEN GenerateProtocolError[stp, eocExpected, mark];
};
GetServerType:
PUBLIC
PROC [server: Rope.
ROPE]
RETURNS [serverType: ServerType] = {
RETURN[
SELECT
TRUE
FROM
Rope.Equal[server, "MAXC", FALSE],
Rope.Equal[server, "MAXC2", FALSE] => tenex,
ENDCASE => ifs];
};
LookAtMark:
PUBLIC
PROC [stp: STPOps.Handle]
RETURNS [[0..256)] = {
IF ~stp.gotMark
THEN
DO
[] ← MyGetChar[stp ! MarkEncountered => CONTINUE];
IF stp.gotMark THEN EXIT;
ENDLOOP;
RETURN[stp.mark]
};
MyGetChar:
PUBLIC
PROC [stp: STPOps.Handle]
RETURNS [char:
CHARACTER] = {
char ← stp.byteStream.GetChar[! IO.EndOfStream => GOTO end];
EXITS end => {
stp.mark ← PupStream.ConsumeMark[stp.byteStream];
stp.gotMark ← TRUE;
ERROR MarkEncountered;
}
};
MyGetMark:
PUBLIC
PROC [stp: STPOps.Handle]
RETURNS [mark: [0..256)] = {
mark ← LookAtMark[stp]; stp.gotMark ← FALSE; RETURN[mark];
};
MyPutString:
PUBLIC
PROC [byteStream:
IO.
STREAM, string: Rope.
ROPE] = {
byteStream.PutRope[string];
};
MyPutStringVal:
PUBLIC
PROC [byteStream:
IO.
STREAM, string: Rope.
ROPE] = {
FOR i:
INT
IN [0..string.Length[])
DO
char: CHAR = string.Fetch[i];
SELECT char
FROM
'(, '), '' => byteStream.PutChar['']
ENDCASE => NULL;
byteStream.PutChar[char];
ENDLOOP;
};
PropertyString: PUBLIC PROC [prop: STPOps.ValidProperties] RETURNS [string: Rope.ROPE] = { RETURN[propertyStrings[prop]]; };
SetCreateTime:
PUBLIC
PROC [stp: STPOps.Handle, creation: BasicTime.
GMT] = {
month:
ARRAY BasicTime.MonthOfYear
OF Rope.
ROPE = [
January: "Jan", February: "Feb", March: "Mar", April: "Apr", May: "May", June: "Jun", July: "Jul", August: "Aug", September: "Sep", October: "Oct", November: "Nov", December: "Dec" ];
zoneIndex: TYPE = [4 .. 10];
zoneChars: ARRAY zoneIndex OF CHAR = ['A, 'E, 'C, 'M, 'P, 'Y, 'H];
unpack: BasicTime.Unpacked = BasicTime.Unpack[creation];
absZone: INT ← ABS[IF unpack.dst = yes THEN unpack.zone - 60 ELSE unpack.zone];
Desired syntax is: "dd-mmm-yy hh:mm:ss zzz"
str: IO.STREAM = IO.ROS[];
str.PutF["%02d-%g-%02d", [integer[unpack.day]], [rope[month[unpack.month]]], [integer[unpack.year MOD 100]] ];
str.PutF[" %02d:%02d:%02d ", [integer[unpack.hour]], [integer[unpack.minute]], [integer[unpack.second]] ];
IF (unpack.zone / 60 IN zoneIndex) AND absZone MOD 60 = 0
THEN {
str.PutChar[zoneChars[unpack.zone / 60]];
str.PutChar[IF unpack.dst = yes THEN 'D ELSE 'S];
str.PutChar['T];
}
ELSE str.PutF["%g%g:%02d", [character[
IF unpack.zone < 0
THEN '-
ELSE '+]],
[integer[absZone/60]], [integer[absZone MOD 60]] ];
stp.plist[createDate] ← str.RopeFromROS[];
};
SetFileType:
PUBLIC
PROC [
stp: STPOps.Handle, fileType: STP.Type] = {
stp.plist[type] ←
SELECT fileType FROM text => "Text", binary => "Binary", ENDCASE => NIL;
};
SetByteSize:
PUBLIC
PROC [stp: STPOps.Handle, fileType:
STP.Type] = {
stp.plist[byteSize] ←
SELECT stp.serverType
FROM
ifs, unknown, tenex => IF fileType = text THEN NIL ELSE "8",
ENDCASE => ERROR STP.Error[stp, undefinedError, NIL];
};
StringToFileType:
PUBLIC
PROC [string: Rope.
ROPE]
RETURNS [type:
STP.Type] = {
type ←
SELECT
TRUE
FROM
Rope.Equal["Text", string, FALSE] => text,
Rope.Equal["Binary", string, FALSE] => binary,
ENDCASE => unknown;
};
PList and FileInfo Utilities
NameToPList:
PUBLIC
PROC [plist: PList, name: Rope.
ROPE, type: FilenameType] = {
pos: INT ← 0;
length: INT = name.Length[];
IF pos >= length THEN RETURN;
IF name.Fetch[pos] = '[
THEN {
end: INT = name.Find["]"];
IF end > pos THEN { plist[device] ← name.Substr[pos+1, end-pos-1]; pos ← end+1 };
IF pos >= length THEN RETURN;
};
IF name.Fetch[pos] = '<
THEN {
end: INT = name.Find[">"];
IF end > pos THEN { plist[directory] ← name.Substr[pos+1, end-pos-1]; pos ← end+1 };
IF pos >= length THEN RETURN;
};
{
end: INT ← name.Find["!"];
IF end >= 0 THEN plist[version] ← name.Substr[end+1, length-end-1] ELSE end ← length;
plist[nameBody] ← name.Substr[pos, end-pos];
};
};
PListToName:
PUBLIC
PROC [plist: PList, type: FilenameType]
RETURNS [name: Rope.
ROPE ←
NIL] = {
dir: Rope.ROPE = plist[directory];
dirLength: INT = dir.Length[];
IF dirLength # 0
THEN
{
SELECT dir.Fetch[0]
FROM
'> => name ← name.Cat[dir.Substr[1, dirLength-1]];
'< => name ← name.Cat[dir];
ENDCASE => name ← name.Cat["<", dir];
IF dir.Fetch[dirLength - 1] # '> THEN name ← name.Cat[">"];
};
IF plist[nameBody].Length[] # 0 THEN name ← name.Cat[plist[nameBody]];
IF plist[version].Length[] # 0
THEN
name ← name.Cat[IF type = alto THEN "!" ELSE ";", plist[version]];
};
MakeRemoteName:
PUBLIC
PROC [plist: PList, type: FilenameType]
RETURNS [Rope.
ROPE] = {
RETURN[
IF plist[serverName].Length[] = 0
THEN PListToName[plist, type]
ELSE plist[serverName] ]};
PutPListItem:
PUBLIC
PROC [byteStream:
IO.
STREAM, property: STPOps.ValidProperties, value: Rope.
ROPE] = {
byteStream.PutChar['(];
MyPutString[byteStream, PropertyString[property]];
byteStream.PutChar[' ];
MyPutStringVal[byteStream, value];
byteStream.PutChar[')];
};
ResetPList:
PUBLIC
PROC [plist: PList] = {
IF plist # NIL THEN plist^ ← ALL[NIL] };
GetFileInfo:
PUBLIC
PROC [stp: STPOps.Handle]
RETURNS [
STP.FileInfo] = {
FOR i: STPOps.FileProperties
IN STPOps.FileProperties
DO
SELECT i
FROM
directory => stp.info.directory ← stp.plist[i];
nameBody => stp.info.body ← stp.plist[i];
version => stp.info.version ← stp.plist[i];
author => stp.info.author ← stp.plist[i];
createDate => stp.info.create ← stp.plist[i];
readDate => stp.info.read ← stp.plist[i];
writeDate => stp.info.write ← stp.plist[i];
size =>
stp.info.size ←
IF stp.plist[i].Length[] = 0 THEN 0
ELSE Convert.IntFromRope[stp.plist[i]];
type => stp.info.type ← StringToFileType[stp.plist[i]];
ENDCASE;
ENDLOOP;
RETURN[stp.info];
};
SetPListItem:
PUBLIC
PROC [plist: PList, property, value: Rope.
ROPE] = {
FOR i: STPOps.ValidProperties
IN STPOps.ValidProperties
DO
IF property.Equal[PropertyString[i], FALSE] THEN { plist[i] ← value; RETURN};
ENDLOOP;
ERROR BadProperty
};
UserStateToPList:
PUBLIC
PROC [stp: STPOps.Handle] = {
FOR i: STPOps.UserProperties
IN STPOps.UserProperties
DO
stp.plist[i] ← stp.userState[i];
ENDLOOP;
};
Desired Property stuff
SetDesiredProperties:
PUBLIC
PROC[stp: STPOps.Handle, props:
STP.DesiredProperties] = {
stp.desiredProps ← props
};
GetDesiredProperties:
PUBLIC
PROC [stp: STPOps.Handle]
RETURNS [props:
STP.DesiredProperties] = {
RETURN[stp.desiredProps]
};
PutDesiredProps:
PUBLIC
PROC [stp: STPOps.Handle] = {
FOR i: STPOps.FileProperties
IN STPOps.FileProperties
DO
IF stp.desiredProps[i]
THEN
PutDesiredPropsItem[stp.byteStream, PropertyString[i]]
ENDLOOP;
};
PutDesiredPropsItem:
PUBLIC
PROC [byteStream:
IO.
STREAM, value: Rope.
ROPE] = {
byteStream.PutChar['(];
MyPutString[byteStream, desiredPropString];
byteStream.PutChar[' ];
MyPutString[byteStream, value];
byteStream.PutChar[')];
};
}. -- of STPsD