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