Presentation is loading. Please wait.

Presentation is loading. Please wait.

Terminal Control operating systems. Terminal Control may seem like an arcane subject, but … It illustrates the relationship between devices and files.

Similar presentations


Presentation on theme: "Terminal Control operating systems. Terminal Control may seem like an arcane subject, but … It illustrates the relationship between devices and files."— Presentation transcript:

1 Terminal Control operating systems

2 Terminal Control may seem like an arcane subject, but … It illustrates the relationship between devices and files The terminal driver is an easy device driver to work with operating systems

3 Consider the following code: #include int main ( ) { int c, n = 0; while( ( c = getchar( ) ) != ‘Q’ ) printf ( “char %3d is %c code %d\n”, n++, c, c); return 0; } Run it..... (listchars.c)

4 The program does not process any data until the Enter key is hit. The enter key generates a Carriage Return character, 13 but the program receives the character code 10, New Line. Some Observations

5 Moreover, when the program outputs a New Line character, it outputs as a Carriage Return. New Line only move the cursor down one line, it does not move the cursor to the left margin! Some Observations

6 The Terminal Driver Terminal Driver Application operating systems The Terminal driver is doing some manipulation of the data as it flows between the terminal and your program!

7 The Terminal Driver Terminal Driver Application operating systems These manipulations are controlled by terminal attributes that can be changed by a process. attributes

8 Terminal Attributes To see the current set of terminal attributes stty stty -a

9 $ stty -a speed 9600 baud; 24 rows; 80 columns;... iflags: -istrp icrnl -inlcr... input flags: what the driver does with characters coming from the terminal - the flag is turned off convert cr to nl man stty to see more detail

10 You can alter setting with the stty command stty -echo This turns off echo mode

11 Terminal Modes Canonical or cooked mode The terminal driver stores incoming characters in a buffer Sends characters to the application a line at a time Handles basic editing functions (delete, backspace, …) operating systems

12 The Terminal Driver Terminal Driver Application operating systems characters lines canonical mode buffer Process Backspace, etc

13 Terminal Modes Non-Canonical or cr-break mode The terminal driver does not buffer keyboard input Sends characters to the application a character at a time, as it is entered Does some character processing * Ctrl-C (interrupt) * carriage return to newline operating systems

14 The Terminal Driver Terminal Driver Application operating systems non-canonical mode characters Process Ctrl-C, CR-NL

15 Terminal Modes Raw mode The terminal driver does not buffer keyboard input. Sends characters to the application a character at a time Does no character processing operating systems

16 The Terminal Driver Terminal Driver Application operating systems raw mode

17 The Terminal Driver Terminal Driver Application operating systems You control the Terminal driver by manipulating A set of terminal attributes stored in the device driver. tcgetattr( ) tcsetattr( ) driver attributes

18 Reading Attributes from the Terminal Driver #include int result = tcgetattr(int fd, struct termios* attrs); returns 0 if successful -1 if an error zero for stdin address of a termios struct operating systems

19 Setting Attributes in the Terminal Driver #include int result = tcsetattr(int fd, int when, struct termios* attrs); returns 0 if successful -1 if an error zero for stdin address of a termios struct when to apply the settings TCSANOW – immediately TCSADRAIN – after draining queued data TCSAFLUSH – after draining + flush input operating systems Note: Some shells will reset these attributes in an effort to protect you from yourself.

20 The termios struct struct termios { tcflag_t c_iflag;/* input mode flags */ tcflag_t c_oflag;/* output mode flags */ tcflag_t c_cflag;/* control mode flags */ tcflag_t c_lflag;/* local mode flags */ cc_t c_cc[NCCS]; /* control characters */ speed_t c_ispeed;/* input speed */ speed_t c_ospeed;/* output speed */ }; operating systems

21 flagsflags c_lflag ISIG – enable signals ICANON – canonical mode ECHO – enable echo operating systems All of these flags have symbolic constants. To see all of the fields in the termios structure see termios man pages.

22 MaskingMasking operating systems

23 Bitwise Operators &and |or ~complement 1 1 1 1 0 0 0 1 0 0 0 0 a b a & b 1 1 1 1 0 1 0 1 1 0 0 0 a b a | b operating systems Remember your boolean logic?

24 To Set a flag flag = flag | mask 0’s except for the bits to be set in the flag 1 1 1 1 0 1 0 1 1 0 0 0 flag mask flag | mask operating systems this bit got set

25 To Clear a flag flag = flag & ~mask 1’s except for the bits to be cleared in the flag 1 1 0 0 1 0 1 1 0 1 0 0 0 0 1 0 flag mask ~ mask flag & ~mask operating systems this bit got cleared

26 To Test a flag if ( flag == flag & mask ) 1 1 1 1 0 0 0 1 0 0 0 0 flag mask flag & mask operating systems Remember that in C, 0 is false and non-zero is true. set the bits in the mask that you want to test in the flag if ( flag & mask )

