home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / PASCAL / CONSOLE.ZIP / CONSOLE.SRC < prev    next >
Encoding:
Text File  |  1989-10-19  |  7.3 KB  |  209 lines

  1. (*  VERSION 0011 *)
  2.  
  3.  
  4.  
  5. MODULE cons;
  6.  
  7.  
  8. (*******************************************************************)
  9. (*           (C) Copyright 1982 by Patrick Wallace                 *)
  10. (* Use of this program module for commercial purposes without the  *)
  11. (* author's prior written consent is expressly forbidden.          *)
  12. (*     Date last update : 1/21/83                                  *)
  13. (*         [to add ^E,^X,^J,^K,^Q,and ESC to stop characters]      *)
  14. (*******************************************************************)
  15.  
  16. EXTERNAL FUNCTION @BDOS(func : INTEGER; parm : WORD) : INTEGER;
  17.  
  18.  
  19. PROCEDURE console(VAR entrych:CHAR; VAR entry:STRING; VAR position:INTEGER;
  20.                   charcount, len : INTEGER);
  21.  
  22. CONST
  23.    contrs = $13;       contrd = $04;          contra = $01;
  24.    backspc = $08;       forspc = $06;           contrf = $06;
  25.    contrg = $07;        tab = $09;              del = $7F;
  26.    fwd = $0C;           nsert = $16;
  27.  
  28. TYPE
  29.   letter = SET OF CHAR;         way = (fwr, bak);
  30.   
  31.  
  32. VAR
  33. dummy : INTEGER;                     ch : BYTE;
  34. ltrs : letter;                     stop : SET OF BYTE;
  35.  
  36.  
  37.  
  38.        PROCEDURE getch(VAR CH : BYTE);
  39.  
  40.        BEGIN
  41.          REPEAT
  42.            ch := @BDOS(6, WRD($FF)) & $7F;  {direct console input}
  43.          UNTIL ORD(ch) <> 0;            {until character is typed}
  44.        END;
  45.  
  46.  
  47.        PROCEDURE emit(ch : BYTE);
  48.  
  49.        BEGIN
  50.          WRITE(CHR(ch))
  51.        END;
  52.  
  53.  
  54.        PROCEDURE go(VAR position : INTEGER;
  55.                       dir : way;
  56.                       num : INTEGER);
  57.  
  58.        VAR
  59.                loop : INTEGER;         ch : BYTE;      offset : INTEGER;
  60.  
  61.        BEGIN
  62.                IF dir = fwr THEN
  63.                    BEGIN
  64.                      offset := 1;
  65.                      ch := forspc
  66.                    END
  67.                ELSE 
  68.                    BEGIN
  69.                      offset := - 1;
  70.                      ch := backspc
  71.                    END;
  72.  
  73.                FOR loop := 1 TO num DO
  74.                        BEGIN
  75.                              WRITE(CHR(ch));
  76.                              position := position + offset
  77.                        END
  78.        END; {go}
  79.  
  80.  
  81. BEGIN {console}
  82.  
  83.   IF charcount > len THEN charcount := len;   {charcount = length(entry) }
  84.  
  85.   IF position > charcount + 1 THEN
  86.      BEGIN
  87.        FOR dummy := position DOWNTO charcount + 2 DO
  88.           WRITE(CHR(backspc));  {make screen agree with us}
  89.  
  90.        position := charcount + 1;
  91.      END;
  92.  
  93.   ltrs := [' ' .. 'z'];
  94.   stop := [5,10,11,13,17,24,27]; {^E,^J,^K,<CR>,^Q,^X,ESC}
  95.   getch(ch);     {ignore entrych and get our own}
  96.  
  97.   WHILE NOT (ch IN stop) DO
  98.     BEGIN
  99.       IF ch IN ltrs THEN
  100.            IF position <= len THEN
  101.              BEGIN
  102.                entry[position] := ch;
  103.  
  104.                IF position = charcount +1 THEN {we're at string's growing end}
  105.                    charcount := charcount + 1;
  106.  
  107.                position := position + 1;
  108.                emit(ch);
  109.              END
  110.            ELSE
  111.              emit(contrg) {beep}
  112.       ELSE
  113.          BEGIN
  114.            CASE ch OF
  115.  
  116.               backspc, contrs: IF position >1 THEN {there's room to backspc}
  117.                                   go(position, bak, 1);
  118.  
  119.               fwd, contrd    : IF position <= charcount THEN {there's room}
  120.                                   go(position, fwr, 1);
  121.  
  122.               contra         :  BEGIN
  123.                                   IF position > 1 THEN
  124.                                         go(position, bak, 1);
  125.  
  126.                                   WHILE (position > 1)  AND
  127.                                    ((entry[position] = ' ') OR
  128.                                    (entry[position -1] <> ' ')) DO
  129.                                         go(position, bak, 1)
  130.                                 END;
  131.  
  132.               contrf         :  BEGIN
  133.                                   IF position <= charcount THEN
  134.                                         go(position, fwr, 1);
  135.  
  136.                                   WHILE (position < charcount)  AND
  137.                                      ((entry[position] = ' ') OR
  138.                                         (entry[position-1] <> ' ')) DO
  139.                                             go(position, fwr, 1);
  140.                                 END;
  141.  
  142.               del, contrg    :  BEGIN
  143.                                   IF (ch = del) AND (position > 1) THEN
  144.                                       go(position, bak, 1);
  145.  
  146.                                   IF (position <= charcount) AND
  147.                                       (charcount > 0) THEN
  148.                                    BEGIN
  149.                                      entry[0] := CHR(charcount);{to del-copy}
  150.                                      DELETE(entry, position, 1);
  151.  
  152.                                      IF position < charcount THEN
  153.                                         BEGIN
  154.                                           WRITE(COPY(entry, position,
  155.                                            LENGTH(entry) - position + 1),' ',
  156.                                            CHR(backspc));
  157.                                           go(dummy, bak, LENGTH(entry) -
  158.                                                 position +1)
  159.                                         END
  160.                                      ELSE
  161.                                            WRITE(' ', CHR(backspc));
  162.                                      charcount := charcount -1
  163.                                     END; {if}
  164.                                 END;
  165.  
  166.               tab             :  BEGIN
  167.                                    IF position + 5 <= charcount THEN
  168.                                        go(position, fwr, 5)
  169.                                    ELSE
  170.                                       emit(contrg); {beep!}
  171.                                  END;
  172.  
  173.               nsert          :  BEGIN
  174.                                  IF (charcount < len) AND
  175.                                     (charcount > position) THEN
  176.                                   BEGIN
  177.                                     MOVERIGHT(entry[position],
  178.                                      entry[position+1],charcount-position+1);
  179.                                     entry[position] := ' ';
  180.                                     charcount := SUCC(charcount);
  181.                                     entry[0] := CHR(charcount);
  182.                                     WRITE(COPY(entry, position,
  183.                                            LENGTH(entry) - position + 1));
  184.                                     go(dummy, bak, LENGTH(entry) -
  185.                                            position + 1)
  186.                                    END
  187.                                   ELSE emit(contrg); {beep}
  188.                                  END;
  189.  
  190.  
  191.            END  {CASE}
  192.  
  193.         END; {ELSE}
  194.  
  195.       getch(ch);
  196.    END; {while}
  197.  
  198.    IF ORD(ch)=24 THEN
  199.       ch := 10             {convert ^X to return as ^J}
  200.    ELSE IF ORD(ch)=5 THEN
  201.       ch := 11;            {convert ^E to return as ^K}
  202.  
  203.    entry[0] := CHR(charcount); {set length of string}
  204.    entrych := CHR(ch); {report out last character}
  205. END; {console}
  206.  
  207. MODEND.
  208.  
  209.  ee¬âeERSIONen Nà3 ■;º