]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/dest-localization.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / cups / dest-localization.c
1 /*
2 * "$Id$"
3 *
4 * Destination localization support for CUPS.
5 *
6 * Copyright 2012-2014 by Apple Inc.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 */
16
17 /*
18 * Include necessary headers...
19 */
20
21 #include "cups-private.h"
22
23
24 /*
25 * Local functions...
26 */
27
28 static void cups_create_localizations(http_t *http, cups_dinfo_t *dinfo);
29 static int cups_read_strings(cups_file_t *fp, char *buffer, size_t bufsize,
30 char **id, char **str);
31 static char *cups_scan_strings(char *buffer);
32
33
34 /*
35 * 'cupsLocalizeDestOption()' - Get the localized string for a destination
36 * option.
37 *
38 * The returned string is stored in the destination information and will become
39 * invalid if the destination information is deleted.
40 *
41 * @since CUPS 1.6/OS X 10.8@
42 */
43
44 const char * /* O - Localized string */
45 cupsLocalizeDestOption(
46 http_t *http, /* I - Connection to destination */
47 cups_dest_t *dest, /* I - Destination */
48 cups_dinfo_t *dinfo, /* I - Destination information */
49 const char *option) /* I - Option to localize */
50 {
51 _cups_message_t key, /* Search key */
52 *match; /* Matching entry */
53
54
55 if (!http || !dest || !dinfo)
56 return (option);
57
58 if (!dinfo->localizations)
59 cups_create_localizations(http, dinfo);
60
61 if (cupsArrayCount(dinfo->localizations) == 0)
62 return (option);
63
64 key.id = (char *)option;
65 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations,
66 &key)) != NULL)
67 return (match->str);
68 else
69 return (option);
70 }
71
72
73 /*
74 * 'cupsLocalizeDestValue()' - Get the localized string for a destination
75 * option+value pair.
76 *
77 * The returned string is stored in the destination information and will become
78 * invalid if the destination information is deleted.
79 *
80 * @since CUPS 1.6/OS X 10.8@
81 */
82
83 const char * /* O - Localized string */
84 cupsLocalizeDestValue(
85 http_t *http, /* I - Connection to destination */
86 cups_dest_t *dest, /* I - Destination */
87 cups_dinfo_t *dinfo, /* I - Destination information */
88 const char *option, /* I - Option to localize */
89 const char *value) /* I - Value to localize */
90 {
91 _cups_message_t key, /* Search key */
92 *match; /* Matching entry */
93 char pair[256]; /* option.value pair */
94
95
96 if (!http || !dest || !dinfo)
97 return (value);
98
99 if (!dinfo->localizations)
100 cups_create_localizations(http, dinfo);
101
102 if (cupsArrayCount(dinfo->localizations) == 0)
103 return (value);
104
105 snprintf(pair, sizeof(pair), "%s.%s", option, value);
106 key.id = pair;
107 if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations,
108 &key)) != NULL)
109 return (match->str);
110 else
111 return (value);
112 }
113
114
115 /*
116 * 'cups_create_localizations()' - Create the localizations array for a
117 * destination.
118 */
119
120 static void
121 cups_create_localizations(
122 http_t *http, /* I - Connection to destination */
123 cups_dinfo_t *dinfo) /* I - Destination informations */
124 {
125 http_t *http2; /* Connection for strings file */
126 http_status_t status; /* Request status */
127 ipp_attribute_t *attr; /* "printer-strings-uri" attribute */
128 char scheme[32], /* URI scheme */
129 userpass[256], /* Username/password info */
130 hostname[256], /* Hostname */
131 resource[1024], /* Resource */
132 http_hostname[256],
133 /* Hostname of connection */
134 tempfile[1024]; /* Temporary filename */
135 int port; /* Port number */
136 http_encryption_t encryption; /* Encryption to use */
137 cups_file_t *temp; /* Temporary file */
138
139
140 /*
141 * Create an empty message catalog...
142 */
143
144 dinfo->localizations = _cupsMessageNew(NULL);
145
146 /*
147 * See if there are any localizations...
148 */
149
150 if ((attr = ippFindAttribute(dinfo->attrs, "printer-strings-uri",
151 IPP_TAG_URI)) == NULL)
152 {
153 /*
154 * Nope...
155 */
156
157 DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) "
158 "value.");
159 return; /* Nope */
160 }
161
162 /*
163 * Pull apart the URI and determine whether we need to try a different
164 * server...
165 */
166
167 if (httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text,
168 scheme, sizeof(scheme), userpass, sizeof(userpass),
169 hostname, sizeof(hostname), &port, resource,
170 sizeof(resource)) < HTTP_URI_STATUS_OK)
171 {
172 DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value "
173 "\"%s\".", attr->values[0].string.text));
174 return;
175 }
176
177 httpGetHostname(http, http_hostname, sizeof(http_hostname));
178
179 if (!_cups_strcasecmp(http_hostname, hostname) &&
180 port == httpAddrPort(http->hostaddr))
181 {
182 /*
183 * Use the same connection...
184 */
185
186 http2 = http;
187 }
188 else
189 {
190 /*
191 * Connect to the alternate host...
192 */
193
194 if (!strcmp(scheme, "https"))
195 encryption = HTTP_ENCRYPTION_ALWAYS;
196 else
197 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
198
199 if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1,
200 30000, NULL)) == NULL)
201 {
202 DEBUG_printf(("4cups_create_localizations: Unable to connect to "
203 "%s:%d: %s", hostname, port, cupsLastErrorString()));
204 return;
205 }
206 }
207
208 /*
209 * Get a temporary file...
210 */
211
212 if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
213 {
214 DEBUG_printf(("4cups_create_localizations: Unable to create temporary "
215 "file: %s", cupsLastErrorString()));
216 if (http2 != http)
217 httpClose(http2);
218 return;
219 }
220
221 status = cupsGetFd(http2, resource, cupsFileNumber(temp));
222
223 DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource,
224 httpStatus(status)));
225
226 if (status == HTTP_STATUS_OK)
227 {
228 /*
229 * Got the file, read it...
230 */
231
232 char buffer[8192], /* Message buffer */
233 *id, /* ID string */
234 *str; /* Translated message */
235 _cups_message_t *m; /* Current message */
236
237 lseek(cupsFileNumber(temp), 0, SEEK_SET);
238
239 while (cups_read_strings(temp, buffer, sizeof(buffer), &id, &str))
240 {
241 if ((m = malloc(sizeof(_cups_message_t))) == NULL)
242 break;
243
244 m->id = strdup(id);
245 m->str = strdup(str);
246
247 if (m->id && m->str)
248 cupsArrayAdd(dinfo->localizations, m);
249 else
250 {
251 if (m->id)
252 free(m->id);
253
254 if (m->str)
255 free(m->str);
256
257 free(m);
258 break;
259 }
260 }
261 }
262
263 DEBUG_printf(("4cups_create_localizations: %d messages loaded.",
264 cupsArrayCount(dinfo->localizations)));
265
266 /*
267 * Cleanup...
268 */
269
270 unlink(tempfile);
271 cupsFileClose(temp);
272
273 if (http2 != http)
274 httpClose(http2);
275 }
276
277
278 /*
279 * 'cups_read_strings()' - Read a pair of strings from a .strings file.
280 */
281
282 static int /* O - 1 on success, 0 on failure */
283 cups_read_strings(cups_file_t *strings, /* I - .strings file */
284 char *buffer, /* I - Line buffer */
285 size_t bufsize, /* I - Size of line buffer */
286 char **id, /* O - Pointer to ID string */
287 char **str) /* O - Pointer to translation string */
288 {
289 char *bufptr; /* Pointer into buffer */
290
291
292 while (cupsFileGets(strings, buffer, bufsize))
293 {
294 if (buffer[0] != '\"')
295 continue;
296
297 *id = buffer + 1;
298 bufptr = cups_scan_strings(buffer);
299
300 if (*bufptr != '\"')
301 continue;
302
303 *bufptr++ = '\0';
304
305 while (*bufptr && *bufptr != '\"')
306 bufptr ++;
307
308 if (!*bufptr)
309 continue;
310
311 *str = bufptr + 1;
312 bufptr = cups_scan_strings(bufptr);
313
314 if (*bufptr != '\"')
315 continue;
316
317 *bufptr = '\0';
318
319 return (1);
320 }
321
322 return (0);
323 }
324
325
326 /*
327 * 'cups_scan_strings()' - Scan a quoted string.
328 */
329
330 static char * /* O - End of string */
331 cups_scan_strings(char *buffer) /* I - Start of string */
332 {
333 char *bufptr; /* Pointer into string */
334
335
336 for (bufptr = buffer + 1; *bufptr && *bufptr != '\"'; bufptr ++)
337 {
338 if (*bufptr == '\\')
339 {
340 if (bufptr[1] >= '0' && bufptr[1] <= '3' &&
341 bufptr[2] >= '0' && bufptr[2] <= '7' &&
342 bufptr[3] >= '0' && bufptr[3] <= '7')
343 {
344 /*
345 * Decode \nnn octal escape...
346 */
347
348 *bufptr = (char)(((((bufptr[1] - '0') << 3) | (bufptr[2] - '0')) << 3) | (bufptr[3] - '0'));
349 _cups_strcpy(bufptr + 1, bufptr + 4);
350 }
351 else
352 {
353 /*
354 * Decode \C escape...
355 */
356
357 _cups_strcpy(bufptr, bufptr + 1);
358 if (*bufptr == 'n')
359 *bufptr = '\n';
360 else if (*bufptr == 'r')
361 *bufptr = '\r';
362 else if (*bufptr == 't')
363 *bufptr = '\t';
364 }
365 }
366 }
367
368 return (bufptr);
369 }
370
371
372
373 /*
374 * End of "$Id$".
375 */