
(********************************************************************)
(*                                                                  *)
(*  echo.s7i      Filter file which generates an echo of the input  *)
(*  Copyright (C) 1992, 1993, 1994, 2005  Thomas Mertes             *)
(*                                                                  *)
(*  This file is part of the Seed7 Runtime Library.                 *)
(*                                                                  *)
(*  The Seed7 Runtime Library is free software; you can             *)
(*  redistribute it and/or modify it under the terms of the GNU     *)
(*  Lesser General Public License as published by the Free Software *)
(*  Foundation; either version 2.1 of the License, or (at your      *)
(*  option) any later version.                                      *)
(*                                                                  *)
(*  The Seed7 Runtime Library is distributed in the hope that it    *)
(*  will be useful, but WITHOUT ANY WARRANTY; without even the      *)
(*  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *)
(*  PURPOSE.  See the GNU Lesser General Public License for more    *)
(*  details.                                                        *)
(*                                                                  *)
(*  You should have received a copy of the GNU Lesser General       *)
(*  Public License along with this program; if not, write to the    *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


const type: echo_file is sub null_file struct
    var file: in_file is STD_NULL;
    var file: out_file is STD_NULL;
    var integer: in_pos is 1;
  end struct;


const func echo_file: open_echo (ref file: in_fil, ref file: out_fil) is func
  result
    var echo_file: echo_fil is echo_file.value;
  begin
    echo_fil.in_file := in_fil;
    echo_fil.out_file := out_fil;
    echo_fil.in_pos := 1;
  end func;


const func char: getc (inout echo_file: echo_fil) is func
  result
    var char: ch is ' ';
  local
    var integer: number is 0;
  begin
    repeat
      flush(echo_fil.out_file);
(*    cursor_on(echo_fil.out_file); *)
      ch := getc(echo_fil.in_file);
(*    cursor_off(echo_fil.out_file); *)
      if ch >= ' ' and ch <= '~' then
        incr(echo_fil.in_pos);
        write(echo_fil.out_file, ch);
      elsif ch = '\n' then
        echo_fil.in_pos := 1;
        writeln(echo_fil.out_file);
      elsif ch = '\b' then
        if echo_fil.in_pos > 1 then
          decr(echo_fil.in_pos);
          backSpace(echo_fil.out_file);
        end if;
      elsif ch = '\C' or ch = '\T' then
        write(echo_fil.out_file, " terminate (y/n)? ");
        flush(echo_fil.out_file);
        if lower(getc(echo_fil.in_file)) = 'y' then
          writeln(echo_fil.out_file, "yes");
          writeln(echo_fil.out_file);
          writeln(echo_fil.out_file, "*** PROGRAM TERMINATED BY USER");
          exit(PROGRAM);
        else
          for number range 1 to 18 do
            backSpace(echo_fil.out_file);
          end for;
        end if;
      else
        incr(echo_fil.in_pos);
        ch := '?';
        write(echo_fil.out_file, ch);
      end if;
    until ch <> '\C' and ch <> '\T';
  end func;


const func string: gets (inout echo_file: echo_fil, in integer: length) is func
  result
    var string: stri is "";
  local
    var integer: number is 0;
  begin
    stri := "";
    for number range 1 to length do
      stri &:= str(getc(echo_fil));
    end for;
  end func;

