]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/langprintf.c
2 * Localized printf/puts functions for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 2002-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
24 * '_cupsLangPrintError()' - Print a message followed by a standard error.
28 _cupsLangPrintError(const char *prefix
, /* I - Non-localized message prefix */
29 const char *message
)/* I - Message */
31 ssize_t bytes
; /* Number of bytes formatted */
32 int last_errno
; /* Last error */
33 char buffer
[2048], /* Message buffer */
34 *bufptr
, /* Pointer into buffer */
35 output
[8192]; /* Output buffer */
36 _cups_globals_t
*cg
; /* Global data */
47 * Save the errno value...
53 * Get the message catalog...
58 if (!cg
->lang_default
)
59 cg
->lang_default
= cupsLangDefault();
62 * Format the message...
67 snprintf(buffer
, sizeof(buffer
), "%s:", prefix
);
68 bufptr
= buffer
+ strlen(buffer
);
73 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
),
74 /* TRANSLATORS: Message is "subject: error" */
75 _cupsLangString(cg
->lang_default
, _("%s: %s")),
76 _cupsLangString(cg
->lang_default
, message
), strerror(last_errno
));
77 strlcat(buffer
, "\n", sizeof(buffer
));
80 * Convert and write to stderr...
83 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
84 cg
->lang_default
->encoding
);
87 fwrite(output
, 1, (size_t)bytes
, stderr
);
92 * '_cupsLangPrintFilter()' - Print a formatted filter message string to a file.
95 int /* O - Number of bytes written */
97 FILE *fp
, /* I - File to write to */
98 const char *prefix
, /* I - Non-localized message prefix */
99 const char *message
, /* I - Message string to use */
100 ...) /* I - Additional arguments as needed */
102 ssize_t bytes
; /* Number of bytes formatted */
103 char temp
[2048], /* Temporary format buffer */
104 buffer
[2048], /* Message buffer */
105 output
[8192]; /* Output buffer */
106 va_list ap
; /* Pointer to additional arguments */
107 _cups_globals_t
*cg
; /* Global data */
119 if (!cg
->lang_default
)
120 cg
->lang_default
= cupsLangDefault();
123 * Format the string...
126 va_start(ap
, message
);
127 snprintf(temp
, sizeof(temp
), "%s: %s\n", prefix
,
128 _cupsLangString(cg
->lang_default
, message
));
129 vsnprintf(buffer
, sizeof(buffer
), temp
, ap
);
133 * Transcode to the destination charset...
136 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
137 cg
->lang_default
->encoding
);
140 * Write the string and return the number of bytes written...
144 return ((int)fwrite(output
, 1, (size_t)bytes
, fp
));
151 * '_cupsLangPrintf()' - Print a formatted message string to a file.
154 int /* O - Number of bytes written */
155 _cupsLangPrintf(FILE *fp
, /* I - File to write to */
156 const char *message
, /* I - Message string to use */
157 ...) /* I - Additional arguments as needed */
159 ssize_t bytes
; /* Number of bytes formatted */
160 char buffer
[2048], /* Message buffer */
161 output
[8192]; /* Output buffer */
162 va_list ap
; /* Pointer to additional arguments */
163 _cups_globals_t
*cg
; /* Global data */
175 if (!cg
->lang_default
)
176 cg
->lang_default
= cupsLangDefault();
179 * Format the string...
182 va_start(ap
, message
);
183 vsnprintf(buffer
, sizeof(buffer
) - 1,
184 _cupsLangString(cg
->lang_default
, message
), ap
);
187 strlcat(buffer
, "\n", sizeof(buffer
));
190 * Transcode to the destination charset...
193 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
194 cg
->lang_default
->encoding
);
197 * Write the string and return the number of bytes written...
201 return ((int)fwrite(output
, 1, (size_t)bytes
, fp
));
208 * '_cupsLangPuts()' - Print a static message string to a file.
211 int /* O - Number of bytes written */
212 _cupsLangPuts(FILE *fp
, /* I - File to write to */
213 const char *message
) /* I - Message string to use */
215 ssize_t bytes
; /* Number of bytes formatted */
216 char output
[8192]; /* Message buffer */
217 _cups_globals_t
*cg
; /* Global data */
229 if (!cg
->lang_default
)
230 cg
->lang_default
= cupsLangDefault();
233 * Transcode to the destination charset...
236 bytes
= cupsUTF8ToCharset(output
,
237 (cups_utf8_t
*)_cupsLangString(cg
->lang_default
,
239 sizeof(output
) - 4, cg
->lang_default
->encoding
);
240 bytes
+= cupsUTF8ToCharset(output
+ bytes
, (cups_utf8_t
*)"\n", (int)(sizeof(output
) - (size_t)bytes
), cg
->lang_default
->encoding
);
243 * Write the string and return the number of bytes written...
247 return ((int)fwrite(output
, 1, (size_t)bytes
, fp
));
254 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
258 _cupsSetLocale(char *argv
[]) /* IO - Command-line arguments */
260 int i
; /* Looping var */
261 char buffer
[8192]; /* Command-line argument buffer */
262 _cups_globals_t
*cg
; /* Global data */
264 const char *lc_time
; /* Current LC_TIME value */
265 char new_lc_time
[255], /* New LC_TIME value */
266 *charset
; /* Pointer to character set */
271 * Set the locale so that times, etc. are displayed properly.
273 * Unfortunately, while we need the localized time value, we *don't*
274 * want to use the localized charset for the time value, so we need
275 * to set LC_TIME to the locale name with .UTF-8 on the end (if
276 * the locale includes a character set specifier...)
279 setlocale(LC_ALL
, "");
282 if ((lc_time
= setlocale(LC_TIME
, NULL
)) == NULL
)
283 lc_time
= setlocale(LC_ALL
, NULL
);
287 strlcpy(new_lc_time
, lc_time
, sizeof(new_lc_time
));
288 if ((charset
= strchr(new_lc_time
, '.')) == NULL
)
289 charset
= new_lc_time
+ strlen(new_lc_time
);
291 strlcpy(charset
, ".UTF-8", sizeof(new_lc_time
) - (size_t)(charset
- new_lc_time
));
294 strlcpy(new_lc_time
, "C", sizeof(new_lc_time
));
296 setlocale(LC_TIME
, new_lc_time
);
300 * Initialize the default language info...
305 if (!cg
->lang_default
)
306 cg
->lang_default
= cupsLangDefault();
309 * Transcode the command-line arguments from the locale charset to
313 if (cg
->lang_default
->encoding
!= CUPS_US_ASCII
&&
314 cg
->lang_default
->encoding
!= CUPS_UTF8
)
316 for (i
= 1; argv
[i
]; i
++)
319 * Try converting from the locale charset to UTF-8...
322 if (cupsCharsetToUTF8((cups_utf8_t
*)buffer
, argv
[i
], sizeof(buffer
),
323 cg
->lang_default
->encoding
) < 0)
327 * Save the new string if it differs from the original...
330 if (strcmp(buffer
, argv
[i
]))
331 argv
[i
] = strdup(buffer
);