27 ExampleExample operating systems #include int main ( ) { struct termios info; int rv; if ( (rv = tcgetattr( 0, &info ) ) == -1 ) { perror(“tcgetattr error”); exit ( 1 ); } if ( info.c_lflag & ECHO) printf(“ echo flag is set\n”); else printf(“ echo flag is not set\n”); return 0; } file descriptor is 0 for stdin Examine Echo flag test the flag Run... echostate.c

28 ExampleExample operating systems if ( (rv = tcgetattr( 0, &info ) ) == -1 ) { perror(“tcgetattr error”); exit ( 1 ); } if ( argv[1][0] == ‘y’ ) info.c_lflag |= ECHO; else info.c_lflag &= ~ECHO; if ( tcsetattr ( 0, TCSANOW, &info ) == -1) { perror(“tcsetattr error”); exit ( 1 ); } Change Echo flag set the flag clear the flag Run... setecho.c

29 Writing a Terminal Program operating systems do some work ask if user wants to do more get response no yes Molay chapter 6

30 operating systems Because the terminal is in canonical mode, The user has the following problems: 1. The user has to press the enter key before the program can act on the input. 2. The user can type something other than ‘y’ or ‘n’ and nothing happens.

31 operating systems do some work ask if user wants to do more no yes turn off canonical mode turn on canonical mode get response response ?

32 operating systems This works great, but replying to every illegal keystroke is annoying! The solution: Turn off echo mode and ignore illegal keystrokes.

33 operating systems What if this program was running in an atm machine, and the person walked away without typing ‘y’ or ‘n’? Someone else could walk up and run a transaction using this person’s account.

34 operating systems Blocking vs Non-blocking I/O read do something The normal read operation waits until the read operation is satisfied before going on to the next instruction.

35 operating systems Blocking vs Non-blocking I/O read do something It is possible to use fcntl or open to change the properties of a file so that when the file is read, the program does not wait for the read to finish, but goes on immediately to the next instruction. The read instruction returns a 0 if no data has been read.

36 Using fcntl( ) #include int fcntl(int filedes, int cmd, … /* int arg */); F_DUPFD - duplicate the file descriptor F_GETFD - get the file descriptor flags F_SETFD - set the file descriptor flags F_GETFL - get the file status flags F_SETFL - set the file status flags...

37 File status flags O_RDONLYopen file for reading only O_WRONLYopen file for writing only O_RDWRopen for reading and writing O_APPENDappend on each write O_NONBLOCKnon-blocking mode O_SYNCwait for writes to complete O_ASYNCasynchronous I/O (bsd and 4.3)

38 void set_nodelay_mode( ) { int termflags; termflags = fcntl(0, F_GETFL); termflags |= NONBLOCK; fcntl(0, F_SETFL, termflags); } get the file descriptor flags set non-blocking flag save the new flags

39 operating systems int get response(char* question, int maxtries) { int input; printf(“%s (y/n)?”, question); flush (stdout); while (1) { sleep(SLEEPTIME); input = tolower(get_ok_char( ) ); if ( input == ‘y’ ) return 1; if ( input == ‘n’ ) return 0; if ( maxtries -- == 0) return 2; } print the question

40 operating systems int get response(char* question, int maxtries) { int input; printf(“%s (y/n)?”, question); flush (stdout); while (1) { sleep(SLEEPTIME); input = tolower(get_ok_char( ) ); if ( input == ‘y’ ) return 1; if ( input == ‘n’ ) return 0; if ( maxtries -- == 0) return 2; } The o/s won’t flush the buffer until it sees a newline or until the program tries to read. and the read doesn’t happen until here. timeout

41 operating systems char get_ok_char( ) { int c: while ( ( c = getchar( ) ) != EOF && strchr(“YyNn”, c) == NULL); return c; } Drops out if EOF is returned. Note that when read is non- blocking an EOF is returned when there is no data. In this case, the EOF ( 0 ) gets returned.

42 operating systems char get_ok_char( ) { int c: while ( ( c = getchar( ) ) != EOF && strchr(“YyNn”, c) == NULL); return c; } Or it drops out when one of ‘Y’, ‘y’, ‘N’, or ‘n’ is typed. In this case, the typed character is returned. The strchr( str1, c) function looks for the character c in the string str1. It returns a pointer to the character or NULL if the character is not found.

43 Your more command should work as follows: Your program will display the first 23 lines of the file. On the last line of the screen, it will then display the file name of the file being displayed, and the percentage of the file which has been displayed. It then will wait for user input. The user has three choices at this point: If the user types a 'q', then your program exits. If the user types a space, then your program displays the next 23 lines of the file. If the user presses the ENTER key, then just the next line of the file is displayed. Note that invalid keystrokes are ignored and keystrokes are not echoed back to the screen. In the last two cases, your program should just display the percentage of the file that has been displayed. It only displays the file name on the first screen displayed. Don’t allow the file name and percentage to scroll up the screen as new lines of data from the file are displayed.


Download ppt "Terminal Control operating systems. Terminal Control may seem like an arcane subject, but … It illustrates the relationship between devices and files."

Similar presentations


Ads by Google