]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/options.c
Remove all of the Subversion keywords from various source files.
[thirdparty/cups.git] / cups / options.c
index b1476a49aa86a3c83a510a6b89d56c32f6c4214c..986ae1ca2cf9b628b737289ee282795f6c77a74f 100644 (file)
@@ -1,37 +1,23 @@
 /*
- * "$Id: options.c 8181 2008-12-10 17:29:57Z mike $"
+ * Option routines for CUPS.
  *
- *   Option routines for the Common UNIX Printing System (CUPS).
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2007 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 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/".
  *
- *   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/".
- *
- *   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.
- *   cupsParseOptions() - Parse options from a command-line argument.
- *   cupsRemoveOption() - Remove an option from an option array.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
  * Include necessary headers...
  */
 
-#include "cups.h"
-#include <stdlib.h>
-#include <ctype.h>
-#include "string.h"
-#include "debug.h"
+#include "cups-private.h"
 
 
 /*
@@ -61,12 +47,11 @@ cupsAddOption(const char    *name,  /* I  - Name of option */
                diff;                   /* Result of search */
 
 
-  DEBUG_printf(("cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, "
-                "options=%p)\n", name, value, num_options, options));
+  DEBUG_printf(("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, options=%p)", name, value, num_options, (void *)options));
+
   if (!name || !name[0] || !value || !options || num_options < 0)
   {
-    DEBUG_printf(("cupsAddOption: Returning %d\n", num_options));
+    DEBUG_printf(("3cupsAddOption: Returning %d", num_options));
     return (num_options);
   }
 
