1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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.83 2011/05/21 18:40:49 tom Exp $
56 #include <test.priv.h>
60 #undef CTRL /* conflict on AIX 5.2 with <sys/ioctl.h> */
65 #if !defined(__MINGW32__)
70 #if !defined(sun) || !HAVE_TERMIOS_H
72 # include <sys/ioctl.h>
78 /* This is needed to compile 'struct winsize' */
80 #include <sys/stream.h>
85 #if HAVE_MBTOWC && HAVE_MBLEN
86 #define reset_mbytes(state) IGNORE_RC(mblen(NULL, 0)), IGNORE_RC(mbtowc(NULL, NULL, 0))
87 #define count_mbytes(buffer,length,state) mblen(buffer,length)
88 #define check_mbytes(wch,buffer,length,state) \
89 (int) mbtowc(&wch, buffer, length)
91 #elif HAVE_MBRTOWC && HAVE_MBRLEN
92 #define reset_mbytes(state) init_mb(state)
93 #define count_mbytes(buffer,length,state) mbrlen(buffer,length,&state)
94 #define check_mbytes(wch,buffer,length,state) \
95 (int) mbrtowc(&wch, buffer, length, &state)
99 #endif /* USE_WIDEC_SUPPORT */
101 static RETSIGTYPE finish(int sig) GCC_NORETURN;
102 static void show_all(const char *tag);
104 #if defined(SIGWINCH) && defined(TIOCGWINSZ) && HAVE_RESIZE_TERM
111 static RETSIGTYPE adjust(int sig);
112 static int interrupted;
115 static bool waiting = FALSE;
116 static int shift = 0;
117 static bool try_color = FALSE;
120 static NCURSES_CH_T **vec_lines;
121 static NCURSES_CH_T **lptr;
122 static int num_lines;
124 static void usage(void) GCC_NORETURN;
129 static const char *msg[] =
131 "Usage: view [options] file"
134 ," -c use color if terminal supports it"
135 ," -i ignore INT, QUIT, TERM signals"
136 ," -n NUM specify maximum number of lines (default 1000)"
137 #if defined(KEY_RESIZE)
138 ," -r use old-style sigwinch handler rather than KEY_RESIZE"
141 ," -t trace screen updates"
142 ," -T NUM specify trace mask"
146 for (n = 0; n < SIZEOF(msg); n++)
147 fprintf(stderr, "%s\n", msg[n]);
148 ExitProgram(EXIT_FAILURE);
152 ch_len(NCURSES_CH_T * src)
155 #if USE_WIDEC_SUPPORT
159 #if USE_WIDEC_SUPPORT
161 TEST_CCHAR(src, count, {
177 * Allocate a string into an array of chtype's. If UTF-8 mode is
178 * active, translate the string accordingly.
180 static NCURSES_CH_T *
183 unsigned len = (unsigned) strlen(src);
184 NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
186 #if USE_WIDEC_SUPPORT
187 wchar_t wstr[CCHARW_MAX + 1];
195 #endif /* USE_WIDEC_SUPPORT */
197 #if USE_WIDEC_SUPPORT
200 for (j = k = 0; j < len; j++) {
201 #if USE_WIDEC_SUPPORT
202 rc = (size_t) check_mbytes(wch, src + j, len - j, state);
203 if (rc == (size_t) -1 || rc == (size_t) -2)
206 if ((width = wcwidth(wch)) < 0)
208 if ((width > 0 && l > 0) || l == CCHARW_MAX) {
211 if (setcchar(dst + k, wstr, 0, 0, NULL) != OK)
215 if (width == 0 && l == 0)
219 dst[k++] = (chtype) UChar(src[j]);
222 #if USE_WIDEC_SUPPORT
225 if (setcchar(dst + k, wstr, 0, 0, NULL) == OK)
229 setcchar(dst + k, wstr, 0, 0, NULL);
237 main(int argc, char *argv[])
244 NCURSES_CH_T **olptr;
247 bool got_number = FALSE;
249 bool nonposix_resize = FALSE;
251 const char *my_label = "Input";
253 setlocale(LC_ALL, "");
255 #ifndef NCURSES_VERSION
257 * We know ncurses will catch SIGINT if we don't establish our own handler.
258 * Other versions of curses may/may not catch it.
260 (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
263 while ((i = getopt(argc, argv, "cin:rtT:")) != -1) {
272 if ((MAXLINES = atoi(optarg)) < 1 ||
278 nonposix_resize = TRUE;
283 trace((unsigned) atoi(optarg));
293 if (optind + 1 != argc)
296 if ((vec_lines = typeCalloc(NCURSES_CH_T *, (size_t) MAXLINES + 2)) == 0)
299 assert(vec_lines != 0);
301 fname = argv[optind];
302 if ((fp = fopen(fname, "r")) == 0) {
304 ExitProgram(EXIT_FAILURE);
308 (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
312 for (lptr = &vec_lines[0]; (lptr - vec_lines) < MAXLINES; lptr++) {
313 char temp[BUFSIZ], *s, *d;
316 if (fgets(buf, sizeof(buf), fp) == 0)
319 /* convert tabs so that shift will work properly */
320 for (s = buf, d = temp, col = 0; (*d = *s) != '\0'; s++) {
324 } else if (*d == '\t') {
326 while ((d - temp) != col)
329 #if USE_WIDEC_SUPPORT
332 if (isprint(UChar(*d))) {
336 sprintf(d, "\\%03o", UChar(*s));
338 col = (int) (d - temp);
342 *lptr = ch_dup(temp);
345 num_lines = (int) (lptr - vec_lines);
347 (void) initscr(); /* initialize the curses library */
348 keypad(stdscr, TRUE); /* enable keyboard mapping */
349 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
350 (void) cbreak(); /* take input chars one at a time, no wait for \n */
351 (void) noecho(); /* don't echo input */
352 nodelay(stdscr, TRUE);
353 idlok(stdscr, TRUE); /* allow use of insert/delete line */
358 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
359 bkgd(COLOR_PAIR(my_pair));
376 my_label = "interrupt";
382 if ((c < 127) && isdigit(c)) {
384 MvPrintw(0, 0, "Count: ");
388 value = 10 * value + (c - '0');
393 if (got_number && value) {
400 my_label = keyname(c);
405 for (i = 0; i < n; i++)
406 if ((lptr - vec_lines) < (num_lines - LINES + 1))
410 scrl((int) (lptr - olptr));
416 for (i = 0; i < n; i++)
417 if (lptr > vec_lines)
421 scrl((int) (lptr - olptr));
431 if (num_lines > LINES)
432 lptr = vec_lines + num_lines - LINES + 1;
456 case KEY_RESIZE: /* ignore this; ncurses will repaint */
461 halfdelay(my_delay = n);
463 nodelay(stdscr, FALSE);
468 nodelay(stdscr, TRUE);
479 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
485 finish(0); /* we're done */
493 if (vec_lines != 0) {
495 for (n = 0; n < num_lines; ++n) {
501 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
506 * This uses functions that are "unsafe", but it seems to work on SunOS.
507 * Usually: the "unsafe" refers to the functions that POSIX lists which may be
508 * called from a signal handler. Those do not include buffered I/O, which is
509 * used for instance in wrefresh(). To be really portable, you should use the
510 * KEY_RESIZE return (which relies on ncurses' sigwinch handler).
512 * The 'wrefresh(curscr)' is needed to force the refresh to start from the top
513 * of the screen -- some xterms mangle the bitmap while resizing.
518 if (waiting || sig == 0) {
521 if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
522 resize_term(size.ws_row, size.ws_col);
524 show_all(sig ? "SIGWINCH" : "interrupt");
530 (void) signal(SIGWINCH, adjust); /* some systems need this */
532 #endif /* CAN_RESIZE */
535 show_all(const char *tag)
543 sprintf(temp, "%.20s (%3dx%3d) col %d ", tag, LINES, COLS, shift);
544 i = (int) strlen(temp);
545 if ((i + 7) < (int) sizeof(temp)) {
546 sprintf(temp + i, "view %.*s",
547 (int) (sizeof(temp) - 7 - (size_t) i),
552 sprintf(temp, "view %.*s", (int) sizeof(temp) - 7, fname);
555 printw("%.*s", COLS, temp);
557 this_time = time((time_t *) 0);
558 strcpy(temp, ctime(&this_time));
559 if ((i = (int) strlen(temp)) != 0) {
561 if (move(0, COLS - i - 2) != ERR)
565 scrollok(stdscr, FALSE); /* prevent screen from moving */
566 for (i = 1; i < LINES; i++) {
568 printw("%3ld:", (long) (lptr + i - vec_lines));
570 if ((s = lptr[i - 1]) != 0) {
573 #if USE_WIDEC_SUPPORT
574 add_wchstr(s + shift);
579 #if defined(NCURSES_VERSION) || defined(HAVE_WCHGAT)
581 wchgat(stdscr, -1, A_NORMAL, my_pair, NULL);
585 setscrreg(1, LINES - 1);
586 scrollok(stdscr, TRUE);