]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/padview.c
ncurses 6.4 - patch 20240414
[ncurses.git] / test / padview.c
index 0445fc74840520b87e977daba758ffaa86b59aca..0defee3430e0f2e07047ddebb042f6db2f4363ed 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2017,2019 Free Software Foundation, Inc.                   *
+ * Copyright 2019-2021,2022 Thomas E. Dickey                                *
+ * Copyright 2017 Free Software Foundation, Inc.                            *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -28,7 +29,7 @@
 /*
  * clone of view.c, using pads
  *
- * $Id: padview.c,v 1.13 2019/01/21 19:47:07 tom Exp $
+ * $Id: padview.c,v 1.22 2022/12/04 00:40:11 tom Exp $
  */
 
 #include <test.priv.h>
 
 #if HAVE_NEWPAD
 
-static void finish(int sig) GCC_NORETURN;
+static GCC_NORETURN void finish(int sig);
 
 #define my_pair 1
 
+static WINDOW *global_pad;
 static int shift = 0;
 static bool try_color = FALSE;
 
@@ -54,8 +56,6 @@ static int num_lines;
 static bool n_option = FALSE;
 #endif
 
-static void usage(void) GCC_NORETURN;
-
 static void
 failed(const char *msg)
 {
@@ -68,6 +68,8 @@ static void
 finish(int sig)
 {
     endwin();
+    if (global_pad != NULL)
+       delwin(global_pad);
     ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
@@ -143,9 +145,12 @@ read_file(const char *filename)
     }
 
     len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
-    my_blob[sb.st_size] = '\0';
     fclose(fp);
 
+    if (len > (size_t) sb.st_size)
+       len = (size_t) sb.st_size;
+    my_blob[len] = '\0';
+
     for (pass = 0; pass < 2; ++pass) {
        char *base = my_blob;
        k = 0;
@@ -159,12 +164,19 @@ read_file(const char *filename)
                ++k;
            }
        }
+       if (base != (my_blob + j)) {
+           if (pass)
+               my_vec[k] = base;
+           ++k;
+       }
        num_lines = k;
-       if (base != (my_blob + j))
-           ++num_lines;
-       if (!pass &&
-           ((my_vec = typeCalloc(char *, (size_t) k + 2)) == 0)) {
-           failed("cannot allocate line-vector #1");
+       if (pass == 0) {
+           if (((my_vec = typeCalloc(char *, (size_t) k + 2)) == 0)) {
+               failed("cannot allocate line-vector #1");
+           }
+       } else {
+           if (my_vec[0] == NULL)
+               my_vec[0] = my_blob;
        }
     }
 
@@ -191,7 +203,7 @@ read_file(const char *filename)
        failed("cannot allocate pad workspace");
     if (try_color) {
        wattrset(my_pad, COLOR_PAIR(my_pair));
-       wbkgd(my_pad, (chtype) COLOR_PAIR(my_pair));
+       wbkgd(my_pad, (chtype) (' ' | COLOR_PAIR(my_pair)));
     }
 
     /*
@@ -236,12 +248,13 @@ read_file(const char *filename)
 }
 
 static void
-usage(void)
+usage(int ok)
 {
     static const char *msg[] =
     {
        "Usage: view [options] file"
        ,""
+       ,USAGE_COMMON
        ,"Options:"
        ," -c       use color if terminal supports it"
        ," -i       ignore INT, QUIT, TERM signals"
@@ -257,8 +270,11 @@ usage(void)
     size_t n;
     for (n = 0; n < SIZEOF(msg); n++)
        fprintf(stderr, "%s\n", msg[n]);
-    ExitProgram(EXIT_FAILURE);
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
 
 int
 main(int argc, char *argv[])
@@ -288,6 +304,7 @@ main(int argc, char *argv[])
        0
     };
 
+    int ch;
     int i;
     int my_delay = 0;
     WINDOW *my_pad;
@@ -301,8 +318,8 @@ main(int argc, char *argv[])
 
     setlocale(LC_ALL, "");
 
-    while ((i = getopt(argc, argv, "cinstT:")) != -1) {
-       switch (i) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "cinstT:")) != -1) {
+       switch (ch) {
        case 'c':
            try_color = TRUE;
            break;
@@ -323,20 +340,24 @@ main(int argc, char *argv[])
                char *next = 0;
                int tvalue = (int) strtol(optarg, &next, 0);
                if (tvalue < 0 || (next != 0 && *next != 0))
-                   usage();
-               trace((unsigned) tvalue);
+                   usage(FALSE);
+               curses_trace((unsigned) tvalue);
            }
            break;
        case 't':
-           trace(TRACE_CALLS);
+           curses_trace(TRACE_CALLS);
            break;
 #endif
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
        }
     }
     if (optind + 1 != argc)
-       usage();
+       usage(FALSE);
 
     InitAndCatch(initscr(), ignore_sigs ? SIG_IGN : finish);
     keypad(stdscr, TRUE);      /* enable keyboard mapping */
@@ -347,18 +368,23 @@ main(int argc, char *argv[])
        nodelay(stdscr, TRUE);
     idlok(stdscr, TRUE);       /* allow use of insert/delete line */
 
-    my_pad = read_file(fname = argv[optind]);
-
     if (try_color) {
        if (has_colors()) {
            start_color();
            init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
-           bkgd((chtype) COLOR_PAIR(my_pair));
+           bkgd((chtype) (' ' | COLOR_PAIR(my_pair)));
        } else {
            try_color = FALSE;
        }
     }
 
+    /*
+     * Do this after starting color, otherwise the pad's background will be
+     * uncolored after the ncurses 6.1.20181208 fixes.
+     */
+    global_pad =
+       my_pad = read_file(fname = argv[optind]);
+
     my_row = 0;
     while (!done) {
        int n, c;