2 * view.c -- a silly little viewer program
4 * written by Eric S. Raymond <esr@snark.thyrsus.com> December 1994
5 * to test the scrolling code in ncurses.
7 * modified by Thomas Dickey <dickey@clark.net> July 1995 to demonstrate
8 * the use of 'resizeterm()'.
10 * Takes a filename argument. It's a simple file-viewer with various
11 * scroll-up and scroll-down commands.
13 * n -- scroll one line forward
14 * p -- scroll one line back
16 * Either command accepts a numeric prefix interpreted as a repeat count.
17 * Thus, typing `5n' should scroll forward 5 lines in the file.
19 * The way you can tell this is working OK is that, in the trace file,
20 * there should be one scroll operation plus a small number of line
21 * updates, as opposed to a whole-page update. This means the physical
22 * scroll operation worked, and the refresh() code only had to do a
25 * $Id: view.c,v 1.20 1997/04/26 18:16:38 tom Exp $
28 #include <test.priv.h>
40 #if !defined(sun) || !HAVE_TERMIOS_H
42 # include <sys/ioctl.h>
46 /* This is needed to compile 'struct winsize' */
47 #if SYSTEM_LOOKS_LIKE_SCO
48 #include <sys/stream.h>
52 #define MAXLINES 256 /* most lines we can handle */
54 static void finish(int sig) GCC_NORETURN;
55 static void show_all(void);
57 #if defined(SIGWINCH) && defined(TIOCGWINSZ) && defined(NCURSES_VERSION)
64 static RETSIGTYPE adjust(int sig);
65 static int interrupted;
72 static char *lines[MAXLINES];
76 static char *strdup (char *s)
80 p = malloc(strlen(s)+1);
85 #endif /* not HAVE_STRDUP */
87 int main(int argc, char *argv[])
100 fprintf(stderr, "usage: view file\n");
104 if ((fp = fopen(fname, "r")) == (FILE *)NULL) {
110 (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
112 (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
115 (void) initscr(); /* initialize the curses library */
116 keypad(stdscr, TRUE); /* enable keyboard mapping */
117 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
118 (void) cbreak(); /* take input chars one at a time, no wait for \n */
119 (void) noecho(); /* don't echo input */
120 idlok(stdscr, TRUE); /* allow use of insert/delete line */
123 for (lptr = &lines[0]; fgets(buf, BUFSIZ, fp) != (char *)NULL; lptr++) {
124 char temp[BUFSIZ], *s, *d;
127 if (lptr - lines >= MAXLINES) {
129 (void) fprintf(stderr, "%s: %s is too large\n", argv[0], argv[1]);
133 /* convert tabs so that shift will work properly */
134 for (s = buf, d = temp, col = 0; (*d = *s) != '\0'; s++) {
138 } else if (*d == '\t') {
140 while ((d-temp) != col)
142 } else if (isprint(*d)) {
146 sprintf(d, "\\%03o", *s);
151 *lptr = strdup(temp);
172 if (c < 127 && isdigit(c)) {
173 n = 10 * n + (c - '0');
178 if (!explicit && n == 0)
185 for (i = 0; i < n; i++)
186 if (lptr + LINES < lines + MAXLINES && lptr[LINES + 1])
190 wscrl(stdscr, lptr - olptr);
196 for (i = 0; i < n; i++)
201 wscrl(stdscr, lptr - olptr);
231 finish(0); /* we're done */
234 static RETSIGTYPE finish(int sig)
237 exit(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
242 * This uses functions that are "unsafe", but it seems to work on SunOS and
243 * Linux. The 'wrefresh(curscr)' is needed to force the refresh to start from
244 * the top of the screen -- some xterms mangle the bitmap while resizing.
246 static RETSIGTYPE adjust(int sig)
248 if (waiting || sig == 0) {
251 if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
252 resizeterm(size.ws_row, size.ws_col);
254 wrefresh(curscr); /* Linux needs this */
261 (void) signal(SIGWINCH, adjust); /* some systems need this */
263 #endif /* CAN_RESIZE */
265 static void show_all(void)
272 sprintf(temp, "(%3dx%3d) col %d ", LINES, COLS, shift);
274 sprintf(temp+i, "view %.*s", (int)(sizeof(temp)-7-i), fname);
276 sprintf(temp, "view %.*s", (int)sizeof(temp)-7, fname);
279 printw("%.*s", COLS, temp);
282 scrollok(stdscr, FALSE); /* prevent screen from moving */
283 for (i = 1; i < LINES; i++) {
285 if ((s = lptr[i-1]) != 0 && (int)strlen(s) > shift)
286 printw("%3d:%.*s", lptr+i-lines, COLS-4, s + shift);
288 printw("%3d:", lptr+i-lines);
291 setscrreg(1, LINES-1);
292 scrollok(stdscr, TRUE);
296 /* view.c ends here */