]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - rain/rain.c
use <termcap.h> instead of <curses.h> for termcap(3) functions
[bsdgames-darwin.git] / rain / rain.c
1 /* $NetBSD: rain.c,v 1.9 1997/10/13 16:13:39 lukem 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: rain.c,v 1.9 1997/10/13 16:13:39 lukem 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 <sys/ioctl.h>
57 #include <err.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <termcap.h>
62 #include <termios.h>
63
64 #define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar)
65
66 static struct termios sg, old_tty;
67
68 void fputchar __P((int));
69 int main __P((int, char **));
70 void onsig __P((int));
71
72
73 char *LL, *TE;
74
75 int
76 main(argc, argv)
77 int argc;
78 char **argv;
79 {
80 extern char *UP;
81 int x, y, j;
82 char *CM, *BC, *DN, *ND, *term;
83 char *TI, *tcp, *mp, tcb[100];
84 long cols, lines;
85 int xpos[5], ypos[5];
86 #ifdef TIOCGWINSZ
87 struct winsize ws;
88 #endif
89
90 if (!(term = getenv("TERM")))
91 errx(1, "TERM: parameter not set");
92 if (!(mp = malloc((u_int)1024)))
93 errx(1, "out of space");
94 if (tgetent(mp, term) <= 0)
95 errx(1, "unknown terminal type `%s'", term);
96 tcp = tcb;
97 if (!(CM = tgetstr("cm", &tcp)))
98 errx(1, "terminal not capable of cursor motion");
99 if (!(BC = tgetstr("bc", &tcp)))
100 BC = "\b";
101 if (!(DN = tgetstr("dn", &tcp)))
102 DN = "\n";
103 if (!(ND = tgetstr("nd", &tcp)))
104 ND = " ";
105 #ifdef TIOCGWINSZ
106 if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 &&
107 ws.ws_col && ws.ws_row) {
108 cols = ws.ws_col;
109 lines = ws.ws_row;
110 } else
111 #endif
112 {
113 if ((cols = tgetnum("co")) == -1)
114 cols = 80;
115 if ((lines = tgetnum("li")) == -1)
116 lines = 24;
117 }
118 cols -= 4;
119 lines -= 4;
120 TE = tgetstr("te", &tcp);
121 TI = tgetstr("ti", &tcp);
122 UP = tgetstr("up", &tcp);
123 if (!(LL = tgetstr("ll", &tcp))) {
124 if (!(LL = malloc((u_int)10))) {
125 fprintf(stderr, "%s: out of space.\n", *argv);
126 exit(1);
127 }
128 (void)strcpy(LL, tgoto(CM, 0, 23));
129 }
130 (void)signal(SIGHUP, onsig);
131 (void)signal(SIGINT, onsig);
132 (void)signal(SIGQUIT, onsig);
133 (void)signal(SIGSTOP, onsig);
134 (void)signal(SIGTSTP, onsig);
135 (void)signal(SIGTERM, onsig);
136 tcgetattr(1, &sg);
137 old_tty = sg;
138 sg.c_iflag &= ~ICRNL;
139 sg.c_oflag &= ~ONLCR;
140 sg.c_lflag &= ~ECHO;
141 tcsetattr(1, TCSADRAIN, &sg);
142 if (TI)
143 tputs(TI, 1, fputchar);
144 tputs(tgetstr("cl", &tcp), 1, fputchar);
145 (void)fflush(stdout);
146 for (j = 4; j >= 0; --j) {
147 xpos[j] = random() % cols + 2;
148 ypos[j] = random() % lines + 2;
149 }
150 for (j = 0;;) {
151 x = random() % cols + 2;
152 y = random() % lines + 2;
153 cursor(x, y);
154 fputchar('.');
155 cursor(xpos[j], ypos[j]);
156 fputchar('o');
157 if (!j--)
158 j = 4;
159 cursor(xpos[j], ypos[j]);
160 fputchar('O');
161 if (!j--)
162 j = 4;
163 cursor(xpos[j], ypos[j] - 1);
164 fputchar('-');
165 tputs(DN, 1, fputchar);
166 tputs(BC, 1, fputchar);
167 tputs(BC, 1, fputchar);
168 fputs("|.|", stdout);
169 tputs(DN, 1, fputchar);
170 tputs(BC, 1, fputchar);
171 tputs(BC, 1, fputchar);
172 fputchar('-');
173 if (!j--)
174 j = 4;
175 cursor(xpos[j], ypos[j] - 2);
176 fputchar('-');
177 tputs(DN, 1, fputchar);
178 tputs(BC, 1, fputchar);
179 tputs(BC, 1, fputchar);
180 fputs("/ \\", stdout);
181 cursor(xpos[j] - 2, ypos[j]);
182 fputs("| O |", stdout);
183 cursor(xpos[j] - 1, ypos[j] + 1);
184 fputs("\\ /", stdout);
185 tputs(DN, 1, fputchar);
186 tputs(BC, 1, fputchar);
187 tputs(BC, 1, fputchar);
188 fputchar('-');
189 if (!j--)
190 j = 4;
191 cursor(xpos[j], ypos[j] - 2);
192 fputchar(' ');
193 tputs(DN, 1, fputchar);
194 tputs(BC, 1, fputchar);
195 tputs(BC, 1, fputchar);
196 fputchar(' ');
197 tputs(ND, 1, fputchar);
198 fputchar(' ');
199 cursor(xpos[j] - 2, ypos[j]);
200 fputchar(' ');
201 tputs(ND, 1, fputchar);
202 fputchar(' ');
203 tputs(ND, 1, fputchar);
204 fputchar(' ');
205 cursor(xpos[j] - 1, ypos[j] + 1);
206 fputchar(' ');
207 tputs(ND, 1, fputchar);
208 fputchar(' ');
209 tputs(DN, 1, fputchar);
210 tputs(BC, 1, fputchar);
211 tputs(BC, 1, fputchar);
212 fputchar(' ');
213 xpos[j] = x;
214 ypos[j] = y;
215 (void)fflush(stdout);
216 }
217 }
218
219 void
220 onsig(dummy)
221 int dummy;
222 {
223 tputs(LL, 1, fputchar);
224 if (TE)
225 tputs(TE, 1, fputchar);
226 (void)fflush(stdout);
227 tcsetattr(1, TCSADRAIN, &old_tty);
228 exit(0);
229 }
230
231 void
232 fputchar(c)
233 int c;
234 {
235 (void)putchar(c);
236 }