]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/options.c
Merge changes from CUPS 1.4svn-r7282.
[thirdparty/cups.git] / cups / options.c
index f90285656e30bd80e2335a7ef9abea7fe78d9fce..4ffb39f8244aa05359fb4b0078b9cf18df6c1278 100644 (file)
@@ -1,36 +1,26 @@
 /*
- * "$Id: options.c 177 2006-06-21 00:20:03Z jlovell $"
+ * "$Id: options.c 7278 2008-01-31 01:23:09Z mike $"
  *
  *   Option routines for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 1997-2006 by Easy Software Products.
+ *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 1997-2007 by Easy Software Products.
  *
  *   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/".
  *
  *   This file is subject to the Apple OS-Developed Software exception.
  *
  * Contents:
  *
- *   cupsAddOption()     - Add an option to an option array.
- *   cupsFreeOptions()   - Free all memory used by options.
- *   cupsGetOption()     - Get an option value.
- *   cupsMarkOptions()   - Mark command-line options in a PPD file.
- *   cupsParseOptions()  - Parse options from a command-line argument.
- *   cupsRemoveOptions() - Remove an option from an option array.
+ *   cupsAddOption()    - Add an option to an option array.
+ *   cupsFreeOptions()  - Free all memory used by options.
+ *   cupsGetOption()    - Get an option value.
+ *   cupsParseOptions() - Parse options from a command-line argument.
+ *   cupsRemoveOption() - Remove an option from an option array.
  */
 
 /*
 
 /*
  * 'cupsAddOption()' - Add an option to an option array.
+ *
+ * New option arrays can be initialized simply by passing 0 for the
+ * "num_options" parameter.
  */
 
