]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - rain/rain.c
Use POSIX tty semantics.
[bsdgames-darwin.git] / rain / rain.c
1 /* $NetBSD: rain.c,v 1.7 1995/04/29 00:51:04 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)rain.c 8.1 (Berkeley) 5/31/93";
45 #else
46 static char rcsid[] = "$NetBSD: rain.c,v 1.7 1995/04/29 00:51:04 mycroft Exp $";
47 #endif
48 #endif /* not lint */
49
50 /*
51 * rain 11/3/1980 EPS/CITHEP
52 * cc rain.c -o rain -O -ltermlib
53 */
54
55 #include <sys/types.h>
56 #include <stdio.h>
57 #include <termios.h>
58 #include <signal.h>
59
60 #define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar)
61
62 static struct termios sg, old_tty;
63
64 void fputchar __P((int));
65 char *LL, *TE, *tgoto();
66
67 main(argc, argv)
68 int argc;
69 char **argv;
70 {
71 extern speed_t ospeed;
72 extern char *UP;
73 register int x, y, j;
74 register char *CM, *BC, *DN, *ND, *term;
75 char *TI, *tcp, *mp, tcb[100],
76 *malloc(), *getenv(), *strcpy(), *tgetstr();
77 long cols, lines, random();
78 int xpos[5], ypos[5];
79 static void onsig();
80 #ifdef TIOCGWINSZ
81 struct winsize ws;
82 #endif
83
84 if (!(term = getenv("TERM"))) {
85 fprintf(stderr, "%s: TERM: parameter not set\n", *argv);
86 exit(1);
87 }
88 if (!(mp = malloc((u_int)1024))) {
89 fprintf(stderr, "%s: out of space.\n", *argv);
90 exit(1);
91 }
92 if (tgetent(mp, term) <= 0) {
93 fprintf(stderr, "%s: %s: unknown terminal type\n", *argv, term);
94 exit(1);
95 }
96 tcp = tcb;
97 if (!(CM = tgetstr("cm", &tcp))) {
98 fprintf(stderr, "%s: terminal not capable of cursor motion\n", *argv);
99 exit(1);
100 }
101 if (!(BC = tgetstr("bc", &tcp)))
102 BC = "\b";
103 if (!(DN = tgetstr("dn", &tcp)))
104 DN = "\n";
105 if (!(ND = tgetstr("nd", &tcp)))
106 ND = " ";
107 #ifdef TIOCGWINSZ
108 if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 &&
109 ws.ws_col && ws.ws_row) {
110 cols = ws.ws_col;
111 lines = ws.ws_row;
112 } else
113 #endif
114 {
115 if ((cols = tgetnum("co")) == -1)
116 cols = 80;
117 if ((lines = tgetnum("li")) == -1)
118 lines = 24;
119 }
120 cols -= 4;
121 lines -= 4;
122 TE = tgetstr("te", &tcp);
123 TI = tgetstr("ti", &tcp);
124 UP = tgetstr("up", &tcp);
125 if (!(LL = tgetstr("ll", &tcp))) {
126 if (!(LL = malloc((u_int)10))) {
127 fprintf(stderr, "%s: out of space.\n", *argv);
128 exit(1);
129 }
130 (void)strcpy(LL, tgoto(CM, 0, 23));
131 }
132 (void)signal(SIGHUP, onsig);
133 (void)signal(SIGINT, onsig);
134 (void)signal(SIGQUIT, onsig);
135 (void)signal(SIGSTOP, onsig);
136 (void)signal(SIGTSTP, onsig);
137 (void)signal(SIGTERM, onsig);
138 tcgetattr(1, &sg);
139 old_tty = sg;
140 ospeed = cfgetospeed(&sg);
141 sg.c_iflag &= ~ICRNL;
142 sg.c_oflag &= ~ONLCR;
143 sg.c_lflag &= ~ECHO;
144 tcsetattr(1, TCSADRAIN, &sg);
145 if (TI)
146 tputs(TI, 1, fputchar);
147 tputs(tgetstr("cl", &tcp), 1, fputchar);
148 (void)fflush(stdout);
149 for (j = 4; j >= 0; --j) {
150 xpos[j] = random() % cols + 2;
151 ypos[j] = random() % lines + 2;
152 }
153 for (j = 0;;) {
154 x = random() % cols + 2;
155 y = random() % lines + 2;
156 cursor(x, y);
157 fputchar('.');
158 cursor(xpos[j], ypos[j]);
159 fputchar('o');
160 if (!j--)
161 j = 4;
162 cursor(xpos[j], ypos[j]);
163 fputchar('O');
164 if (!j--)
165 j = 4;
166 cursor(xpos[j], ypos[j] - 1);
167 fputchar('-');
168 tputs(DN, 1, fputchar);
169 tputs(BC, 1, fputchar);
170 tputs(BC, 1, fputchar);
171 fputs("|.|", stdout);
172 tputs(DN, 1, fputchar);
173 tputs(BC, 1, fputchar);
174 tputs(BC, 1, fputchar);
175 fputchar('-');
176 if (!j--)
177 j = 4;
178 cursor(xpos[j], ypos[j] - 2);
179 fputchar('-');
180 tputs(DN, 1, fputchar);
181 tputs(BC, 1, fputchar);
182 tputs(BC, 1, fputchar);
183 fputs("/ \\", stdout);
184 cursor(xpos[j] - 2, ypos[j]);
185 fputs("| O |", stdout);
186 cursor(xpos[j] - 1, ypos[j] + 1);
187 fputs("\\ /", stdout);
188 tputs(DN, 1, fputchar);
189 tputs(BC, 1, fputchar);
190 tputs(BC, 1, fputchar);
191 fputchar('-');
192 if (!j--)
193 j = 4;
194 cursor(xpos[j], ypos[j] - 2);
195 fputchar(' ');
196 tputs(DN, 1, fputchar);
197 tputs(BC, 1, fputchar);
198 tputs(BC, 1, fputchar);
199 fputchar(' ');
200 tputs(ND, 1, fputchar);
201 fputchar(' ');
202 cursor(xpos[j] - 2, ypos[j]);
203 fputchar(' ');
204 tputs(ND, 1, fputchar);
205 fputchar(' ');
206 tputs(ND, 1, fputchar);
207 fputchar(' ');
208 cursor(xpos[j] - 1, ypos[j] + 1);
209 fputchar(' ');
210 tputs(ND, 1, fputchar);
211 fputchar(' ');
212 tputs(DN, 1, fputchar);
213 tputs(BC, 1, fputchar);
214 tputs(BC, 1, fputchar);
215 fputchar(' ');
216 xpos[j] = x;
217 ypos[j] = y;
218 (void)fflush(stdout);
219 }
220 }
221
222 static void
223 onsig()
224 {
225 tputs(LL, 1, fputchar);
226 if (TE)
227 tputs(TE, 1, fputchar);
228 (void)fflush(stdout);
229 tcsetattr(1, TCSADRAIN, &old_tty);
230 exit(0);
231 }
232
233 void
234 fputchar(c)
235 int c;
236 {
237 (void)putchar(c);
238 }