1*8668cbeeSwiz /* $NetBSD: banner.c,v 1.15 2012/02/29 08:55:25 wiz Exp $ */
25d58f7faScgd
3b83d704eScgd /*
4b83d704eScgd * Changes for banner(1)
5b83d704eScgd *
6b83d704eScgd * @(#)Copyright (c) 1995, Simon J. Gerraty.
7b83d704eScgd *
8b83d704eScgd * This is free software. It comes with NO WARRANTY.
9b83d704eScgd * Permission to use, modify and distribute this source code
10b83d704eScgd * is granted subject to the following conditions.
11b83d704eScgd * 1/ that the above copyright notice and this notice
12b83d704eScgd * are preserved in all copies and that due credit be given
13b83d704eScgd * to the author.
14b83d704eScgd * 2/ that any changes to this code are clearly commented
15b83d704eScgd * as such so that the author does not get blamed for bugs
16b83d704eScgd * other than his own.
17b83d704eScgd *
18b83d704eScgd * Please send copies of changes and bug-fixes to:
19b83d704eScgd * sjg@zen.void.oz.au
20b83d704eScgd */
215d58f7faScgd
22b83d704eScgd /*
23b83d704eScgd * Copyright (c) 1983, 1993
24b83d704eScgd * The Regents of the University of California. All rights reserved.
25b83d704eScgd *
26b83d704eScgd * Redistribution and use in source and binary forms, with or without
27b83d704eScgd * modification, are permitted provided that the following conditions
28b83d704eScgd * are met:
29b83d704eScgd * 1. Redistributions of source code must retain the above copyright
30b83d704eScgd * notice, this list of conditions and the following disclaimer.
31b83d704eScgd * 2. Redistributions in binary form must reproduce the above copyright
32b83d704eScgd * notice, this list of conditions and the following disclaimer in the
33b83d704eScgd * documentation and/or other materials provided with the distribution.
3489aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
35b83d704eScgd * may be used to endorse or promote products derived from this software
36b83d704eScgd * without specific prior written permission.
37b83d704eScgd *
38b83d704eScgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39b83d704eScgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40b83d704eScgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41b83d704eScgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42b83d704eScgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43b83d704eScgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44b83d704eScgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45b83d704eScgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46b83d704eScgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47b83d704eScgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48b83d704eScgd * SUCH DAMAGE.
49b83d704eScgd */
5080baf83fSlukem
5180baf83fSlukem #include <sys/cdefs.h>
52b83d704eScgd #ifndef lint
5398e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
5498e5374cSlukem The Regents of the University of California. All rights reserved.");
55b83d704eScgd #endif /* not lint */
56b83d704eScgd
57b83d704eScgd #ifndef lint
585d58f7faScgd #if 0
59b83d704eScgd static char sccsid[] = "@(#)printjob.c 8.2 (Berkeley) 4/16/94";
605d58f7faScgd #else
61*8668cbeeSwiz __RCSID("$NetBSD: banner.c,v 1.15 2012/02/29 08:55:25 wiz Exp $");
625d58f7faScgd #endif
63b83d704eScgd #endif /* not lint */
64b83d704eScgd
65b83d704eScgd #include <stdio.h>
66fcd0fb11Smatt #include <stdlib.h>
6780baf83fSlukem #include <string.h>
6880baf83fSlukem #include <unistd.h>
69b83d704eScgd
70b83d704eScgd #include "banner.h"
71b83d704eScgd
72b83d704eScgd static long PW = LINELEN;
73023a2d6dSsjg /*
74023a2d6dSsjg * <sjg> lpd makes chars out of the letter in question.
75023a2d6dSsjg * the results are somewhat mixed. Sticking to '#' as
76023a2d6dSsjg * banner(1) does is more consistent.
77023a2d6dSsjg */
78023a2d6dSsjg static int ForeGnd = '#';
79023a2d6dSsjg static int BackGnd = ' ';
80023a2d6dSsjg static int Drop = 0; /* 3 for the LPD font */
81b83d704eScgd
828f69642dSmjl static int dropit (int);
838f69642dSmjl static void scan_out (int, char *, int);
848f69642dSmjl static char *scnline (int, char *, int);
857e47a120Sjoerg __dead static void usage(void);
8680baf83fSlukem
87b83d704eScgd /* the char gen code below is lifted from lpd */
88b83d704eScgd
89b83d704eScgd static char *
scnline(int key,char * p,int c)908f69642dSmjl scnline(int key, char *p, int c)
91b83d704eScgd {
9280baf83fSlukem int scnwidth;
93b83d704eScgd
94023a2d6dSsjg if (ForeGnd)
95023a2d6dSsjg c = ForeGnd;
96b83d704eScgd
97b83d704eScgd for (scnwidth = WIDTH; --scnwidth;) {
98b83d704eScgd key <<= 1;
99023a2d6dSsjg *p++ = key & 0200 ? c : BackGnd;
100b83d704eScgd }
101b83d704eScgd return (p);
102b83d704eScgd }
103b83d704eScgd
104b83d704eScgd #define TRC(q) (((q)-' ')&0177)
105b83d704eScgd
106b83d704eScgd
107b83d704eScgd static int
dropit(int c)1088f69642dSmjl dropit(int c)
109b83d704eScgd {
110b83d704eScgd switch(c) {
111b83d704eScgd
112b83d704eScgd case TRC('_'):
113b83d704eScgd case TRC(';'):
114b83d704eScgd case TRC(','):
115b83d704eScgd case TRC('g'):
116b83d704eScgd case TRC('j'):
117b83d704eScgd case TRC('p'):
118b83d704eScgd case TRC('q'):
119b83d704eScgd case TRC('y'):
120023a2d6dSsjg return (Drop);
121b83d704eScgd
122b83d704eScgd default:
123b83d704eScgd return (0);
124b83d704eScgd }
125b83d704eScgd }
126b83d704eScgd
127b83d704eScgd static void
scan_out(int scfd,char * scsp,int dlm)1288f69642dSmjl scan_out(int scfd, char *scsp, int dlm)
129b83d704eScgd {
13080baf83fSlukem char *strp;
13180baf83fSlukem int nchrs, j;
132b83d704eScgd char outbuf[LINELEN+1], *sp, c, cc;
133b83d704eScgd int d, scnhgt;
134b83d704eScgd
135023a2d6dSsjg for (scnhgt = 0; scnhgt++ < HEIGHT+Drop; ) {
136b83d704eScgd strp = &outbuf[0];
137023a2d6dSsjg if (BackGnd != ' ')
138023a2d6dSsjg *strp++ = BackGnd;
139b83d704eScgd sp = scsp;
14015e3b4e9Smjl for (nchrs = 0; *sp != dlm && *sp != '\0'; ) {
14179ea3ffcSmjl cc = *sp++;
142275c4321She if(cc < ' ' || ((int)cc) >= 0x7f)
14379ea3ffcSmjl cc = ' ';
14479ea3ffcSmjl d = dropit(c = TRC(cc));
145023a2d6dSsjg if ((!d && scnhgt > HEIGHT) || (scnhgt <= Drop && d))
146b83d704eScgd for (j = WIDTH; --j;)
147023a2d6dSsjg *strp++ = BackGnd;
148023a2d6dSsjg else if (Drop == 0)
149023a2d6dSsjg strp = scnline(
150023a2d6dSsjg scnkey_def[(int)c][scnhgt-1-d], strp, cc);
151b83d704eScgd else
15280baf83fSlukem strp = scnline(
153023a2d6dSsjg scnkey_lpd[(int)c][scnhgt-1-d], strp, cc);
15415e3b4e9Smjl if (nchrs++ >= PW/(WIDTH+1)-1)
155b83d704eScgd break;
156023a2d6dSsjg *strp++ = BackGnd;
157b83d704eScgd }
158023a2d6dSsjg if (BackGnd != ' ')
159023a2d6dSsjg *strp++ = BackGnd;
160023a2d6dSsjg else {
161023a2d6dSsjg while (*--strp == ' ' && strp >= outbuf)
162b83d704eScgd ;
163b83d704eScgd strp++;
164023a2d6dSsjg }
165b83d704eScgd *strp++ = '\n';
166b83d704eScgd (void) write(scfd, outbuf, strp-outbuf);
167b83d704eScgd }
168b83d704eScgd }
169b83d704eScgd
170b83d704eScgd /*
171b83d704eScgd * for each word, print up to 10 chars in big letters.
172b83d704eScgd */
173b83d704eScgd int
main(int argc,char ** argv)1748f69642dSmjl main(int argc, char **argv)
175b83d704eScgd {
176b83d704eScgd char word[10+1]; /* strings limited to 10 chars */
177023a2d6dSsjg int c;
178b83d704eScgd
1798f69642dSmjl while ((c = getopt(argc, argv, "b:f:l")) != -1) {
180023a2d6dSsjg switch (c) {
181023a2d6dSsjg case 'f':
182023a2d6dSsjg if (*optarg == '-')
183023a2d6dSsjg ForeGnd = 0;
184023a2d6dSsjg else
185023a2d6dSsjg ForeGnd = *optarg;
186023a2d6dSsjg break;
187023a2d6dSsjg case 'b':
188023a2d6dSsjg BackGnd = *optarg;
189023a2d6dSsjg break;
190023a2d6dSsjg case 'l':
191023a2d6dSsjg Drop = 3; /* for LPD font */
192023a2d6dSsjg break;
1938f69642dSmjl default:
1948f69642dSmjl usage();
195023a2d6dSsjg }
196023a2d6dSsjg }
197023a2d6dSsjg
198023a2d6dSsjg for (; optind < argc; ++optind) {
19915e3b4e9Smjl (void)strlcpy(word, argv[optind], sizeof (word));
20015e3b4e9Smjl scan_out(STDOUT_FILENO, word, '\0');
201b83d704eScgd }
202b83d704eScgd exit(0);
203b83d704eScgd }
2048f69642dSmjl
2057e47a120Sjoerg static void
usage(void)2068f69642dSmjl usage(void)
2078f69642dSmjl {
208*8668cbeeSwiz fprintf(stderr, "usage: %s [-l] [-b bg] [-f fg] string ...\n",
209c2bdafabScgd getprogname());
2108f69642dSmjl exit(1);
2118f69642dSmjl }
212