Visit Our UNIX and Linux User Community


Help with stdin and non-canonical mode


 
Thread Tools Search this Thread
Top Forums Programming Help with stdin and non-canonical mode
# 1  
Old 10-16-2007
Help with stdin and non-canonical mode

I am writing a more command for my class, and have it most of the way done, but with my current implimentation when I pipe stdin to my code it doesnt use my non-canonical/non echo settings I set for the terminal window. Do I need to specify a different terminal in order for this to work ? It works great when I just use the program on an file.
I will paste the code below

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>

#define PAGELEN 24
#define LINELEN 512

int FILE_SIZE = 0; //holds the global file size for my %'s
int READ_SIZE = 0;
int FIRST_READ = 0; //flag to decide wether to display first time info, or just %

void do_more( FILE *fp, char *file_read,int);
int see_more(FILE *cmd, char *file_read,int);
void cbreak(struct termios tty)
{
tty.c_lflag = tty.c_lflag & ~(ECHO | ECHOK | ICANON);
tty.c_cc[VTIME] = 1;
tcsetattr(0, TCSANOW, &tty);
}

int main(int argc, char **argv)
{
FILE *fp;
struct termios tty, otty;
int i, j;
struct stat file_info; //buffer for file info

tcgetattr(0, &otty); // Get original tty settings and save them in otty
tty = otty;
cbreak(tty); //turns off echo and on non-canonical

if ( argc == 1 ){
fp = stdin;
do_more( fp,argv[0],1 );
}else
while ( --argc ){
if ( (fp = fopen( *++argv , "r" )) != NULL )
{
if (stat(argv[0],&file_info) == -1){ //error condition
perror(argv[0]);
}else{
FILE_SIZE = file_info.st_size;
do_more( fp,argv[0],0 ) ;
fclose( fp );
}
}
else
printf("%s: No such file or directory.\n",argv[0]);
}

tcsetattr(0, TCSANOW, &otty); // Reset to the original settings
return (0);
}
void do_more( FILE *fp, char *file_read,int is_stdin)
{
char line[LINELEN];

int num_of_lines = 0;
int see_more(FILE *, char*,int), reply;
FILE *fp_tty;

fp_tty = fopen( "/dev/tty", "r" ); /* NEW: cmd stream */
if ( fp_tty == NULL ) /* if open fails */
exit(1); /* no use in running */

while ( fgets( line, LINELEN, fp ) ){ /* more input */
if ( num_of_lines == PAGELEN ) { /* full screen? */

reply = see_more(fp_tty, file_read,is_stdin); /* NEW: pass FILE * */
if ( reply == 0 ){ /* n: done */
printf("\n");
break;
}

num_of_lines -= reply; /* reset count */

}
if ( fputs( line, stdout ) == EOF ) /* show line */
exit(1); /* or die */
else
READ_SIZE += strlen(line); //adds the total
num_of_lines++; /* count it */
}
}
int see_more(FILE *cmd, char *file_read, int is_stdin) /* NEW: accepts arg */
{
int c;
if (is_stdin == 0){
if (FIRST_READ == 0){
printf("\033[7m%s: %.1f%%\033[m",file_read,((float)READ_SIZE/(float)FILE_SIZE)*100); /* reverse on a vt100 */
FIRST_READ++;
}else
printf("\033[7m%.1f%%\033[m",((float)READ_SIZE/(float)FILE_SIZE)*100); /* reverse on a vt100 */
}else{
printf("\033[7mbytes %d\033[m",READ_SIZE);
}

while( (c=getc(cmd)) != EOF ) // NEW: reads from tty
{
if ( c == 'q' ) // q -> N
return 0;
if ( c == ' ' ) // ' ' => next p
return PAGELEN; // how many to show
if ( c == '\n' ) // Enter key => 1 line
return 1;
}


}
# 2  
Old 10-16-2007
Code:
tcsetattr(0, TCSANOW, &tty);

Remember

fd | stream
=========
0 | stdin
1 | stdout
2 | stderr

If you are talking about output you should be refering to fd 1.

Two useful routines:

1. isatty()

2. cfmakeraw()

Previous Thread | Next Thread
Test Your Knowledge in Computers #603
Difficulty: Medium
MySQL 8.0 does not maintain information about password history so you cannot enabling restrictions on reuse of previous passwords.
True or False?

9 More Discussions You Might Find Interesting

1. HP-UX

From a C++ application how to find if a hpux host is in standard mode or trusted mode

is there a way for my C++ application to find out which mode the hpux OS is running in? standard mode or trusted mode. (3 Replies)
Discussion started by: einsteinBrain
3 Replies

2. Shell Programming and Scripting

Getting canonical path with sed

Hi there, Please read the preliminary notes: readlink does the same ....... I know realpath does the same ....... I know awk can do it ................ I know perl can do it ............... I know You're losing time ........... I knowI wrote the following script that I think prints a... (4 Replies)
Discussion started by: chebarbudo
4 Replies

3. Solaris

DNS service is in maintenance mode. How to bring it back to online mode?

:confused: when i tried to look the status of DNS-client, it is in maintenance mode..... Please tell me how to bring it back to online mode...PLEASE TELL ME STEP BY STEP.... PLEASE... :wall: (2 Replies)
Discussion started by: vamshigvk475
2 Replies

4. Programming

How do you detect keystrokes in canonical mode?

I'm writing a command shell, and I want to be able to detect when the user presses an arrow key (otherwise it just prints [[A, [[B, etc.). I know it's relatively easy (although somewhat more time-consuming) to detect keystrokes in noncanonical mode, but I've noticed that the bash shell detects... (4 Replies)
Discussion started by: Ultrix
4 Replies

5. UNIX for Dummies Questions & Answers

canonical name in solaris and linux

How can I get the "canonical name" in solaris and linux? With canonical name I mean the BOLD text below. /usr/sbin/nslookup hello.com Server: 169.41.160.1 Address: 169.41.160.1#3 Non-authoritative answer: hellos.com canonical name = hello.com Name: hello.com... (1 Reply)
Discussion started by: shifahim
1 Replies

6. UNIX for Advanced & Expert Users

What is the difference between single line mode and multiline mode in Regular expressions?

Hi All, Can please let me know what is the difference between the single line mode and multi line mode in regular expresions? Thanks, Chidhambaram B (3 Replies)
Discussion started by: chidhu.anu
3 Replies

7. SuSE

Convet Linux OS from text mode to graphic mode

Hi All, I used to have my suse linux(VM) server in graphic mode but not anymore since morning. I cant rolback since i loose somuch work. Any idea how to it back to normal. Thanks (6 Replies)
Discussion started by: s_linux
6 Replies

8. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

9. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

Featured Tech Videos