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