banner.c revision 1.3
1/*	$OpenBSD: banner.c,v 1.3 1998/12/07 20:09:40 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. All advertising materials mentioning features or use of this software
36 *    must display the following acknowledgement:
37 *	This product includes software developed by the University of
38 *	California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 *    may be used to endorse or promote products derived from this software
41 *    without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55#ifndef lint
56static char copyright[] =
57"@(#) 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
63static char sccsid[] = "@(#)printjob.c	8.2 (Berkeley) 4/16/94";
64#else
65static char rcsid[] = "$OpenBSD: banner.c,v 1.3 1998/12/07 20:09:40 deraadt Exp $";
66#endif
67#endif /* not lint */
68
69#include <stdio.h>
70#include <unistd.h>
71#include <stdlib.h>
72#include <string.h>
73
74#include "banner.h"
75
76static long PW = LINELEN;
77
78/* the char gen code below is lifted from lpd */
79
80static char *
81scnline(key, p, c)
82	register int key;
83	register char *p;
84	int c;
85{
86	int scnwidth;
87
88	/*
89	 * <sjg> lpd makes chars out of the letter in question.
90	 * the results are somewhat mixed.  Sticking to '#' as
91	 * banner(1) does is more consistent.
92	 */
93#ifndef NOHASH_ONLY
94	c = '#';
95#endif
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
107static int
108dropit(c)
109	int c;
110{
111	switch(c) {
112
113	case TRC('_'):
114	case TRC(';'):
115	case TRC(','):
116	case TRC('g'):
117	case TRC('j'):
118	case TRC('p'):
119	case TRC('q'):
120	case TRC('y'):
121		return (DROP);
122
123	default:
124		return (0);
125	}
126}
127
128static void
129scan_out(scfd, scsp, dlm)
130	int scfd, dlm;
131	char *scsp;
132{
133	char *strp;
134	int nchrs, j;
135	char outbuf[LINELEN+1], *sp, c, cc;
136	int d, scnhgt;
137	extern char scnkey[][HEIGHT];	/* in lpdchar.c */
138
139	for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
140		strp = &outbuf[0];
141		sp = scsp;
142		for (nchrs = 0; ; ) {
143			c = TRC(cc = *sp++);
144			d = dropit(c);
145			if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
146				for (j = WIDTH; --j;)
147					*strp++ = BACKGND;
148			else
149				strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc);
150			if (*sp == dlm || *sp == '\0' || nchrs++ >= PW/(WIDTH+1)-1)
151				break;
152			*strp++ = BACKGND;
153#ifdef LPD_CHSET				/* <sjg> */
154			*strp++ = BACKGND;
155#endif
156		}
157		while (*--strp == BACKGND && strp >= outbuf)
158			;
159		strp++;
160		*strp++ = '\n';
161		(void) write(scfd, outbuf, strp-outbuf);
162	}
163}
164
165/*
166 * for each word, print up to 10 chars in big letters.
167 */
168int
169main(argc, argv)
170	int argc;
171	char **argv;
172{
173	char word[10+1];			/* strings limited to 10 chars */
174
175	while (*++argv) {
176		(void)strncpy(word, *argv, sizeof (word) - 1);
177		word[sizeof (word) - 1] = '\0';
178		scan_out(1, word, '\0');
179	}
180	exit(0);
181}
182