Re: termios guru ?

2002-07-11 Thread Sergey Babkin

bruno schwander wrote:
> 
> thanks, I see the idea but cfmakeraw has some other effects... newlines
> output by the program are not translated, etc.

To get rid of the raw output effects, remove the line

t->c_oflag &= ~OPOST;

> 
> My main program now is the VMIN/VTIME stuff. The way irit tries to use is,
> is basically to be able to do async stdin reading, but this does not
> work. Whenever I try those settings, no input is ever read by the
> program. It fgetc() constantly returns -1.
> 
> Any idea why ?
> 
> bruno
> 
> On Thu, 11 Jul 2002, Cyrille Lefevre wrote:
> 
> > On Wed, Jul 10, 2002 at 09:13:18PM -0700, bruno schwander wrote:
> > > I making a port (not much really) of Irit
> > > (http://www.cs.technion.ac.il/~irit/) a modelling environment.
> > >
> > > I am having some problems with terminal handling, so all termios guru out
> > > there, please help ! :-)
> > >
> > > At stratup, irit does the following
> > >
> > > Termio.c_cc[VEOF] = 0;  /* MIN = 0, no minimal length to wait for. */
> > > Termio.c_cc[VEOL] = 1;  /* TIME - 1 tenth of a second as time o
> > >
> > > which seems wrong, I think it should be
> > >
> > > Termio.c_cc[VMIN] = 0;  /* MIN = 0, no minimal length to wait for. */
> > > Termio.c_cc[VTIME] = 1;  /* TIME - 1 tenth of a second as time o
> >
> > VMIN == VEOF and VTIME == VEOL.

On SysV but not guaranteed to be so on every system. In fact, if we 
look in the FreeBSD /usr/include/sys/termios.h we can see:

#define VEOF0   /* ICANON */
#define VEOL1   /* ICANON */

#define VMIN16  /* !ICANON */
#define VTIME   17  /* !ICANON */

No wonder that it does not work.

-SB

To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: termios guru ?

2002-07-11 Thread Terry Lambert

bruno schwander wrote:
> thanks, I see the idea but cfmakeraw has some other effects... newlines
> output by the program are not translated, etc.
> 
> My main program now is the VMIN/VTIME stuff. The way irit tries to use is,
> is basically to be able to do async stdin reading, but this does not
> work. Whenever I try those settings, no input is ever read by the
> program. It fgetc() constantly returns -1.
> 
> Any idea why ?

 In noncanonical mode input processing, input bytes are not assembled into
 lines, and erase and kill processing does not occur.  The values of the
 VMIN and VTIME members of the c_cc array are used to determine how to
 process the bytes received.

 MIN represents the minimum number of bytes that should be received when
 the read(2) function successfully returns.  TIME is a timer of 0.1 second
 granularity that is used to time out bursty and short term data transmis-
 sions.  If MIN is greater than { MAX_INPUT}, the response to the request
 is undefined.  The four possible values for MIN and TIME and their inter-
 actions are described below.

What's "MAX_INPUT" set to?

Try running:

system("stty -a");

>From your program, after you set the terminal modes, to make sure you set
them correctly.

Make sure you are using termios, not termio.

See also the source code to raw(3) and cbreak(3) in the curses library
source code.

-- Terry

To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: termios guru ?

2002-07-11 Thread bruno schwander

thanks, I see the idea but cfmakeraw has some other effects... newlines
output by the program are not translated, etc.

My main program now is the VMIN/VTIME stuff. The way irit tries to use is,
is basically to be able to do async stdin reading, but this does not
work. Whenever I try those settings, no input is ever read by the
program. It fgetc() constantly returns -1.

Any idea why ?

bruno

On Thu, 11 Jul 2002, Cyrille Lefevre wrote:

