xref: /netbsd/usr.bin/banner/banner.c (revision fcd0fb11)
1 /*	$NetBSD: banner.c,v 1.5 2000/07/03 02:51:12 matt 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.5 2000/07/03 02:51:12 matt 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 __P((int));
87 	int	main __P((int, char **));
88 static	void	scan_out __P((int, char *, int));
89 static	char   *scnline __P((int, char *, int));
90 
91 /* the char gen code below is lifted from lpd */
92 
93 static char *
94 scnline(key, p, c)
95 	int key;
96 	char *p;
97 	int c;
98 {
99 	int scnwidth;
100 
101 	if (ForeGnd)
102 	    c = ForeGnd;
103 
104 	for (scnwidth = WIDTH; --scnwidth;) {
105 		key <<= 1;
106 		*p++ = key & 0200 ? c : BackGnd;
107 	}
108 	return (p);
109 }
110 
111 #define TRC(q)	(((q)-' ')&0177)
112 
113 
114 static int
115 dropit(c)
116 	int c;
117 {
118 	switch(c) {
119 
120 	case TRC('_'):
121 	case TRC(';'):
122 	case TRC(','):
123 	case TRC('g'):
124 	case TRC('j'):
125 	case TRC('p'):
126 	case TRC('q'):
127 	case TRC('y'):
128 		return (Drop);
129 
130 	default:
131 		return (0);
132 	}
133 }
134 
135 static void
136 scan_out(scfd, scsp, dlm)
137 	int scfd;
138 	char *scsp;
139 	int dlm;
140 {
141 	char *strp;
142 	int nchrs, j;
143 	char outbuf[LINELEN+1], *sp, c, cc;
144 	int d, scnhgt;
145 
146 	for (scnhgt = 0; scnhgt++ < HEIGHT+Drop; ) {
147 		strp = &outbuf[0];
148 		if (BackGnd != ' ')
149 		    *strp++ = BackGnd;
150 		sp = scsp;
151 		for (nchrs = 0; ; ) {
152 			d = dropit(c = TRC(cc = *sp++));
153 			if ((!d && scnhgt > HEIGHT) || (scnhgt <= Drop && d))
154 				for (j = WIDTH; --j;)
155 					*strp++ = BackGnd;
156 			else if (Drop == 0)
157 				strp = scnline(
158 				    scnkey_def[(int)c][scnhgt-1-d], strp, cc);
159 			else
160 				strp = scnline(
161 				    scnkey_lpd[(int)c][scnhgt-1-d], strp, cc);
162 			if (*sp == dlm || *sp == '\0' || nchrs++ >= PW/(WIDTH+1)-1)
163 				break;
164 			*strp++ = BackGnd;
165 		}
166 		if (BackGnd != ' ')
167 		    *strp++ = BackGnd;
168 		else {
169 		    while (*--strp == ' ' && strp >= outbuf)
170 			;
171 		    strp++;
172 		}
173 		*strp++ = '\n';
174 		(void) write(scfd, outbuf, strp-outbuf);
175 	}
176 }
177 
178 /*
179  * for each word, print up to 10 chars in big letters.
180  */
181 int
182 main(argc, argv)
183 	int argc;
184 	char **argv;
185 {
186 	char word[10+1];		/* strings limited to 10 chars */
187 	int c;
188 
189 	while ((c = getopt(argc, argv, "b:f:l")) != EOF) {
190 	    switch (c) {
191 	    case 'f':
192 		if (*optarg == '-')
193 		    ForeGnd = 0;
194 		else
195 		    ForeGnd = *optarg;
196 		break;
197 	    case 'b':
198 		BackGnd = *optarg;
199 		break;
200 	    case 'l':
201 		Drop = 3;			/* for LPD font */
202 		break;
203 	    }
204 	}
205 
206 	for (; optind < argc; ++optind) {
207 		(void)strncpy(word, argv[optind], sizeof (word) - 1);
208 		word[sizeof (word) - 1] = '\0';
209 		scan_out(1, word, '\0');
210 	}
211 	exit(0);
212 }
213