]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/bs.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / bs.c
index 4de4dbc7f987f3a4141f3305d1cfd5bbbf8b0d31..c7569cf207fd6a9c79de9814b03642f493464c8a 100644 (file)
--- a/test/bs.c
+++ b/test/bs.c
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998-2018,2019 Free Software Foundation, Inc.              *
+ * Copyright 2018-2022,2023 Thomas E. Dickey                                *
+ * Copyright 1998-2016,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            *
@@ -25,7 +26,7 @@
  * sale, use or other dealings in this Software without prior written       *
  * authorization.                                                           *
  ****************************************************************************/
-/* 
+/*
  * bs.c - original author: Bruce Holloway
  *             salvo option by: Chuck A DeGaul
  * with improved user interface, autoconfiguration and code cleanup
@@ -34,7 +35,7 @@
  * v2.0 featuring strict ANSI/POSIX conformance, November 1993.
  * v2.1 with ncurses mouse support, September 1995
  *
- * $Id: bs.c,v 1.74 2019/08/17 21:49:19 tom Exp $
+ * $Id: bs.c,v 1.79 2023/05/27 20:13:10 tom Exp $
  */
 
 #include <test.priv.h>
@@ -180,7 +181,7 @@ static int salvo, blitz, closepack;
 
 #define        PR      (void)addstr
 
-static void uninitgame(int sig) GCC_NORETURN;
+static GCC_NORETURN void uninitgame(int sig);
 
 static void
 uninitgame(int sig GCC_UNUSED)
@@ -897,8 +898,9 @@ sgetc(const char *s)
            ch = toupper(ch);
        if (is_QUIT(ch))
            uninitgame(0);
-       for (s1 = s; *s1 && ch != *s1; ++s1)
-           continue;
+       for (s1 = s; *s1 && ch != *s1; ++s1) {
+           /* EMPTY */ ;
+       }
        if (*s1) {
            AddCh(ch);
            (void) refresh();
@@ -1161,55 +1163,6 @@ playagain(void)
     return (sgetc("YN") == 'Y');
 }
 
-static void
-do_options(int c, char *op[])
-{
-    if (c > 1) {
-       int i;
-
-       for (i = 1; i < c; i++) {
-           switch (op[i][0]) {
-           default:
-           case '?':
-               (void) fprintf(stderr, "Usage: bs [-s | -b] [-c]\n");
-               (void) fprintf(stderr, "\tWhere the options are:\n");
-               (void) fprintf(stderr, "\t-s : play a salvo game\n");
-               (void) fprintf(stderr, "\t-b : play a blitz game\n");
-               (void) fprintf(stderr, "\t-c : ships may be adjacent\n");
-               ExitProgram(EXIT_FAILURE);
-               break;
-           case '-':
-               switch (op[i][1]) {
-               case 'b':
-                   blitz = 1;
-                   if (salvo == 1) {
-                       (void) fprintf(stderr,
-                                      "Bad Arg: -b and -s are mutually exclusive\n");
-                       ExitProgram(EXIT_FAILURE);
-                   }
-                   break;
-               case 's':
-                   salvo = 1;
-                   if (blitz == 1) {
-                       (void) fprintf(stderr,
-                                      "Bad Arg: -s and -b are mutually exclusive\n");
-                       ExitProgram(EXIT_FAILURE);
-                   }
-                   break;
-               case 'c':
-                   closepack = 1;
-                   break;
-               default:
-                   (void) fprintf(stderr,
-                                  "Bad arg: type \"%s ?\" for usage message\n",
-                                  op[0]);
-                   ExitProgram(EXIT_FAILURE);
-               }
-           }
-       }
-    }
-}
-
 static int
 scount(int who)
 {
@@ -1230,12 +1183,68 @@ scount(int who)
     return (shots);
 }
 
+static void
+usage(int ok)
+{
+    static const char *msg[] =
+    {
+       "Usage: bs [options]"
+       ,""
+       ,USAGE_COMMON
+       ,"Options:"
+       ," -b       play a blitz game"
+       ," -c       ships may be adjacent"
+       ," -s       play a salvo game"
+    };
+    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, char *argv[])
 {
-    setlocale(LC_ALL, "");
+    int ch;
 
-    do_options(argc, argv);
+    while ((ch = getopt(argc, argv, OPTS_COMMON "bcs")) != -1) {
+       switch (ch) {
+       case 'b':
+           blitz = 1;
+           if (salvo == 1) {
+               (void) fprintf(stderr,
+                              "Bad Arg: -b and -s are mutually exclusive\n");
+               ExitProgram(EXIT_FAILURE);
+           }
+           break;
+       case 's':
+           salvo = 1;
+           if (blitz == 1) {
+               (void) fprintf(stderr,
+                              "Bad Arg: -s and -b are mutually exclusive\n");
+               ExitProgram(EXIT_FAILURE);
+           }
+           break;
+       case 'c':
+           closepack = 1;
+           break;
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
+       default:
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
+       }
+    }
+    if (optind < argc)
+       usage(FALSE);
+
+    setlocale(LC_ALL, "");
 
     intro();
     do {
@@ -1262,8 +1271,9 @@ main(int argc, char *argv[])
                    }
                }
            } else
-               while ((turn ? cputurn() : plyturn()) && awinna() == -1)
-                   continue;
+               while ((turn ? cputurn() : plyturn()) && awinna() == -1) {
+                   /* EMPTY */ ;
+               }
            turn = OTHER;
        }
     } while