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.22 2022/12/04 00:40:11 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;
60 failed(const char *msg)
63 fprintf(stderr, "%s\n", msg);
64 ExitProgram(EXIT_FAILURE);
71 if (global_pad != NULL)
73 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
77 show_all(const char *tag, WINDOW *my_pad, int my_row)
84 for (digits = 1, i = num_lines; i > 0; i /= 10) {
88 wattrset(stdscr, COLOR_PAIR(my_pair));
91 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
92 "view %.*s", (int) strlen(tag), tag);
93 i = (int) strlen(temp);
94 _nc_SPRINTF(temp + i, _nc_SLIMIT(sizeof(temp) - (size_t) i)
95 " %.*s", (int) sizeof(temp) - i - 2, fname);
96 mvprintw(0, 0, "%.*s", COLS, temp);
97 this_time = time((time_t *) 0);
98 _nc_STRNCPY(temp, ctime(&this_time), (size_t) 30);
99 if ((i = (int) strlen(temp)) != 0) {
101 mvprintw(0, COLS - i - 2, " %s", temp);
104 for (i = 1; i < LINES; i++) {
105 int actual = my_row + i;
106 if (actual > num_lines) {
109 mvprintw(i, 0, "%*d:", digits, actual);
111 wnoutrefresh(stdscr);
112 pnoutrefresh(my_pad, my_row, shift, 1, digits + 1, LINES - 1, COLS - 1);
117 read_file(const char *filename)
130 if (stat(filename, &sb) != 0
131 || (sb.st_mode & S_IFMT) != S_IFREG) {
132 failed("input is not a file");
135 if (sb.st_size == 0) {
136 failed("input is empty");
139 if ((fp = fopen(filename, "r")) == 0) {
140 failed("cannot open input-file");
143 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0) {
144 failed("cannot allocate memory for input-file");
147 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
150 if (len > (size_t) sb.st_size)
151 len = (size_t) sb.st_size;
154 for (pass = 0; pass < 2; ++pass) {
155 char *base = my_blob;
157 for (j = 0; j < len; ++j) {
158 if (my_blob[j] == '\n') {
163 base = my_blob + j + 1;
167 if (base != (my_blob + j)) {
174 if (((my_vec = typeCalloc(char *, (size_t) k + 2)) == 0)) {
175 failed("cannot allocate line-vector #1");
178 if (my_vec[0] == NULL)
183 #if USE_WIDEC_SUPPORT
184 if (!memcmp("\357\273\277", my_blob, 3)) {
185 char *s = my_blob + 3;
189 } while ((*d++ = *s++) != '\0');
194 width = (int) strlen(my_vec[0]);
195 for (k = 1; my_vec[k]; ++k) {
196 int check = (int) (my_vec[k] - my_vec[k - 1]);
200 width = (width + 1) * 5;
201 my_pad = newpad(height, width);
203 failed("cannot allocate pad workspace");
205 wattrset(my_pad, COLOR_PAIR(my_pair));
206 wbkgd(my_pad, (chtype) (' ' | COLOR_PAIR(my_pair)));
210 * Use the curses library for rendering, including tab-conversion.
212 Trace(("slurp the file"));
213 for (k = 0; my_vec[k]; ++k) {
215 #if USE_WIDEC_SUPPORT
216 char *last = my_vec[k] + (int) strlen(my_vec[k]);
222 #endif /* USE_WIDEC_SUPPORT */
225 #if USE_WIDEC_SUPPORT
229 for (s = my_vec[k]; *s != '\0'; ++s) {
230 #if USE_WIDEC_SUPPORT
232 rc = (size_t) check_mbytes(wch[0], s, (size_t) (last - s), state);
233 if ((long) rc == -1 || (long) rc == -2) {
237 waddwstr(my_pad, wch);
240 waddch(my_pad, *s & 0xff);
253 static const char *msg[] =
255 "Usage: view [options] file"
259 ," -c use color if terminal supports it"
260 ," -i ignore INT, QUIT, TERM signals"
261 #if USE_WIDEC_SUPPORT
262 ," -n use waddch (bytes) rather then wadd_wch (wide-chars)"
264 ," -s start in single-step mode, waiting for input"
266 ," -t trace screen updates"
267 ," -T NUM specify trace mask"
271 for (n = 0; n < SIZEOF(msg); n++)
272 fprintf(stderr, "%s\n", msg[n]);
273 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
280 main(int argc, char *argv[])
282 static const char *help[] =
285 " q,^Q,ESC - quit this program",
287 " p,<Up> - scroll the viewport up by one row",
288 " n,<Down> - scroll the viewport down by one row",
289 " l,<Left> - scroll the viewport left by one column",
290 " r,<Right> - scroll the viewport right by one column",
291 " <,> - scroll the viewport left/right by 8 columns",
293 " h,<Home> - scroll the viewport to top of file",
294 " ^F,<PageDn> - scroll to the next page",
295 " ^B,<PageUp> - scroll to the previous page",
296 " e,<End> - scroll the viewport to end of file",
298 " ^L - repaint using redrawwin()",
300 " 0 through 9 - enter digits for count",
301 " s - use entered count for halfdelay() parameter",
302 " - if no entered count, stop nodelay()",
303 " <space> - begin nodelay()",
314 bool got_number = FALSE;
315 bool ignore_sigs = FALSE;
316 bool single_step = FALSE;
317 const char *my_label = "Input";
319 setlocale(LC_ALL, "");
321 while ((ch = getopt(argc, argv, OPTS_COMMON "cinstT:")) != -1) {
329 #if USE_WIDEC_SUPPORT
341 int tvalue = (int) strtol(optarg, &next, 0);
342 if (tvalue < 0 || (next != 0 && *next != 0))
344 curses_trace((unsigned) tvalue);
348 curses_trace(TRACE_CALLS);
353 ExitProgram(EXIT_SUCCESS);
355 usage(ch == OPTS_USAGE);
359 if (optind + 1 != argc)
362 InitAndCatch(initscr(), ignore_sigs ? SIG_IGN : finish);
363 keypad(stdscr, TRUE); /* enable keyboard mapping */
364 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
365 (void) cbreak(); /* take input chars one at a time, no wait for \n */
366 (void) noecho(); /* don't echo input */
368 nodelay(stdscr, TRUE);
369 idlok(stdscr, TRUE); /* allow use of insert/delete line */
374 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
375 bkgd((chtype) (' ' | COLOR_PAIR(my_pair)));
382 * Do this after starting color, otherwise the pad's background will be
383 * uncolored after the ncurses 6.1.20181208 fixes.
386 my_pad = read_file(fname = argv[optind]);
393 show_all(my_label, my_pad, my_row);
397 if ((c < 127) && isdigit(c)) {
399 MvPrintw(0, 0, "Count: ");
403 value = 10 * value + (c - '0');
408 if (got_number && value) {
415 my_label = keyname(c);
419 for (i = 0; i < n; i++)
420 if (my_row < (num_lines - LINES + 1))
428 for (i = 0; i < n; i++)
442 if ((shift -= 8) < 0)
452 if (num_lines > LINES)
453 my_row = (num_lines - LINES + 1);
455 my_row = (num_lines - 2);
461 for (i = 0; i < n; i++) {
462 if (my_row < (num_lines - 5))
463 my_row += (LINES - 1);
465 my_row = (num_lines - 2);
472 for (i = 0; i < n; i++) {
474 my_row -= (LINES - 1);
501 case KEY_RESIZE: /* ignore this; ncurses will repaint */
507 halfdelay(my_delay = n);
509 nodelay(stdscr, FALSE);
513 nodelay(stdscr, FALSE);
518 nodelay(stdscr, TRUE);
529 popup_msg(stdscr, help);
535 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
541 finish(0); /* we're done */
547 printf("This program requires the curses pad functions\n");
548 ExitProgram(EXIT_FAILURE);