]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - atc/main.c
set the cursor to invisible
[bsdgames-darwin.git] / atc / main.c
index 0910da90557bb7f52cec72fc858d37108dc6d1e0..db0896514e5ab64a16afa13094a8883b4ad8b671 100644 (file)
@@ -1,6 +1,8 @@
+/*     $NetBSD: main.c,v 1.24 2015/06/25 05:33:02 dholland Exp $       */
+
 /*-
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1990, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Ed James.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
  */
 
+#include <sys/cdefs.h>
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
- All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1990, 1993\
+ The Regents of the University of California.  All rights reserved.");
 #endif /* not lint */
 
 #ifndef lint
-/*static char sccsid[] = "from: @(#)main.c     5.4 (Berkeley) 3/5/91";*/
-static char rcsid[] = "$Id: main.c,v 1.2 1993/08/01 18:57:04 mycroft Exp $";
+#if 0
+static char sccsid[] = "@(#)main.c     8.1 (Berkeley) 5/31/93";
+#else
+__RCSID("$NetBSD: main.c,v 1.24 2015/06/25 05:33:02 dholland Exp $");
+#endif
 #endif /* not lint */
 
-#include "include.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <termios.h>
+#include <getopt.h>
+#include <err.h>
+
 #include "pathnames.h"
+#include "def.h"
+#include "struct.h"
+#include "extern.h"
+#include "tunable.h"
+
+extern FILE    *yyin;
 
