2 * "$Id: langprintf.c 11558 2014-02-06 18:33:34Z msweet $"
4 * Localized printf/puts functions for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 2002-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #include "cups-private.h"
26 * '_cupsLangPrintError()' - Print a message followed by a standard error.
30 _cupsLangPrintError(const char *prefix, /* I - Non-localized message prefix */
31 const char *message)/* I - Message */
33 ssize_t bytes; /* Number of bytes formatted */
34 int last_errno; /* Last error */
35 char buffer[2048], /* Message buffer */
36 *bufptr, /* Pointer into buffer */
37 output[8192]; /* Output buffer */
38 _cups_globals_t *cg; /* Global data */
49 * Save the errno value...
55 * Get the message catalog...
60 if (!cg->lang_default)
61 cg->lang_default = cupsLangDefault();
64 * Format the message...
69 snprintf(buffer, sizeof(buffer), "%s:", prefix);
70 bufptr = buffer + strlen(buffer);
75 snprintf(bufptr, sizeof(buffer) - (size_t)(bufptr - buffer),
76 /* TRANSLATORS: Message is "subject: error" */
77 _cupsLangString(cg->lang_default, _("%s: %s")),
78 _cupsLangString(cg->lang_default, message), strerror(last_errno));
79 strlcat(buffer, "\n", sizeof(buffer));
82 * Convert and write to stderr...
85 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
86 cg->lang_default->encoding);
89 fwrite(output, 1, (size_t)bytes, stderr);
94 * '_cupsLangPrintFilter()' - Print a formatted filter message string to a file.
97 int /* O - Number of bytes written */
99 FILE *fp, /* I - File to write to */
100 const char *prefix, /* I - Non-localized message prefix */
101 const char *message, /* I - Message string to use */
102 ...) /* I - Additional arguments as needed */
104 ssize_t bytes; /* Number of bytes formatted */
105 char temp[2048], /* Temporary format buffer */
106 buffer[2048], /* Message buffer */
107 output[8192]; /* Output buffer */
108 va_list ap; /* Pointer to additional arguments */
109 _cups_globals_t *cg; /* Global data */
121 if (!cg->lang_default)
122 cg->lang_default = cupsLangDefault();
125 * Format the string...
128 va_start(ap, message);
129 snprintf(temp, sizeof(temp), "%s: %s\n", prefix,
130 _cupsLangString(cg->lang_default, message));
131 vsnprintf(buffer, sizeof(buffer), temp, ap);
135 * Transcode to the destination charset...
138 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
139 cg->lang_default->encoding);
142 * Write the string and return the number of bytes written...
146 return ((int)fwrite(output, 1, (size_t)bytes, fp));
153 * '_cupsLangPrintf()' - Print a formatted message string to a file.
156 int /* O - Number of bytes written */
157 _cupsLangPrintf(FILE *fp, /* I - File to write to */
158 const char *message, /* I - Message string to use */
159 ...) /* I - Additional arguments as needed */
161 ssize_t bytes; /* Number of bytes formatted */
162 char buffer[2048], /* Message buffer */
163 output[8192]; /* Output buffer */
164 va_list ap; /* Pointer to additional arguments */
165 _cups_globals_t *cg; /* Global data */
177 if (!cg->lang_default)
178 cg->lang_default = cupsLangDefault();
181 * Format the string...
184 va_start(ap, message);
185 vsnprintf(buffer, sizeof(buffer) - 1,
186 _cupsLangString(cg->lang_default, message), ap);
189 strlcat(buffer, "\n", sizeof(buffer));
192 * Transcode to the destination charset...
195 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
196 cg->lang_default->encoding);
199 * Write the string and return the number of bytes written...
203 return ((int)fwrite(output, 1, (size_t)bytes, fp));
210 * '_cupsLangPuts()' - Print a static message string to a file.
213 int /* O - Number of bytes written */
214 _cupsLangPuts(FILE *fp, /* I - File to write to */
215 const char *message) /* I - Message string to use */
217 ssize_t bytes; /* Number of bytes formatted */
218 char output[8192]; /* Message buffer */
219 _cups_globals_t *cg; /* Global data */
231 if (!cg->lang_default)
232 cg->lang_default = cupsLangDefault();
235 * Transcode to the destination charset...
238 bytes = cupsUTF8ToCharset(output,
239 (cups_utf8_t *)_cupsLangString(cg->lang_default,
241 sizeof(output) - 4, cg->lang_default->encoding);
242 bytes += cupsUTF8ToCharset(output + bytes, (cups_utf8_t *)"\n", (int)(sizeof(output) - (size_t)bytes), cg->lang_default->encoding);
245 * Write the string and return the number of bytes written...
249 return ((int)fwrite(output, 1, (size_t)bytes, fp));
256 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
260 _cupsSetLocale(char *argv[]) /* IO - Command-line arguments */
262 int i; /* Looping var */
263 char buffer[8192]; /* Command-line argument buffer */
264 _cups_globals_t *cg; /* Global data */
266 const char *lc_time; /* Current LC_TIME value */
267 char new_lc_time[255], /* New LC_TIME value */
268 *charset; /* Pointer to character set */
273 * Set the locale so that times, etc. are displayed properly.
275 * Unfortunately, while we need the localized time value, we *don't*
276 * want to use the localized charset for the time value, so we need
277 * to set LC_TIME to the locale name with .UTF-8 on the end (if
278 * the locale includes a character set specifier...)
281 setlocale(LC_ALL, "");
284 if ((lc_time = setlocale(LC_TIME, NULL)) == NULL)
285 lc_time = setlocale(LC_ALL, NULL);
289 strlcpy(new_lc_time, lc_time, sizeof(new_lc_time));
290 if ((charset = strchr(new_lc_time, '.')) == NULL)
291 charset = new_lc_time + strlen(new_lc_time);
293 strlcpy(charset, ".UTF-8", sizeof(new_lc_time) - (size_t)(charset - new_lc_time));
296 strlcpy(new_lc_time, "C", sizeof(new_lc_time));
298 setlocale(LC_TIME, new_lc_time);
302 * Initialize the default language info...
307 if (!cg->lang_default)
308 cg->lang_default = cupsLangDefault();
311 * Transcode the command-line arguments from the locale charset to
315 if (cg->lang_default->encoding != CUPS_US_ASCII &&
316 cg->lang_default->encoding != CUPS_UTF8)
318 for (i = 1; argv[i]; i ++)
321 * Try converting from the locale charset to UTF-8...
324 if (cupsCharsetToUTF8((cups_utf8_t *)buffer, argv[i], sizeof(buffer),
325 cg->lang_default->encoding) < 0)
329 * Save the new string if it differs from the original...
332 if (strcmp(buffer, argv[i]))
333 argv[i] = strdup(buffer);
340 * End of "$Id: langprintf.c 11558 2014-02-06 18:33:34Z msweet $".