/*
- * "$Id$"
+ * "$Id: type.c 7694 2008-06-26 00:23:20Z mike $"
*
* MIME typing routines for the Common UNIX Printing System (CUPS).
*
+ * Copyright 2007-2008 by Apple Inc.
* Copyright 1997-2006 by Easy Software Products, all rights reserved.
*
* These coded instructions, statements, and computer programs are the
- * property of Easy Software Products 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 missing or damaged please contact Easy Software Products
- * at:
- *
- * Attn: CUPS Licensing Information
- * Easy Software Products
- * 44141 Airport View Drive, Suite 204
- * Hollywood, Maryland 20636 USA
- *
- * Voice: (301) 373-9600
- * EMail: cups-info@cups.org
- * WWW: http://www.cups.org
+ * 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:
*
strlcpy(temp->super, super, sizeof(temp->super));
strcpy(temp->type, type); /* Safe: temp->type is allocated */
+ temp->priority = 100;
cupsArrayAdd(mime->types, temp);
logic = MIME_MAGIC_NOP;
invert = 0;
- DEBUG_printf(("%s/%s: %s\n", mt->super, mt->type, rule));
+ DEBUG_printf(("mimeAddTypeRule: %s/%s: %s\n", mt->super, mt->type, rule));
while (*rule != '\0')
{
if (*rule == '(')
{
- DEBUG_puts("new parenthesis group");
+ DEBUG_puts("mimeAddTypeRule: New parenthesis group");
logic = MIME_MAGIC_NOP;
rule ++;
}
else if (*rule == ')')
{
- DEBUG_puts("close paren...");
+ DEBUG_puts("mimeAddTypeRule: Close paren...");
if (current == NULL || current->parent == NULL)
return (-1);
current->prev = NULL;
current->parent = temp;
- DEBUG_printf(("creating new AND group %p...\n", temp));
+ DEBUG_printf(("mimeAddTypeRule: Creating new AND group %p...\n", temp));
}
else
{
- DEBUG_printf(("setting group %p op to AND...\n", current->parent));
+ DEBUG_printf(("mimeAddTypeRule: Setting group %p op to AND...\n",
+ current->parent));
current->parent->op = MIME_MAGIC_AND;
}
if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
return (-1);
- DEBUG_printf(("creating new AND group %p inside OR group\n", temp));
+ DEBUG_printf(("mimeAddTypeRule: Creating new AND group %p inside OR "
+ "group\n", temp));
while (current->prev != NULL)
{
* This isn't the top rule, so go up one level...
*/
- DEBUG_puts("going up one level");
+ DEBUG_puts("mimeAddTypeRule: Going up one level");
current = current->parent;
}
}
}
else if (*rule == '!')
{
- DEBUG_puts("NOT");
+ DEBUG_puts("mimeAddTypeRule: NOT");
invert = 1;
rule ++;
}
op = MIME_MAGIC_LOCALE;
else if (!strcmp(name, "contains"))
op = MIME_MAGIC_CONTAINS;
+ else if (!strcmp(name, "priority") && num_values == 1)
+ {
+ mt->priority = atoi(value[0]);
+ continue;
+ }
else
return (-1);
}
* Add parenthetical grouping...
*/
- DEBUG_printf(("making new OR group %p for parenthesis...\n", temp));
+ DEBUG_printf(("mimeAddTypeRule: Making new OR group %p for "
+ "parenthesis...\n", temp));
temp->op = MIME_MAGIC_OR;
logic = MIME_MAGIC_OR;
}
- DEBUG_printf(("adding %p: %s, op = %d, logic = %d, invert = %d\n",
- temp, name, op, logic, invert));
+ DEBUG_printf(("mimeAddTypeRule: adding %p: %s, op=%d, logic=%d, "
+ "invert=%d\n", temp, name, op, logic, invert));
/*
* Fill in data for the rule...
{
_mime_filebuf_t fb; /* File buffer */
const char *base; /* Base filename of file */
- mime_type_t *type; /* File type */
+ mime_type_t *type, /* File type */
+ *best; /* Best match */
DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", "
- "compression=%p)\n",
- mime, pathname ? pathname : "(nil)",
- filename ? filename : "(nil)",
- compression));
+ "compression=%p)\n", mime, pathname, filename, compression));
/*
* Range check input parameters...
if ((base = strrchr(filename, '/')) != NULL)
base ++;
else
- filename = filename;
+ base = filename;
}
else if ((base = strrchr(pathname, '/')) != NULL)
base ++;
else
- filename = pathname;
+ base = pathname;
/*
* Then check it against all known types...
*/
- for (type = (mime_type_t *)cupsArrayFirst(mime->types);
+ for (type = (mime_type_t *)cupsArrayFirst(mime->types), best = NULL;
type;
type = (mime_type_t *)cupsArrayNext(mime->types))
if (checkrules(base, &fb, type->rules))
- break;
+ {
+ if (!best || type->priority > best->priority)
+ best = type;
+ }
/*
* Finally, close the file and return a match (if any)...
cupsFileClose(fb.fp);
- return (type);
+ return (best);
}
int i; /* Result of comparison */
- if ((i = strcmp(t0->super, t1->super)) == 0)
- i = strcmp(t0->type, t1->type);
+ if ((i = strcasecmp(t0->super, t1->super)) == 0)
+ i = strcasecmp(t0->type, t1->type);
return (i);
}
break;
case MIME_MAGIC_STRING :
- DEBUG_printf((" string(%d, \"%s\")\n", rules->offset,
+ DEBUG_printf(("checkrules: string(%d, \"%s\")\n", rules->offset,
rules->value.stringv));
/*
sizeof(fb->buffer));
fb->offset = rules->offset;
- DEBUG_printf((" loaded %d byte fb->buffer at %d, starts "
+ DEBUG_printf(("checkrules: loaded %d byte fb->buffer at %d, starts "
"with \"%c%c%c%c\"...\n",
fb->length, fb->offset, fb->buffer[0], fb->buffer[1],
fb->buffer[2], fb->buffer[3]));
else
result = (memcmp(fb->buffer + rules->offset - fb->offset,
rules->value.stringv, rules->length) == 0);
- DEBUG_printf((" result=%d\n", result));
+ DEBUG_printf(("checkrules: result=%d\n", result));
break;
case MIME_MAGIC_ISTRING :
* the the rule set is false...
*/
- DEBUG_printf((" result of test %p (MIME_MAGIC_%s) is %d\n", rules,
- debug_tests[rules->op], result));
+ DEBUG_printf(("checkrules: result of test %p (MIME_MAGIC_%s) is %d\n",
+ rules, debug_tests[rules->op], result));
if ((result && logic == MIME_MAGIC_OR) ||
(!result && logic == MIME_MAGIC_AND))
/*
- * End of "$Id$".
+ * End of "$Id: type.c 7694 2008-06-26 00:23:20Z mike $".
*/