4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / games / tetris / tetris.c
index 49c2ec5..394465a 100644 (file)
@@ -1,19 +1,19 @@
 /*-
 /*-
- * Copyright (c) 1992 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1992, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Chris Torek and Darren F. Provine.
  *
  * %sccs.include.redist.c%
  *
  *
  * This code is derived from software contributed to Berkeley by
  * Chris Torek and Darren F. Provine.
  *
  * %sccs.include.redist.c%
  *
- *     @(#)tetris.c    5.2 (Berkeley) %G%
+ *     @(#)tetris.c    8.1 (Berkeley) %G%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1992 The Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1992, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -22,6 +22,7 @@ char copyright[] =
 
 #include <sys/time.h>
 
 
 #include <sys/time.h>
 
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -32,11 +33,13 @@ char copyright[] =
 #include "screen.h"
 #include "tetris.h"
 
 #include "screen.h"
 #include "tetris.h"
 
+void onintr __P((int));
+void usage __P((void));
+
 /*
 /*
- * Set up the initial board.
- * The bottom display row is completely set,
- * along with another (hidden) row underneath that.
- * Also, the left and right edges are set.
+ * Set up the initial board.  The bottom display row is completely set,
+ * along with another (hidden) row underneath that.  Also, the left and
+ * right edges are set.
  */
 static void
 setup_board()
  */
 static void
 setup_board()
@@ -84,45 +87,46 @@ elide()
 int
 main(argc, argv)
        int argc;
 int
 main(argc, argv)
        int argc;
-       char **argv;
+       char *argv[];
 {
        register int pos, c;
        register struct shape *curshape;
        register char *keys;
        register int level = 2;
        char key_write[6][10];
 {
        register int pos, c;
        register struct shape *curshape;
        register char *keys;
        register int level = 2;
        char key_write[6][10];
-       int i, j;
+       int ch, i, j;
 
        keys = "jkl pq";
 
 
        keys = "jkl pq";
 
-       if (argc > 3)
-               goto usage;
-
-       while (argc-- >= 2)
-               switch (argv[argc][0]) {
-
-               case '-':
-                       keys = argv[argc];
-                       keys++;
-                       if (strlen(keys) < 6) {
-       usage:
+       while ((ch = getopt(argc, argv, "k:l:s")) != EOF)
+               switch(ch) {
+               case 'k':
+                       if (strlen(keys = optarg) != 6)
+                               usage();
+                       break;
+               case 'l':
+                       level = atoi(optarg);
+                       if (level < MINLEVEL || level > MAXLEVEL) {
                                (void)fprintf(stderr,
                                (void)fprintf(stderr,
-                                   "usage: %s [level] [-keys]\n",
-                                   argv[0]);
+                                   "tetris: level must be from %d to %d",
+                                   MINLEVEL, MAXLEVEL);
                                exit(1);
                        }
                        break;
                                exit(1);
                        }
                        break;
-
+               case 's':
+                       showscores(0);
+                       exit(0);
+               case '?':
                default:
                default:
-                       level = atoi(argv[argc]);
-                       if (level < MINLEVEL) {
-                               showscores(level);
-                               exit(0);
-                       }
-                       if (level > MAXLEVEL)
-                               level = MAXLEVEL;
+                       usage();
                }
 
                }
 
+       argc -= optind;
+       argv += optind;
+
+       if (argc)
+               usage();
+
        fallrate = 1000000 / level;
 
        for (i = 0; i <= 5; i++) {
        fallrate = 1000000 / level;
 
        for (i = 0; i <= 5; i++) {
@@ -147,6 +151,7 @@ main(argc, argv)
                key_write[0], key_write[1], key_write[2], key_write[3],
                key_write[4], key_write[5]);
 
                key_write[0], key_write[1], key_write[2], key_write[3],
                key_write[4], key_write[5]);
 
+       (void)signal(SIGINT, onintr);
        scr_init();
        setup_board();
 
        scr_init();
        setup_board();
 
@@ -263,3 +268,19 @@ main(argc, argv)
 
        exit(0);
 }
 
        exit(0);
 }
+
+void
+onintr(signo)
+       int signo;
+{
+       scr_clear();
+       scr_end();
+       exit(0);
+}
+
+void
+usage()
+{
+       (void)fprintf(stderr, "usage: tetris [-s] [-l level] [-keys]\n");
+       exit(1);
+}