{$symtab-,$linesize:131,$pagesize:86,
$title:'KEYPARSE.PAS -- Parse the input keys'}
{	COPYRIGHT @ 1982
	Jim Holtman and Eric Holtman
	35 Dogwood Trail
	Randolph, NJ 07869
	(201) 361-3395
}
module keyparse;
const
    Alt_C = 46; {Alternate C - log file switch}
    Alt_D = 32; {Alternate D - Dump the file to Comm line}
    Alt_F = 33; {Alternate F - turn on line edit mode}
    Alt_E = 18; {toggle the vi_cursor mode}
    Alt_H = 35; { Help screen }
    Alt_K = 37; { Alternate login functions }
    Alt_L = 38; { Executing login scripts }
    Alt_R = 19; {XModem Receive}
    Alt_T = 20; {XModem Transmit}
    Alt_V = 47; {Ventel functions entry point }
    F1 = 59; {function key 1}
    F2 = 60; {function key 2}
    F3 = 61; {function key 3}
    F5 = 63; {function key 5, send an "l" (for reading notes) }
    F6 = 64; {function key 6, send a "j" (for reading notes) }
    F8 = 66; {function key 8}
    Alt_F1 = 104; {Alternate F1}
    Alt_F2 = 105; {Alternate F2}
    Alt_F3 = 106; {Alternate F3}
    Alt_F4 = 107; {Alternate F4 -- toggle the masking bit}
    Alt_F5 = 108; {Alternate F5 -- clear 25th line}
    Alt_F6 = 109; {Alternate F6 -- display modes on 25th line}
    Alt_F10 = 113; {Exit, but clear screen first}
    HOME = 71; {HOME key}
    PrtSc = 114; {CNTL-PrtSc}
    CNTL_Pg_Dn = 118; {Download -- UNIX -> IBM}
    CNTL_Pg_Up = 132; {Upload -- IBM -> UNIX}
    Alt_1 = 120;
    Alt_0 = 129;
    Alt_MINUS = 130;
    Alt_EQUAL = 131;

var [external]
    function_keys : array[1..10] of lstring(30);
    vi_cursor : boolean;
    rogue_mode : boolean;
    insert_mode : boolean;
    display_buffer_addr : word;
    line_edit : boolean;
    adm_sim_flag : boolean;
    xoff_sent : boolean;
    scroll_top : integer;
    display_columns : boolean;
    parity_mask : integer;
    bbs_numbers : boolean;
    char_graphics : boolean;
    log_flag : boolean;
    ignore_dels : boolean;
    log_file : file of char;
    display_control : boolean;
    lpt_only_flag : boolean;

{$include:'simterm.inc'}
{$include:'graph.inc'}
{$include:'comm.inc'}
procedure savescreen; external;
procedure restorescreen; external;
procedure display_keys; external;
procedure save_line(line : CRT_SIZE; inc : INC_LIMIT);
    external;
procedure display_line(line : CRT_SIZE; inc : INC_LIMIT);
    external;
procedure delete_char; external;
procedure ck(a : integer; const b : string);
    external;
procedure write_file; external;
procedure endxqq; external;
procedure down_load; external;
procedure up_load; external;
procedure dump_file; external;
procedure alogin; external;
procedure xmodem_down; external;
procedure xmodem_up; external;
procedure do_ventels; external;
procedure com_end; external;
procedure parse_file(var filename : lstring);
    external;
procedure chattr(newattr : byte; y, sx, ex : integer); external;
procedure login; external;

procedure keyp(inch : char) [public];
var
   i,j,x,y : integer;
   answer : lstring(20);
   num_chars, ca : integer;
   line_buf : lstring(80);

