]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/move_field.c
ncurses 6.5 - patch 20240504
[ncurses.git] / test / move_field.c
index 5e7fc82e3904ace6694ce2f7a2c67065420a9369..84bac3f4b5659121b4dfa301cb50eb0c250ea5af 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright 2020,2021 Thomas E. Dickey                                     *
+ * Copyright 2020-2022,2023 Thomas E. Dickey                                *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -26,7 +26,7 @@
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: move_field.c,v 1.8 2021/03/27 23:41:57 tom Exp $
+ * $Id: move_field.c,v 1.15 2023/11/11 00:28:19 tom Exp $
  *
  * Demonstrate move_field().
  */
 #include <edit_field.h>
 #include <popup_msg.h>
 
-#ifdef HAVE_NETBSD_FORM_H
-#define form_field_row(field) (field)->form_row
-#define form_field_col(field) (field)->form_col
-#else /* e.g., SVr4, ncurses */
-#define form_field_row(field) (field)->frow
-#define form_field_col(field) (field)->fcol
-#endif
-
 #define DO_DEMO        CTRL('F')       /* actual key for toggling demo-mode */
 #define MY_DEMO        EDIT_FIELD('f') /* internal request-code */
 
@@ -124,7 +116,7 @@ make_label(const char *label, int frow, int fcol)
 
     if (f) {
        set_field_buffer(f, 0, label);
-       set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
+       set_field_opts(f, (int) ((unsigned) field_opts(f) & (unsigned) ~O_ACTIVE));
     }
     return (f);
 }
@@ -151,7 +143,6 @@ erase_form(FORM *f)
     werase(w);
     wrefresh(w);
     delwin(s);
-    delwin(w);
 }
 
 static FieldAttrs *
@@ -277,7 +268,7 @@ my_edit_field(FORM *form, int *result)
 static FIELD **
 copy_fields(FIELD **source, size_t length)
 {
-    FIELD **target = calloc(length + 1, sizeof(FIELD *));
+    FIELD **target = typeCalloc(FIELD *, length + 1);
     memcpy(target, source, length * sizeof(FIELD *));
     return target;
 }
@@ -309,13 +300,13 @@ do_demo(FORM *form)
 {
     int count = field_count(form);
     FIELD *my_field = current_field(form);
+    FIELD **old_fields = form_fields(form);
 
-    if (count > 0 && my_field != NULL) {
+    if (count > 0 && old_fields != NULL && my_field != NULL) {
        size_t needed = (size_t) count;
-       FIELD **old_fields = copy_fields(form_fields(form), needed);
-       FIELD **new_fields = copy_fields(form_fields(form), needed);
+       FIELD **new_fields = copy_fields(old_fields, needed);
 
-       if (old_fields != NULL && new_fields != NULL) {
+       if (new_fields != NULL) {
            bool found = FALSE;
            int ch;
 
@@ -483,9 +474,44 @@ demo_forms(void)
     nl();
 }
 
+static void
+usage(int ok)
+{
+    static const char *msg[] =
+    {
+       "Usage: move_field [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(void)
+main(int argc, char *argv[])
 {
+    int ch;
+
+    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);
+
     setlocale(LC_ALL, "");
 
     initscr();