home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
stg_v4.lzh
/
get_line.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-11
|
2KB
|
165 lines
/*
* get_line(buf,len,echo) - input line with 60 sec timeout
*
* 92/10/29 StG - rewrite from scratch (used to be tor() in login)
* 92/11/11 StG - added word wrap mode and timeout beep
* 93/01/06 StG - hacked for unix
*/
#include "stglib.h"
/* TODO: add tabs */
#define TIME_RES 10
#define TIME_OUT _time_out
extern int _time_out;
int wrap_len=0;
char wrap_buf[256];
get_line(buf,len,echo)
char *buf;
int len;
char echo;
{
int wrap;
int to;
char *p;
char *s;
char c;
if (!len)
return(ERR);
if (wrap=(len<0))
len=-len;
to=TIME_OUT*TIME_RES;
p=buf;
len--;
if (wrap && wrap_len)
{
s=wrap_buf;
/*
while (wrap_len && *s==' ')
{
s++;
wrap_len--;
}
*/
while (wrap_len)
{
writeln(2,s,1);
*p++=*s++;
wrap_len--;
}
}
wrap_len=0;
signal(SIGINT,SIG_IGN);
signal(SIGQUIT,SIG_IGN);
_ss_mode(2,3);
while (to--)
{
if (_gs_rdy(2)==ERR /* && errno==246 */)
{
if (to<TIME_OUT/4 && !(to%TIME_RES))
writeln(2,"\7",1);
usleep(1000000/TIME_RES);
continue;
}
/* _ss_echo(2,0); */
if (read(2,p,1)!=1)
{
writeln(2,"\7",1);
continue;
}
/* _ss_echo(2,1); */
if (*p=='\n')
{
writeln(2,p,1);
*p=0;
_ss_mode(2,0);
signal(SIGINT,SIG_DFL);
signal(SIGQUIT,SIG_DFL);
return(p-buf);
}
if (*p==8)
{
if (p-buf)
{
writeln(2,"\b \b",3);
--p;
}
continue;
}
if (*p==24)
{
while (p-buf)
{
writeln(2,"\b \b",3);
--p;
}
continue;
}
if (p-buf==len)
{
if (!wrap || !len)
{
writeln(2,"\7",1);
continue;
}
s=p-1;
while (s>buf && *s==' ')
s--;
while (s>buf && *s!=' ')
s--;
if (s-buf<len/8)
{
writeln(2,"\7",1);
continue;
}
wrap_len=p-s;
strncpy(wrap_buf,s+1,wrap_len);
while (p-s)
{
writeln(2,"\b \b",3);
--p;
}
writeln(2,"\n",1);
*p=0;
_ss_mode(2,0);
signal(SIGINT,SIG_DFL);
signal(SIGQUIT,SIG_DFL);
return(p-buf);
}
if (*p<32 || *p>126)
{
writeln(2,"\7",1);
continue;
}
if (echo)
writeln(2,&echo,1);
else
writeln(2,p,1);
p++;
}
writeln(2,"\n",1);
_ss_mode(2,0);
signal(SIGINT,SIG_DFL);
signal(SIGQUIT,SIG_DFL);
return(ERR);
}