begin {extended ASCII}
    xrcurp(x,y); {get cursor position}
    if ( (ord(inch) >= Alt_1) and (ord(inch) <= Alt_0) ) then
	eval(send(function_keys[ord(inch)-Alt_1+1]));
    case ord(inch) of

    Alt_EQUAL:
	begin
	    display_keys;
	    xxmove(x,y);
	end;

    Alt_MINUS:
	{change function keys}
	begin
	    savescreen;
	    xxcls;
	    display_keys;
	    xxmove(0,0);
	    write('Which key do you want to change? ');
	    readln(i);
	    if ( (i >= 0) and (i < 11) ) then begin
		if i = 0 then i := 10;
		if (function_keys[i,ord(function_keys[i,0])] = chr(13)) then
		    function_keys[i,ord(function_keys[i,0])] := chr(174);
		write('That key now = "');
		write(function_keys[i]);
		writeln('"');
		if (function_keys[i,ord(function_keys[i,0])] = chr(174)) then
		    function_keys[i,ord(function_keys[i,0])] := chr(13);
		write('New string: ');
		readln(function_keys[i]);
		write('Do you want a newline appended to that? ');
		readln(answer);
		if ( (answer[1] = 'y') or (answer[1] = 'Y') ) then
		    concat(function_keys[i],chr(13));
	    end;
	    restorescreen;
	    display_keys;
	end;

    END_KEY:
	if vi_cursor then eval(send('G'))
	else if rogue_mode then eval(send('b'))
	else eval(breaker); {send BREAK}

    HOME:
	{top of file if in 'vi'}
	if vi_cursor then eval(send('1G'))
	else if rogue_mode then eval(send('y'));

    INSERT_KEY:
	begin {ENTER line}
	    if vi_cursor then
		if insert_mode then begin
		    eval(send(chr(27))); {ESCAPE -- end insert mode}
		    if display_buffer_addr = #B800 then
			xscurt(byword(6,7))
		    else
			xscurt(byword(11,12)); {default cursor}
		end
		else begin
		    eval(send('i')); {go into insert mode}
		    xscurt(byword(2,11));
		end
	    else if not line_edit then begin
		num_chars := 0;
		for i := LEFT_MAR to RIGHT_MAR do begin
		    xxmove(i,y);
		    ca := xrca and #FF; {get character}
		    if ca < 32 then ca := ord(' ');
		    if chr(ca) <> ' ' then num_chars := i; {last non-blank}
		    line_buf[i+1] := chr(ca)
		end;
		line_buf[0] := chr(num_chars+2); {LSTRING len}
		line_buf[num_chars+2] := NL; {'new line'}
		xxmove(LEFT_MAR,y); {position at start of line}
		ck(send(line_buf),'enter')
	    end
	    else insert_mode := not insert_mode;
	end;

    DELETE_KEY:
	{clear display from cursor}
	begin
	    if vi_cursor then eval(send('x'))
	    else if line_edit then delete_char
	    else begin
		xwca(NULLB,(RIGHT_MAR+1)-x); {clear rest of line}
		for i:= y+1 to BOTTOM do begin
		    xxmove(LEFT_MAR,i);
		    xwca(NULLB,(RIGHT_MAR+1))
		end;
		xxmove(x,y) {restore cursor}
	    end
	end;

    C_UP:
	{cursor UP}
	begin
	    if (vi_cursor or rogue_mode) then eval(send('k'))
	    else if adm_sim_flag then eval(send(chr(#B)))
	    else begin
		save_line(y,-1);
		if (y>TOP) then xxmove(x,y-1)
		else begin
		    xscrldn(1,TOP,BOTTOM);
		    display_line(TOP,0)
		end
	    end
	end;

    C_DOWN:
	{cursor DOWN}
	begin
	    if (vi_cursor or rogue_mode) then eval(send('j'))
	    else if adm_sim_flag then eval(send(chr(#A)))
	    else begin
		save_line(y,1);
		if (y<BOTTOM) then xxmove(x,y+1)
		else begin
		    xscrlup(1,TOP,BOTTOM);
		    display_line(BOTTOM,0)
		end
	    end
	end;

    C_LEFT:
	{cursor LEFT}
	if (vi_cursor or rogue_mode) then eval(send('h'))
	else if adm_sim_flag then eval(send(chr(#8)))
	else if (x>LEFT_MAR) then xxmove(x-1,y);

    C_RIGHT:
	{cursor RIGHT}
	if (vi_cursor or rogue_mode) then eval(send('l'))
	else if adm_sim_flag then eval(send(chr(#C)))
	else if (x<RIGHT_MAR) then xxmove(x+1,y);

    PG_UP:
	{Page UP}
	begin
	    if vi_cursor then eval(send('11'*chr(21)))
	    else if rogue_mode then eval(send('u'))
	    else begin
		save_line(y,0);
		xxcls;
		for i := BOTTOM downto TOP+1 do
		    display_line(i,-1);
		display_line(TOP,0);
		xxmove(LEFT_MAR,TOP)
	    end
	end;

    PG_DOWN:
	{Page DOWN}
	begin
	    if vi_cursor then eval(send('11'*chr(4)))
	    else if rogue_mode then eval(send('n'))
	    else begin
		save_line(y,0);
		xxcls;
		for i := TOP to BOTTOM-1 do
		    display_line(i,1);
		display_line(BOTTOM,0);
		xxmove(LEFT_MAR,BOTTOM)
	    end
	end;

    NULL_CHAR:
	{^@ - send 'null'}
	ck(send(chr(0)),'null');

    PrtSc:
	{toggle LOG BOTTOM -- output to printer}
	lpt_only_flag := not lpt_only_flag;

    F1:
	{toggle control character display}
	display_control := not display_control;

    F2:
	{print the current line}
	begin
	    for i := LEFT_MAR to RIGHT_MAR do begin
		xxmove(i,y);
		inch := chr(xrca and #FF); {get character}
		if inch < ' ' then inch := ' '; {make sure it is printable}
		xlpt1(inch)
	    end;
	    xlpt1(chr(13)*chr(10)); {CR/LF}
	    if y < BOTTOM then xxmove(0,y+1)
	    else xxmove(0,y);
	end;

    F3:
	{send BREAK -- same as End, except that End is used with 'vi'}
	eval(breaker);

    F5:
	{send an "l" (for notes) }
	eval(send('l'));

    F6:
	{send an "j" (for notes) }
	eval(send('j'));

    F8:
	{scroll LOCK}
	begin
	    if (scroll_top > 0) then chattr(7, scroll_top, 1, 80);
	    xrcurp(x,scroll_top); {set scroll lock here }
	    if (scroll_top > 0) then chattr(#70, scroll_top, 1, 80);
	end;

    F9:
	{toggle the display row,column indicator}
	begin
	    xxmove(38,24);
	    xttywrt('     ',7); {blank it out}
	    xxmove(x,y);
	    display_columns := not display_columns;
	end;

    F10:
	{toggle the FLOW Control -- XON/XOFF}
	begin
	    if xoff_sent then begin
		xoff_sent := false;
		ck(send(XON_CHAR),'XON')
	    end
	    else begin
		xoff_sent := true;
		ck(send(XOFF_CHAR),'XOFF')
	    end
	end;

    Alt_F1:
	begin
	    writeln(chr(7)*chr(13)*chr(10)*
	    'Terminating the Simulator');
	    write_file; {update bbs file}
	    com_end;
	    endxqq
	end;

    Alt_F2:
	{terminate}
	{terminate and drop the line}
	begin {Terminate}
	    writeln(chr(7)*chr(13)*chr(10)*
	    'Terminating the Simulator & Dropping the Line');
	    dropline;
	    write_file; {update bbs file}
	    com_end;
	    endxqq
	end;

    Alt_F3:
	{drop then pick up the line (for ventels) }
	toggle_tr;

    Alt_F4:
	{toggle the parity_mask}
	parity_mask := parity_mask xor #80;

    Alt_F5:
	{clear the 25th line}
	begin
	    xxmove(0,24);
	    xwca(NULLB,RIGHT_MAR+1); {clear rest of line}
	    xxmove(x,y) {restore cursor}
	end;

    Alt_F6:
	{display some status information}
	begin
	    xxmove(0,24);
	    if (bbs_numbers) then xttywrt('BBS',112)
	    else xttywrt('BBS',7);
	    xttywrt('   ',7);
	    if ((parity_mask and #80) = #80) then xttywrt('Graphics',112)
	    else xttywrt('Graphics',7);
	    xttywrt('   ',7);
	    if (rogue_mode) then xttywrt('rogue',112)
	    else xttywrt('rogue',7);
	    xttywrt('   ',7);
	    if (vi_cursor) then xttywrt('vi',112)
	    else xttywrt('vi',7);
	    xttywrt('   ',7);
	    if (log_flag) then xttywrt('file log',112)
	    else xttywrt('file log',7);
	    xttywrt('   ',7);
	    if (ignore_dels) then xttywrt('ignore del',112)
	    else xttywrt('ignore del',7);
	    xttywrt('   ',7);
	    if (adm_sim_flag) then xttywrt('ADM3A',112)
	    else xttywrt('ADM3A',7);
	    xxmove(x,y);
	end;

    Alt_F10:
	begin
	    writeln(chr(7)*chr(13)*chr(10)*
	    'Terminating the Simulator');
	    write_file; {update bbs file}
	    xxcls;
	    com_end;
	    endxqq
	end;

    CNTL_Pg_Dn:
	{Download}
	down_load;

    CNTL_Pg_Up:
	{Upload}
	up_load;

    Alt_C:
	{toggle the 'LOG FILE' flag}
	if log_flag then begin
	    log_flag := false;
	    close(log_file);
	    xttywrt('Log file is closed.'*chr(10)*chr(13),7);
	end
	else begin
	    savescreen;
	    xxcls;
	    xxmove(0,0);
	    write(output,'Log file name: ');
	    readln(input,line_buf);
	    parse_file(line_buf);
	    assign(log_file,line_buf);
	    rewrite(log_file);
	    log_flag := true;
	    restorescreen;
	end;

    Alt_D:
	{Dump file to Comm line}
	dump_file;

    Alt_E:
	{Toggle the vi_cursor mode}
	begin
	    writeln(output,'current vi_cursor mode is ',vi_cursor,
	    '. New mode is ',not vi_cursor);
	    vi_cursor := not vi_cursor;
	end;

    Alt_F:
	line_edit := not line_edit;

    Alt_K:
	alogin;

    Alt_L:
	login;

    Alt_R:
	{XModem Download/Receive}
	xmodem_down;

    Alt_T:
	{XModem Upload/Transmit}
	xmodem_up;

    Alt_V:
	{ Call varied functions from ventel.pas}
	do_ventels;

    otherwise
	; {anything else -- ignore}
    end
end;
end.
    27:
	{ESCAPE}
	escape;

    ord('H') and #1F:
	{cursor left}
	if (x > LEFT_MAR) th