<> <> <> <> <> <<>> DIRECTORY BasicTime, Convert, GVNames, FS, IO, IPPrinterMain, IPPrinterQueue, IPPrinterRemoteStream, IPPrinterUser, Process, Pup, PupName, PupStream, PupWKS, Rope, RuntimeError USING [UNCAUGHT], SimpleTerminal USING [TurnOn], WatchStats USING [WatchStatsRecord, GetWatchStats]; IPPrinterUserImpl: PROGRAM IMPORTS BasicTime, Convert, GVNames, FS, IO, IPPrinterMain, IPPrinterQueue, IPPrinterRemoteStream, Process, PupName, PupStream, Rope, RuntimeError, SimpleTerminal, WatchStats EXPORTS IPPrinterUser = { ROPE: TYPE ~ Rope.ROPE; helloMsg: ROPE _ "Cedar Interpress 3.0 Print Server"; CommandCode: TYPE = {login, cancel, check, help, listFiles, listQueue, messages, print, resetQueue, setPrinterParams, start, stop, watch, wait, quit, ambiguous, illegal}; commandTable: ARRAY CommandCode[login..quit] OF ROPE _ [ login: "Login", cancel: "Cancel", check: "Check", help: "Help", listFiles: "ListFiles", listQueue: "ListQueue", messages: "Messages", print: "Print", resetQueue: "ResetQueue", setPrinterParams: "SetPrinterParams", start: "Start", stop: "Stop", wait: "Wait", watch: "Watch", quit: "Quit" ]; Upper: PROC [ch: CHAR] RETURNS [CHAR] = INLINE { RETURN [IF ch IN ['a..'z] THEN ch - ('a - 'A) ELSE ch] }; defaultRegistry: ROPE _ ".pa"; loginMessage: ROPE _ NIL; TalkWithUser: PUBLIC PROC [stream: IO.STREAM] = { user: ROPE _ NIL; loggedIn: BOOLEAN _ FALSE; TRUSTED { ENABLE {PupStream.StreamClosing => GOTO Closing; PupStream.Timeout => GOTO TimeOut}; string: REF TEXT _ NEW [TEXT [180]]; password: ROPE; account: ROPE; quitting: BOOLEAN _ FALSE; accessAllowed: BOOLEAN _ TRUE; echo: BOOLEAN _ TRUE; flushed: BOOLEAN _ FALSE; commandCode: CommandCode; lastRequest: INT _ -1; PutChar: PROC [char: CHAR] = { IO.PutChar[stream, char]; flushed _ FALSE; }; PutString: PROC [string: ROPE] = { IO.PutRope[stream, string]; flushed _ FALSE; }; PutOK: PROC = {PutString["\n\lok\n\l"]}; PutXXX: PROC = { PutString[" XXX\n\l"]}; sep: CHAR _ ' ; DelHit: ERROR = CODE; SendNow: PROC = {IO.Flush[stream]; flushed _ TRUE}; GetChar: PROC RETURNS [CHAR] = { c: CHAR; ignore: INT _ 0; IF NOT flushed THEN SendNow[]; WHILE ignore >= 0 DO mark: NAT _ 0; timingMark: NAT = 5; timingMarkReply: NAT = 6; dataMark: NAT = 1; charsAvail: INT _ 0; bytes: PACKED ARRAY [0..4] OF CHAR; charsAvail _ IO.UnsafeGetBlock[stream, [LOOPHOLE[LONG[@bytes]], 0, 1]]; IF charsAvail # 0 THEN c _ bytes[0] ELSE { gotMark: BOOL _ FALSE; IF stream.GetInfo.class = $Pup THEN { gotMark _ TRUE; mark _ PupStream.ConsumeMark[stream ! RuntimeError.UNCAUGHT => {gotMark _ FALSE; CONTINUE}]; }; }; SELECT mark FROM 0 => NULL; dataMark => {ignore _ 1}; timingMark => {ignore _ 1}; ENDCASE => ignore _ 2; ignore _ ignore - 1; ENDLOOP; IF c = '\177 THEN {PutXXX[]; ERROR DelHit}; RETURN [c] }; GetStringToSpace: PROC [stopper1: CHAR _ ' , stopper2: CHAR _ '\t] RETURNS [r: ROPE] = { c: CHAR _ GetChar[]; dashCount: NAT _ 0; inComment: BOOLEAN _ FALSE; commentHit: BOOLEAN _ FALSE; string.length _ 0; UNTIL string.length = string.maxLength OR (NOT inComment AND (c=stopper1 OR c=stopper2)) OR c='\n DO IF c= 'H - 100B OR c= 'A - 100B THEN { IF commentHit THEN {PutXXX[]; ERROR DelHit}; IF string.length > 0 THEN { IF echo THEN PutChar[c]; string.length _ string.length - 1; }; } ELSE IF c= 'W - 100B THEN { IF commentHit THEN {PutXXX[]; ERROR DelHit}; WHILE string.length > 0 DO IF echo THEN PutChar['H - 100B]; string.length _ string.length - 1; ENDLOOP; } ELSE { IF echo THEN PutChar[c]; IF c = '- THEN { commentHit _ TRUE; dashCount _ dashCount + 1; IF dashCount = 2 THEN { inComment _ NOT inComment; dashCount _ 0; }; } ELSE { WHILE dashCount > 0 DO IF NOT inComment THEN { string[string.length] _ '-; string.length _ string.length + 1; }; dashCount _ dashCount - 1; ENDLOOP; IF NOT inComment THEN { string[string.length] _ c; string.length _ string.length + 1; }; }; }; c _ GetChar[]; ENDLOOP; IF string.length <= string.maxLength THEN sep _ c ELSE sep _ ' ; IF string.length = 0 AND sep # '\n THEN { PutChar[sep]; IO.Flush[stream]; RETURN[GetStringToSpace[stopper1, stopper2]]; }; IO.Flush[stream]; RETURN [Rope.FromRefText[string]]; }; GetStringToCR: PROC [] RETURNS [ROPE] = { RETURN[GetStringToSpace['\n, '\n]]; }; GetNumber: PROC [default: INT _ 1] RETURNS [value: INT _ 0] = { r: ROPE _ GetStringToSpace[]; IF Rope.Length[r] = 0 THEN value _ default ELSE value _ Convert.IntFromRope[r ! Convert.Error => {PutString["Not a vailid number.\n\l"]; value _ default; CONTINUE}]; }; Confirm: PROC RETURNS [yes: BOOLEAN _ FALSE] = { response: ROPE _ GetStringToSpace[]; IF Rope.Equal[response, "Yes", FALSE] THEN { RETURN [TRUE] } ELSE IF Rope.Length[response] = 0 OR Upper[Rope.Fetch[response, 0]] # 'N THEN { PutString["Sorry, you must say Yes in just the right way.\n\l"]; RETURN [FALSE] } ELSE PutXXX[] }; EasyConfirm: PROC RETURNS [yes: BOOLEAN _ FALSE] = { response: ROPE _ GetStringToSpace[]; IF Rope.Equal[response, "Yes", FALSE] OR Rope.Equal[response, "Y", FALSE] THEN { RETURN [TRUE] } ELSE IF Rope.Length[response] = 0 OR Upper[Rope.Fetch[response, 0]] # 'N THEN { PutString["Sorry, you must say Yes in just the right way.\n\l"]; RETURN [FALSE] } ELSE PutXXX[] }; GetCommand: PROC = { nMatches: NAT _ 0; matchLength: NAT _ 0; command: ROPE; commandCode _ illegal; PutString[">>"]; command _ GetStringToSpace[]; IF Rope.Length[command] = 0 THEN {PutChar['\n]; PutChar['\l]; IO.Flush[stream]; GetCommand[]} ELSE { FOR cmd: CommandCode IN [login..quit] DO candidate: ROPE _ commandTable[cmd]; IF Rope.Length[command] <= Rope.Length[candidate] THEN { matchLength _ 0; FOR i: INT IN [0 .. Rope.Length[command]) DO IF Upper[Rope.Fetch[command, i]] = Upper[Rope.Fetch[candidate, i]] THEN matchLength _ i+1 ELSE EXIT; ENDLOOP; IF matchLength = Rope.Length[command] THEN { commandCode _ cmd; IF matchLength = Rope.Length[candidate] THEN {nMatches _ 1; EXIT}; nMatches _ nMatches + 1; IF NOT loggedIn THEN EXIT }; }; ENDLOOP; IF nMatches > 1 THEN commandCode _ ambiguous ELSE IF commandCode <= quit THEN { candidate: ROPE _ commandTable[commandCode]; FOR i: INT IN [Rope.Length[command] .. Rope.Length[candidate]) DO PutChar[Rope.Fetch[candidate, i]]; ENDLOOP; IO.Flush[stream]; }; }; }; PutRequestStatus: PROC [request: IPPrinterQueue.Request, status: IPPrinterQueue.RequestStatus] = { PutString[IO.PutFR["%g %g", IO.rope[request.requestTime], IO.rope[request.fileName]]]; IF request.copies # 1 THEN PutString[IO.PutFR1[" (%g copies) ", IO.int[request.copies]]]; PutString[IO.PutFR[" (%g) %g\n\l", IO.rope[request.requestor], IO.rope[(SELECT status FROM canceled => "Cancelled", waiting => "Waiting", printing => "Printing", ENDCASE => NIL)]]]; }; DoCommand: PROC = { GetCommand[]; IF NOT loggedIn AND commandCode # login AND commandCode # quit THEN { PutString["Please log in.\n\l"] } ELSE { SELECT commandCode FROM login => { registryMissing: BOOLEAN _ TRUE; PutString[" --User-- "]; user _ GetStringToSpace[]; registryMissing _ Rope.Find[user, "."] < 0; IF registryMissing THEN { user _ Rope.Concat[user, defaultRegistry]; PutString[defaultRegistry]; }; PutString[" --Password-- "]; echo _ FALSE; password _ GetStringToSpace[ ! UNWIND => echo _ TRUE]; echo _ TRUE; IF sep # '\n THEN { PutString[" --Account-- "]; account _ GetStringToSpace[]; }; PutString[" -- Authenticating ... "]; SendNow[]; SELECT GVNames.Authenticate[user, password] FROM individual => {PutString["OK"]; loggedIn _ TRUE}; allDown => {PutString["all GV servers down; I'll have to trust you."]; loggedIn _ TRUE}; badPwd => {PutString["bad password"]; loggedIn _ FALSE}; ENDCASE => {PutString["bad name"]; loggedIn _ FALSE}; PutChar['\n]; PutChar['\l]; IO.Flush[stream]; IF loginMessage.Length > 0 THEN { IO.PutF1[stream, "%g\n\l", IO.rope[loginMessage]]; }; }; print => { fileName: ROPE; separator: ROPE; time: ROPE; requestNumber: INT _ 0; createDate: ROPE _ NIL; bytes: INT _ 0; copies: INT _ 1; IF NOT accessAllowed THEN { PutString["\n\lSorry, you are not allowed to queue requests at this time."]; }; PutString[" --File-- "]; fileName _ GetStringToSpace[]; IF sep # '\n THEN { PutString[" --Copies-- "]; copies _ MIN[GetNumber[default: 1], CARDINAL.LAST]; }; IF sep # '\n THEN { PutString[" --Title-- "]; separator _ GetStringToCR[]; }; [fileName, bytes, createDate] _ IPPrinterRemoteStream.Lookup[fileName, BasicTime.nullGMT, user, password ! IPPrinterRemoteStream.Error => {IO.PutF1[stream, "Error: %g\n\l", IO.rope[expl]]; GOTO Bad}]; time _ IO.PutR[IO.time[BasicTime.Now[]]]; requestNumber _ IPPrinterQueue.QueueRequest[[fileName, time, user, password, separator, copies]]; IF requestNumber < 0 THEN {PutString["\n\lPrint queue full, request denied.\n\l"]; GOTO Bad}; PutString[IO.PutFR["\n\lPrint request %g queued for %g of %g (%g bytes)\n\l", IO.int[requestNumber], IO.rope[fileName], IO.rope[createDate], IO.int[bytes]]]; { msg: ROPE _ IO.PutFR["Version of %g; %g bytes.", IO.rope[createDate], IO.int[bytes]]; IPPrinterQueue.LogMessage[msg, requestNumber]; }; lastRequest _ requestNumber; EXITS Bad => NULL }; check => { requestNumber: INT; action: PROC [request: IPPrinterQueue.Request, status: IPPrinterQueue.RequestStatus] = { PutString[IO.PutFR1["%g ", IO.int[requestNumber]]]; IF status = notFound THEN { PutString[" not found.\n\l"]; } ELSE PutRequestStatus[request, status]; }; PutString[" --Request Number<"]; IF lastRequest # -1 THEN IO.Put[stream, IO.int[lastRequest]]; PutString[">-- "]; lastRequest _ requestNumber _ GetNumber[default: lastRequest]; IF requestNumber = -1 THEN {PutChar['\n]; PutChar['\l]} ELSE IF ABS[requestNumber] > NAT.LAST THEN action[[NIL,NIL,NIL,NIL,NIL,1], notFound] ELSE IPPrinterQueue.CheckRequest[requestNumber, action]; }; cancel => { requestNumber: INT; PutString[" --Request Number-- "]; requestNumber _ GetNumber[default: -1]; IF requestNumber = -1 THEN {PutChar['\n]; PutChar['\l]} ELSE { owner: ROPE _ NIL; reqSatus: IPPrinterQueue.RequestStatus _ notFound; action: PROC [request: IPPrinterQueue.Request, status: IPPrinterQueue.RequestStatus] = { reqSatus _ status; IF status # notFound THEN owner _ request.requestor; }; IPPrinterQueue.CheckRequest[requestNumber, action]; PutChar['\n]; PutChar['\l]; IF IPPrinterQueue.CancelRequest[requestNumber].ok THEN { msg: ROPE; IF reqSatus = printing THEN IPPrinterQueue.CancelReprint[]; msg _ IO.PutFR["Cancelled by %g", IO.rope[user]]; IPPrinterQueue.LogMessage[msg, requestNumber, IF Rope.IsEmpty[owner] THEN NIL ELSE owner]; PutString["Print request cancelled."]; } ELSE {PutString["No such request in queue."]}; PutChar['\n]; PutChar['\l]; }; }; listFiles => { { -- For enable ENABLE FS.Error => {PutString[error.explanation]; PutString["\n\l"]; GOTO Bad}; Proc: FS.NameProc = TRUSTED { -- [fullFName: ROPE] RETURNS [continue: BOOL]; PutString[fullFName]; PutString["\n\l"]; RETURN [TRUE]; }; fileNames: ROPE; cp: FS.ComponentPositions; PutString[" --Pattern-- "]; fileNames _ GetStringToSpace[]; IF fileNames = NIL THEN { PutString[" No file specified.\n\l"]; GOTO Bad; }; [fileNames, cp, ] _ FS.ExpandName[fileNames]; IF cp.server.length # 0 THEN { PutString[" Illegal file specified (not a local file).\n\l"]; GOTO Bad; }; FS.EnumerateForNames[fileNames, Proc]; }; -- For enable EXITS Bad => NULL }; listQueue => { action: PROC [requestNumber: CARDINAL, request: IPPrinterQueue.Request, status: IPPrinterQueue.RequestStatus] RETURNS [continue: BOOLEAN _ TRUE] = { IO.Put[stream, IO.int[requestNumber]]; PutChar[' ]; PutRequestStatus[request, status]; }; PutChar['\n]; PutChar['\l]; IF IPPrinterQueue.GetSuspended[] THEN { PutString[" *** Printing is suspended ***"]; PutChar['\n]; PutChar['\l]; }; IPPrinterQueue.EnumerateRequests[action]; }; messages => { action: PROC [message: ROPE] RETURNS [continue: BOOLEAN _ TRUE] = { IF Match[message] THEN { PutString[message]; PutChar['\n]; PutChar['\l]; }; }; key: ROPE _ NIL; Match: PROC [message: ROPE] RETURNS [BOOLEAN] = { IF Rope.IsEmpty[key] THEN RETURN [TRUE]; RETURN [Rope.Find[s1: message, s2: key, case: FALSE]>=0]; }; IF sep # '\n THEN { PutString[" --matching string-- "]; key _ GetStringToCR[]; }; PutChar['\n]; PutChar['\l]; IPPrinterQueue.EnumerateMessages[action]; }; start => { IF IPPrinterQueue.SetSuspended[FALSE].old = TRUE THEN { IPPrinterQueue.LogMessage["Printing started.",,user]; }; PutOK[]; }; stop => { IF IPPrinterQueue.SetSuspended[TRUE].old = FALSE THEN { IPPrinterQueue.LogMessage["Printing suspended.",,user]; }; PutOK[]; }; setPrinterParams => { sMargin, fMargin, newsMargin, newfMargin: CARDINAL; PutString[" \n\lGetting current parameters. "]; [sMargin, fMargin] _ IPPrinterMain.GetMargins[]; PutString[IO.PutFR[" -- Current Parameters: sMargin: %g, fMargin: %g \n\l", IO.card[sMargin], IO.card[fMargin]]]; PutString[" -- New sMargin<"]; IO.Put[stream, IO.card[sMargin]]; PutString[">-- "]; newsMargin _ GetNumber[default: sMargin]; IF newsMargin NOT IN [1..30] THEN { PutString[" --must be in [1..30]-- "]; PutChar['\n]; PutChar['\l]; RETURN; }; PutString[" -- New fMargin<"]; IO.Put[stream, IO.card[fMargin]]; PutString[">-- "]; newfMargin _ GetNumber[default: fMargin]; IF newfMargin NOT IN [10..43] THEN { PutString[" --must be in [10..43]-- "]; PutChar['\n]; PutChar['\l]; RETURN; }; PutString[" -- You sure? -- "]; IF EasyConfirm[] THEN { PutOK[]; IPPrinterMain.SetMargins[newsMargin, newfMargin]; [sMargin, fMargin] _ IPPrinterMain.GetMargins[]; IPPrinterQueue.LogMessage[ IO.PutFR["Setting margins to: sMargin: %g, fMargin: %g", IO.card[sMargin], IO.card[fMargin]],,user]; }; }; resetQueue => { PutString[" --Do you really want to reset the entire queue?-- "]; IF Confirm[] THEN { PutOK[]; IPPrinterQueue.Reset[]; IPPrinterQueue.LogMessage["Queue reset",,user]; }; }; help, illegal => { PutString["\n\lValid commands are: "]; FOR cmd: CommandCode IN [login..quit] DO candidate: ROPE _ commandTable[cmd]; PutString[candidate]; IF cmd # quit THEN PutString[", "]; ENDLOOP; PutChar['\n]; PutChar['\l]; }; quit => {loggedIn _ FALSE; PutChar['\n]; PutChar['\l]; SendNow[]; quitting _ TRUE}; watch => { stats: WatchStats.WatchStatsRecord _ WatchStats.GetWatchStats[]; IO.PutF[stream, " Free disk:\t%g\n\l", IO.int[stats.diskFree]]; IO.PutF[stream, " Free mds:\t%g\n\l", IO.int[stats.mdsFree]]; IO.PutF[stream, " Free gfi:\t%g\n\l", IO.int[stats.gfiFree]]; IO.PutF[stream, " Free VM:\t%g\n\l", IO.int[stats.vmFree]]; IO.PutF[stream, " VM run:\t%g\n\l", IO.int[stats.vmRun]]; IO.PutF[stream, " Load:\t%g\n\l", IO.real[stats.cpuLoad]]; flushed _ FALSE; }; wait => { requestNumber: INT _ lastRequest; requestStatus: IPPrinterQueue.RequestStatus _ waiting; action: PROC [request: IPPrinterQueue.Request, status: IPPrinterQueue.RequestStatus] = { requestStatus _ status; }; toldUserThatItIsPrinting: BOOLEAN _ FALSE; DoPrintingMsg: PROC = { IF requestStatus = printing AND NOT toldUserThatItIsPrinting THEN { PutString["Printing..."]; toldUserThatItIsPrinting _ TRUE; }; }; IF sep # '\n THEN { PutString[" --Request Number<"]; IF lastRequest # -1 THEN IO.Put[stream, IO.int[lastRequest]]; PutString[">-- "]; lastRequest _ requestNumber _ GetNumber[default: lastRequest]; }; PutChar['\n]; PutChar['\l]; IF requestNumber IN [0..NAT.LAST] THEN { IPPrinterQueue.CheckRequest[requestNumber, action]; IF requestStatus = waiting THEN { PutString["Waiting..."]; }; DoPrintingMsg[]; UNTIL requestStatus = notFound OR requestStatus = canceled DO SendNow[]; Process.Pause[Process.MsecToTicks[10000]]; IPPrinterQueue.CheckRequest[requestNumber, action]; DoPrintingMsg[]; PutChar['.]; ENDLOOP; PutString["Done.\n\l"]; }; }; ambiguous => { PutString["\n\lAmbiguous command (type Help for help)\n\l"]; }; ENDCASE => ERROR; }; }; IO.PutF1[stream, "\n\l%g\n\l", IO.rope[helloMsg]]; UNTIL loggedIn OR quitting DO DoCommand[! DelHit => CONTINUE] ENDLOOP; IF loggedIn THEN IPPrinterQueue.LogMessage["Login", , user]; WHILE loggedIn DO DoCommand[! DelHit => CONTINUE] ENDLOOP; IPPrinterQueue.LogMessage["Logout", , user]; EXITS Closing => { IF loggedIn THEN IPPrinterQueue.LogMessage["Logged off due to remote close", , user]; }; TimeOut => { IF loggedIn THEN IPPrinterQueue.LogMessage["Logged off due to TimeOut", , user]; }; }; }; monthName: ARRAY BasicTime.MonthOfYear OF Rope.ROPE ~ ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???"]; TimeToRope: PROC [time: BasicTime.GMT _ BasicTime.nullGMT] RETURNS [r: ROPE] = { unpacked: BasicTime.Unpacked _ BasicTime.Unpack[(IF time = BasicTime.nullGMT THEN BasicTime.Now[] ELSE time)]; stream: IO.STREAM _ IO.ROS[]; zoneChar: CHAR _ ' ; IO.PutF[stream, "%2g-%g-%02g ", IO.int[unpacked.day], IO.rope[monthName[unpacked.month]], IO.int[unpacked.year MOD 100]]; IO.PutF[stream, "%2g:%02g:%02g", IO.int[unpacked.hour], IO.int[unpacked.minute], IO.int[unpacked.second]]; r _ IO.RopeFromROS[stream]; }; abortCurrent: REF BOOL _ NEW[BOOL _ FALSE]; logStream: IO.STREAM _ NIL; MakeLogViewer: PROC = { IF logStream # NIL THEN RETURN; <> logStream _ SimpleTerminal.TurnOn[helloMsg].out; IPPrinterQueue.RegisterTTY[logStream]; }; NewConnection: PupStream.ListenerProc = TRUSTED { otherGuy: Rope.ROPE _ PupName.HisName[remote]; TalkWithUser[stream ! PupStream.StreamClosing => CONTINUE]; IO.Close[stream]; }; Init: PROC = TRUSTED { MakeLogViewer[]; pupListener _ PupStream.CreateListener[ local: PupWKS.telnet, worker: NewConnection, getTimeout: 300000, putTimeout: 300000 ]; }; pupListener: PupStream.Listener _ NIL; Init[]; }.