Skip to content

Commit

Permalink
Generate strings files for localizing PPD options (Issue #5194)
Browse files Browse the repository at this point in the history
cups/language-private.h:
- New _cupsMessageAdd and _cupsMessageSave private APIs.

cups/language.c:
- Implement new _cupsMessageAdd and _cupsMessageSave private APIs.

cups/ppd-cache.c:
- Generate strings array when loading cache from PPD.
- Remove strings_uri (just pulling strings from PPD now).

cups/ppd-private.h:
- Remove strings_uri and add strings array to cache.

scheduler/client.c:
- Add support for /strings/NAME.strings
- Cleanup implementation of GET/HEAD/POST to files.

scheduler/ipp.c:
- Return local strings file URI.
- Clean up copy_printer_attrs implementation.

scheduler/printers.c:
- Save strings array to cache file, drop support for strings_uri.

scheduler/printers.h:
- Add strings filename to cupsd_printer_t structure.
  • Loading branch information
Michael R Sweet committed Dec 19, 2017
1 parent 3d90ace commit 2fa1ba3
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 479 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CHANGES - 2.3b1 - 2017-12-18
CHANGES - 2.3b1 - 2017-12-19
============================


Expand Down Expand Up @@ -56,3 +56,5 @@ Changes in CUPS v2.3b1
- The scheduler now substitutes default values for invalid job attributes when
running in "relaxed conformance" mode (Issue #5186)
- The scheduler did not work with older versions of uClibc (Issue #5188)
- The scheduler now generates a strings file for localizing PPD options
(Issue #5194)
1 change: 1 addition & 0 deletions cups/language-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ extern void _cupsMessageFree(cups_array_t *a);
extern cups_array_t *_cupsMessageLoad(const char *filename, int flags);
extern const char *_cupsMessageLookup(cups_array_t *a, const char *m);
extern cups_array_t *_cupsMessageNew(void *context);
extern int _cupsMessageSave(const char *filename, int flags, cups_array_t *a);
extern void _cupsSetLocale(char *argv[]);


Expand Down
90 changes: 90 additions & 0 deletions cups/language.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ static cups_lang_t *cups_cache_lookup(const char *name, cups_encoding_t encoding
static int cups_message_compare(_cups_message_t *m1, _cups_message_t *m2);
static void cups_message_free(_cups_message_t *m);
static void cups_message_load(cups_lang_t *lang);
static void cups_message_puts(cups_file_t *fp, const char *s);
static int cups_read_strings(cups_file_t *fp, int flags, cups_array_t *a);
static void cups_unquote(char *d, const char *s);

Expand Down Expand Up @@ -1225,6 +1226,57 @@ _cupsMessageNew(void *context) /* I - User data */
}


/*
* '_cupsMessageSave()' - Save a message catalog array.
*/

int /* O - 0 on success, -1 on failure */
_cupsMessageSave(const char *filename,/* I - Output filename */
int flags, /* I - Format flags */
cups_array_t *a) /* I - Message array */
{
cups_file_t *fp; /* Output file */
_cups_message_t *m; /* Current message */


/*
* Output message catalog file...
*/

if ((fp = cupsFileOpen(filename, "w")) == NULL)
return (-1);

/*
* Write each message...
*/

if (flags & _CUPS_MESSAGE_STRINGS)
{
for (m = (_cups_message_t *)cupsArrayFirst(a); m; m = (_cups_message_t *)cupsArrayNext(a))
{
cupsFilePuts(fp, "\"");
cups_message_puts(fp, m->msg);
cupsFilePuts(fp, "\" = \"");
cups_message_puts(fp, m->str);
cupsFilePuts(fp, "\";\n");
}
}
else
{
for (m = (_cups_message_t *)cupsArrayFirst(a); m; m = (_cups_message_t *)cupsArrayNext(a))
{
cupsFilePuts(fp, "msgid \"");
cups_message_puts(fp, m->msg);
cupsFilePuts(fp, "\"\nmsgstr \"");
cups_message_puts(fp, m->str);
cupsFilePuts(fp, "\"\n");
}
}

return (cupsFileClose(fp));
}


#ifdef __APPLE__
/*
* 'appleLangDefault()' - Get the default locale string.
Expand Down Expand Up @@ -1669,6 +1721,44 @@ cups_message_load(cups_lang_t *lang) /* I - Language */
}


/*
* 'cups_message_puts()' - Write a message string with quoting.
*/

static void
cups_message_puts(cups_file_t *fp, /* I - File to write to */
const char *s) /* I - String to write */
{
const char *start, /* Start of substring */
*ptr; /* Pointer into string */


for (start = s, ptr = s; *ptr; ptr ++)
{
if (strchr("\\\"\n\t", *ptr))
{
if (ptr > start)
{
cupsFileWrite(fp, start, (size_t)(ptr - start));
start = ptr + 1;
}

if (*ptr == '\\')
cupsFileWrite(fp, "\\\\", 2);
else if (*ptr == '\"')
cupsFileWrite(fp, "\\\"", 2);
else if (*ptr == '\n')
cupsFileWrite(fp, "\\n", 2);
else /* if (*ptr == '\t') */
cupsFileWrite(fp, "\\t", 2);
}
}

if (ptr > start)
cupsFileWrite(fp, start, (size_t)(ptr - start));
}


/*
* 'cups_read_strings()' - Read a pair of strings from a .strings file.
*/
Expand Down
84 changes: 68 additions & 16 deletions cups/ppd-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

static int cups_get_url(http_t **http, const char *url, char *name, size_t namesize);
static void pwg_add_finishing(cups_array_t *finishings, ipp_finishings_t template, const char *name, const char *value);
static void pwg_add_message(cups_array_t *a, const char *msg, const char *str);
static int pwg_compare_finishings(_pwg_finishings_t *a, _pwg_finishings_t *b);
static int pwg_compare_sizes(cups_size_t *a, cups_size_t *b);
static cups_size_t *pwg_copy_size(cups_size_t *size);
Expand Down Expand Up @@ -914,8 +915,6 @@ _ppdCacheCreateWithFile(
else
pc->mandatory = _cupsArrayNewStrings(value, ' ');
}
else if (!_cups_strcasecmp(line, "StringsURI"))
pc->strings_uri = _cupsStrAlloc(value);
else if (!_cups_strcasecmp(line, "SupportFile"))
{
if (!pc->support_files)
Expand Down Expand Up @@ -1028,6 +1027,7 @@ _ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */
pwg_size_t *new_size; /* New size to add, if any */
const char *filter; /* Current filter */
_pwg_finishings_t *finishings; /* Current finishings value */
char msg_id[256]; /* Message identifier */


DEBUG_printf(("_ppdCacheCreateWithPPD(ppd=%p)", ppd));
Expand All @@ -1049,6 +1049,8 @@ _ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */
goto create_error;
}

pc->strings = _cupsMessageNew(NULL);

/*
* Copy and convert size data...
*/
Expand Down Expand Up @@ -1285,6 +1287,13 @@ _ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */

map->pwg = _cupsStrAlloc(pwg_name);
map->ppd = _cupsStrAlloc(choice->choice);

/*
* Add localized text for PWG keyword to message catalog...
*/

snprintf(msg_id, sizeof(msg_id), "media-source.%s", pwg_name);
pwg_add_message(pc->strings, msg_id, choice->text);
}
}

Expand Down Expand Up @@ -1349,6 +1358,13 @@ _ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */

map->pwg = _cupsStrAlloc(pwg_name);
map->ppd = _cupsStrAlloc(choice->choice);

/*
* Add localized text for PWG keyword to message catalog...
*/

snprintf(msg_id, sizeof(msg_id), "media-type.%s", pwg_name);
pwg_add_message(pc->strings, msg_id, choice->text);
}
}

