1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * view.c -- a silly little viewer program
32 * written by Eric S. Raymond <esr@snark.thyrsus.com> December 1994
33 * to test the scrolling code in ncurses.
35 * modified by Thomas Dickey <dickey@clark.net> July 1995 to demonstrate
36 * the use of 'resizeterm()', and May 2000 to illustrate wide-character
37 * handling. This program intentionally does not use pads, to allow testing
38 * with less-capable implementations of curses.
40 * Takes a filename argument. It's a simple file-viewer with various
41 * scroll-up and scroll-down commands.
43 * n -- scroll one line forward
44 * p -- scroll one line back
46 * Either command accepts a numeric prefix interpreted as a repeat count.
47 * Thus, typing `5n' should scroll forward 5 lines in the file.
49 * The way you can tell this is working OK is that, in the trace file,
50 * there should be one scroll operation plus a small number of line
51 * updates, as opposed to a whole-page update. This means the physical
52 * scroll operation worked, and the refresh() code only had to do a
55 * $Id: view.c,v 1.138 2020/02/02 23:34:34 tom Exp $
58 #include <test.priv.h>
59 #include <widechars.h>
60 #include <popup_msg.h>
65 static void finish(int sig) GCC_NORETURN;
70 static bool try_color = FALSE;
73 static NCURSES_CH_T **vec_lines;
74 static NCURSES_CH_T **lptr;
78 static bool n_option = FALSE;
81 static void usage(void) GCC_NORETURN;
84 failed(const char *msg)
87 fprintf(stderr, "%s\n", msg);
88 ExitProgram(EXIT_FAILURE);
92 ch_len(NCURSES_CH_T *src)
101 TEST_CCHAR(src, count, {
102 int len = wcwidth(test_wch[0]);
103 result += (len > 0) ? len : 1;
122 if (vec_lines != 0) {
124 for (n = 0; n < num_lines; ++n) {
130 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
134 show_all(const char *tag)
142 for (digits = 1, i = num_lines; i > 0; i /= 10) {
146 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
147 "view %.*s", (int) strlen(tag), tag);
148 i = (int) strlen(temp);
149 _nc_SPRINTF(temp + i, _nc_SLIMIT(sizeof(temp) - (size_t) i)
150 " %.*s", (int) sizeof(temp) - i - 2, fname);
152 printw("%.*s", COLS, temp);
154 this_time = time((time_t *) 0);
155 _nc_STRNCPY(temp, ctime(&this_time), (size_t) 30);
156 if ((i = (int) strlen(temp)) != 0) {
158 if (move(0, COLS - i - 2) != ERR)
162 scrollok(stdscr, FALSE); /* prevent screen from moving */
163 for (i = 1; i < LINES; i++) {
165 int actual = (int) (lptr + i - vec_lines);
166 if (actual > num_lines) {
177 printw("%*d:", digits, actual);
179 if ((s = lptr[i - 1]) == 0) {
184 #if USE_WIDEC_SUPPORT
186 * An index into an array of cchar_t's is not necessarily the same
187 * as the column-offset. A pad would do this directly. Here we
188 * must translate (or compute a table of offsets).
192 int width = 1, count;
193 for (j = actual = 0; j < shift; ++j) {
194 TEST_CCHAR(s + j, count, {
195 width = wcwidth(test_wch[0]);
201 if (actual > shift) {
203 } else if (actual == shift) {
211 add_wchstr(s + j + (actual > shift));
218 #if defined(NCURSES_VERSION) || defined(HAVE_WCHGAT)
220 wchgat(stdscr, -1, WA_NORMAL, my_pair, NULL);
223 setscrreg(1, LINES - 1);
224 scrollok(stdscr, TRUE);
229 read_file(const char *filename)
242 if (stat(filename, &sb) != 0
243 || (sb.st_mode & S_IFMT) != S_IFREG) {
244 failed("input is not a file");
247 if (sb.st_size == 0) {
248 failed("input is empty");
251 if ((fp = fopen(filename, "r")) == 0) {
252 failed("cannot open input-file");
255 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0) {
256 failed("cannot allocate memory for input-file");
259 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
260 my_blob[sb.st_size] = '\0';
263 for (pass = 0; pass < 2; ++pass) {
264 char *base = my_blob;
266 for (j = 0; j < len; ++j) {
267 if (my_blob[j] == '\n') {
272 base = my_blob + j + 1;
277 if (base != (my_blob + j))
280 ((my_vec = typeCalloc(char *, (size_t) k + 2)) == 0)) {
281 failed("cannot allocate line-vector #1");
285 #if USE_WIDEC_SUPPORT
286 if (!memcmp("\357\273\277", my_blob, 3)) {
287 char *s = my_blob + 3;
291 } while ((*d++ = *s++) != '\0');
295 width = (int) strlen(my_vec[0]);
296 for (k = 1; my_vec[k]; ++k) {
297 int check = (int) (my_vec[k] - my_vec[k - 1]);
301 width = (width + 1) * 5;
302 my_win = newwin(2, width, 0, 0);
304 failed("cannot allocate temporary window");
307 if ((vec_lines = typeCalloc(NCURSES_CH_T *, (size_t) num_lines + 2)) == 0) {
308 failed("cannot allocate line-vector #2");
312 * Use the curses library for rendering, including tab-conversion. This
313 * will not make the resulting array's indices correspond to column for
314 * lines containing double-width cells because the "in_wch" functions will
315 * ignore the skipped cells. Use pads for that sort of thing.
317 Trace(("slurp the file"));
318 for (k = 0; my_vec[k]; ++k) {
321 #if USE_WIDEC_SUPPORT
322 char *last = my_vec[k] + (int) strlen(my_vec[k]);
328 #endif /* USE_WIDEC_SUPPORT */
332 #if USE_WIDEC_SUPPORT
336 for (s = my_vec[k]; *s != '\0'; ++s) {
337 #if USE_WIDEC_SUPPORT
339 rc = (size_t) check_mbytes(wch[0], s, (size_t) (last - s), state);
340 if ((long) rc == -1 || (long) rc == -2) {
344 waddwstr(my_win, wch);
347 waddch(my_win, *s & 0xff);
353 /* "x + 1" works with standard curses; some implementations are buggy */
354 if ((vec_lines[k] = typeCalloc(NCURSES_CH_T, x + width + 1)) == 0) {
355 failed("cannot allocate line-vector #3");
357 #if USE_WIDEC_SUPPORT
358 win_wchnstr(my_win, vec_lines[k], x);
360 winchnstr(my_win, vec_lines[k], x);
372 static const char *msg[] =
374 "Usage: view [options] file"
377 ," -c use color if terminal supports it"
378 ," -i ignore INT, QUIT, TERM signals"
379 #if USE_WIDEC_SUPPORT
380 ," -n use waddch (bytes) rather then wadd_wch (wide-chars)"
382 ," -s start in single-step mode, waiting for input"
384 ," -t trace screen updates"
385 ," -T NUM specify trace mask"
389 for (n = 0; n < SIZEOF(msg); n++)
390 fprintf(stderr, "%s\n", msg[n]);
391 ExitProgram(EXIT_FAILURE);
395 main(int argc, char *argv[])
397 static const char *help[] =
400 " q,^Q,ESC - quit this program",
402 " p,<Up> - scroll the viewport up by one row",
403 " n,<Down> - scroll the viewport down by one row",
404 " l,<Left> - scroll the viewport left by one column",
405 " r,<Right> - scroll the viewport right by one column",
406 " <,> - scroll the viewport left/right by 8 columns",
408 " h,<Home> - scroll the viewport to top of file",
409 " ^F,<PageDn> - scroll to the next page",
410 " ^B,<PageUp> - scroll to the previous page",
411 " e,<End> - scroll the viewport to end of file",
413 " ^L - repaint using redrawwin()",
415 " 0 through 9 - enter digits for count",
416 " s - use entered count for halfdelay() parameter",
417 " - if no entered count, stop nodelay()",
418 " <space> - begin nodelay()",
424 NCURSES_CH_T **olptr;
427 bool got_number = FALSE;
428 bool ignore_sigs = FALSE;
429 bool single_step = FALSE;
430 const char *my_label = "Input";
432 setlocale(LC_ALL, "");
434 while ((i = getopt(argc, argv, "cinstT:")) != -1) {
442 #if USE_WIDEC_SUPPORT
454 int tvalue = (int) strtol(optarg, &next, 0);
455 if (tvalue < 0 || (next != 0 && *next != 0))
457 curses_trace((unsigned) tvalue);
461 curses_trace(TRACE_CALLS);
468 if (optind + 1 != argc)
471 InitAndCatch(initscr(), ignore_sigs ? SIG_IGN : finish);
472 keypad(stdscr, TRUE); /* enable keyboard mapping */
473 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
474 (void) cbreak(); /* take input chars one at a time, no wait for \n */
475 (void) noecho(); /* don't echo input */
477 nodelay(stdscr, TRUE);
478 idlok(stdscr, TRUE); /* allow use of insert/delete line */
480 read_file(fname = argv[optind]);
485 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
486 bkgd((chtype) COLOR_PAIR(my_pair));
501 if ((c < 127) && isdigit(c)) {
503 MvPrintw(0, 0, "Count: ");
507 value = 10 * value + (c - '0');
512 if (got_number && value) {
519 my_label = keyname(c);
524 for (i = 0; i < n; i++)
525 if ((lptr - vec_lines) < (num_lines - LINES + 1))
529 scrl((int) (lptr - olptr));
535 for (i = 0; i < n; i++)
536 if (lptr > vec_lines)
540 scrl((int) (lptr - olptr));
550 if ((shift -= 8) < 0)
560 if (num_lines > LINES)
561 lptr = (vec_lines + num_lines - LINES + 1);
563 lptr = (vec_lines + (num_lines - 2));
569 for (i = 0; i < n; i++) {
570 if ((lptr - vec_lines) < (num_lines - 5))
573 lptr = (vec_lines + num_lines - 2);
580 for (i = 0; i < n; i++) {
581 if ((lptr - vec_lines) >= LINES)
609 case KEY_RESIZE: /* ignore this; ncurses will repaint */
615 halfdelay(my_delay = n);
617 nodelay(stdscr, FALSE);
621 nodelay(stdscr, FALSE);
626 nodelay(stdscr, TRUE);
637 popup_msg(stdscr, help);
643 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
649 finish(0); /* we're done */