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()', and May 2000 to illustrate wide-character
11 * Takes a filename argument. It's a simple file-viewer with various
12 * scroll-up and scroll-down commands.
14 * n -- scroll one line forward
15 * p -- scroll one line back
17 * Either command accepts a numeric prefix interpreted as a repeat count.
18 * Thus, typing `5n' should scroll forward 5 lines in the file.
20 * The way you can tell this is working OK is that, in the trace file,
21 * there should be one scroll operation plus a small number of line
22 * updates, as opposed to a whole-page update. This means the physical
23 * scroll operation worked, and the refresh() code only had to do a
26 * $Id: view.c,v 1.62 2005/05/28 21:40:25 tom Exp $
29 #include <test.priv.h>
33 #undef CTRL /* conflict on AIX 5.2 with <sys/ioctl.h> */
41 #if !defined(sun) || !HAVE_TERMIOS_H
43 # include <sys/ioctl.h>
49 /* This is needed to compile 'struct winsize' */
51 #include <sys/stream.h>
55 static RETSIGTYPE finish(int sig) GCC_NORETURN;
56 static void show_all(const char *tag);
58 #if defined(SIGWINCH) && defined(TIOCGWINSZ) && HAVE_RESIZE_TERM
65 static RETSIGTYPE adjust(int sig);
66 static int interrupted;
69 static bool waiting = FALSE;
71 static bool try_color = FALSE;
74 static NCURSES_CH_T **vec_lines;
75 static NCURSES_CH_T **lptr;
81 static const char *msg[] =
83 "Usage: view [options] file"
86 ," -c use color if terminal supports it"
87 ," -i ignore INT, QUIT, TERM signals"
88 ," -n NUM specify maximum number of lines (default 1000)"
89 #if defined(KEY_RESIZE)
90 ," -r use old-style sigwinch handler rather than KEY_RESIZE"
93 ," -t trace screen updates"
94 ," -T NUM specify trace mask"
98 for (n = 0; n < SIZEOF(msg); n++)
99 fprintf(stderr, "%s\n", msg[n]);
100 ExitProgram(EXIT_FAILURE);
104 ch_len(NCURSES_CH_T * src)
107 #if USE_WIDEC_SUPPORT
110 #if USE_WIDEC_SUPPORT
111 while (getcchar(src++, NULL, NULL, NULL, NULL) > 0)
121 * Allocate a string into an array of chtype's. If UTF-8 mode is
122 * active, translate the string accordingly.
124 static NCURSES_CH_T *
127 unsigned len = strlen(src);
128 NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
130 #if USE_WIDEC_SUPPORT
131 wchar_t wstr[CCHARW_MAX + 1];
139 #if USE_WIDEC_SUPPORT
140 memset(&state, 0, sizeof(state));
142 for (j = k = 0; j < len; j++) {
143 #if USE_WIDEC_SUPPORT
144 rc = mbrtowc(&wch, src + j, len - j, &state);
145 if (rc == (size_t) -1 || rc == (size_t) -2)
148 if ((width = wcwidth(wch)) < 0)
150 if ((width > 0 && l > 0) || l == CCHARW_MAX) {
153 if (setcchar(dst + k, wstr, 0, 0, NULL) != OK)
157 if (width == 0 && l == 0)
164 #if USE_WIDEC_SUPPORT
167 if (setcchar(dst + k, wstr, 0, 0, NULL) == OK)
171 setcchar(dst + k, wstr, 0, 0, NULL);
179 main(int argc, char *argv[])
186 NCURSES_CH_T **olptr;
189 bool got_number = FALSE;
191 bool nonposix_resize = FALSE;
193 const char *my_label = "Input";
195 setlocale(LC_ALL, "");
197 #ifndef NCURSES_VERSION
199 * We know ncurses will catch SIGINT if we don't establish our own handler.
200 * Other versions of curses may/may not catch it.
202 (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
205 while ((i = getopt(argc, argv, "cin:rtT:")) != EOF) {
211 signal(SIGINT, SIG_IGN);
212 signal(SIGQUIT, SIG_IGN);
213 signal(SIGTERM, SIG_IGN);
216 if ((MAXLINES = atoi(optarg)) < 1)
221 nonposix_resize = TRUE;
226 trace((unsigned) atoi(optarg));
236 if (optind + 1 != argc)
239 if ((vec_lines = typeMalloc(NCURSES_CH_T *, MAXLINES + 2)) == 0)
242 fname = argv[optind];
243 if ((fp = fopen(fname, "r")) == 0) {
245 ExitProgram(EXIT_FAILURE);
249 (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
253 for (lptr = &vec_lines[0]; (lptr - vec_lines) < MAXLINES; lptr++) {
254 char temp[BUFSIZ], *s, *d;
257 if (fgets(buf, sizeof(buf), fp) == 0)
260 /* convert tabs so that shift will work properly */
261 for (s = buf, d = temp, col = 0; (*d = *s) != '\0'; s++) {
265 } else if (*d == '\t') {
267 while ((d - temp) != col)
270 #if USE_WIDEC_SUPPORT
273 if (isprint(UChar(*d))) {
277 sprintf(d, "\\%03o", UChar(*s));
283 *lptr = ch_dup(temp);
286 num_lines = lptr - vec_lines;
288 (void) initscr(); /* initialize the curses library */
289 keypad(stdscr, TRUE); /* enable keyboard mapping */
290 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
291 (void) cbreak(); /* take input chars one at a time, no wait for \n */
292 (void) noecho(); /* don't echo input */
293 nodelay(stdscr, TRUE);
294 idlok(stdscr, TRUE); /* allow use of insert/delete line */
299 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
300 bkgd(COLOR_PAIR(my_pair));
318 my_label = "interrupt";
324 if ((c < 127) && isdigit(c)) {
326 mvprintw(0, 0, "Count: ");
330 value = 10 * value + (c - '0');
335 if (got_number && value) {
342 my_label = keyname(c);
347 for (i = 0; i < n; i++)
348 if ((lptr - vec_lines) < (num_lines - LINES + 1))
352 wscrl(stdscr, lptr - olptr);
358 for (i = 0; i < n; i++)
359 if (lptr > vec_lines)
363 wscrl(stdscr, lptr - olptr);
373 if (num_lines > LINES)
374 lptr = vec_lines + num_lines - LINES + 1;
398 case KEY_RESIZE: /* ignore this; ncurses will repaint */
403 halfdelay(my_delay = n);
405 nodelay(stdscr, FALSE);
410 nodelay(stdscr, TRUE);
421 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
427 finish(0); /* we're done */
435 if (vec_lines != 0) {
437 for (n = 0; n < num_lines; ++n) {
443 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
448 * This uses functions that are "unsafe", but it seems to work on SunOS and
449 * Linux. Usually: the "unsafe" refers to the functions that POSIX lists
450 * which may be called from a signal handler. Those do not include buffered
451 * I/O, which is used for instance in wrefresh(). To be really portable, you
452 * should use the KEY_RESIZE return (which relies on ncurses' sigwinch
455 * The 'wrefresh(curscr)' is needed to force the refresh to start from the top
456 * of the screen -- some xterms mangle the bitmap while resizing.
461 if (waiting || sig == 0) {
464 if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
465 resize_term(size.ws_row, size.ws_col);
466 wrefresh(curscr); /* Linux needs this */
467 show_all(sig ? "SIGWINCH" : "interrupt");
473 (void) signal(SIGWINCH, adjust); /* some systems need this */
475 #endif /* CAN_RESIZE */
478 show_all(const char *tag)
486 sprintf(temp, "%s (%3dx%3d) col %d ", tag, LINES, COLS, shift);
488 sprintf(temp + i, "view %.*s", (int) (sizeof(temp) - 7 - i), fname);
490 sprintf(temp, "view %.*s", (int) sizeof(temp) - 7, fname);
493 printw("%.*s", COLS, temp);
495 this_time = time((time_t *) 0);
496 strcpy(temp, ctime(&this_time));
497 if ((i = strlen(temp)) != 0) {
499 if (move(0, COLS - i - 2) != ERR)
503 scrollok(stdscr, FALSE); /* prevent screen from moving */
504 for (i = 1; i < LINES; i++) {
506 printw("%3ld:", (long) (lptr + i - vec_lines));
508 if ((s = lptr[i - 1]) != 0) {
511 #if USE_WIDEC_SUPPORT
512 add_wchstr(s + shift);
517 #if defined(NCURSES_VERSION) || defined(HAVE_WCHGAT)
519 wchgat(stdscr, -1, A_NORMAL, my_pair, NULL);
523 setscrreg(1, LINES - 1);
524 scrollok(stdscr, TRUE);