]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/dest.c
Import CUPS v2.0.3
[thirdparty/cups.git] / cups / dest.c
index cdf1c4491570418147556295163f52691143a369..384105ffbe1bedbc7fb58c45cb9a636c96ed8d81 100644 (file)
@@ -1,85 +1,18 @@
 /*
- * "$Id: dest.c 11141 2013-07-16 14:58:25Z msweet $"
+ * "$Id: dest.c 12665 2015-05-25 15:08:55Z msweet $"
  *
- *   User-defined destination (and option) support for CUPS.
+ * User-defined destination (and option) support for CUPS.
  *
- *   Copyright 2007-2013 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products.
+ * Copyright 2007-2015 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:
- *
- *   cupsAddDest()                 - Add a destination to the list of
- *                                   destinations.
- *   _cupsAppleCopyDefaultPaperID() - Get the default paper ID.
- *   _cupsAppleCopyDefaultPrinter() - Get the default printer at this location.
- *   _cupsAppleGetUseLastPrinter()  - Get whether to use the last used printer.
- *   _cupsAppleSetDefaultPaperID()  - Set the default paper id.
- *   _cupsAppleSetDefaultPrinter()  - Set the default printer for this
- *                                   location.
- *   _cupsAppleSetUseLastPrinter()  - Set whether to use the last used printer.
- *   cupsConnectDest()             - Connect to the server for a destination.
- *   cupsConnectDestBlock()        - Connect to the server for a destination.
- *   cupsCopyDest()                - Copy a destination.
- *   cupsEnumDests()               - Enumerate available destinations with a
- *                                   callback function.
- *   cupsEnumDestsBlock()          - Enumerate available destinations with a
- *                                   block.
- *   cupsFreeDests()               - Free the memory used by the list of
- *                                   destinations.
- *   cupsGetDest()                 - Get the named destination from the list.
- *   _cupsGetDestResource()        - Get the resource path and URI for a
- *                                   destination.
- *   _cupsGetDests()               - Get destinations from a server.
- *   cupsGetDests()                - Get the list of destinations from the
- *                                   default server.
- *   cupsGetDests2()               - Get the list of destinations from the
- *                                   specified server.
- *   cupsGetNamedDest()            - Get options for the named destination.
- *   cupsRemoveDest()              - Remove a destination from the destination
- *                                   list.
- *   cupsSetDefaultDest()          - Set the default destination.
- *   cupsSetDests()                - Save the list of destinations for the
- *                                   default server.
- *   cupsSetDests2()               - Save the list of destinations for the
- *                                   specified server.
- *   _cupsUserDefault()            - Get the user default printer from
- *                                   environment variables and location
- *                                   information.
- *   appleCopyLocations()          - Copy the location history array.
- *   appleCopyNetwork()            - Get the network ID for the current
- *                                   location.
- *   appleGetPaperSize()           - Get the default paper size.
- *   appleGetPrinter()             - Get a printer from the history array.
- *   cups_add_dest()               - Add a destination to the array.
- *   cups_block_cb()               - Enumeration callback for block API.
- *   cups_compare_dests()          - Compare two destinations.
- *   cups_dnssd_browse_cb()        - Browse for printers.
- *   cups_dnssd_browse_cb()        - Browse for printers.
- *   cups_dnssd_client_cb()        - Avahi client callback function.
- *   cups_dnssd_compare_device()    - Compare two devices.
- *   cups_dnssd_free_device()      - Free the memory used by a device.
- *   cups_dnssd_get_device()       - Lookup a device and create it as needed.
- *   cups_dnssd_local_cb()         - Browse for local printers.
- *   cups_dnssd_poll_cb()          - Wait for input on the specified file
- *                                   descriptors.
- *   cups_dnssd_query_cb()         - Process query data.
- *   cups_dnssd_resolve()          - Resolve a Bonjour printer URI.
- *   cups_dnssd_resolve_cb()       - See if we should continue resolving.
- *   cups_dnssd_unquote()          - Unquote a name string.
- *   cups_find_dest()              - Find a destination using a binary search.
- *   cups_get_default()            - Get the default destination from an
- *                                   lpoptions file.
- *   cups_get_dests()              - Get destinations from a file.
- *   cups_make_string()            - Make a comma-separated string of values
- *                                   from an IPP attribute.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
@@ -334,7 +267,7 @@ cupsAddDest(const char  *name,              /* I  - Destination name */
       * Copy options from parent...
       */
 
