1 /****************************************************************************
2 * Copyright (c) 1998-2017,2019 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
36 * handling. This program intentionally does not use pads, to allow testing
37 * with less-capable implementations of curses.
39 * Takes a filename argument. It's a simple file-viewer with various
40 * scroll-up and scroll-down commands.
42 * n -- scroll one line forward
43 * p -- scroll one line back
45 * Either command accepts a numeric prefix interpreted as a repeat count.
46 * Thus, typing `5n' should scroll forward 5 lines in the file.
48 * The way you can tell this is working OK is that, in the trace file,
49 * there should be one scroll operation plus a small number of line
50 * updates, as opposed to a whole-page update. This means the physical
51 * scroll operation worked, and the refresh() code only had to do a
54 * $Id: view.c,v 1.136 2019/01/27 00:52:06 tom Exp $
57 #include <test.priv.h>
58 #include <widechars.h>
59 #include <popup_msg.h>
64 static void finish(int sig) GCC_NORETURN;
69 static bool try_color = FALSE;
72 static NCURSES_CH_T **vec_lines;
73 static NCURSES_CH_T **lptr;
77 static bool n_option = FALSE;
80 static void usage(void) GCC_NORETURN;
83 failed(const char *msg)
86 fprintf(stderr, "%s\n", msg);
87 ExitProgram(EXIT_FAILURE);
91 ch_len(NCURSES_CH_T * src)
100 TEST_CCHAR(src, count, {
101 int len = wcwidth(test_wch[0]);
102 result += (len > 0) ? len : 1;
121 if (vec_lines != 0) {
123 for (n = 0; n < num_lines; ++n) {
129 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
133 show_all(const char *tag)
141 for (digits = 1, i = num_lines; i > 0; i /= 10) {
145 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
146 "view %.*s", (int) strlen(tag), tag);
147 i = (int) strlen(temp);
148 _nc_SPRINTF(temp + i, _nc_SLIMIT(sizeof(temp) - (size_t) i)
149 " %.*s", (int) sizeof(temp) - i - 2, fname);
151 printw("%.*s", COLS, temp);
153 this_time = time((time_t *) 0);
154 _nc_STRNCPY(temp, ctime(&this_time), (size_t) 30);
155 if ((i = (int) strlen(temp)) != 0) {
157 if (move(0, COLS - i - 2) != ERR)
161 scrollok(stdscr, FALSE); /* prevent screen from moving */
162 for (i = 1; i < LINES; i++) {
164 int actual = (int) (lptr + i - vec_lines);
165 if (actual > num_lines) {
176 printw("%*d:", digits, actual);
178 if ((s = lptr[i - 1]) == 0) {
183 #if USE_WIDEC_SUPPORT
185 * An index into an array of cchar_t's is not necessarily the same
186 * as the column-offset. A pad would do this directly. Here we
187 * must translate (or compute a table of offsets).
191 int width = 1, count;
192 for (j = actual = 0; j < shift; ++j) {
193 TEST_CCHAR(s + j, count, {
194 width = wcwidth(test_wch[0]);
200 if (actual > shift) {
202 } else if (actual == shift) {
210 add_wchstr(s + j + (actual > shift));
217 #if defined(NCURSES_VERSION) || defined(HAVE_WCHGAT)
219 wchgat(stdscr, -1, WA_NORMAL, my_pair, NULL);
222 setscrreg(1, LINES - 1);
223 scrollok(stdscr, TRUE);
228 read_file(const char *filename)
241 if (stat(filename, &sb) != 0
242 || (sb.st_mode & S_IFMT) != S_IFREG) {
243 failed("input is not a file");
246 if (sb.st_size == 0) {
247 failed("input is empty");
250 if ((fp = fopen(filename, "r")) == 0) {
251 failed("cannot open input-file");
254 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0) {
255 failed("cannot allocate memory for input-file");
258 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
259 my_blob[sb.st_size] = '\0';
262 for (pass = 0; pass < 2; ++pass) {
263 char *base = my_blob;
265 for (j = 0; j < len; ++j) {
266 if (my_blob[j] == '\n') {
271 base = my_blob + j + 1;
276 if (base != (my_blob + j))
279 ((my_vec = typeCalloc(char *, (size_t) k + 2)) == 0)) {
280 failed("cannot allocate line-vector #1");
284 #if USE_WIDEC_SUPPORT
285 if (!memcmp("\357\273\277", my_blob, 3)) {
286 char *s = my_blob + 3;
290 } while ((*d++ = *s++) != '\0');
294 width = (int) strlen(my_vec[0]);
295 for (k = 1; my_vec[k]; ++k) {
296 int check = (int) (my_vec[k] - my_vec[k - 1]);
300 width = (width + 1) * 5;
301 my_win = newwin(2, width, 0, 0);
303 failed("cannot allocate temporary window");
305 if ((vec_lines = typeCalloc(NCURSES_CH_T *, (size_t) num_lines + 2)) == 0)
306 failed("cannot allocate line-vector #2");
309 * Use the curses library for rendering, including tab-conversion. This
310 * will not make the resulting array's indices correspond to column for
311 * lines containing double-width cells because the "in_wch" functions will
312 * ignore the skipped cells. Use pads for that sort of thing.
314 Trace(("slurp the file"));
315 for (k = 0; my_vec[k]; ++k) {
318 #if USE_WIDEC_SUPPORT
319 char *last = my_vec[k] + (int) strlen(my_vec[k]);
325 #endif /* USE_WIDEC_SUPPORT */
329 #if USE_WIDEC_SUPPORT
333 for (s = my_vec[k]; *s != '\0'; ++s) {
334 #if USE_WIDEC_SUPPORT
336 rc = (size_t) check_mbytes(wch[0], s, (size_t) (last - s), state);
337 if ((long) rc == -1 || (long) rc == -2) {
341 waddwstr(my_win, wch);
344 waddch(my_win, *s & 0xff);
350 /* "x + 1" works with standard curses; some implementations are buggy */
351 if ((vec_lines[k] = typeCalloc(NCURSES_CH_T, x + width + 1)) == 0)
352 failed("cannot allocate line-vector #3");
353 #if USE_WIDEC_SUPPORT
354 win_wchnstr(my_win, vec_lines[k], x);
356 winchnstr(my_win, vec_lines[k], x);
368 static const char *msg[] =
370 "Usage: view [options] file"
373 ," -c use color if terminal supports it"
374 ," -i ignore INT, QUIT, TERM signals"
375 #if USE_WIDEC_SUPPORT
376 ," -n use waddch (bytes) rather then wadd_wch (wide-chars)"
378 ," -s start in single-step mode, waiting for input"
380 ," -t trace screen updates"
381 ," -T NUM specify trace mask"
385 for (n = 0; n < SIZEOF(msg); n++)
386 fprintf(stderr, "%s\n", msg[n]);
387 ExitProgram(EXIT_FAILURE);
391 main(int argc, char *argv[])
393 static const char *help[] =
396 " q,^Q,ESC - quit this program",
398 " p,<Up> - scroll the viewport up by one row",
399 " n,<Down> - scroll the viewport down by one row",
400 " l,<Left> - scroll the viewport left by one column",
401 " r,<Right> - scroll the viewport right by one column",
402 " <,> - scroll the viewport left/right by 8 columns",
404 " h,<Home> - scroll the viewport to top of file",
405 " ^F,<PageDn> - scroll to the next page",
406 " ^B,<PageUp> - scroll to the previous page",
407 " e,<End> - scroll the viewport to end of file",
409 " ^L - repaint using redrawwin()",
411 " 0 through 9 - enter digits for count",
412 " s - use entered count for halfdelay() parameter",
413 " - if no entered count, stop nodelay()",
414 " <space> - begin nodelay()",
420 NCURSES_CH_T **olptr;
423 bool got_number = FALSE;
424 bool ignore_sigs = FALSE;
425 bool single_step = FALSE;
426 const char *my_label = "Input";
428 setlocale(LC_ALL, "");
430 while ((i = getopt(argc, argv, "cinstT:")) != -1) {
438 #if USE_WIDEC_SUPPORT
450 int tvalue = (int) strtol(optarg, &next, 0);
451 if (tvalue < 0 || (next != 0 && *next != 0))
453 trace((unsigned) tvalue);
464 if (optind + 1 != argc)
467 InitAndCatch(initscr(), ignore_sigs ? SIG_IGN : finish);
468 keypad(stdscr, TRUE); /* enable keyboard mapping */
469 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
470 (void) cbreak(); /* take input chars one at a time, no wait for \n */
471 (void) noecho(); /* don't echo input */
473 nodelay(stdscr, TRUE);
474 idlok(stdscr, TRUE); /* allow use of insert/delete line */
476 read_file(fname = argv[optind]);
481 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
482 bkgd((chtype) COLOR_PAIR(my_pair));
497 if ((c < 127) && isdigit(c)) {
499 MvPrintw(0, 0, "Count: ");
503 value = 10 * value + (c - '0');
508 if (got_number && value) {
515 my_label = keyname(c);
520 for (i = 0; i < n; i++)
521 if ((lptr - vec_lines) < (num_lines - LINES + 1))
525 scrl((int) (lptr - olptr));
531 for (i = 0; i < n; i++)
532 if (lptr > vec_lines)
536 scrl((int) (lptr - olptr));
546 if ((shift -= 8) < 0)
556 if (num_lines > LINES)
557 lptr = (vec_lines + num_lines - LINES + 1);
559 lptr = (vec_lines + (num_lines - 2));
565 for (i = 0; i < n; i++) {
566 if ((lptr - vec_lines) < (num_lines - 5))
569 lptr = (vec_lines + num_lines - 2);
576 for (i = 0; i < n; i++) {
577 if ((lptr - vec_lines) >= LINES)
605 case KEY_RESIZE: /* ignore this; ncurses will repaint */
611 halfdelay(my_delay = n);
613 nodelay(stdscr, FALSE);
617 nodelay(stdscr, FALSE);
622 nodelay(stdscr, TRUE);
633 popup_msg(stdscr, help);
639 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
645 finish(0); /* we're done */