Sponsored Content
Full Discussion: Input Redirection
Top Forums UNIX for Dummies Questions & Answers Input Redirection Post 24225 by Perderabo on Monday 8th of July 2002 03:35:02 PM
Old 07-08-2002
Quote:
Originally posted by majeed73
Thanks Perderabo for your reply, what I am sure about is that i have no clue about what you mean by saying your program seems to require tty input and how that would make a difference,
There are many ways that a program can set itself up to work with a tty but fail with file input. They are rather complex. But I will describe one of them.

By sending the correct ioctl()'s calls to the standard input (which is assumed to be a tty), the program can set the MIN > 0 and the TIME > 0. (exactly how to do this is documented under "man termio") Once that is done, the program can then issue a multi-char read system call. The editor vi works this way and you can't redirect your input into it either.

If The MIN is 1, then a read() will not return until at least one character has been typed. But if TIME is also 1, after a character arrives, the tty driver will wait up to one tenth of a second to see if another character arrives. If it does, a read() of 10 characters would get both characters returned to it. If it didn't, the read() would get just the one character. This is how a program can read a single keystroke. The "A" key will just send the character "A". But the home key will send a mult-character sequence. The program wants to read keystrokes, not characters.

But now redirect a file into our program. First the ioctl()'s will now fail. But some programs just ignore the error. Next the multi-byte read() occurs and it gets as many bytes as it specified. In your example you have the characters:
2
1
But there is an implied delay between those keystrokes. The program was depending on that delay. So it gets "21" when it just wanted "2".

This is just one way to depend on a tty for input. Read the "man termio" to see everything that the tty driver can offer to the program.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

input redirection question

Hi, in my script I need to execute the following command: query $id 456 432 but it waits for a RETURN character from keyboard and therefore, it fails. I tried something like: query $id 456 432 << '\n' but, i'ts clear it is not correct. Is there any way to do this? Thxs. (0 Replies)
Discussion started by: luistid
0 Replies

2. Shell Programming and Scripting

permanent redirection of standard input

while running a user inter-active program how can we get the commands from a file instead of the user? is there anyway to permanently redirect content of a file to standard input? (6 Replies)
Discussion started by: gfhgfnhhn
6 Replies

3. UNIX for Advanced & Expert Users

password required when using input redirection

Hello, I need to change user and run some commands by using a script. lets say, I'm using su - someuser << start password required -----> how can I enter the password here command 1 command 2 command 3 command 4 start While trying to run this I got the following message: "standard... (2 Replies)
Discussion started by: Alalush
2 Replies

4. Shell Programming and Scripting

Input redirection and for loop

Hello, I need help with a bash script that I try to improve. I could not find answer so far, maybe because I'm not to familiar with the terminology so feel free to correct my language. I have a script that looks like: NODES="node_a node_b node_c" for NODE in $NODES do ... (4 Replies)
Discussion started by: pn8830
4 Replies

5. Shell Programming and Scripting

How to check for Input Redirection in my script?

All, I have a requirement to write a script where I check for Input redirection when the script was executed, based on which I handle my logic. Below is the example: my.script #! /bin/ksh # Not sure how to frame the if condition below if ; then echo "Input Redirected from a file" ... (7 Replies)
Discussion started by: bharath.gct
7 Replies

6. UNIX for Dummies Questions & Answers

Send job to Background after input redirection

