1 /****************************************************************************
2 * Copyright (c) 2017,2019 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 * clone of view.c, using pads
31 * $Id: padview.c,v 1.13 2019/01/21 19:47:07 tom Exp $
34 #include <test.priv.h>
35 #include <widechars.h>
36 #include <popup_msg.h>
43 static void finish(int sig) GCC_NORETURN;
48 static bool try_color = FALSE;
54 static bool n_option = FALSE;
57 static void usage(void) GCC_NORETURN;
60 failed(const char *msg)
63 fprintf(stderr, "%s\n", msg);
64 ExitProgram(EXIT_FAILURE);
71 ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
75 show_all(const char *tag, WINDOW *my_pad, int my_row)
82 for (digits = 1, i = num_lines; i > 0; i /= 10) {
86 wattrset(stdscr, COLOR_PAIR(my_pair));
89 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
90 "view %.*s", (int) strlen(tag), tag);
91 i = (int) strlen(temp);
92 _nc_SPRINTF(temp + i, _nc_SLIMIT(sizeof(temp) - (size_t) i)
93 " %.*s", (int) sizeof(temp) - i - 2, fname);
94 mvprintw(0, 0, "%.*s", COLS, temp);
95 this_time = time((time_t *) 0);
96 _nc_STRNCPY(temp, ctime(&this_time), (size_t) 30);
97 if ((i = (int) strlen(temp)) != 0) {
99 mvprintw(0, COLS - i - 2, " %s", temp);
102 for (i = 1; i < LINES; i++) {
103 int actual = my_row + i;
104 if (actual > num_lines) {
107 mvprintw(i, 0, "%*d:", digits, actual);
109 wnoutrefresh(stdscr);
110 pnoutrefresh(my_pad, my_row, shift, 1, digits + 1, LINES - 1, COLS - 1);
115 read_file(const char *filename)
128 if (stat(filename, &sb) != 0
129 || (sb.st_mode & S_IFMT) != S_IFREG) {
130 failed("input is not a file");
133 if (sb.st_size == 0) {
134 failed("input is empty");
137 if ((fp = fopen(filename, "r")) == 0) {
138 failed("cannot open input-file");
141 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0) {
142 failed("cannot allocate memory for input-file");
145 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
146 my_blob[sb.st_size] = '\0';
149 for (pass = 0; pass < 2; ++pass) {
150 char *base = my_blob;
152 for (j = 0; j < len; ++j) {
153 if (my_blob[j] == '\n') {
158 base = my_blob + j + 1;
163 if (base != (my_blob + j))
166 ((my_vec = typeCalloc(char *, (size_t) k + 2)) == 0)) {
167 failed("cannot allocate line-vector #1");
171 #if USE_WIDEC_SUPPORT
172 if (!memcmp("\357\273\277", my_blob, 3)) {
173 char *s = my_blob + 3;
177 } while ((*d++ = *s++) != '\0');
182 width = (int) strlen(my_vec[0]);
183 for (k = 1; my_vec[k]; ++k) {
184 int check = (int) (my_vec[k] - my_vec[k - 1]);
188 width = (width + 1) * 5;
189 my_pad = newpad(height, width);
191 failed("cannot allocate pad workspace");
193 wattrset(my_pad, COLOR_PAIR(my_pair));
194 wbkgd(my_pad, (chtype) COLOR_PAIR(my_pair));
198 * Use the curses library for rendering, including tab-conversion.
200 Trace(("slurp the file"));
201 for (k = 0; my_vec[k]; ++k) {
203 #if USE_WIDEC_SUPPORT
204 char *last = my_vec[k] + (int) strlen(my_vec[k]);
210 #endif /* USE_WIDEC_SUPPORT */
213 #if USE_WIDEC_SUPPORT
217 for (s = my_vec[k]; *s != '\0'; ++s) {
218 #if USE_WIDEC_SUPPORT
220 rc = (size_t) check_mbytes(wch[0], s, (size_t) (last - s), state);
221 if ((long) rc == -1 || (long) rc == -2) {
225 waddwstr(my_pad, wch);
228 waddch(my_pad, *s & 0xff);
241 static const char *msg[] =
243 "Usage: view [options] file"
246 ," -c use color if terminal supports it"
247 ," -i ignore INT, QUIT, TERM signals"
248 #if USE_WIDEC_SUPPORT
249 ," -n use waddch (bytes) rather then wadd_wch (wide-chars)"
251 ," -s start in single-step mode, waiting for input"
253 ," -t trace screen updates"
254 ," -T NUM specify trace mask"
258 for (n = 0; n < SIZEOF(msg); n++)
259 fprintf(stderr, "%s\n", msg[n]);
260 ExitProgram(EXIT_FAILURE);
264 main(int argc, char *argv[])
266 static const char *help[] =
269 " q,^Q,ESC - quit this program",
271 " p,<Up> - scroll the viewport up by one row",
272 " n,<Down> - scroll the viewport down by one row",
273 " l,<Left> - scroll the viewport left by one column",
274 " r,<Right> - scroll the viewport right by one column",
275 " <,> - scroll the viewport left/right by 8 columns",
277 " h,<Home> - scroll the viewport to top of file",
278 " ^F,<PageDn> - scroll to the next page",
279 " ^B,<PageUp> - scroll to the previous page",
280 " e,<End> - scroll the viewport to end of file",
282 " ^L - repaint using redrawwin()",
284 " 0 through 9 - enter digits for count",
285 " s - use entered count for halfdelay() parameter",
286 " - if no entered count, stop nodelay()",
287 " <space> - begin nodelay()",
297 bool got_number = FALSE;
298 bool ignore_sigs = FALSE;
299 bool single_step = FALSE;
300 const char *my_label = "Input";
302 setlocale(LC_ALL, "");
304 while ((i = getopt(argc, argv, "cinstT:")) != -1) {
312 #if USE_WIDEC_SUPPORT
324 int tvalue = (int) strtol(optarg, &next, 0);
325 if (tvalue < 0 || (next != 0 && *next != 0))
327 trace((unsigned) tvalue);
338 if (optind + 1 != argc)
341 InitAndCatch(initscr(), ignore_sigs ? SIG_IGN : finish);
342 keypad(stdscr, TRUE); /* enable keyboard mapping */
343 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
344 (void) cbreak(); /* take input chars one at a time, no wait for \n */
345 (void) noecho(); /* don't echo input */
347 nodelay(stdscr, TRUE);
348 idlok(stdscr, TRUE); /* allow use of insert/delete line */
350 my_pad = read_file(fname = argv[optind]);
355 init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
356 bkgd((chtype) COLOR_PAIR(my_pair));
367 show_all(my_label, my_pad, my_row);
371 if ((c < 127) && isdigit(c)) {
373 MvPrintw(0, 0, "Count: ");
377 value = 10 * value + (c - '0');
382 if (got_number && value) {
389 my_label = keyname(c);
393 for (i = 0; i < n; i++)
394 if (my_row < (num_lines - LINES + 1))
402 for (i = 0; i < n; i++)
416 if ((shift -= 8) < 0)
426 if (num_lines > LINES)
427 my_row = (num_lines - LINES + 1);
429 my_row = (num_lines - 2);
435 for (i = 0; i < n; i++) {
436 if (my_row < (num_lines - 5))
437 my_row += (LINES - 1);
439 my_row = (num_lines - 2);
446 for (i = 0; i < n; i++) {
448 my_row -= (LINES - 1);
475 case KEY_RESIZE: /* ignore this; ncurses will repaint */
481 halfdelay(my_delay = n);
483 nodelay(stdscr, FALSE);
487 nodelay(stdscr, FALSE);
492 nodelay(stdscr, TRUE);
503 popup_msg(stdscr, help);
509 if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
515 finish(0); /* we're done */
521 printf("This program requires the curses pad functions\n");
522 ExitProgram(EXIT_FAILURE);