xref: /titanic_51/usr/src/cmd/lp/lib/oam/fmtmsg.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include "stdio.h"
35*7c478bd9Sstevel@tonic-gate #include "string.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include "oam.h"
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <widec.h>
40*7c478bd9Sstevel@tonic-gate #include <libintl.h>
41*7c478bd9Sstevel@tonic-gate #include <locale.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define LINE_LEN 70
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define SHORT_S 80
46*7c478bd9Sstevel@tonic-gate #define LONG_S  2000
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static char		*severity_names[MAX_SEVERITY-MIN_SEVERITY+1] = {
49*7c478bd9Sstevel@tonic-gate 	"HALT",
50*7c478bd9Sstevel@tonic-gate 	"ERROR",
51*7c478bd9Sstevel@tonic-gate 	"WARNING",
52*7c478bd9Sstevel@tonic-gate 	"INFO"
53*7c478bd9Sstevel@tonic-gate };
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate static const char	*TOFIX	= "TO FIX";
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static int		wrap(wchar_t *, wchar_t *, int, wchar_t *);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /**
60*7c478bd9Sstevel@tonic-gate  ** fmtmsg()
61*7c478bd9Sstevel@tonic-gate  **/
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate void
64*7c478bd9Sstevel@tonic-gate fmtmsg(char *label, int severity, char *text, char *action)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	int	tofix_len, indent_len;
67*7c478bd9Sstevel@tonic-gate 	wchar_t	wtofix[SHORT_S], wlabel[SHORT_S], wsev[SHORT_S], wtext[LONG_S],
68*7c478bd9Sstevel@tonic-gate 		null[1] = {0};
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	/*
71*7c478bd9Sstevel@tonic-gate 	 * Return if the severity isn't recognized.
72*7c478bd9Sstevel@tonic-gate 	 */
73*7c478bd9Sstevel@tonic-gate 	if (severity < MIN_SEVERITY || MAX_SEVERITY < severity)
74*7c478bd9Sstevel@tonic-gate 		return;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	mbstowcs(wtofix, gettext(TOFIX), SHORT_S);
77*7c478bd9Sstevel@tonic-gate 	mbstowcs(wlabel, label, SHORT_S);
78*7c478bd9Sstevel@tonic-gate 	mbstowcs(wsev, gettext(severity_names[severity]), SHORT_S);
79*7c478bd9Sstevel@tonic-gate 	mbstowcs(wtext, text, LONG_S);
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	tofix_len = wscol(wtofix),
82*7c478bd9Sstevel@tonic-gate 	indent_len = wscol(wlabel) + wscol(wsev) + 2;
83*7c478bd9Sstevel@tonic-gate 	if (indent_len < tofix_len)
84*7c478bd9Sstevel@tonic-gate 		indent_len = tofix_len;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	if (wrap(wlabel, wsev, indent_len, wtext) <= 0)
87*7c478bd9Sstevel@tonic-gate 		return;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if (action && *action) {
90*7c478bd9Sstevel@tonic-gate 		if (fputc('\n', stderr) == EOF)
91*7c478bd9Sstevel@tonic-gate 			return;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 		mbstowcs(wtext, action, LONG_S);
94*7c478bd9Sstevel@tonic-gate 		if (wrap(wtofix, null, indent_len, wtext) <= 0)
95*7c478bd9Sstevel@tonic-gate 			return;
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	if (fputc('\n', stderr) == EOF)
99*7c478bd9Sstevel@tonic-gate 		return;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	fflush (stderr);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /**
105*7c478bd9Sstevel@tonic-gate  ** wrap() - PUT OUT "STUFF: string", WRAPPING string AS REQUIRED
106*7c478bd9Sstevel@tonic-gate  **/
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate static int
109*7c478bd9Sstevel@tonic-gate wrap(wchar_t *prefix, wchar_t *suffix, int indent_len, wchar_t *str)
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	int	len, n, col;
112*7c478bd9Sstevel@tonic-gate 	int	maxlen, tmpcol;
113*7c478bd9Sstevel@tonic-gate 	wchar_t	*p, *pw, *ppw;
114*7c478bd9Sstevel@tonic-gate 	static const wchar_t	eol[] = {L'\r', L'\n', L'\0'};
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	/*
117*7c478bd9Sstevel@tonic-gate 	 * Display the initial stuff followed by a colon.
118*7c478bd9Sstevel@tonic-gate 	 */
119*7c478bd9Sstevel@tonic-gate 	if ((len = wscol(suffix)))
120*7c478bd9Sstevel@tonic-gate 		n = fprintf(stderr, gettext("%*ws: %ws: "),
121*7c478bd9Sstevel@tonic-gate 			indent_len - len - 2, prefix, suffix);
122*7c478bd9Sstevel@tonic-gate 	else
123*7c478bd9Sstevel@tonic-gate 		n = fprintf(stderr, gettext("%*ws: "), indent_len, prefix);
124*7c478bd9Sstevel@tonic-gate 	if (n <= 0)
125*7c478bd9Sstevel@tonic-gate 		return (-1);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	maxlen = LINE_LEN - indent_len - 1;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	/* Check for bogus indent_len */
130*7c478bd9Sstevel@tonic-gate 	if (maxlen < 1) {
131*7c478bd9Sstevel@tonic-gate 		return (-1);
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	/*
135*7c478bd9Sstevel@tonic-gate 	 * Loop once for each line of the string to display.
136*7c478bd9Sstevel@tonic-gate 	 */
137*7c478bd9Sstevel@tonic-gate 	for (p = str; *p; ) {
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 		/*
140*7c478bd9Sstevel@tonic-gate 		 * Display the next "len" bytes of the string, where
141*7c478bd9Sstevel@tonic-gate 		 * "len" is the smallest of:
142*7c478bd9Sstevel@tonic-gate 		 *
143*7c478bd9Sstevel@tonic-gate 		 *	- LINE_LEN
144*7c478bd9Sstevel@tonic-gate 		 *	- # bytes before control character
145*7c478bd9Sstevel@tonic-gate 		 *	- # bytes left in string
146*7c478bd9Sstevel@tonic-gate 		 *
147*7c478bd9Sstevel@tonic-gate 		 */
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		len = wcscspn(p, eol);
150*7c478bd9Sstevel@tonic-gate 		/* calc how many columns the string will take */
151*7c478bd9Sstevel@tonic-gate 		col = wcswidth(p, len);
152*7c478bd9Sstevel@tonic-gate 		if (col > maxlen) {
153*7c478bd9Sstevel@tonic-gate 			/*
154*7c478bd9Sstevel@tonic-gate 			 * How many characters fit into our desired line length
155*7c478bd9Sstevel@tonic-gate 			 */
156*7c478bd9Sstevel@tonic-gate 			pw = p;
157*7c478bd9Sstevel@tonic-gate 			tmpcol = 0;
158*7c478bd9Sstevel@tonic-gate 			while (*pw) {
159*7c478bd9Sstevel@tonic-gate 				if (iswprint(*pw))
160*7c478bd9Sstevel@tonic-gate 					tmpcol += wcwidth(*pw);
161*7c478bd9Sstevel@tonic-gate 				if (tmpcol > maxlen)
162*7c478bd9Sstevel@tonic-gate 					break;
163*7c478bd9Sstevel@tonic-gate 				else
164*7c478bd9Sstevel@tonic-gate 					pw++;
165*7c478bd9Sstevel@tonic-gate 			}
166*7c478bd9Sstevel@tonic-gate 			/*
167*7c478bd9Sstevel@tonic-gate 			 * At this point, pw may point to:
168*7c478bd9Sstevel@tonic-gate 			 * A null character:  EOL found (should never happen, though)
169*7c478bd9Sstevel@tonic-gate 			 * The character that just overruns the maxlen.
170*7c478bd9Sstevel@tonic-gate 			 */
171*7c478bd9Sstevel@tonic-gate 			if (!*pw) {
172*7c478bd9Sstevel@tonic-gate 				/*
173*7c478bd9Sstevel@tonic-gate 				 * Found a EOL.
174*7c478bd9Sstevel@tonic-gate 				 * This should never happen.
175*7c478bd9Sstevel@tonic-gate 				 */
176*7c478bd9Sstevel@tonic-gate 				len = pw - p;
177*7c478bd9Sstevel@tonic-gate 				goto printline;
178*7c478bd9Sstevel@tonic-gate 			}
179*7c478bd9Sstevel@tonic-gate 			ppw = pw;
180*7c478bd9Sstevel@tonic-gate 			/*
181*7c478bd9Sstevel@tonic-gate 			 * Don't split words
182*7c478bd9Sstevel@tonic-gate 			 *
183*7c478bd9Sstevel@tonic-gate 			 * Bugid 4202307 - liblpoam in lp internal library doesn't
184*7c478bd9Sstevel@tonic-gate 			 * handle multibyte character.
185*7c478bd9Sstevel@tonic-gate 			 */
186*7c478bd9Sstevel@tonic-gate 			while (pw > p) {
187*7c478bd9Sstevel@tonic-gate 				if (iswspace(*pw) ||
188*7c478bd9Sstevel@tonic-gate 				    (wdbindf(*(pw - 1), *pw, 1) < 5)) {
189*7c478bd9Sstevel@tonic-gate 					break;
190*7c478bd9Sstevel@tonic-gate 				} else {
191*7c478bd9Sstevel@tonic-gate 					pw--;
192*7c478bd9Sstevel@tonic-gate 				}
193*7c478bd9Sstevel@tonic-gate 			}
194*7c478bd9Sstevel@tonic-gate 			if (pw != p) {
195*7c478bd9Sstevel@tonic-gate 				len = pw - p;
196*7c478bd9Sstevel@tonic-gate 			} else {
197*7c478bd9Sstevel@tonic-gate 				/*
198*7c478bd9Sstevel@tonic-gate 				 * Failed to find the best place to fold.
199*7c478bd9Sstevel@tonic-gate 				 * So, prints as much characters as maxlen allows
200*7c478bd9Sstevel@tonic-gate 				 */
201*7c478bd9Sstevel@tonic-gate 				len = ppw - p;
202*7c478bd9Sstevel@tonic-gate 			}
203*7c478bd9Sstevel@tonic-gate 		}
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate printline:
206*7c478bd9Sstevel@tonic-gate 		for (n = 0; n < len; n++, p++) {
207*7c478bd9Sstevel@tonic-gate 			if (iswprint(*p)) {
208*7c478bd9Sstevel@tonic-gate 				if (fputwc(*p, stderr) == WEOF) {
209*7c478bd9Sstevel@tonic-gate 					return (-1);
210*7c478bd9Sstevel@tonic-gate 				}
211*7c478bd9Sstevel@tonic-gate 			}
212*7c478bd9Sstevel@tonic-gate 		}
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 		/*
215*7c478bd9Sstevel@tonic-gate 		 * If we displayed up to a control character,
216*7c478bd9Sstevel@tonic-gate 		 * put out the control character now; otherwise,
217*7c478bd9Sstevel@tonic-gate 		 * put out a newline unless we've put out all
218*7c478bd9Sstevel@tonic-gate 		 * the text.
219*7c478bd9Sstevel@tonic-gate 		 */
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 		if (*p == L'\r' || *p == L'\n') {
222*7c478bd9Sstevel@tonic-gate 			while (*p == L'\r' || *p == L'\n') {
223*7c478bd9Sstevel@tonic-gate 				if (fputwc(*p, stderr) == WEOF)
224*7c478bd9Sstevel@tonic-gate 					return (-1);
225*7c478bd9Sstevel@tonic-gate 				p++;
226*7c478bd9Sstevel@tonic-gate 			}
227*7c478bd9Sstevel@tonic-gate 		} else if (*p) {
228*7c478bd9Sstevel@tonic-gate 			if (fputwc(L'\n', stderr) == WEOF)
229*7c478bd9Sstevel@tonic-gate 				return (-1);
230*7c478bd9Sstevel@tonic-gate 		}
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 		while (iswspace(*p))
233*7c478bd9Sstevel@tonic-gate 			p++;
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 		/*
236*7c478bd9Sstevel@tonic-gate 		 * If the loop won't end this time (because we
237*7c478bd9Sstevel@tonic-gate 		 * have more stuff to display) put out leading
238*7c478bd9Sstevel@tonic-gate 		 * blanks to align the next line with the previous
239*7c478bd9Sstevel@tonic-gate 		 * lines.
240*7c478bd9Sstevel@tonic-gate 		 */
241*7c478bd9Sstevel@tonic-gate 		if (*p) {
242*7c478bd9Sstevel@tonic-gate 			for (n = 0; n < indent_len + 2; n++)
243*7c478bd9Sstevel@tonic-gate 				(void) fputwc(L' ', stderr);
244*7c478bd9Sstevel@tonic-gate 		}
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	return (1);
248*7c478bd9Sstevel@tonic-gate }
249