xref: /original-bsd/games/rain/rain.c (revision a043e977)
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.7 (Berkeley) 02/03/93";
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];
55 	static void onsig();
56 
57 	if (!(term = getenv("TERM"))) {
58 		fprintf(stderr, "%s: TERM: parameter not set\n", *argv);
59 		exit(1);
60 	}
61 	if (!(mp = malloc((u_int)1024))) {
62 		fprintf(stderr, "%s: out of space.\n", *argv);
63 		exit(1);
64 	}
65 	if (tgetent(mp, term) <= 0) {
66 		fprintf(stderr, "%s: %s: unknown terminal type\n", *argv, term);
67 		exit(1);
68 	}
69 	tcp = tcb;
70 	if (!(CM = tgetstr("cm", &tcp))) {
71 		fprintf(stderr, "%s: terminal not capable of cursor motion\n", *argv);
72 		exit(1);
73 	}
74 	if (!(BC = tgetstr("bc", &tcp)))
75 		BC = "\b";
76 	if (!(DN = tgetstr("dn", &tcp)))
77 		DN = "\n";
78 	if (!(ND = tgetstr("nd", &tcp)))
79 		ND = " ";
80 	if ((cols = tgetnum("co")) == -1)
81 		cols = 80;
82 	if ((lines = tgetnum("li")) == -1)
83 		lines = 24;
84 	cols -= 4;
85 	lines -= 4;
86 	TE = tgetstr("te", &tcp);
87 	TI = tgetstr("ti", &tcp);
88 	UP = tgetstr("up", &tcp);
89 	if (!(LL = tgetstr("ll", &tcp))) {
90 		if (!(LL = malloc((u_int)10))) {
91 			fprintf(stderr, "%s: out of space.\n", *argv);
92 			exit(1);
93 		}
94 		(void)strcpy(LL, tgoto(CM, 0, 23));
95 	}
96 #ifdef USG
97 	ioctl(1, TCGETA, &sg);
98 	ospeed = sg.c_cflag&CBAUD;
99 #else
100 	gtty(1, &sg);
101 	ospeed = sg.sg_ospeed;
102 #endif
103 	(void)signal(SIGHUP, onsig);
104 	(void)signal(SIGINT, onsig);
105 	(void)signal(SIGQUIT, onsig);
106 	(void)signal(SIGSTOP, onsig);
107 	(void)signal(SIGTSTP, onsig);
108 	(void)signal(SIGTERM, onsig);
109 #ifdef USG
110 	ioctl(1, TCGETA, &old_tty);	/* save tty bits for exit */
111 	ioctl(1, TCGETA, &sg);
112 	sg.c_iflag &= ~ICRNL;
113 	sg.c_oflag &= ~ONLCR;
114 	sg.c_lflag &= ~ECHO;
115 	ioctl(1, TCSETAW, &sg);
116 #else
117 	gtty(1, &old_tty);		/* save tty bits for exit */
118 	gtty(1, &sg);
119 	sg.sg_flags &= ~(CRMOD|ECHO);
120 	stty(1, &sg);
121 #endif
122 	if (TI)
123 		tputs(TI, 1, fputchar);
124 	tputs(tgetstr("cl", &tcp), 1, fputchar);
125 	(void)fflush(stdout);
126 	for (j = 4; j >= 0; --j) {
127 		xpos[j] = random() % cols + 2;
128 		ypos[j] = random() % lines + 2;
129 	}
130 	for (j = 0;;) {
131 		x = random() % cols + 2;
132 		y = random() % lines + 2;
133 		cursor(x, y);
134 		fputchar('.');
135 		cursor(xpos[j], ypos[j]);
136 		fputchar('o');
137 		if (!j--)
138 			j = 4;
139 		cursor(xpos[j], ypos[j]);
140 		fputchar('O');
141 		if (!j--)
142 			j = 4;
143 		cursor(xpos[j], ypos[j] - 1);
144 		fputchar('-');
145 		tputs(DN, 1, fputchar);
146 		tputs(BC, 1, fputchar);
147 		tputs(BC, 1, fputchar);
148 		fputs("|.|", stdout);
149 		tputs(DN, 1, fputchar);
150 		tputs(BC, 1, fputchar);
151 		tputs(BC, 1, fputchar);
152 		fputchar('-');
153 		if (!j--)
154 			j = 4;
155 		cursor(xpos[j], ypos[j] - 2);
156 		fputchar('-');
157 		tputs(DN, 1, fputchar);
158 		tputs(BC, 1, fputchar);
159 		tputs(BC, 1, fputchar);
160 		fputs("/ \\", stdout);
161 		cursor(xpos[j] - 2, ypos[j]);
162 		fputs("| O |", stdout);
163 		cursor(xpos[j] - 1, ypos[j] + 1);
164 		fputs("\\ /", stdout);
165 		tputs(DN, 1, fputchar);
166 		tputs(BC, 1, fputchar);
167 		tputs(BC, 1, fputchar);
168 		fputchar('-');
169 		if (!j--)
170 			j = 4;
171 		cursor(xpos[j], ypos[j] - 2);
172 		fputchar(' ');
173 		tputs(DN, 1, fputchar);
174 		tputs(BC, 1, fputchar);
175 		tputs(BC, 1, fputchar);
176 		fputchar(' ');
177 		tputs(ND, 1, fputchar);
178 		fputchar(' ');
179 		cursor(xpos[j] - 2, ypos[j]);
180 		fputchar(' ');
181 		tputs(ND, 1, fputchar);
182 		fputchar(' ');
183 		tputs(ND, 1, fputchar);
184 		fputchar(' ');
185 		cursor(xpos[j] - 1, ypos[j] + 1);
186 		fputchar(' ');
187 		tputs(ND, 1, fputchar);
188 		fputchar(' ');
189 		tputs(DN, 1, fputchar);
190 		tputs(BC, 1, fputchar);
191 		tputs(BC, 1, fputchar);
192 		fputchar(' ');
193 		xpos[j] = x;
194 		ypos[j] = y;
195 		(void)fflush(stdout);
196 	}
197 }
198 
199 static void
200 onsig()
201 {
202 	tputs(LL, 1, fputchar);
203 	if (TE)
204 		tputs(TE, 1, fputchar);
205 	(void)fflush(stdout);
206 #ifdef USG
207 	ioctl(1, TCSETAW, &old_tty);
208 #else
209 	stty(1, &old_tty);
210 #endif
211 	exit(0);
212 }
213 
214 int
215 fputchar(c)
216 	int c;
217 {
218 	putchar(c);
219 }
220