Expand Down Expand Up @@ -1376,6 +1392,13 @@ _ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */

map->pwg = _cupsStrAlloc(pwg_keyword);
map->ppd = _cupsStrAlloc(choice->choice);

/*
* Add localized text for PWG keyword to message catalog...
*/

snprintf(msg_id, sizeof(msg_id), "output-bin.%s", pwg_name);
pwg_add_message(pc->strings, msg_id, choice->text);
}
}

Expand All @@ -1393,6 +1416,17 @@ _ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */

do
{
/*
* Add localized text for PWG keyword to message catalog...
*/

snprintf(msg_id, sizeof(msg_id), "preset-name.%s", ppd_attr->spec);
pwg_add_message(pc->strings, msg_id, ppd_attr->text);

/*
* Get the options for this preset...
*/

num_options = _ppdParseOptions(ppd_attr->value, 0, &options,
_PPD_PARSE_ALL);

Expand Down Expand Up @@ -1836,7 +1870,16 @@ _ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */
pc->templates = cupsArrayNew3((cups_array_func_t)strcmp, NULL, NULL, 0, (cups_acopy_func_t)_cupsStrAlloc, (cups_afree_func_t)_cupsStrFree);

for (choice = ppd_option->choices, i = ppd_option->num_choices; i > 0; choice ++, i --)
{
cupsArrayAdd(pc->templates, (void *)choice->choice);

/*
* Add localized text for PWG keyword to message catalog...
*/

snprintf(msg_id, sizeof(msg_id), "finishing-template.%s", choice->choice);
pwg_add_message(pc->strings, msg_id, choice->text);
}
}

