]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/options.c
Save work on documentation.
[thirdparty/cups.git] / cups / options.c
index 0c9069c096d5625848f0e178cf64432ef551afd8..9aa20f8954ec5fe0acdb0acd0ed38317ac44c77a 100644 (file)
@@ -1,40 +1,23 @@
 /*
- * "$Id: options.c 8181 2008-12-10 17:29:57Z mike $"
+ * Option routines for CUPS.
  *
- *   Option routines for CUPS.
+ * Copyright 2007-2017 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products.
  *
- *   Copyright 2007-2010 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
+ * 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.
- *   _cupsGet1284Values()   - Get 1284 device ID keys and values.
- *   cups_compare_options() - Compare two options.
- *   cups_find_option()     - Find an option using a binary search.
+ * 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"
 
 
 /*
@@ -46,6 +29,31 @@ static int   cups_find_option(const char *name, int num_options,
                                 cups_option_t *option, int prev, int *rdiff);
 
 
+/*
+ * 'cupsAddIntegerOption()' - Add an integer option to an option array.
+ *
+ * New option arrays can be initialized simply by passing 0 for the
+ * "num_options" parameter.
+ *
+ * @since CUPS 2.2.4/macOS 10.13@
+ */
+
+int                                    /* O  - Number of options */
+cupsAddIntegerOption(
+    const char    *name,               /* I  - Name of option */
+    int           value,               /* I  - Value of option */
+    int           num_options,         /* I  - Number of options */
+    cups_option_t **options)           /* IO - Pointer to options */
+{
+  char strvalue[32];                   /* String value */
+
+
+  snprintf(strvalue, sizeof(strvalue), "%d", value);
+
+  return (cupsAddOption(name, strvalue, num_options, options));
+}
+
+
 /*
  * 'cupsAddOption()' - Add an option to an option array.
  *
@@ -64,9 +72,8 @@ cupsAddOption(const char    *name,    /* I  - Name of option */
                diff;                   /* Result of search */
 
 
-  DEBUG_printf(("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, "
-                "options=%p)", 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(("3cupsAddOption: Returning %d", num_options));
@@ -103,10 +110,9 @@ cupsAddOption(const char    *name, /* I  - Name of option */
     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("3cupsAddOption: Unable to expand option array, returning 0");
       return (0);
@@ -118,8 +124,7 @@ cupsAddOption(const char    *name,  /* I  - Name of option */
     {
       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;
@@ -159,8 +164,7 @@ cupsFreeOptions(
   int  i;                              /* Looping var */
 
 
-  DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options,
-                options));
+  DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options, (void *)options));
 
   if (num_options <= 0 || !options)
     return;
@@ -175,6 +179,38 @@ cupsFreeOptions(
 }
 
 
+/*
+ * 'cupsGetIntegerOption()' - Get an integer option value.
+ *
+ * INT_MIN is returned when the option does not exist, is not an integer, or
+ * exceeds the range of values for the "int" type.
+ *
+ * @since CUPS 2.2.4/macOS 10.13@
+ */
+
+int                                    /* O - Option value or @code INT_MIN@ */
+cupsGetIntegerOption(
+    const char    *name,               /* I - Name of option */
+    int           num_options,         /* I - Number of options */
+    cups_option_t *options)            /* I - Options */
+{
+  const char   *value = cupsGetOption(name, num_options, options);
+                                       /* String value of option */
+  char         *ptr;                   /* Pointer into string value */
+  long         intvalue;               /* Integer value */
+
+
+  if (!value || !*value)
+    return (INT_MIN);
+
+  intvalue = strtol(value, &ptr, 10);
+  if (intvalue < INT_MIN || intvalue > INT_MAX || *ptr)
+    return (INT_MIN);
+
+  return ((int)intvalue);
+}
+
+
 /*
  * 'cupsGetOption()' - Get an option value.
  */
@@ -188,8 +224,7 @@ cupsGetOption(const char    *name,  /* I - Name of option */
        match;                          /* Matching index */
 
 
-  DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)",
-                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)
   {
@@ -234,8 +269,7 @@ cupsParseOptions(
        quote;                          /* Quote character */
 
 
-  DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)",
-                arg, num_options, options));
+  DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)", arg, num_options, (void *)options));
 
  /*
   * Range check input...
@@ -285,7 +319,7 @@ cupsParseOptions(
   * Skip leading spaces...
   */
 
-  while (isspace(*ptr & 255))
+  while (_cups_isspace(*ptr))
     ptr ++;
 
  /*
@@ -299,7 +333,7 @@ cupsParseOptions(
     */
 
     name = ptr;
-    while (!isspace(*ptr & 255) && *ptr != '=' && *ptr)
+    while (!strchr("\f\n\r\t\v =", *ptr) && *ptr)
       ptr ++;
 
    /*
@@ -313,7 +347,7 @@ cupsParseOptions(
     * Skip trailing spaces...
     */
 
-    while (isspace(*ptr & 255))
+    while (_cups_isspace(*ptr))
       *ptr++ = '\0';
 
     if ((sep = *ptr) == '=')
@@ -327,7 +361,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
@@ -342,7 +376,7 @@ cupsParseOptions(
 
     value = ptr;
 
-    while (*ptr && !isspace(*ptr & 255))
+    while (*ptr && !_cups_isspace(*ptr))
     {
       if (*ptr == ',')
         ptr ++;
@@ -397,7 +431,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);
@@ -416,7 +450,7 @@ cupsParseOptions(
     * Skip trailing whitespace...
     */
 
-    while (isspace(*ptr & 255))
+    while (_cups_isspace(*ptr))
       ptr ++;
 
    /*
@@ -442,7 +476,7 @@ cupsParseOptions(
 /*
  * 'cupsRemoveOption()' - Remove an option from an option array.
  *
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 int                                    /* O  - New number of options */
@@ -455,8 +489,7 @@ cupsRemoveOption(
   cups_option_t        *option;                /* Current option */
 
 
-  DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)",
-                name, num_options, options));
+  DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)", name, num_options, (void *)options));
 
  /*
   * Range check input...
@@ -473,7 +506,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)
@@ -491,7 +524,7 @@ 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));
   }
 
  /*
@@ -541,7 +574,7 @@ _cupsGet1284Values(
   num_values = 0;
   while (*device_id)
   {
-    while (isspace(*device_id & 255))
+    while (_cups_isspace(*device_id))
       device_id ++;
 
     if (!*device_id)
@@ -554,13 +587,13 @@ _cupsGet1284Values(
     if (!*device_id)
       break;
 
-    while (ptr > key && isspace(ptr[-1] & 255))
+    while (ptr > key && _cups_isspace(ptr[-1]))
       ptr --;
 
     *ptr = '\0';
     device_id ++;
 
-    while (isspace(*device_id & 255))
+    while (_cups_isspace(*device_id))
       device_id ++;
 
     if (!*device_id)
@@ -573,7 +606,7 @@ _cupsGet1284Values(
     if (!*device_id)
       break;
 
-    while (ptr > value && isspace(ptr[-1] & 255))
+    while (ptr > value && _cups_isspace(ptr[-1]))
       ptr --;
 
     *ptr = '\0';
@@ -594,7 +627,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));
 }
 
 
@@ -617,9 +650,7 @@ cups_find_option(
   cups_option_t        key;                    /* Search key */
 
 
-  DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, "
-               "prev=%d, rdiff=%p)", 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 ++)
@@ -708,8 +739,3 @@ cups_find_option(
 
   return (current);
 }
-
-
-/*
- * End of "$Id: options.c 8181 2008-12-10 17:29:57Z mike $".
- */