xref: /csrg-svn/games/rain/rain.c (revision 42588)
121200Sdist /*
221200Sdist  * Copyright (c) 1980 Regents of the University of California.
333199Sbostic  * All rights reserved.
433199Sbostic  *
5*42588Sbostic  * %sccs.include.redist.c%
621200Sdist  */
78869Smckusick 
821200Sdist #ifndef lint
921200Sdist char copyright[] =
1021200Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1121200Sdist  All rights reserved.\n";
1233199Sbostic #endif /* not lint */
138869Smckusick 
1421200Sdist #ifndef lint
15*42588Sbostic static char sccsid[] = "@(#)rain.c	5.5 (Berkeley) 06/01/90";
1633199Sbostic #endif /* not lint */
1721200Sdist 
1833199Sbostic /*
1933199Sbostic  * rain 11/3/1980 EPS/CITHEP
2033199Sbostic  * cc rain.c -o rain -O -ltermlib
2133199Sbostic  */
2223869Smckusick 
2333199Sbostic #include <sys/types.h>
248869Smckusick #include <stdio.h>
2523869Smckusick #ifdef USG
2623869Smckusick #include <termio.h>
2723869Smckusick #else
288869Smckusick #include <sgtty.h>
2923869Smckusick #endif
308869Smckusick #include <signal.h>
3133199Sbostic 
3233199Sbostic #define	cursor(c, r)	tputs(tgoto(CM, c, r), 1, fputchar)
3333199Sbostic 
3423869Smckusick #ifdef USG
3533199Sbostic static struct termio sg, old_tty;
3623869Smckusick #else
3733199Sbostic static struct sgttyb sg, old_tty;
3823869Smckusick #endif
3933199Sbostic 
4033199Sbostic int	fputchar();
4133199Sbostic char	*LL, *TE, *tgoto();
4233199Sbostic 
4333199Sbostic main(argc, argv)
4433199Sbostic 	int argc;
4533199Sbostic 	char **argv;
468869Smckusick {
4733199Sbostic 	extern short ospeed;
4833199Sbostic 	extern char *UP;
4933199Sbostic 	register int x, y, j;
5033199Sbostic 	register char *CM, *BC, *DN, *ND, *term;
5133199Sbostic 	char *TI, *tcp, *mp, tcb[100],
5233199Sbostic 		*malloc(), *getenv(), *strcpy(), *tgetstr();
5333199Sbostic 	long cols, lines, random();
5433199Sbostic 	int xpos[5], ypos[5], onsig();
5523869Smckusick 
5633199Sbostic 	if (!(term = getenv("TERM"))) {
5733199Sbostic 		fprintf(stderr, "%s: TERM: parameter not set\n", *argv);
5833199Sbostic 		exit(1);
5933199Sbostic 	}
6033199Sbostic 	if (!(mp = malloc((u_int)1024))) {
6133199Sbostic 		fprintf(stderr, "%s: out of space.\n", *argv);
6233199Sbostic 		exit(1);
6333199Sbostic 	}
6433199Sbostic 	if (tgetent(mp, term) <= 0) {
6533199Sbostic 		fprintf(stderr, "%s: %s: unknown terminal type\n", *argv, term);
6633199Sbostic 		exit(1);
6733199Sbostic 	}
6833199Sbostic 	tcp = tcb;
6933199Sbostic 	if (!(CM = tgetstr("cm", &tcp))) {
7033199Sbostic 		fprintf(stderr, "%s: terminal not capable of cursor motion\n", *argv);
7133199Sbostic 		exit(1);
7233199Sbostic 	}
7333199Sbostic 	if (!(BC = tgetstr("bc", &tcp)))
7433199Sbostic 		BC = "\b";
7533199Sbostic 	if (!(DN = tgetstr("dn", &tcp)))
7633199Sbostic 		DN = "\n";
7733199Sbostic 	if (!(ND = tgetstr("nd", &tcp)))
7833199Sbostic 		ND = " ";
7933199Sbostic 	if ((cols = tgetnum("co")) == -1)
8033199Sbostic 		cols = 80;
8133199Sbostic 	if ((lines = tgetnum("li")) == -1)
8233199Sbostic 		lines = 24;
8333199Sbostic 	cols -= 4;
8433199Sbostic 	lines -= 4;
8533199Sbostic 	TE = tgetstr("te", &tcp);
8633199Sbostic 	TI = tgetstr("ti", &tcp);
8733199Sbostic 	UP = tgetstr("up", &tcp);
8833199Sbostic 	if (!(LL = tgetstr("ll", &tcp))) {
8933199Sbostic 		if (!(LL = malloc((u_int)10))) {
9033199Sbostic 			fprintf(stderr, "%s: out of space.\n", *argv);
9133199Sbostic 			exit(1);
9233199Sbostic 		}
9333199Sbostic 		(void)strcpy(LL, tgoto(CM, 0, 23));
9433199Sbostic 	}
9523869Smckusick #ifdef USG
9633199Sbostic 	ioctl(1, TCGETA, &sg);
9733199Sbostic 	ospeed = sg.c_cflag&CBAUD;
9823869Smckusick #else
9933199Sbostic 	gtty(1, &sg);
10033199Sbostic 	ospeed = sg.sg_ospeed;
10123869Smckusick #endif
10233199Sbostic 	(void)signal(SIGHUP, onsig);
10333199Sbostic 	(void)signal(SIGINT, onsig);
10433199Sbostic 	(void)signal(SIGQUIT, onsig);
10533199Sbostic 	(void)signal(SIGSTOP, onsig);
10633199Sbostic 	(void)signal(SIGTSTP, onsig);
10733199Sbostic 	(void)signal(SIGTERM, onsig);
10823869Smckusick #ifdef USG
10933199Sbostic 	ioctl(1, TCGETA, &old_tty);	/* save tty bits for exit */
11033199Sbostic 	ioctl(1, TCGETA, &sg);
11133199Sbostic 	sg.c_iflag &= ~ICRNL;
11233199Sbostic 	sg.c_oflag &= ~ONLCR;
11333199Sbostic 	sg.c_lflag &= ~ECHO;
11433199Sbostic 	ioctl(1, TCSETAW, &sg);
11523869Smckusick #else
11633199Sbostic 	gtty(1, &old_tty);		/* save tty bits for exit */
11733199Sbostic 	gtty(1, &sg);
11833199Sbostic 	sg.sg_flags &= ~(CRMOD|ECHO);
11933199Sbostic 	stty(1, &sg);
12023869Smckusick #endif
12133199Sbostic 	if (TI)
12233199Sbostic 		tputs(TI, 1, fputchar);
12333199Sbostic 	tputs(tgetstr("cl", &tcp), 1, fputchar);
12433199Sbostic 	(void)fflush(stdout);
12533199Sbostic 	for (j = 4; j >= 0; --j) {
12633199Sbostic 		xpos[j] = random() % cols + 2;
12733199Sbostic 		ypos[j] = random() % lines + 2;
12833199Sbostic 	}
12933199Sbostic 	for (j = 0;;) {
13033199Sbostic 		x = random() % cols + 2;
13133199Sbostic 		y = random() % lines + 2;
13233199Sbostic 		cursor(x, y);
13333199Sbostic 		fputchar('.');
13433199Sbostic 		cursor(xpos[j], ypos[j]);
13533199Sbostic 		fputchar('o');
13633199Sbostic 		if (!j--)
13733199Sbostic 			j = 4;
13833199Sbostic 		cursor(xpos[j], ypos[j]);
13933199Sbostic 		fputchar('O');
14033199Sbostic 		if (!j--)
14133199Sbostic 			j = 4;
14233199Sbostic 		cursor(xpos[j], ypos[j] - 1);
14333199Sbostic 		fputchar('-');
14433199Sbostic 		tputs(DN, 1, fputchar);
14533199Sbostic 		tputs(BC, 1, fputchar);
14633199Sbostic 		tputs(BC, 1, fputchar);
14733199Sbostic 		fputs("|.|", stdout);
14833199Sbostic 		tputs(DN, 1, fputchar);
14933199Sbostic 		tputs(BC, 1, fputchar);
15033199Sbostic 		tputs(BC, 1, fputchar);
15133199Sbostic 		fputchar('-');
15233199Sbostic 		if (!j--)
15333199Sbostic 			j = 4;
15433199Sbostic 		cursor(xpos[j], ypos[j] - 2);
15533199Sbostic 		fputchar('-');
15633199Sbostic 		tputs(DN, 1, fputchar);
15733199Sbostic 		tputs(BC, 1, fputchar);
15833199Sbostic 		tputs(BC, 1, fputchar);
15933199Sbostic 		fputs("/ \\", stdout);
16033199Sbostic 		cursor(xpos[j] - 2, ypos[j]);
16133199Sbostic 		fputs("| O |", stdout);
16233199Sbostic 		cursor(xpos[j] - 1, ypos[j] + 1);
16333199Sbostic 		fputs("\\ /", stdout);
16433199Sbostic 		tputs(DN, 1, fputchar);
16533199Sbostic 		tputs(BC, 1, fputchar);
16633199Sbostic 		tputs(BC, 1, fputchar);
16733199Sbostic 		fputchar('-');
16833199Sbostic 		if (!j--)
16933199Sbostic 			j = 4;
17033199Sbostic 		cursor(xpos[j], ypos[j] - 2);
17133199Sbostic 		fputchar(' ');
17233199Sbostic 		tputs(DN, 1, fputchar);
17333199Sbostic 		tputs(BC, 1, fputchar);
17433199Sbostic 		tputs(BC, 1, fputchar);
17533199Sbostic 		fputchar(' ');
17633199Sbostic 		tputs(ND, 1, fputchar);
17733199Sbostic 		fputchar(' ');
17833199Sbostic 		cursor(xpos[j] - 2, ypos[j]);
17933199Sbostic 		fputchar(' ');
18033199Sbostic 		tputs(ND, 1, fputchar);
18133199Sbostic 		fputchar(' ');
18233199Sbostic 		tputs(ND, 1, fputchar);
18333199Sbostic 		fputchar(' ');
18433199Sbostic 		cursor(xpos[j] - 1, ypos[j] + 1);
18533199Sbostic 		fputchar(' ');
18633199Sbostic 		tputs(ND, 1, fputchar);
18733199Sbostic 		fputchar(' ');
18833199Sbostic 		tputs(DN, 1, fputchar);
18933199Sbostic 		tputs(BC, 1, fputchar);
19033199Sbostic 		tputs(BC, 1, fputchar);
19133199Sbostic 		fputchar(' ');
19233199Sbostic 		xpos[j] = x;
19333199Sbostic 		ypos[j] = y;
19433199Sbostic 		(void)fflush(stdout);
19533199Sbostic 	}
1968869Smckusick }
19733199Sbostic 
19833199Sbostic static
19933199Sbostic onsig()
2008869Smckusick {
20133199Sbostic 	tputs(LL, 1, fputchar);
20233199Sbostic 	if (TE)
20333199Sbostic 		tputs(TE, 1, fputchar);
20433199Sbostic 	(void)fflush(stdout);
20523869Smckusick #ifdef USG
20633199Sbostic 	ioctl(1, TCSETAW, &old_tty);
20723869Smckusick #else
20833199Sbostic 	stty(1, &old_tty);
20923869Smckusick #endif
21033199Sbostic 	exit(0);
2118869Smckusick }
21233199Sbostic 
21333199Sbostic static
2148869Smckusick fputchar(c)
21533199Sbostic 	char c;
2168869Smckusick {
21733199Sbostic 	putchar(c);
2188869Smckusick }
219