-      dest->options = calloc(sizeof(cups_option_t), parent->num_options);
+      dest->options = calloc(sizeof(cups_option_t), (size_t)parent->num_options);
 
       if (dest->options)
       {
@@ -672,9 +605,9 @@ cupsConnectDest(
 
   if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
                       userpass, sizeof(userpass), hostname, sizeof(hostname),
-                      &port, resource, resourcesize) < HTTP_URI_STATUS_OK)
+                      &port, resource, (int)resourcesize) < HTTP_URI_STATUS_OK)
   {
-    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer URI."), 1);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer-uri."), 1);
 
     if (cb)
       (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR,
@@ -724,6 +657,7 @@ cupsConnectDest(
 
   http = httpConnect2(hostname, port, addrlist, AF_UNSPEC, encryption, 1, 0,
                       NULL);
+  httpAddrFreeList(addrlist);
 
  /*
   * Connect if requested...
@@ -842,8 +776,7 @@ cupsCopyDest(cups_dest_t *dest,
 
   if (new_dest)
   {
-    if ((new_dest->options = calloc(sizeof(cups_option_t),
-                                    dest->num_options)) == NULL)
+    if ((new_dest->options = calloc(sizeof(cups_option_t), (size_t)dest->num_options)) == NULL)
       return (cupsRemoveDest(dest->name, dest->instance, num_dests, dests));
 
     new_dest->num_options = dest->num_options;
@@ -891,6 +824,10 @@ cupsEnumDests(
                        num_dests;      /* Number of destinations */
   cups_dest_t          *dests = NULL,  /* Destinations */
                        *dest;          /* Current destination */
+  const char           *defprinter;    /* Default printer */
+  char                 name[1024],     /* Copy of printer name */
+                       *instance,      /* Pointer to instance name */
+                       *user_default;  /* User default printer */
 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   int                  count,          /* Number of queries started */
                        remaining;      /* Remainder of timeout */
@@ -936,6 +873,31 @@ cupsEnumDests(
   num_dests = _cupsGetDests(CUPS_HTTP_DEFAULT, IPP_OP_CUPS_GET_PRINTERS, NULL,
                             &dests, type, mask);
 
+  if ((user_default = _cupsUserDefault(name, sizeof(name))) != NULL)
+    defprinter = name;
+  else if ((defprinter = cupsGetDefault2(CUPS_HTTP_DEFAULT)) != NULL)
+  {
+    strlcpy(name, defprinter, sizeof(name));
+    defprinter = name;
+  }
+
+  if (defprinter)
+  {
+   /*
+    * Separate printer and instance name...
+    */
+
+    if ((instance = strchr(name, '/')) != NULL)
+      *instance++ = '\0';
+
+   /*
+    * Lookup the printer and instance and make it the default...
+    */
+
+    if ((dest = cupsGetDest(name, instance, num_dests, dests)) != NULL)
+      dest->is_default = 1;
+  }
+
   for (i = num_dests, dest = dests;
        i > 0 && (!cancel || !*cancel);
        i --, dest ++)
@@ -953,11 +915,11 @@ cupsEnumDests(
   * Get Bonjour-shared printers...
   */
 
-  data.type    = type;
-  data.mask    = mask;
-  data.devices = cupsArrayNew3((cups_array_func_t)cups_dnssd_compare_devices,
-                               NULL, NULL, 0, NULL,
-                               (cups_afree_func_t)cups_dnssd_free_device);
+  data.type      = type;
+  data.mask      = mask;
+  data.cb        = cb;
+  data.user_data = user_data;
+  data.devices   = cupsArrayNew3((cups_array_func_t)cups_dnssd_compare_devices, NULL, NULL, 0, NULL, (cups_afree_func_t)cups_dnssd_free_device);
 
 #  ifdef HAVE_DNSSD
   if (DNSServiceCreateConnection(&data.main_ref) != kDNSServiceErr_NoError)
@@ -1330,9 +1292,9 @@ _cupsGetDestResource(
 
   if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
                       userpass, sizeof(userpass), hostname, sizeof(hostname),
-                      &port, resource, resourcesize) < HTTP_URI_STATUS_OK)
+                      &port, resource, (int)resourcesize) < HTTP_URI_STATUS_OK)
   {
-    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer URI."), 1);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer-uri."), 1);
 
     return (NULL);
   }
