1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * view.c -- a silly little viewer program
31 * written by Eric S. Raymond <esr@snark.thyrsus.com> December 1994
32 * to test the scrolling code in ncurses.
34 * modified by Thomas Dickey <dickey@clark.net> July 1995 to demonstrate
35 * the use of 'resizeterm()', and May 2000 to illustrate wide-character
38 * Takes a filename argument. It's a simple file-viewer with various
39 * scroll-up and scroll-down commands.
41 * n -- scroll one line forward
42 * p -- scroll one line back
44 * Either command accepts a numeric prefix interpreted as a repeat count.
45 * Thus, typing `5n' should scroll forward 5 lines in the file.
47 * The way you can tell this is working OK is that, in the trace file,
48 * there should be one scroll operation plus a small number of line
49 * updates, as opposed to a whole-page update. This means the physical
50 * scroll operation worked, and the refresh() code only had to do a
53 * $Id: view.c,v 1.64 2006/05/20 15:37:03 tom Exp $
56 #include <test.priv.h>
60 #undef CTRL /* conflict on AIX 5.2 with <sys/ioctl.h> */
68 #if !defined(sun) || !HAVE_TERMIOS_H
70 # include <sys/ioctl.h>
76 /* This is needed to compile 'struct winsize' */
78 #include <sys/stream.h>
82 static RETSIGTYPE finish(int sig) GCC_NORETURN;
83 static void show_all(const char *tag);
85 #if defined(SIGWINCH) && defined(TIOCGWINSZ) && HAVE_RESIZE_TERM
92 static RETSIGTYPE adjust(int sig);
93 static int interrupted;
96 static bool waiting = FALSE;
98 static bool try_color = FALSE;
101 static NCURSES_CH_T **vec_lines;
102 static NCURSES_CH_T **lptr;
103 static int num_lines;
108 static const char *msg[] =
110 "Usage: view [options] file"
113 ," -c use color if terminal supports it"
114 ," -i ignore INT, QUIT, TERM signals"
115 ," -n NUM specify maximum number of lines (default 1000)"
116 #if defined(KEY_RESIZE)
117 ," -r use old-style sigwinch handler rather than KEY_RESIZE"
120 ," -t trace screen updates"
121 ," -T NUM specify trace mask"
125 for (n = 0; n < SIZEOF(msg); n++)
126 fprintf(stderr, "%s\n", msg[n]);
127 ExitProgram(EXIT_FAILURE);
131 ch_len(NCURSES_CH_T * src)
134 #if USE_WIDEC_SUPPORT
137 #if USE_WIDEC_SUPPORT
138 while (getcchar(src++, NULL, NULL, NULL, NULL) > 0)
148 * Allocate a string into an array of chtype's. If UTF-8 mode is
149 * active, translate the string accordingly.
151 static NCURSES_CH_T *
154 unsigned len = strlen(src);
155 NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
157 #if USE_WIDEC_SUPPORT
158 wchar_t wstr[CCHARW_MAX + 1];
166 #if USE_WIDEC_SUPPORT
167 memset(&state, 0, sizeof(state));
169 for (j = k = 0; j < len; j++) {
170 #if USE_WIDEC_SUPPORT
171 rc = mbrtowc(&wch, src + j, len - j, &state);
172 if (rc == (size_t) -1 || rc == (size_t) -2)
175 if ((width = wcwidth(wch)) < 0)
177 if ((width > 0 && l > 0) || l == CCHARW_MAX) {
180 if (setcchar(dst + k, wstr, 0, 0, NULL) != OK)
184 if (width == 0 && l == 0)
191 #if USE_WIDEC_SUPPORT
194 if (setcchar(dst + k, wstr, 0, 0, NULL) == OK)
198 setcchar(dst + k, wstr, 0, 0, NULL);
206 main(int argc, char *argv[])
213 NCURSES_CH_T **olptr;
216 bool got_number = FALSE;
218 bool nonposix_resize = FALSE;
220 const char *my_label = "Input";
222 setlocale(LC_ALL, "");
224 #ifndef NCURSES_VERSION
226 * We know ncurses will catch SIGINT if we don't establish our own handler.
227 * Other versions of curses may/may not catch it.
229 (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
232 while ((i = getopt(argc, argv, "cin:rtT:")) != EOF) {
241 if ((MAXLINES = atoi(optarg)) < 1)
246 nonposix_resize = TRUE;
251 trace((unsigned) atoi(optarg));
261 if (optind + 1 != argc)
264 if ((vec_lines = typeMalloc(NCURSES_CH_T *, MAXLINES + 2)) == 0)
267 fname = argv[optind];
268 if ((fp = fopen(fname, "r")) == 0) {
270 ExitProgram(EXIT_FAILURE);
274 (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
278 for (lptr = &vec_lines[0]; (lptr - vec_lines) < MAXLINES; lptr++) {
279 char temp[BUFSIZ], *s, *d;
282 if (fgets(buf, sizeof(buf), fp) == 0)
285 /* convert tabs so that shift will work properly */
286 for (s = buf, d = temp, col = 0; (*d = *s) != '\0'; s++) {
290 } else if (*d == '\t') {
292 while ((d - temp) != col)
295 #if USE_WIDEC_SUPPORT
298 if (isprint(UChar(*d))) {
302 sprintf(d, "\\%03o", UChar(*s));
308 *lptr = ch_dup(temp);
311 num_lines = lptr - vec_lines;
313 (void) initscr(); /* initialize the curses library */
314 keypad(stdscr, TRUE); /* enable keyboard mapping */
315 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
316 (void) cbreak(); /* take input chars one at a time, no wait for \n */
317 (void) noecho(); /* don't echo input */
318 nodelay(stdscr, TRUE);
319 idlok(stdscr, TRUE); /* allow use of insert/delete line */
324 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
325 bkgd(COLOR_PAIR(my_pair));
343 my_label = "interrupt";
349 if ((c < 127) && isdigit(c)) {
351 mvprintw(0, 0, "Count: ");
355 value = 10 * value + (c - '0');
360 if (got_number && value) {
367 my_label = keyname(c);
372 for (i = 0; i < n; i++)
373 if ((lptr - vec_lines) < (num_lines - LINES + 1))
377 wscrl(stdscr, lptr - olptr);
383 for (i = 0; i < n; i++)
384 if (lptr > vec_lines)
388 wscrl(stdscr, lptr - olptr);
398 if (num_lines > LINES)
399 lptr = vec_lines + num_lines - LINES + 1;
423 case KEY_RESIZE: /* ignore this; ncurses will repaint */
428 halfdelay(my_delay = n);
430 nodelay(stdscr, FALSE);
435 nodelay(stdscr, TRUE);
446 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
452 finish(0); /* we're done */
460 if (vec_lines != 0) {
462 for (n = 0; n < num_lines; ++n) {
468 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
473 * This uses functions that are "unsafe", but it seems to work on SunOS and
474 * Linux. Usually: the "unsafe" refers to the functions that POSIX lists
475 * which may be called from a signal handler. Those do not include buffered
476 * I/O, which is used for instance in wrefresh(). To be really portable, you
477 * should use the KEY_RESIZE return (which relies on ncurses' sigwinch
480 * The 'wrefresh(curscr)' is needed to force the refresh to start from the top
481 * of the screen -- some xterms mangle the bitmap while resizing.
486 if (waiting || sig == 0) {
489 if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
490 resize_term(size.ws_row, size.ws_col);
491 wrefresh(curscr); /* Linux needs this */
492 show_all(sig ? "SIGWINCH" : "interrupt");
498 (void) signal(SIGWINCH, adjust); /* some systems need this */
500 #endif /* CAN_RESIZE */
503 show_all(const char *tag)
511 sprintf(temp, "%s (%3dx%3d) col %d ", tag, LINES, COLS, shift);
513 sprintf(temp + i, "view %.*s", (int) (sizeof(temp) - 7 - i), fname);
515 sprintf(temp, "view %.*s", (int) sizeof(temp) - 7, fname);
518 printw("%.*s", COLS, temp);
520 this_time = time((time_t *) 0);
521 strcpy(temp, ctime(&this_time));
522 if ((i = strlen(temp)) != 0) {
524 if (move(0, COLS - i - 2) != ERR)
528 scrollok(stdscr, FALSE); /* prevent screen from moving */
529 for (i = 1; i < LINES; i++) {
531 printw("%3ld:", (long) (lptr + i - vec_lines));
533 if ((s = lptr[i - 1]) != 0) {
536 #if USE_WIDEC_SUPPORT
537 add_wchstr(s + shift);
542 #if defined(NCURSES_VERSION) || defined(HAVE_WCHGAT)
544 wchgat(stdscr, -1, A_NORMAL, my_pair, NULL);
548 setscrreg(1, LINES - 1);
549 scrollok(stdscr, TRUE);