Kyle Burton on Wed, 5 Jan 2000 11:26:15 -0500 (EST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] non buffered c program


This has been driving me nuts, I've done this before, but couldn't remember
how/where I'd done it.

At approximatly page 325 of the Lion book (UNIX Systems Programming for SVR4),
in Chapter 12: terminals, the text describes the difference between 
cooked mode (aka canonical mode), and raw mode (aka non-canonical mode).

Now, looking in the manpages on my linux laptop, I couldn't find anything
about cooked or raw modes -- but I had someone track down the Lion book
for me, and I've written and attached an example of what you have to 
do.

The manpages for tcsetattr should explain more if you need it...I hope
this helps.

k

------------------------------------------------------------------------------
Finding out what goes on in the C.I.A. is like performing acupuncture on a
rock. 
    -- New York Times, Jan. 20, 1981
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------

On Wed, 5 Jan 2000, mattybeast wrote:

> On Tue, 04 Jan 2000, you wrote:
> > i am trying to write a program that counts words and would prefer the user
> > not having to hit return.
> > i have tried to use setvbuf but it is not working.  does anyone know how to
> > use this?
> 
> I'm only familiar with good 'ol ANSI C
> but just at a glance I believe 
> you need to define a valid mode for setvbuf
> legit values of mode are       _IOFBF
>                                         _IONBF
>                                         _IOLBF 
> here's a url that may help get you on track
> http://www.introl.com/introl-demo/Libraries/C/ANSI_C/stdio/setbuf.html 
> the code you sent has issues ........but I digress
> 
> I also feel while a bit off  topic it is apropos as I use C every day with my linux boxen
> anyone else ever use csh?
> I'm not saying we need this list deluged with c questions, I just think being
> helpful to our fellow Linux users is what a lug is for...
>                                    feel free to call me 
> 				Matty
> 
> Silence is not always tact and it is tact that is golden, not silence.
> 						-Samuel Butler 1912
> 
> _______________________________________________
> PLUG maillist  -  PLUG@lists.nothinbut.net
> http://lists.nothinbut.net/mail/listinfo/plug
> 
/*
   Description: Example program to turn off canonical mode for terminal IO.
   Created:     Wed Jan  5 11:07:25 EST 2000
   Build:       gcc -o term2 term2.c

   Copyright (C) 2000 Kyle R. Burton, All Rights Reserved
   mortis@voicenet.com
   http://www.voicenet.com/~mortis
   http://www.bgw.org

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
*/

#include <termios.h>         /* for: tcgetattr() and tcsetattr() */
#include <stdio.h>           /* for: pritnf() */

int main( int argc, char** argv )
{
  char c,                    /* for reading the input 1 char at a time */
       buff[255];            /* to store the user's input */
  struct termios modes,      /* so we can change the modes */
                 savemodes;  /* so we can reset the modes when we're done */
  int i = 0;                 /* index into buffer during input */

  tcgetattr(0,&modes);        /* get the current terminal modes */
  savemodes = modes;          /* save a copy so we can reset them */

  printf("changing attributes...\n");
  modes.c_lflag &= ~ICANON;   /* turn off canonical mode */
  tcsetattr(0,0,&modes);      /* set the new modes */

  printf("type some text (CR to end): ");
  while(read(0,&c,1) > 0) {   /* process some raw input... */
    /* 
     * break on '*', newline, EOF, or if the buffer filled up 
     *
     */
    if('*'==c || '\n'==c || savemodes.c_cc[VEOF]==c || i>=(sizeof(buff)-1)) {
      break;
    }

    buff[i++] = c;            /* stuff the input into our buffer */
  }

  buff[i] = '\0';             /* null terminate the buffer... */
  printf("You typed: %.255s\n",buff);

  tcsetattr(0,0,&savemodes);  /* reset the terminal to the saved mode */
  return 0;                   /* exit... */
}