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
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.129 2017/10/22 00:49:23 tom Exp $
57 #include <test.priv.h>
58 #include <widechars.h>
59 #include <popup_msg.h>
64 #undef CTRL /* conflict on AIX 5.2 with <sys/ioctl.h> */
66 static void finish(int sig) GCC_NORETURN;
71 #define CTRL(x) ((x) & 0x1f)
74 static bool try_color = FALSE;
77 static NCURSES_CH_T **vec_lines;
78 static NCURSES_CH_T **lptr;
82 static bool n_option = FALSE;
85 static void usage(void) GCC_NORETURN;
88 failed(const char *msg)
91 fprintf(stderr, "%s\n", msg);
92 ExitProgram(EXIT_FAILURE);
96 ch_len(NCURSES_CH_T * src)
103 #if USE_WIDEC_SUPPORT
105 TEST_CCHAR(src, count, {
106 int len = wcwidth(test_wch[0]);
107 result += (len > 0) ? len : 1;
126 if (vec_lines != 0) {
128 for (n = 0; n < num_lines; ++n) {
134 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
138 show_all(const char *tag)
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) - 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("%3d:", 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("", 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 if ((vec_lines[k] = typeCalloc(NCURSES_CH_T, (size_t) x + 1)) == 0)
351 failed("cannot allocate line-vector #3");
352 #if USE_WIDEC_SUPPORT
353 win_wchnstr(my_win, vec_lines[k], x);
355 winchnstr(my_win, vec_lines[k], x);
367 static const char *msg[] =
369 "Usage: view [options] file"
372 ," -c use color if terminal supports it"
373 ," -i ignore INT, QUIT, TERM signals"
374 #if USE_WIDEC_SUPPORT
375 ," -n use waddch (bytes) rather then wadd_wch (wide-chars)"
377 ," -s start in single-step mode, waiting for input"
379 ," -t trace screen updates"
380 ," -T NUM specify trace mask"
384 for (n = 0; n < SIZEOF(msg); n++)
385 fprintf(stderr, "%s\n", msg[n]);
386 ExitProgram(EXIT_FAILURE);
390 main(int argc, char *argv[])
392 static const char *help[] =
395 " q,^Q,ESC - quit this program",
397 " p,<Up> - scroll the viewport up by one row",
398 " n,<Down> - scroll the viewport down by one row",
399 " l,<Left> - scroll the viewport left by one column",
400 " r,<Right> - scroll the viewport right by one column",
402 " h,<Home> - scroll the viewport to top of file",
403 " ^F,<PageDn> - scroll to the next page",
404 " ^B,<PageUp> - scroll to the previous page",
405 " e,<End> - scroll the viewport to end of file",
407 " ^L - repaint using redrawwin()",
409 " 0 through 9 - enter digits for count",
410 " s - use entered count for halfdelay() parameter",
411 " - if no entered count, stop nodelay()",
412 " <space> - begin nodelay()",
418 NCURSES_CH_T **olptr;
421 bool got_number = FALSE;
422 bool ignore_sigs = FALSE;
423 bool single_step = FALSE;
424 const char *my_label = "Input";
426 setlocale(LC_ALL, "");
428 while ((i = getopt(argc, argv, "cirstT:")) != -1) {
443 int tvalue = (int) strtol(optarg, &next, 0);
444 if (tvalue < 0 || (next != 0 && *next != 0))
446 trace((unsigned) tvalue);
457 if (optind + 1 != argc)
460 InitAndCatch(initscr(), ignore_sigs ? SIG_IGN : finish);
461 keypad(stdscr, TRUE); /* enable keyboard mapping */
462 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
463 (void) cbreak(); /* take input chars one at a time, no wait for \n */
464 (void) noecho(); /* don't echo input */
466 nodelay(stdscr, TRUE);
467 idlok(stdscr, TRUE); /* allow use of insert/delete line */
469 read_file(fname = argv[optind]);
474 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
475 bkgd((chtype) COLOR_PAIR(my_pair));
490 if ((c < 127) && isdigit(c)) {
492 MvPrintw(0, 0, "Count: ");
496 value = 10 * value + (c - '0');
501 if (got_number && value) {
508 my_label = keyname(c);
513 for (i = 0; i < n; i++)
514 if ((lptr - vec_lines) < (num_lines - LINES + 1))
518 scrl((int) (lptr - olptr));
524 for (i = 0; i < n; i++)
525 if (lptr > vec_lines)
529 scrl((int) (lptr - olptr));
547 if (num_lines > LINES)
548 lptr = (vec_lines + num_lines - LINES + 1);
550 lptr = (vec_lines + (num_lines - 2));
556 for (i = 0; i < n; i++) {
557 if ((lptr - vec_lines) < (num_lines - 5))
560 lptr = (vec_lines + num_lines - 2);
567 for (i = 0; i < n; i++) {
568 if ((lptr - vec_lines) >= LINES)
596 case KEY_RESIZE: /* ignore this; ncurses will repaint */
602 halfdelay(my_delay = n);
604 nodelay(stdscr, FALSE);
608 nodelay(stdscr, FALSE);
613 nodelay(stdscr, TRUE);
624 popup_msg(stdscr, help);
630 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
636 finish(0); /* we're done */