> On Wed, Jul 10, 2002 at 09:13:18PM -0700, bruno schwander wrote:
> > I making a port (not much really) of Irit
> > (http://www.cs.technion.ac.il/~irit/) a modelling environment.
> > 
> > I am having some problems with terminal handling, so all termios guru out
> > there, please help ! :-)
> > 
> > At stratup, irit does the following
> > 
> > Termio.c_cc[VEOF] = 0;  /* MIN = 0, no minimal length to wait for. */
> > Termio.c_cc[VEOL] = 1;  /* TIME - 1 tenth of a second as time o
> > 
> > which seems wrong, I think it should be
> > 
> > Termio.c_cc[VMIN] = 0;  /* MIN = 0, no minimal length to wait for. */
> > Termio.c_cc[VTIME] = 1;  /* TIME - 1 tenth of a second as time o
> 
> VMIN == VEOF and VTIME == VEOL.
> 
> > then later:
> > 
> > Termio.c_lflag &= ~ICANON;
> 
> take a look at cfmakeraw(3) which is BSD specific, but that's
> not important since it's a port *to* BSD :)
> 
> more +/cfmakeraw /usr/src/lib/libc/gen/termios.c
> 
> cfmakeraw(t)
> struct termios *t;
> {
> 
> t->c_iflag &= 
>~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR);
> t->c_iflag |= IGNBRK;
> t->c_oflag &= ~OPOST;
> t->c_lflag &= 
>~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN);
> t->c_cflag &= ~(CSIZE|PARENB);
> t->c_cflag |= CS8|CREAD;
> t->c_cc[VMIN] = 1;
> t->c_cc[VTIME] = 0;
> }
> 
> so, a short answer could be, as Bernd Walter suggested :
> 
> int
> settty(raw)
>   int raw;
> {
>   static int init;
>   static struct termios old;
> struct termios buf, *new;
> 
>   if (!init) {
>   if (tcgetattr(STDIN_FILENO, &old) < 0) {
>   printf("tcgetattr failed: %s\n", strerror(errno));
>   return(1);
>   }
>   init++;
>   }
>   if (raw) {
>   if (init < 2) {
>   cfmakeraw(&buf);
>   init++;
>   }
>   new = buf;
>   } else
>   new = old;
> if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) < 0) {
> printf("tcsetattr failed: %s\n", strerror(errno));
> return(1);
> }   
>   return(0);
> }
> 
> Cyrille.
> -- 
> Cyrille Lefevre mailto:[EMAIL PROTECTED]
> 


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: termios guru ?

2002-07-11 Thread Cyrille Lefevre

On Wed, Jul 10, 2002 at 09:13:18PM -0700, bruno schwander wrote:
> I making a port (not much really) of Irit
> (http://www.cs.technion.ac.il/~irit/) a modelling environment.
> 
> I am having some problems with terminal handling, so all termios guru out
> there, please help ! :-)
> 
> At stratup, irit does the following
> 
> Termio.c_cc[VEOF] = 0;  /* MIN = 0, no minimal length to wait for. */
> Termio.c_cc[VEOL] = 1;  /* TIME - 1 tenth of a second as time o
> 
> which seems wrong, I think it should be
> 
> Termio.c_cc[VMIN] = 0;  /* MIN = 0, no minimal length to wait for. */
> Termio.c_cc[VTIME] = 1;  /* TIME - 1 tenth of a second as time o

VMIN == VEOF and VTIME == VEOL.

> then later:
> 
> Termio.c_lflag &= ~ICANON;

take a look at cfmakeraw(3) which is BSD specific, but that's
not important since it's a port *to* BSD :)

more +/cfmakeraw /usr/src/lib/libc/gen/termios.c

cfmakeraw(t)
struct termios *t;
{

t->c_iflag &= 
~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR);
t->c_iflag |= IGNBRK;
t->c_oflag &= ~OPOST;
t->c_lflag &= 
~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN);
t->c_cflag &= ~(CSIZE|PARENB);
t->c_cflag |= CS8|CREAD;
t->c_cc[VMIN] = 1;
t->c_cc[VTIME] = 0;
}

so, a short answer could be, as Bernd Walter suggested :

int
settty(raw)
int raw;
{
static int init;
static struct termios old;
struct termios buf, *new;

if (!init) {
if (tcgetattr(STDIN_FILENO, &old) < 0) {
printf("tcgetattr failed: %s\n", strerror(errno));
return(1);
}
init++;
}
if (raw) {
if (init < 2) {
cfmakeraw(&buf);
init++;
}
new = buf;
} else
new = old;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) < 0) {
printf("tcsetattr failed: %s\n", strerror(errno));
return(1);
}   
return(0);
}

Cyrille.
-- 
Cyrille Lefevre mailto:[EMAIL PROTECTED]

To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: termios guru ?

2002-07-11 Thread Bernd Walter

On Wed, Jul 10, 2002 at 09:13:18PM -0700, bruno schwander wrote:
> I making a port (not much really) of Irit
> (http://www.cs.technion.ac.il/~irit/) a modelling environment.
> 
> I am having some problems with terminal handling, so all termios guru out
> there, please help ! :-)

That's what I did for a terminal programm to setup the controlling tty:

void
settty(void) {
struct termios buf;

if (tcgetattr(STDIN_FILENO, &save_termios) < 0) {
printf("tcgetattr failed: %s\n", strerror(errno));
exit (1);
}
memcpy(&buf, &save_termios, sizeof(buf));
buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
buf.c_iflag &= ~(ICRNL | INPCK | ISTRIP | IXON);
buf.c_cflag &= ~(CSIZE | PARENB);
buf.c_cflag |= CS8;
buf.c_oflag &= ~OPOST;
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &buf) < 0) {
printf("tcsetattr failed: %s\n", strerror(errno));
exit (1);
}
ttyset = 1;
return;
}

> At stratup, irit does the following
> 
> Termio.c_cc[VEOF] = 0;  /* MIN = 0, no minimal length to wait for. */
> Termio.c_cc[VEOL] = 1;  /* TIME - 1 tenth of a second as time o

Setting VEOL or VEOF with disabled ICANON is senseless.

> which seems wrong, I think it should be
> 
> Termio.c_cc[VMIN] = 0;  /* MIN = 0, no minimal length to wait for. */
> Termio.c_cc[VTIME] = 1;  /* TIME - 1 tenth of a second as time o
> 
> then later:
> 
> Termio.c_lflag &= ~ICANON;

Sparse initialisation may require specific defaults to work.

> basically, irit wants to manage line editing itself, to manage the irit
> command prompt. There is some code doing the ^A, ^H, etc handling and line
> printing, and reading periodically stdin.
> 
> What I see happening, is that usually at the very beginning, input seems
> locked. Running in the debugger, I see that characters are fgetc'ed
> periodically, but fgetc always returns -1 even when there should be
> characters available.
> 
> I then tried using fcntl(0, F_SETFL, O_NONBLOCK) instead of the above 2
> lines. which I thought would do the right thing, ie: non blocking IO, but
> anything available from stdin is buffered and provided on the next read.
> This works, however I am seeing something strange on stdout now: when
> outputting lots of lines, outputs stalls after a few dozen lines. Adding a
> usleep between each fwrite() solves the problem but slows it all
> down... (and is inherently wrong)

I'm not shure if nonblocking or character mode is compatible with
stdio.
I always used direct io in such cases.

> What is going on here ? I do not understand very well all the terminal/IO
> discipline here.
> 
> I agree that this is all bad design, and should probably multithread or
> use select() but I am not Irit's author...

-- 
B.Walter  COSMO-Project http://www.cosmo-project.de
[EMAIL PROTECTED] Usergroup   [EMAIL PROTECTED]


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message