xref: /illumos-gate/usr/src/cmd/banner/banner.c (revision 763f1f5f97e4c16840af2ced98915f0ed0f46616)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 
36 #define	nchars 128	/* number of chars in char set */
37 #define	nlines  7	/* number of lines in a banner character */
38 #define	pposs 85	/* number of print positions */
39 			/* on a line (must be multiple of 4) */
40 			/* followed by end of string character */
41 #define	pospch 8	/* number of char positions per banner char */
42 #define	chpln  10	/* number of banner characters per line */
43 
44 struct bann {
45 	char alpha[nlines][pposs];
46 };
47 static struct bann buffer, *bp = &buffer;
48 static char ctbl[nchars][nlines] = {
49 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
50 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
51 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
52 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
53 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
54 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
55 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
56 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
57 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
58 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
59 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
60 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
61 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
62 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
63 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
64 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
65 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
66 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
67 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
68 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
69 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
70 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
71 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
72 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
73 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
74 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
75 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
76 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
77 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
78 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
79 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
80 	0, 000, 000, 000, 000, 000, 000, 	/* below 040 */
81 	0, 000, 000, 000, 000, 000, 000, 	/*  */
82 	034, 034, 034, 010, 0, 034, 034, 	/* ! */
83 	0167, 0167, 042, 0, 0, 0, 0,		/* " */
84 	024, 024, 0177, 024, 0177, 024, 024, 	/* # */
85 	076, 0111, 0110, 076, 011, 0111, 076, 	/* $ */
86 	0161, 0122, 0164, 010, 027, 045, 0107, 	/* % */
87 	030, 044, 030, 070, 0105, 0102, 071, 	/* & */
88 	034, 034, 010, 020, 0, 0, 0,    	/* ' */
89 	014, 020, 040, 040, 040, 020, 014, 	/* ( */
90 	030, 4, 2, 2, 2, 4, 030,		/* ) */
91 	0, 042, 024, 0177, 024, 042, 0, 	/* * */
92 	0, 010, 010, 076, 010, 010, 0,   	/* + */
93 	0, 0, 0, 034, 034, 010, 020,    	/* , */
94 	0, 0, 0, 076, 0, 0, 0,			/* - */
95 	0, 0, 0, 0, 034, 034, 034,		/* . */
96 	1, 2, 4, 010, 020, 040, 0100,   	/* / */
97 	034, 042, 0101, 0101, 0101, 042, 034, 	/* 0 */
98 	010, 030, 050, 010, 010, 010, 076, 	/* 1 */
99 	076, 0101, 1, 076, 0100, 0100, 0177, 	/* 2 */
100 	076, 0101, 1, 076, 1, 0101, 076, 	/* 3 */
101 	0100, 0102, 0102, 0102, 0177, 2, 2, 	/* 4 */
102 	0177, 0100, 0100, 0176, 1, 0101, 076, 	/* 5 */
103 	076, 0101, 0100, 0176, 0101, 0101, 076, /* 6 */
104 	0177, 0102, 04, 010, 020, 020, 020, 	/* 7 */
105 	076, 0101, 0101, 076, 0101, 0101, 076, 	/* 8 */
106 	076, 0101, 0101, 077, 1, 0101, 076, 	/* 9 */
107 	010, 034, 010, 0, 010, 034, 010, 	/* : */
108 	034, 034, 0, 034, 034, 010, 020, 	/* ; */
109 	4, 010, 020, 040, 020, 010, 4,    	/* < */
110 	0, 0, 076, 0, 076, 0, 0,		/* = */
111 	020, 010, 4, 2, 4, 010, 020,    	/* > */
112 	076, 0101, 1, 016, 010, 0, 010, 	/* ? */
113 	076, 0101, 0135, 0135, 0136, 0100, 076, 	/* @ */
114 	010, 024, 042, 0101, 0177, 0101, 0101,   	/* A */
115 	0176, 0101, 0101, 0176, 0101, 0101, 0176, 	/* B */
116 	076, 0101, 0100, 0100, 0100, 0101, 076, 	/* C */
117 	0176, 0101, 0101, 0101, 0101, 0101, 0176, 	/* D */
118 	0177, 0100, 0100, 0174, 0100, 0100, 0177, 	/* E */
119 	0177, 0100, 0100, 0174, 0100, 0100, 0100, 	/* F */
120 	076, 0101, 0100, 0117, 0101, 0101, 076, 	/* G */
121 	0101, 0101, 0101, 0177, 0101, 0101, 0101, 	/* H */
122 	034, 010, 010, 010, 010, 010, 034,		/* I */
123 	1, 1, 1, 1, 0101, 0101, 076,			/* J */
124 	0102, 0104, 0110, 0160, 0110, 0104, 0102, 	/* K */
125 	0100, 0100, 0100, 0100, 0100, 0100, 0177, 	/* L */
126 	0101, 0143, 0125, 0111, 0101, 0101, 0101, 	/* M */
127 	0101, 0141, 0121, 0111, 0105, 0103, 0101, 	/* N */
128 	0177, 0101, 0101, 0101, 0101, 0101, 0177, 	/* O */
129 	0176, 0101, 0101, 0176, 0100, 0100, 0100, 	/* P */
130 	076, 0101, 0101, 0101, 0105, 0102, 075, 	/* Q */
131 	0176, 0101, 0101, 0176, 0104, 0102, 0101, 	/* R */
132 	076, 0101, 0100, 076, 1, 0101, 076,		/* S */
133 	0177, 010, 010, 010, 010, 010, 010,		/* T */
134 	0101, 0101, 0101, 0101, 0101, 0101, 076, 	/* U */
135 	0101, 0101, 0101, 0101, 042, 024, 010,   	/* V */
136 	0101, 0111, 0111, 0111, 0111, 0111, 066, 	/* W */
137 	0101, 042, 024, 010, 024, 042, 0101,    	/* X */
138 	0101, 042, 024, 010, 010, 010, 010,		/* Y */
139 	0177, 2, 4, 010, 020, 040, 0177,		/* Z */
140 	076, 040, 040, 040, 040, 040, 076,		/* [ */
141 	0100, 040, 020, 010, 004, 002, 001,		/* \ */
142 	076, 2, 2, 2, 2, 2, 076,			/* ] */
143 	010, 024, 042, 0, 0, 0, 0,			/* ^ */
144 	0, 000, 000, 000, 000, 000, 0177,		/* _ */
145 	034, 034, 010, 04, 0, 0, 0,			/* ` */
146 	0, 014, 022, 041, 077, 041, 041, 	/* A */
147 	0, 076, 041, 076, 041, 041, 076, 	/* B */
148 	0, 036, 041, 040, 040, 041, 036, 	/* C */
149 	0, 076, 041, 041, 041, 041, 076, 	/* D */
150 	0, 077, 040, 076, 040, 040, 077, 	/* E */
151 	0, 077, 040, 076, 040, 040, 040, 	/* F */
152 	0, 036, 041, 040, 047, 041, 036, 	/* G */
153 	0, 041, 041, 077, 041, 041, 041, 	/* H */
154 	0, 004, 004, 004, 004, 004, 004, 	/* I */
155 	0, 001, 001, 001, 001, 041, 036, 	/* J */
156 	0, 041, 042, 074, 044, 042, 041, 	/* K */
157 	0, 040, 040, 040, 040, 040, 077, 	/* L */
158 	0, 041, 063, 055, 041, 041, 041, 	/* M */
159 	0, 041, 061, 051, 045, 043, 041, 	/* N */
160 	0, 036, 041, 041, 041, 041, 036, 	/* O */
161 	0, 076, 041, 041, 076, 040, 040, 	/* P */
162 	0, 036, 041, 041, 045, 042, 035, 	/* Q */
163 	0, 076, 041, 041, 076, 042, 041, 	/* R */
164 	0, 036, 040, 036, 001, 041, 036, 	/* S */
165 	0, 037, 004, 004, 004, 004, 004, 	/* T */
166 	0, 041, 041, 041, 041, 041, 036, 	/* U */
167 	0, 041, 041, 041, 041, 022, 014, 	/* V */
168 	0, 041, 041, 041, 055, 063, 041, 	/* W */
169 	0, 041, 022, 014, 014, 022, 041, 	/* X */
170 	0, 021, 012, 004, 004, 004, 004, 	/* Y */
171 	0, 077, 002, 004, 010, 020, 077, 	/* Z */
172 	034, 040, 040, 0140, 040, 040, 034, 	/* { */
173 	010, 010, 010, 0, 010, 010, 010, 	/* | */
174 	034, 2, 2, 3, 2, 2, 034,		/* } */
175 	060, 0111, 06, 0, 0, 0, 0,		/* ~ */
176 	0, 000, 000, 000, 000, 000, 000 	/* DEL */
177 };
178 static char blank = ' ';
179 static char plot = '#';
180 static int  msk = 0100; /* ? */
181 
182 static void banner(char *s, struct bann *bufp);
183 static void banfil(char *c, struct bann *p);
184 static void banset(char c, struct bann *p);
185 static void banprt(struct bann *ptr);
186 static char convert(unsigned char c);
187 
188 int
189 main(int argc, char **argv)
190 {
191 	int i;
192 	/*
193 	 * if invoked with no arguments, prints error comment
194 	 * if invoked with an argument, prints it in banner form.
195 	 */
196 	(void) setlocale(LC_ALL, "");
197 #if !defined(TEXT_DOMAIN)
198 #define	TEXT_DOMAIN	"SYS_TEST"
199 #endif
200 	(void) textdomain(TEXT_DOMAIN);
201 	if (argc < 2) {
202 		(void) printf(gettext
203 		    ("Usage: banner \"up to 10 char arg string\" . . .\n"));
204 		exit(1);
205 	}
206 	for (i = 1; i < argc; ++i) {
207 		banner(argv[i], bp);
208 		banprt(bp);
209 		(void) printf("\n");
210 	}
211 	return (0);
212 }
213 
214 static void
215 banner(char *s, struct bann *bufp)
216 {
217 	char *p, *q;
218 	unsigned char c;
219 	struct bann *r;
220 	p = s;
221 	r = bufp;
222 	banset(blank, bufp);
223 	while ((c = *s++) != 0 && ((ptrdiff_t)s-(ptrdiff_t)p) <= chpln) {
224 		if ((c & 0300)) { /* 8th bit is on */
225 			c = convert(c);
226 		}
227 		q = ctbl[c];
228 		banfil(q, r);
229 		r = (struct bann *)((char *)r + pospch);
230 	}
231 }
232 
233 static void
234 banfil(char *c, struct bann *p)
235 {
236 	int i, j;
237 	for (i = 0; i < nlines; i++) {
238 		for (j = 0; j < pospch; j++) {
239 			if (((c[i]<<j)&msk) != 0) p->alpha[i][j] = plot;
240 		}
241 	}
242 }
243 
244 static void
245 banset(char c, struct bann *p)
246 {
247 	int i, j;
248 	for (i = 0; i < nlines; i++)
249 		for (j = 0; j < pposs-1; j++)
250 			p->alpha[i][j] = c;
251 }
252 
253 static void
254 banprt(struct bann *ptr)
255 {
256 	int i, j;
257 	for (i = 0; i < nlines; i++) {
258 		ptr->alpha[i][pposs-1] = '\0';
259 		for (j = pposs-2; j >= 0; j--) {
260 			if (ptr->alpha[i][j] != blank)break;
261 			ptr->alpha[i][j] = '\0';
262 		}
263 		(void) printf("%s\n", ptr->alpha[i]);
264 	}
265 }
266 
267 static char
268 convert(unsigned char c)
269 {
270 	if (c >= 0xc0 && c <= 0xc6)
271 		return ('A');
272 	if (c >= 0xc8 && c <= 0xcb)
273 		return ('E');
274 	if (c == 0xc7)
275 		return ('C');
276 	if (c >= 0xcc && c <= 0xcf)
277 		return ('I');
278 	if (c == 0xd0)
279 		return ('D');
280 	if (c == 0xd1)
281 		return ('N');
282 	if (c >= 0xd2 && c <= 0xd6)
283 		return ('O');
284 	if (c == 0xd7)
285 		return ('X');
286 	if (c == 0xd8)
287 		return ('O');
288 	if (c >= 0xd9 && c <= 0xdc)
289 		return ('U');
290 	if (c == 0xdd)
291 		return ('Y');
292 	if (c == 0xde)
293 		return ('P');
294 	if (c == 0xdf)
295 		return ('B');
296 	if (c >= 0xe0 && c <= 0xe6)
297 		return ('a');
298 	if (c == 0xe7)
299 		return ('c');
300 	if (c >= 0xe8 && c <= 0xeb)
301 		return ('e');
302 	if (c >= 0xec && c <= 0xef)
303 		return ('i');
304 	if (c == 0xf0)
305 		return ('d');
306 	if (c == 0xf1)
307 		return ('n');
308 	if (c >= 0xf2 && c <= 0xf6)
309 		return ('o');
310 	if (c >= 0xf9 && c <= 0xfc)
311 		return ('u');
312 	if (c == 0xfd)
313 		return ('y');
314 	if (c == 0xfe)
315 		return ('p');
316 	if (c == 0xff)
317 		return ('y');
318 	return (c & 0177);
319 }
320