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