]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/type.c
Merge changes from CUPS 1.4svn-r7696.
[thirdparty/cups.git] / scheduler / type.c
index 79f7a229b150ddc0cecda33834a31de610eb1844..6532b9c08b79a39b9ca23b9a8f02dd597d089232 100644 (file)
@@ -1,25 +1,16 @@
 /*
- * "$Id: type.c 5180 2006-02-26 01:31:45Z mike $"
+ * "$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:
  *
@@ -111,6 +102,7 @@ mimeAddType(mime_t     *mime,               /* I - MIME database */
 
   strlcpy(temp->super, super, sizeof(temp->super));
   strcpy(temp->type, type);            /* Safe: temp->type is allocated */
+  temp->priority = 100;
 
   cupsArrayAdd(mime->types, temp);
 
@@ -165,7 +157,7 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
   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')
   {
@@ -174,13 +166,13 @@ mimeAddTypeRule(mime_type_t *mt,  /* I - Type to add to */
 
     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);
 
@@ -216,11 +208,12 @@ mimeAddTypeRule(mime_type_t *mt,  /* I - Type to add to */
         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;
       }
 
@@ -246,7 +239,8 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
          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)
          {
@@ -266,7 +260,7 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
          * 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;
        }
       }
@@ -276,7 +270,7 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
     }
     else if (*rule == '!')
     {
-      DEBUG_puts("NOT");
+      DEBUG_puts("mimeAddTypeRule: NOT");
       invert = 1;
       rule ++;
     }
@@ -405,6 +399,11 @@ mimeAddTypeRule(mime_type_t *mt,   /* I - Type to add to */
          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);
       }
@@ -444,7 +443,8 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
         * 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;
 
@@ -457,8 +457,8 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
         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...
@@ -541,14 +541,12 @@ mimeFileType(mime_t     *mime,            /* I - MIME database */
 {
   _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...
@@ -576,22 +574,25 @@ mimeFileType(mime_t     *mime,            /* I - MIME database */
     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)...
@@ -602,7 +603,7 @@ mimeFileType(mime_t     *mime,              /* I - MIME database */
 
   cupsFileClose(fb.fp);
 
-  return (type);
+  return (best);
 }
 
 
@@ -647,8 +648,8 @@ compare_types(mime_type_t *t0,              /* I - First type */
   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);
 }
@@ -802,7 +803,7 @@ checkrules(const char      *filename,       /* I - Filename */
          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));
 
          /*
@@ -821,7 +822,7 @@ checkrules(const char      *filename,       /* I - Filename */
                                      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]));
@@ -837,7 +838,7 @@ checkrules(const char      *filename,       /* I - Filename */
          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 :
@@ -1038,8 +1039,8 @@ checkrules(const char      *filename,     /* I - Filename */
     * 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))
@@ -1161,5 +1162,5 @@ patmatch(const char *s,           /* I - String to match against */
 
 
 /*
- * End of "$Id: type.c 5180 2006-02-26 01:31:45Z mike $".
+ * End of "$Id: type.c 7694 2008-06-26 00:23:20Z mike $".
  */