Initial import from FreeBSD RELENG_4:
[games.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
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)teach.c     8.1 (Berkeley) 5/31/93";
43 #endif
44 static const char rcsid[] =
45  "$FreeBSD: src/games/backgammon/teachgammon/teach.c,v 1.12 1999/11/30 03:48:30 billf Exp $";
46 #endif /* not lint */
47
48 #include <string.h>
49 #include <sys/types.h>
50 #include <unistd.h>
51 #include <signal.h>
52 #include "back.h"
53
54 extern char     *hello[];
55 extern char     *list[];
56 extern char     *intro1[];
57 extern char     *intro2[];
58 extern char     *moves[];
59 extern char     *remove[];
60 extern char     *hits[];
61 extern char     *endgame[];
62 extern char     *doubl[];
63 extern char     *stragy[];
64 extern char     *prog[];
65 extern char     *lastch[];
66
67 extern char     ospeed;                 /* tty output speed for termlib */
68
69 const char *const helpm[] = {
70         "\nEnter a space or newline to roll, or",
71         "     b   to display the board",
72         "     d   to double",
73         "     q   to quit\n",
74         0
75 };
76
77 const char *const contin[] = {
78         "",
79         0
80 };
81
82 main (argc,argv)
83 int     argc;
84 char    **argv;
85
86 {
87         int     i;
88
89         /* revoke privs */
90         setgid(getgid());
91
92         acnt = 1;
93         signal (SIGINT,getout);
94         if (gtty (0,&tty) == -1)                        /* get old tty mode */
95                 errexit ("teachgammon(gtty)");
96         old = tty.sg_flags;
97 #ifdef V7
98         raw = ((noech = old & ~ECHO) | CBREAK);         /* set up modes */
99 #else
100         raw = ((noech = old & ~ECHO) | RAW);            /* set up modes */
101 #endif
102         ospeed = tty.sg_ospeed;                         /* for termlib */
103         tflag = getcaps (getenv ("TERM"));
104         getarg (argc, argv);
105         if (tflag)  {
106                 noech &= ~(CRMOD|XTABS);
107                 raw &= ~(CRMOD|XTABS);
108                 clear();
109         }
110         text (hello);
111         text (list);
112         i = text (contin);
113         if (i == 0)
114                 i = 2;
115         init();
116         while (i)
117                 switch (i)  {
118
119                 case 1:
120                         leave();
121
122                 case 2:
123                         if (i = text(intro1))
124                                 break;
125                         wrboard();
126                         if (i = text(intro2))
127                                 break;
128
129                 case 3:
130                         if (i = text(moves))
131                                 break;
132
133                 case 4:
134                         if (i = text(remove))
135                                 break;
136
137                 case 5:
138                         if (i = text(hits))
139                                 break;
140
141                 case 6:
142                         if (i = text(endgame))
143                                 break;
144
145                 case 7:
146                         if (i = text(doubl))
147                                 break;
148
149                 case 8:
150                         if (i = text(stragy))
151                                 break;
152
153                 case 9:
154                         if (i = text(prog))
155                                 break;
156
157                 case 10:
158                         if (i = text(lastch))
159                                 break;
160                 }
161         tutor();
162 }
163
164 leave()  {
165         int i;
166         if (tflag)
167                 clear();
168         else
169                 writec ('\n');
170         fixtty(old);
171         args[0] = strdup("backgammon");
172         args[acnt++] = strdup("-n");
173         args[acnt] = 0;
174         execv (EXEC,args);
175         for (i = 0; i < acnt; i++)
176             free (args[i]);
177         writel ("Help! Backgammon program is missing\007!!\n");
178         exit (-1);
179 }