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