2 * Destination localization support for CUPS.
4 * Copyright 2012-2014 by Apple Inc.
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright
8 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
9 * which should have been included with this file. If this file is
10 * file is missing or damaged, see the license at "http://www.cups.org/".
12 * This file is subject to the Apple OS-Developed Software exception.
16 * Include necessary headers...
19 #include "cups-private.h"
26 static void cups_create_localizations(http_t
*http
, cups_dinfo_t
*dinfo
);
27 static int cups_read_strings(cups_file_t
*fp
, char *buffer
, size_t bufsize
,
28 char **id
, char **str
);
29 static char *cups_scan_strings(char *buffer
);
33 * 'cupsLocalizeDestMedia()' - Get the localized string for a destination media
36 * The returned string is stored in the destination information and will become
37 * invalid if the destination information is deleted.
39 * @since CUPS 2.0/macOS 10.10@
42 const char * /* O - Localized string */
43 cupsLocalizeDestMedia(
44 http_t
*http
, /* I - Connection to destination */
45 cups_dest_t
*dest
, /* I - Destination */
46 cups_dinfo_t
*dinfo
, /* I - Destination information */
47 unsigned flags
, /* I - Media flags */
48 cups_size_t
*size
) /* I - Media size */
50 cups_lang_t
*lang
; /* Standard localizations */
51 _cups_message_t key
, /* Search key */
52 *match
; /* Matching entry */
53 pwg_media_t
*pwg
; /* PWG media information */
54 cups_array_t
*db
; /* Media database */
55 _cups_media_db_t
*mdb
; /* Media database entry */
56 char name
[1024], /* Size name */
57 temp
[256]; /* Temporary string */
58 const char *lsize
, /* Localized media size */
59 *lsource
, /* Localized media source */
60 *ltype
; /* Localized media type */
64 * Range check input...
67 if (!http
|| !dest
|| !dinfo
|| !size
)
69 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
74 * See if the localization is cached...
77 if (!dinfo
->localizations
)
78 cups_create_localizations(http
, dinfo
);
81 if ((match
= (_cups_message_t
*)cupsArrayFind(dinfo
->localizations
, &key
)) != NULL
)
85 * If not, get the localized size, source, and type strings...
88 lang
= cupsLangDefault();
89 pwg
= pwgMediaForSize(size
->width
, size
->length
);
92 lsize
= _cupsLangString(lang
, pwg
->ppd
);
98 if ((size
->width
% 635) == 0 && (size
->length
% 635) == 0)
101 * Use inches since the size is a multiple of 1/4 inch.
104 snprintf(temp
, sizeof(temp
), _cupsLangString(lang
, _("%g x %g")), size
->width
/ 2540.0, size
->length
/ 2540.0);
109 * Use millimeters since the size is not a multiple of 1/4 inch.
112 snprintf(temp
, sizeof(temp
), _cupsLangString(lang
, _("%d x %d mm")), (size
->width
+ 50) / 100, (size
->length
+ 50) / 100);
118 if (flags
& CUPS_MEDIA_FLAGS_READY
)
119 db
= dinfo
->ready_db
;
121 db
= dinfo
->media_db
;
123 DEBUG_printf(("1cupsLocalizeDestMedia: size->media=\"%s\"", size
->media
));
125 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
127 if (mdb
->key
&& !strcmp(mdb
->key
, size
->media
))
129 else if (mdb
->size_name
&& !strcmp(mdb
->size_name
, size
->media
))
135 for (mdb
= (_cups_media_db_t
*)cupsArrayFirst(db
); mdb
; mdb
= (_cups_media_db_t
*)cupsArrayNext(db
))
137 if (mdb
->width
== size
->width
&& mdb
->length
== size
->length
&& mdb
->bottom
== size
->bottom
&& mdb
->left
== size
->left
&& mdb
->right
== size
->right
&& mdb
->top
== size
->top
)
144 DEBUG_printf(("1cupsLocalizeDestMedia: MATCH mdb%p [key=\"%s\" size_name=\"%s\" source=\"%s\" type=\"%s\" width=%d length=%d B%d L%d R%d T%d]", (void *)mdb
, mdb
->key
, mdb
->size_name
, mdb
->source
, mdb
->type
, mdb
->width
, mdb
->length
, mdb
->bottom
, mdb
->left
, mdb
->right
, mdb
->top
));
146 lsource
= cupsLocalizeDestValue(http
, dest
, dinfo
, "media-source", mdb
->source
);
147 ltype
= cupsLocalizeDestValue(http
, dest
, dinfo
, "media-type", mdb
->type
);
155 if (!lsource
&& !ltype
)
157 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
158 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless)")), lsize
);
160 strlcpy(name
, lsize
, sizeof(name
));
164 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
165 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless, %s)")), lsize
, ltype
);
167 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (%s)")), lsize
, ltype
);
171 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
172 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless, %s)")), lsize
, lsource
);
174 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (%s)")), lsize
, lsource
);
178 if (size
->bottom
|| size
->left
|| size
->right
|| size
->top
)
179 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (Borderless, %s, %s)")), lsize
, ltype
, lsource
);
181 snprintf(name
, sizeof(name
), _cupsLangString(lang
, _("%s (%s, %s)")), lsize
, ltype
, lsource
);
184 if ((match
= (_cups_message_t
*)calloc(1, sizeof(_cups_message_t
))) == NULL
)
187 match
->id
= strdup(size
->media
);
188 match
->str
= strdup(name
);
190 cupsArrayAdd(dinfo
->localizations
, match
);
197 * 'cupsLocalizeDestOption()' - Get the localized string for a destination
200 * The returned string is stored in the destination information and will become
201 * invalid if the destination information is deleted.
203 * @since CUPS 1.6/macOS 10.8@
206 const char * /* O - Localized string */
207 cupsLocalizeDestOption(
208 http_t
*http
, /* I - Connection to destination */
209 cups_dest_t
*dest
, /* I - Destination */
210 cups_dinfo_t
*dinfo
, /* I - Destination information */
211 const char *option
) /* I - Option to localize */
213 _cups_message_t key
, /* Search key */
214 *match
; /* Matching entry */
217 if (!http
|| !dest
|| !dinfo
)
220 if (!dinfo
->localizations
)
221 cups_create_localizations(http
, dinfo
);
223 if (cupsArrayCount(dinfo
->localizations
) == 0)
226 key
.id
= (char *)option
;
227 if ((match
= (_cups_message_t
*)cupsArrayFind(dinfo
->localizations
,
236 * 'cupsLocalizeDestValue()' - Get the localized string for a destination
239 * The returned string is stored in the destination information and will become
240 * invalid if the destination information is deleted.
242 * @since CUPS 1.6/macOS 10.8@
245 const char * /* O - Localized string */
246 cupsLocalizeDestValue(
247 http_t
*http
, /* I - Connection to destination */
248 cups_dest_t
*dest
, /* I - Destination */
249 cups_dinfo_t
*dinfo
, /* I - Destination information */
250 const char *option
, /* I - Option to localize */
251 const char *value
) /* I - Value to localize */
253 _cups_message_t key
, /* Search key */
254 *match
; /* Matching entry */
255 char pair
[256]; /* option.value pair */
258 if (!http
|| !dest
|| !dinfo
)
261 if (!dinfo
->localizations
)
262 cups_create_localizations(http
, dinfo
);
264 if (cupsArrayCount(dinfo
->localizations
) == 0)
267 snprintf(pair
, sizeof(pair
), "%s.%s", option
, value
);
269 if ((match
= (_cups_message_t
*)cupsArrayFind(dinfo
->localizations
,
278 * 'cups_create_localizations()' - Create the localizations array for a
283 cups_create_localizations(
284 http_t
*http
, /* I - Connection to destination */
285 cups_dinfo_t
*dinfo
) /* I - Destination informations */
287 http_t
*http2
; /* Connection for strings file */
288 http_status_t status
; /* Request status */
289 ipp_attribute_t
*attr
; /* "printer-strings-uri" attribute */
290 char scheme
[32], /* URI scheme */
291 userpass
[256], /* Username/password info */
292 hostname
[256], /* Hostname */
293 resource
[1024], /* Resource */
295 /* Hostname of connection */
296 tempfile
[1024]; /* Temporary filename */
297 int port
; /* Port number */
298 http_encryption_t encryption
; /* Encryption to use */
299 cups_file_t
*temp
; /* Temporary file */
303 * Create an empty message catalog...
306 dinfo
->localizations
= _cupsMessageNew(NULL
);
309 * See if there are any localizations...
312 if ((attr
= ippFindAttribute(dinfo
->attrs
, "printer-strings-uri",
313 IPP_TAG_URI
)) == NULL
)
319 DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) "
325 * Pull apart the URI and determine whether we need to try a different
329 if (httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
,
330 scheme
, sizeof(scheme
), userpass
, sizeof(userpass
),
331 hostname
, sizeof(hostname
), &port
, resource
,
332 sizeof(resource
)) < HTTP_URI_STATUS_OK
)
334 DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value "
335 "\"%s\".", attr
->values
[0].string
.text
));
339 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
341 if (!_cups_strcasecmp(http_hostname
, hostname
) &&
342 port
== httpAddrPort(http
->hostaddr
))
345 * Use the same connection...
353 * Connect to the alternate host...
356 if (!strcmp(scheme
, "https"))
357 encryption
= HTTP_ENCRYPTION_ALWAYS
;
359 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
361 if ((http2
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
, encryption
, 1,
362 30000, NULL
)) == NULL
)
364 DEBUG_printf(("4cups_create_localizations: Unable to connect to "
365 "%s:%d: %s", hostname
, port
, cupsLastErrorString()));
371 * Get a temporary file...
374 if ((temp
= cupsTempFile2(tempfile
, sizeof(tempfile
))) == NULL
)
376 DEBUG_printf(("4cups_create_localizations: Unable to create temporary "
377 "file: %s", cupsLastErrorString()));
383 status
= cupsGetFd(http2
, resource
, cupsFileNumber(temp
));
385 DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource
,
386 httpStatus(status
)));
388 if (status
== HTTP_STATUS_OK
)
391 * Got the file, read it...
394 char buffer
[8192], /* Message buffer */
396 *str
; /* Translated message */
397 _cups_message_t
*m
; /* Current message */
399 lseek(cupsFileNumber(temp
), 0, SEEK_SET
);
401 while (cups_read_strings(temp
, buffer
, sizeof(buffer
), &id
, &str
))
403 if ((m
= malloc(sizeof(_cups_message_t
))) == NULL
)
407 m
->str
= strdup(str
);
410 cupsArrayAdd(dinfo
->localizations
, m
);
425 DEBUG_printf(("4cups_create_localizations: %d messages loaded.",
426 cupsArrayCount(dinfo
->localizations
)));
441 * 'cups_read_strings()' - Read a pair of strings from a .strings file.
444 static int /* O - 1 on success, 0 on failure */
445 cups_read_strings(cups_file_t
*strings
, /* I - .strings file */
446 char *buffer
, /* I - Line buffer */
447 size_t bufsize
, /* I - Size of line buffer */
448 char **id
, /* O - Pointer to ID string */
449 char **str
) /* O - Pointer to translation string */
451 char *bufptr
; /* Pointer into buffer */
454 while (cupsFileGets(strings
, buffer
, bufsize
))
456 if (buffer
[0] != '\"')
460 bufptr
= cups_scan_strings(buffer
);
467 while (*bufptr
&& *bufptr
!= '\"')
474 bufptr
= cups_scan_strings(bufptr
);
489 * 'cups_scan_strings()' - Scan a quoted string.
492 static char * /* O - End of string */
493 cups_scan_strings(char *buffer
) /* I - Start of string */
495 char *bufptr
; /* Pointer into string */
498 for (bufptr
= buffer
+ 1; *bufptr
&& *bufptr
!= '\"'; bufptr
++)
502 if (bufptr
[1] >= '0' && bufptr
[1] <= '3' &&
503 bufptr
[2] >= '0' && bufptr
[2] <= '7' &&
504 bufptr
[3] >= '0' && bufptr
[3] <= '7')
507 * Decode \nnn octal escape...
510 *bufptr
= (char)(((((bufptr
[1] - '0') << 3) | (bufptr
[2] - '0')) << 3) | (bufptr
[3] - '0'));
511 _cups_strcpy(bufptr
+ 1, bufptr
+ 4);
516 * Decode \C escape...
519 _cups_strcpy(bufptr
, bufptr
+ 1);
522 else if (*bufptr
== 'r')
524 else if (*bufptr
== 't')