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