1 /* $OpenBSD: ppt.c,v 1.10 2003/07/10 00:03:01 david 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 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1988, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static const char sccsid[] = "@(#)ppt.c 8.1 (Berkeley) 5/31/93"; 42 #else 43 static const char rcsid[] = "$OpenBSD: ppt.c,v 1.10 2003/07/10 00:03:01 david Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 #include <sys/types.h> 48 #include <err.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 #include <vis.h> 54 55 56 #define EDGE "___________" 57 58 void usage(void); 59 void putppt(int); 60 int getppt(const char *buf); 61 62 void 63 usage(void) 64 { 65 extern char *__progname; 66 fprintf(stderr, "usage: %s [-d [-b] | string ...]\n", __progname); 67 exit(1); 68 } 69 70 int 71 main(int argc, char **argv) 72 { 73 char *p, buf[132]; 74 int c, start, seenl, dflag, bflag; 75 76 dflag = bflag = 0; 77 while ((c = getopt(argc, argv, "bdh")) != -1) 78 switch(c) { 79 case 'd': 80 dflag = 1; 81 break; 82 case 'b': 83 bflag = 1; 84 break; 85 case '?': case 'h': 86 default: 87 usage(); 88 } 89 if (bflag && !dflag) 90 usage(); 91 argc -= optind; 92 argv += optind; 93 94 if (dflag) { 95 if (argc > 0) 96 usage(); 97 98 seenl = start = 0; 99 while (fgets(buf, sizeof(buf), stdin) != NULL) { 100 c = getppt(buf); 101 if (c == -2) 102 continue; 103 if (c == -1) { 104 if (start) 105 /* lost sync */ 106 putchar('x'); 107 continue; 108 } 109 start = 1; 110 if (bflag) 111 putchar(c); 112 else { 113 char vbuf[5]; 114 vis(vbuf, c, VIS_NOSLASH, 0); 115 fputs(vbuf, stdout); 116 } 117 seenl = (c == '\n'); 118 } 119 if (!feof(stdin)) 120 err(1, "fgets"); 121 if (!seenl && !bflag) 122 putchar('\n'); 123 } else { 124 (void) puts(EDGE); 125 if (argc > 0) 126 while ((p = *argv++)) { 127 for (; *p; ++p) 128 putppt((int)*p); 129 if (*argv) 130 putppt((int)' '); 131 } 132 else while ((c = getchar()) != EOF) 133 putppt(c); 134 (void) puts(EDGE); 135 } 136 exit(0); 137 } 138 139 void 140 putppt(int c) 141 { 142 int i; 143 144 (void) putchar('|'); 145 for (i = 7; i >= 0; i--) { 146 if (i == 2) 147 (void) putchar('.'); /* feed hole */ 148 if ((c&(1<<i)) != 0) 149 (void) putchar('o'); 150 else 151 (void) putchar(' '); 152 } 153 (void) putchar('|'); 154 (void) putchar('\n'); 155 } 156 157 int 158 getppt(const char *buf) 159 { 160 int c; 161 162 /* Demand left-aligned paper tape, but allow comments to the right */ 163 if (strncmp(buf, EDGE, strlen(EDGE)) == 0) 164 return (-2); 165 if (strlen(buf) < 12 || buf[0] != '|' || buf[10] != '|' || 166 buf[6] != '.' || strspn(buf, "| o.") < 11) 167 return (-1); 168 169 c = 0; 170 if (buf[1] != ' ') 171 c |= 0200; 172 if (buf[2] != ' ') 173 c |= 0100; 174 if (buf[3] != ' ') 175 c |= 0040; 176 if (buf[4] != ' ') 177 c |= 0020; 178 if (buf[5] != ' ') 179 c |= 0010; 180 if (buf[7] != ' ') 181 c |= 0004; 182 if (buf[8] != ' ') 183 c |= 0002; 184 if (buf[9] != ' ') 185 c |= 0001; 186 187 return (c); 188 } 189