xref: /netbsd-src/games/ppt/ppt.c (revision 4511a017c166dba311b5566fe7dbf6e2a22914fc)
1*4511a017Sjoerg /*	$NetBSD: ppt.c,v 1.19 2011/08/29 20:30:37 joerg Exp $	*/
242fb1b9dScgd 
361f28255Scgd /*
442fb1b9dScgd  * Copyright (c) 1988, 1993
542fb1b9dScgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
325ab1a671Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
342fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
352fe2731dSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
3942fb1b9dScgd #if 0
4042fb1b9dScgd static char sccsid[] = "@(#)ppt.c	8.1 (Berkeley) 5/31/93";
4142fb1b9dScgd #else
42*4511a017Sjoerg __RCSID("$NetBSD: ppt.c,v 1.19 2011/08/29 20:30:37 joerg Exp $");
4342fb1b9dScgd #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
46291eba38Skim #include <err.h>
4761f28255Scgd #include <stdio.h>
4832330650Smatt #include <stdlib.h>
4914498367Skim #include <string.h>
505367f340Sjsm #include <unistd.h>
5161f28255Scgd 
5214498367Skim #define	EDGE	"___________"
5314498367Skim 
54cb5fd834Sjsm static void putppt(int);
555305281bSdholland static int getppt(const char *);
5614498367Skim 
57*4511a017Sjoerg __dead static void
usage(void)5814498367Skim usage(void)
5914498367Skim {
6014498367Skim 	extern char *__progname;
6114498367Skim 	fprintf(stderr, "usage: %s [-d] [string ...]\n", __progname);
6214498367Skim 	exit(1);
6314498367Skim }
6461f28255Scgd 
655ab1a671Slukem int
main(int argc,char ** argv)665305281bSdholland main(int argc, char **argv)
6761f28255Scgd {
6814498367Skim 	char *p, buf[132];
694ed80a29Satatat 	int c, start, neednl, dflag;
7061f28255Scgd 
715367f340Sjsm 	/* Revoke setgid privileges */
72f9eca697Smycroft 	setgid(getgid());
735367f340Sjsm 
741de00516Skim 	dflag = 0;
7514498367Skim 	while ((c = getopt(argc, argv, "dh")) != -1)
7614498367Skim 		switch(c) {
7714498367Skim 		case 'd':
7814498367Skim 			dflag = 1;
7914498367Skim 			break;
801de00516Skim 		case 'h':
811de00516Skim 		case '?':
8214498367Skim 		default:
8314498367Skim 			usage();
8414498367Skim 		}
8514498367Skim 	argc -= optind;
8614498367Skim 	argv += optind;
8714498367Skim 
8814498367Skim 	if (dflag) {
8914498367Skim 		if (argc > 0)
9014498367Skim 			usage();
9114498367Skim 
9214498367Skim 		start = 0;
934ed80a29Satatat 		neednl = 0;
9414498367Skim 		while (fgets(buf, sizeof(buf), stdin) != NULL) {
9514498367Skim 			c = getppt(buf);
9614498367Skim 			if (c < 0) {
9714498367Skim 				if (start) {
984ed80a29Satatat 					/* lost sync? */
994ed80a29Satatat 					if (neednl)
10014498367Skim 						putchar('\n');
10114498367Skim 					exit(0);
10214498367Skim 				} else
10314498367Skim 					continue;
10414498367Skim 			}
10514498367Skim 			start = 1;
10614498367Skim 			putchar(c);
1074ed80a29Satatat 			neednl = (c != '\n');
10814498367Skim 		}
10914498367Skim 		if (!feof(stdin))
11014498367Skim 			err(1, "fgets");
1114ed80a29Satatat 		if (neednl)
11214498367Skim 			putchar('\n');
11314498367Skim 	} else {
11414498367Skim 		(void) puts(EDGE);
1154ed80a29Satatat 		if (argc > 0)
11642bac90aSkim 			while ((p = *argv++)) {
11761f28255Scgd 				for (; *p; ++p)
11861f28255Scgd 					putppt((int)*p);
11942bac90aSkim 				if ((*(argv)))
12094964991Sjsm 					putppt((int)' ');
12194964991Sjsm 			}
12261f28255Scgd 		else while ((c = getchar()) != EOF)
12361f28255Scgd 			putppt(c);
12414498367Skim 		(void) puts(EDGE);
12514498367Skim 	}
12661f28255Scgd 	exit(0);
12761f28255Scgd }
12861f28255Scgd 
12961f28255Scgd static void
putppt(int c)130*4511a017Sjoerg putppt(int c)
13161f28255Scgd {
1325ab1a671Slukem 	int i;
13361f28255Scgd 
13461f28255Scgd 	(void) putchar('|');
13561f28255Scgd 	for (i = 7; i >= 0; i--) {
13661f28255Scgd 		if (i == 2)
13761f28255Scgd 			(void) putchar('.');	/* feed hole */
13861f28255Scgd 		if ((c&(1<<i)) != 0)
13961f28255Scgd 			(void) putchar('o');
14061f28255Scgd 		else
14161f28255Scgd 			(void) putchar(' ');
14261f28255Scgd 	}
14361f28255Scgd 	(void) putchar('|');
14461f28255Scgd 	(void) putchar('\n');
14561f28255Scgd }
14614498367Skim 
1475305281bSdholland static int
getppt(const char * buf)14814498367Skim getppt(const char *buf)
14914498367Skim {
15014498367Skim 	const char *p = strchr(buf, '.');
15114498367Skim 	int c;
15214498367Skim 
15314498367Skim 	if (p == NULL)
15414498367Skim 		return (-1);
15514498367Skim 
15614498367Skim 	c = 0;
15714498367Skim 	if (p[ 3] != ' ')
15814498367Skim 		c |= 0001;
15914498367Skim 	if (p[ 2] != ' ')
16014498367Skim 		c |= 0002;
16114498367Skim 	if (p[ 1] != ' ')
16214498367Skim 		c |= 0004;
16314498367Skim 	if (p[-1] != ' ')
16414498367Skim 		c |= 0010;
16514498367Skim 	if (p[-2] != ' ')
16614498367Skim 		c |= 0020;
16714498367Skim 	if (p[-3] != ' ')
16814498367Skim 		c |= 0040;
16914498367Skim 	if (p[-4] != ' ')
17014498367Skim 		c |= 0100;
17114498367Skim 	if (p[-5] != ' ')
17214498367Skim 		c |= 0200;
17314498367Skim 
17414498367Skim 	return (c);
17514498367Skim }
176