All pastes #384500 Raw Edit

TODOS BITCH

public c v1 · immutable
·published 2007-03-07 03:15 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <termios.h>#include <signal.h>#include <sys/time.h>/**** * * This program is a simple example of using setitimer and sigaction to * generate and handle SIGALRM signals.  The program sets up an interval timer * that generates a SIGALRM once a second.  The SIGALRM handler simply * increments a signal counter and prints out its value. * * Before starting the timer, the program puts the terminal in "raw mode". * This means that it does not echo input characters, wait for a newline to * complete an input, or respond to interrupt characters. * * After starting the timer and setting up the handler, the main function waits * for the user to type the character 'q' to quit.  Since the terminal is in * raw mode, typing 'q' is the only way to stop, since all other printable * characters are ignored, and terminal interrupt characters are disabled. * *//** * The ticks variable counts the number of one-second ticks since the program * started. */static int ticks;/** * The tick function is the handler for SIGALRM. */void tick(int sig) {    printf("%d\r", ++ticks);}/** * Set the terminal to "raw" input mode.  This is defined by setting terminal * control flags to noncanonical mode, turning off character echoing, and * ignoring signaling characters.  Before setting to raw mode, save the * current mode so it can be restored later.  After setting, return the * saved mode. * * For explanatory details, see Sections 18.10, 18.11 of Stevens, the * termio(7I) man page, and the tcsetattr(3C) man page.  (To see a particular * man page section, use the "-s" argument, e.g., "man -s 7I termio" on * falcon/hornet.) */struct termios set_raw_term_mode() {    struct termios cur_term_mode, raw_term_mode;    tcgetattr(STDIN_FILENO, &cur_term_mode);    raw_term_mode = cur_term_mode;    raw_term_mode.c_lflag &= ~(ICANON | ECHO | ISIG) ;    raw_term_mode.c_cc[VMIN] = 1 ;    raw_term_mode.c_cc[VTIME] = 0;    tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);    return cur_term_mode;}/** * Restore the terminal mode to that saved by set_raw_term_mode. */void restore_term_mode(struct termios saved_term_mode) {    tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);}/** * Set the terminal to raw mode, set up a one-second timer, set up the SIGARLM * handler, and then wait for the user to type 'q'. * * For details of timer setup, see Stevens Section 6.1 and the man pages for * setitimer(2) and gettimeofday(3C). * * For details of signal setup, see Stevens Section 10.14, and the man page for * sigaction. */int main(int argc, char** argv) {    struct termios saved_term_mode;      /* saved entering terminal mode */    struct itimerval tbuf;               /* interval timer structure */    struct sigaction action;             /* signal action structure */    /*     * Explain to the user how to quit.     */    printf("Type 'q' to quit.\n\n");    /*     * Initialize ticks to 0.     */    ticks = 0;    /*     * Set up the SIGALRM handler.     */    action.sa_handler = tick;     /* set tick to be the handler function */    sigemptyset(&action.sa_mask); /* clear out masked functions */    action.sa_flags   = 0;        /* no special handling */    /*     * Use the sigaction function to associate the signal action with SIGALRM.     */    if (sigaction(SIGALRM, &action, NULL) < 0 ) {        perror("SIGALRM");        exit(-1);    }    /*     * Define a one-second timer.     */    tbuf.it_interval.tv_sec  = 1;    tbuf.it_interval.tv_usec = 0;    tbuf.it_value.tv_sec  = 1;    tbuf.it_value.tv_usec = 0;    /*     * Use the setitimer function to start the timer.     */    if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {        perror("setitimer");        exit(-1);                   /* should only fail for serious reasons */    }    /*     * Set the terminal to raw mode.     */    saved_term_mode = set_raw_term_mode();    /*     * Busy wait until the user types 'q'.     */    while (getchar() != 'q') {}    /*     * Restore the terminal to the mode it was in at program entry.     */    restore_term_mode(saved_term_mode);}