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