]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/dest.c
Import CUPS v2.0b1
[thirdparty/cups.git] / cups / dest.c
index 7b30340a5facf22f62a6b795e3a544e1d4e13ec5..ab36a0660025f698846c1e986617cfd5f27da534 100644 (file)
@@ -1,85 +1,18 @@
 /*
- * "$Id: dest.c 11688 2014-03-05 21:11:32Z msweet $"
+ * "$Id: dest.c 11959 2014-06-26 18:30:19Z 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-2014 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,
@@ -842,8 +775,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;
@@ -1359,9 +1291,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);
   }
@@ -1370,6 +1302,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@
+ */
+
+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.
  *
@@ -1486,10 +1512,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);
   }
 
  /*
@@ -1734,10 +1758,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;
   }
@@ -2008,10 +2032,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);
 }
@@ -2345,7 +2369,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
@@ -2553,7 +2577,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);
@@ -2580,8 +2604,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) ++;
 
@@ -3186,7 +3209,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 == '=')
@@ -3194,7 +3217,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));
@@ -3271,7 +3294,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)
       {
@@ -3419,7 +3442,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,
@@ -3873,22 +3896,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 :
@@ -3920,5 +3942,5 @@ cups_make_string(
 
 
 /*
- * End of "$Id: dest.c 11688 2014-03-05 21:11:32Z msweet $".
+ * End of "$Id: dest.c 11959 2014-06-26 18:30:19Z msweet $".
  */