-- FTPTrace.mesa - last edit:
-- MAS May 19, 1980  6:09 PM  
-- HGM July 31, 1980  5:28 PM  

-- Copyright  Xerox Corporation 1979, 1980

DIRECTORY
  FTPDefs,
  FTPPrivateDefs,
  String USING [AppendChar, AppendDecimal];

FTPTrace: PROGRAM IMPORTS String EXPORTS FTPDefs, FTPPrivateDefs SHARES FTPDefs =
  BEGIN OPEN FTPDefs, FTPPrivateDefs;


  FTPEnableTrace: PUBLIC PROCEDURE [
    ftpuser: FTPUser, writeString: PROCEDURE [STRING]] =
    BEGIN OPEN ftpuser;
    -- enable tracing of both ftp and mtp protocol exchanges
    ftper.tracing ← mtper.tracing ← TRUE;
    ftper.directionOfLastTrace ← mtper.directionOfLastTrace ← in;
    -- note client's writeString procedure
    ftper.writeString ← mtper.writeString ← writeString;
    END;

  FTPDisableTrace: PUBLIC PROCEDURE [ftpuser: FTPUser] =
    BEGIN OPEN ftpuser;
    -- disable tracing of both ftp and mtp protocol exchanges
    ftper.tracing ← mtper.tracing ← FALSE;
    END;

  TraceOut: PUBLIC PROCEDURE [ftper: FTPer, mark: Byte] =
    BEGIN OPEN ftper;
    IF directionOfLastTrace = in THEN
      BEGIN
      TraceCharacter[ftper, outgoingTraceIndicator];
      directionOfLastTrace ← out;
      END;
    TraceCharacter[ftper, LOOPHOLE[mark]];
    END;

  TraceIn: PUBLIC PROCEDURE [ftper: FTPer] =
    BEGIN OPEN ftper;
    IF directionOfLastTrace = out THEN
      BEGIN
      TraceCharacter[ftper, incomingTraceIndicator];
      directionOfLastTrace ← in;
      END;
    END;

  TraceString: PUBLIC PROCEDURE [ftper: FTPer, string: STRING] =
    BEGIN OPEN ftper;
    -- Note:  Assumes tracing enabled.
    -- local constants
    traceString: STRING = [maxStringLength];
    -- local variables
    i: CARDINAL;
    -- construct trace string
    FOR i IN [0..string.length) DO
      VerbalizeCharacter[traceString, string[i]]; ENDLOOP;
    -- output trace string
    writeString[traceString];
    END;

  TraceCharacter: PUBLIC PROCEDURE [ftper: FTPer, character: CHARACTER] =
    BEGIN OPEN ftper;
    -- Note:  Assumes tracing enabled.
    -- local constants
    traceString: STRING = [maxStringLength];
    -- construct trace string
    VerbalizeCharacter[traceString, character];
    -- output trace string
    writeString[traceString];
    END;

  VerbalizeCharacter: PROCEDURE [string: STRING, character: CHARACTER] =
    BEGIN
    -- Note:  Outputs decimal value of nonprinting characters.
    -- verbalize character
    SELECT character FROM
      IN [' ..'←], IN ['a..'~] => String.AppendChar[string, character];
      ENDCASE =>
        BEGIN
        String.AppendChar[string, traceByteLeftDelimiter];
        String.AppendDecimal[string, LOOPHOLE[character, CARDINAL]];
        String.AppendChar[string, traceByteRightDelimiter];
        END;
    END;

  END. -- of FTPTrace