xref: /netbsd/usr.bin/banner/banner.c (revision bf9ec67e)
1 /*	$NetBSD: banner.c,v 1.7 2001/02/20 23:51:59 cgd 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. All advertising materials mentioning features or use of this software
35  *    must display the following acknowledgement:
36  *	This product includes software developed by the University of
37  *	California, Berkeley and its contributors.
38  * 4. Neither the name of the University nor the names of its contributors
39  *    may be used to endorse or promote products derived from this software
40  *    without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  */
54 
55 #include <sys/cdefs.h>
56 #ifndef lint
57 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
58 	The Regents of the University of California.  All rights reserved.\n");
59 #endif /* not lint */
60 
61 #ifndef lint
62 #if 0
63 static char sccsid[] = "@(#)printjob.c	8.2 (Berkeley) 4/16/94";
64 #else
65 __RCSID("$NetBSD: banner.c,v 1.7 2001/02/20 23:51:59 cgd Exp $");
66 #endif
67 #endif /* not lint */
68 
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <unistd.h>
73 
74 #include "banner.h"
75 
76 static long PW = LINELEN;
77 /*
78  * <sjg> lpd makes chars out of the letter in question.
79  * the results are somewhat mixed.  Sticking to '#' as
80  * banner(1) does is more consistent.
81  */
82 static int ForeGnd = '#';
83 static int BackGnd = ' ';
84 static int Drop = 0;				/* 3 for the LPD font */
85 
86 static	int	dropit (int);
87 static	void	scan_out (int, char *, int);
88 static	char   *scnline (int, char *, int);
89 static	void	usage(void);
90 
91 /* the char gen code below is lifted from lpd */
92 
93 static char *
94 scnline(int key, char *p, int c)
95 {
96 	int scnwidth;
97 
98 	if (ForeGnd)
99 	    c = ForeGnd;
100 
101 	for (scnwidth = WIDTH; --scnwidth;) {
102 		key <<= 1;
103 		*p++ = key & 0200 ? c : BackGnd;
104 	}
105 	return (p);
106 }
107 
108 #define TRC(q)	(((q)-' ')&0177)
109 
110 
111 static int
112 dropit(int c)
113 {
114 	switch(c) {
115 
116 	case TRC('_'):
117 	case TRC(';'):
118 	case TRC(','):
119 	case TRC('g'):
120 	case TRC('j'):
121 	case TRC('p'):
122 	case TRC('q'):
123 	case TRC('y'):
124 		return (Drop);
125 
126 	default:
127 		return (0);
128 	}
129 }
130 
131 static void
132 scan_out(int scfd, char *scsp, int dlm)
133 {
134 	char *strp;
135 	int nchrs, j;
136 	char outbuf[LINELEN+1], *sp, c, cc;
137 	int d, scnhgt;
138 
139 	for (scnhgt = 0; scnhgt++ < HEIGHT+Drop; ) {
140 		strp = &outbuf[0];
141 		if (BackGnd != ' ')
142 		    *strp++ = BackGnd;
143 		sp = scsp;
144 		for (nchrs = 0; ; ) {
145 			d = dropit(c = TRC(cc = *sp++));
146 			if ((!d && scnhgt > HEIGHT) || (scnhgt <= Drop && d))
147 				for (j = WIDTH; --j;)
148 					*strp++ = BackGnd;
149 			else if (Drop == 0)
150 				strp = scnline(
151 				    scnkey_def[(int)c][scnhgt-1-d], strp, cc);
152 			else
153 				strp = scnline(
154 				    scnkey_lpd[(int)c][scnhgt-1-d], strp, cc);
155 			if (*sp == dlm || *sp == '\0' || nchrs++ >= PW/(WIDTH+1)-1)
156 				break;
157 			*strp++ = BackGnd;
158 		}
159 		if (BackGnd != ' ')
160 		    *strp++ = BackGnd;
161 		else {
162 		    while (*--strp == ' ' && strp >= outbuf)
163 			;
164 		    strp++;
165 		}
166 		*strp++ = '\n';
167 		(void) write(scfd, outbuf, strp-outbuf);
168 	}
169 }
170 
171 /*
172  * for each word, print up to 10 chars in big letters.
173  */
174 int
175 main(int argc, char **argv)
176 {
177 	char word[10+1];		/* strings limited to 10 chars */
178 	int c;
179 
180 	while ((c = getopt(argc, argv, "b:f:l")) != -1) {
181 	    switch (c) {
182 	    case 'f':
183 		if (*optarg == '-')
184 		    ForeGnd = 0;
185 		else
186 		    ForeGnd = *optarg;
187 		break;
188 	    case 'b':
189 		BackGnd = *optarg;
190 		break;
191 	    case 'l':
192 		Drop = 3;			/* for LPD font */
193 		break;
194 	    default:
195 		usage();
196 	    }
197 	}
198 
199 	for (; optind < argc; ++optind) {
200 		(void)strncpy(word, argv[optind], sizeof (word) - 1);
201 		word[sizeof (word) - 1] = '\0';
202 		scan_out(1, word, '\0');
203 	}
204 	exit(0);
205 }
206 
207 void
208 usage(void)
209 {
210     fprintf(stderr, "usage: %s [-f fgchar] [-b bgchar] [-l] message...\n",
211 	getprogname());
212     exit(1);
213 }
214