]>
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 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
15 #include "debug-internal.h"
19 * '_cupsLangPrintError()' - Print a message followed by a standard error.
23 _cupsLangPrintError(const char *prefix
, /* I - Non-localized message prefix */
24 const char *message
)/* I - Message */
26 ssize_t bytes
; /* Number of bytes formatted */
27 int last_errno
; /* Last error */
28 char buffer
[2048], /* Message buffer */
29 *bufptr
, /* Pointer into buffer */
30 output
[8192]; /* Output buffer */
31 _cups_globals_t
*cg
; /* Global data */
42 * Save the errno value...
48 * Get the message catalog...
53 if (!cg
->lang_default
)
54 cg
->lang_default
= cupsLangDefault();
57 * Format the message...
62 snprintf(buffer
, sizeof(buffer
), "%s:", prefix
);
63 bufptr
= buffer
+ strlen(buffer
);
68 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
),
69 /* TRANSLATORS: Message is "subject: error" */
70 _cupsLangString(cg
->lang_default
, _("%s: %s")),
71 _cupsLangString(cg
->lang_default
, message
), strerror(last_errno
));
72 strlcat(buffer
, "\n", sizeof(buffer
));
75 * Convert and write to stderr...
78 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
79 cg
->lang_default
->encoding
);
82 fwrite(output
, 1, (size_t)bytes
, stderr
);
87 * '_cupsLangPrintFilter()' - Print a formatted filter message string to a file.
90 int /* O - Number of bytes written */
92 FILE *fp
, /* I - File to write to */
93 const char *prefix
, /* I - Non-localized message prefix */
94 const char *message
, /* I - Message string to use */
95 ...) /* I - Additional arguments as needed */
97 ssize_t bytes
; /* Number of bytes formatted */
98 char temp
[2048], /* Temporary format buffer */
99 buffer
[2048], /* Message buffer */
100 output
[8192]; /* Output buffer */
101 va_list ap
; /* Pointer to additional arguments */
102 _cups_globals_t
*cg
; /* Global data */
114 if (!cg
->lang_default
)
115 cg
->lang_default
= cupsLangDefault();
118 * Format the string...
121 va_start(ap
, message
);
122 snprintf(temp
, sizeof(temp
), "%s: %s\n", prefix
,
123 _cupsLangString(cg
->lang_default
, message
));
124 vsnprintf(buffer
, sizeof(buffer
), temp
, ap
);
128 * Transcode to the destination charset...
131 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
132 cg
->lang_default
->encoding
);
135 * Write the string and return the number of bytes written...
139 return ((int)fwrite(output
, 1, (size_t)bytes
, fp
));
146 * '_cupsLangPrintf()' - Print a formatted message string to a file.
149 int /* O - Number of bytes written */
150 _cupsLangPrintf(FILE *fp
, /* I - File to write to */
151 const char *message
, /* I - Message string to use */
152 ...) /* I - Additional arguments as needed */
154 ssize_t bytes
; /* Number of bytes formatted */
155 char buffer
[2048], /* Message buffer */
156 output
[8192]; /* Output buffer */
157 va_list ap
; /* Pointer to additional arguments */
158 _cups_globals_t
*cg
; /* Global data */
170 if (!cg
->lang_default
)
171 cg
->lang_default
= cupsLangDefault();
174 * Format the string...
177 va_start(ap
, message
);
178 vsnprintf(buffer
, sizeof(buffer
) - 1,
179 _cupsLangString(cg
->lang_default
, message
), ap
);
182 strlcat(buffer
, "\n", sizeof(buffer
));
185 * Transcode to the destination charset...
188 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
189 cg
->lang_default
->encoding
);
192 * Write the string and return the number of bytes written...
196 return ((int)fwrite(output
, 1, (size_t)bytes
, fp
));
203 * '_cupsLangPuts()' - Print a static message string to a file.
206 int /* O - Number of bytes written */
207 _cupsLangPuts(FILE *fp
, /* I - File to write to */
208 const char *message
) /* I - Message string to use */
210 ssize_t bytes
; /* Number of bytes formatted */
211 char output
[8192]; /* Message buffer */
212 _cups_globals_t
*cg
; /* Global data */
224 if (!cg
->lang_default
)
225 cg
->lang_default
= cupsLangDefault();
228 * Transcode to the destination charset...
231 bytes
= cupsUTF8ToCharset(output
,
232 (cups_utf8_t
*)_cupsLangString(cg
->lang_default
,
234 sizeof(output
) - 4, cg
->lang_default
->encoding
);
235 bytes
+= cupsUTF8ToCharset(output
+ bytes
, (cups_utf8_t
*)"\n", (int)(sizeof(output
) - (size_t)bytes
), cg
->lang_default
->encoding
);
238 * Write the string and return the number of bytes written...
242 return ((int)fwrite(output
, 1, (size_t)bytes
, fp
));
249 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
253 _cupsSetLocale(char *argv
[]) /* IO - Command-line arguments */
255 int i
; /* Looping var */
256 char buffer
[8192]; /* Command-line argument buffer */
257 _cups_globals_t
*cg
; /* Global data */
259 const char *lc_time
; /* Current LC_TIME value */
260 char new_lc_time
[255], /* New LC_TIME value */
261 *charset
; /* Pointer to character set */
266 * Set the locale so that times, etc. are displayed properly.
268 * Unfortunately, while we need the localized time value, we *don't*
269 * want to use the localized charset for the time value, so we need
270 * to set LC_TIME to the locale name with .UTF-8 on the end (if
271 * the locale includes a character set specifier...)
274 setlocale(LC_ALL
, "");
277 if ((lc_time
= setlocale(LC_TIME
, NULL
)) == NULL
)
278 lc_time
= setlocale(LC_ALL
, NULL
);
282 strlcpy(new_lc_time
, lc_time
, sizeof(new_lc_time
));
283 if ((charset
= strchr(new_lc_time
, '.')) == NULL
)
284 charset
= new_lc_time
+ strlen(new_lc_time
);
286 strlcpy(charset
, ".UTF-8", sizeof(new_lc_time
) - (size_t)(charset
- new_lc_time
));
289 strlcpy(new_lc_time
, "C", sizeof(new_lc_time
));
291 setlocale(LC_TIME
, new_lc_time
);
295 * Initialize the default language info...
300 if (!cg
->lang_default
)
301 cg
->lang_default
= cupsLangDefault();
304 * Transcode the command-line arguments from the locale charset to
308 if (cg
->lang_default
->encoding
!= CUPS_US_ASCII
&&
309 cg
->lang_default
->encoding
!= CUPS_UTF8
)
311 for (i
= 1; argv
[i
]; i
++)
314 * Try converting from the locale charset to UTF-8...
317 if (cupsCharsetToUTF8((cups_utf8_t
*)buffer
, argv
[i
], sizeof(buffer
),
318 cg
->lang_default
->encoding
) < 0)
322 * Save the new string if it differs from the original...
325 if (strcmp(buffer
, argv
[i
]))
326 argv
[i
] = strdup(buffer
);