xref: /original-bsd/games/rain/rain.c (revision bff54947)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)rain.c	5.5 (Berkeley) 06/01/90";
16 #endif /* not lint */
17 
18 /*
19  * rain 11/3/1980 EPS/CITHEP
20  * cc rain.c -o rain -O -ltermlib
21  */
22 
23 #include <sys/types.h>
24 #include <stdio.h>
25 #ifdef USG
26 #include <termio.h>
27 #else
28 #include <sgtty.h>
29 #endif
30 #include <signal.h>
31 
32 #define	cursor(c, r)	tputs(tgoto(CM, c, r), 1, fputchar)
33 
34 #ifdef USG
35 static struct termio sg, old_tty;
36 #else
37 static struct sgttyb sg, old_tty;
38 #endif
39 
40 int	fputchar();
41 char	*LL, *TE, *tgoto();
42 
43 main(argc, argv)
44 	int argc;
45 	char **argv;
46 {
47 	extern short ospeed;
48 	extern char *UP;
49 	register int x, y, j;
50 	register char *CM, *BC, *DN, *ND, *term;
51 	char *TI, *tcp, *mp, tcb[100],
52 		*malloc(), *getenv(), *strcpy(), *tgetstr();
53 	long cols, lines, random();
54 	int xpos[5], ypos[5], onsig();
55 
56 	if (!(term = getenv("TERM"))) {
57 		fprintf(stderr, "%s: TERM: parameter not set\n", *argv);
58 		exit(1);
59 	}
60 	if (!(mp = malloc((u_int)1024))) {
61 		fprintf(stderr, "%s: out of space.\n", *argv);
62 		exit(1);
63 	}
64 	if (tgetent(mp, term) <= 0) {
65 		fprintf(stderr, "%s: %s: unknown terminal type\n", *argv, term);
66 		exit(1);
67 	}
68 	tcp = tcb;
69 	if (!(CM = tgetstr("cm", &tcp))) {
70 		fprintf(stderr, "%s: terminal not capable of cursor motion\n", *argv);
71 		exit(1);
72 	}
73 	if (!(BC = tgetstr("bc", &tcp)))
74 		BC = "\b";
75 	if (!(DN = tgetstr("dn", &tcp)))
76 		DN = "\n";
77 	if (!(ND = tgetstr("nd", &tcp)))
78 		ND = " ";
79 	if ((cols = tgetnum("co")) == -1)
80 		cols = 80;
81 	if ((lines = tgetnum("li")) == -1)
82 		lines = 24;
83 	cols -= 4;
84 	lines -= 4;
85 	TE = tgetstr("te", &tcp);
86 	TI = tgetstr("ti", &tcp);
87 	UP = tgetstr("up", &tcp);
88 	if (!(LL = tgetstr("ll", &tcp))) {
89 		if (!(LL = malloc((u_int)10))) {
90 			fprintf(stderr, "%s: out of space.\n", *argv);
91 			exit(1);
92 		}
93 		(void)strcpy(LL, tgoto(CM, 0, 23));
94 	}
95 #ifdef USG
96 	ioctl(1, TCGETA, &sg);
97 	ospeed = sg.c_cflag&CBAUD;
98 #else
99 	gtty(1, &sg);
100 	ospeed = sg.sg_ospeed;
101 #endif
102 	(void)signal(SIGHUP, onsig);
103 	(void)signal(SIGINT, onsig);
104 	(void)signal(SIGQUIT, onsig);
105 	(void)signal(SIGSTOP, onsig);
106 	(void)signal(SIGTSTP, onsig);
107 	(void)signal(SIGTERM, onsig);
108 #ifdef USG
109 	ioctl(1, TCGETA, &old_tty);	/* save tty bits for exit */
110 	ioctl(1, TCGETA, &sg);
111 	sg.c_iflag &= ~ICRNL;
112 	sg.c_oflag &= ~ONLCR;
113 	sg.c_lflag &= ~ECHO;
114 	ioctl(1, TCSETAW, &sg);
115 #else
116 	gtty(1, &old_tty);		/* save tty bits for exit */
117 	gtty(1, &sg);
118 	sg.sg_flags &= ~(CRMOD|ECHO);
119 	stty(1, &sg);
120 #endif
121 	if (TI)
122 		tputs(TI, 1, fputchar);
123 	tputs(tgetstr("cl", &tcp), 1, fputchar);
124 	(void)fflush(stdout);
125 	for (j = 4; j >= 0; --j) {
126 		xpos[j] = random() % cols + 2;
127 		ypos[j] = random() % lines + 2;
128 	}
129 	for (j = 0;;) {
130 		x = random() % cols + 2;
131 		y = random() % lines + 2;
132 		cursor(x, y);
133 		fputchar('.');
134 		cursor(xpos[j], ypos[j]);
135 		fputchar('o');
136 		if (!j--)
137 			j = 4;
138 		cursor(xpos[j], ypos[j]);
139 		fputchar('O');
140 		if (!j--)
141 			j = 4;
142 		cursor(xpos[j], ypos[j] - 1);
143 		fputchar('-');
144 		tputs(DN, 1, fputchar);
145 		tputs(BC, 1, fputchar);
146 		tputs(BC, 1, fputchar);
147 		fputs("|.|", stdout);
148 		tputs(DN, 1, fputchar);
149 		tputs(BC, 1, fputchar);
150 		tputs(BC, 1, fputchar);
151 		fputchar('-');
152 		if (!j--)
153 			j = 4;
154 		cursor(xpos[j], ypos[j] - 2);
155 		fputchar('-');
156 		tputs(DN, 1, fputchar);
157 		tputs(BC, 1, fputchar);
158 		tputs(BC, 1, fputchar);
159 		fputs("/ \\", stdout);
160 		cursor(xpos[j] - 2, ypos[j]);
161 		fputs("| O |", stdout);
162 		cursor(xpos[j] - 1, ypos[j] + 1);
163 		fputs("\\ /", stdout);
164 		tputs(DN, 1, fputchar);
165 		tputs(BC, 1, fputchar);
166 		tputs(BC, 1, fputchar);
167 		fputchar('-');
168 		if (!j--)
169 			j = 4;
170 		cursor(xpos[j], ypos[j] - 2);
171 		fputchar(' ');
172 		tputs(DN, 1, fputchar);
173 		tputs(BC, 1, fputchar);
174 		tputs(BC, 1, fputchar);
175 		fputchar(' ');
176 		tputs(ND, 1, fputchar);
177 		fputchar(' ');
178 		cursor(xpos[j] - 2, ypos[j]);
179 		fputchar(' ');
180 		tputs(ND, 1, fputchar);
181 		fputchar(' ');
182 		tputs(ND, 1, fputchar);
183 		fputchar(' ');
184 		cursor(xpos[j] - 1, ypos[j] + 1);
185 		fputchar(' ');
186 		tputs(ND, 1, fputchar);
187 		fputchar(' ');
188 		tputs(DN, 1, fputchar);
189 		tputs(BC, 1, fputchar);
190 		tputs(BC, 1, fputchar);
191 		fputchar(' ');
192 		xpos[j] = x;
193 		ypos[j] = y;
194 		(void)fflush(stdout);
195 	}
196 }
197 
198 static
199 onsig()
200 {
201 	tputs(LL, 1, fputchar);
202 	if (TE)
203 		tputs(TE, 1, fputchar);
204 	(void)fflush(stdout);
205 #ifdef USG
206 	ioctl(1, TCSETAW, &old_tty);
207 #else
208 	stty(1, &old_tty);
209 #endif
210 	exit(0);
211 }
212 
213 static
214 fputchar(c)
215 	char c;
216 {
217 	putchar(c);
218 }
219