/*
- * "$Id: ppd.c 6660 2007-07-13 05:09:30Z mike $"
+ * "$Id: ppd.c 6937 2007-09-10 21:13:31Z mike $"
*
* PPD file routines for the Common UNIX Printing System (CUPS).
*
#define PPD_HASHSIZE 512 /* Size of hash */
+/*
+ * Line buffer structure...
+ */
+
+typedef struct _ppd_line_s
+{
+ char *buffer; /* Pointer to buffer */
+ size_t bufsize; /* Size of the buffer */
+} _ppd_line_t;
+
+
/*
* Local functions...
*/
cups_encoding_t encoding);
static ppd_option_t *ppd_get_option(ppd_group_t *group, const char *name);
static int ppd_hash_option(ppd_option_t *option);
-static int ppd_read(cups_file_t *fp, char *keyword, char *option,
- char *text, char **string, int ignoreblank,
+static int ppd_read(cups_file_t *fp, _ppd_line_t *line,
+ char *keyword, char *option, char *text,
+ char **string, int ignoreblank,
_cups_globals_t *cg);
* Free all strings at the top level...
*/
- ppd_free(ppd->lang_encoding);
- ppd_free(ppd->nickname);
- ppd_free(ppd->patches);
- ppd_free(ppd->jcl_begin);
- ppd_free(ppd->jcl_end);
- ppd_free(ppd->jcl_ps);
+ _cupsStrFree(ppd->lang_encoding);
+ _cupsStrFree(ppd->nickname);
+ _cupsStrFree(ppd->patches);
+ _cupsStrFree(ppd->jcl_begin);
+ _cupsStrFree(ppd->jcl_end);
+ _cupsStrFree(ppd->jcl_ps);
/*
* Free any emulations...
{
for (i = ppd->num_emulations, emul = ppd->emulations; i > 0; i --, emul ++)
{
- ppd_free(emul->start);
- ppd_free(emul->stop);
+ _cupsStrFree(emul->start);
+ _cupsStrFree(emul->stop);
}
ppd_free(ppd->emulations);
if (ppd->num_filters > 0)
{
for (i = ppd->num_filters, filter = ppd->filters; i > 0; i --, filter ++)
- {
- ppd_free(*filter);
- }
+ _cupsStrFree(*filter);
ppd_free(ppd->filters);
}
if (ppd->num_fonts > 0)
{
for (i = ppd->num_fonts, font = ppd->fonts; i > 0; i --, font ++)
- {
- ppd_free(*font);
- }
+ _cupsStrFree(*font);
ppd_free(ppd->fonts);
}
{
for (i = ppd->num_attrs, attr = ppd->attrs; i > 0; i --, attr ++)
{
- ppd_free((*attr)->value);
+ _cupsStrFree((*attr)->value);
ppd_free(*attr);
}
case PPD_CUSTOM_PASSCODE :
case PPD_CUSTOM_PASSWORD :
case PPD_CUSTOM_STRING :
- ppd_free(cparam->current.custom_string);
+ _cupsStrFree(cparam->current.custom_string);
break;
default :
{
int i, j, k; /* Looping vars */
int count; /* Temporary count */
+ _ppd_line_t line; /* Line buffer */
ppd_file_t *ppd; /* PPD file record */
ppd_group_t *group, /* Current group */
*subgroup; /* Current sub-group */
* Grab the first line and make sure it reads '*PPD-Adobe: "major.minor"'...
*/
- mask = ppd_read(fp, keyword, name, text, &string, 0, cg);
+ line.buffer = NULL;
+ line.bufsize = 0;
+
+ mask = ppd_read(fp, &line, keyword, name, text, &string, 0, cg);
DEBUG_printf(("mask=%x, keyword=\"%s\"...\n", mask, keyword));
if (cg->ppd_status == PPD_OK)
cg->ppd_status = PPD_MISSING_PPDADOBE4;
- ppd_free(string);
+ _cupsStrFree(string);
return (NULL);
}
DEBUG_printf(("ppdOpen: keyword = %s, string = %p\n", keyword, string));
- ppd_free(string);
+ _cupsStrFree(string);
/*
* Allocate memory for the PPD file record...
return (NULL);
}
- ppd->language_level = 1;
+ ppd->language_level = 2;
ppd->color_device = 0;
ppd->colorspace = PPD_CS_GRAY;
ppd->landscape = -90;
ui_keyword = 0;
encoding = CUPS_ISO8859_1;
- while ((mask = ppd_read(fp, keyword, name, text, &string, 1, cg)) != 0)
+ while ((mask = ppd_read(fp, &line, keyword, name, text, &string, 1, cg)) != 0)
{
#ifdef DEBUG
printf("mask = %x, keyword = \"%s\"", mask, keyword);
* Say all PPD files are UTF-8, since we convert to UTF-8...
*/
- ppd->lang_encoding = strdup("UTF-8");
+ ppd->lang_encoding = _cupsStrAlloc("UTF-8");
encoding = _ppdGetEncoding(string);
}
else if (!strcmp(keyword, "LanguageVersion"))
cupsCharsetToUTF8(utf8, string, sizeof(utf8), encoding);
- ppd->nickname = strdup((char *)utf8);
+ ppd->nickname = _cupsStrAlloc((char *)utf8);
}
else
- ppd->nickname = strdup(string);
+ ppd->nickname = _cupsStrAlloc(string);
}
else if (!strcmp(keyword, "Product"))
ppd->product = string;
ppd->ttrasterizer = string;
else if (!strcmp(keyword, "JCLBegin"))
{
- ppd->jcl_begin = strdup(string);
+ ppd->jcl_begin = _cupsStrAlloc(string);
ppd_decode(ppd->jcl_begin); /* Decode quoted string */
}
else if (!strcmp(keyword, "JCLEnd"))
{
- ppd->jcl_end = strdup(string);
+ ppd->jcl_end = _cupsStrAlloc(string);
ppd_decode(ppd->jcl_end); /* Decode quoted string */
}
else if (!strcmp(keyword, "JCLToPSInterpreter"))
{
- ppd->jcl_ps = strdup(string);
+ ppd->jcl_ps = _cupsStrAlloc(string);
ppd_decode(ppd->jcl_ps); /* Decode quoted string */
}
else if (!strcmp(keyword, "AccurateScreensSupport"))
if (filter == NULL)
{
- ppd_free(filter);
-
cg->ppd_status = PPD_ALLOC_ERROR;
goto error;
}
ppd->fonts = tempfonts;
- ppd->fonts[ppd->num_fonts] = strdup(name);
+ ppd->fonts[ppd->num_fonts] = _cupsStrAlloc(name);
ppd->num_fonts ++;
}
else if (!strncmp(keyword, "ParamCustom", 11))
else if (!strcmp(keyword, "JobPatchFile"))
{
if (ppd->patches == NULL)
- ppd->patches = strdup(string);
+ ppd->patches = _cupsStrAlloc(string);
else
{
temp = realloc(ppd->patches, strlen(ppd->patches) +
option->section = PPD_ORDER_ANY;
- ppd_free(string);
+ _cupsStrFree(string);
string = NULL;
}
else if (!strcmp(keyword, "JCLOpenUI"))
option->section = PPD_ORDER_JCL;
group = NULL;
- ppd_free(string);
+ _cupsStrFree(string);
string = NULL;
}
else if (!strcmp(keyword, "CloseUI") || !strcmp(keyword, "JCLCloseUI"))
{
option = NULL;
- ppd_free(string);
+ _cupsStrFree(string);
string = NULL;
}
else if (!strcmp(keyword, "OpenGroup"))
if (group == NULL)
goto error;
- ppd_free(string);
+ _cupsStrFree(string);
string = NULL;
}
else if (!strcmp(keyword, "CloseGroup"))
{
group = NULL;
- ppd_free(string);
+ _cupsStrFree(string);
string = NULL;
}
else if (!strcmp(keyword, "OrderDependency") ||
option->order = order;
}
- ppd_free(string);
+ _cupsStrFree(string);
string = NULL;
}
else if (!strncmp(keyword, "Default", 7))
* Don't add this one as an attribute...
*/
- ppd_free(string);
+ _cupsStrFree(string);
string = NULL;
}
else if (!strcmp(keyword, "PaperDimension"))
size->width = (float)_cupsStrScand(string, &sptr, loc);
size->length = (float)_cupsStrScand(sptr, NULL, loc);
- ppd_free(string);
+ _cupsStrFree(string);
string = NULL;
}
else if (!strcmp(keyword, "ImageableArea"))
size->right = (float)_cupsStrScand(sptr, &sptr, loc);
size->top = (float)_cupsStrScand(sptr, NULL, loc);
- ppd_free(string);
+ _cupsStrFree(string);
string = NULL;
}
else if (option != NULL &&
(mask & (PPD_KEYWORD | PPD_STRING)) == (PPD_KEYWORD | PPD_STRING))
ppd_add_attr(ppd, keyword, name, text, string);
else
- ppd_free(string);
+ _cupsStrFree(string);
}
+ if (line.buffer)
+ free(line.buffer);
+
/*
* Reset language preferences...
*/
error:
- ppd_free(string);
+ if (line.buffer)
+ free(line.buffer);
+
+ _cupsStrFree(string);
ppdClose(ppd);
ppd_compare_choices(ppd_choice_t *a, /* I - First choice */
ppd_choice_t *b) /* I - Second choice */
{
- return (a->option - b->option);
+ return (strcmp(a->option->keyword, b->option->keyword));
}
i > 0;
i --, choice ++)
{
- ppd_free(choice->code);
+ _cupsStrFree(choice->code);
}
ppd_free(option->choices);
static int /* O - Bitmask of fields read */
ppd_read(cups_file_t *fp, /* I - File to read from */
+ _ppd_line_t *line, /* I - Line buffer */
char *keyword, /* O - Keyword from line */
char *option, /* O - Option from line */
char *text, /* O - Human-readable text from line */
*optptr, /* Option pointer */
*textptr, /* Text pointer */
*strptr, /* Pointer into string */
- *lineptr, /* Current position in line buffer */
- *line; /* Line buffer */
- int linesize; /* Current size of line buffer */
-
- /*
- * Range check everything...
- */
+ *lineptr; /* Current position in line buffer */
- if (!fp || !keyword || !option || !text || !string)
- return (0);
/*
* Now loop until we have a valid line...
*string = NULL;
col = 0;
startline = cg->ppd_line + 1;
- linesize = 1024;
- line = malloc(linesize);
- if (!line)
- return (0);
+ if (!line->buffer)
+ {
+ line->bufsize = 1024;
+ line->buffer = malloc(1024);
+
+ if (!line->buffer)
+ return (0);
+ }
do
{
* Read the line...
*/
- lineptr = line;
+ lineptr = line->buffer;
endquote = 0;
colon = 0;
while ((ch = cupsFileGetChar(fp)) != EOF)
{
- if (lineptr >= (line + linesize - 1))
+ if (lineptr >= (line->buffer + line->bufsize - 1))
{
/*
* Expand the line buffer...
char *temp; /* Temporary line pointer */
- linesize += 1024;
- if (linesize > 262144)
+ line->bufsize += 1024;
+ if (line->bufsize > 262144)
{
/*
* Don't allow lines longer than 256k!
cg->ppd_line = startline;
cg->ppd_status = PPD_LINE_TOO_LONG;
- free(line);
-
return (0);
}
- temp = realloc(line, linesize);
+ temp = realloc(line->buffer, line->bufsize);
if (!temp)
{
cg->ppd_line = startline;
cg->ppd_status = PPD_LINE_TOO_LONG;
- free(line);
-
return (0);
}
- lineptr = temp + (lineptr - line);
- line = temp;
+ lineptr = temp + (lineptr - line->buffer);
+ line->buffer = temp;
}
if (ch == '\r' || ch == '\n')
cupsFileGetChar(fp);
}
- if (lineptr == line && ignoreblank)
+ if (lineptr == line->buffer && ignoreblank)
continue; /* Skip blank lines */
ch = '\n';
cg->ppd_line = startline;
cg->ppd_status = PPD_ILLEGAL_CHARACTER;
- free(line);
-
return (0);
}
else if (ch != 0x1a)
cg->ppd_line = startline;
cg->ppd_status = PPD_LINE_TOO_LONG;
- free(line);
-
return (0);
}
- if (ch == ':' && strncmp(line, "*%", 2) != 0)
+ if (ch == ':' && strncmp(line->buffer, "*%", 2) != 0)
colon = 1;
if (ch == '\"' && colon)
cg->ppd_line = startline;
cg->ppd_status = PPD_ILLEGAL_CHARACTER;
- free(line);
-
return (0);
}
else if (ch != 0x1a)
cg->ppd_line = startline;
cg->ppd_status = PPD_LINE_TOO_LONG;
- free(line);
-
return (0);
}
}
cg->ppd_line = startline;
cg->ppd_status = PPD_ILLEGAL_CHARACTER;
- free(line);
-
return (0);
}
else if (ch != 0x1a)
cg->ppd_line = startline;
cg->ppd_status = PPD_LINE_TOO_LONG;
- free(line);
-
return (0);
}
}
}
- if (lineptr > line && lineptr[-1] == '\n')
+ if (lineptr > line->buffer && lineptr[-1] == '\n')
lineptr --;
*lineptr = '\0';
* reading the PPD when we get to the start of this data.
*/
- if (!strcmp(line, "*%APLWORKSET START"))
- {
- free(line);
+ if (!strcmp(line->buffer, "*%APLWORKSET START"))
return (0);
- }
- if (ch == EOF && lineptr == line)
- {
- free(line);
+ if (ch == EOF && lineptr == line->buffer)
return (0);
- }
/*
* Now parse it...
*/
mask = 0;
- lineptr = line + 1;
+ lineptr = line->buffer + 1;
keyword[0] = '\0';
option[0] = '\0';
text[0] = '\0';
*string = NULL;
- if ((!line[0] || /* Blank line */
- !strncmp(line, "*%", 2) || /* Comment line */
- !strcmp(line, "*End")) && /* End of multi-line string */
+ if ((!line->buffer[0] || /* Blank line */
+ !strncmp(line->buffer, "*%", 2) || /* Comment line */
+ !strcmp(line->buffer, "*End")) && /* End of multi-line string */
ignoreblank) /* Ignore these? */
{
startline = cg->ppd_line + 1;
continue;
}
- if (!strcmp(line, "*")) /* (Bad) comment line */
+ if (!strcmp(line->buffer, "*")) /* (Bad) comment line */
{
if (cg->ppd_conform == PPD_CONFORM_RELAXED)
{
cg->ppd_line = startline;
cg->ppd_status = PPD_ILLEGAL_MAIN_KEYWORD;
- free(line);
return (0);
}
}
- if (line[0] != '*') /* All lines start with an asterisk */
+ if (line->buffer[0] != '*') /* All lines start with an asterisk */
{
/*
* Allow lines consisting of just whitespace...
*/
- for (lineptr = line; *lineptr; lineptr ++)
+ for (lineptr = line->buffer; *lineptr; lineptr ++)
if (!isspace(*lineptr & 255))
break;
if (*lineptr)
{
cg->ppd_status = PPD_MISSING_ASTERISK;
- free(line);
return (0);
}
else if (ignoreblank)
continue;
else
- {
- free(line);
return (0);
- }
}
/*
(keyptr - keyword) >= (PPD_MAX_NAME - 1))
{
cg->ppd_status = PPD_ILLEGAL_MAIN_KEYWORD;
- free(line);
return (0);
}
(optptr - option) >= (PPD_MAX_NAME - 1))
{
cg->ppd_status = PPD_ILLEGAL_OPTION_KEYWORD;
- free(line);
return (0);
}
if (isspace(*lineptr & 255) && cg->ppd_conform == PPD_CONFORM_STRICT)
{
cg->ppd_status = PPD_ILLEGAL_WHITESPACE;
- free(line);
return (0);
}
(textptr - text) >= (PPD_MAX_LINE - 1))
{
cg->ppd_status = PPD_ILLEGAL_TRANSLATION;
- free(line);
return (0);
}
if (textlen > PPD_MAX_TEXT && cg->ppd_conform == PPD_CONFORM_STRICT)
{
cg->ppd_status = PPD_ILLEGAL_TRANSLATION;
- free(line);
return (0);
}
if (isspace(*lineptr & 255) && cg->ppd_conform == PPD_CONFORM_STRICT)
{
cg->ppd_status = PPD_ILLEGAL_WHITESPACE;
- free(line);
return (0);
}
if (*strptr == '\"')
{
/*
- * Quoted string by itself...
+ * Quoted string by itself, remove quotes...
*/
- *string = malloc(strlen(lineptr) + 1);
-
- strptr = *string;
-
- for (; *lineptr != '\0'; lineptr ++)
- if (*lineptr != '\"')
- *strptr++ = *lineptr;
-
- *strptr = '\0';
+ *strptr = '\0';
+ lineptr ++;
}
- else
- *string = strdup(lineptr);
+
+ *string = _cupsStrAlloc(lineptr);
/* DEBUG_printf(("string = \"%s\", lineptr = \"%s\"\n", *string, lineptr));*/
}
while (mask == 0);
- free(line);
-
return (mask);
}
/*
- * End of "$Id: ppd.c 6660 2007-07-13 05:09:30Z mike $".
+ * End of "$Id: ppd.c 6937 2007-09-10 21:13:31Z mike $".
*/