1 /****************************************************************************
2 * Copyright (c) 1998-2013,2015 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.95 2015/10/10 20:03:58 tom Exp $
56 #include <test.priv.h>
57 #include <widechars.h>
61 #undef CTRL /* conflict on AIX 5.2 with <sys/ioctl.h> */
66 #if !defined(__MINGW32__)
71 #if !defined(sun) || !HAVE_TERMIOS_H
73 # include <sys/ioctl.h>
79 /* This is needed to compile 'struct winsize' */
81 #include <sys/stream.h>
86 #define CTRL(x) ((x) & 0x1f)
88 static void finish(int sig) GCC_NORETURN;
89 static void show_all(const char *tag);
91 #if defined(SIGWINCH) && defined(TIOCGWINSZ) && HAVE_RESIZE_TERM
98 static void adjust(int sig);
99 static int interrupted;
100 static bool waiting = FALSE;
103 static int shift = 0;
104 static bool try_color = FALSE;
107 static NCURSES_CH_T **vec_lines;
108 static NCURSES_CH_T **lptr;
109 static int num_lines;
111 static void usage(void) GCC_NORETURN;
116 static const char *msg[] =
118 "Usage: view [options] file"
121 ," -c use color if terminal supports it"
122 ," -i ignore INT, QUIT, TERM signals"
123 ," -n NUM specify maximum number of lines (default 1000)"
124 #if defined(KEY_RESIZE)
125 ," -r use old-style sigwinch handler rather than KEY_RESIZE"
127 ," -s start in single-step mode, waiting for input"
129 ," -t trace screen updates"
130 ," -T NUM specify trace mask"
134 for (n = 0; n < SIZEOF(msg); n++)
135 fprintf(stderr, "%s\n", msg[n]);
136 ExitProgram(EXIT_FAILURE);
140 ch_len(NCURSES_CH_T * src)
143 #if USE_WIDEC_SUPPORT
147 #if USE_WIDEC_SUPPORT
149 TEST_CCHAR(src, count, {
165 * Allocate a string into an array of chtype's. If UTF-8 mode is
166 * active, translate the string accordingly.
168 static NCURSES_CH_T *
171 unsigned len = (unsigned) strlen(src);
172 NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
174 #if USE_WIDEC_SUPPORT
175 wchar_t wstr[CCHARW_MAX + 1];
183 #endif /* USE_WIDEC_SUPPORT */
185 #if USE_WIDEC_SUPPORT
188 for (j = k = 0; j < len; j++) {
189 #if USE_WIDEC_SUPPORT
190 rc = (size_t) check_mbytes(wch, src + j, len - j, state);
191 if (rc == (size_t) -1 || rc == (size_t) -2) {
195 width = wcwidth(wch);
200 } else if ((l > 0) || (l == CCHARW_MAX)) {
203 if (setcchar(dst + k, wstr, 0, 0, NULL) != OK) {
210 dst[k++] = (chtype) UChar(src[j]);
213 #if USE_WIDEC_SUPPORT
216 if (setcchar(dst + k, wstr, 0, 0, NULL) == OK)
220 setcchar(dst + k, wstr, 0, 0, NULL);
228 main(int argc, char *argv[])
235 NCURSES_CH_T **olptr;
238 bool got_number = FALSE;
239 bool single_step = FALSE;
241 bool nonposix_resize = FALSE;
243 const char *my_label = "Input";
245 setlocale(LC_ALL, "");
247 #ifndef NCURSES_VERSION
249 * We know ncurses will catch SIGINT if we don't establish our own handler.
250 * Other versions of curses may/may not catch it.
252 (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
255 while ((i = getopt(argc, argv, "cin:rstT:")) != -1) {
264 if ((MAXLINES = atoi(optarg)) < 1 ||
270 nonposix_resize = TRUE;
280 int tvalue = (int) strtol(optarg, &next, 0);
281 if (tvalue < 0 || (next != 0 && *next != 0))
283 trace((unsigned) tvalue);
294 if (optind + 1 != argc)
297 if ((vec_lines = typeCalloc(NCURSES_CH_T *, (size_t) MAXLINES + 2)) == 0)
300 assert(vec_lines != 0);
302 fname = argv[optind];
303 if ((fp = fopen(fname, "r")) == 0) {
305 ExitProgram(EXIT_FAILURE);
309 (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
312 Trace(("slurp the file"));
313 for (lptr = &vec_lines[0]; (lptr - vec_lines) < MAXLINES; lptr++) {
314 char temp[BUFSIZ], *s, *d;
317 if (fgets(buf, sizeof(buf), fp) == 0)
320 #if USE_WIDEC_SUPPORT
321 if (lptr == vec_lines) {
322 if (!memcmp("", buf, 3)) {
327 } while ((*d++ = *s++) != '\0');
332 /* convert tabs and nonprinting chars so that shift will work properly */
333 for (s = buf, d = temp, col = 0; (*d = *s) != '\0'; s++) {
344 } else if (*d == '\t') {
346 while ((d - temp) != col)
349 #if USE_WIDEC_SUPPORT
352 if (isprint(UChar(*d))) {
356 sprintf(d, "\\%03o", UChar(*s));
358 col = (int) (d - temp);
362 *lptr = ch_dup(temp);
365 num_lines = (int) (lptr - vec_lines);
367 (void) initscr(); /* initialize the curses library */
368 keypad(stdscr, TRUE); /* enable keyboard mapping */
369 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
370 (void) cbreak(); /* take input chars one at a time, no wait for \n */
371 (void) noecho(); /* don't echo input */
373 nodelay(stdscr, TRUE);
374 idlok(stdscr, TRUE); /* allow use of insert/delete line */
379 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
380 bkgd((chtype) COLOR_PAIR(my_pair));
397 my_label = "interrupt";
405 if ((c < 127) && isdigit(c)) {
407 MvPrintw(0, 0, "Count: ");
411 value = 10 * value + (c - '0');
416 if (got_number && value) {
423 my_label = keyname(c);
428 for (i = 0; i < n; i++)
429 if ((lptr - vec_lines) < (num_lines - LINES + 1))
433 scrl((int) (lptr - olptr));
439 for (i = 0; i < n; i++)
440 if (lptr > vec_lines)
444 scrl((int) (lptr - olptr));
454 if (num_lines > LINES)
455 lptr = vec_lines + num_lines - LINES + 1;
479 case KEY_RESIZE: /* ignore this; ncurses will repaint */
484 halfdelay(my_delay = n);
486 nodelay(stdscr, FALSE);
491 nodelay(stdscr, TRUE);
505 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
511 finish(0); /* we're done */
519 if (vec_lines != 0) {
521 for (n = 0; n < num_lines; ++n) {
527 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
532 * This uses functions that are "unsafe", but it seems to work on SunOS.
533 * Usually: the "unsafe" refers to the functions that POSIX lists which may be
534 * called from a signal handler. Those do not include buffered I/O, which is
535 * used for instance in wrefresh(). To be really portable, you should use the
536 * KEY_RESIZE return (which relies on ncurses' sigwinch handler).
538 * The 'wrefresh(curscr)' is needed to force the refresh to start from the top
539 * of the screen -- some xterms mangle the bitmap while resizing.
544 if (waiting || sig == 0) {
547 if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
548 resize_term(size.ws_row, size.ws_col);
550 show_all(sig ? "SIGWINCH" : "interrupt");
556 (void) signal(SIGWINCH, adjust); /* some systems need this */
558 #endif /* CAN_RESIZE */
561 show_all(const char *tag)
569 sprintf(temp, "%.20s (%3dx%3d) col %d ", tag, LINES, COLS, shift);
570 i = (int) strlen(temp);
571 if ((i + 7) < (int) sizeof(temp)) {
572 sprintf(temp + i, "view %.*s",
573 (int) (sizeof(temp) - 7 - (size_t) i),
578 sprintf(temp, "view %.*s", (int) sizeof(temp) - 7, fname);
581 printw("%.*s", COLS, temp);
583 this_time = time((time_t *) 0);
584 strncpy(temp, ctime(&this_time), (size_t) 30);
585 if ((i = (int) strlen(temp)) != 0) {
587 if (move(0, COLS - i - 2) != ERR)
591 scrollok(stdscr, FALSE); /* prevent screen from moving */
592 for (i = 1; i < LINES; i++) {
594 printw("%3ld:", (long) (lptr + i - vec_lines));
596 if ((s = lptr[i - 1]) != 0) {
599 #if USE_WIDEC_SUPPORT
600 add_wchstr(s + shift);
605 #if defined(NCURSES_VERSION) || defined(HAVE_WCHGAT)
607 wchgat(stdscr, -1, A_NORMAL, my_pair, NULL);
611 setscrreg(1, LINES - 1);
612 scrollok(stdscr, TRUE);