<> <> DIRECTORY Ascii, Basics, Commander, CommandTool, Convert, FileNames, FS, IO, PeachPrint, Process, PupDefs, PupStream, PupTypes, Rope, UserCredentials, UserProfile ; PeachPrintImpl: CEDAR PROGRAM IMPORTS Basics, Commander, CommandTool, Convert, FileNames, FS, IO, Process, PupDefs, PupStream, Rope, UserCredentials, UserProfile EXPORTS PeachPrint = BEGIN PupAborted: PUBLIC ERROR = CODE; localServer: PUBLIC Rope.ROPE _ PupDefs.GetMyName[]; MarkByte: TYPE = [0..256); setLineWidth: MarkByte = 2; timingMark: MarkByte = 5; timingMarkReply: MarkByte = 6; Status: TYPE = {waiting, started, complete, error}; PeachPrintProc: Commander.CommandProc = { ENABLE { ABORTED => GOTO Abort; PupAborted => GOTO Abort }; remoteServer: Rope.ROPE _ UserProfile.Token["PeachPrint.PrintServer", "Sleepy"]; primaryFile: Rope.ROPE _ FileName[fullPath, primary, 1, ".pd"]; secondaryFile: Rope.ROPE _ FileName[fullPath, secondary, 1, ".pd"]; list: LIST OF Rope.ROPE; length: NAT; [list, length] _ CommandTool.ParseToList[cmd]; SELECT length FROM 0 => { DoPeachPrintCommand[localServer, primaryFile, cmd.out]; DoPeachPrintCommand[remoteServer, secondaryFile, cmd.out]}; 1 => { DoPeachPrintCommand[list.first, primaryFile, cmd.out]}; ENDCASE => { DoPeachPrintCommand[list.first, list.rest.first, cmd.out]}; EXITS Abort => {cmd.out.PutF["aborted\n"]; RETURN[$Failure] } }; FileName: PUBLIC PROC[ length: PeachPrint.FileNameLength, order: PeachPrint.FileNameOrder, index: INT _ 1, ext: Rope.ROPE _ NIL ] RETURNS[rope: Rope.ROPE] = { rope _ SELECT length FROM fullPath => IO.PutFR["/%g/Cedar/", IO.rope[localServer]], long => "///", ENDCASE => NIL; IF order=primary THEN rope _ IO.PutFR["%gPD/Plot", IO.rope[rope] ] ELSE rope _ IO.PutFR["%gPD/Plot%g-", IO.rope[rope] , IO.int[index] ]; rope _ IO.PutFR["%g%g%g", IO.rope[rope], IO.int[index], IO.rope[ext] ]}; ThisMachineFileName: PROC[file, ext: Rope.ROPE ] RETURNS[machineFileName: Rope.ROPE] = { IF file = NIL THEN RETURN[NIL]; IF FS.ExpandName[file].cp.server.length=0 THEN { file _ FileNames.FileWithSearchRules[file, ext, FALSE, FALSE, NIL].fullPath; file _ IO.PutFR["/%g/Cedar/%g", IO.rope[localServer], IO.rope[Rope.Substr[file, 3]]] }; RETURN[file]}; <> <> <> <> <> DoPeachPrintCommand: PUBLIC PROC[server, file: Rope.ROPE, log: IO.STREAM _ IO.noWhereStream] = { request: INT _ -1; file _ ThisMachineFileName[file, ".pd"]; IF file#NIL THEN { ENABLE ABORTED => [] _ CancelRequest[server, request, log]; status: Status _ waiting; rope: Rope.ROPE; log.PutF["Post request at %g . . . ", IO.rope[server]]; [request, rope] _ GetRequestNum[server, file, log]; log.PutF["done\n"]; Process.CheckForAbort[]; IF request=-1 THEN { log.PutF["\nI could not find the request number in here:\n%g\n", IO.rope[rope]]; RETURN}; log.PutF["Request R%03g queued at %g\n %g", IO.int[request], IO.rope[server], IO.rope[file]]; log.PutF[" . . . waiting . . . "]; status _ WaitOnRequest[server, request, log]; IF status=complete THEN log.PutF["done\n"] ELSE log.PutF["????\n"] } }; GetRequestNum: PROC[server, fileCopiesTitle: Rope.ROPE, log: IO.STREAM _ IO.noWhereStream] RETURNS[req: INT, rope: Rope.ROPE] = { index: INT; msgRope: Rope.ROPE _ IO.PutFR["Print %g\n", IO.rope[fileCopiesTitle]]; temp: Rope.ROPE _ PeachQuery[server, msgRope, log]; rope _ temp; IF (index_Rope.Index[rope, 0, "Print request "]+14)>=rope.Length[] THEN {log.PutF["\n%g . . . ", IO.rope[rope]]; ERROR ABORTED}; temp _ Rope.Substr[rope, index]; temp _ Rope.Substr[temp, 0, Rope.Index[temp, 0, " queued"]]; IF temp.Length=0 THEN req _ -1 ELSE req _ Convert.IntFromRope[temp]}; WaitOnRequest: PROC[server: Rope.ROPE, req: INT, log: IO.STREAM _ IO.noWhereStream] RETURNS[status: Status] = { msgRope: Rope.ROPE _ IO.PutFR["Wait %03g\n", IO.int[req]]; rope: Rope.ROPE _ PeachQuery[server, msgRope, log]; status _ error; IF Rope.Find[rope,"Done"]#-1 THEN status _ complete ELSE log.PutF["<<%g>>", IO.rope[rope]]}; CancelRequest: PROC[server: Rope.ROPE, req: INT, log: IO.STREAM _ IO.noWhereStream] = { msgRope: Rope.ROPE _ IO.PutFR["Cancel %03g\n", IO.int[req]]; IF req>=0 THEN [ ] _ PeachQuery[server, msgRope, log] }; PeachQuery: PROC[server, msg: Rope.ROPE, log: IO.STREAM _ IO.noWhereStream] RETURNS[rope: Rope.ROPE] = { PupAborting: BOOL _ FALSE; PupDefs.PupPackageMake[]; BEGIN addr: PupDefs.PupAddress; serverStream: IO.STREAM; <> addr _ PupStream.GetPupAddress[PupTypes.telnetSoc, server ! PupStream.PupNameTrouble => {log.PutF["PUP name error"]; Finish[log, e]; GOTO PupAbort}]; serverStream _ PupStream.PupByteStreamCreate[addr, PupStream.SecondsToTocks[1] ! PupStream.StreamClosing => {log.PutF["Can't connect"]; Finish[log, text]; GOTO PupAbort}]; rope _ GetResponse[serverStream, log ! PupStream.StreamClosing => {log.PutF["Can't get response"]; Finish[log, text]; GOTO PupAbort}]; LogIn[serverStream]; rope _ GetResponse[serverStream, log ! PupStream.StreamClosing => {log.PutF["Can't log in"]; Finish[log, text]; GOTO PupAbort} ]; serverStream.PutRope[msg]; serverStream.Flush[]; rope _ GetResponse[serverStream, log ! PupStream.StreamClosing => {log.PutF["Server won't except command"]; Finish[log, text]; GOTO PupAbort}]; serverStream.PutRope["Quit "]; serverStream.Flush[]; serverStream.Close[]; serverStream _ NIL; EXITS PupAbort => PupAborting_TRUE; END; PupDefs.PupPackageDestroy[]; IF PupAborting THEN ERROR PupAborted }; Finish: PROC [log: IO.STREAM, errorMsg: Rope.ROPE] = { IF errorMsg # NIL THEN log.PutF[": %s.\n", IO.rope[errorMsg]] ELSE log.PutF[".\n"] }; LogIn: PROC [serverStream: IO.STREAM] = TRUSTED { name, password: Rope.ROPE; PupStream.SendMark[serverStream, setLineWidth]; serverStream.PutChar[0C]; serverStream.Flush[]; [name: name, password: password] _ UserCredentials.Get[]; serverStream.PutRope["Login "]; serverStream.PutRope[name]; IF Rope.Find[s1: name, s2: "."] = -1 THEN serverStream.PutRope[".PA"]; serverStream.PutRope[" "]; serverStream.PutRope[password]; serverStream.PutRope[" \n"]; serverStream.Flush[] }; <<>> GetResponse: PROC[serverStream: IO.STREAM, log: IO.STREAM _ IO.noWhereStream] RETURNS[rope: Rope.ROPE] = { ros: IO.STREAM _ IO.ROS[]; lastChar: CHARACTER _ ' ; DO c: CHARACTER; IF serverStream.EndOf[] THEN { mySST: MarkByte _ PupStream.ConsumeMark[serverStream]; IF mySST = timingMark THEN PupStream.SendMark[serverStream, timingMarkReply] }; c _ serverStream.GetChar[! PupStream.TimeOut => RESUME; IO.EndOfStream => LOOP]; c _ LOOPHOLE[Basics.BITAND[LOOPHOLE[c, CARDINAL], 177B], CHAR]; SELECT c FROM '> => {ros.PutChar[c]; IF lastChar=c THEN EXIT}; Ascii.BEL => ERROR; Ascii.ControlA, Ascii.BS => ERROR; Ascii.TAB, IN[Ascii.SP..0176C] => ros.PutChar[c]; Ascii.LF => ros.PutChar[Ascii.CR]; ENDCASE => NULL; lastChar _ c; Process.CheckForAbort[]; ENDLOOP; rope _ IO.RopeFromROS[ros]; <> }; Commander.Register[key: "PeachPrint", proc: PeachPrintProc, doc: "PrinterName /SomeMachine/Cedar/SomePDFile.pd"]; END.