backgammon(6)/hack(6)/larn(6): replace sgtty with termios
[dragonfly.git] / games / backgammon / teachgammon / teach.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)teach.c  8.1 (Berkeley) 5/31/93
35  * $FreeBSD: src/games/backgammon/teachgammon/teach.c,v 1.12 1999/11/30 03:48:30 billf Exp $
36  * $DragonFly: src/games/backgammon/teachgammon/teach.c,v 1.3 2006/08/08 16:36:11 pavalos Exp $
37  */
38
39 #include <string.h>
40 #include <sys/types.h>
41 #include <termcap.h>
42 #include <unistd.h>
43 #include <signal.h>
44 #include "back.h"
45 #include "tutor.h"
46
47 const char *const helpm[] = {
48         "\nEnter a space or newline to roll, or",
49         "     b   to display the board",
50         "     d   to double",
51         "     q   to quit\n",
52         0
53 };
54
55 const char *const contin[] = {
56         "",
57         0
58 };
59
60 int
61 main(int argc, char **argv)
62 {
63         int     i;
64
65         /* revoke privs */
66         setgid(getgid());
67
68         acnt = 1;
69         signal (SIGINT,(sig_t)getout);
70         if (tcgetattr (0,&tty) == -1)                   /* get old tty mode */
71                 errexit ("teachgammon(tcgetattr)");
72         old = tty.c_lflag;
73         raw = ((noech = old & ~ECHO) & ~ICANON);                /* set up modes */
74         ospeed = cfgetospeed(&tty);                             /* for termlib */
75         tflag = getcaps (getenv ("TERM"));
76         getarg (argc, argv);
77         if (tflag)  {
78                 noech &= ~(ICRNL|OXTABS);
79                 raw &= ~(ICRNL|OXTABS);
80                 clear();
81         }
82         text (hello);
83         text (list);
84         i = text (contin);
85         if (i == 0)
86                 i = 2;
87         init();
88         while (i)
89                 switch (i)  {
90
91                 case 1:
92                         leave();
93
94                 case 2:
95                         if ((i = text(intro1)) != 0)
96                                 break;
97                         wrboard();
98                         if ((i = text(intro2)) != 0)
99                                 break;
100
101                 case 3:
102                         if ((i = text(moves)) != 0)
103                                 break;
104
105                 case 4:
106                         if ((i = text(remove)) != 0)
107                                 break;
108
109                 case 5:
110                         if ((i = text(hits)) != 0)
111                                 break;
112
113                 case 6:
114                         if ((i = text(endgame)) != 0)
115                                 break;
116
117                 case 7:
118                         if ((i = text(doubl)) != 0)
119                                 break;
120
121                 case 8:
122                         if ((i = text(stragy)) != 0)
123                                 break;
124
125                 case 9:
126                         if ((i = text(prog)) != 0)
127                                 break;
128
129                 case 10:
130                         if ((i = text(lastch)) != 0)
131                                 break;
132                 }
133         tutor();
134         /* NOTREACHED */
135         return(0);
136 }
137
138 void
139 leave(void)
140 {
141         int i;
142         if (tflag)
143                 clear();
144         else
145                 writec ('\n');
146         fixtty(old);
147         args[0] = strdup("backgammon");
148         args[acnt++] = strdup("-n");
149         args[acnt] = 0;
150         execv (EXEC,args);
151         for (i = 0; i < acnt; i++)
152             free (args[i]);
153         writel ("Help! Backgammon program is missing\007!!\n");
154         exit (-1);
155 }