-- Copyright (C) 1983  by Xerox Corporation. All rights reserved. 
-- OscarAdm.mesa, HGM, 21-Dec-83  0:49:46

DIRECTORY
  Ascii USING [BS, CR],
  Inline USING [LowHalf],
  OthelloDefs USING [],
  OthelloForgot USING [],
  String USING [AppendLongNumber, AppendNumber],
  TTY USING [
    BlinkDisplay, Create, Destroy, GetChar, Handle,
    nullHandle, PutChar, ResetUserAbort, UserAbort];

OscarAdm: MONITOR
  IMPORTS Inline, String, TTY
  EXPORTS OthelloDefs, OthelloForgot =
  BEGIN

  ttyHandle: TTY.Handle ← TTY.Create["Oscar"L];  -- Hack to see startup printout
  
  WaitForConnection: PUBLIC PROCEDURE =
    BEGIN
    temp: TTY.Handle ← GrabIt[];
    GrabIt: ENTRY PROCEDURE RETURNS [it: TTY.Handle] =
      BEGIN
      it ← ttyHandle;
      ttyHandle ← TTY.nullHandle;
      END;
    IF temp # TTY.nullHandle THEN TTY.Destroy[temp];
    ttyHandle ← TTY.Create["Oscar"L];
    END;

  BlinkDisplay: PUBLIC PROC = {
    TTY.BlinkDisplay[ttyHandle]};
    
  CheckUserAbort: PUBLIC PROC = {
    IF TTY.UserAbort[ttyHandle] THEN {ResetAbort[]; ERROR ABORTED}};
    
  EraseTTYChar: PUBLIC PROC [c: CHARACTER] = {
    SELECT c FROM IN [' ..'~] => NULL; Ascii.CR => RETURN; ENDCASE => EraseTTYChar[' ];
    WriteChar[Ascii.BS]; WriteChar[' ]; WriteChar[Ascii.BS]};

  NewLine: PUBLIC PROC = {WriteChar[Ascii.CR]};

  ReadChar: PUBLIC PROC RETURNS [c: CHARACTER] = {RETURN[TTY.GetChar[ttyHandle]]};

  ResetAbort: PUBLIC PROC =
    BEGIN
    TTY.ResetUserAbort[ttyHandle];
    END;
  
  WriteChar: PUBLIC ENTRY PROC [c: CHARACTER] =
    BEGIN
    IF ttyHandle = TTY.nullHandle THEN RETURN;
    TTY.PutChar[ttyHandle, c ! ABORTED => CONTINUE];
    END;

  WriteFixedWidthNumber: PUBLIC PROCEDURE [
    x: LONG CARDINAL, count: CARDINAL, base: CARDINAL ← 10] = {
    WFD: PROCEDURE [x: LONG CARDINAL, c: CARDINAL] = {
      IF c = count THEN RETURN;
      WFD[x/base, c + 1];
      WriteChar[IF c = 0 OR x # 0 THEN Inline.LowHalf[x MOD base] + '0 ELSE ' ]};
    WFD[x, 0]};

  WriteLongNumber: PUBLIC PROCEDURE [num: LONG CARDINAL] = {
    s: STRING ← [40];
    String.AppendLongNumber[s, num, 10];
    WriteString[s]};

  WriteLongOctal: PUBLIC PROCEDURE [num: LONG CARDINAL] = {
    s: STRING ← [40];
    String.AppendLongNumber[s, num, 8];
    WriteString[s]};

  WriteOctal: PUBLIC PROCEDURE [num: CARDINAL] = {
    s: STRING ← [40];
    String.AppendNumber[s, num, 8];
    WriteString[s]};

  WriteLine: PUBLIC PROC [s: LONG STRING] = {WriteString[s]; NewLine[]};

  WriteString: PUBLIC PROC [s: LONG STRING] =
    BEGIN
    i: CARDINAL;
    IF s = NIL THEN RETURN;
    FOR i ← 0, i + 1 WHILE i < s.length DO
      IF s[i] = '\\ AND s[i + 1] = 'r THEN {
      NewLine[]; i ← i + 1} ELSE WriteChar[s[i]];
      ENDLOOP;
    END;
  
  END..