[comp.lang.pascal] TP and security menu system

defaria@hpclapd.HP.COM (Andy DeFaria) (06/12/90)

I have two questions with regards to Turbo Pascal.   I am trying to write a
simple menu type system.

1) How to you turn  off  echo so that I can  read in  a password without it
showing up on the screen?

2) What is  the best way  to safeguard the program  so that a  ^C  does not
abort it?

Thanks.....

gt3070b@prism.gatech.EDU (Jeff Watkins) (06/12/90)

I was going to redirect you to my unit at attlab.gatech.edu of the same name,
but then I realized that I had it here in a directory and might as well make
the mods since I know the code and there are no docs yet (wird schreiben).
The addition of the echo (undefined initially) flag is the key.

DOC entry follows:
	Readin is loop called until the return of InputDone is true or 
the return of InputAbort is true.  The params are self explanatory.
Echo sets whether echo is enabled.

ciao
jeff

ps set the CheckBreak variable in versions 4+
-----------CUT-HERE----------

unit Input;
interface
var
	echo:	boolean;

function  ReadKey:char;
Procedure ClearBuffer;
function  ReadCursorKey:char;
procedure ReturnKey(C:char);
function  Keypressed:boolean;
procedure ReadIn(Var InString;l,v,x,y:integer);
function  InputDone:boolean;
function  FieldChange:boolean;
function  InputAbort:boolean;

var
	ScanChar	: char;

implementation
uses crt,misc,mouse;
var
 CharReady		: boolean;
 CharBuf		: char;
 __InputDone	: boolean;
 __InputAbort	: boolean;
 __FieldChange	: boolean;
 Displayed		: boolean;

Function ReadCursorKey:char;
var
 c:char;
begin
 ReadCursorKey:=#0;
 if keypressed then
 begin
  c:=ReadKey;
  if (c=#0) and Keypressed then
   ReadCursorKey:=readkey
  else
   ReturnKey(c);
 end;
end;

Function ReadKey:char;
begin
 if CharReady then
 begin
  ReadKey:=CharBuf;
  CharReady:=false;
 end
 else
  ReadKey:=crt.Readkey;
end;

Procedure ReturnKey(c:char);
begin
 CharReady:=true;
 CharBuf:=c;
end;

Function KeyPressed:boolean;
begin
 if CharReady or crt.keypressed then
  Keypressed:=true
 else
  Keypressed:=false;
end;

Procedure ClearBuffer;
var
	c:char;
begin
	While Keypressed do
		c:=readkey;
end;

Procedure ReadIn(var InString;l,v,x,y:integer);
var
 c:char;
 S:^String;
begin
 S:=@InString;
 if not displayed and echo then
 begin
	if Length(S^)>v then
	begin
		gotoxy(x,y);
		write(size(copy(S^,1+length(S^)-V,v),v));
	end
	else
	begin
		gotoxy(x,y);
		write(size(s^,V));
		gotoxy(x+length(s^),y);
	end;
	displayed:=true;
 end;
 if Keypressed then
  C:=Readkey
 else
  exit;
 if c=#8 then
  S^:=copy(S^,1,length(S^)-1);
 if c=#0 then
 begin
	c:=readkey;
	ScanChar:=ConvertScan(C);
	c:=#0;
 end
 else
	ScanChar:=#0;
 if c=#13 then __InputDone:=true;
 if c=^I then __FieldChange:=true;
 if c=#27 then __InputAbort:=true;
 if (c in [#13,^I,#27]) then
	Displayed:=false;
 if (c in [' '..#255]) and (length(s^)<l) then
   S^:=S^+c;
 if echo then
 begin
  hidem;
  if length(S^)>V then
  begin
	gotoxy(x,y);
	write(size(copy(S^,1+length(S^)-V,v),v))
  end
  else
  begin
	gotoxy(x,y);
	write(size(S^,v));
	gotoxy(x+length(s^),y);
  end;
  showm;
 end;
 CursorOn;
 if not (c in [#8,^I,#0,#13,' '..#255]) then
end;

Function InputDone:boolean;
begin
 InputDone:=__InputDone;
 __InputDone:=false;
end;

Function InputAbort:boolean;
begin
	InputAbort:=__InputAbort;
	__InputAbort:=false;
end;

Function FieldChange:boolean;
begin
	FieldChange:=__FieldChange;
	__FieldChange:=false;
end;

begin
 CharReady:=false;
 __InputDone:=false;
 __FieldChange:=false;
 __InputAbort:=false;
 CharBuf:=#0;
 ScanChar:=#0;
 Displayed:=false;
end.
-- 
Jeff Watkins  gt3070b@prism.gatech.edu
"All opinions are mine... so don't even think of keeping some to yourself!"

lowey@herald.usask.ca (Kevin Lowey) (06/14/90)

From article <950034@hpclapd.HP.COM>, by defaria@hpclapd.HP.COM (Andy DeFaria):
> I have two questions with regards to Turbo Pascal.   I am trying to write a
> simple menu type system.
> 
> 1) How to you turn  off  echo so that I can  read in  a password without it
> showing up on the screen?

Use the READKEY function in the CRT unit.  For example:

  string_var := '';
  While keypressed do
    string_var := concat (string_var,readkey);

> 2) What is  the best way  to safeguard the program  so that a  ^C  does not
> abort it?

  There is a Turbo Pascal built in variable called CHECKBREAK (or in versions
before TP4.0, CBREAK).  To turn off Control-C checking, assign FALSE to this
value.  CHECKBREAK := FALSE;   This lets you turn off CONTROL-C checking for
vital parts of the program, but leave it available in other parts.

-- Kevin Lowey