|
|
This program pages through a file, showing one screen of its contents each time you depress the space bar. The program calls cbreak so that you can depress the space bar without having to hit return; it calls noecho to prevent the space from echoing on the screen. The nonl routine, which we have not previously discussed, is called to enable more cursor optimization. The idlok routine, which we also have not discussed, is called to allow insert and delete line. (See the curses(3ocurses) pages for more information about these routines). Also notice that clrtoeol and clrtobot are called.
By creating an input file for show made up of screen-sized (about 24 lines) pages, each varying slightly from the previous page, nearly any exercise for a curses program can be created. This type of input file is called a show script.
#include <ocurses.h> #include <signal.h>main(argc, argv) int argc; char *argv[]; { FILE *fd; char linebuf[BUFSIZ]; int line; void done(), perror(), exit();
if (argc != 2) { fprintf(stderr, "usage: %s file\n", argv[0]); exit(1); }
if ((fd=fopen(argv[1], "r")) == NULL) {
perror(argv[1]); exit(2); } signal(SIGINT, done);
initscr(); noecho(); cbreak(); nonl(); idlok(stdscr, TRUE);
while(1) { move(0,0); for (line = 0; line < LINES; line++) { if (!fgets(linebuf, sizeof linebuf, fd)) { clrtobot(); done(); } move(line, 0); printw("%s", linebuf); } refresh(); if (getch() == 'q') done(); } }
void done() { move(LINES - 1, 0); clrtoeol(); refresh(); endwin(); exit(0); }