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