@@ -1341,6 +1303,100 @@ _cupsGetDestResource(
 }
 
 
+/*
+ * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
+ *
+ * "name" is the desired name for the printer. If @code NULL@, a name will be
+ * created using the URI.
+ *
+ * "uri" is the "ipp" or "ipps" URI for the printer.
+ *
+ * @since CUPS 2.0/OS X 10.10@
+ */
+
+cups_dest_t *                          /* O - Destination or @code NULL@ */
+cupsGetDestWithURI(const char *name,   /* I - Desired printer name or @code NULL@ */
+                   const char *uri)    /* I - URI for the printer */
+{
+  cups_dest_t  *dest;                  /* New destination */
+  char         temp[1024],             /* Temporary string */
+               scheme[256],            /* Scheme from URI */
+               userpass[256],          /* Username:password from URI */
+               hostname[256],          /* Hostname from URI */
+               resource[1024],         /* Resource path from URI */
+               *ptr;                   /* Pointer into string */
+  int          port;                   /* Port number from URI */
+
+
+ /*
+  * Range check input...
+  */
+
+  if (!uri)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (NULL);
+  }
+
+  if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK ||
+      (strncmp(uri, "ipp://", 6) && strncmp(uri, "ipps://", 7)))
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer-uri."), 1);
+
+    return (NULL);
+  }
+
+  if (!name)
+  {
+   /*
+    * Create the name from the URI...
+    */
+
+    if (strstr(hostname, "._tcp"))
+    {
+     /*
+      * Use the service instance name...
+      */
+
+      if ((ptr = strchr(hostname, '.')) != NULL)
+        *ptr = '\0';
+
+      name = hostname;
+    }
+    else if (!strncmp(resource, "/classes/", 9))
+    {
+      snprintf(temp, sizeof(temp), "%s @ %s", resource + 9, hostname);
+      name = temp;
+    }
+    else if (!strncmp(resource, "/printers/", 10))
+    {
+      snprintf(temp, sizeof(temp), "%s @ %s", resource + 10, hostname);
+      name = temp;
+    }
+    else
+    {
+      name = hostname;
+    }
+  }
+
+ /*
+  * Create the destination...
+  */
+
+  if ((dest = calloc(1, sizeof(cups_dest_t))) == NULL)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
+    return (NULL);
+  }
+
+  dest->name        = _cupsStrAlloc(name);
+  dest->num_options = cupsAddOption("printer-uri-supported", uri, dest->num_options, &(dest->options));
+  dest->num_options = cupsAddOption("printer-info", name, dest->num_options, &(dest->options));
+
+  return (dest);
+}
+
+
 /*
  * '_cupsGetDests()' - Get destinations from a server.
  *
@@ -1457,10 +1513,8 @@ _cupsGetDests(http_t       *http,        /* I  - Connection to server or
   }
   else if (mask)
   {
-    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type",
-                  type);
-    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask",
-                  mask);
+    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type", (int)type);
+    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask", (int)mask);
   }
 
  /*
@@ -1705,10 +1759,10 @@ cupsGetDests2(http_t      *http,        /* I - Connection to server or @code CUPS_HTTP_
   if (num_dests > 0)
   {
     num_reals = num_dests;
-    reals     = calloc(num_reals, sizeof(cups_dest_t));
+    reals     = calloc((size_t)num_reals, sizeof(cups_dest_t));
 
     if (reals)
-      memcpy(reals, *dests, num_reals * sizeof(cups_dest_t));
+      memcpy(reals, *dests, (size_t)num_reals * sizeof(cups_dest_t));
     else
       num_reals = 0;
   }
@@ -1979,10 +2033,10 @@ cupsRemoveDest(const char  *name,       /* I  - Destination name */
 
   num_dests --;
 
-  i = dest - *dests;
+  i = (int)(dest - *dests);
 
   if (i < num_dests)
-    memmove(dest, dest + 1, (num_dests - i) * sizeof(cups_dest_t));
+    memmove(dest, dest + 1, (size_t)(num_dests - i) * sizeof(cups_dest_t));
 
   return (num_dests);
 }
