1 /* $OpenBSD: ppt.c,v 1.12 2014/11/07 22:17:49 schwarze 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 [string ...]\n", __progname); 53 fprintf(stderr, "usage: %s -d [-b]\n", __progname); 54 exit(1); 55 } 56 57 int 58 main(int argc, char **argv) 59 { 60 char *p, buf[132]; 61 int c, start, seenl, dflag, bflag; 62 63 dflag = bflag = 0; 64 while ((c = getopt(argc, argv, "bdh")) != -1) 65 switch(c) { 66 case 'd': 67 dflag = 1; 68 break; 69 case 'b': 70 bflag = 1; 71 break; 72 case '?': case 'h': 73 default: 74 usage(); 75 } 76 if (bflag && !dflag) 77 usage(); 78 argc -= optind; 79 argv += optind; 80 81 if (dflag) { 82 if (argc > 0) 83 usage(); 84 85 seenl = start = 0; 86 while (fgets(buf, sizeof(buf), stdin) != NULL) { 87 c = getppt(buf); 88 if (c == -2) 89 continue; 90 if (c == -1) { 91 if (start) 92 /* lost sync */ 93 putchar('x'); 94 continue; 95 } 96 start = 1; 97 if (bflag) 98 putchar(c); 99 else { 100 char vbuf[5]; 101 vis(vbuf, c, VIS_NOSLASH, 0); 102 fputs(vbuf, stdout); 103 } 104 seenl = (c == '\n'); 105 } 106 if (!feof(stdin)) 107 err(1, "fgets"); 108 if (!seenl && !bflag) 109 putchar('\n'); 110 } else { 111 (void) puts(EDGE); 112 if (argc > 0) 113 while ((p = *argv++)) { 114 for (; *p; ++p) 115 putppt((int)*p); 116 if (*argv) 117 putppt((int)' '); 118 } 119 else while ((c = getchar()) != EOF) 120 putppt(c); 121 (void) puts(EDGE); 122 } 123 exit(0); 124 } 125 126 void 127 putppt(int c) 128 { 129 int i; 130 131 (void) putchar('|'); 132 for (i = 7; i >= 0; i--) { 133 if (i == 2) 134 (void) putchar('.'); /* feed hole */ 135 if ((c&(1<<i)) != 0) 136 (void) putchar('o'); 137 else 138 (void) putchar(' '); 139 } 140 (void) putchar('|'); 141 (void) putchar('\n'); 142 } 143 144 int 145 getppt(const char *buf) 146 { 147 int c; 148 149 /* Demand left-aligned paper tape, but allow comments to the right */ 150 if (strncmp(buf, EDGE, strlen(EDGE)) == 0) 151 return (-2); 152 if (strlen(buf) < 12 || buf[0] != '|' || buf[10] != '|' || 153 buf[6] != '.' || strspn(buf, "| o.") < 11) 154 return (-1); 155 156 c = 0; 157 if (buf[1] != ' ') 158 c |= 0200; 159 if (buf[2] != ' ') 160 c |= 0100; 161 if (buf[3] != ' ') 162 c |= 0040; 163 if (buf[4] != ' ') 164 c |= 0020; 165 if (buf[5] != ' ') 166 c |= 0010; 167 if (buf[7] != ' ') 168 c |= 0004; 169 if (buf[8] != ' ') 170 c |= 0002; 171 if (buf[9] != ' ') 172 c |= 0001; 173 174 return (c); 175 } 176