|
|
This program pages through a file, writing one page to the terminal from which the program is invoked and the next page to the terminal named on the command line. It then waits for a space to be typed on either terminal and writes the next page to the terminal at which the space is typed.
The two program is just a simple example of a two-terminal curses program. It does not handle notification; instead, it requires the name and type of the second terminal on the command line. As written, the command "sleep 100000" must be typed at the second terminal to put it to sleep while the program runs, and the user of the first terminal must have both read and write permission on the second terminal.
#include <ocurses.h> #include <signal.h>SCREEN *me, *you; SCREEN *set_term();
FILE *fd, *fdyou; char linebuf[512];
main(argc, argv) int argc; char **argv; { void done(), exit(); unsigned sleep(); char *getenv(); int c;
if (argc != 4) { fprintf(stderr, "Usage: two othertty otherttytype inputfile\n"); exit(1); } fd = fopen(argv[3], "r"); fdyou = fopen(argv[1], "w+"); signal(SIGINT, done); /* die gracefully */
me = newterm(getenv("TERM"), stdout, stdin); /* initialize my tty */ you = newterm(argv[2], fdyou, fdyou); /* Initialize the other terminal */
set_term(me); /* Set modes for my terminal */ noecho(); /* turn off tty echo */ cbreak(); /* enter cbreak mode */ nonl(); /* Allow linefeed */ nodelay(stdscr, TRUE); /* No hang on input */
set_term(you); /* Set modes for other terminal */ noecho(); cbreak(); nonl(); nodelay(stdscr,TRUE);
/* Dump first screen full on my terminal */ dump_page(me);
/* Dump second screen full on the other terminal */ dump_page(you);
for (;;) /* for each screen full */ { set_term(me); c = getch(); if (c == 'q') /* wait for user to read it */ done(); if (c == ' ') dump_page(me);
set_term(you); c = getch(); if (c == 'q') /* wait for user to read it */ done(); if (c == ' ') dump_page(you); sleep(1); } } dump_page(term) SCREEN *term; { int line;
set_term(term); move(0, 0); for (line = 0; line < LINES - 1; line++) { if (fgets(linebuf, sizeof linebuf, fd) == NULL) { clrtobot(); done(); } mvaddstr(line, 0, linebuf); } standout(); mvprintw(LINES - 1, 0, "--More--"); standend(); refresh(); /* sync screen */ } /* Clean up and exit. */ void done() { /* Clean up first terminal */ set_term(you); move(LINES - 1,0); /* to lower left corner */
clrtoeol(); /* clear bottom line */ refresh(); /* flush out everything */ endwin(); /* curses cleanup */ delscreen(); /* remove screen */
/* Clean up second terminal */ set_term(me); move(LINES - 1,0); /* to lower left corner */ clrtoeol(); /* clear bottom line */ refresh(); /* flush out everything */ endwin(); /* curses cleanup */ delscreen(); /* remove screen */ exit(0); }