xref: /openbsd/games/ppt/ppt.c (revision 3d8817e4)
1 /*	$OpenBSD: ppt.c,v 1.11 2009/10/27 23:59:26 deraadt Exp $	*/
2 /*	$NetBSD: ppt.c,v 1.4 1995/03/23 08:35:40 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <err.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <vis.h>
40 
41 
42 #define	EDGE	"___________"
43 
44 void	usage(void);
45 void	putppt(int);
46 int	getppt(const char *buf);
47 
48 void
49 usage(void)
50 {
51 	extern char *__progname;
52 	fprintf(stderr, "usage: %s [-d [-b] | string ...]\n", __progname);
53 	exit(1);
54 }
55 
56 int
57 main(int argc, char **argv)
58 {
59 	char *p, buf[132];
60 	int c, start, seenl, dflag, bflag;
61 
62 	dflag = bflag = 0;
63 	while ((c = getopt(argc, argv, "bdh")) != -1)
64 		switch(c) {
65 		case 'd':
66 			dflag = 1;
67 			break;
68 		case 'b':
69 			bflag = 1;
70 			break;
71 		case '?': case 'h':
72 		default:
73 			usage();
74 		}
75 	if (bflag && !dflag)
76 		usage();
77 	argc -= optind;
78 	argv += optind;
79 
80 	if (dflag) {
81 		if (argc > 0)
82 			usage();
83 
84 		seenl = start = 0;
85 		while (fgets(buf, sizeof(buf), stdin) != NULL) {
86 			c = getppt(buf);
87 			if (c == -2)
88 				continue;
89 			if (c == -1) {
90 				if (start)
91 					/* lost sync */
92 					putchar('x');
93 				continue;
94 			}
95 			start = 1;
96 			if (bflag)
97 				putchar(c);
98 			else {
99 				char vbuf[5];
100 				vis(vbuf, c, VIS_NOSLASH, 0);
101 				fputs(vbuf, stdout);
102 			}
103 			seenl = (c == '\n');
104 		}
105 		if (!feof(stdin))
106 			err(1, "fgets");
107 		if (!seenl && !bflag)
108 			putchar('\n');
109 	} else {
110 		(void) puts(EDGE);
111 		if (argc > 0)
112 			while ((p = *argv++)) {
113 				for (; *p; ++p)
114 					putppt((int)*p);
115 				if (*argv)
116 					putppt((int)' ');
117 			}
118 		else while ((c = getchar()) != EOF)
119 			putppt(c);
120 		(void) puts(EDGE);
121 	}
122 	exit(0);
123 }
124 
125 void
126 putppt(int c)
127 {
128 	int i;
129 
130 	(void) putchar('|');
131 	for (i = 7; i >= 0; i--) {
132 		if (i == 2)
133 			(void) putchar('.');	/* feed hole */
134 		if ((c&(1<<i)) != 0)
135 			(void) putchar('o');
136 		else
137 			(void) putchar(' ');
138 	}
139 	(void) putchar('|');
140 	(void) putchar('\n');
141 }
142 
143 int
144 getppt(const char *buf)
145 {
146 	int c;
147 
148 	/* Demand left-aligned paper tape, but allow comments to the right */
149 	if (strncmp(buf, EDGE, strlen(EDGE)) == 0)
150 	    return (-2);
151 	if (strlen(buf) < 12 || buf[0] != '|' || buf[10] != '|' ||
152 	    buf[6] != '.' || strspn(buf, "| o.") < 11)
153 		return (-1);
154 
155 	c = 0;
156 	if (buf[1] != ' ')
157 		c |= 0200;
158 	if (buf[2] != ' ')
159 		c |= 0100;
160 	if (buf[3] != ' ')
161 		c |= 0040;
162 	if (buf[4] != ' ')
163 		c |= 0020;
164 	if (buf[5] != ' ')
165 		c |= 0010;
166 	if (buf[7] != ' ')
167 		c |= 0004;
168 	if (buf[8] != ' ')
169 		c |= 0002;
170 	if (buf[9] != ' ')
171 		c |= 0001;
172 
173 	return (c);
174 }
175