1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 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.104 2017/09/30 17:50:38 tom Exp $
56 #include <test.priv.h>
57 #include <widechars.h>
58 #include <popup_msg.h>
62 #undef CTRL /* conflict on AIX 5.2 with <sys/ioctl.h> */
67 #if !defined(__MINGW32__)
72 #if !defined(sun) || !HAVE_TERMIOS_H
74 # include <sys/ioctl.h>
80 /* This is needed to compile 'struct winsize' */
82 #include <sys/stream.h>
87 #define CTRL(x) ((x) & 0x1f)
89 static void finish(int sig) GCC_NORETURN;
90 static void show_all(const char *tag);
92 #if defined(SIGWINCH) && defined(TIOCGWINSZ) && HAVE_RESIZE_TERM
99 static void adjust(int sig);
100 static int interrupted;
101 static bool waiting = FALSE;
104 static int shift = 0;
105 static bool try_color = FALSE;
108 static NCURSES_CH_T **vec_lines;
109 static NCURSES_CH_T **lptr;
110 static int num_lines;
112 static void usage(void) GCC_NORETURN;
117 static const char *msg[] =
119 "Usage: view [options] file"
122 ," -c use color if terminal supports it"
123 ," -i ignore INT, QUIT, TERM signals"
124 ," -n NUM specify maximum number of lines (default 1000)"
125 #if defined(KEY_RESIZE)
126 ," -r use old-style sigwinch handler rather than KEY_RESIZE"
128 ," -s start in single-step mode, waiting for input"
130 ," -t trace screen updates"
131 ," -T NUM specify trace mask"
135 for (n = 0; n < SIZEOF(msg); n++)
136 fprintf(stderr, "%s\n", msg[n]);
137 ExitProgram(EXIT_FAILURE);
141 ch_len(NCURSES_CH_T * src)
144 #if USE_WIDEC_SUPPORT
148 #if USE_WIDEC_SUPPORT
150 TEST_CCHAR(src, count, {
166 * Allocate a string into an array of chtype's. If UTF-8 mode is
167 * active, translate the string accordingly.
169 static NCURSES_CH_T *
172 unsigned len = (unsigned) strlen(src);
173 NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
175 #if USE_WIDEC_SUPPORT
176 wchar_t wstr[CCHARW_MAX + 1];
184 #endif /* USE_WIDEC_SUPPORT */
186 #if USE_WIDEC_SUPPORT
189 for (j = k = 0; j < len; j++) {
190 #if USE_WIDEC_SUPPORT
191 rc = (size_t) check_mbytes(wch, src + j, len - j, state);
192 if (rc == (size_t) -1 || rc == (size_t) -2) {
196 width = wcwidth(wch);
201 } else if ((l > 0) || (l == CCHARW_MAX)) {
204 if (setcchar(dst + k, wstr, 0, 0, NULL) != OK) {
211 dst[k++] = (chtype) UChar(src[j]);
214 #if USE_WIDEC_SUPPORT
217 if (setcchar(dst + k, wstr, 0, 0, NULL) == OK)
221 setcchar(dst + k, wstr, 0, 0, NULL);
229 main(int argc, char *argv[])
231 static const char *help[] =
234 " q,^Q,ESC - quit this program",
236 " p,<Up> - scroll the viewport up by one row",
237 " n,<Down> - scroll the viewport down by one row",
238 " l,<Left> - scroll the viewport left by one column",
239 " r,<Right> - scroll the viewport right by one column",
241 " h,<Home> - scroll the viewport to top of file",
242 " e,<End> - scroll the viewport to end of file",
244 " ^L - repaint using redrawwin()",
246 " 0 through 9 - enter digits for count",
247 " s - use entered count for halfdelay() parameter",
248 " - if no entered count, stop nodelay()",
249 " <space> - begin nodelay()",
258 NCURSES_CH_T **olptr;
261 bool got_number = FALSE;
262 bool ignore_sigs = FALSE;
263 bool single_step = FALSE;
265 bool nonposix_resize = FALSE;
267 const char *my_label = "Input";
269 setlocale(LC_ALL, "");
271 #ifndef NCURSES_VERSION
273 * We know ncurses will catch SIGINT if we don't establish our own handler.
274 * Other versions of curses may/may not catch it.
276 (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
279 while ((i = getopt(argc, argv, "cin:rstT:")) != -1) {
288 if ((MAXLINES = atoi(optarg)) < 1 ||
294 nonposix_resize = TRUE;
304 int tvalue = (int) strtol(optarg, &next, 0);
305 if (tvalue < 0 || (next != 0 && *next != 0))
307 trace((unsigned) tvalue);
318 if (optind + 1 != argc)
321 if ((vec_lines = typeCalloc(NCURSES_CH_T *, (size_t) MAXLINES + 2)) == 0)
324 assert(vec_lines != 0);
326 fname = argv[optind];
327 if ((fp = fopen(fname, "r")) == 0) {
329 ExitProgram(EXIT_FAILURE);
333 (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
336 Trace(("slurp the file"));
337 for (lptr = &vec_lines[0]; (lptr - vec_lines) < MAXLINES; lptr++) {
338 char temp[BUFSIZ], *s, *d;
341 if (fgets(buf, sizeof(buf), fp) == 0)
344 #if USE_WIDEC_SUPPORT
345 if (lptr == vec_lines) {
346 if (!memcmp("", buf, 3)) {
351 } while ((*d++ = *s++) != '\0');
356 /* convert tabs and nonprinting chars so that shift will work properly */
357 for (s = buf, d = temp, col = 0; (*d = *s) != '\0'; s++) {
368 } else if (*d == '\t') {
370 while ((d - temp) != col)
373 #if USE_WIDEC_SUPPORT
376 if (isprint(UChar(*d))) {
380 _nc_SPRINTF(d, _nc_SLIMIT(sizeof(temp) - (d - buf))
381 "\\%03o", UChar(*s));
383 col = (int) (d - temp);
387 *lptr = ch_dup(temp);
390 num_lines = (int) (lptr - vec_lines);
393 InitAndCatch(initscr(), SIG_IGN);
397 keypad(stdscr, TRUE); /* enable keyboard mapping */
398 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
399 (void) cbreak(); /* take input chars one at a time, no wait for \n */
400 (void) noecho(); /* don't echo input */
402 nodelay(stdscr, TRUE);
403 idlok(stdscr, TRUE); /* allow use of insert/delete line */
408 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
409 bkgd((chtype) COLOR_PAIR(my_pair));
426 my_label = "interrupt";
434 if ((c < 127) && isdigit(c)) {
436 MvPrintw(0, 0, "Count: ");
440 value = 10 * value + (c - '0');
445 if (got_number && value) {
452 my_label = keyname(c);
457 for (i = 0; i < n; i++)
458 if ((lptr - vec_lines) < (num_lines - LINES + 1))
462 scrl((int) (lptr - olptr));
468 for (i = 0; i < n; i++)
469 if (lptr > vec_lines)
473 scrl((int) (lptr - olptr));
483 if (num_lines > LINES)
484 lptr = vec_lines + num_lines - LINES + 1;
510 case KEY_RESIZE: /* ignore this; ncurses will repaint */
516 halfdelay(my_delay = n);
518 nodelay(stdscr, FALSE);
522 nodelay(stdscr, FALSE);
527 nodelay(stdscr, TRUE);
538 popup_msg(stdscr, help);
544 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
550 finish(0); /* we're done */
558 if (vec_lines != 0) {
560 for (n = 0; n < num_lines; ++n) {
566 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
571 * This uses functions that are "unsafe", but it seems to work on SunOS.
572 * Usually: the "unsafe" refers to the functions that POSIX lists which may be
573 * called from a signal handler. Those do not include buffered I/O, which is
574 * used for instance in wrefresh(). To be really portable, you should use the
575 * KEY_RESIZE return (which relies on ncurses' sigwinch handler).
577 * The 'wrefresh(curscr)' is needed to force the refresh to start from the top
578 * of the screen -- some xterms mangle the bitmap while resizing.
583 if (waiting || sig == 0) {
586 if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
587 resize_term(size.ws_row, size.ws_col);
589 show_all(sig ? "SIGWINCH" : "interrupt");
595 (void) signal(SIGWINCH, adjust); /* some systems need this */
597 #endif /* CAN_RESIZE */
600 show_all(const char *tag)
608 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
609 "%.20s (%3dx%3d) col %d ", tag, LINES, COLS, shift);
610 i = (int) strlen(temp);
611 if ((i + 7) < (int) sizeof(temp)) {
612 _nc_SPRINTF(temp + i, _nc_SLIMIT(sizeof(temp) - (size_t) i)
614 (int) (sizeof(temp) - 7 - (size_t) i),
619 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
620 "view %.*s", (int) sizeof(temp) - 7, fname);
623 printw("%.*s", COLS, temp);
625 this_time = time((time_t *) 0);
626 _nc_STRNCPY(temp, ctime(&this_time), (size_t) 30);
627 if ((i = (int) strlen(temp)) != 0) {
629 if (move(0, COLS - i - 2) != ERR)
633 scrollok(stdscr, FALSE); /* prevent screen from moving */
634 for (i = 1; i < LINES; i++) {
636 printw("%3ld:", (long) (lptr + i - vec_lines));
638 if ((s = lptr[i - 1]) != 0) {
641 #if USE_WIDEC_SUPPORT
642 add_wchstr(s + shift);
647 #if defined(NCURSES_VERSION) || defined(HAVE_WCHGAT)
649 wchgat(stdscr, -1, WA_NORMAL, my_pair, NULL);
653 setscrreg(1, LINES - 1);
654 scrollok(stdscr, TRUE);