1 /****************************************************************************
2 * Copyright 2019-2020,2021 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.18 2021/06/12 23:16:31 tom Exp $
35 #include <test.priv.h>
36 #include <widechars.h>
37 #include <popup_msg.h>
44 static GCC_NORETURN void finish(int sig);
49 static bool try_color = FALSE;
55 static bool n_option = FALSE;
58 static GCC_NORETURN void usage(void);
61 failed(const char *msg)
64 fprintf(stderr, "%s\n", msg);
65 ExitProgram(EXIT_FAILURE);
72 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
76 show_all(const char *tag, WINDOW *my_pad, int my_row)
83 for (digits = 1, i = num_lines; i > 0; i /= 10) {
87 wattrset(stdscr, COLOR_PAIR(my_pair));
90 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
91 "view %.*s", (int) strlen(tag), tag);
92 i = (int) strlen(temp);
93 _nc_SPRINTF(temp + i, _nc_SLIMIT(sizeof(temp) - (size_t) i)
94 " %.*s", (int) sizeof(temp) - i - 2, fname);
95 mvprintw(0, 0, "%.*s", COLS, temp);
96 this_time = time((time_t *) 0);
97 _nc_STRNCPY(temp, ctime(&this_time), (size_t) 30);
98 if ((i = (int) strlen(temp)) != 0) {
100 mvprintw(0, COLS - i - 2, " %s", temp);
103 for (i = 1; i < LINES; i++) {
104 int actual = my_row + i;
105 if (actual > num_lines) {
108 mvprintw(i, 0, "%*d:", digits, actual);
110 wnoutrefresh(stdscr);
111 pnoutrefresh(my_pad, my_row, shift, 1, digits + 1, LINES - 1, COLS - 1);
116 read_file(const char *filename)
129 if (stat(filename, &sb) != 0
130 || (sb.st_mode & S_IFMT) != S_IFREG) {
131 failed("input is not a file");
134 if (sb.st_size == 0) {
135 failed("input is empty");
138 if ((fp = fopen(filename, "r")) == 0) {
139 failed("cannot open input-file");
142 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0) {
143 failed("cannot allocate memory for input-file");
146 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
149 if (len > (size_t) sb.st_size)
150 len = (size_t) sb.st_size;
153 for (pass = 0; pass < 2; ++pass) {
154 char *base = my_blob;
156 for (j = 0; j < len; ++j) {
157 if (my_blob[j] == '\n') {
162 base = my_blob + j + 1;
166 if (base != (my_blob + j)) {
173 if (((my_vec = typeCalloc(char *, (size_t) k + 2)) == 0)) {
174 failed("cannot allocate line-vector #1");
177 if (my_vec[0] == NULL)
182 #if USE_WIDEC_SUPPORT
183 if (!memcmp("\357\273\277", my_blob, 3)) {
184 char *s = my_blob + 3;
188 } while ((*d++ = *s++) != '\0');
193 width = (int) strlen(my_vec[0]);
194 for (k = 1; my_vec[k]; ++k) {
195 int check = (int) (my_vec[k] - my_vec[k - 1]);
199 width = (width + 1) * 5;
200 my_pad = newpad(height, width);
202 failed("cannot allocate pad workspace");
204 wattrset(my_pad, COLOR_PAIR(my_pair));
205 wbkgd(my_pad, (chtype) (' ' | COLOR_PAIR(my_pair)));
209 * Use the curses library for rendering, including tab-conversion.
211 Trace(("slurp the file"));
212 for (k = 0; my_vec[k]; ++k) {
214 #if USE_WIDEC_SUPPORT
215 char *last = my_vec[k] + (int) strlen(my_vec[k]);
221 #endif /* USE_WIDEC_SUPPORT */
224 #if USE_WIDEC_SUPPORT
228 for (s = my_vec[k]; *s != '\0'; ++s) {
229 #if USE_WIDEC_SUPPORT
231 rc = (size_t) check_mbytes(wch[0], s, (size_t) (last - s), state);
232 if ((long) rc == -1 || (long) rc == -2) {
236 waddwstr(my_pad, wch);
239 waddch(my_pad, *s & 0xff);
252 static const char *msg[] =
254 "Usage: view [options] file"
257 ," -c use color if terminal supports it"
258 ," -i ignore INT, QUIT, TERM signals"
259 #if USE_WIDEC_SUPPORT
260 ," -n use waddch (bytes) rather then wadd_wch (wide-chars)"
262 ," -s start in single-step mode, waiting for input"
264 ," -t trace screen updates"
265 ," -T NUM specify trace mask"
269 for (n = 0; n < SIZEOF(msg); n++)
270 fprintf(stderr, "%s\n", msg[n]);
271 ExitProgram(EXIT_FAILURE);
275 main(int argc, char *argv[])
277 static const char *help[] =
280 " q,^Q,ESC - quit this program",
282 " p,<Up> - scroll the viewport up by one row",
283 " n,<Down> - scroll the viewport down by one row",
284 " l,<Left> - scroll the viewport left by one column",
285 " r,<Right> - scroll the viewport right by one column",
286 " <,> - scroll the viewport left/right by 8 columns",
288 " h,<Home> - scroll the viewport to top of file",
289 " ^F,<PageDn> - scroll to the next page",
290 " ^B,<PageUp> - scroll to the previous page",
291 " e,<End> - scroll the viewport to end of file",
293 " ^L - repaint using redrawwin()",
295 " 0 through 9 - enter digits for count",
296 " s - use entered count for halfdelay() parameter",
297 " - if no entered count, stop nodelay()",
298 " <space> - begin nodelay()",
308 bool got_number = FALSE;
309 bool ignore_sigs = FALSE;
310 bool single_step = FALSE;
311 const char *my_label = "Input";
313 setlocale(LC_ALL, "");
315 while ((i = getopt(argc, argv, "cinstT:")) != -1) {
323 #if USE_WIDEC_SUPPORT
335 int tvalue = (int) strtol(optarg, &next, 0);
336 if (tvalue < 0 || (next != 0 && *next != 0))
338 curses_trace((unsigned) tvalue);
342 curses_trace(TRACE_CALLS);
349 if (optind + 1 != argc)
352 InitAndCatch(initscr(), ignore_sigs ? SIG_IGN : finish);
353 keypad(stdscr, TRUE); /* enable keyboard mapping */
354 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
355 (void) cbreak(); /* take input chars one at a time, no wait for \n */
356 (void) noecho(); /* don't echo input */
358 nodelay(stdscr, TRUE);
359 idlok(stdscr, TRUE); /* allow use of insert/delete line */
364 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
365 bkgd((chtype) (' ' | COLOR_PAIR(my_pair)));
372 * Do this after starting color, otherwise the pad's background will be
373 * uncolored after the ncurses 6.1.20181208 fixes.
375 my_pad = read_file(fname = argv[optind]);
382 show_all(my_label, my_pad, my_row);
386 if ((c < 127) && isdigit(c)) {
388 MvPrintw(0, 0, "Count: ");
392 value = 10 * value + (c - '0');
397 if (got_number && value) {
404 my_label = keyname(c);
408 for (i = 0; i < n; i++)
409 if (my_row < (num_lines - LINES + 1))
417 for (i = 0; i < n; i++)
431 if ((shift -= 8) < 0)
441 if (num_lines > LINES)
442 my_row = (num_lines - LINES + 1);
444 my_row = (num_lines - 2);
450 for (i = 0; i < n; i++) {
451 if (my_row < (num_lines - 5))
452 my_row += (LINES - 1);
454 my_row = (num_lines - 2);
461 for (i = 0; i < n; i++) {
463 my_row -= (LINES - 1);
490 case KEY_RESIZE: /* ignore this; ncurses will repaint */
496 halfdelay(my_delay = n);
498 nodelay(stdscr, FALSE);
502 nodelay(stdscr, FALSE);
507 nodelay(stdscr, TRUE);
518 popup_msg(stdscr, help);
524 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
530 finish(0); /* we're done */
536 printf("This program requires the curses pad functions\n");
537 ExitProgram(EXIT_FAILURE);