/*
- * "$Id: mime.c 7694 2008-06-26 00:23:20Z mike $"
+ * "$Id$"
*
- * MIME database file routines for CUPS.
+ * MIME database file routines for CUPS.
*
- * Copyright 2007-2011 by Apple Inc.
- * Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products, all rights reserved.
*
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law. Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file. If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * Contents:
- *
- * mimeDelete() - Delete (free) a MIME database.
- * mimeDeleteFilter() - Delete a filter from the MIME database.
- * mimeDeleteType() - Delete a type from the MIME database.
- * mimeFirstFilter() - Get the first filter in the MIME database.
- * mimeFirstType() - Get the first type in the MIME database.
- * mimeLoad() - Create a new MIME database from disk.
- * mimeMerge() - Merge a MIME database from disk with the current one.
- * mimeNew() - Create a new, empty MIME database.
- * mimeNextFilter() - Get the next filter in the MIME database.
- * mimeNextType() - Get the next type in the MIME database.
- * mimeNumFilters() - Get the number of filters in a MIME database.
- * mimeNumTypes() - Get the number of types in a MIME database.
- * mime_add_fcache() - Add a filter to the filter cache.
- * mime_compare_fcache() - Compare two filter cache entries.
- * mime_delete_fcache() - Free all memory used by the filter cache.
- * mime_delete_rules() - Free all memory for the given rule tree.
- * mime_load_convs() - Load a xyz.convs file...
- * mime_load_types() - Load a xyz.types file...
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law. Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file. If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
*/
/*
#include <cups/string-private.h>
#include <cups/debug-private.h>
#include <cups/dir.h>
-#include "mime.h"
+#include "mime-private.h"
/*
const char *filterpath,
cups_array_t *filtercache);
static void mime_load_types(mime_t *mime, const char *filename);
-static mime_t *mime_new(void);
/*
filter ? filter->dst->super : "???",
filter ? filter->cost : -1,
filter ? CUPS_LLCAST filter->maxsize : CUPS_LLCAST -1));
-
+
if (!mime || !filter)
return;
}
+/*
+ * '_mimeError()' - Show an error message.
+ */
+
+void
+_mimeError(mime_t *mime, /* I - MIME database */
+ const char *message, /* I - Printf-style message string */
+ ...) /* I - Additional arguments as needed */
+{
+ va_list ap; /* Argument pointer */
+ char buffer[8192]; /* Message buffer */
+
+
+ if (mime->error_cb)
+ {
+ va_start(ap, message);
+ vsnprintf(buffer, sizeof(buffer), message, ap);
+ va_end(ap);
+
+ (*mime->error_cb)(mime->error_ctx, buffer);
+ }
+}
+
+
/*
* 'mimeFirstFilter()' - Get the first filter in the MIME database.
*/
{
DEBUG_printf(("1mimeLoadFilters: Unable to open \"%s\": %s", pathname,
strerror(errno)));
+ _mimeError(mime, "Unable to open \"%s\": %s", pathname, strerror(errno));
return (mime);
}
DEBUG_printf(("1mimeLoadTypes: Unable to open \"%s\": %s", pathname,
strerror(errno)));
DEBUG_printf(("1mimeLoadTypes: Returning %p.", mime));
+ _mimeError(mime, "Unable to open \"%s\": %s", pathname, strerror(errno));
return (mime);
}
*/
if (!mime)
- mime = mime_new();
+ mime = mimeNew();
if (!mime)
{
}
+/*
+ * 'mimeNew()' - Create a new, empty MIME database.
+ */
+
+mime_t * /* O - MIME database */
+mimeNew(void)
+{
+ return ((mime_t *)calloc(1, sizeof(mime_t)));
+}
+
+
/*
* 'mimeNextFilter()' - Get the next filter in the MIME database.
*/
}
+/*
+ * 'mimeSetErrorCallback()' - Set the callback for error messages.
+ */
+
+void
+mimeSetErrorCallback(
+ mime_t *mime, /* I - MIME database */
+ mime_error_cb_t cb, /* I - Callback function */
+ void *ctx) /* I - Context pointer for callback */
+{
+ if (mime)
+ {
+ mime->error_cb = cb;
+ mime->error_ctx = ctx;
+ }
+}
+
+
/*
* 'mime_add_fcache()' - Add a filter to the filter cache.
*/
if (rules->child != NULL)
mime_delete_rules(rules->child);
+ if (rules->op == MIME_MAGIC_REGEX)
+ regfree(&(rules->value.rev));
+
free(rules);
rules = next;
}
/*
- * 'mime_load_convs()' - Load a xyz.convs file...
+ * 'mime_load_convs()' - Load a xyz.convs file.
*/
static void
{
DEBUG_printf(("3mime_load_convs: Unable to open \"%s\": %s", filename,
strerror(errno)));
+ _mimeError(mime, "Unable to open \"%s\": %s", filename, strerror(errno));
return;
}
while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
(temp - super + 1) < MIME_MAX_SUPER)
- *temp++ = tolower(*lineptr++ & 255);
+ *temp++ = (char)tolower(*lineptr++ & 255);
*temp = '\0';
while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
*lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
- *temp++ = tolower(*lineptr++ & 255);
+ *temp++ = (char)tolower(*lineptr++ & 255);
*temp = '\0';
if (!mime_add_fcache(filtercache, filter, filterpath))
{
DEBUG_printf(("mime_load_convs: Filter %s not found in %s.", filter,
- filterpath));
+ filterpath));
+ _mimeError(mime, "Filter \"%s\" not found.", filter);
continue;
}
}
while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
(temp - super + 1) < MIME_MAX_SUPER)
- *temp++ = tolower(*lineptr++ & 255);
+ *temp++ = (char)tolower(*lineptr++ & 255);
*temp = '\0';
while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
*lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
- *temp++ = tolower(*lineptr++ & 255);
+ *temp++ = (char)tolower(*lineptr++ & 255);
*temp = '\0';
* Force * / * to be "application/octet-stream"...
*/
- strcpy(super, "application");
- strcpy(type, "octet-stream");
+ strlcpy(super, "application", sizeof(super));
+ strlcpy(type, "octet-stream", sizeof(type));
}
/*
/*
- * 'mime_load_types()' - Load a xyz.types file...
+ * 'mime_load_types()' - Load a xyz.types file.
*/
static void
const char *filename) /* I - Types file to load */
{
cups_file_t *fp; /* Types file */
- int linelen; /* Length of line */
+ size_t linelen; /* Length of line */
char line[32768], /* Input line from file */
*lineptr, /* Current position in line */
super[MIME_MAX_SUPER], /* Super-type name */
{
DEBUG_printf(("3mime_load_types: Unable to open \"%s\": %s", filename,
strerror(errno)));
+ _mimeError(mime, "Unable to open \"%s\": %s", filename, strerror(errno));
return;
}
while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
(temp - super + 1) < MIME_MAX_SUPER)
- *temp++ = tolower(*lineptr++ & 255);
+ *temp++ = (char)tolower(*lineptr++ & 255);
*temp = '\0';
while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
*lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
- *temp++ = tolower(*lineptr++ & 255);
+ *temp++ = (char)tolower(*lineptr++ & 255);
*temp = '\0';
/*
- * 'mime_new()' - Create a new, empty MIME database.
- */
-
-static mime_t * /* O - MIME database */
-mime_new(void)
-{
- return ((mime_t *)calloc(1, sizeof(mime_t)));
-}
-
-
-/*
- * End of "$Id: mime.c 7694 2008-06-26 00:23:20Z mike $".
+ * End of "$Id$".
*/