@@ -2316,7 +2370,7 @@ _cupsUserDefault(char   *name,            /* I - Name buffer */
 
   if ((locprinter = _cupsAppleCopyDefaultPrinter()) != NULL)
   {
-    CFStringGetCString(locprinter, name, namesize, kCFStringEncodingUTF8);
+    CFStringGetCString(locprinter, name, (CFIndex)namesize, kCFStringEncodingUTF8);
     CFRelease(locprinter);
   }
   else
@@ -2524,7 +2578,7 @@ cups_add_dest(const char  *name,  /* I  - Name of destination */
   if (*num_dests == 0)
     dest = malloc(sizeof(cups_dest_t));
   else
-    dest = realloc(*dests, sizeof(cups_dest_t) * (*num_dests + 1));
+    dest = realloc(*dests, sizeof(cups_dest_t) * (size_t)(*num_dests + 1));
 
   if (!dest)
     return (NULL);
@@ -2551,8 +2605,7 @@ cups_add_dest(const char  *name,  /* I  - Name of destination */
   */
 
   if (insert < *num_dests)
-    memmove(*dests + insert + 1, *dests + insert,
-            (*num_dests - insert) * sizeof(cups_dest_t));
+    memmove(*dests + insert + 1, *dests + insert, (size_t)(*num_dests - insert) * sizeof(cups_dest_t));
 
   (*num_dests) ++;
 
@@ -3157,7 +3210,7 @@ cups_dnssd_query_cb(
       txtnext = txt + txtlen;
 
       for (ptr = key; txt < txtnext && *txt != '='; txt ++)
-       *ptr++ = *txt;
+       *ptr++ = (char)*txt;
       *ptr = '\0';
 
       if (txt < txtnext && *txt == '=')
@@ -3165,7 +3218,7 @@ cups_dnssd_query_cb(
        txt ++;
 
        if (txt < txtnext)
-         memcpy(value, txt, txtnext - txt);
+         memcpy(value, txt, (size_t)(txtnext - txt));
        value[txtnext - txt] = '\0';
 
        DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key, value));
@@ -3242,7 +3295,7 @@ cups_dnssd_query_cb(
         */
 
        saw_printer_type = 1;
-        type             = strtol(value, NULL, 0);
+        type             = (cups_ptype_t)strtol(value, NULL, 0);
       }
       else if (!saw_printer_type)
       {
@@ -3390,7 +3443,7 @@ cups_dnssd_resolve(
                             _HTTP_RESOLVE_FQDN, cups_dnssd_resolve_cb,
                             &resolve)) == NULL)
   {
-    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to resolve printer URI."), 1);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to resolve printer-uri."), 1);
 
     if (cb)
       (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR,
@@ -3844,22 +3897,21 @@ cups_make_string(
     {
       case IPP_TAG_INTEGER :
       case IPP_TAG_ENUM :
-         snprintf(ptr, end - ptr + 1, "%d", attr->values[i].integer);
+         snprintf(ptr, (size_t)(end - ptr + 1), "%d", attr->values[i].integer);
          break;
 
       case IPP_TAG_BOOLEAN :
          if (attr->values[i].boolean)
-           strlcpy(ptr, "true", end - ptr + 1);
+           strlcpy(ptr, "true", (size_t)(end - ptr + 1));
          else
-           strlcpy(ptr, "false", end - ptr + 1);
+           strlcpy(ptr, "false", (size_t)(end - ptr + 1));
          break;
 
       case IPP_TAG_RANGE :
          if (attr->values[i].range.lower == attr->values[i].range.upper)
-           snprintf(ptr, end - ptr + 1, "%d", attr->values[i].range.lower);
+           snprintf(ptr, (size_t)(end - ptr + 1), "%d", attr->values[i].range.lower);
          else
-           snprintf(ptr, end - ptr + 1, "%d-%d", attr->values[i].range.lower,
-                    attr->values[i].range.upper);
+           snprintf(ptr, (size_t)(end - ptr + 1), "%d-%d", attr->values[i].range.lower, attr->values[i].range.upper);
          break;
 
       default :
@@ -3891,5 +3943,5 @@ cups_make_string(
 
 
 /*
- * End of "$Id: dest.c 11141 2013-07-16 14:58:25Z msweet $".
+ * End of "$Id: dest.c 12665 2015-05-25 15:08:55Z msweet $".
  */