-- StateCold.mesa  last edit: Sandman  July 18, 1980  11:44 AM
--		    last edit: Bruce  26-Sep-80 13:04:07

DIRECTORY
  Commands USING [DispatchChar, StringId],
  DebugOps USING [fileSW],
  Event USING [Notify],
  DHeap USING [FreeEverything, Zero],
  HeapString USING [Replace],
  Init USING [],
  Nub USING [DoCommand],
  Profile USING [],
  State USING [Context, GSHandle, GState, Handle, SetParse, Stack, StringArray, StringProc, Strings],
  Storage USING [Free, FreeString, Node, String],
  TajoMisc USING [],
  TextSource USING [Handle],
  TextSW USING [Adjust, GetSource],
  ToolWindow USING [AdjustProcType, TransitionProcType],
  UserInput USING [SetStringIn];

StateCold: PROGRAM
  IMPORTS
    Commands, DebugOps, Event, DHeap, HeapString, Nub, State,
    Storage, TextSW, UserInput
  EXPORTS Init, Profile, State, TajoMisc =
  BEGIN OPEN State;

  debugging: PUBLIC BOOLEAN ← FALSE;
  librarian: PUBLIC STRING ← NIL;
  registry: PUBLIC STRING ← NIL;
  userName: PUBLIC STRING;
  userPassword: PUBLIC STRING;
  top: PUBLIC Handle ← NIL;
  strings: PUBLIC Strings;
  gstate: GState ← [];
  gs: PUBLIC GSHandle ← @gstate;

  OutOfState: PUBLIC ERROR = CODE;

  Adjust: PUBLIC ToolWindow.AdjustProcType =
    BEGIN
    IF when = after THEN
      TextSW.Adjust[DebugOps.fileSW, [[0,0], box.dims], when];
    END;

  Transition: PUBLIC ToolWindow.TransitionProcType = {
    source: TextSource.Handle;
    IF new # tiny THEN RETURN;
    source ← TextSW.GetSource[DebugOps.fileSW];
    [] ← source.actOn[source,truncate]};

  -- TajoMisc
  SetDebugging: PUBLIC PROC [debug: BOOLEAN] = 
    BEGIN
    debugging ← debug;
    Event.Notify[setDefaults];
    END;
    
  SetLibrarian: PUBLIC PROCEDURE [s: STRING] =
    BEGIN
    HeapString.Replace[to: @librarian, from: s];
    Event.Notify[setDefaults];
    END;
    
  SetRegistry: PUBLIC PROCEDURE [s: STRING] =
    BEGIN
    HeapString.Replace[to: @registry, from: s];
    Event.Notify[setDefaults];
    END;
    
  -- strings
  SetString: PUBLIC PROCEDURE [id: Commands.StringId, val: STRING] =
    BEGIN
    IF strings[id] # NIL THEN Storage.FreeString[strings[id]];
    strings[id] ← val;
    END;

  -- context
  Push: PUBLIC PROCEDURE =
    BEGIN
    temp: Handle ← MakeContext[];
    temp.prev ← top;
    IF top.prev = NIL THEN DHeap.FreeEverything[];
    gs.level ← gs.level+1;
    top←temp;
    top.howSet ← state;
    END;

  Pop: PUBLIC PROCEDURE =
    BEGIN
    temp: Handle;
    i, next: POINTER TO Stack;
    IF top = NIL THEN ERROR OutOfState;
    temp ← top.prev;
    FOR i ← top.fooStack, next UNTIL i = NIL DO
      next ← i.link;
      Storage.Free[i];
      ENDLOOP;
    DHeap.FreeEverything[];
    Storage.Free[top];
    top ← temp;
    gs.level ← gs.level-1;
    END;

  ResetParse: PUBLIC PROC =
    {SetParse[IF gs.inNub THEN Nub.DoCommand ELSE Commands.DispatchChar]};

  MakeContext: PROCEDURE RETURNS [h: Handle] =
    BEGIN
    h ← Storage.Node[SIZE[Context]];
    h↑ ← [];
    END;

  Go: PUBLIC PROCEDURE =
    BEGIN
    top ← MakeContext[];
    [] ← UserInput.SetStringIn[DebugOps.fileSW, State.StringProc, FALSE]
    END;

  OnYourMark: PUBLIC PROCEDURE =
    BEGIN
    p: POINTER ← Storage.Node[SIZE[StringArray]];
    strings ← DESCRIPTOR[p,SIZE[StringArray]];
    DHeap.Zero[p,SIZE[StringArray]];
    gs.parse ← Commands.DispatchChar;
    strings[user] ← userName ← Storage.String[30];
    strings[password] ← userPassword ← Storage.String[30];
    END;

  END.