|
|
This program illustrates a use of the routine attrset. highlight reads a text file and uses embedded escape sequences to control attributes. \U turns on underlining, \B turns on bold, and \N restores the default output attributes.
Note the first call to scrollok, a routine that we have not previously discussed (see the curses(3ocurses) manual pages). This routine allows the terminal to scroll if the file is longer than one screen. When an attempt is made to draw past the bottom of the screen, scrollok automatically scrolls the terminal up a line and calls refresh.
/* * highlight: a program to turn \U, \B, and * \N sequences into highlighted * output, allowing words to be * displayed underlined or in bold. */#include <stdio.h> #include <ocurses.h>
main(argc, argv) int argc; char **argv; { FILE *fd; int c, c2; void exit(), perror();
if (argc != 2) { fprintf(stderr, "Usage: highlight file\n"); exit(1); }
fd = fopen(argv[1], "r");
if (fd == NULL) { perror(argv[1]); exit(2); }
initscr(); scrollok(stdscr, TRUE); nonl(); while ((c = getc(fd)) != EOF) { if (c == '\\') { c2 = getc(fd); switch (c2) { case 'B': attrset(A_BOLD); continue; case 'U': attrset(A_UNDERLINE); continue; case 'N': attrset(0); continue; } addch(c); addch(c2); } else addch(c); } fclose(fd); refresh(); endwin(); exit(0); }