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.52 2002/04/27 22:37:39 tom Exp $
33 #include <test.priv.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_RESIZETERM
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 **my_lines;
75 static NCURSES_CH_T **lptr;
80 static const char *msg[] =
82 "Usage: view [options] file"
85 ," -c use color if terminal supports it"
86 ," -i ignore INT, QUIT, TERM signals"
87 ," -n NUM specify maximum number of lines (default 1000)"
88 #if defined(KEY_RESIZE)
89 ," -r use old-style sigwinch handler rather than KEY_RESIZE"
92 ," -t trace screen updates"
93 ," -T NUM specify trace mask"
97 for (n = 0; n < SIZEOF(msg); n++)
98 fprintf(stderr, "%s\n", msg[n]);
99 ExitProgram(EXIT_FAILURE);
103 ch_len(NCURSES_CH_T * src)
106 #if USE_WIDEC_SUPPORT
109 #if USE_WIDEC_SUPPORT
110 while (getcchar(src++, NULL, NULL, NULL, NULL) > 0)
120 * Allocate a string into an array of chtype's. If UTF-8 mode is
121 * active, translate the string accordingly.
123 static NCURSES_CH_T *
126 unsigned len = strlen(src);
127 NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
129 #if USE_WIDEC_SUPPORT
130 wchar_t wstr[CCHARW_MAX + 1];
138 #if USE_WIDEC_SUPPORT
139 memset(&state, 0, sizeof(state));
141 for (j = k = 0; j < len; j++) {
142 #if USE_WIDEC_SUPPORT
143 rc = mbrtowc(&wch, src + j, len - j, &state);
144 if (rc == (size_t) -1 || rc == (size_t) -2)
147 if ((width = wcwidth(wch)) < 0)
149 if ((width > 0 && l > 0) || l == CCHARW_MAX) {
152 if (setcchar(dst + k, wstr, 0, 0, NULL) != OK)
156 if (width == 0 && l == 0)
163 #if USE_WIDEC_SUPPORT
166 if (setcchar(dst + k, wstr, 0, 0, NULL) == OK)
169 setcchar(dst + k, L"", 0, 0, NULL);
177 main(int argc, char *argv[])
184 NCURSES_CH_T **olptr;
188 bool got_number = FALSE;
190 bool nonposix_resize = FALSE;
192 const char *my_label = "Input";
194 setlocale(LC_ALL, "");
196 #ifndef NCURSES_VERSION
198 * We know ncurses will catch SIGINT if we don't establish our own handler.
199 * Other versions of curses may/may not catch it.
201 (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
204 while ((i = getopt(argc, argv, "cin:rtT:")) != EOF) {
210 signal(SIGINT, SIG_IGN);
211 signal(SIGQUIT, SIG_IGN);
212 signal(SIGTERM, SIG_IGN);
215 if ((MAXLINES = atoi(optarg)) < 1)
220 nonposix_resize = TRUE;
235 if (optind + 1 != argc)
238 if ((my_lines = typeMalloc(NCURSES_CH_T *, MAXLINES + 2)) == 0)
241 fname = argv[optind];
242 if ((fp = fopen(fname, "r")) == 0) {
244 ExitProgram(EXIT_FAILURE);
248 (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
252 for (lptr = &my_lines[0]; (lptr - my_lines) < MAXLINES; lptr++) {
253 char temp[BUFSIZ], *s, *d;
256 if (fgets(buf, sizeof(buf), fp) == 0)
259 /* convert tabs so that shift will work properly */
260 for (s = buf, d = temp, col = 0; (*d = *s) != '\0'; s++) {
264 } else if (*d == '\t') {
266 while ((d - temp) != col)
269 #if USE_WIDEC_SUPPORT
272 if (isprint(UChar(*d))) {
276 sprintf(d, "\\%03o", UChar(*s));
282 *lptr = ch_dup(temp);
285 length = lptr - my_lines;
287 (void) initscr(); /* initialize the curses library */
288 keypad(stdscr, TRUE); /* enable keyboard mapping */
289 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
290 (void) cbreak(); /* take input chars one at a time, no wait for \n */
291 (void) noecho(); /* don't echo input */
292 nodelay(stdscr, TRUE);
293 idlok(stdscr, TRUE); /* allow use of insert/delete line */
298 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
299 bkgd(COLOR_PAIR(my_pair));
317 my_label = "interrupt";
323 if ((c < 127) && isdigit(c)) {
325 mvprintw(0, 0, "Count: ");
329 value = 10 * value + (c - '0');
334 if (got_number && value) {
341 my_label = keyname(c);
346 for (i = 0; i < n; i++)
347 if ((lptr - my_lines) < (length - LINES + 1))
351 wscrl(stdscr, lptr - olptr);
357 for (i = 0; i < n; i++)
362 wscrl(stdscr, lptr - olptr);
373 lptr = my_lines + length - LINES + 1;
397 case KEY_RESIZE: /* ignore this; ncurses will repaint */
402 halfdelay(my_delay = n);
404 nodelay(stdscr, FALSE);
409 nodelay(stdscr, TRUE);
420 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
426 finish(0); /* we're done */
433 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
438 * This uses functions that are "unsafe", but it seems to work on SunOS and
439 * Linux. Usually: the "unsafe" refers to the functions that POSIX lists
440 * which may be called from a signal handler. Those do not include buffered
441 * I/O, which is used for instance in wrefresh(). To be really portable, you
442 * should use the KEY_RESIZE return (which relies on ncurses' sigwinch
445 * The 'wrefresh(curscr)' is needed to force the refresh to start from the top
446 * of the screen -- some xterms mangle the bitmap while resizing.
451 if (waiting || sig == 0) {
454 if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
455 resize_term(size.ws_row, size.ws_col);
456 wrefresh(curscr); /* Linux needs this */
457 show_all(sig ? "SIGWINCH" : "interrupt");
463 (void) signal(SIGWINCH, adjust); /* some systems need this */
465 #endif /* CAN_RESIZE */
468 show_all(const char *tag)
476 sprintf(temp, "%s (%3dx%3d) col %d ", tag, LINES, COLS, shift);
478 sprintf(temp + i, "view %.*s", (int) (sizeof(temp) - 7 - i), fname);
480 sprintf(temp, "view %.*s", (int) sizeof(temp) - 7, fname);
483 printw("%.*s", COLS, temp);
485 this_time = time((time_t *) 0);
486 strcpy(temp, ctime(&this_time));
487 if ((i = strlen(temp)) != 0) {
489 if (move(0, COLS - i - 2) != ERR)
493 scrollok(stdscr, FALSE); /* prevent screen from moving */
494 for (i = 1; i < LINES; i++) {
496 printw("%3ld:", (long) (lptr + i - my_lines));
498 if ((s = lptr[i - 1]) != 0) {
501 #if USE_WIDEC_SUPPORT
502 add_wchstr(s + shift);
507 wchgat(stdscr, -1, A_NORMAL, my_pair, NULL);
510 setscrreg(1, LINES - 1);
511 scrollok(stdscr, TRUE);