1 /****************************************************************************
2 * Copyright 2019-2021,2022 Thomas E. Dickey *
3 * Copyright 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 * clone of view.c, using pads
32 * $Id: padview.c,v 1.19 2022/05/15 14:36:23 tom Exp $
35 #include <test.priv.h>
36 #include <widechars.h>
37 #include <popup_msg.h>
44 static GCC_NORETURN void finish(int sig);
48 static WINDOW *global_pad;
50 static bool try_color = FALSE;
56 static bool n_option = FALSE;
59 static GCC_NORETURN void usage(void);
62 failed(const char *msg)
65 fprintf(stderr, "%s\n", msg);
66 ExitProgram(EXIT_FAILURE);
73 if (global_pad != NULL)
75 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
79 show_all(const char *tag, WINDOW *my_pad, int my_row)
86 for (digits = 1, i = num_lines; i > 0; i /= 10) {
90 wattrset(stdscr, COLOR_PAIR(my_pair));
93 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
94 "view %.*s", (int) strlen(tag), tag);
95 i = (int) strlen(temp);
96 _nc_SPRINTF(temp + i, _nc_SLIMIT(sizeof(temp) - (size_t) i)
97 " %.*s", (int) sizeof(temp) - i - 2, fname);
98 mvprintw(0, 0, "%.*s", COLS, temp);
99 this_time = time((time_t *) 0);
100 _nc_STRNCPY(temp, ctime(&this_time), (size_t) 30);
101 if ((i = (int) strlen(temp)) != 0) {
103 mvprintw(0, COLS - i - 2, " %s", temp);
106 for (i = 1; i < LINES; i++) {
107 int actual = my_row + i;
108 if (actual > num_lines) {
111 mvprintw(i, 0, "%*d:", digits, actual);
113 wnoutrefresh(stdscr);
114 pnoutrefresh(my_pad, my_row, shift, 1, digits + 1, LINES - 1, COLS - 1);
119 read_file(const char *filename)
132 if (stat(filename, &sb) != 0
133 || (sb.st_mode & S_IFMT) != S_IFREG) {
134 failed("input is not a file");
137 if (sb.st_size == 0) {
138 failed("input is empty");
141 if ((fp = fopen(filename, "r")) == 0) {
142 failed("cannot open input-file");
145 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0) {
146 failed("cannot allocate memory for input-file");
149 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
152 if (len > (size_t) sb.st_size)
153 len = (size_t) sb.st_size;
156 for (pass = 0; pass < 2; ++pass) {
157 char *base = my_blob;
159 for (j = 0; j < len; ++j) {
160 if (my_blob[j] == '\n') {
165 base = my_blob + j + 1;
169 if (base != (my_blob + j)) {
176 if (((my_vec = typeCalloc(char *, (size_t) k + 2)) == 0)) {
177 failed("cannot allocate line-vector #1");
180 if (my_vec[0] == NULL)
185 #if USE_WIDEC_SUPPORT
186 if (!memcmp("\357\273\277", my_blob, 3)) {
187 char *s = my_blob + 3;
191 } while ((*d++ = *s++) != '\0');
196 width = (int) strlen(my_vec[0]);
197 for (k = 1; my_vec[k]; ++k) {
198 int check = (int) (my_vec[k] - my_vec[k - 1]);
202 width = (width + 1) * 5;
203 my_pad = newpad(height, width);
205 failed("cannot allocate pad workspace");
207 wattrset(my_pad, COLOR_PAIR(my_pair));
208 wbkgd(my_pad, (chtype) (' ' | COLOR_PAIR(my_pair)));
212 * Use the curses library for rendering, including tab-conversion.
214 Trace(("slurp the file"));
215 for (k = 0; my_vec[k]; ++k) {
217 #if USE_WIDEC_SUPPORT
218 char *last = my_vec[k] + (int) strlen(my_vec[k]);
224 #endif /* USE_WIDEC_SUPPORT */
227 #if USE_WIDEC_SUPPORT
231 for (s = my_vec[k]; *s != '\0'; ++s) {
232 #if USE_WIDEC_SUPPORT
234 rc = (size_t) check_mbytes(wch[0], s, (size_t) (last - s), state);
235 if ((long) rc == -1 || (long) rc == -2) {
239 waddwstr(my_pad, wch);
242 waddch(my_pad, *s & 0xff);
255 static const char *msg[] =
257 "Usage: view [options] file"
260 ," -c use color if terminal supports it"
261 ," -i ignore INT, QUIT, TERM signals"
262 #if USE_WIDEC_SUPPORT
263 ," -n use waddch (bytes) rather then wadd_wch (wide-chars)"
265 ," -s start in single-step mode, waiting for input"
267 ," -t trace screen updates"
268 ," -T NUM specify trace mask"
272 for (n = 0; n < SIZEOF(msg); n++)
273 fprintf(stderr, "%s\n", msg[n]);
274 ExitProgram(EXIT_FAILURE);
278 main(int argc, char *argv[])
280 static const char *help[] =
283 " q,^Q,ESC - quit this program",
285 " p,<Up> - scroll the viewport up by one row",
286 " n,<Down> - scroll the viewport down by one row",
287 " l,<Left> - scroll the viewport left by one column",
288 " r,<Right> - scroll the viewport right by one column",
289 " <,> - scroll the viewport left/right by 8 columns",
291 " h,<Home> - scroll the viewport to top of file",
292 " ^F,<PageDn> - scroll to the next page",
293 " ^B,<PageUp> - scroll to the previous page",
294 " e,<End> - scroll the viewport to end of file",
296 " ^L - repaint using redrawwin()",
298 " 0 through 9 - enter digits for count",
299 " s - use entered count for halfdelay() parameter",
300 " - if no entered count, stop nodelay()",
301 " <space> - begin nodelay()",
311 bool got_number = FALSE;
312 bool ignore_sigs = FALSE;
313 bool single_step = FALSE;
314 const char *my_label = "Input";
316 setlocale(LC_ALL, "");
318 while ((i = getopt(argc, argv, "cinstT:")) != -1) {
326 #if USE_WIDEC_SUPPORT
338 int tvalue = (int) strtol(optarg, &next, 0);
339 if (tvalue < 0 || (next != 0 && *next != 0))
341 curses_trace((unsigned) tvalue);
345 curses_trace(TRACE_CALLS);
352 if (optind + 1 != argc)
355 InitAndCatch(initscr(), ignore_sigs ? SIG_IGN : finish);
356 keypad(stdscr, TRUE); /* enable keyboard mapping */
357 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
358 (void) cbreak(); /* take input chars one at a time, no wait for \n */
359 (void) noecho(); /* don't echo input */
361 nodelay(stdscr, TRUE);
362 idlok(stdscr, TRUE); /* allow use of insert/delete line */
367 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
368 bkgd((chtype) (' ' | COLOR_PAIR(my_pair)));
375 * Do this after starting color, otherwise the pad's background will be
376 * uncolored after the ncurses 6.1.20181208 fixes.
379 my_pad = read_file(fname = argv[optind]);
386 show_all(my_label, my_pad, my_row);
390 if ((c < 127) && isdigit(c)) {
392 MvPrintw(0, 0, "Count: ");
396 value = 10 * value + (c - '0');
401 if (got_number && value) {
408 my_label = keyname(c);
412 for (i = 0; i < n; i++)
413 if (my_row < (num_lines - LINES + 1))
421 for (i = 0; i < n; i++)
435 if ((shift -= 8) < 0)
445 if (num_lines > LINES)
446 my_row = (num_lines - LINES + 1);
448 my_row = (num_lines - 2);
454 for (i = 0; i < n; i++) {
455 if (my_row < (num_lines - 5))
456 my_row += (LINES - 1);
458 my_row = (num_lines - 2);
465 for (i = 0; i < n; i++) {
467 my_row -= (LINES - 1);
494 case KEY_RESIZE: /* ignore this; ncurses will repaint */
500 halfdelay(my_delay = n);
502 nodelay(stdscr, FALSE);
506 nodelay(stdscr, FALSE);
511 nodelay(stdscr, TRUE);
522 popup_msg(stdscr, help);
528 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
534 finish(0); /* we're done */
540 printf("This program requires the curses pad functions\n");
541 ExitProgram(EXIT_FAILURE);