MODULE T15;

IMPORT IO, SYSTEM, NumberIO, Strings, ASCII, STextIO;
FROM Strings IMPORT Concat;

VAR
    str     : ARRAY[0..15] OF CHAR;
    i       : CARDINAL;
    c       : CHAR;
    cc      : Strings.String1;
    cursorposStr : ARRAY[0..15] OF CHAR;

PROCEDURE WriteString( str : ARRAY OF CHAR);

    VAR
        i         : CARDINAL;            

    BEGIN
        FOR i := 1 TO Strings.Length(str) DO
            IO.Write(str[i])
        END;
    END WriteString;



PROCEDURE Card2Str ( x : CARDINAL; VAR str : ARRAY OF CHAR);

BEGIN
IF x < 10 THEN
        NumberIO.CardToStr(x,1,str);
    ELSIF x < 100 THEN
        NumberIO.CardToStr(x,2,str);
    ELSE
        NumberIO.CardToStr(x,3,str);
    END;
END Card2Str;

PROCEDURE cursorpos(x,y : CARDINAL);

VAR
    tmpString : ARRAY[0..10] OF CHAR;
    tmpString1: ARRAY[0..5] OF CHAR;
    tmpString2: Strings.String1;

BEGIN
    tmpString := "";
    Concat(tmpString,cursorposStr, tmpString);
    tmpString1 := "";
    Card2Str(y,tmpString1);
    Concat(tmpString,tmpString1, tmpString);
    tmpString2[0] := ";";
    Concat(tmpString, tmpString2,tmpString);
    tmpString1 := "";
    Card2Str(x,tmpString1);
    Concat(tmpString,tmpString1, tmpString);
    tmpString2[0] := "H";
    Concat(tmpString, tmpString2,tmpString);
    WriteString(tmpString);
END cursorpos;

BEGIN
    cursorposStr := "";
    Concat(cursorposStr,ASCII.esc,cursorposStr);
    Concat(cursorposStr,"[",cursorposStr);

    cursorpos(35,50);

        (* quering the position *)
    str := "";
    Strings.Concat(str,ASCII.esc,str);
    Strings.Concat(str,"[6n", str);
    (* PROCEDURE UnBufferedMode (fd: INTEGER; input: BOOLEAN) ; *)
    IO.UnBufferedMode(0,TRUE);
    IO.UnBufferedMode(1,TRUE);
    (* PROCEDURE Read (VAR ch: CHAR) ;
    PROCEDURE Write (ch: CHAR) ; *)
    (* sending request *)
    FOR i := 0 TO 3 DO
        IO.Write(str[i])
    END;
    (* getting answer *)
    str := "";
    REPEAT
        IO.Read(c);
        cc[0] := c;
        Strings.Concat(str,cc,str);
    UNTIL c = "R";

    (* changing the escape char to ? to avoid interpretation by the terminal*)
    str[0] := "?";

    IO.BufferedMode(0,TRUE);
    IO.BufferedMode(1,TRUE);

    NumberIO.WriteCard(Strings.Length(str),5);
    STextIO.WriteLn;
    STextIO.WriteString(str);
END T15.