-- PDRemoteStreamImpl.mesa -- Copyright (C) 1984, Xerox Corporation. All rights reserved. -- Michael Plass, September 11, 1984 4:00:48 pm PDT -- DIRECTORY IO, Stream, STP, Rope, PDRemoteStream, String; PDRemoteStreamImpl: PROGRAM IMPORTS Rope, STP, String, Stream EXPORTS PDRemoteStream = BEGIN Error: PUBLIC ERROR [expl: LONG STRING, retryable: BOOL] = CODE; RopeFromString: PROC [string: LONG STRING] RETURNS [Rope.ROPE] = { i: INTEGER _ -1; fet: SAFE PROC RETURNS [CHAR] = TRUSTED {RETURN [string[i_i+1]]}; IF string = NIL THEN RETURN [NIL]; RETURN [Rope.FromProc[string.length, fet]]; }; Rs: PROC [string: LONG STRING] RETURNS [Rope.ROPE] = RopeFromString; AppendRope: PROC [string: LONG STRING, rope: Rope.ROPE] = { action: Rope.ActionType = TRUSTED { string[string.length] _ c; string.length _ string.length + 1; }; [] _ rope.Map[action: action]; }; Retryable: PROC [code: STP.ErrorCode] RETURNS [BOOL] = { RETURN [SELECT code FROM connectionTimedOut, connectionRejected, connectionClosed, noConnection, noNameLookupResponse => TRUE, ENDCASE => FALSE] }; Lookup: PUBLIC PROC [fileName: LONG STRING, createDate: LONG STRING, name, password: LONG STRING] RETURNS [bytes: INT] = { bytes _ MyLookup[fileName, createDate, name, password ! STP.Error => TRUSTED { expl: LONG STRING _ [80]; AppendRope[expl, error]; ERROR Error[expl: expl, retryable: Retryable[code]] }; ]; }; ParseTest: PROC [fileName: Rope.ROPE] RETURNS [server, rest: Rope.ROPE] = { f: LONG STRING _ [80]; s: LONG STRING _ [80]; r: LONG STRING _ [80]; AppendRope[f, fileName]; ParseName[f, s, r]; server _ Rs[s]; rest _ Rs[r]; }; ParseName: PUBLIC PROC [fileName, server, rest: LONG STRING] = { state: NAT _ 0; server.length _ 0; rest.length _ 0; FOR i: NAT IN [0..fileName.length) DO c: CHAR _ fileName[i]; charType: NAT _ SELECT c FROM '[ => 0, '/ => 1, '] => 2, '< => 3, '> => 4, '! => 5, IN ['0..'9], IN ['a..'z], IN ['A..'Z] => 6, '-, '+, '$, '., '' => 6 ENDCASE => Error[expl: "Illegal character in filename", retryable: FALSE]; SELECT state*7+charType FROM 0*7+0 => state _ 1; 0*7+1 => state _ 7; 1*7+6 => {server[server.length] _ c; server.length _ server.length + 1}; 1*7+2 => state _ 2; 2*7+3 => {state _ 3; rest[rest.length] _ '<; rest.length _ rest.length + 1}; 3*7+6, 3*7+4 => {rest[rest.length] _ c; rest.length _ rest.length + 1}; 3*7+5 => {state _ 4; rest[rest.length] _ c; rest.length _ rest.length + 1}; 4*7+6 => { rest[rest.length] _ c; rest.length _ rest.length + 1; IF c IN ['0..'9] THEN state _ 5 ELSE IF c = 'l OR c = 'L OR c = 'h OR c='H THEN state _ 6 ELSE state _ 9 }; 5*7+6 => { rest[rest.length] _ c; rest.length _ rest.length + 1; IF c NOT IN ['0..'9] THEN state _ 9 }; 7*7+6 => {server[server.length] _ c; server.length _ server.length + 1}; 7*7+1 => {state _ 8; rest[rest.length] _ '<; rest.length _ rest.length + 1}; 7*8+6 => {rest[rest.length] _ c; rest.length _ rest.length + 1}; 7*8+1 => {rest[rest.length] _ '>; rest.length _ rest.length + 1}; 7*8+5 => {state _ 4; rest[rest.length] _ c; rest.length _ rest.length + 1}; ENDCASE => Error[expl: "Illegal filename", retryable: FALSE]; ENDLOOP; SELECT state FROM 3, 8 => {rest[rest.length] _ '!; rest[rest.length+1] _ 'H; rest.length _ rest.length + 2}; 5, 6 => NULL; ENDCASE => Error[expl: "Illegal filename", retryable: FALSE]; }; MyLookup: PROC [fileName, createDate, name, password: LONG STRING] RETURNS [bytes: INT _ 0] = { server: LONG STRING _ [80]; rest: LONG STRING _ [80]; stp: STP.Handle _ STP.Create[]; open: BOOL _ FALSE; BEGIN ENABLE UNWIND => { -- IF open THEN stp.Close[ ! STP.Error => TRUSTED {CONTINUE}]; -- stp _ stp.Destroy }; expandedName: LONG STRING _ [80]; success: BOOLEAN _ FALSE; NoteFileProc: STP.NoteFileProcType = TRUSTED { fileInfo: STP.FileInfo _ stp.GetFileInfo[]; continue _ no; success _ TRUE; AppendRope[expandedName, file]; bytes _ fileInfo.size; createDate.length _ 0; AppendRope[createDate, fileInfo.create]; }; desiredProps: STP.DesiredProperties _ ALL[FALSE]; desiredProps[directory] _ TRUE; desiredProps[nameBody] _ TRUE; desiredProps[version] _ TRUE; desiredProps[createDate] _ TRUE; desiredProps[size] _ TRUE; ParseName[fileName, server, rest]; String.AppendChar[expandedName, '[ ]; String.AppendString[expandedName, server]; String.AppendChar[expandedName, '] ]; [] _ stp.Open[Rs[server]]; open _ TRUE; stp.Login[Rs[name], Rs[password]]; stp.SetDesiredProperties[desiredProps]; stp.Enumerate[Rs[rest], NoteFileProc]; stp.Close[ ! STP.Error => TRUSTED {CONTINUE}]; -- stp _ stp.Destroy; IF success THEN {String.Copy[to: fileName, from: expandedName]} ELSE Error[expl: "File not found", retryable: FALSE]; END; }; Read: PUBLIC PROC [fileName: LONG STRING, name, password: LONG STRING, action: PROC[Stream.Handle]] = { MyRead[fileName, name, password, action ! STP.Error => TRUSTED { expl: LONG STRING _ [80]; AppendRope[expl, error]; ERROR Error[expl: expl, retryable: Retryable[code]] }; ]; }; MyRead: PROC [fileName: LONG STRING, name, password: LONG STRING, action: PROC[Stream.Handle]] = { server: LONG STRING _ [80]; rest: LONG STRING _ [80]; stp: STP.Handle _ STP.Create[]; open: BOOL _ FALSE; stream: Stream.Handle; BEGIN ENABLE UNWIND => { -- IF open THEN stp.Close[ ! STP.Error => TRUSTED {CONTINUE}]; -- stp _ stp.Destroy IF stream # NIL THEN {stream.Delete; stream _ NIL}; }; ParseName[fileName, server, rest]; [] _ stp.Open[Rs[server]]; open _ TRUE; stp.Login[Rs[name], Rs[password]]; stream _ Stream.FromIOStreams[stp.CreateRemoteStream[Rs[rest], read], NIL]; action[stream]; stp.Close[ ! STP.Error => TRUSTED {CONTINUE}]; -- stp _ stp.Destroy; stream.Delete; stream _ NIL END; }; END.