2 * User-defined destination (and option) support for CUPS.
4 * Copyright © 2007-2019 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "cups-private.h"
16 #include "debug-internal.h"
21 #endif /* HAVE_NOTIFY_H */
25 #endif /* HAVE_POLL */
29 #endif /* HAVE_DNSSD */
32 # include <avahi-client/client.h>
33 # include <avahi-client/lookup.h>
34 # include <avahi-common/simple-watch.h>
35 # include <avahi-common/domain.h>
36 # include <avahi-common/error.h>
37 # include <avahi-common/malloc.h>
38 #define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
39 #endif /* HAVE_AVAHI */
47 # if HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
48 # include <SystemConfiguration/SystemConfiguration.h>
49 # define _CUPS_LOCATION_DEFAULTS 1
50 # endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
51 # define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
52 # define kDefaultPaperIDKey CFSTR("DefaultPaperID")
53 # define kLastUsedPrintersKey CFSTR("LastUsedPrinters")
54 # define kLocationNetworkKey CFSTR("Network")
55 # define kLocationPrinterIDKey CFSTR("PrinterID")
56 # define kUseLastPrinter CFSTR("UseLastPrinter")
57 #endif /* __APPLE__ */
59 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
60 # define _CUPS_DNSSD_GET_DESTS 250 /* Milliseconds for cupsGetDests */
61 # define _CUPS_DNSSD_MAXTIME 50 /* Milliseconds for maximum quantum of time */
63 # define _CUPS_DNSSD_GET_DESTS 0 /* Milliseconds for cupsGetDests */
64 #endif /* HAVE_DNSSD || HAVE_AVAHI */
71 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
72 typedef enum _cups_dnssd_state_e
/* Enumerated device state */
78 _CUPS_DNSSD_INCOMPATIBLE
,
80 } _cups_dnssd_state_t
;
82 typedef struct _cups_dnssd_data_s
/* Enumeration data */
85 DNSServiceRef main_ref
; /* Main service reference */
86 # else /* HAVE_AVAHI */
87 AvahiSimplePoll
*simple_poll
; /* Polling interface */
88 AvahiClient
*client
; /* Client information */
89 int got_data
; /* Did we get data? */
90 int browsers
; /* How many browsers are running? */
91 # endif /* HAVE_DNSSD */
92 cups_dest_cb_t cb
; /* Callback */
93 void *user_data
; /* User data pointer */
94 cups_ptype_t type
, /* Printer type filter */
95 mask
; /* Printer type mask */
96 cups_array_t
*devices
; /* Devices found so far */
97 int num_dests
; /* Number of lpoptions destinations */
98 cups_dest_t
*dests
; /* lpoptions destinations */
99 char def_name
[1024], /* Default printer name, if any */
100 *def_instance
; /* Default printer instance, if any */
101 } _cups_dnssd_data_t
;
103 typedef struct _cups_dnssd_device_s
/* Enumerated device */
105 _cups_dnssd_state_t state
; /* State of device listing */
107 DNSServiceRef ref
; /* Service reference for query */
108 # else /* HAVE_AVAHI */
109 AvahiRecordBrowser
*ref
; /* Browser for query */
110 # endif /* HAVE_DNSSD */
111 char *fullName
, /* Full name */
112 *regtype
, /* Registration type */
113 *domain
; /* Domain name */
114 cups_ptype_t type
; /* Device registration type */
115 cups_dest_t dest
; /* Destination record */
116 } _cups_dnssd_device_t
;
118 typedef struct _cups_dnssd_resolve_s
/* Data for resolving URI */
120 int *cancel
; /* Pointer to "cancel" variable */
121 struct timeval end_time
; /* Ending time */
122 } _cups_dnssd_resolve_t
;
123 #endif /* HAVE_DNSSD */
125 typedef struct _cups_getdata_s
127 int num_dests
; /* Number of destinations */
128 cups_dest_t
*dests
; /* Destinations */
129 char def_name
[1024], /* Default printer name, if any */
130 *def_instance
; /* Default printer instance, if any */
133 typedef struct _cups_namedata_s
135 const char *name
; /* Named destination */
136 cups_dest_t
*dest
; /* Destination */
144 #if _CUPS_LOCATION_DEFAULTS
145 static CFArrayRef
appleCopyLocations(void);
146 static CFStringRef
appleCopyNetwork(void);
147 #endif /* _CUPS_LOCATION_DEFAULTS */
149 static char *appleGetPaperSize(char *name
, size_t namesize
);
150 #endif /* __APPLE__ */
151 #if _CUPS_LOCATION_DEFAULTS
152 static CFStringRef
appleGetPrinter(CFArrayRef locations
,
153 CFStringRef network
, CFIndex
*locindex
);
154 #endif /* _CUPS_LOCATION_DEFAULTS */
155 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
156 int *num_dests
, cups_dest_t
**dests
);
158 static int cups_block_cb(cups_dest_block_t block
, unsigned flags
,
160 #endif /* __BLOCKS__ */
161 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
162 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
164 static void cups_dnssd_browse_cb(DNSServiceRef sdRef
,
165 DNSServiceFlags flags
,
166 uint32_t interfaceIndex
,
167 DNSServiceErrorType errorCode
,
168 const char *serviceName
,
170 const char *replyDomain
,
172 # else /* HAVE_AVAHI */
173 static void cups_dnssd_browse_cb(AvahiServiceBrowser
*browser
,
174 AvahiIfIndex interface
,
175 AvahiProtocol protocol
,
176 AvahiBrowserEvent event
,
177 const char *serviceName
,
179 const char *replyDomain
,
180 AvahiLookupResultFlags flags
,
182 static void cups_dnssd_client_cb(AvahiClient
*client
,
183 AvahiClientState state
,
185 # endif /* HAVE_DNSSD */
186 static int cups_dnssd_compare_devices(_cups_dnssd_device_t
*a
,
187 _cups_dnssd_device_t
*b
);
188 static void cups_dnssd_free_device(_cups_dnssd_device_t
*device
,
189 _cups_dnssd_data_t
*data
);
190 static _cups_dnssd_device_t
*
191 cups_dnssd_get_device(_cups_dnssd_data_t
*data
,
192 const char *serviceName
,
194 const char *replyDomain
);
196 static void cups_dnssd_query_cb(DNSServiceRef sdRef
,
197 DNSServiceFlags flags
,
198 uint32_t interfaceIndex
,
199 DNSServiceErrorType errorCode
,
200 const char *fullName
,
201 uint16_t rrtype
, uint16_t rrclass
,
202 uint16_t rdlen
, const void *rdata
,
203 uint32_t ttl
, void *context
);
204 # else /* HAVE_AVAHI */
205 static int cups_dnssd_poll_cb(struct pollfd
*pollfds
,
206 unsigned int num_pollfds
,
207 int timeout
, void *context
);
208 static void cups_dnssd_query_cb(AvahiRecordBrowser
*browser
,
209 AvahiIfIndex interface
,
210 AvahiProtocol protocol
,
211 AvahiBrowserEvent event
,
212 const char *name
, uint16_t rrclass
,
213 uint16_t rrtype
, const void *rdata
,
215 AvahiLookupResultFlags flags
,
217 # endif /* HAVE_DNSSD */
218 static const char *cups_dnssd_resolve(cups_dest_t
*dest
, const char *uri
,
219 int msec
, int *cancel
,
220 cups_dest_cb_t cb
, void *user_data
);
221 static int cups_dnssd_resolve_cb(void *context
);
222 static void cups_dnssd_unquote(char *dst
, const char *src
,
224 static int cups_elapsed(struct timeval
*t
);
225 #endif /* HAVE_DNSSD || HAVE_AVAHI */
226 static int cups_enum_dests(http_t
*http
, unsigned flags
, int msec
, int *cancel
, cups_ptype_t type
, cups_ptype_t mask
, cups_dest_cb_t cb
, void *user_data
);
227 static int cups_find_dest(const char *name
, const char *instance
,
228 int num_dests
, cups_dest_t
*dests
, int prev
,
230 static int cups_get_cb(_cups_getdata_t
*data
, unsigned flags
, cups_dest_t
*dest
);
231 static char *cups_get_default(const char *filename
, char *namebuf
,
232 size_t namesize
, const char **instance
);
233 static int cups_get_dests(const char *filename
, const char *match_name
, const char *match_inst
, int load_all
, int user_default_set
, int num_dests
, cups_dest_t
**dests
);
234 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
236 static int cups_name_cb(_cups_namedata_t
*data
, unsigned flags
, cups_dest_t
*dest
);
237 static void cups_queue_name(char *name
, const char *serviceName
, size_t namesize
);
241 * 'cupsAddDest()' - Add a destination to the list of destinations.
243 * This function cannot be used to add a new class or printer queue,
244 * it only adds a new container of saved options for the named
245 * destination or instance.
247 * If the named destination already exists, the destination list is
248 * returned unchanged. Adding a new instance of a destination creates
249 * a copy of that destination's options.
251 * Use the @link cupsSaveDests@ function to save the updated list of
252 * destinations to the user's lpoptions file.
255 int /* O - New number of destinations */
256 cupsAddDest(const char *name
, /* I - Destination name */
257 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
258 int num_dests
, /* I - Number of destinations */
259 cups_dest_t
**dests
) /* IO - Destinations */
261 int i
; /* Looping var */
262 cups_dest_t
*dest
; /* Destination pointer */
263 cups_dest_t
*parent
= NULL
; /* Parent destination */
264 cups_option_t
*doption
, /* Current destination option */
265 *poption
; /* Current parent option */
271 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
273 if (instance
&& !cupsGetDest(name
, NULL
, num_dests
, *dests
))
276 if ((dest
= cups_add_dest(name
, instance
, &num_dests
, dests
)) == NULL
)
280 * Find the base dest again now the array has been realloc'd.
283 parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
);
285 if (instance
&& parent
&& parent
->num_options
> 0)
288 * Copy options from parent...
291 dest
->options
= calloc(sizeof(cups_option_t
), (size_t)parent
->num_options
);
295 dest
->num_options
= parent
->num_options
;
297 for (i
= dest
->num_options
, doption
= dest
->options
,
298 poption
= parent
->options
;
300 i
--, doption
++, poption
++)
302 doption
->name
= _cupsStrRetain(poption
->name
);
303 doption
->value
= _cupsStrRetain(poption
->value
);
315 * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
318 CFStringRef
/* O - Default paper ID */
319 _cupsAppleCopyDefaultPaperID(void)
321 return (CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
322 kCUPSPrintingPrefs
));
327 * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location.
330 CFStringRef
/* O - Default printer name */
331 _cupsAppleCopyDefaultPrinter(void)
333 # if _CUPS_LOCATION_DEFAULTS
334 CFStringRef network
; /* Network location */
335 CFArrayRef locations
; /* Location array */
336 CFStringRef locprinter
; /* Current printer */
340 * Use location-based defaults only if "use last printer" is selected in the
341 * system preferences...
344 if (!_cupsAppleGetUseLastPrinter())
346 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as "
352 * Get the current location...
355 if ((network
= appleCopyNetwork()) == NULL
)
357 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current "
363 * Lookup the network in the preferences...
366 if ((locations
= appleCopyLocations()) == NULL
)
369 * Missing or bad location array, so no location-based default...
372 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used "
380 DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.",
381 (int)CFArrayGetCount(locations
)));
383 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
384 CFRetain(locprinter
);
387 CFRelease(locations
);
393 # endif /* _CUPS_LOCATION_DEFAULTS */
398 * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
401 int /* O - 1 to use last printer, 0 otherwise */
402 _cupsAppleGetUseLastPrinter(void)
404 Boolean uselast
, /* Use last printer preference value */
405 uselast_set
; /* Valid is set? */
408 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
411 uselast
= CFPreferencesGetAppBooleanValue(kUseLastPrinter
,
422 * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
426 _cupsAppleSetDefaultPaperID(
427 CFStringRef name
) /* I - New paper ID */
429 CFPreferencesSetAppValue(kDefaultPaperIDKey
, name
, kCUPSPrintingPrefs
);
430 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
432 # ifdef HAVE_NOTIFY_POST
433 notify_post("com.apple.printerPrefsChange");
434 # endif /* HAVE_NOTIFY_POST */
439 * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location.
443 _cupsAppleSetDefaultPrinter(
444 CFStringRef name
) /* I - Default printer/class name */
446 # if _CUPS_LOCATION_DEFAULTS
447 CFStringRef network
; /* Current network */
448 CFArrayRef locations
; /* Old locations array */
449 CFIndex locindex
; /* Index in locations array */
450 CFStringRef locprinter
; /* Current printer */
451 CFMutableArrayRef newlocations
; /* New locations array */
452 CFMutableDictionaryRef newlocation
; /* New location */
456 * Get the current location...
459 if ((network
= appleCopyNetwork()) == NULL
)
461 DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network...");
466 * Lookup the network in the preferences...
469 if ((locations
= appleCopyLocations()) != NULL
)
470 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
477 if (!locprinter
|| CFStringCompare(locprinter
, name
, 0) != kCFCompareEqualTo
)
480 * Need to change the locations array...
485 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
489 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
492 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
493 &kCFTypeArrayCallBacks
);
495 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
496 &kCFTypeDictionaryKeyCallBacks
,
497 &kCFTypeDictionaryValueCallBacks
);
499 if (newlocation
&& newlocations
)
502 * Put the new location at the front of the array...
505 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
506 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, name
);
507 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
510 * Limit the number of locations to 10...
513 while (CFArrayGetCount(newlocations
) > 10)
514 CFArrayRemoveValueAtIndex(newlocations
, 10);
517 * Push the changes out...
520 CFPreferencesSetAppValue(kLastUsedPrintersKey
, newlocations
,
522 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
524 # ifdef HAVE_NOTIFY_POST
525 notify_post("com.apple.printerPrefsChange");
526 # endif /* HAVE_NOTIFY_POST */
530 CFRelease(newlocations
);
533 CFRelease(newlocation
);
537 CFRelease(locations
);
543 # endif /* _CUPS_LOCATION_DEFAULTS */
548 * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
552 _cupsAppleSetUseLastPrinter(
553 int uselast
) /* O - 1 to use last printer, 0 otherwise */
555 CFPreferencesSetAppValue(kUseLastPrinter
,
556 uselast
? kCFBooleanTrue
: kCFBooleanFalse
,
558 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
560 # ifdef HAVE_NOTIFY_POST
561 notify_post("com.apple.printerPrefsChange");
562 # endif /* HAVE_NOTIFY_POST */
564 #endif /* __APPLE__ */
568 * 'cupsConnectDest()' - Open a connection to the destination.
570 * Connect to the destination, returning a new @code http_t@ connection object
571 * and optionally the resource path to use for the destination. These calls
572 * will block until a connection is made, the timeout expires, the integer
573 * pointed to by "cancel" is non-zero, or the callback function (or block)
574 * returns 0. The caller is responsible for calling @link httpClose@ on the
575 * returned connection.
577 * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
578 * for the "flags" argument to connect directly to the device associated with
579 * the destination. Otherwise, the connection is made to the CUPS scheduler
580 * associated with the destination.
582 * @since CUPS 1.6/macOS 10.8@
585 http_t
* /* O - Connection to destination or @code NULL@ */
587 cups_dest_t
*dest
, /* I - Destination */
588 unsigned flags
, /* I - Connection flags */
589 int msec
, /* I - Timeout in milliseconds */
590 int *cancel
, /* I - Pointer to "cancel" variable */
591 char *resource
, /* I - Resource buffer */
592 size_t resourcesize
, /* I - Size of resource buffer */
593 cups_dest_cb_t cb
, /* I - Callback function */
594 void *user_data
) /* I - User data pointer */
596 const char *uri
; /* Printer URI */
597 char scheme
[32], /* URI scheme */
598 userpass
[256], /* Username and password (unused) */
599 hostname
[256], /* Hostname */
600 tempresource
[1024]; /* Temporary resource buffer */
601 int port
; /* Port number */
602 char portstr
[16]; /* Port number string */
603 http_encryption_t encryption
; /* Encryption to use */
604 http_addrlist_t
*addrlist
; /* Address list for server */
605 http_t
*http
; /* Connection to server */
608 DEBUG_printf(("cupsConnectDest(dest=%p, flags=0x%x, msec=%d, cancel=%p(%d), resource=\"%s\", resourcesize=" CUPS_LLFMT
", cb=%p, user_data=%p)", (void *)dest
, flags
, msec
, (void *)cancel
, cancel
? *cancel
: -1, resource
, CUPS_LLCAST resourcesize
, (void *)cb
, user_data
));
611 * Range check input...
619 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
623 if (!resource
|| resourcesize
< 1)
625 resource
= tempresource
;
626 resourcesize
= sizeof(tempresource
);
630 * Grab the printer URI...
633 if (flags
& CUPS_DEST_FLAGS_DEVICE
)
635 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
637 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
638 if (strstr(uri
, "._tcp"))
639 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
640 #endif /* HAVE_DNSSD || HAVE_AVAHI */
643 else if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
645 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
647 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
648 if (strstr(uri
, "._tcp"))
649 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
650 #endif /* HAVE_DNSSD || HAVE_AVAHI */
654 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, tempresource
, sizeof(tempresource
));
658 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
660 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
666 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
669 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
674 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
675 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
676 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
678 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
681 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
688 * Lookup the address for the server...
692 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
694 snprintf(portstr
, sizeof(portstr
), "%d", port
);
696 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portstr
)) == NULL
)
699 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
704 if (cancel
&& *cancel
)
706 httpAddrFreeList(addrlist
);
709 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CANCELED
, dest
);
715 * Create the HTTP object pointing to the server referenced by the URI...
718 if (!strcmp(scheme
, "ipps") || port
== 443)
719 encryption
= HTTP_ENCRYPTION_ALWAYS
;
721 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
723 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, encryption
, 1, 0, NULL
);
724 httpAddrFreeList(addrlist
);
727 * Connect if requested...
730 if (flags
& CUPS_DEST_FLAGS_UNCONNECTED
)
733 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
, dest
);
738 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
740 if (!httpReconnect2(http
, msec
, cancel
) && cb
)
742 if (cancel
&& *cancel
)
743 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
745 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
748 (*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
);
757 * 'cupsConnectDestBlock()' - Open a connection to the destination.
759 * Connect to the destination, returning a new @code http_t@ connection object
760 * and optionally the resource path to use for the destination. These calls
761 * will block until a connection is made, the timeout expires, the integer
762 * pointed to by "cancel" is non-zero, or the block returns 0. The caller is
763 * responsible for calling @link httpClose@ on the returned connection.
765 * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
766 * for the "flags" argument to connect directly to the device associated with
767 * the destination. Otherwise, the connection is made to the CUPS scheduler
768 * associated with the destination.
770 * @since CUPS 1.6/macOS 10.8@ @exclude all@
773 http_t
* /* O - Connection to destination or @code NULL@ */
774 cupsConnectDestBlock(
775 cups_dest_t
*dest
, /* I - Destination */
776 unsigned flags
, /* I - Connection flags */
777 int msec
, /* I - Timeout in milliseconds */
778 int *cancel
, /* I - Pointer to "cancel" variable */
779 char *resource
, /* I - Resource buffer */
780 size_t resourcesize
, /* I - Size of resource buffer */
781 cups_dest_block_t block
) /* I - Callback block */
783 return (cupsConnectDest(dest
, flags
, msec
, cancel
, resource
, resourcesize
,
784 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
786 #endif /* __BLOCKS__ */
790 * 'cupsCopyDest()' - Copy a destination.
792 * Make a copy of the destination to an array of destinations (or just a single
793 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
794 * for calling cupsFreeDests() on the returned object(s).
796 * @since CUPS 1.6/macOS 10.8@
799 int /* O - New number of destinations */
800 cupsCopyDest(cups_dest_t
*dest
, /* I - Destination to copy */
801 int num_dests
, /* I - Number of destinations */
802 cups_dest_t
**dests
) /* IO - Destination array */
804 int i
; /* Looping var */
805 cups_dest_t
*new_dest
; /* New destination pointer */
806 cups_option_t
*new_option
, /* Current destination option */
807 *option
; /* Current parent option */
811 * Range check input...
814 if (!dest
|| num_dests
< 0 || !dests
)
818 * See if the destination already exists...
821 if ((new_dest
= cupsGetDest(dest
->name
, dest
->instance
, num_dests
,
825 * Protect against copying destination to itself...
828 if (new_dest
== dest
)
832 * Otherwise, free the options...
835 cupsFreeOptions(new_dest
->num_options
, new_dest
->options
);
837 new_dest
->num_options
= 0;
838 new_dest
->options
= NULL
;
841 new_dest
= cups_add_dest(dest
->name
, dest
->instance
, &num_dests
, dests
);
845 new_dest
->is_default
= dest
->is_default
;
847 if ((new_dest
->options
= calloc(sizeof(cups_option_t
), (size_t)dest
->num_options
)) == NULL
)
848 return (cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
, dests
));
850 new_dest
->num_options
= dest
->num_options
;
852 for (i
= dest
->num_options
, option
= dest
->options
,
853 new_option
= new_dest
->options
;
855 i
--, option
++, new_option
++)
857 new_option
->name
= _cupsStrRetain(option
->name
);
858 new_option
->value
= _cupsStrRetain(option
->value
);
867 * '_cupsCreateDest()' - Create a local (temporary) queue.
870 char * /* O - Printer URI or @code NULL@ on error */
871 _cupsCreateDest(const char *name
, /* I - Printer name */
872 const char *info
, /* I - Printer description of @code NULL@ */
873 const char *device_id
, /* I - 1284 Device ID or @code NULL@ */
874 const char *device_uri
, /* I - Device URI */
875 char *uri
, /* I - Printer URI buffer */
876 size_t urisize
) /* I - Size of URI buffer */
878 http_t
*http
; /* Connection to server */
879 ipp_t
*request
, /* CUPS-Create-Local-Printer request */
880 *response
; /* CUPS-Create-Local-Printer response */
881 ipp_attribute_t
*attr
; /* printer-uri-supported attribute */
882 ipp_pstate_t state
= IPP_PSTATE_STOPPED
;
883 /* printer-state value */
886 if (!name
|| !device_uri
|| !uri
|| urisize
< 32)
889 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
)) == NULL
)
892 request
= ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER
);
894 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, "ipp://localhost/");
895 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
897 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
, device_uri
);
898 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
, name
);
900 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
, info
);
902 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
904 response
= cupsDoRequest(http
, request
, "/");
906 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
907 strlcpy(uri
, ippGetString(attr
, 0, NULL
), urisize
);
915 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
916 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
918 while (state
== IPP_PSTATE_STOPPED
&& cupsLastError() == IPP_STATUS_OK
)
923 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
925 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
926 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
927 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "printer-state");
929 response
= cupsDoRequest(http
, request
, "/");
931 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
932 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
944 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
946 * Destinations are enumerated from one or more sources. The callback function
947 * receives the @code user_data@ pointer and the destination pointer which can
948 * be used as input to the @link cupsCopyDest@ function. The function must
949 * return 1 to continue enumeration or 0 to stop.
951 * The @code type@ and @code mask@ arguments allow the caller to filter the
952 * destinations that are enumerated. Passing 0 for both will enumerate all
953 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
954 * destinations that are available but have not yet been added locally.
956 * Enumeration happens on the current thread and does not return until all
957 * destinations have been enumerated or the callback function returns 0.
959 * Note: The callback function will likely receive multiple updates for the same
960 * destinations - it is up to the caller to suppress any duplicate destinations.
962 * @since CUPS 1.6/macOS 10.8@
965 int /* O - 1 on success, 0 on failure */
967 unsigned flags
, /* I - Enumeration flags */
968 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
969 int *cancel
, /* I - Pointer to "cancel" variable */
970 cups_ptype_t type
, /* I - Printer type bits */
971 cups_ptype_t mask
, /* I - Mask for printer type bits */
972 cups_dest_cb_t cb
, /* I - Callback function */
973 void *user_data
) /* I - User data */
975 return (cups_enum_dests(CUPS_HTTP_DEFAULT
, flags
, msec
, cancel
, type
, mask
, cb
, user_data
));
981 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
983 * Destinations are enumerated from one or more sources. The block receives the
984 * @code user_data@ pointer and the destination pointer which can be used as
985 * input to the @link cupsCopyDest@ function. The block must return 1 to
986 * continue enumeration or 0 to stop.
988 * The @code type@ and @code mask@ arguments allow the caller to filter the
989 * destinations that are enumerated. Passing 0 for both will enumerate all
990 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
991 * destinations that are available but have not yet been added locally.
993 * Enumeration happens on the current thread and does not return until all
994 * destinations have been enumerated or the block returns 0.
996 * Note: The block will likely receive multiple updates for the same
997 * destinations - it is up to the caller to suppress any duplicate destinations.
999 * @since CUPS 1.6/macOS 10.8@ @exclude all@
1002 int /* O - 1 on success, 0 on failure */
1004 unsigned flags
, /* I - Enumeration flags */
1005 int timeout
, /* I - Timeout in milliseconds, 0 for indefinite */
1006 int *cancel
, /* I - Pointer to "cancel" variable */
1007 cups_ptype_t type
, /* I - Printer type bits */
1008 cups_ptype_t mask
, /* I - Mask for printer type bits */
1009 cups_dest_block_t block
) /* I - Block */
1011 return (cupsEnumDests(flags
, timeout
, cancel
, type
, mask
,
1012 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
1014 # endif /* __BLOCKS__ */
1018 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1022 cupsFreeDests(int num_dests
, /* I - Number of destinations */
1023 cups_dest_t
*dests
) /* I - Destinations */
1025 int i
; /* Looping var */
1026 cups_dest_t
*dest
; /* Current destination */
1029 if (num_dests
== 0 || dests
== NULL
)
1032 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1034 _cupsStrFree(dest
->name
);
1035 _cupsStrFree(dest
->instance
);
1037 cupsFreeOptions(dest
->num_options
, dest
->options
);
1045 * 'cupsGetDest()' - Get the named destination from the list.
1047 * Use the @link cupsEnumDests@ or @link cupsGetDests2@ functions to get a
1048 * list of supported destinations for the current user.
1051 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
1052 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1053 const char *instance
, /* I - Instance name or @code NULL@ */
1054 int num_dests
, /* I - Number of destinations */
1055 cups_dest_t
*dests
) /* I - Destinations */
1057 int diff
, /* Result of comparison */
1058 match
; /* Matching index */
1061 if (num_dests
<= 0 || !dests
)
1067 * NULL name for default printer.
1070 while (num_dests
> 0)
1072 if (dests
->is_default
)
1082 * Lookup name and optionally the instance...
1085 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
1088 return (dests
+ match
);
1096 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1099 const char * /* O - URI */
1100 _cupsGetDestResource(
1101 cups_dest_t
*dest
, /* I - Destination */
1102 unsigned flags
, /* I - Destination flags */
1103 char *resource
, /* I - Resource buffer */
1104 size_t resourcesize
) /* I - Size of resource buffer */
1106 const char *uri
, /* URI */
1107 *device_uri
, /* Device URI */
1108 *printer_uri
; /* Printer URI */
1109 char scheme
[32], /* URI scheme */
1110 userpass
[256], /* Username and password (unused) */
1111 hostname
[256]; /* Hostname */
1112 int port
; /* Port number */
1115 DEBUG_printf(("_cupsGetDestResource(dest=%p(%s), flags=%u, resource=%p, resourcesize=%d)", (void *)dest
, dest
->name
, flags
, (void *)resource
, (int)resourcesize
));
1118 * Range check input...
1121 if (!dest
|| !resource
|| resourcesize
< 1)
1126 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1131 * Grab the printer and device URIs...
1134 device_uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
);
1135 printer_uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
1137 DEBUG_printf(("1_cupsGetDestResource: device-uri=\"%s\", printer-uri-supported=\"%s\".", device_uri
, printer_uri
));
1139 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1140 if (((flags
& CUPS_DEST_FLAGS_DEVICE
) || !printer_uri
) && strstr(device_uri
, "._tcp"))
1142 if ((device_uri
= cups_dnssd_resolve(dest
, device_uri
, 5000, NULL
, NULL
, NULL
)) != NULL
)
1144 DEBUG_printf(("1_cupsGetDestResource: Resolved device-uri=\"%s\".", device_uri
));
1148 DEBUG_puts("1_cupsGetDestResource: Unable to resolve device.");
1153 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1158 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1160 if (flags
& CUPS_DEST_FLAGS_DEVICE
)
1164 else if (printer_uri
)
1170 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, device_uri
, resource
, resourcesize
);
1174 DEBUG_printf(("1_cupsGetDestResource: Local printer-uri-supported=\"%s\"", uri
));
1176 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
1178 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
1184 DEBUG_puts("1_cupsGetDestResource: No printer-uri-supported or device-uri found.");
1189 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1193 else if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
1195 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad URI."), 1);
1200 DEBUG_printf(("1_cupsGetDestResource: resource=\"%s\"", resource
));
1207 * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
1209 * "name" is the desired name for the printer. If @code NULL@, a name will be
1210 * created using the URI.
1212 * "uri" is the "ipp" or "ipps" URI for the printer.
1214 * @since CUPS 2.0/macOS 10.10@
1217 cups_dest_t
* /* O - Destination or @code NULL@ */
1218 cupsGetDestWithURI(const char *name
, /* I - Desired printer name or @code NULL@ */
1219 const char *uri
) /* I - URI for the printer */
1221 cups_dest_t
*dest
; /* New destination */
1222 char temp
[1024], /* Temporary string */
1223 scheme
[256], /* Scheme from URI */
1224 userpass
[256], /* Username:password from URI */
1225 hostname
[256], /* Hostname from URI */
1226 resource
[1024], /* Resource path from URI */
1227 *ptr
; /* Pointer into string */
1228 const char *info
; /* printer-info string */
1229 int port
; /* Port number from URI */
1233 * Range check input...
1238 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1242 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
||
1243 (strncmp(uri
, "ipp://", 6) && strncmp(uri
, "ipps://", 7)))
1245 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1257 * Create the name from the URI...
1260 if (strstr(hostname
, "._tcp"))
1263 * Use the service instance name...
1266 if ((ptr
= strstr(hostname
, "._")) != NULL
)
1269 cups_queue_name(temp
, hostname
, sizeof(temp
));
1273 else if (!strncmp(resource
, "/classes/", 9))
1275 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 9, hostname
);
1276 name
= resource
+ 9;
1279 else if (!strncmp(resource
, "/printers/", 10))
1281 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 10, hostname
);
1282 name
= resource
+ 10;
1285 else if (!strncmp(resource
, "/ipp/print/", 11))
1287 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 11, hostname
);
1288 name
= resource
+ 11;
1299 * Create the destination...
1302 if ((dest
= calloc(1, sizeof(cups_dest_t
))) == NULL
)
1304 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1308 dest
->name
= _cupsStrAlloc(name
);
1309 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &(dest
->options
));
1310 dest
->num_options
= cupsAddOption("printer-info", info
, dest
->num_options
, &(dest
->options
));
1317 * '_cupsGetDests()' - Get destinations from a server.
1319 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1320 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1323 * "name" is the name of an existing printer and is only used when "op" is
1324 * IPP_OP_GET_PRINTER_ATTRIBUTES.
1326 * "dest" is initialized to point to the array of destinations.
1328 * 0 is returned if there are no printers, no default printer, or the named
1329 * printer does not exist, respectively.
1331 * Free the memory used by the destination array using the @link cupsFreeDests@
1334 * Note: On macOS this function also gets the default paper from the system
1335 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1336 * options array for each destination that supports it.
1339 int /* O - Number of destinations */
1340 _cupsGetDests(http_t
*http
, /* I - Connection to server or
1341 * @code CUPS_HTTP_DEFAULT@ */
1342 ipp_op_t op
, /* I - IPP operation */
1343 const char *name
, /* I - Name of destination */
1344 cups_dest_t
**dests
, /* IO - Destinations */
1345 cups_ptype_t type
, /* I - Printer type bits */
1346 cups_ptype_t mask
) /* I - Printer type mask */
1348 int num_dests
= 0; /* Number of destinations */
1349 cups_dest_t
*dest
; /* Current destination */
1350 ipp_t
*request
, /* IPP Request */
1351 *response
; /* IPP Response */
1352 ipp_attribute_t
*attr
; /* Current attribute */
1353 const char *printer_name
; /* printer-name attribute */
1354 char uri
[1024]; /* printer-uri value */
1355 int num_options
; /* Number of options */
1356 cups_option_t
*options
; /* Options */
1358 char media_default
[41]; /* Default paper size */
1359 #endif /* __APPLE__ */
1360 char optname
[1024], /* Option name */
1361 value
[2048], /* Option value */
1362 *ptr
; /* Pointer into name/value */
1363 static const char * const pattrs
[] = /* Attributes we're interested in */
1365 "auth-info-required",
1367 "job-sheets-default",
1368 "marker-change-time",
1370 "marker-high-levels",
1372 "marker-low-levels",
1378 #endif /* __APPLE__ */
1382 "printer-is-accepting-jobs",
1383 "printer-is-shared",
1384 "printer-is-temporary",
1386 "printer-make-and-model",
1387 "printer-mandatory-job-attributes",
1390 "printer-state-change-time",
1391 "printer-state-reasons",
1393 "printer-uri-supported"
1397 DEBUG_printf(("_cupsGetDests(http=%p, op=%x(%s), name=\"%s\", dests=%p, type=%x, mask=%x)", (void *)http
, op
, ippOpString(op
), name
, (void *)dests
, type
, mask
));
1401 * Get the default paper size...
1404 appleGetPaperSize(media_default
, sizeof(media_default
));
1405 DEBUG_printf(("1_cupsGetDests: Default media is '%s'.", media_default
));
1406 #endif /* __APPLE__ */
1409 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
1410 * require the following attributes:
1412 * attributes-charset
1413 * attributes-natural-language
1414 * requesting-user-name
1415 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
1418 request
= ippNewRequest(op
);
1420 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1421 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1424 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1425 "requesting-user-name", NULL
, cupsUser());
1427 if (name
&& op
!= IPP_OP_CUPS_GET_DEFAULT
)
1429 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1430 "localhost", ippPort(), "/printers/%s", name
);
1431 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1436 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type", (int)type
);
1437 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask", (int)mask
);
1441 * Do the request and get back a response...
1444 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1446 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1449 * Skip leading attributes until we hit a printer...
1452 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1459 * Pull the needed attributes from this printer...
1462 printer_name
= NULL
;
1466 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1468 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1469 attr
->value_tag
!= IPP_TAG_ENUM
&&
1470 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1471 attr
->value_tag
!= IPP_TAG_TEXT
&&
1472 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1473 attr
->value_tag
!= IPP_TAG_NAME
&&
1474 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1475 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1476 attr
->value_tag
!= IPP_TAG_RANGE
&&
1477 attr
->value_tag
!= IPP_TAG_URI
)
1480 if (!strcmp(attr
->name
, "auth-info-required") ||
1481 !strcmp(attr
->name
, "device-uri") ||
1482 !strcmp(attr
->name
, "marker-change-time") ||
1483 !strcmp(attr
->name
, "marker-colors") ||
1484 !strcmp(attr
->name
, "marker-high-levels") ||
1485 !strcmp(attr
->name
, "marker-levels") ||
1486 !strcmp(attr
->name
, "marker-low-levels") ||
1487 !strcmp(attr
->name
, "marker-message") ||
1488 !strcmp(attr
->name
, "marker-names") ||
1489 !strcmp(attr
->name
, "marker-types") ||
1490 !strcmp(attr
->name
, "printer-commands") ||
1491 !strcmp(attr
->name
, "printer-info") ||
1492 !strcmp(attr
->name
, "printer-is-shared") ||
1493 !strcmp(attr
->name
, "printer-is-temporary") ||
1494 !strcmp(attr
->name
, "printer-make-and-model") ||
1495 !strcmp(attr
->name
, "printer-mandatory-job-attributes") ||
1496 !strcmp(attr
->name
, "printer-state") ||
1497 !strcmp(attr
->name
, "printer-state-change-time") ||
1498 !strcmp(attr
->name
, "printer-type") ||
1499 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1500 !strcmp(attr
->name
, "printer-location") ||
1501 !strcmp(attr
->name
, "printer-state-reasons") ||
1502 !strcmp(attr
->name
, "printer-uri-supported"))
1505 * Add a printer description attribute...
1508 num_options
= cupsAddOption(attr
->name
,
1509 cups_make_string(attr
, value
,
1511 num_options
, &options
);
1514 else if (!strcmp(attr
->name
, "media-supported") && media_default
[0])
1517 * See if we can set a default media size...
1520 int i
; /* Looping var */
1522 for (i
= 0; i
< attr
->num_values
; i
++)
1523 if (!_cups_strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1525 DEBUG_printf(("1_cupsGetDests: Setting media to '%s'.", media_default
));
1526 num_options
= cupsAddOption("media", media_default
, num_options
, &options
);
1530 #endif /* __APPLE__ */
1531 else if (!strcmp(attr
->name
, "printer-name") &&
1532 attr
->value_tag
== IPP_TAG_NAME
)
1533 printer_name
= attr
->values
[0].string
.text
;
1534 else if (strncmp(attr
->name
, "notify-", 7) &&
1535 strncmp(attr
->name
, "print-quality-", 14) &&
1536 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1537 attr
->value_tag
== IPP_TAG_ENUM
||
1538 attr
->value_tag
== IPP_TAG_INTEGER
||
1539 attr
->value_tag
== IPP_TAG_KEYWORD
||
1540 attr
->value_tag
== IPP_TAG_NAME
||
1541 attr
->value_tag
== IPP_TAG_RANGE
) &&
1542 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1545 * Add a default option...
1548 strlcpy(optname
, attr
->name
, sizeof(optname
));
1549 optname
[ptr
- attr
->name
] = '\0';
1551 if (_cups_strcasecmp(optname
, "media") || !cupsGetOption("media", num_options
, options
))
1552 num_options
= cupsAddOption(optname
, cups_make_string(attr
, value
, sizeof(value
)), num_options
, &options
);
1557 * See if we have everything needed...
1562 cupsFreeOptions(num_options
, options
);
1570 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1572 dest
->num_options
= num_options
;
1573 dest
->options
= options
;
1576 cupsFreeOptions(num_options
, options
);
1582 ippDelete(response
);
1586 * Return the count...
1594 * 'cupsGetDests()' - Get the list of destinations from the default server.
1596 * Starting with CUPS 1.2, the returned list of destinations include the
1597 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1598 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1599 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1600 * attributes as options.
1602 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1603 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1604 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1606 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1607 * be used. The "printer-uri-supported" option will be present for those IPP
1608 * printers that have been recently used.
1610 * Use the @link cupsFreeDests@ function to free the destination list and
1611 * the @link cupsGetDest@ function to find a particular destination.
1616 int /* O - Number of destinations */
1617 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
1619 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
1624 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
1626 * Starting with CUPS 1.2, the returned list of destinations include the
1627 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1628 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1629 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1630 * attributes as options.
1632 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1633 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1634 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1636 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1637 * be used. The "printer-uri-supported" option will be present for those IPP
1638 * printers that have been recently used.
1640 * Use the @link cupsFreeDests@ function to free the destination list and
1641 * the @link cupsGetDest@ function to find a particular destination.
1643 * @since CUPS 1.1.21/macOS 10.4@
1646 int /* O - Number of destinations */
1647 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1648 cups_dest_t
**dests
) /* O - Destinations */
1650 _cups_getdata_t data
; /* Enumeration data */
1653 DEBUG_printf(("cupsGetDests2(http=%p, dests=%p)", (void *)http
, (void *)dests
));
1656 * Range check the input...
1661 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad NULL dests pointer"), 1);
1662 DEBUG_puts("1cupsGetDests2: NULL dests pointer, returning 0.");
1667 * Connect to the server as needed...
1672 if ((http
= _cupsConnect()) == NULL
)
1681 * Grab the printers and classes...
1687 if (!httpAddrLocalhost(httpGetAddress(http
)))
1690 * When talking to a remote cupsd, just enumerate printers on the remote
1694 cups_enum_dests(http
, 0, _CUPS_DNSSD_GET_DESTS
, NULL
, 0, CUPS_PRINTER_DISCOVERED
, (cups_dest_cb_t
)cups_get_cb
, &data
);
1699 * When talking to a local cupsd, enumerate both local printers and ones we
1700 * can find on the network...
1703 cups_enum_dests(http
, 0, _CUPS_DNSSD_GET_DESTS
, NULL
, 0, 0, (cups_dest_cb_t
)cups_get_cb
, &data
);
1707 * Return the number of destinations...
1710 *dests
= data
.dests
;
1712 if (data
.num_dests
> 0)
1713 _cupsSetError(IPP_STATUS_OK
, NULL
, 0);
1715 DEBUG_printf(("1cupsGetDests2: Returning %d destinations.", data
.num_dests
));
1717 return (data
.num_dests
);
1722 * 'cupsGetNamedDest()' - Get options for the named destination.
1724 * This function is optimized for retrieving a single destination and should
1725 * be used instead of @link cupsGetDests2@ and @link cupsGetDest@ when you
1726 * either know the name of the destination or want to print to the default
1727 * destination. If @code NULL@ is returned, the destination does not exist or
1728 * there is no default destination.
1730 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
1731 * server will be used.
1733 * If "name" is @code NULL@, the default printer for the current user will be
1736 * The returned destination must be freed using @link cupsFreeDests@ with a
1737 * "num_dests" value of 1.
1739 * @since CUPS 1.4/macOS 10.6@
1742 cups_dest_t
* /* O - Destination or @code NULL@ */
1743 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1744 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1745 const char *instance
) /* I - Instance name or @code NULL@ */
1747 const char *dest_name
; /* Working destination name */
1748 cups_dest_t
*dest
; /* Destination */
1749 char filename
[1024], /* Path to lpoptions */
1750 defname
[256]; /* Default printer name */
1751 const char *home
= getenv("HOME"); /* Home directory */
1752 int set_as_default
= 0; /* Set returned destination as default */
1753 ipp_op_t op
= IPP_OP_GET_PRINTER_ATTRIBUTES
;
1754 /* IPP operation to get server ops */
1755 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1758 DEBUG_printf(("cupsGetNamedDest(http=%p, name=\"%s\", instance=\"%s\")", (void *)http
, name
, instance
));
1761 * If "name" is NULL, find the default destination...
1769 dest_name
= _cupsUserDefault(defname
, sizeof(defname
));
1773 char *ptr
; /* Temporary pointer... */
1775 if ((ptr
= strchr(defname
, '/')) != NULL
)
1786 * No default in the environment, try the user's lpoptions files...
1789 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1791 dest_name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
1800 * Still not there? Try the system lpoptions file...
1803 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1804 dest_name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
1813 * No locally-set default destination, ask the server...
1816 op
= IPP_OP_CUPS_GET_DEFAULT
;
1819 DEBUG_puts("1cupsGetNamedDest: Asking server for default printer...");
1822 DEBUG_printf(("1cupsGetNamedDest: Using name=\"%s\"...", name
));
1826 * Get the printer's attributes...
1829 if (!_cupsGetDests(http
, op
, dest_name
, &dest
, 0, 0))
1833 _cups_namedata_t data
; /* Callback data */
1835 DEBUG_puts("1cupsGetNamedDest: No queue found for printer, looking on network...");
1840 cupsEnumDests(0, 1000, NULL
, 0, 0, (cups_dest_cb_t
)cups_name_cb
, &data
);
1849 switch (set_as_default
)
1854 case 1 : /* Set from env vars */
1855 if (getenv("LPDEST"))
1856 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("LPDEST environment variable names default destination that does not exist."), 1);
1857 else if (getenv("PRINTER"))
1858 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("PRINTER environment variable names default destination that does not exist."), 1);
1860 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("No default destination."), 1);
1863 case 2 : /* Set from ~/.cups/lpoptions */
1864 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("~/.cups/lpoptions file names default destination that does not exist."), 1);
1867 case 3 : /* Set from /etc/cups/lpoptions */
1868 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("/etc/cups/lpoptions file names default destination that does not exist."), 1);
1871 case 4 : /* Set from server */
1872 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("No default destination."), 1);
1880 DEBUG_printf(("1cupsGetNamedDest: Got dest=%p", (void *)dest
));
1883 dest
->instance
= _cupsStrAlloc(instance
);
1886 dest
->is_default
= 1;
1889 * Then add local options...
1892 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1893 cups_get_dests(filename
, dest_name
, instance
, 0, 1, 1, &dest
);
1897 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1899 cups_get_dests(filename
, dest_name
, instance
, 0, 1, 1, &dest
);
1903 * Return the result...
1911 * 'cupsRemoveDest()' - Remove a destination from the destination list.
1913 * Removing a destination/instance does not delete the class or printer
1914 * queue, merely the lpoptions for that destination/instance. Use the
1915 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
1916 * options for the user.
1918 * @since CUPS 1.3/macOS 10.5@
1921 int /* O - New number of destinations */
1922 cupsRemoveDest(const char *name
, /* I - Destination name */
1923 const char *instance
, /* I - Instance name or @code NULL@ */
1924 int num_dests
, /* I - Number of destinations */
1925 cups_dest_t
**dests
) /* IO - Destinations */
1927 int i
; /* Index into destinations */
1928 cups_dest_t
*dest
; /* Pointer to destination */
1932 * Find the destination...
1935 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1942 _cupsStrFree(dest
->name
);
1943 _cupsStrFree(dest
->instance
);
1944 cupsFreeOptions(dest
->num_options
, dest
->options
);
1947 * Remove the destination from the array...
1952 i
= (int)(dest
- *dests
);
1955 memmove(dest
, dest
+ 1, (size_t)(num_dests
- i
) * sizeof(cups_dest_t
));
1962 * 'cupsSetDefaultDest()' - Set the default destination.
1964 * @since CUPS 1.3/macOS 10.5@
1969 const char *name
, /* I - Destination name */
1970 const char *instance
, /* I - Instance name or @code NULL@ */
1971 int num_dests
, /* I - Number of destinations */
1972 cups_dest_t
*dests
) /* I - Destinations */
1974 int i
; /* Looping var */
1975 cups_dest_t
*dest
; /* Current destination */
1979 * Range check input...
1982 if (!name
|| num_dests
<= 0 || !dests
)
1986 * Loop through the array and set the "is_default" flag for the matching
1990 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1991 dest
->is_default
= !_cups_strcasecmp(name
, dest
->name
) &&
1992 ((!instance
&& !dest
->instance
) ||
1993 (instance
&& dest
->instance
&&
1994 !_cups_strcasecmp(instance
, dest
->instance
)));
1999 * 'cupsSetDests()' - Save the list of destinations for the default server.
2001 * This function saves the destinations to /etc/cups/lpoptions when run
2002 * as root and ~/.cups/lpoptions when run as a normal user.
2008 cupsSetDests(int num_dests
, /* I - Number of destinations */
2009 cups_dest_t
*dests
) /* I - Destinations */
2011 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
2016 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
2018 * This function saves the destinations to /etc/cups/lpoptions when run
2019 * as root and ~/.cups/lpoptions when run as a normal user.
2021 * @since CUPS 1.1.21/macOS 10.4@
2024 int /* O - 0 on success, -1 on error */
2025 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2026 int num_dests
, /* I - Number of destinations */
2027 cups_dest_t
*dests
) /* I - Destinations */
2029 int i
, j
; /* Looping vars */
2030 int wrote
; /* Wrote definition? */
2031 cups_dest_t
*dest
; /* Current destination */
2032 cups_option_t
*option
; /* Current option */
2033 _ipp_option_t
*match
; /* Matching attribute for option */
2034 FILE *fp
; /* File pointer */
2036 const char *home
; /* HOME environment variable */
2038 char filename
[1024]; /* lpoptions file */
2039 int num_temps
; /* Number of temporary destinations */
2040 cups_dest_t
*temps
= NULL
, /* Temporary destinations */
2041 *temp
; /* Current temporary dest */
2042 const char *val
; /* Value of temporary option */
2043 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2047 * Range check the input...
2050 if (!num_dests
|| !dests
)
2054 * Get the server destinations...
2057 num_temps
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &temps
, 0, 0);
2059 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2061 cupsFreeDests(num_temps
, temps
);
2066 * Figure out which file to write to...
2069 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2075 * Point to user defaults...
2078 if ((home
= getenv("HOME")) != NULL
)
2081 * Create ~/.cups subdirectory...
2084 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
2085 if (access(filename
, 0))
2086 mkdir(filename
, 0700);
2088 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2091 #endif /* !_WIN32 */
2094 * Try to open the file...
2097 if ((fp
= fopen(filename
, "w")) == NULL
)
2099 cupsFreeDests(num_temps
, temps
);
2105 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2110 fchmod(fileno(fp
), 0644);
2111 #endif /* !_WIN32 */
2114 * Write each printer; each line looks like:
2116 * Dest name[/instance] options
2117 * Default name[/instance] options
2120 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2121 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
2123 if (dest
->is_default
)
2125 fprintf(fp
, "Default %s", dest
->name
);
2127 fprintf(fp
, "/%s", dest
->instance
);
2134 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
2136 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
2139 * See if this option is a printer attribute; if so, skip it...
2142 if ((match
= _ippFindOption(option
->name
)) != NULL
&& match
->group_tag
== IPP_TAG_PRINTER
)
2146 * See if the server options match these; if so, don't write 'em.
2149 if (temp
&& (val
= cupsGetOption(option
->name
, temp
->num_options
, temp
->options
)) != NULL
&& !_cups_strcasecmp(val
, option
->value
))
2153 * Options don't match, write to the file...
2158 fprintf(fp
, "Dest %s", dest
->name
);
2160 fprintf(fp
, "/%s", dest
->instance
);
2164 if (option
->value
[0])
2166 if (strchr(option
->value
, ' ') || strchr(option
->value
, '\\') || strchr(option
->value
, '\"') || strchr(option
->value
, '\''))
2169 * Quote the value...
2172 fprintf(fp
, " %s=\"", option
->name
);
2174 for (val
= option
->value
; *val
; val
++)
2176 if (strchr("\"\'\\", *val
))
2187 * Store the literal value...
2190 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
2194 fprintf(fp
, " %s", option
->name
);
2202 * Free the temporary destinations and close the file...
2205 cupsFreeDests(num_temps
, temps
);
2211 * Set the default printer for this location - this allows command-line
2212 * and GUI applications to share the same default destination...
2215 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
2217 CFStringRef name
= CFStringCreateWithCString(kCFAllocatorDefault
, dest
->name
, kCFStringEncodingUTF8
);
2218 /* Default printer name */
2222 _cupsAppleSetDefaultPrinter(name
);
2226 #endif /* __APPLE__ */
2228 #ifdef HAVE_NOTIFY_POST
2230 * Send a notification so that macOS applications can know about the
2234 notify_post("com.apple.printerListChange");
2235 #endif /* HAVE_NOTIFY_POST */
2242 * '_cupsUserDefault()' - Get the user default printer from environment
2243 * variables and location information.
2246 char * /* O - Default printer or NULL */
2247 _cupsUserDefault(char *name
, /* I - Name buffer */
2248 size_t namesize
) /* I - Size of name buffer */
2250 const char *env
; /* LPDEST or PRINTER env variable */
2252 CFStringRef locprinter
; /* Last printer as this location */
2253 #endif /* __APPLE__ */
2256 if ((env
= getenv("LPDEST")) == NULL
)
2257 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
2262 strlcpy(name
, env
, namesize
);
2268 * Use location-based defaults if "use last printer" is selected in the
2269 * system preferences...
2272 if ((locprinter
= _cupsAppleCopyDefaultPrinter()) != NULL
)
2274 CFStringGetCString(locprinter
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
);
2275 CFRelease(locprinter
);
2280 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name
));
2282 return (*name
? name
: NULL
);
2286 * No location-based defaults on this platform...
2291 #endif /* __APPLE__ */
2295 #if _CUPS_LOCATION_DEFAULTS
2297 * 'appleCopyLocations()' - Copy the location history array.
2300 static CFArrayRef
/* O - Location array or NULL */
2301 appleCopyLocations(void)
2303 CFArrayRef locations
; /* Location array */
2307 * Look up the location array in the preferences...
2310 if ((locations
= CFPreferencesCopyAppValue(kLastUsedPrintersKey
,
2311 kCUPSPrintingPrefs
)) == NULL
)
2314 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
2316 CFRelease(locations
);
2325 * 'appleCopyNetwork()' - Get the network ID for the current location.
2328 static CFStringRef
/* O - Network ID */
2329 appleCopyNetwork(void)
2331 SCDynamicStoreRef dynamicStore
; /* System configuration data */
2332 CFStringRef key
; /* Current network configuration key */
2333 CFDictionaryRef ip_dict
; /* Network configuration data */
2334 CFStringRef network
= NULL
; /* Current network ID */
2337 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("libcups"), NULL
,
2341 * First use the IPv6 router address, if available, since that will generally
2342 * be a globally-unique link-local address.
2345 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2346 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv6
)) != NULL
)
2348 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2350 if ((network
= CFDictionaryGetValue(ip_dict
,
2351 kSCPropNetIPv6Router
)) != NULL
)
2361 * If that doesn't work, try the IPv4 router address. This isn't as unique
2362 * and will likely be a 10.x.y.z or 192.168.y.z address...
2367 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2368 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
2370 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2372 if ((network
= CFDictionaryGetValue(ip_dict
,
2373 kSCPropNetIPv4Router
)) != NULL
)
2383 CFRelease(dynamicStore
);
2388 #endif /* _CUPS_LOCATION_DEFAULTS */
2393 * 'appleGetPaperSize()' - Get the default paper size.
2396 static char * /* O - Default paper size */
2397 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
2398 size_t namesize
) /* I - Size of buffer */
2400 CFStringRef defaultPaperID
; /* Default paper ID */
2401 pwg_media_t
*pwgmedia
; /* PWG media size */
2404 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
2405 if (!defaultPaperID
||
2406 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
2407 !CFStringGetCString(defaultPaperID
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
))
2409 else if ((pwgmedia
= pwgMediaForLegacy(name
)) != NULL
)
2410 strlcpy(name
, pwgmedia
->pwg
, namesize
);
2413 CFRelease(defaultPaperID
);
2417 #endif /* __APPLE__ */
2420 #if _CUPS_LOCATION_DEFAULTS
2422 * 'appleGetPrinter()' - Get a printer from the history array.
2425 static CFStringRef
/* O - Printer name or NULL */
2426 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
2427 CFStringRef network
, /* I - Network name */
2428 CFIndex
*locindex
) /* O - Index in array */
2430 CFIndex i
, /* Looping var */
2431 count
; /* Number of locations */
2432 CFDictionaryRef location
; /* Current location */
2433 CFStringRef locnetwork
, /* Current network */
2434 locprinter
; /* Current printer */
2437 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
2438 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
2439 CFGetTypeID(location
) == CFDictionaryGetTypeID())
2441 if ((locnetwork
= CFDictionaryGetValue(location
,
2442 kLocationNetworkKey
)) != NULL
&&
2443 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
2444 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
2445 (locprinter
= CFDictionaryGetValue(location
,
2446 kLocationPrinterIDKey
)) != NULL
&&
2447 CFGetTypeID(locprinter
) == CFStringGetTypeID())
2452 return (locprinter
);
2458 #endif /* _CUPS_LOCATION_DEFAULTS */
2462 * 'cups_add_dest()' - Add a destination to the array.
2464 * Unlike cupsAddDest(), this function does not check for duplicates.
2467 static cups_dest_t
* /* O - New destination */
2468 cups_add_dest(const char *name
, /* I - Name of destination */
2469 const char *instance
, /* I - Instance or NULL */
2470 int *num_dests
, /* IO - Number of destinations */
2471 cups_dest_t
**dests
) /* IO - Destinations */
2473 int insert
, /* Insertion point */
2474 diff
; /* Result of comparison */
2475 cups_dest_t
*dest
; /* Destination pointer */
2479 * Add new destination...
2482 if (*num_dests
== 0)
2483 dest
= malloc(sizeof(cups_dest_t
));
2485 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (size_t)(*num_dests
+ 1));
2493 * Find where to insert the destination...
2496 if (*num_dests
== 0)
2500 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
2508 * Move the array elements as needed...
2511 if (insert
< *num_dests
)
2512 memmove(*dests
+ insert
+ 1, *dests
+ insert
, (size_t)(*num_dests
- insert
) * sizeof(cups_dest_t
));
2517 * Initialize the destination...
2520 dest
= *dests
+ insert
;
2521 dest
->name
= _cupsStrAlloc(name
);
2522 dest
->instance
= _cupsStrAlloc(instance
);
2523 dest
->is_default
= 0;
2524 dest
->num_options
= 0;
2525 dest
->options
= (cups_option_t
*)0;
2533 * 'cups_block_cb()' - Enumeration callback for block API.
2536 static int /* O - 1 to continue, 0 to stop */
2538 cups_dest_block_t block
, /* I - Block */
2539 unsigned flags
, /* I - Destination flags */
2540 cups_dest_t
*dest
) /* I - Destination */
2542 return ((block
)(flags
, dest
));
2544 # endif /* __BLOCKS__ */
2548 * 'cups_compare_dests()' - Compare two destinations.
2551 static int /* O - Result of comparison */
2552 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
2553 cups_dest_t
*b
) /* I - Second destination */
2555 int diff
; /* Difference */
2558 if ((diff
= _cups_strcasecmp(a
->name
, b
->name
)) != 0)
2560 else if (a
->instance
&& b
->instance
)
2561 return (_cups_strcasecmp(a
->instance
, b
->instance
));
2563 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
2567 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2570 * 'cups_dnssd_browse_cb()' - Browse for printers.
2574 cups_dnssd_browse_cb(
2575 DNSServiceRef sdRef
, /* I - Service reference */
2576 DNSServiceFlags flags
, /* I - Option flags */
2577 uint32_t interfaceIndex
, /* I - Interface number */
2578 DNSServiceErrorType errorCode
, /* I - Error, if any */
2579 const char *serviceName
, /* I - Name of service/device */
2580 const char *regtype
, /* I - Type of service */
2581 const char *replyDomain
, /* I - Service domain */
2582 void *context
) /* I - Enumeration data */
2584 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2585 /* Enumeration data */
2588 DEBUG_printf(("5cups_dnssd_browse_cb(sdRef=%p, flags=%x, interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\", context=%p)", (void *)sdRef
, flags
, interfaceIndex
, errorCode
, serviceName
, regtype
, replyDomain
, context
));
2591 * Don't do anything on error...
2594 if (errorCode
!= kDNSServiceErr_NoError
)
2601 cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
2605 # else /* HAVE_AVAHI */
2607 * 'cups_dnssd_browse_cb()' - Browse for printers.
2611 cups_dnssd_browse_cb(
2612 AvahiServiceBrowser
*browser
, /* I - Browser */
2613 AvahiIfIndex interface
, /* I - Interface index (unused) */
2614 AvahiProtocol protocol
, /* I - Network protocol (unused) */
2615 AvahiBrowserEvent event
, /* I - What happened */
2616 const char *name
, /* I - Service name */
2617 const char *type
, /* I - Registration type */
2618 const char *domain
, /* I - Domain */
2619 AvahiLookupResultFlags flags
, /* I - Flags */
2620 void *context
) /* I - Devices array */
2623 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
2624 /* Client information */
2626 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2627 /* Enumeration data */
2634 DEBUG_printf(("cups_dnssd_browse_cb(..., name=\"%s\", type=\"%s\", domain=\"%s\", ...);", name
, type
, domain
));
2638 case AVAHI_BROWSER_FAILURE
:
2639 DEBUG_printf(("cups_dnssd_browse_cb: %s", avahi_strerror(avahi_client_errno(client
))));
2640 avahi_simple_poll_quit(data
->simple_poll
);
2643 case AVAHI_BROWSER_NEW
:
2645 * This object is new on the network.
2648 cups_dnssd_get_device(data
, name
, type
, domain
);
2651 case AVAHI_BROWSER_REMOVE
:
2652 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
2655 case AVAHI_BROWSER_ALL_FOR_NOW
:
2656 DEBUG_puts("cups_dnssd_browse_cb: ALL_FOR_NOW");
2664 * 'cups_dnssd_client_cb()' - Avahi client callback function.
2668 cups_dnssd_client_cb(
2669 AvahiClient
*client
, /* I - Client information (unused) */
2670 AvahiClientState state
, /* I - Current state */
2671 void *context
) /* I - User data (unused) */
2673 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2674 /* Enumeration data */
2679 DEBUG_printf(("cups_dnssd_client_cb(client=%p, state=%d, context=%p)", client
, state
, context
));
2682 * If the connection drops, quit.
2685 if (state
== AVAHI_CLIENT_FAILURE
)
2687 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
2688 avahi_simple_poll_quit(data
->simple_poll
);
2691 # endif /* HAVE_DNSSD */
2695 * 'cups_dnssd_compare_device()' - Compare two devices.
2698 static int /* O - Result of comparison */
2699 cups_dnssd_compare_devices(
2700 _cups_dnssd_device_t
*a
, /* I - First device */
2701 _cups_dnssd_device_t
*b
) /* I - Second device */
2703 return (strcmp(a
->dest
.name
, b
->dest
.name
));
2708 * 'cups_dnssd_free_device()' - Free the memory used by a device.
2712 cups_dnssd_free_device(
2713 _cups_dnssd_device_t
*device
, /* I - Device */
2714 _cups_dnssd_data_t
*data
) /* I - Enumeration data */
2716 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", (void *)device
, device
->dest
.name
, (void *)data
));
2720 DNSServiceRefDeallocate(device
->ref
);
2721 # else /* HAVE_AVAHI */
2723 avahi_record_browser_free(device
->ref
);
2724 # endif /* HAVE_DNSSD */
2726 _cupsStrFree(device
->domain
);
2727 _cupsStrFree(device
->fullName
);
2728 _cupsStrFree(device
->regtype
);
2729 _cupsStrFree(device
->dest
.name
);
2731 cupsFreeOptions(device
->dest
.num_options
, device
->dest
.options
);
2738 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
2741 static _cups_dnssd_device_t
* /* O - Device */
2742 cups_dnssd_get_device(
2743 _cups_dnssd_data_t
*data
, /* I - Enumeration data */
2744 const char *serviceName
, /* I - Service name */
2745 const char *regtype
, /* I - Registration type */
2746 const char *replyDomain
) /* I - Domain name */
2748 _cups_dnssd_device_t key
, /* Search key */
2749 *device
; /* Device */
2750 char fullName
[kDNSServiceMaxDomainName
],
2751 /* Full name for query */
2752 name
[128]; /* Queue name */
2755 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\")", (void *)data
, serviceName
, regtype
, replyDomain
));
2758 * See if this is an existing device...
2761 cups_queue_name(name
, serviceName
, sizeof(name
));
2763 key
.dest
.name
= name
;
2765 if ((device
= cupsArrayFind(data
->devices
, &key
)) != NULL
)
2768 * Yes, see if we need to do anything with this...
2771 int update
= 0; /* Non-zero if we need to update */
2773 if (!_cups_strcasecmp(replyDomain
, "local.") &&
2774 _cups_strcasecmp(device
->domain
, replyDomain
))
2777 * Update the "global" listing to use the .local domain name instead.
2780 _cupsStrFree(device
->domain
);
2781 device
->domain
= _cupsStrAlloc(replyDomain
);
2783 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
2784 "domain.", device
->dest
.name
));
2789 if (!_cups_strcasecmp(regtype
, "_ipps._tcp") &&
2790 _cups_strcasecmp(device
->regtype
, regtype
))
2793 * Prefer IPPS over IPP.
2796 _cupsStrFree(device
->regtype
);
2797 device
->regtype
= _cupsStrAlloc(regtype
);
2799 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
2800 device
->dest
.name
));
2807 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
2808 device
->dest
.name
));
2815 * No, add the device...
2818 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
2819 "'%s'.", serviceName
,
2820 !strcmp(regtype
, "_ipps._tcp") ? "IPPS" : "IPP",
2823 device
= calloc(sizeof(_cups_dnssd_device_t
), 1);
2824 device
->dest
.name
= _cupsStrAlloc(name
);
2825 device
->domain
= _cupsStrAlloc(replyDomain
);
2826 device
->regtype
= _cupsStrAlloc(regtype
);
2828 device
->dest
.num_options
= cupsAddOption("printer-info", serviceName
, 0, &device
->dest
.options
);
2830 cupsArrayAdd(data
->devices
, device
);
2834 * Set the "full name" of this service, which is used for queries...
2838 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
2839 # else /* HAVE_AVAHI */
2840 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
, regtype
, replyDomain
);
2841 # endif /* HAVE_DNSSD */
2843 _cupsStrFree(device
->fullName
);
2844 device
->fullName
= _cupsStrAlloc(fullName
);
2849 DNSServiceRefDeallocate(device
->ref
);
2850 # else /* HAVE_AVAHI */
2851 avahi_record_browser_free(device
->ref
);
2852 # endif /* HAVE_DNSSD */
2857 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
2859 DEBUG_printf(("6cups_dnssd_get_device: Remove callback for \"%s\".", device
->dest
.name
));
2861 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
2862 device
->state
= _CUPS_DNSSD_NEW
;
2871 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
2873 * Note: This function is needed because avahi_simple_poll_iterate is broken
2874 * and always uses a timeout of 0 (!) milliseconds.
2875 * (https://github.com/lathiat/avahi/issues/127)
2880 static int /* O - Number of file descriptors matching */
2882 struct pollfd
*pollfds
, /* I - File descriptors */
2883 unsigned int num_pollfds
, /* I - Number of file descriptors */
2884 int timeout
, /* I - Timeout in milliseconds (unused) */
2885 void *context
) /* I - User data (unused) */
2887 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2888 /* Enumeration data */
2889 int val
; /* Return value */
2892 DEBUG_printf(("cups_dnssd_poll_cb(pollfds=%p, num_pollfds=%d, timeout=%d, context=%p)", pollfds
, num_pollfds
, timeout
, context
));
2896 val
= poll(pollfds
, num_pollfds
, _CUPS_DNSSD_MAXTIME
);
2898 DEBUG_printf(("cups_dnssd_poll_cb: poll() returned %d", val
));
2902 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno
)));
2911 # endif /* HAVE_AVAHI */
2915 * 'cups_dnssd_query_cb()' - Process query data.
2919 cups_dnssd_query_cb(
2921 DNSServiceRef sdRef
, /* I - Service reference */
2922 DNSServiceFlags flags
, /* I - Data flags */
2923 uint32_t interfaceIndex
, /* I - Interface */
2924 DNSServiceErrorType errorCode
, /* I - Error, if any */
2925 const char *fullName
, /* I - Full service name */
2926 uint16_t rrtype
, /* I - Record type */
2927 uint16_t rrclass
, /* I - Record class */
2928 uint16_t rdlen
, /* I - Length of record data */
2929 const void *rdata
, /* I - Record data */
2930 uint32_t ttl
, /* I - Time-to-live */
2931 # else /* HAVE_AVAHI */
2932 AvahiRecordBrowser
*browser
, /* I - Record browser */
2933 AvahiIfIndex interfaceIndex
,
2934 /* I - Interface index (unused) */
2935 AvahiProtocol protocol
, /* I - Network protocol (unused) */
2936 AvahiBrowserEvent event
, /* I - What happened? */
2937 const char *fullName
, /* I - Service name */
2938 uint16_t rrclass
, /* I - Record class */
2939 uint16_t rrtype
, /* I - Record type */
2940 const void *rdata
, /* I - TXT record */
2941 size_t rdlen
, /* I - Length of TXT record */
2942 AvahiLookupResultFlags flags
, /* I - Flags */
2943 # endif /* HAVE_DNSSD */
2944 void *context
) /* I - Enumeration data */
2946 # if defined(DEBUG) && defined(HAVE_AVAHI)
2947 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
2948 /* Client information */
2949 # endif /* DEBUG && HAVE_AVAHI */
2950 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2951 /* Enumeration data */
2952 char serviceName
[256],/* Service name */
2953 name
[128], /* Queue name */
2954 *ptr
; /* Pointer into string */
2955 _cups_dnssd_device_t dkey
, /* Search key */
2956 *device
; /* Device */
2960 DEBUG_printf(("5cups_dnssd_query_cb(sdRef=%p, flags=%x, interfaceIndex=%d, errorCode=%d, fullName=\"%s\", rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, context=%p)", (void *)sdRef
, flags
, interfaceIndex
, errorCode
, fullName
, rrtype
, rrclass
, rdlen
, rdata
, ttl
, context
));
2963 * Only process "add" data...
2966 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
2969 # else /* HAVE_AVAHI */
2970 DEBUG_printf(("cups_dnssd_query_cb(browser=%p, interfaceIndex=%d, protocol=%d, event=%d, fullName=\"%s\", rrclass=%u, rrtype=%u, rdata=%p, rdlen=%u, flags=%x, context=%p)", browser
, interfaceIndex
, protocol
, event
, fullName
, rrclass
, rrtype
, rdata
, (unsigned)rdlen
, flags
, context
));
2973 * Only process "add" data...
2976 if (event
!= AVAHI_BROWSER_NEW
)
2978 if (event
== AVAHI_BROWSER_FAILURE
)
2979 DEBUG_printf(("cups_dnssd_query_cb: %s", avahi_strerror(avahi_client_errno(client
))));
2983 # endif /* HAVE_DNSSD */
2986 * Lookup the service in the devices array.
2989 cups_dnssd_unquote(serviceName
, fullName
, sizeof(serviceName
));
2991 if ((ptr
= strstr(serviceName
, "._")) != NULL
)
2994 cups_queue_name(name
, serviceName
, sizeof(name
));
2996 dkey
.dest
.name
= name
;
2998 if ((device
= cupsArrayFind(data
->devices
, &dkey
)) != NULL
&& device
->state
== _CUPS_DNSSD_NEW
)
3001 * Found it, pull out the make and model from the TXT record and save it...
3004 const uint8_t *txt
, /* Pointer into data */
3005 *txtnext
, /* Next key/value pair */
3006 *txtend
; /* End of entire TXT record */
3007 uint8_t txtlen
; /* Length of current key/value pair */
3008 char key
[256], /* Key string */
3009 value
[256], /* Value string */
3010 make_and_model
[512],
3011 /* Manufacturer and model */
3012 model
[256], /* Model */
3013 uriname
[1024], /* Name for URI */
3014 uri
[1024]; /* Printer URI */
3015 cups_ptype_t type
= CUPS_PRINTER_DISCOVERED
| CUPS_PRINTER_BW
;
3017 int saw_printer_type
= 0;
3018 /* Did we see a printer-type key? */
3020 device
->state
= _CUPS_DNSSD_PENDING
;
3021 make_and_model
[0] = '\0';
3023 strlcpy(model
, "Unknown", sizeof(model
));
3025 for (txt
= rdata
, txtend
= txt
+ rdlen
;
3030 * Read a key/value pair starting with an 8-bit length. Since the
3031 * length is 8 bits and the size of the key/value buffers is 256, we
3032 * don't need to check for overflow...
3037 if (!txtlen
|| (txt
+ txtlen
) > txtend
)
3040 txtnext
= txt
+ txtlen
;
3042 for (ptr
= key
; txt
< txtnext
&& *txt
!= '='; txt
++)
3043 *ptr
++ = (char)*txt
;
3046 if (txt
< txtnext
&& *txt
== '=')
3051 memcpy(value
, txt
, (size_t)(txtnext
- txt
));
3052 value
[txtnext
- txt
] = '\0';
3054 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key
, value
));
3058 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key
));
3062 if (!_cups_strcasecmp(key
, "usb_MFG") ||
3063 !_cups_strcasecmp(key
, "usb_MANU") ||
3064 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
3065 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
3066 else if (!_cups_strcasecmp(key
, "usb_MDL") ||
3067 !_cups_strcasecmp(key
, "usb_MODEL"))
3068 strlcpy(model
, value
, sizeof(model
));
3069 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
3071 if (value
[0] == '(')
3074 * Strip parenthesis...
3077 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
3080 strlcpy(model
, value
+ 1, sizeof(model
));
3083 strlcpy(model
, value
, sizeof(model
));
3085 else if (!_cups_strcasecmp(key
, "ty"))
3087 strlcpy(model
, value
, sizeof(model
));
3089 if ((ptr
= strchr(model
, ',')) != NULL
)
3092 else if (!_cups_strcasecmp(key
, "note"))
3093 device
->dest
.num_options
= cupsAddOption("printer-location", value
,
3094 device
->dest
.num_options
,
3095 &device
->dest
.options
);
3096 else if (!_cups_strcasecmp(key
, "pdl"))
3099 * Look for PDF-capable printers; only PDF-capable printers are shown.
3102 const char *start
, *next
; /* Pointer into value */
3103 int have_pdf
= 0, /* Have PDF? */
3104 have_raster
= 0;/* Have raster format support? */
3106 for (start
= value
; start
&& *start
; start
= next
)
3108 if (!_cups_strncasecmp(start
, "application/pdf", 15) && (!start
[15] || start
[15] == ','))
3113 else if ((!_cups_strncasecmp(start
, "image/pwg-raster", 16) && (!start
[16] || start
[16] == ',')) ||
3114 (!_cups_strncasecmp(start
, "image/urf", 9) && (!start
[9] || start
[9] == ',')))
3120 if ((next
= strchr(start
, ',')) != NULL
)
3124 if (!have_pdf
&& !have_raster
)
3125 device
->state
= _CUPS_DNSSD_INCOMPATIBLE
;
3127 else if (!_cups_strcasecmp(key
, "printer-type"))
3130 * Value is either NNNN or 0xXXXX
3133 saw_printer_type
= 1;
3134 type
= (cups_ptype_t
)strtol(value
, NULL
, 0) | CUPS_PRINTER_DISCOVERED
;
3136 else if (!saw_printer_type
)
3138 if (!_cups_strcasecmp(key
, "air") &&
3139 !_cups_strcasecmp(value
, "t"))
3140 type
|= CUPS_PRINTER_AUTHENTICATED
;
3141 else if (!_cups_strcasecmp(key
, "bind") &&
3142 !_cups_strcasecmp(value
, "t"))
3143 type
|= CUPS_PRINTER_BIND
;
3144 else if (!_cups_strcasecmp(key
, "collate") &&
3145 !_cups_strcasecmp(value
, "t"))
3146 type
|= CUPS_PRINTER_COLLATE
;
3147 else if (!_cups_strcasecmp(key
, "color") &&
3148 !_cups_strcasecmp(value
, "t"))
3149 type
|= CUPS_PRINTER_COLOR
;
3150 else if (!_cups_strcasecmp(key
, "copies") &&
3151 !_cups_strcasecmp(value
, "t"))
3152 type
|= CUPS_PRINTER_COPIES
;
3153 else if (!_cups_strcasecmp(key
, "duplex") &&
3154 !_cups_strcasecmp(value
, "t"))
3155 type
|= CUPS_PRINTER_DUPLEX
;
3156 else if (!_cups_strcasecmp(key
, "fax") &&
3157 !_cups_strcasecmp(value
, "t"))
3158 type
|= CUPS_PRINTER_MFP
;
3159 else if (!_cups_strcasecmp(key
, "papercustom") &&
3160 !_cups_strcasecmp(value
, "t"))
3161 type
|= CUPS_PRINTER_VARIABLE
;
3162 else if (!_cups_strcasecmp(key
, "papermax"))
3164 if (!_cups_strcasecmp(value
, "legal-a4"))
3165 type
|= CUPS_PRINTER_SMALL
;
3166 else if (!_cups_strcasecmp(value
, "isoc-a2"))
3167 type
|= CUPS_PRINTER_MEDIUM
;
3168 else if (!_cups_strcasecmp(value
, ">isoc-a2"))
3169 type
|= CUPS_PRINTER_LARGE
;
3171 else if (!_cups_strcasecmp(key
, "punch") &&
3172 !_cups_strcasecmp(value
, "t"))
3173 type
|= CUPS_PRINTER_PUNCH
;
3174 else if (!_cups_strcasecmp(key
, "scan") &&
3175 !_cups_strcasecmp(value
, "t"))
3176 type
|= CUPS_PRINTER_MFP
;
3177 else if (!_cups_strcasecmp(key
, "sort") &&
3178 !_cups_strcasecmp(value
, "t"))
3179 type
|= CUPS_PRINTER_SORT
;
3180 else if (!_cups_strcasecmp(key
, "staple") &&
3181 !_cups_strcasecmp(value
, "t"))
3182 type
|= CUPS_PRINTER_STAPLE
;
3187 * Save the printer-xxx values...
3190 if (make_and_model
[0])
3192 strlcat(make_and_model
, " ", sizeof(make_and_model
));
3193 strlcat(make_and_model
, model
, sizeof(make_and_model
));
3195 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", make_and_model
, device
->dest
.num_options
, &device
->dest
.options
);
3198 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", model
, device
->dest
.num_options
, &device
->dest
.options
);
3200 device
->type
= type
;
3201 snprintf(value
, sizeof(value
), "%u", type
);
3202 device
->dest
.num_options
= cupsAddOption("printer-type", value
, device
->dest
.num_options
, &device
->dest
.options
);
3208 cups_dnssd_unquote(uriname
, device
->fullName
, sizeof(uriname
));
3209 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
3210 !strcmp(device
->regtype
, "_ipps._tcp") ? "ipps" : "ipp",
3211 NULL
, uriname
, 0, saw_printer_type
? "/cups" : "/");
3213 DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri
));
3215 device
->dest
.num_options
= cupsAddOption("device-uri", uri
, device
->dest
.num_options
, &device
->dest
.options
);
3218 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3224 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3227 static const char * /* O - Resolved URI or NULL */
3229 cups_dest_t
*dest
, /* I - Destination */
3230 const char *uri
, /* I - Current printer URI */
3231 int msec
, /* I - Time in milliseconds */
3232 int *cancel
, /* I - Pointer to "cancel" variable */
3233 cups_dest_cb_t cb
, /* I - Callback */
3234 void *user_data
) /* I - User data for callback */
3236 char tempuri
[1024]; /* Temporary URI buffer */
3237 _cups_dnssd_resolve_t resolve
; /* Resolve data */
3241 * Resolve the URI...
3244 resolve
.cancel
= cancel
;
3245 gettimeofday(&resolve
.end_time
, NULL
);
3248 resolve
.end_time
.tv_sec
+= msec
/ 1000;
3249 resolve
.end_time
.tv_usec
+= (msec
% 1000) * 1000;
3251 while (resolve
.end_time
.tv_usec
>= 1000000)
3253 resolve
.end_time
.tv_sec
++;
3254 resolve
.end_time
.tv_usec
-= 1000000;
3258 resolve
.end_time
.tv_sec
+= 75;
3261 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
3263 if ((uri
= _httpResolveURI(uri
, tempuri
, sizeof(tempuri
), _HTTP_RESOLVE_DEFAULT
, cups_dnssd_resolve_cb
, &resolve
)) == NULL
)
3265 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to resolve printer-uri."), 1);
3268 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
3274 * Save the resolved URI...
3277 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &dest
->options
);
3279 return (cupsGetOption("device-uri", dest
->num_options
, dest
->options
));
3284 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3287 static int /* O - 1 to continue, 0 to stop */
3288 cups_dnssd_resolve_cb(void *context
) /* I - Resolve data */
3290 _cups_dnssd_resolve_t
*resolve
= (_cups_dnssd_resolve_t
*)context
;
3292 struct timeval curtime
; /* Current time */
3296 * If the cancel variable is set, return immediately.
3299 if (resolve
->cancel
&& *(resolve
->cancel
))
3301 DEBUG_puts("4cups_dnssd_resolve_cb: Canceled.");
3306 * Otherwise check the end time...
3309 gettimeofday(&curtime
, NULL
);
3311 DEBUG_printf(("4cups_dnssd_resolve_cb: curtime=%d.%06d, end_time=%d.%06d", (int)curtime
.tv_sec
, (int)curtime
.tv_usec
, (int)resolve
->end_time
.tv_sec
, (int)resolve
->end_time
.tv_usec
));
3313 return (curtime
.tv_sec
< resolve
->end_time
.tv_sec
||
3314 (curtime
.tv_sec
== resolve
->end_time
.tv_sec
&&
3315 curtime
.tv_usec
< resolve
->end_time
.tv_usec
));
3320 * 'cups_dnssd_unquote()' - Unquote a name string.
3324 cups_dnssd_unquote(char *dst
, /* I - Destination buffer */
3325 const char *src
, /* I - Source string */
3326 size_t dstsize
) /* I - Size of destination buffer */
3328 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
3331 while (*src
&& dst
< dstend
)
3336 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
3337 isdigit(src
[2] & 255))
3339 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';
3351 #endif /* HAVE_DNSSD */
3354 #if defined(HAVE_AVAHI) || defined(HAVE_DNSSD)
3356 * 'cups_elapsed()' - Return the elapsed time in milliseconds.
3359 static int /* O - Elapsed time in milliseconds */
3360 cups_elapsed(struct timeval
*t
) /* IO - Previous time */
3362 int msecs
; /* Milliseconds */
3363 struct timeval nt
; /* New time */
3366 gettimeofday(&nt
, NULL
);
3368 msecs
= (int)(1000 * (nt
.tv_sec
- t
->tv_sec
) + (nt
.tv_usec
- t
->tv_usec
) / 1000);
3374 #endif /* HAVE_AVAHI || HAVE_DNSSD */
3378 * 'cups_enum_dests()' - Enumerate destinations from a specific server.
3381 static int /* O - 1 on success, 0 on failure */
3383 http_t
*http
, /* I - Connection to scheduler */
3384 unsigned flags
, /* I - Enumeration flags */
3385 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
3386 int *cancel
, /* I - Pointer to "cancel" variable */
3387 cups_ptype_t type
, /* I - Printer type bits */
3388 cups_ptype_t mask
, /* I - Mask for printer type bits */
3389 cups_dest_cb_t cb
, /* I - Callback function */
3390 void *user_data
) /* I - User data */
3392 int i
, j
, /* Looping vars */
3393 num_dests
; /* Number of destinations */
3394 cups_dest_t
*dests
= NULL
, /* Destinations */
3395 *dest
, /* Current destination */
3396 *user_dest
; /* User destination */
3397 cups_option_t
*option
; /* Current option */
3398 char *user_default
; /* User default printer */
3399 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3400 int count
, /* Number of queries started */
3401 completed
, /* Number of completed queries */
3402 remaining
; /* Remainder of timeout */
3403 struct timeval curtime
; /* Current time */
3404 _cups_dnssd_data_t data
; /* Data for callback */
3405 _cups_dnssd_device_t
*device
; /* Current device */
3407 int nfds
, /* Number of files responded */
3408 main_fd
; /* File descriptor for lookups */
3409 DNSServiceRef ipp_ref
= NULL
; /* IPP browser */
3411 DNSServiceRef ipps_ref
= NULL
; /* IPPS browser */
3412 # endif /* HAVE_SSL */
3414 struct pollfd pfd
; /* Polling data */
3416 fd_set input
; /* Input set for select() */
3417 struct timeval timeout
; /* Timeout for select() */
3418 # endif /* HAVE_POLL */
3419 # else /* HAVE_AVAHI */
3420 int error
; /* Error value */
3421 AvahiServiceBrowser
*ipp_ref
= NULL
; /* IPP browser */
3423 AvahiServiceBrowser
*ipps_ref
= NULL
; /* IPPS browser */
3424 # endif /* HAVE_SSL */
3425 # endif /* HAVE_DNSSD */
3427 _cups_getdata_t data
; /* Data for callback */
3428 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3429 const char *home
; /* HOME environment variable */
3430 char filename
[1024]; /* Local lpoptions file */
3431 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
3434 DEBUG_printf(("cups_enum_dests(flags=%x, msec=%d, cancel=%p, type=%x, mask=%x, cb=%p, user_data=%p)", flags
, msec
, (void *)cancel
, type
, mask
, (void *)cb
, (void *)user_data
));
3437 * Range check input...
3444 DEBUG_puts("1cups_enum_dests: No callback, returning 0.");
3449 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
3452 memset(&data
, 0, sizeof(data
));
3454 if ((user_default
= _cupsUserDefault(data
.def_name
, sizeof(data
.def_name
))) == NULL
)
3456 const char *defprinter
= cupsGetDefault2(http
);
3457 /* Server default, if any */
3460 strlcpy(data
.def_name
, defprinter
, sizeof(data
.def_name
));
3463 if (data
.def_name
[0])
3466 * Separate printer and instance name...
3469 if ((data
.def_instance
= strchr(data
.def_name
, '/')) != NULL
)
3470 *data
.def_instance
++ = '\0';
3473 DEBUG_printf(("1cups_enum_dests: def_name=\"%s\", def_instance=\"%s\"", data
.def_name
, data
.def_instance
));
3475 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
3476 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, 1, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
3478 if ((home
= getenv("HOME")) != NULL
)
3480 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
3482 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, 1, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
3486 * Get ready to enumerate...
3489 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3493 data
.user_data
= user_data
;
3494 data
.devices
= cupsArrayNew3((cups_array_func_t
)cups_dnssd_compare_devices
, NULL
, NULL
, 0, NULL
, (cups_afree_func_t
)cups_dnssd_free_device
);
3495 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3497 if (!(mask
& CUPS_PRINTER_DISCOVERED
) || !(type
& CUPS_PRINTER_DISCOVERED
))
3500 * Get the list of local printers and pass them to the callback function...
3503 num_dests
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &dests
, type
, mask
);
3505 if (data
.def_name
[0])
3508 * Lookup the named default printer and instance and make it the default...
3511 if ((dest
= cupsGetDest(data
.def_name
, data
.def_instance
, num_dests
, dests
)) != NULL
)
3513 DEBUG_printf(("1cups_enum_dests: Setting is_default on \"%s/%s\".", dest
->name
, dest
->instance
));
3514 dest
->is_default
= 1;
3518 for (i
= num_dests
, dest
= dests
;
3519 i
> 0 && (!cancel
|| !*cancel
);
3522 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3523 const char *device_uri
; /* Device URI */
3524 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3526 if ((user_dest
= cupsGetDest(dest
->name
, dest
->instance
, data
.num_dests
, data
.dests
)) != NULL
)
3529 * Apply user defaults to this destination...
3532 for (j
= user_dest
->num_options
, option
= user_dest
->options
; j
> 0; j
--, option
++)
3533 dest
->num_options
= cupsAddOption(option
->name
, option
->value
, dest
->num_options
, &dest
->options
);
3536 if (!(*cb
)(user_data
, i
> 1 ? CUPS_DEST_FLAGS_MORE
: CUPS_DEST_FLAGS_NONE
, dest
))
3539 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3540 if (!dest
->instance
&& (device_uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
&& !strncmp(device_uri
, "dnssd://", 8))
3543 * Add existing queue using service name, etc. so we don't list it again...
3546 char scheme
[32], /* URI scheme */
3547 userpass
[32], /* Username:password */
3548 serviceName
[256], /* Service name (host field) */
3549 resource
[256], /* Resource (options) */
3550 *regtype
, /* Registration type */
3551 *replyDomain
; /* Registration domain */
3552 int port
; /* Port number (not used) */
3554 if (httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), serviceName
, sizeof(serviceName
), &port
, resource
, sizeof(resource
)) >= HTTP_URI_STATUS_OK
)
3556 if ((regtype
= strstr(serviceName
, "._ipp")) != NULL
)
3560 if ((replyDomain
= strstr(regtype
, "._tcp.")) != NULL
)
3562 replyDomain
[5] = '\0';
3565 if ((device
= cups_dnssd_get_device(&data
, serviceName
, regtype
, replyDomain
)) != NULL
)
3566 device
->state
= _CUPS_DNSSD_ACTIVE
;
3571 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3574 cupsFreeDests(num_dests
, dests
);
3576 if (i
> 0 || msec
== 0)
3581 * Return early if the caller doesn't want to do discovery...
3584 if ((mask
& CUPS_PRINTER_DISCOVERED
) && !(type
& CUPS_PRINTER_DISCOVERED
))
3587 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3589 * Get Bonjour-shared printers...
3592 gettimeofday(&curtime
, NULL
);
3595 if (DNSServiceCreateConnection(&data
.main_ref
) != kDNSServiceErr_NoError
)
3597 DEBUG_puts("1cups_enum_dests: Unable to create service browser, returning 0.");
3599 cupsFreeDests(data
.num_dests
, data
.dests
);
3604 main_fd
= DNSServiceRefSockFD(data
.main_ref
);
3606 ipp_ref
= data
.main_ref
;
3607 if (DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0, "_ipp._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
) != kDNSServiceErr_NoError
)
3609 DEBUG_puts("1cups_enum_dests: Unable to create IPP browser, returning 0.");
3610 DNSServiceRefDeallocate(data
.main_ref
);
3612 cupsFreeDests(data
.num_dests
, data
.dests
);
3618 ipps_ref
= data
.main_ref
;
3619 if (DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0, "_ipps._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
) != kDNSServiceErr_NoError
)
3621 DEBUG_puts("1cups_enum_dests: Unable to create IPPS browser, returning 0.");
3622 DNSServiceRefDeallocate(data
.main_ref
);
3624 cupsFreeDests(data
.num_dests
, data
.dests
);
3628 # endif /* HAVE_SSL */
3630 # else /* HAVE_AVAHI */
3631 if ((data
.simple_poll
= avahi_simple_poll_new()) == NULL
)
3633 DEBUG_puts("1cups_enum_dests: Unable to create Avahi poll, returning 0.");
3635 cupsFreeDests(data
.num_dests
, data
.dests
);
3640 avahi_simple_poll_set_func(data
.simple_poll
, cups_dnssd_poll_cb
, &data
);
3642 data
.client
= avahi_client_new(avahi_simple_poll_get(data
.simple_poll
),
3643 0, cups_dnssd_client_cb
, &data
,
3647 DEBUG_puts("1cups_enum_dests: Unable to create Avahi client, returning 0.");
3648 avahi_simple_poll_free(data
.simple_poll
);
3650 cupsFreeDests(data
.num_dests
, data
.dests
);
3656 if ((ipp_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipp._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
)) == NULL
)
3658 DEBUG_puts("1cups_enum_dests: Unable to create Avahi IPP browser, returning 0.");
3660 avahi_client_free(data
.client
);
3661 avahi_simple_poll_free(data
.simple_poll
);
3663 cupsFreeDests(data
.num_dests
, data
.dests
);
3670 if ((ipps_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipps._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
)) == NULL
)
3672 DEBUG_puts("1cups_enum_dests: Unable to create Avahi IPPS browser, returning 0.");
3674 avahi_service_browser_free(ipp_ref
);
3675 avahi_client_free(data
.client
);
3676 avahi_simple_poll_free(data
.simple_poll
);
3678 cupsFreeDests(data
.num_dests
, data
.dests
);
3682 # endif /* HAVE_SSL */
3683 # endif /* HAVE_DNSSD */
3686 remaining
= INT_MAX
;
3690 while (remaining
> 0 && (!cancel
|| !*cancel
))
3693 * Check for input...
3696 DEBUG_printf(("1cups_enum_dests: remaining=%d", remaining
));
3698 cups_elapsed(&curtime
);
3703 pfd
.events
= POLLIN
;
3705 nfds
= poll(&pfd
, 1, remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
3709 FD_SET(main_fd
, &input
);
3712 timeout
.tv_usec
= 1000 * (remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
3714 nfds
= select(main_fd
+ 1, &input
, NULL
, NULL
, &timeout
);
3715 # endif /* HAVE_POLL */
3718 DNSServiceProcessResult(data
.main_ref
);
3719 else if (nfds
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
)
3722 # else /* HAVE_AVAHI */
3725 if ((error
= avahi_simple_poll_iterate(data
.simple_poll
, _CUPS_DNSSD_MAXTIME
)) > 0)
3728 * We've been told to exit the loop. Perhaps the connection to
3735 DEBUG_printf(("1cups_enum_dests: got_data=%d", data
.got_data
));
3736 # endif /* HAVE_DNSSD */
3738 remaining
-= cups_elapsed(&curtime
);
3740 for (device
= (_cups_dnssd_device_t
*)cupsArrayFirst(data
.devices
),
3741 count
= 0, completed
= 0;
3743 device
= (_cups_dnssd_device_t
*)cupsArrayNext(data
.devices
))
3748 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3751 if (!device
->ref
&& device
->state
== _CUPS_DNSSD_NEW
)
3753 DEBUG_printf(("1cups_enum_dests: Querying '%s'.", device
->fullName
));
3756 device
->ref
= data
.main_ref
;
3758 if (DNSServiceQueryRecord(&(device
->ref
), kDNSServiceFlagsShareConnection
, 0, device
->fullName
, kDNSServiceType_TXT
, kDNSServiceClass_IN
, (DNSServiceQueryRecordReply
)cups_dnssd_query_cb
, &data
) == kDNSServiceErr_NoError
)
3765 device
->state
= _CUPS_DNSSD_ERROR
;
3767 DEBUG_puts("1cups_enum_dests: Query failed.");
3770 # else /* HAVE_AVAHI */
3771 if ((device
->ref
= avahi_record_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, device
->fullName
, AVAHI_DNS_CLASS_IN
, AVAHI_DNS_TYPE_TXT
, 0, cups_dnssd_query_cb
, &data
)) != NULL
)
3773 DEBUG_printf(("1cups_enum_dests: Query ref=%p", device
->ref
));
3778 device
->state
= _CUPS_DNSSD_ERROR
;
3780 DEBUG_printf(("1cups_enum_dests: Query failed: %s", avahi_strerror(avahi_client_errno(data
.client
))));
3782 # endif /* HAVE_DNSSD */
3784 else if (device
->ref
&& device
->state
== _CUPS_DNSSD_PENDING
)
3788 DEBUG_printf(("1cups_enum_dests: Query for \"%s\" is complete.", device
->fullName
));
3790 if ((device
->type
& mask
) == type
)
3792 dest
= &device
->dest
;
3794 if ((user_dest
= cupsGetDest(dest
->name
, dest
->instance
, data
.num_dests
, data
.dests
)) != NULL
)
3797 * Apply user defaults to this destination...
3800 for (j
= user_dest
->num_options
, option
= user_dest
->options
; j
> 0; j
--, option
++)
3801 dest
->num_options
= cupsAddOption(option
->name
, option
->value
, dest
->num_options
, &dest
->options
);
3804 if (!strcasecmp(dest
->name
, data
.def_name
) && !data
.def_instance
)
3806 DEBUG_printf(("1cups_enum_dests: Setting is_default on discovered \"%s\".", dest
->name
));
3807 dest
->is_default
= 1;
3810 DEBUG_printf(("1cups_enum_dests: Add callback for \"%s\".", device
->dest
.name
));
3811 if (!(*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
))
3818 device
->state
= _CUPS_DNSSD_ACTIVE
;
3823 DEBUG_printf(("1cups_enum_dests: remaining=%d, browsers=%d, completed=%d, count=%d, devices count=%d", remaining
, data
.browsers
, completed
, count
, cupsArrayCount(data
.devices
)));
3825 if (data
.browsers
== 0 && completed
== cupsArrayCount(data
.devices
))
3828 DEBUG_printf(("1cups_enum_dests: remaining=%d, completed=%d, count=%d, devices count=%d", remaining
, completed
, count
, cupsArrayCount(data
.devices
)));
3830 if (completed
== cupsArrayCount(data
.devices
))
3832 # endif /* HAVE_AVAHI */
3834 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3842 cupsFreeDests(data
.num_dests
, data
.dests
);
3844 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3845 cupsArrayDelete(data
.devices
);
3849 DNSServiceRefDeallocate(ipp_ref
);
3853 DNSServiceRefDeallocate(ipps_ref
);
3854 # endif /* HAVE_SSL */
3857 DNSServiceRefDeallocate(data
.main_ref
);
3859 # else /* HAVE_AVAHI */
3861 avahi_service_browser_free(ipp_ref
);
3864 avahi_service_browser_free(ipps_ref
);
3865 # endif /* HAVE_SSL */
3868 avahi_client_free(data
.client
);
3869 if (data
.simple_poll
)
3870 avahi_simple_poll_free(data
.simple_poll
);
3871 # endif /* HAVE_DNSSD */
3872 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3874 DEBUG_puts("1cups_enum_dests: Returning 1.");
3881 * 'cups_find_dest()' - Find a destination using a binary search.
3884 static int /* O - Index of match */
3885 cups_find_dest(const char *name
, /* I - Destination name */
3886 const char *instance
, /* I - Instance or NULL */
3887 int num_dests
, /* I - Number of destinations */
3888 cups_dest_t
*dests
, /* I - Destinations */
3889 int prev
, /* I - Previous index */
3890 int *rdiff
) /* O - Difference of match */
3892 int left
, /* Low mark for binary search */
3893 right
, /* High mark for binary search */
3894 current
, /* Current index */
3895 diff
; /* Result of comparison */
3896 cups_dest_t key
; /* Search key */
3899 key
.name
= (char *)name
;
3900 key
.instance
= (char *)instance
;
3905 * Start search on either side of previous...
3908 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
3909 (diff
< 0 && prev
== 0) ||
3910 (diff
> 0 && prev
== (num_dests
- 1)))
3918 * Start with previous on right side...
3927 * Start wih previous on left side...
3931 right
= num_dests
- 1;
3937 * Start search in the middle...
3941 right
= num_dests
- 1;
3946 current
= (left
+ right
) / 2;
3947 diff
= cups_compare_dests(&key
, dests
+ current
);
3956 while ((right
- left
) > 1);
3961 * Check the last 1 or 2 elements...
3964 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
3968 diff
= cups_compare_dests(&key
, dests
+ right
);
3974 * Return the closest destination and the difference...
3984 * 'cups_get_cb()' - Collect enumerated destinations.
3987 static int /* O - 1 to continue, 0 to stop */
3988 cups_get_cb(_cups_getdata_t
*data
, /* I - Data from cupsGetDests */
3989 unsigned flags
, /* I - Enumeration flags */
3990 cups_dest_t
*dest
) /* I - Destination */
3992 if (flags
& CUPS_DEST_FLAGS_REMOVED
)
3995 * Remove destination from array...
3998 data
->num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
->num_dests
, &data
->dests
);
4003 * Add destination to array...
4006 data
->num_dests
= cupsCopyDest(dest
, data
->num_dests
, &data
->dests
);
4014 * 'cups_get_default()' - Get the default destination from an lpoptions file.
4017 static char * /* O - Default destination or NULL */
4018 cups_get_default(const char *filename
, /* I - File to read */
4019 char *namebuf
, /* I - Name buffer */
4020 size_t namesize
, /* I - Size of name buffer */
4021 const char **instance
) /* I - Instance */
4023 cups_file_t
*fp
; /* lpoptions file */
4024 char line
[8192], /* Line from file */
4025 *value
, /* Value for line */
4026 *nameptr
; /* Pointer into name */
4027 int linenum
; /* Current line */
4032 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
4036 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4038 if (!_cups_strcasecmp(line
, "default") && value
)
4040 strlcpy(namebuf
, value
, namesize
);
4042 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
4044 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
4047 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
4050 *instance
= nameptr
;
4058 return (*namebuf
? namebuf
: NULL
);
4063 * 'cups_get_dests()' - Get destinations from a file.
4066 static int /* O - Number of destinations */
4068 const char *filename
, /* I - File to read from */
4069 const char *match_name
, /* I - Destination name we want */
4070 const char *match_inst
, /* I - Instance name we want */
4071 int load_all
, /* I - Load all saved destinations? */
4072 int user_default_set
, /* I - User default printer set? */
4073 int num_dests
, /* I - Number of destinations */
4074 cups_dest_t
**dests
) /* IO - Destinations */
4076 int i
; /* Looping var */
4077 cups_dest_t
*dest
; /* Current destination */
4078 cups_file_t
*fp
; /* File pointer */
4079 char line
[8192], /* Line from file */
4080 *lineptr
, /* Pointer into line */
4081 *name
, /* Name of destination/option */
4082 *instance
; /* Instance of destination */
4083 int linenum
; /* Current line number */
4086 DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", match_inst=\"%s\", load_all=%d, user_default_set=%d, num_dests=%d, dests=%p)", filename
, match_name
, match_inst
, load_all
, user_default_set
, num_dests
, (void *)dests
));
4089 * Try to open the file...
4092 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4096 * Read each printer; each line looks like:
4098 * Dest name[/instance] options
4099 * Default name[/instance] options
4104 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
4107 * See what type of line it is...
4110 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
4111 linenum
, line
, lineptr
));
4113 if ((_cups_strcasecmp(line
, "dest") && _cups_strcasecmp(line
, "default")) || !lineptr
)
4115 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
4122 * Search for an instance...
4125 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
4128 if (*lineptr
== '/')
4131 * Found an instance...
4138 * Search for an instance...
4141 while (!isspace(*lineptr
& 255) && *lineptr
)
4150 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
4154 * Match and/or ignore missing destinations...
4159 if (_cups_strcasecmp(name
, match_name
) ||
4160 (!instance
&& match_inst
) ||
4161 (instance
&& !match_inst
) ||
4162 (instance
&& _cups_strcasecmp(instance
, match_inst
)))
4167 else if (!load_all
&& cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
4169 DEBUG_puts("9cups_get_dests: Not found!");
4175 * Add the destination...
4178 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
4180 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
4186 DEBUG_puts("9cups_get_dests: Out of memory!");
4192 * Add options until we hit the end of the line...
4195 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
, &(dest
->options
));
4198 * If we found what we were looking for, stop now...
4205 * Set this as default if needed...
4208 if (!user_default_set
&& !_cups_strcasecmp(line
, "default"))
4210 DEBUG_puts("9cups_get_dests: Setting as default...");
4212 for (i
= 0; i
< num_dests
; i
++)
4213 (*dests
)[i
].is_default
= 0;
4215 dest
->is_default
= 1;
4220 * Close the file and return...
4230 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
4234 static char * /* O - New string */
4236 ipp_attribute_t
*attr
, /* I - Attribute to convert */
4237 char *buffer
, /* I - Buffer */
4238 size_t bufsize
) /* I - Size of buffer */
4240 int i
; /* Looping var */
4241 char *ptr
, /* Pointer into buffer */
4242 *end
, /* Pointer to end of buffer */
4243 *valptr
; /* Pointer into string attribute */
4247 * Return quickly if we have a single string value...
4250 if (attr
->num_values
== 1 &&
4251 attr
->value_tag
!= IPP_TAG_INTEGER
&&
4252 attr
->value_tag
!= IPP_TAG_ENUM
&&
4253 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
4254 attr
->value_tag
!= IPP_TAG_RANGE
)
4255 return (attr
->values
[0].string
.text
);
4258 * Copy the values to the string, separating with commas and escaping strings
4262 end
= buffer
+ bufsize
- 1;
4264 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
4269 switch (attr
->value_tag
)
4271 case IPP_TAG_INTEGER
:
4273 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].integer
);
4276 case IPP_TAG_BOOLEAN
:
4277 if (attr
->values
[i
].boolean
)
4278 strlcpy(ptr
, "true", (size_t)(end
- ptr
+ 1));
4280 strlcpy(ptr
, "false", (size_t)(end
- ptr
+ 1));
4283 case IPP_TAG_RANGE
:
4284 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
4285 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].range
.lower
);
4287 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d-%d", attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4291 for (valptr
= attr
->values
[i
].string
.text
;
4292 *valptr
&& ptr
< end
;)
4294 if (strchr(" \t\n\\\'\"", *valptr
))
4296 if (ptr
>= (end
- 1))
4319 * 'cups_name_cb()' - Find an enumerated destination.
4322 static int /* O - 1 to continue, 0 to stop */
4323 cups_name_cb(_cups_namedata_t
*data
, /* I - Data from cupsGetNamedDest */
4324 unsigned flags
, /* I - Enumeration flags */
4325 cups_dest_t
*dest
) /* I - Destination */
4327 DEBUG_printf(("2cups_name_cb(data=%p(%s), flags=%x, dest=%p(%s)", (void *)data
, data
->name
, flags
, (void *)dest
, dest
->name
));
4329 if (!(flags
& CUPS_DEST_FLAGS_REMOVED
) && !dest
->instance
&& !strcasecmp(data
->name
, dest
->name
))
4332 * Copy destination and stop enumeration...
4335 cupsCopyDest(dest
, 0, &data
->dest
);
4344 * 'cups_queue_name()' - Create a local queue name based on the service name.
4349 char *name
, /* I - Name buffer */
4350 const char *serviceName
, /* I - Service name */
4351 size_t namesize
) /* I - Size of name buffer */
4353 const char *ptr
; /* Pointer into serviceName */
4354 char *nameptr
; /* Pointer into name */
4357 for (nameptr
= name
, ptr
= serviceName
; *ptr
&& nameptr
< (name
+ namesize
- 1); ptr
++)
4360 * Sanitize the printer name...
4363 if (_cups_isalnum(*ptr
))
4365 else if (nameptr
== name
|| nameptr
[-1] != '_')