@@ -94,18 +79,17 @@ cupsAddOption(const char    *name,  /* I  - Name of option */
     * No matching option name...
     */
 
-    DEBUG_printf(("cupsAddOption: New option inserted at index %d...\n",
+    DEBUG_printf(("4cupsAddOption: New option inserted at index %d...",
                   insert));
 
     if (num_options == 0)
       temp = (cups_option_t *)malloc(sizeof(cups_option_t));
     else
-      temp = (cups_option_t *)realloc(*options, sizeof(cups_option_t) *
-                                               (num_options + 1));
+      temp = (cups_option_t *)realloc(*options, sizeof(cups_option_t) * (size_t)(num_options + 1));
 
-    if (temp == NULL)
+    if (!temp)
     {
-      DEBUG_puts("cupsAddOption: Unable to expand option array, returning 0");
+      DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0");
       return (0);
     }
 
@@ -113,10 +97,9 @@ cupsAddOption(const char    *name,  /* I  - Name of option */
 
     if (insert < num_options)
     {
-      DEBUG_printf(("cupsAddOption: Shifting %d options...\n",
+      DEBUG_printf(("4cupsAddOption: Shifting %d options...",
                     (int)(num_options - insert)));
-      memmove(temp + insert + 1, temp + insert,
-             (num_options - insert) * sizeof(cups_option_t));
+      memmove(temp + insert + 1, temp + insert, (size_t)(num_options - insert) * sizeof(cups_option_t));
     }
 
     temp        += insert;
@@ -129,7 +112,7 @@ cupsAddOption(const char    *name,  /* I  - Name of option */
     * Match found; free the old value...
     */
 
-    DEBUG_printf(("cupsAddOption: Option already exists at index %d...\n",
+    DEBUG_printf(("4cupsAddOption: Option already exists at index %d...",
                   insert));
 
     temp = *options + insert;
@@ -138,7 +121,7 @@ cupsAddOption(const char    *name,  /* I  - Name of option */
 
   temp->value = _cupsStrAlloc(value);
 
-  DEBUG_printf(("cupsAddOption: Returning %d\n", num_options));
+  DEBUG_printf(("3cupsAddOption: Returning %d", num_options));
 
   return (num_options);
 }
@@ -156,8 +139,7 @@ cupsFreeOptions(
   int  i;                              /* Looping var */
 
 
-  DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)\n", num_options,
-                options));
+  DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options, (void *)options));
 
   if (num_options <= 0 || !options)
     return;
@@ -185,12 +167,11 @@ cupsGetOption(const char    *name,        /* I - Name of option */
        match;                          /* Matching index */
 
 
-  DEBUG_printf(("cupsGetOption(name=\"%s\", num_options=%d, options=%p)\n",
-                name, num_options, options));
+  DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)", name, num_options, (void *)options));
 
   if (!name || num_options <= 0 || !options)
   {
-    DEBUG_puts("cupsGetOption: Returning NULL");
+    DEBUG_puts("3cupsGetOption: Returning NULL");
     return (NULL);
   }
 
@@ -198,11 +179,11 @@ cupsGetOption(const char    *name,        /* I - Name of option */
 
   if (!diff)
   {
-    DEBUG_printf(("cupsGetOption: Returning \"%s\"\n", options[match].value));
+    DEBUG_printf(("3cupsGetOption: Returning \"%s\"", options[match].value));
     return (options[match].value);
   }
 
-  DEBUG_puts("cupsGetOption: Returning NULL");
+  DEBUG_puts("3cupsGetOption: Returning NULL");
   return (NULL);
 }
 
@@ -231,8 +212,7 @@ cupsParseOptions(
        quote;                          /* Quote character */
 
 
-  DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)\n",
-                arg, num_options, options));
+  DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)", arg, num_options, (void *)options));
 
  /*
   * Range check input...
@@ -240,13 +220,13 @@ cupsParseOptions(
 
   if (!arg)
   {
-    DEBUG_printf(("cupsParseOptions: Returning %d\n", num_options));
+    DEBUG_printf(("1cupsParseOptions: Returning %d", num_options));
     return (num_options);
   }
 
   if (!options || num_options < 0)
   {
-    DEBUG_puts("cupsParseOptions: Returning 0");
+    DEBUG_puts("1cupsParseOptions: Returning 0");
     return (0);
   }
 
@@ -256,8 +236,8 @@ cupsParseOptions(
 
   if ((copyarg = strdup(arg)) == NULL)
   {
-    DEBUG_puts("cupsParseOptions: Unable to copy arg string");
-    DEBUG_printf(("cupsParseOptions: Returning %d\n", num_options));
+    DEBUG_puts("1cupsParseOptions: Unable to copy arg string");
+    DEBUG_printf(("1cupsParseOptions: Returning %d", num_options));
     return (num_options);
   }
 
@@ -282,7 +262,7 @@ cupsParseOptions(
   * Skip leading spaces...
   */
 
-  while (isspace(*ptr & 255))
+  while (_cups_isspace(*ptr))
     ptr ++;
 
  /*
@@ -296,7 +276,7 @@ cupsParseOptions(
     */
 
     name = ptr;
-    while (!isspace(*ptr & 255) && *ptr != '=' && *ptr)
+    while (!strchr("\f\n\r\t\v =", *ptr) && *ptr)
       ptr ++;
 
    /*
@@ -310,13 +290,13 @@ cupsParseOptions(
     * Skip trailing spaces...
     */
 
-    while (isspace(*ptr & 255))
+    while (_cups_isspace(*ptr))
       *ptr++ = '\0';
 
     if ((sep = *ptr) == '=')
       *ptr++ = '\0';
 
-    DEBUG_printf(("cupsParseOptions: name=\"%s\"\n", name));
+    DEBUG_printf(("2cupsParseOptions: name=\"%s\"", name));
 
     if (sep != '=')
     {
@@ -324,7 +304,7 @@ cupsParseOptions(
       * Boolean option...
       */
 
-      if (!strncasecmp(name, "no", 2))
+      if (!_cups_strncasecmp(name, "no", 2))
         num_options = cupsAddOption(name + 2, "false", num_options,
                                    options);
       else
@@ -339,7 +319,7 @@ cupsParseOptions(
 
     value = ptr;
 
-    while (*ptr && !isspace(*ptr & 255))
+    while (*ptr && !_cups_isspace(*ptr))
     {
       if (*ptr == ',')
         ptr ++;
@@ -394,7 +374,7 @@ cupsParseOptions(
        * Normal space-delimited string...
        */
 
-       while (!isspace(*ptr & 255) && *ptr)
+       while (*ptr && !_cups_isspace(*ptr))
        {
          if (*ptr == '\\' && ptr[1])
            _cups_strcpy(ptr, ptr + 1);
@@ -407,13 +387,13 @@ cupsParseOptions(
     if (*ptr != '\0')
       *ptr++ = '\0';
 
-    DEBUG_printf(("cupsParseOptions: value=\"%s\"\n", value));
+    DEBUG_printf(("2cupsParseOptions: value=\"%s\"", value));
 
    /*
     * Skip trailing whitespace...
     */
 
-    while (isspace(*ptr & 255))
+    while (_cups_isspace(*ptr))
       ptr ++;
 
    /*
@@ -430,7 +410,7 @@ cupsParseOptions(
 
   free(copyarg);
 
-  DEBUG_printf(("cupsParseOptions: Returning %d\n", num_options));
+  DEBUG_printf(("1cupsParseOptions: Returning %d", num_options));
 
   return (num_options);
 }
@@ -439,7 +419,7 @@ cupsParseOptions(
 /*
  * 'cupsRemoveOption()' - Remove an option from an option array.
  *
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                    /* O  - New number of options */
@@ -452,8 +432,7 @@ cupsRemoveOption(
   cups_option_t        *option;                /* Current option */
 
 
-  DEBUG_printf(("cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)\n",
-                name, num_options, options));
+  DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)", name, num_options, (void *)options));
 
  /*
   * Range check input...
@@ -461,7 +440,7 @@ cupsRemoveOption(
 
   if (!name || num_options < 1 || !options)
   {
-    DEBUG_printf(("cupsRemoveOption: Returning %d\n", num_options));
+    DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options));
     return (num_options);
   }
 
@@ -470,7 +449,7 @@ cupsRemoveOption(
   */
 
   for (i = num_options, option = *options; i > 0; i --, option ++)
-    if (!strcasecmp(name, option->name))
+    if (!_cups_strcasecmp(name, option->name))
       break;
 
   if (i)
@@ -479,7 +458,7 @@ cupsRemoveOption(
     * Remove this option from the array...
     */
 
-    DEBUG_puts("cupsRemoveOption: Found option, removing it...");
+    DEBUG_puts("4cupsRemoveOption: Found option, removing it...");
 
     num_options --;
     i --;
@@ -488,18 +467,101 @@ cupsRemoveOption(
     _cupsStrFree(option->value);
 
     if (i > 0)
-      memmove(option, option + 1, i * sizeof(cups_option_t));
+      memmove(option, option + 1, (size_t)i * sizeof(cups_option_t));
   }
 
  /*
   * Return the new number of options...
   */
 
-  DEBUG_printf(("cupsRemoveOption: Returning %d\n", num_options));
+  DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options));
   return (num_options);
 }
 
 
+/*
+ * '_cupsGet1284Values()' - Get 1284 device ID keys and values.
+ *
+ * The returned dictionary is a CUPS option array that can be queried with
+ * cupsGetOption and freed with cupsFreeOptions.
+ */
+
+int                                    /* O - Number of key/value pairs */
+_cupsGet1284Values(
+    const char *device_id,             /* I - IEEE-1284 device ID string */
+    cups_option_t **values)            /* O - Array of key/value pairs */
+{
+  int          num_values;             /* Number of values */
+  char         key[256],               /* Key string */
+               value[256],             /* Value string */
+               *ptr;                   /* Pointer into key/value */
+
+
+ /*
+  * Range check input...
+  */
+
+  if (values)
+    *values = NULL;
+
+  if (!device_id || !values)
+    return (0);
+
+ /*
+  * Parse the 1284 device ID value into keys and values.  The format is
+  * repeating sequences of:
+  *
+  *   [whitespace]key:value[whitespace];
+  */
+
+  num_values = 0;
+  while (*device_id)
+  {
+    while (_cups_isspace(*device_id))
+      device_id ++;
+
+    if (!*device_id)
+      break;
+
+    for (ptr = key; *device_id && *device_id != ':'; device_id ++)
+      if (ptr < (key + sizeof(key) - 1))
+        *ptr++ = *device_id;
+
+    if (!*device_id)
+      break;
+
+    while (ptr > key && _cups_isspace(ptr[-1]))
+      ptr --;
+
+    *ptr = '\0';
+    device_id ++;
+
+    while (_cups_isspace(*device_id))
+      device_id ++;
+
+    if (!*device_id)
+      break;
+
+    for (ptr = value; *device_id && *device_id != ';'; device_id ++)
+      if (ptr < (value + sizeof(value) - 1))
+        *ptr++ = *device_id;
+
+    if (!*device_id)
+      break;
+
+    while (ptr > value && _cups_isspace(ptr[-1]))
+      ptr --;
+
+    *ptr = '\0';
+    device_id ++;
+
+    num_values = cupsAddOption(key, value, num_values, values);
+  }
+
+  return (num_values);
+}
+
+
 /*
  * 'cups_compare_options()' - Compare two options.
  */
@@ -508,7 +570,7 @@ static int                          /* O - Result of comparison */
 cups_compare_options(cups_option_t *a, /* I - First option */
                     cups_option_t *b)  /* I - Second option */
 {
-  return (strcasecmp(a->name, b->name));
+  return (_cups_strcasecmp(a->name, b->name));
 }
 
 
@@ -531,13 +593,11 @@ cups_find_option(
   cups_option_t        key;                    /* Search key */
 
 
-  DEBUG_printf(("cups_find_option(name=\"%s\", num_options=%d, options=%p, "
-               "prev=%d, rdiff=%p)\n", name, num_options, options, prev,
-               rdiff));
+  DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, prev=%d, rdiff=%p)", name, num_options, (void *)options, prev, (void *)rdiff));
 
 #ifdef DEBUG
   for (left = 0; left < num_options; left ++)
-    DEBUG_printf(("cups_find_option: options[%d].name=\"%s\", .value=\"%s\"\n",
+    DEBUG_printf(("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"",
                   left, options[left].name, options[left].value));
 #endif /* DEBUG */
 
@@ -622,8 +682,3 @@ cups_find_option(
 
   return (current);
 }
-
-
-/*
- * End of "$Id: options.c 8181 2008-12-10 17:29:57Z mike $".
- */