xref: /illumos-gate/usr/src/cmd/lp/lib/oam/fmtmsg.c (revision 2a8bcb4e)
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 /* LINTLIBRARY */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "stdio.h"
33*7c478bd9Sstevel@tonic-gate #include "string.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include "oam.h"
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <widec.h>
38*7c478bd9Sstevel@tonic-gate #include <libintl.h>
39*7c478bd9Sstevel@tonic-gate #include <locale.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #define LINE_LEN 70
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define SHORT_S 80
44*7c478bd9Sstevel@tonic-gate #define LONG_S  2000
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static char		*severity_names[MAX_SEVERITY-MIN_SEVERITY+1] = {
47*7c478bd9Sstevel@tonic-gate 	"HALT",
48*7c478bd9Sstevel@tonic-gate 	"ERROR",
49*7c478bd9Sstevel@tonic-gate 	"WARNING",
50*7c478bd9Sstevel@tonic-gate 	"INFO"
51*7c478bd9Sstevel@tonic-gate };
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static const char	*TOFIX	= "TO FIX";
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate static int		wrap(wchar_t *, wchar_t *, int, wchar_t *);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /**
58*7c478bd9Sstevel@tonic-gate  ** fmtmsg()
59*7c478bd9Sstevel@tonic-gate  **/
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate void
fmtmsg(char * label,int severity,char * text,char * action)62*7c478bd9Sstevel@tonic-gate fmtmsg(char *label, int severity, char *text, char *action)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	int	tofix_len, indent_len;
65*7c478bd9Sstevel@tonic-gate 	wchar_t	wtofix[SHORT_S], wlabel[SHORT_S], wsev[SHORT_S], wtext[LONG_S],
66*7c478bd9Sstevel@tonic-gate 		null[1] = {0};
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	/*
69*7c478bd9Sstevel@tonic-gate 	 * Return if the severity isn't recognized.
70*7c478bd9Sstevel@tonic-gate 	 */
71*7c478bd9Sstevel@tonic-gate 	if (severity < MIN_SEVERITY || MAX_SEVERITY < severity)
72*7c478bd9Sstevel@tonic-gate 		return;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	mbstowcs(wtofix, gettext(TOFIX), SHORT_S);
75*7c478bd9Sstevel@tonic-gate 	mbstowcs(wlabel, label, SHORT_S);
76*7c478bd9Sstevel@tonic-gate 	mbstowcs(wsev, gettext(severity_names[severity]), SHORT_S);
77*7c478bd9Sstevel@tonic-gate 	mbstowcs(wtext, text, LONG_S);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	tofix_len = wscol(wtofix),
80*7c478bd9Sstevel@tonic-gate 	indent_len = wscol(wlabel) + wscol(wsev) + 2;
81*7c478bd9Sstevel@tonic-gate 	if (indent_len < tofix_len)
82*7c478bd9Sstevel@tonic-gate 		indent_len = tofix_len;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if (wrap(wlabel, wsev, indent_len, wtext) <= 0)
85*7c478bd9Sstevel@tonic-gate 		return;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	if (action && *action) {
88*7c478bd9Sstevel@tonic-gate 		if (fputc('\n', stderr) == EOF)
89*7c478bd9Sstevel@tonic-gate 			return;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 		mbstowcs(wtext, action, LONG_S);
92*7c478bd9Sstevel@tonic-gate 		if (wrap(wtofix, null, indent_len, wtext) <= 0)
93*7c478bd9Sstevel@tonic-gate 			return;
94*7c478bd9Sstevel@tonic-gate 	}
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	if (fputc('\n', stderr) == EOF)
97*7c478bd9Sstevel@tonic-gate 		return;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	fflush (stderr);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate /**
103*7c478bd9Sstevel@tonic-gate  ** wrap() - PUT OUT "STUFF: string", WRAPPING string AS REQUIRED
104*7c478bd9Sstevel@tonic-gate  **/
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate static int
wrap(wchar_t * prefix,wchar_t * suffix,int indent_len,wchar_t * str)107*7c478bd9Sstevel@tonic-gate wrap(wchar_t *prefix, wchar_t *suffix, int indent_len, wchar_t *str)
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate 	int	len, n, col;
110*7c478bd9Sstevel@tonic-gate 	int	maxlen, tmpcol;
111*7c478bd9Sstevel@tonic-gate 	wchar_t	*p, *pw, *ppw;
112*7c478bd9Sstevel@tonic-gate 	static const wchar_t	eol[] = {L'\r', L'\n', L'\0'};
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	/*
115*7c478bd9Sstevel@tonic-gate 	 * Display the initial stuff followed by a colon.
116*7c478bd9Sstevel@tonic-gate 	 */
117*7c478bd9Sstevel@tonic-gate 	if ((len = wscol(suffix)))
118*7c478bd9Sstevel@tonic-gate 		n = fprintf(stderr, gettext("%*ws: %ws: "),
119*7c478bd9Sstevel@tonic-gate 			indent_len - len - 2, prefix, suffix);
120*7c478bd9Sstevel@tonic-gate 	else
121*7c478bd9Sstevel@tonic-gate 		n = fprintf(stderr, gettext("%*ws: "), indent_len, prefix);
122*7c478bd9Sstevel@tonic-gate 	if (n <= 0)
123*7c478bd9Sstevel@tonic-gate 		return (-1);
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	maxlen = LINE_LEN - indent_len - 1;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	/* Check for bogus indent_len */
128*7c478bd9Sstevel@tonic-gate 	if (maxlen < 1) {
129*7c478bd9Sstevel@tonic-gate 		return (-1);
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	/*
133*7c478bd9Sstevel@tonic-gate 	 * Loop once for each line of the string to display.
134*7c478bd9Sstevel@tonic-gate 	 */
135*7c478bd9Sstevel@tonic-gate 	for (p = str; *p; ) {
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 		/*
138*7c478bd9Sstevel@tonic-gate 		 * Display the next "len" bytes of the string, where
139*7c478bd9Sstevel@tonic-gate 		 * "len" is the smallest of:
140*7c478bd9Sstevel@tonic-gate 		 *
141*7c478bd9Sstevel@tonic-gate 		 *	- LINE_LEN
142*7c478bd9Sstevel@tonic-gate 		 *	- # bytes before control character
143*7c478bd9Sstevel@tonic-gate 		 *	- # bytes left in string
144*7c478bd9Sstevel@tonic-gate 		 *
145*7c478bd9Sstevel@tonic-gate 		 */
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 		len = wcscspn(p, eol);
148*7c478bd9Sstevel@tonic-gate 		/* calc how many columns the string will take */
149*7c478bd9Sstevel@tonic-gate 		col = wcswidth(p, len);
150*7c478bd9Sstevel@tonic-gate 		if (col > maxlen) {
151*7c478bd9Sstevel@tonic-gate 			/*
152*7c478bd9Sstevel@tonic-gate 			 * How many characters fit into our desired line length
153*7c478bd9Sstevel@tonic-gate 			 */
154*7c478bd9Sstevel@tonic-gate 			pw = p;
155*7c478bd9Sstevel@tonic-gate 			tmpcol = 0;
156*7c478bd9Sstevel@tonic-gate 			while (*pw) {
157*7c478bd9Sstevel@tonic-gate 				if (iswprint(*pw))
158*7c478bd9Sstevel@tonic-gate 					tmpcol += wcwidth(*pw);
159*7c478bd9Sstevel@tonic-gate 				if (tmpcol > maxlen)
160*7c478bd9Sstevel@tonic-gate 					break;
161*7c478bd9Sstevel@tonic-gate 				else
162*7c478bd9Sstevel@tonic-gate 					pw++;
163*7c478bd9Sstevel@tonic-gate 			}
164*7c478bd9Sstevel@tonic-gate 			/*
165*7c478bd9Sstevel@tonic-gate 			 * At this point, pw may point to:
166*7c478bd9Sstevel@tonic-gate 			 * A null character:  EOL found (should never happen, though)
167*7c478bd9Sstevel@tonic-gate 			 * The character that just overruns the maxlen.
168*7c478bd9Sstevel@tonic-gate 			 */
169*7c478bd9Sstevel@tonic-gate 			if (!*pw) {
170*7c478bd9Sstevel@tonic-gate 				/*
171*7c478bd9Sstevel@tonic-gate 				 * Found a EOL.
172*7c478bd9Sstevel@tonic-gate 				 * This should never happen.
173*7c478bd9Sstevel@tonic-gate 				 */
174*7c478bd9Sstevel@tonic-gate 				len = pw - p;
175*7c478bd9Sstevel@tonic-gate 				goto printline;
176*7c478bd9Sstevel@tonic-gate 			}
177*7c478bd9Sstevel@tonic-gate 			ppw = pw;
178*7c478bd9Sstevel@tonic-gate 			/*
179*7c478bd9Sstevel@tonic-gate 			 * Don't split words
180*7c478bd9Sstevel@tonic-gate 			 *
181*7c478bd9Sstevel@tonic-gate 			 * Bugid 4202307 - liblpoam in lp internal library doesn't
182*7c478bd9Sstevel@tonic-gate 			 * handle multibyte character.
183*7c478bd9Sstevel@tonic-gate 			 */
184*7c478bd9Sstevel@tonic-gate 			while (pw > p) {
185*7c478bd9Sstevel@tonic-gate 				if (iswspace(*pw) ||
186*7c478bd9Sstevel@tonic-gate 				    (wdbindf(*(pw - 1), *pw, 1) < 5)) {
187*7c478bd9Sstevel@tonic-gate 					break;
188*7c478bd9Sstevel@tonic-gate 				} else {
189*7c478bd9Sstevel@tonic-gate 					pw--;
190*7c478bd9Sstevel@tonic-gate 				}
191*7c478bd9Sstevel@tonic-gate 			}
192*7c478bd9Sstevel@tonic-gate 			if (pw != p) {
193*7c478bd9Sstevel@tonic-gate 				len = pw - p;
194*7c478bd9Sstevel@tonic-gate 			} else {
195*7c478bd9Sstevel@tonic-gate 				/*
196*7c478bd9Sstevel@tonic-gate 				 * Failed to find the best place to fold.
197*7c478bd9Sstevel@tonic-gate 				 * So, prints as much characters as maxlen allows
198*7c478bd9Sstevel@tonic-gate 				 */
199*7c478bd9Sstevel@tonic-gate 				len = ppw - p;
200*7c478bd9Sstevel@tonic-gate 			}
201*7c478bd9Sstevel@tonic-gate 		}
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate printline:
204*7c478bd9Sstevel@tonic-gate 		for (n = 0; n < len; n++, p++) {
205*7c478bd9Sstevel@tonic-gate 			if (iswprint(*p)) {
206*7c478bd9Sstevel@tonic-gate 				if (fputwc(*p, stderr) == WEOF) {
207*7c478bd9Sstevel@tonic-gate 					return (-1);
208*7c478bd9Sstevel@tonic-gate 				}
209*7c478bd9Sstevel@tonic-gate 			}
210*7c478bd9Sstevel@tonic-gate 		}
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 		/*
213*7c478bd9Sstevel@tonic-gate 		 * If we displayed up to a control character,
214*7c478bd9Sstevel@tonic-gate 		 * put out the control character now; otherwise,
215*7c478bd9Sstevel@tonic-gate 		 * put out a newline unless we've put out all
216*7c478bd9Sstevel@tonic-gate 		 * the text.
217*7c478bd9Sstevel@tonic-gate 		 */
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 		if (*p == L'\r' || *p == L'\n') {
220*7c478bd9Sstevel@tonic-gate 			while (*p == L'\r' || *p == L'\n') {
221*7c478bd9Sstevel@tonic-gate 				if (fputwc(*p, stderr) == WEOF)
222*7c478bd9Sstevel@tonic-gate 					return (-1);
223*7c478bd9Sstevel@tonic-gate 				p++;
224*7c478bd9Sstevel@tonic-gate 			}
225*7c478bd9Sstevel@tonic-gate 		} else if (*p) {
226*7c478bd9Sstevel@tonic-gate 			if (fputwc(L'\n', stderr) == WEOF)
227*7c478bd9Sstevel@tonic-gate 				return (-1);
228*7c478bd9Sstevel@tonic-gate 		}
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 		while (iswspace(*p))
231*7c478bd9Sstevel@tonic-gate 			p++;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 		/*
234*7c478bd9Sstevel@tonic-gate 		 * If the loop won't end this time (because we
235*7c478bd9Sstevel@tonic-gate 		 * have more stuff to display) put out leading
236*7c478bd9Sstevel@tonic-gate 		 * blanks to align the next line with the previous
237*7c478bd9Sstevel@tonic-gate 		 * lines.
238*7c478bd9Sstevel@tonic-gate 		 */
239*7c478bd9Sstevel@tonic-gate 		if (*p) {
240*7c478bd9Sstevel@tonic-gate 			for (n = 0; n < indent_len + 2; n++)
241*7c478bd9Sstevel@tonic-gate 				(void) fputwc(L' ', stderr);
242*7c478bd9Sstevel@tonic-gate 		}
243*7c478bd9Sstevel@tonic-gate 	}
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	return (1);
246*7c478bd9Sstevel@tonic-gate }
247