]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/movewindow.c
ncurses 6.4 - patch 20240414
[ncurses.git] / test / movewindow.c
index d1e8dee8b7848f739c09ec733e4ae3f679b177ec..07797ca67b9ef7ce3937c770d96f42dd9f277f82 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2006-2013,2017 Free Software Foundation, Inc.              *
+ * Copyright 2018-2022,2023 Thomas E. Dickey                                *
+ * Copyright 2006-2013,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            *
@@ -26,7 +27,7 @@
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: movewindow.c,v 1.42 2017/04/15 18:37:42 tom Exp $
+ * $Id: movewindow.c,v 1.54 2023/05/27 20:13:10 tom Exp $
  *
  * Demonstrate move functions for windows and derived windows from the curses
  * library.
@@ -45,19 +46,15 @@ TODO:
  */
 
 #include <test.priv.h>
-#include <stdarg.h>
+
+#if HAVE_MVDERWIN && HAVE_MVWIN
+
 #include <popup_msg.h>
 
 #ifdef HAVE_XCURSES
 #undef derwin
 #endif
 
-#ifdef NCURSES_VERSION
-#define CONST_FMT const
-#else
-#define CONST_FMT              /* nothing */
-#endif
-
 #undef LINE_MAX
 
 #define LINE_MIN       2
@@ -74,8 +71,8 @@ typedef struct {
     WINDOW *child;             /* the actual value */
 } FRAME;
 
-static void head_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
-static void tail_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
+static void head_line(CONST_FMT char *fmt, ...) GCC_PRINTFLIKE(1, 2);
+static void tail_line(CONST_FMT char *fmt, ...) GCC_PRINTFLIKE(1, 2);
 
 static unsigned num_windows;
 static FRAME *all_windows;
@@ -103,6 +100,8 @@ message(int lineno, CONST_FMT char *fmt, va_list argp)
        vsprintf(buffer, fmt, argp);
        addstr(buffer);
     }
+#elif defined(HAVE_VW_PRINTW)
+    vw_printw(stdscr, fmt, argp);
 #else
     vwprintw(stdscr, fmt, argp);
 #endif
@@ -112,7 +111,7 @@ message(int lineno, CONST_FMT char *fmt, va_list argp)
 }
 
 static void
-head_line(CONST_FMT char *fmt,...)
+head_line(CONST_FMT char *fmt, ...)
 {
     va_list argp;
 
@@ -122,7 +121,7 @@ head_line(CONST_FMT char *fmt,...)
 }
 
 static void
-tail_line(CONST_FMT char *fmt,...)
+tail_line(CONST_FMT char *fmt, ...)
 {
     va_list argp;
 
@@ -191,6 +190,7 @@ selectcell(WINDOW *parent,
            moved = TRUE;
            break;
        case QUIT:
+           /* FALLTHRU */
        case ESCAPE:
            return ((PAIR *) 0);
 #ifdef NCURSES_MOUSE_VERSION
@@ -212,8 +212,8 @@ selectcell(WINDOW *parent,
                    break;
                }
            }
-           /* FALLTHRU */
 #endif
+           /* FALLTHRU */
        default:
            res.y = uli + i;
            res.x = ulj + j;
@@ -324,6 +324,7 @@ add_window(WINDOW *parent, WINDOW *child)
        all_windows = typeRealloc(FRAME, need, all_windows);
        if (!all_windows)
            failed("add_window");
+       have = need;
     }
     all_windows[num_windows].parent = parent;
     all_windows[num_windows].child = child;
@@ -331,7 +332,7 @@ add_window(WINDOW *parent, WINDOW *child)
 }
 
 static int
-window2num(WINDOW *win)
+window2num(const WINDOW *const win)
 {
     int n;
     int result = -1;
@@ -407,7 +408,7 @@ prev_window(WINDOW *win)
 }
 
 static void
-recur_move_window(WINDOW *parent, int dy, int dx)
+recur_move_window(const WINDOW *const parent, int dy, int dx)
 {
     unsigned n;
 
@@ -659,8 +660,10 @@ show_help(WINDOW *current)
     size_t n;
 
     for (n = 0; n < SIZEOF(help); ++n) {
-       msgs[n] = typeMalloc(char, 21 + strlen(help[n].msg));
-       sprintf(msgs[n], "%-20s%s", keyname(help[n].key), help[n].msg);
+       size_t need = (21 + strlen(help[n].msg));
+       msgs[n] = typeMalloc(char, need);
+       _nc_SPRINTF(msgs[n], _nc_SLIMIT(need)
+                   "%-20s%s", keyname(help[n].key), help[n].msg);
     }
     popup_msg2(current, msgs);
     for (n = 0; n < SIZEOF(help); ++n) {
@@ -669,13 +672,46 @@ show_help(WINDOW *current)
     free(msgs);
 }
 
+static void
+usage(int ok)
+{
+    static const char *msg[] =
+    {
+       "Usage: movewindow [options]"
+       ,""
+       ,USAGE_COMMON
+    };
+    size_t n;
+
+    for (n = 0; n < SIZEOF(msg); n++)
+       fprintf(stderr, "%s\n", msg[n]);
+
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
+
 int
-main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
+main(int argc, char *argv[])
 {
     WINDOW *current_win;
     int ch;
     bool done = FALSE;
 
+    while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
+       switch (ch) {
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
+       default:
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
+       }
+    }
+    if (optind < argc)
+       usage(FALSE);
+
     initscr();
     cbreak();
     noecho();
@@ -766,3 +802,11 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
 #endif
     ExitProgram(EXIT_SUCCESS);
 }
+#else
+int
+main(void)
+{
+    printf("This program requires the curses mvderwin and mvwin functions\n");
+    ExitProgram(EXIT_FAILURE);
+}
+#endif