-main(ac, av)
-       char    *av[];
+static int read_file(const char *);
+static const char *default_game(void);
+static const char *okay_game(const char *);
+static int list_games(void);
+static void quit(int);
+
+int
+main(int argc, char *argv[])
 {
-       int                     seed;
+       unsigned long           seed;
        int                     f_usage = 0, f_list = 0, f_showscore = 0;
        int                     f_printpath = 0;
-       char                    *file = NULL;
-       char                    *name, *ptr;
+       const char              *file = NULL;
+       int                     ch;
+       struct sigaction        sa;
 #ifdef BSD
        struct itimerval        itv;
 #endif
-       extern char             *default_game(), *okay_game();
-       extern void             log_score(), quit(), update();
 
-       start_time = seed = time(0);
+       /* Open the score file then revoke setgid privileges */
+       open_score_file();
+       (void)setgid(getgid());
+
+       start_time = time(NULL);
+       seed = start_time;
 
-       name = *av++;
-       while (*av) {
-#ifndef SAVEDASH
-               if (**av == '-') 
-                       *++*av;
-               else
+       while ((ch = getopt(argc, argv, ":u?lstpg:f:r:")) != -1) {
+               switch (ch) {
+               case '?':
+               case 'u':
+               default: 
+                       f_usage = 1;
+                       break;
+               case 'l':
+                       f_list = 1;
+                       break;
+               case 's':
+               case 't':
+                       f_showscore = 1;
+                       break;
+               case 'p':
+                       f_printpath = 1;
+                       break;
+               case 'r':
+                       seed = atoi(optarg);
+                       break;
+               case 'f':
+               case 'g':
+                       file = optarg;
                        break;
-#endif
-               ptr = *av++;
-               while (*ptr) {
-                       switch (*ptr) {
-                       case '?':
-                       case 'u':
-                               f_usage++;
-                               break;
-                       case 'l':
-                               f_list++;
-                               break;
-                       case 's':
-                       case 't':
-                               f_showscore++;
-                               break;
-                       case 'p':
-                               f_printpath++;
-                               break;
-                       case 'r':
-                               seed = atoi(*av);
-                               av++;
-                               break;
-                       case 'f':
-                       case 'g':
-                               file = *av;
-                               av++;
-                               break;
-                       default: 
-                               fprintf(stderr, "Unknown option '%c'\n", *ptr,
-                                       name);
-                               f_usage++;
-                               break;
-                       }
-                       ptr++;
                }
        }
+       if (optind < argc)
+               f_usage = 1;
        srandom(seed);
 
        if (f_usage)
-               fprintf(stderr, 
+               (void)fprintf(stderr, 
                    "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
-                       name);
+                   argv[0]);
        if (f_showscore)
-               log_score(1);
+               (void)log_score(1);
        if (f_list)
-               list_games();
+               (void)list_games();
        if (f_printpath) {
                char    buf[100];
 
-               strcpy(buf, _PATH_GAMES);
-               buf[strlen(buf) - 1] = '\0';
-               puts(buf);
+               (void)strlcpy(buf, _PATH_GAMES, 100);
+               (void)puts(buf);
        }
                
        if (f_usage || f_showscore || f_list || f_printpath)
@@ -150,41 +158,35 @@ main(ac, av)
 
        addplane();
 
-       signal(SIGINT, quit);
-       signal(SIGQUIT, quit);
-#ifdef BSD
-       signal(SIGTSTP, SIG_IGN);
-       signal(SIGSTOP, SIG_IGN);
-#endif
-       signal(SIGHUP, log_score);
-       signal(SIGTERM, log_score);
-
+       (void)signal(SIGINT, quit);
+       (void)signal(SIGQUIT, quit);
 #ifdef BSD
-       ioctl(fileno(stdin), TIOCGETP, &tty_start);
-       bcopy(&tty_start, &tty_new, sizeof(tty_new));
-       tty_new.sg_flags |= CBREAK;
-       tty_new.sg_flags &= ~ECHO;
-       ioctl(fileno(stdin), TIOCSETP, &tty_new);
+       (void)signal(SIGTSTP, SIG_IGN);
 #endif
+       (void)signal(SIGHUP, log_score_quit);
+       (void)signal(SIGTERM, log_score_quit);
 
-#ifdef SYSV
-       ioctl(fileno(stdin), TCGETA, &tty_start);
-       bcopy(&tty_start, &tty_new, sizeof(tty_new));
-       tty_new.c_lflag &= ~ICANON;
-       tty_new.c_lflag &= ~ECHO;
+       (void)tcgetattr(fileno(stdin), &tty_start);
+       tty_new = tty_start;
+       tty_new.c_lflag &= ~(ICANON|ECHO);
+       tty_new.c_iflag |= ICRNL;
        tty_new.c_cc[VMIN] = 1;
        tty_new.c_cc[VTIME] = 0;
-       ioctl(fileno(stdin), TCSETAW, &tty_new);
-#endif
+       (void)tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
 
-       signal(SIGALRM, update);
+       sa.sa_handler = update;
+       (void)sigemptyset(&sa.sa_mask);
+       (void)sigaddset(&sa.sa_mask, SIGALRM);
+       (void)sigaddset(&sa.sa_mask, SIGINT);
+       sa.sa_flags = 0;
+       (void)sigaction(SIGALRM, &sa, (struct sigaction *)0);
 
 #ifdef BSD
        itv.it_value.tv_sec = 0;
        itv.it_value.tv_usec = 1;
        itv.it_interval.tv_sec = sp->update_secs;
        itv.it_interval.tv_usec = 0;
-       setitimer(ITIMER_REAL, &itv, NULL);
+       (void)setitimer(ITIMER_REAL, &itv, NULL);
 #endif
 #ifdef SYSV
        alarm(sp->update_secs);
@@ -197,20 +199,20 @@ main(ac, av)
 #ifdef BSD
                        itv.it_value.tv_sec = 0;
                        itv.it_value.tv_usec = 0;
-                       setitimer(ITIMER_REAL, &itv, NULL);
+                       (void)setitimer(ITIMER_REAL, &itv, NULL);
 #endif
 #ifdef SYSV
                        alarm(0);
 #endif
 
-                       update();
+                       update(0);
 
 #ifdef BSD
                        itv.it_value.tv_sec = sp->update_secs;
                        itv.it_value.tv_usec = 0;
                        itv.it_interval.tv_sec = sp->update_secs;
                        itv.it_interval.tv_usec = 0;
-                       setitimer(ITIMER_REAL, &itv, NULL);
+                       (void)setitimer(ITIMER_REAL, &itv, NULL);
 #endif
 #ifdef SYSV
                        alarm(sp->update_secs);
@@ -219,20 +221,19 @@ main(ac, av)
        }
 }
 
-read_file(s)
-       char    *s;
+static int
+read_file(const char *s)
 {
-       extern FILE     *yyin;
        int             retval;
 
-       file = s;
+       filename = s;
        yyin = fopen(s, "r");
        if (yyin == NULL) {
-               perror(s);
+               warn("fopen %s", s);
                return (-1);
        }
        retval = yyparse();
-       fclose(yyin);
+       (void)fclose(yyin);
 
        if (retval != 0)
                return (-1);
@@ -240,88 +241,117 @@ read_file(s)
                return (0);
 }
 
-char   *
-default_game()
+static const char *
+default_game(void)
 {
        FILE            *fp;
        static char     file[256];
        char            line[256], games[256];
 
-       strcpy(games, _PATH_GAMES);
-       strcat(games, GAMES);
+       (void)strlcpy(games, _PATH_GAMES, 256);
+       (void)strlcat(games, GAMES, 256);
 
        if ((fp = fopen(games, "r")) == NULL) {
-               perror(games);
+               warn("fopen %s", games);
                return (NULL);
        }
        if (fgets(line, sizeof(line), fp) == NULL) {
-               fprintf(stderr, "%s: no default game available\n", games);
+               (void)fprintf(stderr, "%s: no default game available\n", games);
+               fclose(fp);
                return (NULL);
        }
-       fclose(fp);
+       (void)fclose(fp);
        line[strlen(line) - 1] = '\0';
-       strcpy(file, _PATH_GAMES);
-       strcat(file, line);
+       (void)strlcpy(file, _PATH_GAMES, 256);
+       (void)strlcat(file, line, 256);
        return (file);
 }
 
-char   *
-okay_game(s)
-       char    *s;
+static const char *
+okay_game(const char *s)
 {
        FILE            *fp;
        static char     file[256];
-       char            *ret = NULL, line[256], games[256];
+       const char      *ret = NULL;
+       char            line[256], games[256];
 
-       strcpy(games, _PATH_GAMES);
-       strcat(games, GAMES);
+       (void)strlcpy(games, _PATH_GAMES, 256);
+       (void)strlcat(games, GAMES, 256);
 
        if ((fp = fopen(games, "r")) == NULL) {
-               perror(games);
+               warn("fopen %s", games);
                return (NULL);
        }
        while (fgets(line, sizeof(line), fp) != NULL) {
                line[strlen(line) - 1] = '\0';
                if (strcmp(s, line) == 0) {
-                       strcpy(file, _PATH_GAMES);
-                       strcat(file, line);
+                       (void)strlcpy(file, _PATH_GAMES, 256);
+                       (void)strlcat(file, line, 256);
                        ret = file;
                        break;
                }
        }
-       fclose(fp);
+       (void)fclose(fp);
        if (ret == NULL) {
                test_mode = 1;
                ret = s;
-               fprintf(stderr, "%s: %s: game not found\n", games, s);
-               fprintf(stderr, "Your score will not be logged.\n");
-               sleep(2);       /* give the guy time to read it */
+               (void)fprintf(stderr, "%s: %s: game not found\n", games, s);
+               (void)fprintf(stderr, "Your score will not be logged.\n");
+               (void)sleep(2); /* give the guy time to read it */
        }
        return (ret);
 }
 
-list_games()
+static int
+list_games(void)
 {
        FILE            *fp;
        char            line[256], games[256];
        int             num_games = 0;
 
-       strcpy(games, _PATH_GAMES);
-       strcat(games, GAMES);
+       (void)strlcpy(games, _PATH_GAMES, 256);
+       (void)strlcat(games, GAMES, 256);
 
        if ((fp = fopen(games, "r")) == NULL) {
-               perror(games);
+               warn("fopen %s", games);
                return (-1);
        }
-       puts("available games:");
+       (void)puts("available games:");
        while (fgets(line, sizeof(line), fp) != NULL) {
-               printf("        %s", line);
+               (void)printf("  %s", line);
                num_games++;
        }
-       fclose(fp);
+       (void)fclose(fp);
        if (num_games == 0) {
-               fprintf(stderr, "%s: no games available\n", games);
+               (void)fprintf(stderr, "%s: no games available\n", games);
                return (-1);
        }
        return (0);
 }
+
+/* ARGSUSED */
+static void
+quit(int dummy __unused)
+{
+       int c;
+#ifdef BSD
+       struct itimerval        itv;
+#endif
+       ioaskquit();
+       c = getAChar();
+       if (c == EOF || c == 'y') {
+               /* disable timer */
+#ifdef BSD
+               itv.it_value.tv_sec = 0;
+               itv.it_value.tv_usec = 0;
+               (void)setitimer(ITIMER_REAL, &itv, NULL);
+#endif
+#ifdef SYSV
+               alarm(0);
+#endif
+               shutdown_gr();
+               (void)log_score(0);
+               exit(0);
+       }
+       ionoquit();
+}