xref: /csrg-svn/games/rain/rain.c (revision 33199)
121200Sdist /*
221200Sdist  * Copyright (c) 1980 Regents of the University of California.
3*33199Sbostic  * All rights reserved.
4*33199Sbostic  *
5*33199Sbostic  * Redistribution and use in source and binary forms are permitted
6*33199Sbostic  * provided that this notice is preserved and that due credit is given
7*33199Sbostic  * to the University of California at Berkeley. The name of the University
8*33199Sbostic  * may not be used to endorse or promote products derived from this
9*33199Sbostic  * software without specific prior written permission. This software
10*33199Sbostic  * is provided ``as is'' without express or implied warranty.
1121200Sdist  */
128869Smckusick 
1321200Sdist #ifndef lint
1421200Sdist char copyright[] =
1521200Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1621200Sdist  All rights reserved.\n";
17*33199Sbostic #endif /* not lint */
188869Smckusick 
1921200Sdist #ifndef lint
20*33199Sbostic static char sccsid[] = "@(#)rain.c	5.3 (Berkeley) 01/02/88";
21*33199Sbostic #endif /* not lint */
2221200Sdist 
23*33199Sbostic /*
24*33199Sbostic  * rain 11/3/1980 EPS/CITHEP
25*33199Sbostic  * cc rain.c -o rain -O -ltermlib
26*33199Sbostic  */
2723869Smckusick 
28*33199Sbostic #include <sys/types.h>
298869Smckusick #include <stdio.h>
3023869Smckusick #ifdef USG
3123869Smckusick #include <termio.h>
3223869Smckusick #else
338869Smckusick #include <sgtty.h>
3423869Smckusick #endif
358869Smckusick #include <signal.h>
36*33199Sbostic 
37*33199Sbostic #define	cursor(c, r)	tputs(tgoto(CM, c, r), 1, fputchar)
38*33199Sbostic 
3923869Smckusick #ifdef USG
40*33199Sbostic static struct termio sg, old_tty;
4123869Smckusick #else
42*33199Sbostic static struct sgttyb sg, old_tty;
4323869Smckusick #endif
44*33199Sbostic 
45*33199Sbostic int	fputchar();
46*33199Sbostic char	*LL, *TE, *tgoto();
47*33199Sbostic 
48*33199Sbostic main(argc, argv)
49*33199Sbostic 	int argc;
50*33199Sbostic 	char **argv;
518869Smckusick {
52*33199Sbostic 	extern short ospeed;
53*33199Sbostic 	extern char *UP;
54*33199Sbostic 	register int x, y, j;
55*33199Sbostic 	register char *CM, *BC, *DN, *ND, *term;
56*33199Sbostic 	char *TI, *tcp, *mp, tcb[100],
57*33199Sbostic 		*malloc(), *getenv(), *strcpy(), *tgetstr();
58*33199Sbostic 	long cols, lines, random();
59*33199Sbostic 	int xpos[5], ypos[5], onsig();
6023869Smckusick 
61*33199Sbostic 	if (!(term = getenv("TERM"))) {
62*33199Sbostic 		fprintf(stderr, "%s: TERM: parameter not set\n", *argv);
63*33199Sbostic 		exit(1);
64*33199Sbostic 	}
65*33199Sbostic 	if (!(mp = malloc((u_int)1024))) {
66*33199Sbostic 		fprintf(stderr, "%s: out of space.\n", *argv);
67*33199Sbostic 		exit(1);
68*33199Sbostic 	}
69*33199Sbostic 	if (tgetent(mp, term) <= 0) {
70*33199Sbostic 		fprintf(stderr, "%s: %s: unknown terminal type\n", *argv, term);
71*33199Sbostic 		exit(1);
72*33199Sbostic 	}
73*33199Sbostic 	tcp = tcb;
74*33199Sbostic 	if (!(CM = tgetstr("cm", &tcp))) {
75*33199Sbostic 		fprintf(stderr, "%s: terminal not capable of cursor motion\n", *argv);
76*33199Sbostic 		exit(1);
77*33199Sbostic 	}
78*33199Sbostic 	if (!(BC = tgetstr("bc", &tcp)))
79*33199Sbostic 		BC = "\b";
80*33199Sbostic 	if (!(DN = tgetstr("dn", &tcp)))
81*33199Sbostic 		DN = "\n";
82*33199Sbostic 	if (!(ND = tgetstr("nd", &tcp)))
83*33199Sbostic 		ND = " ";
84*33199Sbostic 	if ((cols = tgetnum("co")) == -1)
85*33199Sbostic 		cols = 80;
86*33199Sbostic 	if ((lines = tgetnum("li")) == -1)
87*33199Sbostic 		lines = 24;
88*33199Sbostic 	cols -= 4;
89*33199Sbostic 	lines -= 4;
90*33199Sbostic 	TE = tgetstr("te", &tcp);
91*33199Sbostic 	TI = tgetstr("ti", &tcp);
92*33199Sbostic 	UP = tgetstr("up", &tcp);
93*33199Sbostic 	if (!(LL = tgetstr("ll", &tcp))) {
94*33199Sbostic 		if (!(LL = malloc((u_int)10))) {
95*33199Sbostic 			fprintf(stderr, "%s: out of space.\n", *argv);
96*33199Sbostic 			exit(1);
97*33199Sbostic 		}
98*33199Sbostic 		(void)strcpy(LL, tgoto(CM, 0, 23));
99*33199Sbostic 	}
10023869Smckusick #ifdef USG
101*33199Sbostic 	ioctl(1, TCGETA, &sg);
102*33199Sbostic 	ospeed = sg.c_cflag&CBAUD;
10323869Smckusick #else
104*33199Sbostic 	gtty(1, &sg);
105*33199Sbostic 	ospeed = sg.sg_ospeed;
10623869Smckusick #endif
107*33199Sbostic 	(void)signal(SIGHUP, onsig);
108*33199Sbostic 	(void)signal(SIGINT, onsig);
109*33199Sbostic 	(void)signal(SIGQUIT, onsig);
110*33199Sbostic 	(void)signal(SIGSTOP, onsig);
111*33199Sbostic 	(void)signal(SIGTSTP, onsig);
112*33199Sbostic 	(void)signal(SIGTERM, onsig);
11323869Smckusick #ifdef USG
114*33199Sbostic 	ioctl(1, TCGETA, &old_tty);	/* save tty bits for exit */
115*33199Sbostic 	ioctl(1, TCGETA, &sg);
116*33199Sbostic 	sg.c_iflag &= ~ICRNL;
117*33199Sbostic 	sg.c_oflag &= ~ONLCR;
118*33199Sbostic 	sg.c_lflag &= ~ECHO;
119*33199Sbostic 	ioctl(1, TCSETAW, &sg);
12023869Smckusick #else
121*33199Sbostic 	gtty(1, &old_tty);		/* save tty bits for exit */
122*33199Sbostic 	gtty(1, &sg);
123*33199Sbostic 	sg.sg_flags &= ~(CRMOD|ECHO);
124*33199Sbostic 	stty(1, &sg);
12523869Smckusick #endif
126*33199Sbostic 	if (TI)
127*33199Sbostic 		tputs(TI, 1, fputchar);
128*33199Sbostic 	tputs(tgetstr("cl", &tcp), 1, fputchar);
129*33199Sbostic 	(void)fflush(stdout);
130*33199Sbostic 	for (j = 4; j >= 0; --j) {
131*33199Sbostic 		xpos[j] = random() % cols + 2;
132*33199Sbostic 		ypos[j] = random() % lines + 2;
133*33199Sbostic 	}
134*33199Sbostic 	for (j = 0;;) {
135*33199Sbostic 		x = random() % cols + 2;
136*33199Sbostic 		y = random() % lines + 2;
137*33199Sbostic 		cursor(x, y);
138*33199Sbostic 		fputchar('.');
139*33199Sbostic 		cursor(xpos[j], ypos[j]);
140*33199Sbostic 		fputchar('o');
141*33199Sbostic 		if (!j--)
142*33199Sbostic 			j = 4;
143*33199Sbostic 		cursor(xpos[j], ypos[j]);
144*33199Sbostic 		fputchar('O');
145*33199Sbostic 		if (!j--)
146*33199Sbostic 			j = 4;
147*33199Sbostic 		cursor(xpos[j], ypos[j] - 1);
148*33199Sbostic 		fputchar('-');
149*33199Sbostic 		tputs(DN, 1, fputchar);
150*33199Sbostic 		tputs(BC, 1, fputchar);
151*33199Sbostic 		tputs(BC, 1, fputchar);
152*33199Sbostic 		fputs("|.|", stdout);
153*33199Sbostic 		tputs(DN, 1, fputchar);
154*33199Sbostic 		tputs(BC, 1, fputchar);
155*33199Sbostic 		tputs(BC, 1, fputchar);
156*33199Sbostic 		fputchar('-');
157*33199Sbostic 		if (!j--)
158*33199Sbostic 			j = 4;
159*33199Sbostic 		cursor(xpos[j], ypos[j] - 2);
160*33199Sbostic 		fputchar('-');
161*33199Sbostic 		tputs(DN, 1, fputchar);
162*33199Sbostic 		tputs(BC, 1, fputchar);
163*33199Sbostic 		tputs(BC, 1, fputchar);
164*33199Sbostic 		fputs("/ \\", stdout);
165*33199Sbostic 		cursor(xpos[j] - 2, ypos[j]);
166*33199Sbostic 		fputs("| O |", stdout);
167*33199Sbostic 		cursor(xpos[j] - 1, ypos[j] + 1);
168*33199Sbostic 		fputs("\\ /", stdout);
169*33199Sbostic 		tputs(DN, 1, fputchar);
170*33199Sbostic 		tputs(BC, 1, fputchar);
171*33199Sbostic 		tputs(BC, 1, fputchar);
172*33199Sbostic 		fputchar('-');
173*33199Sbostic 		if (!j--)
174*33199Sbostic 			j = 4;
175*33199Sbostic 		cursor(xpos[j], ypos[j] - 2);
176*33199Sbostic 		fputchar(' ');
177*33199Sbostic 		tputs(DN, 1, fputchar);
178*33199Sbostic 		tputs(BC, 1, fputchar);
179*33199Sbostic 		tputs(BC, 1, fputchar);
180*33199Sbostic 		fputchar(' ');
181*33199Sbostic 		tputs(ND, 1, fputchar);
182*33199Sbostic 		fputchar(' ');
183*33199Sbostic 		cursor(xpos[j] - 2, ypos[j]);
184*33199Sbostic 		fputchar(' ');
185*33199Sbostic 		tputs(ND, 1, fputchar);
186*33199Sbostic 		fputchar(' ');
187*33199Sbostic 		tputs(ND, 1, fputchar);
188*33199Sbostic 		fputchar(' ');
189*33199Sbostic 		cursor(xpos[j] - 1, ypos[j] + 1);
190*33199Sbostic 		fputchar(' ');
191*33199Sbostic 		tputs(ND, 1, fputchar);
192*33199Sbostic 		fputchar(' ');
193*33199Sbostic 		tputs(DN, 1, fputchar);
194*33199Sbostic 		tputs(BC, 1, fputchar);
195*33199Sbostic 		tputs(BC, 1, fputchar);
196*33199Sbostic 		fputchar(' ');
197*33199Sbostic 		xpos[j] = x;
198*33199Sbostic 		ypos[j] = y;
199*33199Sbostic 		(void)fflush(stdout);
200*33199Sbostic 	}
2018869Smckusick }
202*33199Sbostic 
203*33199Sbostic static
204*33199Sbostic onsig()
2058869Smckusick {
206*33199Sbostic 	tputs(LL, 1, fputchar);
207*33199Sbostic 	if (TE)
208*33199Sbostic 		tputs(TE, 1, fputchar);
209*33199Sbostic 	(void)fflush(stdout);
21023869Smckusick #ifdef USG
211*33199Sbostic 	ioctl(1, TCSETAW, &old_tty);
21223869Smckusick #else
213*33199Sbostic 	stty(1, &old_tty);
21423869Smckusick #endif
215*33199Sbostic 	exit(0);
2168869Smckusick }
217*33199Sbostic 
218*33199Sbostic static
2198869Smckusick fputchar(c)
220*33199Sbostic 	char c;
2218869Smckusick {
222*33199Sbostic 	putchar(c);
2238869Smckusick }
224