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