/*
Expand Down Expand Up @@ -1870,13 +1913,6 @@ _ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */
if ((ppd_attr = ppdFindAttr(ppd, "cupsMandatory", NULL)) != NULL)
pc->mandatory = _cupsArrayNewStrings(ppd_attr->value, ' ');

/*
* Strings (remote) file...
*/

if ((ppd_attr = ppdFindAttr(ppd, "cupsStringsURI", NULL)) != NULL)
pc->strings_uri = _cupsStrAlloc(ppd_attr->value);

/*
* Support files...
*/
Expand Down Expand Up @@ -2000,6 +2036,8 @@ _ppdCacheDestroy(_ppd_cache_t *pc) /* I - PPD cache and mapping data */

cupsArrayDelete(pc->support_files);

cupsArrayDelete(pc->strings);

free(pc);
}

Expand Down Expand Up @@ -2947,13 +2985,6 @@ _ppdCacheWriteFile(
value = (char *)cupsArrayNext(pc->mandatory))
cupsFilePutConf(fp, "Mandatory", value);

/*
* (Remote) strings file...
*/

if (pc->strings_uri)
cupsFilePutConf(fp, "StringsURI", pc->strings_uri);

/*
* Support files...
*/
Expand Down Expand Up @@ -4613,6 +4644,27 @@ pwg_add_finishing(
}


/*
* 'pwg_add_message()' - Add a message to the PPD cached strings.
*/

static void
pwg_add_message(cups_array_t *a, /* I - Message catalog */
const char *msg, /* I - Message identifier */
const char *str) /* I - Localized string */
{
_cups_message_t *m; /* New message */


if ((m = calloc(1, sizeof(_cups_message_t))) != NULL)
{
m->msg = strdup(msg);
m->str = strdup(str);
cupsArrayAdd(a, m);
}
}


/*
* 'pwg_compare_finishings()' - Compare two finishings values.
*/
Expand Down
2 changes: 1 addition & 1 deletion cups/ppd-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ struct _ppd_cache_s /**** PPD cache and PWG conversion data ****/
char *password; /* cupsJobPassword value */
cups_array_t *mandatory; /* cupsMandatory value */
char *charge_info_uri; /* cupsChargeInfoURI value */
char *strings_uri; /* cupsStringsURI value */
cups_array_t *strings; /* Localization strings */
cups_array_t *support_files; /* Support files - ICC profiles, etc. */
};

Expand Down
Loading

0 comments on commit 2fa1ba3

Please sign in to comment.