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.53 2002/06/29 23:28:27 tom Exp $
32 #include <test.priv.h>
40 #if !defined(sun) || !HAVE_TERMIOS_H
42 # include <sys/ioctl.h>
48 /* This is needed to compile 'struct winsize' */
50 #include <sys/stream.h>
54 static RETSIGTYPE finish(int sig) GCC_NORETURN;
55 static void show_all(const char *tag);
57 #if defined(SIGWINCH) && defined(TIOCGWINSZ) && HAVE_RESIZETERM
64 static RETSIGTYPE adjust(int sig);
65 static int interrupted;
68 static bool waiting = FALSE;
70 static bool try_color = FALSE;
73 static NCURSES_CH_T **my_lines;
74 static NCURSES_CH_T **lptr;
79 static const char *msg[] =
81 "Usage: view [options] file"
84 ," -c use color if terminal supports it"
85 ," -i ignore INT, QUIT, TERM signals"
86 ," -n NUM specify maximum number of lines (default 1000)"
87 #if defined(KEY_RESIZE)
88 ," -r use old-style sigwinch handler rather than KEY_RESIZE"
91 ," -t trace screen updates"
92 ," -T NUM specify trace mask"
96 for (n = 0; n < SIZEOF(msg); n++)
97 fprintf(stderr, "%s\n", msg[n]);
98 ExitProgram(EXIT_FAILURE);
102 ch_len(NCURSES_CH_T * src)
105 #if USE_WIDEC_SUPPORT
108 #if USE_WIDEC_SUPPORT
109 while (getcchar(src++, NULL, NULL, NULL, NULL) > 0)
119 * Allocate a string into an array of chtype's. If UTF-8 mode is
120 * active, translate the string accordingly.
122 static NCURSES_CH_T *
125 unsigned len = strlen(src);
126 NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
128 #if USE_WIDEC_SUPPORT
129 wchar_t wstr[CCHARW_MAX + 1];
137 #if USE_WIDEC_SUPPORT
138 memset(&state, 0, sizeof(state));
140 for (j = k = 0; j < len; j++) {
141 #if USE_WIDEC_SUPPORT
142 rc = mbrtowc(&wch, src + j, len - j, &state);
143 if (rc == (size_t) -1 || rc == (size_t) -2)
146 if ((width = wcwidth(wch)) < 0)
148 if ((width > 0 && l > 0) || l == CCHARW_MAX) {
151 if (setcchar(dst + k, wstr, 0, 0, NULL) != OK)
155 if (width == 0 && l == 0)
162 #if USE_WIDEC_SUPPORT
165 if (setcchar(dst + k, wstr, 0, 0, NULL) == OK)
168 setcchar(dst + k, L"", 0, 0, NULL);
176 main(int argc, char *argv[])
183 NCURSES_CH_T **olptr;
187 bool got_number = FALSE;
189 bool nonposix_resize = FALSE;
191 const char *my_label = "Input";
193 setlocale(LC_ALL, "");
195 #ifndef NCURSES_VERSION
197 * We know ncurses will catch SIGINT if we don't establish our own handler.
198 * Other versions of curses may/may not catch it.
200 (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
203 while ((i = getopt(argc, argv, "cin:rtT:")) != EOF) {
209 signal(SIGINT, SIG_IGN);
210 signal(SIGQUIT, SIG_IGN);
211 signal(SIGTERM, SIG_IGN);
214 if ((MAXLINES = atoi(optarg)) < 1)
219 nonposix_resize = TRUE;
234 if (optind + 1 != argc)
237 if ((my_lines = typeMalloc(NCURSES_CH_T *, MAXLINES + 2)) == 0)
240 fname = argv[optind];
241 if ((fp = fopen(fname, "r")) == 0) {
243 ExitProgram(EXIT_FAILURE);
247 (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
251 for (lptr = &my_lines[0]; (lptr - my_lines) < MAXLINES; lptr++) {
252 char temp[BUFSIZ], *s, *d;
255 if (fgets(buf, sizeof(buf), fp) == 0)
258 /* convert tabs so that shift will work properly */
259 for (s = buf, d = temp, col = 0; (*d = *s) != '\0'; s++) {
263 } else if (*d == '\t') {
265 while ((d - temp) != col)
268 #if USE_WIDEC_SUPPORT
271 if (isprint(UChar(*d))) {
275 sprintf(d, "\\%03o", UChar(*s));
281 *lptr = ch_dup(temp);
284 length = lptr - my_lines;
286 (void) initscr(); /* initialize the curses library */
287 keypad(stdscr, TRUE); /* enable keyboard mapping */
288 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
289 (void) cbreak(); /* take input chars one at a time, no wait for \n */
290 (void) noecho(); /* don't echo input */
291 nodelay(stdscr, TRUE);
292 idlok(stdscr, TRUE); /* allow use of insert/delete line */
297 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
298 bkgd(COLOR_PAIR(my_pair));
316 my_label = "interrupt";
322 if ((c < 127) && isdigit(c)) {
324 mvprintw(0, 0, "Count: ");
328 value = 10 * value + (c - '0');
333 if (got_number && value) {
340 my_label = keyname(c);
345 for (i = 0; i < n; i++)
346 if ((lptr - my_lines) < (length - LINES + 1))
350 wscrl(stdscr, lptr - olptr);
356 for (i = 0; i < n; i++)
361 wscrl(stdscr, lptr - olptr);
372 lptr = my_lines + length - LINES + 1;
396 case KEY_RESIZE: /* ignore this; ncurses will repaint */
401 halfdelay(my_delay = n);
403 nodelay(stdscr, FALSE);
408 nodelay(stdscr, TRUE);
419 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
425 finish(0); /* we're done */
432 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
437 * This uses functions that are "unsafe", but it seems to work on SunOS and
438 * Linux. Usually: the "unsafe" refers to the functions that POSIX lists
439 * which may be called from a signal handler. Those do not include buffered
440 * I/O, which is used for instance in wrefresh(). To be really portable, you
441 * should use the KEY_RESIZE return (which relies on ncurses' sigwinch
444 * The 'wrefresh(curscr)' is needed to force the refresh to start from the top
445 * of the screen -- some xterms mangle the bitmap while resizing.
450 if (waiting || sig == 0) {
453 if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
454 resize_term(size.ws_row, size.ws_col);
455 wrefresh(curscr); /* Linux needs this */
456 show_all(sig ? "SIGWINCH" : "interrupt");
462 (void) signal(SIGWINCH, adjust); /* some systems need this */
464 #endif /* CAN_RESIZE */
467 show_all(const char *tag)
475 sprintf(temp, "%s (%3dx%3d) col %d ", tag, LINES, COLS, shift);
477 sprintf(temp + i, "view %.*s", (int) (sizeof(temp) - 7 - i), fname);
479 sprintf(temp, "view %.*s", (int) sizeof(temp) - 7, fname);
482 printw("%.*s", COLS, temp);
484 this_time = time((time_t *) 0);
485 strcpy(temp, ctime(&this_time));
486 if ((i = strlen(temp)) != 0) {
488 if (move(0, COLS - i - 2) != ERR)
492 scrollok(stdscr, FALSE); /* prevent screen from moving */
493 for (i = 1; i < LINES; i++) {
495 printw("%3ld:", (long) (lptr + i - my_lines));
497 if ((s = lptr[i - 1]) != 0) {
500 #if USE_WIDEC_SUPPORT
501 add_wchstr(s + shift);
506 wchgat(stdscr, -1, A_NORMAL, my_pair, NULL);
509 setscrreg(1, LINES - 1);
510 scrollok(stdscr, TRUE);