dest-localization.c [plain text]
#include "cups-private.h"
static void cups_create_localizations(http_t *http, cups_dinfo_t *dinfo);
static int cups_read_strings(cups_file_t *fp, char *buffer, size_t bufsize,
char **id, char **str);
static char *cups_scan_strings(char *buffer);
const char *
cupsLocalizeDestOption(
http_t *http,
cups_dest_t *dest,
cups_dinfo_t *dinfo,
const char *option)
{
_cups_message_t key,
*match;
if (!http || !dest || !dinfo)
return (option);
if (!dinfo->localizations)
cups_create_localizations(http, dinfo);
if (cupsArrayCount(dinfo->localizations) == 0)
return (option);
key.id = (char *)option;
if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations,
&key)) != NULL)
return (match->str);
else
return (option);
}
const char *
cupsLocalizeDestValue(
http_t *http,
cups_dest_t *dest,
cups_dinfo_t *dinfo,
const char *option,
const char *value)
{
_cups_message_t key,
*match;
char pair[256];
if (!http || !dest || !dinfo)
return (value);
if (!dinfo->localizations)
cups_create_localizations(http, dinfo);
if (cupsArrayCount(dinfo->localizations) == 0)
return (value);
snprintf(pair, sizeof(pair), "%s.%s", option, value);
key.id = pair;
if ((match = (_cups_message_t *)cupsArrayFind(dinfo->localizations,
&key)) != NULL)
return (match->str);
else
return (value);
}
static void
cups_create_localizations(
http_t *http,
cups_dinfo_t *dinfo)
{
http_t *http2;
http_status_t status;
ipp_attribute_t *attr;
char scheme[32],
userpass[256],
hostname[256],
resource[1024],
http_hostname[256],
tempfile[1024];
int port;
http_encryption_t encryption;
cups_file_t *temp;
dinfo->localizations = _cupsMessageNew(NULL);
if ((attr = ippFindAttribute(dinfo->attrs, "printer-strings-uri",
IPP_TAG_URI)) == NULL)
{
DEBUG_puts("4cups_create_localizations: No printer-strings-uri (uri) "
"value.");
return;
}
if (httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text,
scheme, sizeof(scheme), userpass, sizeof(userpass),
hostname, sizeof(hostname), &port, resource,
sizeof(resource)) < HTTP_URI_STATUS_OK)
{
DEBUG_printf(("4cups_create_localizations: Bad printer-strings-uri value "
"\"%s\".", attr->values[0].string.text));
return;
}
httpGetHostname(http, http_hostname, sizeof(http_hostname));
if (!_cups_strcasecmp(http_hostname, hostname) &&
port == httpAddrPort(http->hostaddr))
{
http2 = http;
}
else
{
if (!strcmp(scheme, "https"))
encryption = HTTP_ENCRYPTION_ALWAYS;
else
encryption = HTTP_ENCRYPTION_IF_REQUESTED;
if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC, encryption, 1,
30000, NULL)) == NULL)
{
DEBUG_printf(("4cups_create_localizations: Unable to connect to "
"%s:%d: %s", hostname, port, cupsLastErrorString()));
return;
}
}
if ((temp = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
{
DEBUG_printf(("4cups_create_localizations: Unable to create temporary "
"file: %s", cupsLastErrorString()));
if (http2 != http)
httpClose(http2);
return;
}
status = cupsGetFd(http2, resource, cupsFileNumber(temp));
DEBUG_printf(("4cups_create_localizations: GET %s = %s", resource,
httpStatus(status)));
if (status == HTTP_STATUS_OK)
{
char buffer[8192],
*id,
*str;
_cups_message_t *m;
lseek(cupsFileNumber(temp), 0, SEEK_SET);
while (cups_read_strings(temp, buffer, sizeof(buffer), &id, &str))
{
if ((m = malloc(sizeof(_cups_message_t))) == NULL)
break;
m->id = strdup(id);
m->str = strdup(str);
if (m->id && m->str)
cupsArrayAdd(dinfo->localizations, m);
else
{
if (m->id)
free(m->id);
if (m->str)
free(m->str);
free(m);
break;
}
}
}
DEBUG_printf(("4cups_create_localizations: %d messages loaded.",
cupsArrayCount(dinfo->localizations)));
unlink(tempfile);
cupsFileClose(temp);
if (http2 != http)
httpClose(http2);
}
static int
cups_read_strings(cups_file_t *strings,
char *buffer,
size_t bufsize,
char **id,
char **str)
{
char *bufptr;
while (cupsFileGets(strings, buffer, bufsize))
{
if (buffer[0] != '\"')
continue;
*id = buffer + 1;
bufptr = cups_scan_strings(buffer);
if (*bufptr != '\"')
continue;
*bufptr++ = '\0';
while (*bufptr && *bufptr != '\"')
bufptr ++;
if (!*bufptr)
continue;
*str = bufptr + 1;
bufptr = cups_scan_strings(bufptr);
if (*bufptr != '\"')
continue;
*bufptr = '\0';
return (1);
}
return (0);
}
static char *
cups_scan_strings(char *buffer)
{
char *bufptr;
for (bufptr = buffer + 1; *bufptr && *bufptr != '\"'; bufptr ++)
{
if (*bufptr == '\\')
{
if (bufptr[1] >= '0' && bufptr[1] <= '3' &&
bufptr[2] >= '0' && bufptr[2] <= '7' &&
bufptr[3] >= '0' && bufptr[3] <= '7')
{
*bufptr = ((((bufptr[1] - '0') << 3) | (bufptr[2] - '0')) << 3) |
(bufptr[3] - '0');
_cups_strcpy(bufptr + 1, bufptr + 4);
}
else
{
_cups_strcpy(bufptr, bufptr + 1);
if (*bufptr == 'n')
*bufptr = '\n';
else if (*bufptr == 'r')
*bufptr = '\r';
else if (*bufptr == 't')
*bufptr = '\t';
}
}
}
return (bufptr);
}