xref: /openbsd-src/usr.bin/banner/banner.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: banner.c,v 1.9 2009/10/27 23:59:35 deraadt Exp $	*/
2 /*	$NetBSD: banner.c,v 1.2 1995/04/09 06:00:15 cgd Exp $	*/
3 
4 /*
5  *	Changes for banner(1)
6  *
7  *      @(#)Copyright (c) 1995, Simon J. Gerraty.
8  *
9  *      This is free software.  It comes with NO WARRANTY.
10  *      Permission to use, modify and distribute this source code
11  *      is granted subject to the following conditions.
12  *      1/ that the above copyright notice and this notice
13  *      are preserved in all copies and that due credit be given
14  *      to the author.
15  *      2/ that any changes to this code are clearly commented
16  *      as such so that the author does not get blamed for bugs
17  *      other than his own.
18  *
19  *      Please send copies of changes and bug-fixes to:
20  *      sjg@zen.void.oz.au
21  */
22 
23 /*
24  * Copyright (c) 1983, 1993
25  *	The Regents of the University of California.  All rights reserved.
26  *
27  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions
29  * are met:
30  * 1. Redistributions of source code must retain the above copyright
31  *    notice, this list of conditions and the following disclaimer.
32  * 2. Redistributions in binary form must reproduce the above copyright
33  *    notice, this list of conditions and the following disclaimer in the
34  *    documentation and/or other materials provided with the distribution.
35  * 3. Neither the name of the University nor the names of its contributors
36  *    may be used to endorse or promote products derived from this software
37  *    without specific prior written permission.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
40  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
43  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  */
51 
52 #include <stdio.h>
53 #include <unistd.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include "banner.h"
58 
59 static long PW = LINELEN;
60 
61 /* the char gen code below is lifted from lpd */
62 
63 static char *
64 scnline(int key, char *p, int c)
65 {
66 	int scnwidth;
67 
68 	/*
69 	 * <sjg> lpd makes chars out of the letter in question.
70 	 * the results are somewhat mixed.  Sticking to '#' as
71 	 * banner(1) does is more consistent.
72 	 */
73 #ifndef NOHASH_ONLY
74 	c = '#';
75 #endif
76 
77 	for (scnwidth = WIDTH; --scnwidth;) {
78 		key <<= 1;
79 		*p++ = key & 0200 ? c : BACKGND;
80 	}
81 	return (p);
82 }
83 
84 #define TRC(q)	(((q)-' ')&0177)
85 
86 
87 static int
88 dropit(int c)
89 {
90 	switch(c) {
91 
92 	case TRC('_'):
93 	case TRC(';'):
94 	case TRC(','):
95 	case TRC('g'):
96 	case TRC('j'):
97 	case TRC('p'):
98 	case TRC('q'):
99 	case TRC('y'):
100 		return (DROP);
101 
102 	default:
103 		return (0);
104 	}
105 }
106 
107 static void
108 scan_out(int scfd, char *scsp, int dlm)
109 {
110 	char *strp;
111 	int nchrs, j;
112 	char outbuf[LINELEN+1], *sp, c, cc;
113 	int d, scnhgt;
114 	extern char scnkey[][HEIGHT];	/* in lpdchar.c */
115 
116 	for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
117 		strp = &outbuf[0];
118 		sp = scsp;
119 		for (nchrs = 0; *sp != dlm && *sp != '\0'; ) {
120 			cc = *sp++;
121 			if ((unsigned char)cc < ' ' ||
122 			    (unsigned char)cc > 0x7f)
123 				cc = INVALID;
124 
125 			c = TRC(cc);
126 			d = dropit(c);
127 			if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
128 				for (j = WIDTH; --j;)
129 					*strp++ = BACKGND;
130 			else
131 				strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc);
132 			if (nchrs++ >= PW/(WIDTH+1)-1)
133 				break;
134 			*strp++ = BACKGND;
135 #ifdef LPD_CHSET				/* <sjg> */
136 			*strp++ = BACKGND;
137 #endif
138 		}
139 		while (*--strp == BACKGND && strp >= outbuf)
140 			;
141 		strp++;
142 		*strp++ = '\n';
143 		(void) write(scfd, outbuf, strp-outbuf);
144 	}
145 }
146 
147 /*
148  * for each word, print up to 10 chars in big letters.
149  */
150 int
151 main(int argc, char *argv[])
152 {
153 	char word[10+1];			/* strings limited to 10 chars */
154 
155 	while (*++argv) {
156 		(void)strlcpy(word, *argv, sizeof (word));
157 		scan_out(1, word, '\0');
158 	}
159 	exit(0);
160 }
161