-int                                    /* O - Number of options */
-cupsAddOption(const char    *name,     /* I - Name of option */
-              const char    *value,    /* I - Value of option */
-             int           num_options,/* I - Number of options */
+int                                    /* O  - Number of options */
+cupsAddOption(const char    *name,     /* I  - Name of option */
+              const char    *value,    /* I  - Value of option */
+             int           num_options,/* I  - Number of options */
               cups_option_t **options) /* IO - Pointer to options */
 {
   int          i;                      /* Looping var */
@@ -67,7 +60,7 @@ cupsAddOption(const char    *name,    /* I - Name of option */
   */
 
   for (i = 0, temp = *options; i < num_options; i ++, temp ++)
-    if (strcasecmp(temp->name, name) == 0)
+    if (!strcasecmp(temp->name, name))
       break;
 
   if (i >= num_options)
@@ -87,7 +80,7 @@ cupsAddOption(const char    *name,    /* I - Name of option */
 
     *options    = temp;
     temp        += num_options;
-    temp->name  = strdup(name);
+    temp->name  = _cupsStrAlloc(name);
     num_options ++;
   }
   else
@@ -96,10 +89,10 @@ cupsAddOption(const char    *name,  /* I - Name of option */
     * Match found; free the old value...
     */
 
-    free(temp->value);
+    _cupsStrFree(temp->value);
   }
 
-  temp->value = strdup(value);
+  temp->value = _cupsStrAlloc(value);
 
   return (num_options);
 }
@@ -122,8 +115,8 @@ cupsFreeOptions(
 
   for (i = 0; i < num_options; i ++)
   {
-    free(options[i].name);
-    free(options[i].value);
+    _cupsStrFree(options[i].name);
+    _cupsStrFree(options[i].value);
   }
 
   free(options);
@@ -134,7 +127,7 @@ cupsFreeOptions(
  * 'cupsGetOption()' - Get an option value.
  */
 
-const char *                           /* O - Option value or NULL */
+const char *                           /* O - Option value or @code NULL@ */
 cupsGetOption(const char    *name,     /* I - Name of option */
               int           num_options,/* I - Number of options */
               cups_option_t *options)  /* I - Options */
@@ -153,238 +146,14 @@ cupsGetOption(const char    *name,       /* I - Name of option */
 }
 
 
-/*
- * 'cupsMarkOptions()' - Mark command-line options in a PPD file.
- */
-
-int                                    /* O - 1 if conflicting */
-cupsMarkOptions(
-    ppd_file_t    *ppd,                        /* I - PPD file */
-    int           num_options,         /* I - Number of options */
-    cups_option_t *options)            /* I - Options */
-{
-  int          i, j, k;                /* Looping vars */
-  int          conflict;               /* Option conflicts */
-  char         *val,                   /* Pointer into value */
-               *ptr,                   /* Pointer into string */
-               s[255];                 /* Temporary string */
-  const char   *page_size;             /* PageSize option */
-  cups_option_t        *optptr;                /* Current option */
-  ppd_option_t *option;                /* PPD option */
-  static const char * const duplex_options[] =
-               {                       /* Duplex option names */
-                 "Duplex",             /* Adobe */
-                 "EFDuplex",           /* EFI */
-                 "EFDuplexing",        /* EFI */
-                 "KD03Duplex",         /* Kodak */
-                 "JCLDuplex"           /* Samsung */
-               };
-  static const char * const duplex_one[] =
-               {                       /* one-sided names */
-                 "None",
-                 "False"
-               };
-  static const char * const duplex_two_long[] =
-               {                       /* two-sided-long-edge names */
-                 "DuplexNoTumble",     /* Adobe */
-                 "LongEdge",           /* EFI */
-                 "Top"                 /* EFI */
-               };
-  static const char * const duplex_two_short[] =
-               {                       /* two-sided-long-edge names */
-                 "DuplexTumble",       /* Adobe */
-                 "ShortEdge",          /* EFI */
-                 "Bottom"              /* EFI */
-               };
-
-
- /*
-  * Check arguments...
-  */
-
-  if (ppd == NULL || num_options <= 0 || options == NULL)
-    return (0);
-
- /*
-  * Mark options...
-  */
-
-  conflict  = 0;
-
-  for (i = num_options, optptr = options; i > 0; i --, optptr ++)
-    if (!strcasecmp(optptr->name, "media"))
-    {
-     /*
-      * Loop through the option string, separating it at commas and
-      * marking each individual option as long as the corresponding
-      * PPD option (PageSize, InputSlot, etc.) is not also set.
-      *
-      * For PageSize, we also check for an empty option value since
-      * some versions of MacOS X use it to specify auto-selection
-      * of the media based solely on the size.
-      */
-
-      page_size = cupsGetOption("PageSize", num_options, options);
-
-      for (val = optptr->value; *val;)
-      {
-       /*
-        * Extract the sub-option from the string...
-       */
-
-        for (ptr = s; *val && *val != ',' && (ptr - s) < (sizeof(s) - 1);)
-         *ptr++ = *val++;
-       *ptr++ = '\0';
-
-       if (*val == ',')
-         val ++;
-
-       /*
-        * Mark it...
-       */
-
-        if (!page_size || !page_size[0])
-         if (ppdMarkOption(ppd, "PageSize", s))
-            conflict = 1;
-
-        if (cupsGetOption("InputSlot", num_options, options) == NULL)
-         if (ppdMarkOption(ppd, "InputSlot", s))
-            conflict = 1;
-
-        if (cupsGetOption("MediaType", num_options, options) == NULL)
-         if (ppdMarkOption(ppd, "MediaType", s))
-            conflict = 1;
-
-        if (cupsGetOption("EFMediaType", num_options, options) == NULL)
-         if (ppdMarkOption(ppd, "EFMediaType", s))
-            conflict = 1;
-
-        if (cupsGetOption("EFMediaQualityMode", num_options, options) == NULL)
-         if (ppdMarkOption(ppd, "EFMediaQualityMode", s))      /* EFI */
-            conflict = 1;
-
-       if (strcasecmp(s, "manual") == 0 &&
-           cupsGetOption("ManualFeed", num_options, options) == NULL)
-          if (ppdMarkOption(ppd, "ManualFeed", "True"))
-           conflict = 1;
-      }
-    }
-    else if (!strcasecmp(optptr->name, "sides"))
-    {
-      for (j = 0; j < (int)(sizeof(duplex_options) / sizeof(duplex_options[0])); j ++)
-        if (cupsGetOption(duplex_options[j], num_options, options) != NULL)
-         break;
-
-      if (j < (int)(sizeof(duplex_options) / sizeof(duplex_options[0])))
-      {
-       /*
-        * Don't override the PPD option with the IPP attribute...
-       */
-
-        continue;
-      }
-
-      if (!strcasecmp(optptr->value, "one-sided"))
-      {
-       /*
-        * Mark the appropriate duplex option for one-sided output...
-       */
-
-        for (j = 0; j < (int)(sizeof(duplex_options) / sizeof(duplex_options[0])); j ++)
-         if ((option = ppdFindOption(ppd, duplex_options[j])) != NULL)
-           break;
-
-       if (j < (int)(sizeof(duplex_options) / sizeof(duplex_options[0])))
-       {
-          for (k = 0; k < (int)(sizeof(duplex_one) / sizeof(duplex_one[0])); k ++)
-            if (ppdFindChoice(option, duplex_one[k]))
-           {
-             if (ppdMarkOption(ppd, duplex_options[j], duplex_one[k]))
-               conflict = 1;
-
-             break;
-            }
-        }
-      }
-      else if (!strcasecmp(optptr->value, "two-sided-long-edge"))
-      {
-       /*
-        * Mark the appropriate duplex option for two-sided-long-edge output...
-       */
-
-        for (j = 0; j < (int)(sizeof(duplex_options) / sizeof(duplex_options[0])); j ++)
-         if ((option = ppdFindOption(ppd, duplex_options[j])) != NULL)
-           break;
-
-       if (j < (int)(sizeof(duplex_options) / sizeof(duplex_options[0])))
-       {
-          for (k = 0; k < (int)(sizeof(duplex_two_long) / sizeof(duplex_two_long[0])); k ++)
-            if (ppdFindChoice(option, duplex_two_long[k]))
-           {
-             if (ppdMarkOption(ppd, duplex_options[j], duplex_two_long[k]))
-               conflict = 1;
-
-             break;
-            }
-        }
-      }
-      else if (!strcasecmp(optptr->value, "two-sided-short-edge"))
-      {
-       /*
-        * Mark the appropriate duplex option for two-sided-short-edge output...
-       */
-
-        for (j = 0; j < (int)(sizeof(duplex_options) / sizeof(duplex_options[0])); j ++)
-         if ((option = ppdFindOption(ppd, duplex_options[j])) != NULL)
-           break;
-
-       if (j < (int)(sizeof(duplex_options) / sizeof(duplex_options[0])))
-       {
-          for (k = 0; k < (int)(sizeof(duplex_two_short) / sizeof(duplex_two_short[0])); k ++)
-            if (ppdFindChoice(option, duplex_two_short[k]))
-           {
-             if (ppdMarkOption(ppd, duplex_options[j], duplex_two_short[k]))
-               conflict = 1;
-
-             break;
-            }
-        }
-      }
-    }
-    else if (!strcasecmp(optptr->name, "resolution") ||
-             !strcasecmp(optptr->name, "printer-resolution"))
-    {
-      if (ppdMarkOption(ppd, "Resolution", optptr->value))
-        conflict = 1;
-      if (ppdMarkOption(ppd, "SetResolution", optptr->value))
-       /* Calcomp, Linotype, QMS, Summagraphics, Tektronix, Varityper */
-        conflict = 1;
-      if (ppdMarkOption(ppd, "JCLResolution", optptr->value))  /* HP */
-        conflict = 1;
-      if (ppdMarkOption(ppd, "CNRes_PGP", optptr->value))      /* Canon */
-        conflict = 1;
-    }
-    else if (!strcasecmp(optptr->name, "output-bin"))
-    {
-      if (cupsGetOption("OutputBin", num_options, options) == NULL)
-        if (ppdMarkOption(ppd, "OutputBin", optptr->value))
-          conflict = 1;
-    }
-    else if (ppdMarkOption(ppd, optptr->name, optptr->value))
-      conflict = 1;
-
-  return (conflict);
-}
-
-
 /*
  * 'cupsParseOptions()' - Parse options from a command-line argument.
  *
  * This function converts space-delimited name/value pairs according
  * to the PAPI text option ABNF specification. Collection values
  * ("name={a=... b=... c=...}") are stored with the curley brackets
- * intact - use cupsParseOptions() on the value to extract the collection
- * attributes.
+ * intact - use @code cupsParseOptions@ on the value to extract the
+ * collection attributes.
  */
 
 int                                    /* O - Number of options found */
@@ -396,18 +165,28 @@ cupsParseOptions(
   char *copyarg,                       /* Copy of input string */
        *ptr,                           /* Pointer into string */
        *name,                          /* Pointer to name */
-       *value;                         /* Pointer to value */
+       *value,                         /* Pointer to value */
+       quote;                          /* Quote character */
 
 
-  if (arg == NULL || options == NULL || num_options < 0)
+ /*
+  * Range check input...
+  */
+
+  if (!arg)
+    return (num_options);
+
+  if (!options || num_options < 0)
     return (0);
 
  /*
   * Make a copy of the argument string and then divide it up...
   */
 
-  copyarg     = strdup(arg);
-  ptr         = copyarg;
+  if ((copyarg = strdup(arg)) == NULL)
+    return (num_options);
+
+  ptr = copyarg;
 
  /*
   * Skip leading spaces...
@@ -427,7 +206,7 @@ cupsParseOptions(
     */
 
     name = ptr;
-    while (!isspace(*ptr & 255) && *ptr != '=' && *ptr != '\0')
+    while (!isspace(*ptr & 255) && *ptr != '=' && *ptr)
       ptr ++;
 
    /*
@@ -447,10 +226,10 @@ cupsParseOptions(
     if (*ptr != '=')
     {
      /*
-      * Start of another option...
+      * Boolean option...
       */
 
-      if (strncasecmp(name, "no", 2) == 0)
+      if (!strncasecmp(name, "no", 2))
         num_options = cupsAddOption(name + 2, "false", num_options,
                                    options);
       else
@@ -465,38 +244,18 @@ cupsParseOptions(
 
     *ptr++ = '\0';
 
-    if (*ptr == '\'')
+    if (*ptr == '\'' || *ptr == '\"')
     {
      /*
       * Quoted string constant...
       */
 
-      ptr ++;
-      value = ptr;
-
-      while (*ptr != '\'' && *ptr != '\0')
-      {
-        if (*ptr == '\\')
-         _cups_strcpy(ptr, ptr + 1);
-
-        ptr ++;
-      }
-
-      if (*ptr != '\0')
-        *ptr++ = '\0';
-    }
-    else if (*ptr == '\"')
-    {
-     /*
-      * Double-quoted string constant...
-      */
-
-      ptr ++;
+      quote = *ptr++;
       value = ptr;
 
-      while (*ptr != '\"' && *ptr != '\0')
+      while (*ptr != quote && *ptr)
       {
-        if (*ptr == '\\')
+        if (*ptr == '\\' && ptr[1])
          _cups_strcpy(ptr, ptr + 1);
 
         ptr ++;
@@ -529,7 +288,7 @@ cupsParseOptions(
              break;
          }
         }
-        else if (*ptr == '\\')
+        else if (*ptr == '\\' && ptr[1])
          _cups_strcpy(ptr, ptr + 1);
 
       if (*ptr != '\0')
@@ -543,9 +302,9 @@ cupsParseOptions(
 
       value = ptr;
 
-      while (!isspace(*ptr & 255) && *ptr != '\0')
+      while (!isspace(*ptr & 255) && *ptr)
       {
-        if (*ptr == '\\')
+        if (*ptr == '\\' && ptr[1])
          _cups_strcpy(ptr, ptr + 1);
 
         ptr ++;
@@ -578,7 +337,7 @@ cupsParseOptions(
 
 
 /*
- * 'cupsRemoveOptions()' - Remove an option from an option array.
+ * 'cupsRemoveOption()' - Remove an option from an option array.
  *
  * @since CUPS 1.2@
  */
@@ -617,12 +376,11 @@ cupsRemoveOption(
     num_options --;
     i --;
 
-    free(option->name);
-    if (option->value)
-      free(option->value);
+    _cupsStrFree(option->name);
+    _cupsStrFree(option->value);
 
     if (i > 0)
-      memmove(option, option + 1, i * sizeof(cups_option_t *));
+      memmove(option, option + 1, i * sizeof(cups_option_t));
   }
 
  /*
@@ -634,5 +392,5 @@ cupsRemoveOption(
 
 
 /*
- * End of "$Id: options.c 177 2006-06-21 00:20:03Z jlovell $".
+ * End of "$Id: options.c 7278 2008-01-31 01:23:09Z mike $".
  */