xref: /dflybsd-src/usr.bin/banner/banner.c (revision 024d5a36398473d79db5a61bd8dd7a48f9e5b2f3)
1f1cb5275Szrj /*	$OpenBSD: banner.c,v 1.13 2017/05/27 07:55:44 tedu Exp $	*/
2f1cb5275Szrj /*	$NetBSD: banner.c,v 1.2 1995/04/09 06:00:15 cgd Exp $	*/
3f1cb5275Szrj 
4f1cb5275Szrj /*
5f1cb5275Szrj  *      Changes for banner(1)
6f1cb5275Szrj  *
7f1cb5275Szrj  *      @(#)Copyright (c) 1995, Simon J. Gerraty.
8f1cb5275Szrj  *
9f1cb5275Szrj  *      This is free software.  It comes with NO WARRANTY.
10f1cb5275Szrj  *      Permission to use, modify and distribute this source code
11f1cb5275Szrj  *      is granted subject to the following conditions.
12f1cb5275Szrj  *      1/ that the above copyright notice and this notice
13f1cb5275Szrj  *      are preserved in all copies and that due credit be given
14f1cb5275Szrj  *      to the author.
15f1cb5275Szrj  *      2/ that any changes to this code are clearly commented
16f1cb5275Szrj  *      as such so that the author does not get blamed for bugs
17f1cb5275Szrj  *      other than his own.
18f1cb5275Szrj  *
19f1cb5275Szrj  *      Please send copies of changes and bug-fixes to:
20f1cb5275Szrj  *      sjg@zen.void.oz.au
21f1cb5275Szrj  */
22f1cb5275Szrj 
23f1cb5275Szrj /*
24f1cb5275Szrj  * Copyright (c) 1983, 1993
25f1cb5275Szrj  *	The Regents of the University of California.  All rights reserved.
26f1cb5275Szrj  *
27f1cb5275Szrj  * Redistribution and use in source and binary forms, with or without
28f1cb5275Szrj  * modification, are permitted provided that the following conditions
29f1cb5275Szrj  * are met:
30f1cb5275Szrj  * 1. Redistributions of source code must retain the above copyright
31f1cb5275Szrj  *    notice, this list of conditions and the following disclaimer.
32f1cb5275Szrj  * 2. Redistributions in binary form must reproduce the above copyright
33f1cb5275Szrj  *    notice, this list of conditions and the following disclaimer in the
34f1cb5275Szrj  *    documentation and/or other materials provided with the distribution.
35f1cb5275Szrj  * 3. Neither the name of the University nor the names of its contributors
36f1cb5275Szrj  *    may be used to endorse or promote products derived from this software
37f1cb5275Szrj  *    without specific prior written permission.
38f1cb5275Szrj  *
39f1cb5275Szrj  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
40f1cb5275Szrj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41f1cb5275Szrj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42f1cb5275Szrj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
43f1cb5275Szrj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44f1cb5275Szrj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45f1cb5275Szrj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46f1cb5275Szrj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47f1cb5275Szrj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48f1cb5275Szrj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49f1cb5275Szrj  * SUCH DAMAGE.
50f1cb5275Szrj  */
51f1cb5275Szrj 
52f1cb5275Szrj #include <stdio.h>
53f1cb5275Szrj #include <unistd.h>
54f1cb5275Szrj #include <stdlib.h>
55f1cb5275Szrj #include <string.h>
56f1cb5275Szrj #include <err.h>
57f1cb5275Szrj 
58f1cb5275Szrj #include "banner.h"
59f1cb5275Szrj 
60f1cb5275Szrj extern const char scnkey[][HEIGHT];	/* in lpdchar.c */
61f1cb5275Szrj 
62f1cb5275Szrj static long PW = LINELEN;
63f1cb5275Szrj 
64f1cb5275Szrj /* the char gen code below is lifted from lpd */
65f1cb5275Szrj 
66f1cb5275Szrj static char *
scnline(int key,char * p,int c)67f1cb5275Szrj scnline(int key, char *p, int c)
68f1cb5275Szrj {
69f1cb5275Szrj 	int scnwidth;
70f1cb5275Szrj 
71f1cb5275Szrj 	/*
72f1cb5275Szrj 	 * <sjg> lpd makes chars out of the letter in question.
73f1cb5275Szrj 	 * the results are somewhat mixed.  Sticking to '#' as
74f1cb5275Szrj 	 * banner(1) does is more consistent.
75f1cb5275Szrj 	 */
76f1cb5275Szrj 	c = '#';
77f1cb5275Szrj 
78f1cb5275Szrj 	for (scnwidth = WIDTH; --scnwidth;) {
79f1cb5275Szrj 		key <<= 1;
80f1cb5275Szrj 		*p++ = key & 0200 ? c : BACKGND;
81f1cb5275Szrj 	}
82f1cb5275Szrj 	return (p);
83f1cb5275Szrj }
84f1cb5275Szrj 
85f1cb5275Szrj #define TRC(q)	(((q)-' ')&0177)
86f1cb5275Szrj 
87f1cb5275Szrj 
88f1cb5275Szrj static int
dropit(int c)89f1cb5275Szrj dropit(int c)
90f1cb5275Szrj {
91f1cb5275Szrj 	switch(c) {
92f1cb5275Szrj 
93f1cb5275Szrj 	case TRC('_'):
94f1cb5275Szrj 	case TRC(';'):
95f1cb5275Szrj 	case TRC(','):
96f1cb5275Szrj 	case TRC('g'):
97f1cb5275Szrj 	case TRC('j'):
98f1cb5275Szrj 	case TRC('p'):
99f1cb5275Szrj 	case TRC('q'):
100f1cb5275Szrj 	case TRC('y'):
101f1cb5275Szrj 		return (DROP);
102f1cb5275Szrj 
103f1cb5275Szrj 	default:
104f1cb5275Szrj 		return (0);
105f1cb5275Szrj 	}
106f1cb5275Szrj }
107f1cb5275Szrj 
108f1cb5275Szrj static void
scan_out(int scfd,char * scsp,int dlm)109f1cb5275Szrj scan_out(int scfd, char *scsp, int dlm)
110f1cb5275Szrj {
111f1cb5275Szrj 	char *strp;
112f1cb5275Szrj 	int nchrs, j;
113f1cb5275Szrj 	char outbuf[LINELEN+1], *sp, c, cc;
114f1cb5275Szrj 	int d, scnhgt;
115f1cb5275Szrj 
116f1cb5275Szrj 	for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
117f1cb5275Szrj 		strp = &outbuf[0];
118f1cb5275Szrj 		sp = scsp;
119f1cb5275Szrj 		for (nchrs = 0; *sp != dlm && *sp != '\0'; ) {
120f1cb5275Szrj 			cc = *sp++;
121f1cb5275Szrj 			if ((unsigned char)cc < ' ' ||
122f1cb5275Szrj 			    (unsigned char)cc > 0x7f)
123f1cb5275Szrj 				cc = INVALID;
124f1cb5275Szrj 
125f1cb5275Szrj 			c = TRC(cc);
126f1cb5275Szrj 			d = dropit(c);
127f1cb5275Szrj 			if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
128f1cb5275Szrj 				for (j = WIDTH; --j;)
129f1cb5275Szrj 					*strp++ = BACKGND;
130f1cb5275Szrj 			else
131f1cb5275Szrj 				strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc);
132f1cb5275Szrj 			if (nchrs++ >= PW/(WIDTH+1)-1)
133f1cb5275Szrj 				break;
134f1cb5275Szrj 			*strp++ = BACKGND;
135f1cb5275Szrj 		}
136f1cb5275Szrj 		while (*--strp == BACKGND && strp >= outbuf)
137f1cb5275Szrj 			;
138f1cb5275Szrj 		strp++;
139f1cb5275Szrj 		*strp++ = '\n';
140*024d5a36Szrj 		write(scfd, outbuf, strp-outbuf);
141f1cb5275Szrj 	}
142f1cb5275Szrj }
143f1cb5275Szrj 
144f1cb5275Szrj /*
145f1cb5275Szrj  * for each word, print up to 10 chars in big letters.
146f1cb5275Szrj  */
147f1cb5275Szrj int
main(int argc __unused,char * argv[])148f1cb5275Szrj main(int argc __unused, char *argv[])
149f1cb5275Szrj {
150f1cb5275Szrj 	char word[10+1];			/* strings limited to 10 chars */
151f1cb5275Szrj 
152f1cb5275Szrj 	while (*++argv) {
153*024d5a36Szrj 		strlcpy(word, *argv, sizeof (word));
154f1cb5275Szrj 		scan_out(1, word, '\0');
155f1cb5275Szrj 	}
156f1cb5275Szrj 	exit(0);
157f1cb5275Szrj }
158