<> <> <> DIRECTORY Ascii, Basics, Commander, CommandTool, Convert, File, FileNames, FS, IO, PeachPrint, Process, Pup USING [Address], PupName USING [Error, NameLookup, MyName], PupStream, PupWKS USING [telnet], Rope, UserCredentials, UserProfile; PeachPrintImpl: CEDAR PROGRAM IMPORTS Basics, Commander, CommandTool, Convert, File, FileNames, FS, IO, Process, PupName, PupStream, Rope, UserCredentials, UserProfile EXPORTS PeachPrint = BEGIN PupAborted: PUBLIC ERROR = CODE; localServer: PUBLIC Rope.ROPE _ PupName.MyName[]; localVolume: Rope.ROPE _ File.GetVolumeName[File.SystemVolume[]]; workingDir: Rope.ROPE _ FileNames.CurrentWorkingDirectory[].Substr[3]; MarkByte: TYPE = [0..256); setLineWidth: MarkByte = 2; timingMark: MarkByte = 5; timingMarkReply: MarkByte = 6; Status: TYPE = {waiting, started, complete, error}; PeachPrintProc: Commander.CommandProc = { ENABLE { BadFile => {cmd.out.PutF["File Unknown. Format is: \n PeachPrint %g\n ", IO.rope[doc]]; GOTO Abort}; ABORTED => GOTO Abort; PupAborted => GOTO Abort }; list: LIST OF Rope.ROPE _ LIST[UserProfile.Token["PeachPrint.PrintServer", "???"]]; fork: BOOL _ FALSE; copies: INT _ 1; ii: INT _ 1; argv: CommandTool.ArgumentVector _ CommandTool.Parse[cmd]; WHILE ii < argv.argc DO rope: Rope.ROPE _ CommandTool.ArgN[cmd, ii]; SELECT rope.Fetch[0] FROM '- => SELECT rope.Fetch[1] FROM 'f, 'F => fork _ TRUE; 'c, 'C => { ii _ ii+1; copies _ MAX[0, Convert.IntFromRope[CommandTool.ArgN[cmd, ii]]]}; ENDCASE; ENDCASE => {list _ CONS[rope, list]}; ii _ ii+1; ENDLOOP; IF list.rest=NIL THEN cmd.out.PutF["\n PeachPrint %g\n", IO.rope[doc]] ELSE DoPeachPrintCommand[list.rest.first, list.first, cmd.out, fork, copies]; 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/%g/", IO.rope[localServer], IO.rope[localVolume]], 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] ]}; FullMachineFileName: PROC[file, dExt: Rope.ROPE ] RETURNS[fullName: Rope.ROPE] = { cp: FS.ComponentPositions; server, dir, subDirs, base: Rope.ROPE; IF file.Length[] = 0 THEN RETURN[NIL]; IF file.Find["."] =-1 THEN file _ file.Cat[dExt]; [fullName, cp, ] _ FS.ExpandName[file, Rope.Cat["///", workingDir]]; server _ fullName.Substr[cp.server.start, cp.server.length]; dir _ fullName.Substr[cp.dir.start, cp.dir.length]; subDirs _ fullName.Substr[cp.subDirs.start, cp.subDirs.length]; base _ fullName.Substr[cp.base.start]; IF server.Length[]=0 THEN server _ localServer; IF dir.Length[]=0 THEN dir _ localVolume; fullName _ Rope.Cat["[", server, "]<", dir, ">"]; IF subDirs.Length[]#0 THEN fullName _ fullName.Cat[subDirs, ">"]; fullName _ fullName.Cat[base]; RETURN[FileNames.ConvertToSlashFormat[fullName]]}; BadFile: SIGNAL = CODE; DoPeachPrintCommand: PUBLIC PROC [server, file: Rope.ROPE, log: IO.STREAM_IO.noWhereStream, fork: BOOL_FALSE, copies: INT_1]={ DO ENABLE { FS.Error => IF error.group = user THEN {log.PutRope[error.explanation]; EXIT }}; request: INT _ -1; fileCopiesTitle: Rope.ROPE _ FullMachineFileName[file, ".pd"]; IF fileCopiesTitle#NIL THEN { ENABLE ABORTED => [] _ CancelRequest[server, request, log]; status: Status _ waiting; rope: Rope.ROPE; fileCopiesTitle _ IO.PutFR["%g %g", IO.rope[fileCopiesTitle], IO.int[copies]]; log.PutF["Post request at %g . . . ", IO.rope[server]]; [request, rope] _ GetRequestNum[server, fileCopiesTitle, 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 Copies: %g", IO.int[request], IO.rope[server], IO.rope[file], IO.int[copies]]; IF fork THEN {log.PutF["\n"]; RETURN}; log.PutF[" . . . waiting . . . "]; status _ WaitOnRequest[server, request, log]; IF status=complete THEN log.PutF["done\n"] ELSE log.PutF["????\n"]}; EXIT ENDLOOP }; 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; BEGIN addr: Pup.Address; serverStream: IO.STREAM; <> addr _ PupName.NameLookup[server, PupWKS.telnet ! PupName.Error => { log.PutF["PUP name lookup error: %g", IO.rope[text]]; Finish[log, text]; GOTO PupAbort} ]; serverStream _ PupStream.Create[addr, 60000, 60000 -- Milliseconds ! PupStream.StreamClosing => {log.PutF["Can't connect: %g", IO.rope[text]]; Finish[log, text]; GOTO PupAbort}]; rope _ GetResponse[serverStream, log ! PupStream.StreamClosing => {log.PutF["Can't get response: %g", IO.rope[text]]; Finish[log, text]; GOTO PupAbort}]; LogIn[serverStream ! PupStream.Timeout => { log.PutRope["Timeout during log in"]; RESUME} ]; rope _ GetResponse[serverStream, log ! PupStream.StreamClosing => {log.PutF["Can't log in: %g", IO.rope[text]]; Finish[log, text]; GOTO PupAbort} ]; serverStream.PutRope[msg]; serverStream.Flush[]; rope _ GetResponse[serverStream, log ! PupStream.StreamClosing => { log.PutF["Server won't accept command: %g", IO.rope[text]]; Finish[log, text]; GOTO PupAbort} ]; DO ENABLE PupStream.StreamClosing => EXIT; serverStream.PutRope["Quit "]; serverStream.Flush[]; serverStream.Close[]; EXIT; ENDLOOP; serverStream _ NIL; EXITS PupAbort => PupAborting _TRUE; END; 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]}; doc: Rope.ROPE _ "PeachPrinterName SomePDFile"; Commander.Register[key: "PeachPrint", proc: PeachPrintProc, doc: doc]; END.