Hi, I am having issues with syntax when I am trying to send a job to the background after a input redirection. I have this script which sends some files to different servers after zipping them. Once I execute it, it will ask for user input as of which server the files need to go to. (The... (3 Replies)
Discussion started by: grep_me
3 Replies

7. UNIX for Dummies Questions & Answers

Redirection of file input to command

Hello, I'm new to Unix (working with OS X 10.8.5) and therefore at the beginning of my adventure. If I ask something stupid, then this is not intentional, but simple nescience. :rolleyes: I have a problem with the redirection of text file content to echo. I was experimenting with redirection... (6 Replies)
Discussion started by: pseudo
6 Replies

8. UNIX for Dummies Questions & Answers

When do I use input redirection?

Can someone please explain when input redirection is necessary? For example, "cat filename" and "cat< filename" produce the same result. I was told that if I need to bunzip a file that I should type "bunzip2<filename.bz2." However, if I omit the "<" I still get the same result. Can someone... (4 Replies)
Discussion started by: PTcharger
4 Replies

9. Shell Programming and Scripting

Input redirection script

Hi, #!/bin/bash while ; do rm -f /tmp/pipe mkfifo /tmp/pipe ./yuv4mpeg_to_v4l2 < /tmp/pipe & mplayer tom_and_jerry.mp4 -vf scale=480:360 -vo yuv4mpeg:file=/tmp/pipe sleep 65; done When I run this - after mplayer finishes playing video it says - Exiting... (End of... (2 Replies)
Discussion started by: ashokvpp
2 Replies

10. Shell Programming and Scripting

Input redirection within bash script

Hi, when I try to redirect input and the command is described as a string within an array redirection does not work. why? #!/bin/bash dir=("tail < ./hello.txt") tail < ./hello.txt #works ${dir} #does not work (2 Replies)
Discussion started by: heinzel
2 Replies
curs_inopts(3CURSES)					     Curses Library Functions					      curs_inopts(3CURSES)

NAME
curs_inopts, cbreak, nocbreak, echo, noecho, halfdelay, intrflush, keypad, meta, nodelay, notimeout, raw, noraw, noqiflush, qiflush, time- out, wtimeout, typeahead - curses terminal input option control routines SYNOPSIS
cc [ flag ... ] file ... -lcurses [ library ... ] #include <curses.h> int cbreak(void); int nocbreak(void); int echo(void); int noecho(void); int halfdelay(int tenths); int intrflush(WINDOW *win, bool bf); int keypad(WINDOW *win, bool bf); int meta(WINDOW *win, bool bf); int nodelay(WINDOW *win, bool bf); int notimeout(WINDOW *win, bool bf); int raw(void); int noraw(void); void noqiflush(void); void qiflush(void); void timeout(int delay); void wtimeout(WINDOW *win, int delay); int typeahead(int fildes); DESCRIPTION
The cbreak() and nocbreak() routines put the terminal into and out of cbreak() mode, respectively. In this mode, characters typed by the user are immediately available to the program, and erase/kill character-processing is not performed. When out of this mode, the tty driver buffers the typed characters until a newline or carriage return is typed. Interrupt and flow control characters are unaffected by this mode. Initially the terminal may or may not be in cbreak() mode, as the mode is inherited; therefore, a program should call cbreak() or nocbreak() explicitly. Most interactive programs using curses set the cbreak() mode. Note that cbreak() overrides raw(). (See curs_getch(3CURSES) for a discussion of how these routines interact with echo() and noecho().) The echo() and noecho() routines control whether characters typed by the user are echoed by getch() as they are typed. Echoing by the tty driver is always disabled, but initially getch() is in echo mode, so characters typed are echoed. Authors of most interactive programs pre- fer to do their own echoing in a controlled area of the screen, or not to echo at all, so they disable echoing by calling noecho(). (See curs_getch(3CURSES) for a discussion of how these routines interact with cbreak() and nocbreak().) The halfdelay() routine is used for half-delay mode, which is similar to cbreak() mode in that characters typed by the user are immediately available to the program. However, after blocking for tenths tenths of seconds, ERR is returned if nothing has been typed. The value of tenths must be a number between 1 and 255. Use nocbreak() to leave half-delay mode. If the intrflush() option is enabled, (bf is TRUE), when an interrupt key is pressed on the keyboard (interrupt, break, quit) all output in the tty driver queue will be flushed, giving the effect of faster response to the interrupt, but causing curses to have the wrong idea of what is on the screen. Disabling (bf is FALSE), the option prevents the flush. The default for the option is inherited from the tty driver settings. The window argument is ignored. The keypad() option enables the keypad of the user's terminal. If enabled (bf is TRUE), the user can press a function key (such as an arrow key) and wgetch() returns a single value representing the function key, as in KEY_LEFT. If disabled (bf is FALSE), curses does not treat function keys specially and the program has to interpret the escape sequences itself. If the keypad in the terminal can be turned on (made to transmit) and off (made to work locally), turning on this option causes the terminal keypad to be turned on when wgetch() is called. The default value for keypad is false. Initially, whether the terminal returns 7 or 8 significant bits on input depends on the control mode of the tty driver (see termio(7I)). To force 8 bits to be returned, invoke meta(win, TRUE). To force 7 bits to be returned, invoke meta(win, FALSE). The window argument, win, is always ignored. If the terminfo capabilities smm (meta_on) and rmm (meta_off) are defined for the terminal, smm is sent to the terminal when meta( win, TRUE) is called and rmm is sent when meta(win, FALSE) is called. The nodelay() option causes getch() to be a non-blocking call. If no input is ready, getch() returns ERR. If disabled (bf is FALSE), getch() waits until a key is pressed. While interpreting an input escape sequence, wgetch() sets a timer while waiting for the next character. If notimeout(win, TRUE) is called, then wgetch() does not set a timer. The purpose of the timeout is to differentiate between sequences received from a function key and those typed by a user. With the raw() and noraw() routines, the terminal is placed into or out of raw mode. Raw mode is similar to cbreak() mode, in that charac- ters typed are immediately passed through to the user program. The differences are that in raw mode, the interrupt, quit, suspend, and flow control characters are all passed through uninterpreted, instead of generating a signal. The behavior of the BREAK key depends on other bits in the tty driver that are not set by curses. When the noqiflush() routine is used, normal flush of input and output queues associated with the INTR, QUIT and SUSP characters will not be done (see termio(7I)). When qiflush() is called, the queues will be flushed when these control characters are read. The timeout() and wtimeout() routines set blocking or non-blocking read for a given window. If delay is negative, blocking read is used (that is, waits indefinitely for input). If delay is zero, then non-blocking read is used (that is, read returns ERR if no input is wait- ing). If delay is positive, then read blocks for delay milliseconds, and returns ERR if there is still no input. Hence, these routines provide the same functionality as nodelay(), plus the additional capability of being able to block for only delay milliseconds (where delay is positive). curses does ``line-breakout optimization'' by looking for typeahead periodically while updating the screen. If input is found, and it is coming from a tty, the current update is postponed until refresh() or doupdate() is called again. This allows faster response to commands typed in advance. Normally, the input FILE pointer passed to newterm(), or stdin in the case that initscr() was used, will be used to do this typeahead checking. The typeahead() routine specifies that the file descriptor fildes is to be used to check for typeahead instead. If fildes is -1, then no typeahead checking is done. RETURN VALUES
All routines that return an integer return ERR upon failure and an integer value other than ERR upon successful completion, unless other- wise noted in the preceding routine descriptions. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
curs_getch(3CURSES), curs_initscr(3CURSES), curses(3CURSES), attributes(5), termio(7I) NOTES
The header <curses.h> automatically includes the headers <stdio.h> and <unctrl.h>. Note that echo(), noecho(), halfdelay(), intrflush(), meta(), nodelay(), notimeout(), noqiflush(), qiflush(), timeout(), and wtimeout() may be macros. SunOS 5.10 31 Dec 1996 curs_inopts(3CURSES)
All times are GMT -4. The time now is 12:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy