2 * User-defined destination (and option) support for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
25 #endif /* HAVE_NOTIFY_H */
29 #endif /* HAVE_POLL */
33 #endif /* HAVE_DNSSD */
36 # include <avahi-client/client.h>
37 # include <avahi-client/lookup.h>
38 # include <avahi-common/simple-watch.h>
39 # include <avahi-common/domain.h>
40 # include <avahi-common/error.h>
41 # include <avahi-common/malloc.h>
42 #define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
43 #endif /* HAVE_AVAHI */
52 # include <SystemConfiguration/SystemConfiguration.h>
53 # define _CUPS_LOCATION_DEFAULTS 1
54 # endif /* !TARGET_OS_IOS */
55 # define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
56 # define kDefaultPaperIDKey CFSTR("DefaultPaperID")
57 # define kLastUsedPrintersKey CFSTR("LastUsedPrinters")
58 # define kLocationNetworkKey CFSTR("Network")
59 # define kLocationPrinterIDKey CFSTR("PrinterID")
60 # define kUseLastPrinter CFSTR("UseLastPrinter")
61 #endif /* __APPLE__ */
63 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
64 # define _CUPS_DNSSD_MAXTIME 500 /* Milliseconds for maximum quantum of time */
65 #endif /* HAVE_DNSSD || HAVE_AVAHI */
72 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
73 typedef enum _cups_dnssd_state_e
/* Enumerated device state */
80 _CUPS_DNSSD_INCOMPATIBLE
,
82 } _cups_dnssd_state_t
;
84 typedef struct _cups_dnssd_data_s
/* Enumeration data */
87 DNSServiceRef main_ref
; /* Main service reference */
88 # else /* HAVE_AVAHI */
89 AvahiSimplePoll
*simple_poll
; /* Polling interface */
90 AvahiClient
*client
; /* Client information */
91 int got_data
; /* Did we get data? */
92 int browsers
; /* How many browsers are running? */
93 # endif /* HAVE_DNSSD */
94 cups_dest_cb_t cb
; /* Callback */
95 void *user_data
; /* User data pointer */
96 cups_ptype_t type
, /* Printer type filter */
97 mask
; /* Printer type mask */
98 cups_array_t
*devices
; /* Devices found so far */
101 typedef struct _cups_dnssd_device_s
/* Enumerated device */
103 _cups_dnssd_state_t state
; /* State of device listing */
105 DNSServiceRef ref
; /* Service reference for query */
106 # else /* HAVE_AVAHI */
107 AvahiRecordBrowser
*ref
; /* Browser for query */
108 # endif /* HAVE_DNSSD */
109 char *fullName
, /* Full name */
110 *regtype
, /* Registration type */
111 *domain
; /* Domain name */
112 cups_ptype_t type
; /* Device registration type */
113 cups_dest_t dest
; /* Destination record */
114 } _cups_dnssd_device_t
;
116 typedef struct _cups_dnssd_resolve_s
/* Data for resolving URI */
118 int *cancel
; /* Pointer to "cancel" variable */
119 struct timeval end_time
; /* Ending time */
120 } _cups_dnssd_resolve_t
;
121 #endif /* HAVE_DNSSD */
123 typedef struct _cups_getdata_s
125 int num_dests
; /* Number of destinations */
126 cups_dest_t
*dests
; /* Destinations */
129 typedef struct _cups_namedata_s
131 const char *name
; /* Named destination */
132 cups_dest_t
*dest
; /* Destination */
140 #if _CUPS_LOCATION_DEFAULTS
141 static CFArrayRef
appleCopyLocations(void);
142 static CFStringRef
appleCopyNetwork(void);
143 #endif /* _CUPS_LOCATION_DEFAULTS */
145 static char *appleGetPaperSize(char *name
, size_t namesize
);
146 #endif /* __APPLE__ */
147 #if _CUPS_LOCATION_DEFAULTS
148 static CFStringRef
appleGetPrinter(CFArrayRef locations
,
149 CFStringRef network
, CFIndex
*locindex
);
150 #endif /* _CUPS_LOCATION_DEFAULTS */
151 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
152 int *num_dests
, cups_dest_t
**dests
);
154 static int cups_block_cb(cups_dest_block_t block
, unsigned flags
,
156 #endif /* __BLOCKS__ */
157 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
158 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
160 static void cups_dnssd_browse_cb(DNSServiceRef sdRef
,
161 DNSServiceFlags flags
,
162 uint32_t interfaceIndex
,
163 DNSServiceErrorType errorCode
,
164 const char *serviceName
,
166 const char *replyDomain
,
168 # else /* HAVE_AVAHI */
169 static void cups_dnssd_browse_cb(AvahiServiceBrowser
*browser
,
170 AvahiIfIndex interface
,
171 AvahiProtocol protocol
,
172 AvahiBrowserEvent event
,
173 const char *serviceName
,
175 const char *replyDomain
,
176 AvahiLookupResultFlags flags
,
178 static void cups_dnssd_client_cb(AvahiClient
*client
,
179 AvahiClientState state
,
181 # endif /* HAVE_DNSSD */
182 static int cups_dnssd_compare_devices(_cups_dnssd_device_t
*a
,
183 _cups_dnssd_device_t
*b
);
184 static void cups_dnssd_free_device(_cups_dnssd_device_t
*device
,
185 _cups_dnssd_data_t
*data
);
186 static _cups_dnssd_device_t
*
187 cups_dnssd_get_device(_cups_dnssd_data_t
*data
,
188 const char *serviceName
,
190 const char *replyDomain
);
192 static void cups_dnssd_local_cb(DNSServiceRef sdRef
,
193 DNSServiceFlags flags
,
194 uint32_t interfaceIndex
,
195 DNSServiceErrorType errorCode
,
196 const char *serviceName
,
198 const char *replyDomain
,
200 static void cups_dnssd_query_cb(DNSServiceRef sdRef
,
201 DNSServiceFlags flags
,
202 uint32_t interfaceIndex
,
203 DNSServiceErrorType errorCode
,
204 const char *fullName
,
205 uint16_t rrtype
, uint16_t rrclass
,
206 uint16_t rdlen
, const void *rdata
,
207 uint32_t ttl
, void *context
);
208 # else /* HAVE_AVAHI */
209 static int cups_dnssd_poll_cb(struct pollfd
*pollfds
,
210 unsigned int num_pollfds
,
211 int timeout
, void *context
);
212 static void cups_dnssd_query_cb(AvahiRecordBrowser
*browser
,
213 AvahiIfIndex interface
,
214 AvahiProtocol protocol
,
215 AvahiBrowserEvent event
,
216 const char *name
, uint16_t rrclass
,
217 uint16_t rrtype
, const void *rdata
,
219 AvahiLookupResultFlags flags
,
221 # endif /* HAVE_DNSSD */
222 static const char *cups_dnssd_resolve(cups_dest_t
*dest
, const char *uri
,
223 int msec
, int *cancel
,
224 cups_dest_cb_t cb
, void *user_data
);
225 static int cups_dnssd_resolve_cb(void *context
);
226 static void cups_dnssd_unquote(char *dst
, const char *src
,
228 static int cups_elapsed(struct timeval
*t
);
229 #endif /* HAVE_DNSSD || HAVE_AVAHI */
230 static int cups_find_dest(const char *name
, const char *instance
,
231 int num_dests
, cups_dest_t
*dests
, int prev
,
233 static int cups_get_cb(_cups_getdata_t
*data
, unsigned flags
, cups_dest_t
*dest
);
234 static char *cups_get_default(const char *filename
, char *namebuf
,
235 size_t namesize
, const char **instance
);
236 static int cups_get_dests(const char *filename
, const char *match_name
,
237 const char *match_inst
, int user_default_set
,
238 int num_dests
, cups_dest_t
**dests
);
239 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
241 static int cups_name_cb(_cups_namedata_t
*data
, unsigned flags
, cups_dest_t
*dest
);
242 static void cups_queue_name(char *name
, const char *serviceName
, size_t namesize
);
246 * 'cupsAddDest()' - Add a destination to the list of destinations.
248 * This function cannot be used to add a new class or printer queue,
249 * it only adds a new container of saved options for the named
250 * destination or instance.
252 * If the named destination already exists, the destination list is
253 * returned unchanged. Adding a new instance of a destination creates
254 * a copy of that destination's options.
256 * Use the @link cupsSaveDests@ function to save the updated list of
257 * destinations to the user's lpoptions file.
260 int /* O - New number of destinations */
261 cupsAddDest(const char *name
, /* I - Destination name */
262 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
263 int num_dests
, /* I - Number of destinations */
264 cups_dest_t
**dests
) /* IO - Destinations */
266 int i
; /* Looping var */
267 cups_dest_t
*dest
; /* Destination pointer */
268 cups_dest_t
*parent
= NULL
; /* Parent destination */
269 cups_option_t
*doption
, /* Current destination option */
270 *poption
; /* Current parent option */
276 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
278 if (instance
&& !cupsGetDest(name
, NULL
, num_dests
, *dests
))
281 if ((dest
= cups_add_dest(name
, instance
, &num_dests
, dests
)) == NULL
)
285 * Find the base dest again now the array has been realloc'd.
288 parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
);
290 if (instance
&& parent
&& parent
->num_options
> 0)
293 * Copy options from parent...
296 dest
->options
= calloc(sizeof(cups_option_t
), (size_t)parent
->num_options
);
300 dest
->num_options
= parent
->num_options
;
302 for (i
= dest
->num_options
, doption
= dest
->options
,
303 poption
= parent
->options
;
305 i
--, doption
++, poption
++)
307 doption
->name
= _cupsStrRetain(poption
->name
);
308 doption
->value
= _cupsStrRetain(poption
->value
);
320 * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
323 CFStringRef
/* O - Default paper ID */
324 _cupsAppleCopyDefaultPaperID(void)
326 return (CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
327 kCUPSPrintingPrefs
));
332 * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location.
335 CFStringRef
/* O - Default printer name */
336 _cupsAppleCopyDefaultPrinter(void)
338 # if _CUPS_LOCATION_DEFAULTS
339 CFStringRef network
; /* Network location */
340 CFArrayRef locations
; /* Location array */
341 CFStringRef locprinter
; /* Current printer */
345 * Use location-based defaults only if "use last printer" is selected in the
346 * system preferences...
349 if (!_cupsAppleGetUseLastPrinter())
351 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as "
357 * Get the current location...
360 if ((network
= appleCopyNetwork()) == NULL
)
362 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current "
368 * Lookup the network in the preferences...
371 if ((locations
= appleCopyLocations()) == NULL
)
374 * Missing or bad location array, so no location-based default...
377 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used "
385 DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.",
386 (int)CFArrayGetCount(locations
)));
388 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
389 CFRetain(locprinter
);
392 CFRelease(locations
);
398 # endif /* _CUPS_LOCATION_DEFAULTS */
403 * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
406 int /* O - 1 to use last printer, 0 otherwise */
407 _cupsAppleGetUseLastPrinter(void)
409 Boolean uselast
, /* Use last printer preference value */
410 uselast_set
; /* Valid is set? */
413 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
416 uselast
= CFPreferencesGetAppBooleanValue(kUseLastPrinter
,
427 * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
431 _cupsAppleSetDefaultPaperID(
432 CFStringRef name
) /* I - New paper ID */
434 CFPreferencesSetAppValue(kDefaultPaperIDKey
, name
, kCUPSPrintingPrefs
);
435 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
437 # ifdef HAVE_NOTIFY_POST
438 notify_post("com.apple.printerPrefsChange");
439 # endif /* HAVE_NOTIFY_POST */
444 * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location.
448 _cupsAppleSetDefaultPrinter(
449 CFStringRef name
) /* I - Default printer/class name */
451 # if _CUPS_LOCATION_DEFAULTS
452 CFStringRef network
; /* Current network */
453 CFArrayRef locations
; /* Old locations array */
454 CFIndex locindex
; /* Index in locations array */
455 CFStringRef locprinter
; /* Current printer */
456 CFMutableArrayRef newlocations
; /* New locations array */
457 CFMutableDictionaryRef newlocation
; /* New location */
461 * Get the current location...
464 if ((network
= appleCopyNetwork()) == NULL
)
466 DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network...");
471 * Lookup the network in the preferences...
474 if ((locations
= appleCopyLocations()) != NULL
)
475 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
482 if (!locprinter
|| CFStringCompare(locprinter
, name
, 0) != kCFCompareEqualTo
)
485 * Need to change the locations array...
490 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
494 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
497 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
498 &kCFTypeArrayCallBacks
);
500 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
501 &kCFTypeDictionaryKeyCallBacks
,
502 &kCFTypeDictionaryValueCallBacks
);
504 if (newlocation
&& newlocations
)
507 * Put the new location at the front of the array...
510 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
511 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, name
);
512 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
515 * Limit the number of locations to 10...
518 while (CFArrayGetCount(newlocations
) > 10)
519 CFArrayRemoveValueAtIndex(newlocations
, 10);
522 * Push the changes out...
525 CFPreferencesSetAppValue(kLastUsedPrintersKey
, newlocations
,
527 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
529 # ifdef HAVE_NOTIFY_POST
530 notify_post("com.apple.printerPrefsChange");
531 # endif /* HAVE_NOTIFY_POST */
535 CFRelease(newlocations
);
538 CFRelease(newlocation
);
542 CFRelease(locations
);
548 # endif /* _CUPS_LOCATION_DEFAULTS */
553 * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
557 _cupsAppleSetUseLastPrinter(
558 int uselast
) /* O - 1 to use last printer, 0 otherwise */
560 CFPreferencesSetAppValue(kUseLastPrinter
,
561 uselast
? kCFBooleanTrue
: kCFBooleanFalse
,
563 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
565 # ifdef HAVE_NOTIFY_POST
566 notify_post("com.apple.printerPrefsChange");
567 # endif /* HAVE_NOTIFY_POST */
569 #endif /* __APPLE__ */
573 * 'cupsConnectDest()' - Open a conection to the destination.
575 * Connect to the destination, returning a new @code http_t@ connection object
576 * and optionally the resource path to use for the destination. These calls
577 * will block until a connection is made, the timeout expires, the integer
578 * pointed to by "cancel" is non-zero, or the callback function (or block)
579 * returns 0. The caller is responsible for calling @link httpClose@ on the
580 * returned connection.
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 ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
635 if ((uri
= cupsGetOption("resolved-device-uri", dest
->num_options
, dest
->options
)) == NULL
)
637 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
639 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
640 if (strstr(uri
, "._tcp"))
641 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
642 #endif /* HAVE_DNSSD || HAVE_AVAHI */
647 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, tempresource
, sizeof(tempresource
));
651 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
653 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
659 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
662 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
667 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
668 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
669 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
671 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
674 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
681 * Lookup the address for the server...
685 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
687 snprintf(portstr
, sizeof(portstr
), "%d", port
);
689 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portstr
)) == NULL
)
692 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
697 if (cancel
&& *cancel
)
699 httpAddrFreeList(addrlist
);
702 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CANCELED
, dest
);
708 * Create the HTTP object pointing to the server referenced by the URI...
711 if (!strcmp(scheme
, "ipps") || port
== 443)
712 encryption
= HTTP_ENCRYPTION_ALWAYS
;
714 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
716 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, encryption
, 1, 0, NULL
);
717 httpAddrFreeList(addrlist
);
720 * Connect if requested...
723 if (flags
& CUPS_DEST_FLAGS_UNCONNECTED
)
726 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
, dest
);
731 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
733 if (!httpReconnect2(http
, msec
, cancel
) && cb
)
735 if (cancel
&& *cancel
)
736 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
738 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
741 (*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
);
750 * 'cupsConnectDestBlock()' - Open a connection to the destination.
752 * Connect to the destination, returning a new @code http_t@ connection object
753 * and optionally the resource path to use for the destination. These calls
754 * will block until a connection is made, the timeout expires, the integer
755 * pointed to by "cancel" is non-zero, or the block returns 0. The caller is
756 * responsible for calling @link httpClose@ on the returned connection.
758 * @since CUPS 1.6/macOS 10.8@ @exclude all@
761 http_t
* /* O - Connection to destination or @code NULL@ */
762 cupsConnectDestBlock(
763 cups_dest_t
*dest
, /* I - Destination */
764 unsigned flags
, /* I - Connection flags */
765 int msec
, /* I - Timeout in milliseconds */
766 int *cancel
, /* I - Pointer to "cancel" variable */
767 char *resource
, /* I - Resource buffer */
768 size_t resourcesize
, /* I - Size of resource buffer */
769 cups_dest_block_t block
) /* I - Callback block */
771 return (cupsConnectDest(dest
, flags
, msec
, cancel
, resource
, resourcesize
,
772 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
774 #endif /* __BLOCKS__ */
778 * 'cupsCopyDest()' - Copy a destination.
780 * Make a copy of the destination to an array of destinations (or just a single
781 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
782 * for calling cupsFreeDests() on the returned object(s).
784 * @since CUPS 1.6/macOS 10.8@
787 int /* O - New number of destinations */
788 cupsCopyDest(cups_dest_t
*dest
, /* I - Destination to copy */
789 int num_dests
, /* I - Number of destinations */
790 cups_dest_t
**dests
) /* IO - Destination array */
792 int i
; /* Looping var */
793 cups_dest_t
*new_dest
; /* New destination pointer */
794 cups_option_t
*new_option
, /* Current destination option */
795 *option
; /* Current parent option */
799 * Range check input...
802 if (!dest
|| num_dests
< 0 || !dests
)
806 * See if the destination already exists...
809 if ((new_dest
= cupsGetDest(dest
->name
, dest
->instance
, num_dests
,
813 * Protect against copying destination to itself...
816 if (new_dest
== dest
)
820 * Otherwise, free the options...
823 cupsFreeOptions(new_dest
->num_options
, new_dest
->options
);
825 new_dest
->num_options
= 0;
826 new_dest
->options
= NULL
;
829 new_dest
= cups_add_dest(dest
->name
, dest
->instance
, &num_dests
, dests
);
833 if ((new_dest
->options
= calloc(sizeof(cups_option_t
), (size_t)dest
->num_options
)) == NULL
)
834 return (cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
, dests
));
836 new_dest
->num_options
= dest
->num_options
;
838 for (i
= dest
->num_options
, option
= dest
->options
,
839 new_option
= new_dest
->options
;
841 i
--, option
++, new_option
++)
843 new_option
->name
= _cupsStrRetain(option
->name
);
844 new_option
->value
= _cupsStrRetain(option
->value
);
853 * '_cupsCreateDest()' - Create a local (temporary) queue.
856 char * /* O - Printer URI or @code NULL@ on error */
857 _cupsCreateDest(const char *name
, /* I - Printer name */
858 const char *info
, /* I - Printer description of @code NULL@ */
859 const char *device_id
, /* I - 1284 Device ID or @code NULL@ */
860 const char *device_uri
, /* I - Device URI */
861 char *uri
, /* I - Printer URI buffer */
862 size_t urisize
) /* I - Size of URI buffer */
864 http_t
*http
; /* Connection to server */
865 ipp_t
*request
, /* CUPS-Create-Local-Printer request */
866 *response
; /* CUPS-Create-Local-Printer response */
867 ipp_attribute_t
*attr
; /* printer-uri-supported attribute */
868 ipp_pstate_t state
= IPP_PSTATE_STOPPED
;
869 /* printer-state value */
872 if (!name
|| !device_uri
|| !uri
|| urisize
< 32)
875 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
)) == NULL
)
878 request
= ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER
);
880 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, "ipp://localhost/");
881 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
883 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
, device_uri
);
884 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
, name
);
886 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
, info
);
888 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
890 response
= cupsDoRequest(http
, request
, "/");
892 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
893 strlcpy(uri
, ippGetString(attr
, 0, NULL
), urisize
);
901 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
902 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
904 while (state
== IPP_PSTATE_STOPPED
&& cupsLastError() == IPP_STATUS_OK
)
909 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
911 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
912 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
913 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "printer-state");
915 response
= cupsDoRequest(http
, request
, "/");
917 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
918 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
930 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
932 * Destinations are enumerated from one or more sources. The callback function
933 * receives the @code user_data@ pointer and the destination pointer which can
934 * be used as input to the @link cupsCopyDest@ function. The function must
935 * return 1 to continue enumeration or 0 to stop.
937 * The @code type@ and @code mask@ arguments allow the caller to filter the
938 * destinations that are enumerated. Passing 0 for both will enumerate all
939 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
940 * destinations that are available but have not yet been added locally.
942 * Enumeration happens on the current thread and does not return until all
943 * destinations have been enumerated or the callback function returns 0.
945 * Note: The callback function will likely receive multiple updates for the same
946 * destinations - it is up to the caller to suppress any duplicate destinations.
948 * @since CUPS 1.6/macOS 10.8@
951 int /* O - 1 on success, 0 on failure */
953 unsigned flags
, /* I - Enumeration flags */
954 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
955 int *cancel
, /* I - Pointer to "cancel" variable */
956 cups_ptype_t type
, /* I - Printer type bits */
957 cups_ptype_t mask
, /* I - Mask for printer type bits */
958 cups_dest_cb_t cb
, /* I - Callback function */
959 void *user_data
) /* I - User data */
961 int i
, /* Looping var */
962 num_dests
; /* Number of destinations */
963 cups_dest_t
*dests
= NULL
, /* Destinations */
964 *dest
; /* Current destination */
965 const char *defprinter
; /* Default printer */
966 char name
[1024], /* Copy of printer name */
967 *instance
, /* Pointer to instance name */
968 *user_default
; /* User default printer */
969 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
970 int count
, /* Number of queries started */
971 completed
, /* Number of completed queries */
972 remaining
; /* Remainder of timeout */
973 struct timeval curtime
; /* Current time */
974 _cups_dnssd_data_t data
; /* Data for callback */
975 _cups_dnssd_device_t
*device
; /* Current device */
977 int nfds
, /* Number of files responded */
978 main_fd
; /* File descriptor for lookups */
979 DNSServiceRef ipp_ref
= NULL
, /* IPP browser */
980 local_ipp_ref
= NULL
; /* Local IPP browser */
982 DNSServiceRef ipps_ref
= NULL
,/* IPPS browser */
983 local_ipps_ref
= NULL
; /* Local IPPS browser */
984 # endif /* HAVE_SSL */
986 struct pollfd pfd
; /* Polling data */
988 fd_set input
; /* Input set for select() */
989 struct timeval timeout
; /* Timeout for select() */
990 # endif /* HAVE_POLL */
991 # else /* HAVE_AVAHI */
992 int error
; /* Error value */
993 AvahiServiceBrowser
*ipp_ref
= NULL
;/* IPP browser */
995 AvahiServiceBrowser
*ipps_ref
= NULL
; /* IPPS browser */
996 # endif /* HAVE_SSL */
997 # endif /* HAVE_DNSSD */
998 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1002 * Range check input...
1011 * Get ready to enumerate...
1014 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1015 memset(&data
, 0, sizeof(data
));
1020 data
.user_data
= user_data
;
1021 data
.devices
= cupsArrayNew3((cups_array_func_t
)cups_dnssd_compare_devices
, NULL
, NULL
, 0, NULL
, (cups_afree_func_t
)cups_dnssd_free_device
);
1022 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1024 if (!(mask
& CUPS_PRINTER_DISCOVERED
) || !(type
& CUPS_PRINTER_DISCOVERED
))
1027 * Get the list of local printers and pass them to the callback function...
1030 num_dests
= _cupsGetDests(CUPS_HTTP_DEFAULT
, IPP_OP_CUPS_GET_PRINTERS
, NULL
,
1031 &dests
, type
, mask
);
1033 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
1035 else if ((defprinter
= cupsGetDefault2(CUPS_HTTP_DEFAULT
)) != NULL
)
1037 strlcpy(name
, defprinter
, sizeof(name
));
1044 * Separate printer and instance name...
1047 if ((instance
= strchr(name
, '/')) != NULL
)
1051 * Lookup the printer and instance and make it the default...
1054 if ((dest
= cupsGetDest(name
, instance
, num_dests
, dests
)) != NULL
)
1055 dest
->is_default
= 1;
1058 for (i
= num_dests
, dest
= dests
;
1059 i
> 0 && (!cancel
|| !*cancel
);
1062 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1063 const char *device_uri
; /* Device URI */
1064 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1066 if (!(*cb
)(user_data
, i
> 1 ? CUPS_DEST_FLAGS_MORE
: CUPS_DEST_FLAGS_NONE
,
1070 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1071 if (!dest
->instance
&& (device_uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
&& !strncmp(device_uri
, "dnssd://", 8))
1074 * Add existing queue using service name, etc. so we don't list it again...
1077 char scheme
[32], /* URI scheme */
1078 userpass
[32], /* Username:password */
1079 serviceName
[256], /* Service name (host field) */
1080 resource
[256], /* Resource (options) */
1081 *regtype
, /* Registration type */
1082 *replyDomain
; /* Registration domain */
1083 int port
; /* Port number (not used) */
1085 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
)
1087 if ((regtype
= strstr(serviceName
, "._ipp")) != NULL
)
1091 if ((replyDomain
= strstr(regtype
, "._tcp.")) != NULL
)
1093 replyDomain
[5] = '\0';
1096 if ((device
= cups_dnssd_get_device(&data
, serviceName
, regtype
, replyDomain
)) != NULL
)
1097 device
->state
= _CUPS_DNSSD_ACTIVE
;
1102 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1105 cupsFreeDests(num_dests
, dests
);
1107 if (i
> 0 || msec
== 0)
1112 * Return early if the caller doesn't want to do discovery...
1115 if ((mask
& CUPS_PRINTER_DISCOVERED
) && !(type
& CUPS_PRINTER_DISCOVERED
))
1118 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1120 * Get Bonjour-shared printers...
1123 gettimeofday(&curtime
, NULL
);
1126 if (DNSServiceCreateConnection(&data
.main_ref
) != kDNSServiceErr_NoError
)
1129 main_fd
= DNSServiceRefSockFD(data
.main_ref
);
1131 ipp_ref
= data
.main_ref
;
1132 DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0,
1134 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
1136 local_ipp_ref
= data
.main_ref
;
1137 DNSServiceBrowse(&local_ipp_ref
, kDNSServiceFlagsShareConnection
,
1138 kDNSServiceInterfaceIndexLocalOnly
,
1140 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
1143 ipps_ref
= data
.main_ref
;
1144 DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0,
1146 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
1148 local_ipps_ref
= data
.main_ref
;
1149 DNSServiceBrowse(&local_ipps_ref
, kDNSServiceFlagsShareConnection
,
1150 kDNSServiceInterfaceIndexLocalOnly
,
1152 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
1153 # endif /* HAVE_SSL */
1155 # else /* HAVE_AVAHI */
1156 if ((data
.simple_poll
= avahi_simple_poll_new()) == NULL
)
1158 DEBUG_puts("cupsEnumDests: Unable to create Avahi simple poll object.");
1162 avahi_simple_poll_set_func(data
.simple_poll
, cups_dnssd_poll_cb
, &data
);
1164 data
.client
= avahi_client_new(avahi_simple_poll_get(data
.simple_poll
),
1165 0, cups_dnssd_client_cb
, &data
,
1169 DEBUG_puts("cupsEnumDests: Unable to create Avahi client.");
1170 avahi_simple_poll_free(data
.simple_poll
);
1175 ipp_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipp._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
);
1179 ipps_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipps._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
);
1180 # endif /* HAVE_SSL */
1181 # endif /* HAVE_DNSSD */
1184 remaining
= INT_MAX
;
1188 while (remaining
> 0 && (!cancel
|| !*cancel
))
1191 * Check for input...
1194 DEBUG_printf(("1cupsEnumDests: remaining=%d", remaining
));
1196 cups_elapsed(&curtime
);
1201 pfd
.events
= POLLIN
;
1203 nfds
= poll(&pfd
, 1, remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
1207 FD_SET(main_fd
, &input
);
1210 timeout
.tv_usec
= 1000 * (remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
1212 nfds
= select(main_fd
+ 1, &input
, NULL
, NULL
, &timeout
);
1213 # endif /* HAVE_POLL */
1216 DNSServiceProcessResult(data
.main_ref
);
1218 # else /* HAVE_AVAHI */
1221 if ((error
= avahi_simple_poll_iterate(data
.simple_poll
, _CUPS_DNSSD_MAXTIME
)) > 0)
1224 * We've been told to exit the loop. Perhaps the connection to
1231 DEBUG_printf(("1cupsEnumDests: got_data=%d", data
.got_data
));
1232 # endif /* HAVE_DNSSD */
1234 remaining
-= cups_elapsed(&curtime
);
1236 for (device
= (_cups_dnssd_device_t
*)cupsArrayFirst(data
.devices
),
1237 count
= 0, completed
= 0;
1239 device
= (_cups_dnssd_device_t
*)cupsArrayNext(data
.devices
))
1244 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
1247 if (!device
->ref
&& device
->state
== _CUPS_DNSSD_NEW
)
1249 DEBUG_printf(("1cupsEnumDests: Querying '%s'.", device
->fullName
));
1252 device
->ref
= data
.main_ref
;
1254 if (DNSServiceQueryRecord(&(device
->ref
),
1255 kDNSServiceFlagsShareConnection
,
1256 0, device
->fullName
,
1257 kDNSServiceType_TXT
,
1258 kDNSServiceClass_IN
,
1259 (DNSServiceQueryRecordReply
)cups_dnssd_query_cb
,
1260 &data
) == kDNSServiceErr_NoError
)
1267 device
->state
= _CUPS_DNSSD_ERROR
;
1269 DEBUG_puts("1cupsEnumDests: Query failed.");
1272 # else /* HAVE_AVAHI */
1273 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
)
1275 DEBUG_printf(("1cupsEnumDests: Query ref=%p", device
->ref
));
1280 device
->state
= _CUPS_DNSSD_ERROR
;
1282 DEBUG_printf(("1cupsEnumDests: Query failed: %s", avahi_strerror(avahi_client_errno(data
.client
))));
1284 # endif /* HAVE_DNSSD */
1286 else if (device
->ref
&& device
->state
== _CUPS_DNSSD_PENDING
)
1290 DEBUG_printf(("1cupsEnumDests: Query for \"%s\" is complete.", device
->fullName
));
1292 if ((device
->type
& mask
) == type
)
1294 DEBUG_printf(("1cupsEnumDests: Add callback for \"%s\".", device
->dest
.name
));
1295 if (!(*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, &device
->dest
))
1302 device
->state
= _CUPS_DNSSD_ACTIVE
;
1307 DEBUG_printf(("1cupsEnumDests: remaining=%d, browsers=%d, completed=%d, count=%d, devices count=%d", remaining
, data
.browsers
, completed
, count
, cupsArrayCount(data
.devices
)));
1309 if (data
.browsers
== 0 && completed
== cupsArrayCount(data
.devices
))
1312 DEBUG_printf(("1cupsEnumDests: remaining=%d, completed=%d, count=%d, devices count=%d", remaining
, completed
, count
, cupsArrayCount(data
.devices
)));
1314 if (completed
== cupsArrayCount(data
.devices
))
1316 # endif /* HAVE_AVAHI */
1318 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1326 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1328 DNSServiceRefDeallocate(ipp_ref
);
1329 DNSServiceRefDeallocate(local_ipp_ref
);
1332 DNSServiceRefDeallocate(ipp_ref
);
1333 DNSServiceRefDeallocate(local_ipp_ref
);
1334 # endif /* HAVE_SSL */
1336 DNSServiceRefDeallocate(data
.main_ref
);
1338 # else /* HAVE_AVAHI */
1339 avahi_service_browser_free(ipp_ref
);
1341 avahi_service_browser_free(ipps_ref
);
1342 # endif /* HAVE_SSL */
1344 avahi_client_free(data
.client
);
1345 avahi_simple_poll_free(data
.simple_poll
);
1346 # endif /* HAVE_DNSSD */
1348 cupsArrayDelete(data
.devices
);
1349 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1357 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
1359 * Destinations are enumerated from one or more sources. The block receives the
1360 * @code user_data@ pointer and the destination pointer which can be used as
1361 * input to the @link cupsCopyDest@ function. The block must return 1 to
1362 * continue enumeration or 0 to stop.
1364 * The @code type@ and @code mask@ arguments allow the caller to filter the
1365 * destinations that are enumerated. Passing 0 for both will enumerate all
1366 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
1367 * destinations that are available but have not yet been added locally.
1369 * Enumeration happens on the current thread and does not return until all
1370 * destinations have been enumerated or the block returns 0.
1372 * Note: The block will likely receive multiple updates for the same
1373 * destinations - it is up to the caller to suppress any duplicate destinations.
1375 * @since CUPS 1.6/macOS 10.8@ @exclude all@
1378 int /* O - 1 on success, 0 on failure */
1380 unsigned flags
, /* I - Enumeration flags */
1381 int timeout
, /* I - Timeout in milliseconds, 0 for indefinite */
1382 int *cancel
, /* I - Pointer to "cancel" variable */
1383 cups_ptype_t type
, /* I - Printer type bits */
1384 cups_ptype_t mask
, /* I - Mask for printer type bits */
1385 cups_dest_block_t block
) /* I - Block */
1387 return (cupsEnumDests(flags
, timeout
, cancel
, type
, mask
,
1388 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
1390 # endif /* __BLOCKS__ */
1394 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1398 cupsFreeDests(int num_dests
, /* I - Number of destinations */
1399 cups_dest_t
*dests
) /* I - Destinations */
1401 int i
; /* Looping var */
1402 cups_dest_t
*dest
; /* Current destination */
1405 if (num_dests
== 0 || dests
== NULL
)
1408 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1410 _cupsStrFree(dest
->name
);
1411 _cupsStrFree(dest
->instance
);
1413 cupsFreeOptions(dest
->num_options
, dest
->options
);
1421 * 'cupsGetDest()' - Get the named destination from the list.
1423 * Use the @link cupsEnumDests@ or @link cupsGetDests2@ functions to get a
1424 * list of supported destinations for the current user.
1427 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
1428 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1429 const char *instance
, /* I - Instance name or @code NULL@ */
1430 int num_dests
, /* I - Number of destinations */
1431 cups_dest_t
*dests
) /* I - Destinations */
1433 int diff
, /* Result of comparison */
1434 match
; /* Matching index */
1437 if (num_dests
<= 0 || !dests
)
1443 * NULL name for default printer.
1446 while (num_dests
> 0)
1448 if (dests
->is_default
)
1458 * Lookup name and optionally the instance...
1461 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
1464 return (dests
+ match
);
1472 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1475 const char * /* O - Printer URI */
1476 _cupsGetDestResource(
1477 cups_dest_t
*dest
, /* I - Destination */
1478 char *resource
, /* I - Resource buffer */
1479 size_t resourcesize
) /* I - Size of resource buffer */
1481 const char *uri
; /* Printer URI */
1482 char scheme
[32], /* URI scheme */
1483 userpass
[256], /* Username and password (unused) */
1484 hostname
[256]; /* Hostname */
1485 int port
; /* Port number */
1488 DEBUG_printf(("_cupsGetDestResource(dest=%p(%s), resource=%p, resourcesize=%d)", (void *)dest
, dest
->name
, (void *)resource
, (int)resourcesize
));
1491 * Range check input...
1494 if (!dest
|| !resource
|| resourcesize
< 1)
1499 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1504 * Grab the printer URI...
1507 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
1509 if ((uri
= cupsGetOption("resolved-device-uri", dest
->num_options
, dest
->options
)) == NULL
)
1511 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
1513 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1514 if (strstr(uri
, "._tcp"))
1515 uri
= cups_dnssd_resolve(dest
, uri
, 5000, NULL
, NULL
, NULL
);
1516 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1522 DEBUG_printf(("1_cupsGetDestResource: Resolved printer-uri-supported=\"%s\"", uri
));
1524 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, resource
, resourcesize
);
1529 DEBUG_printf(("1_cupsGetDestResource: Local printer-uri-supported=\"%s\"", uri
));
1531 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
1533 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
1537 DEBUG_puts("1_cupsGetDestResource: No printer-uri-supported found.");
1542 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1549 DEBUG_printf(("1_cupsGetDestResource: printer-uri-supported=\"%s\"", uri
));
1551 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1552 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
1553 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
1555 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1561 DEBUG_printf(("1_cupsGetDestResource: resource=\"%s\"", resource
));
1568 * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
1570 * "name" is the desired name for the printer. If @code NULL@, a name will be
1571 * created using the URI.
1573 * "uri" is the "ipp" or "ipps" URI for the printer.
1575 * @since CUPS 2.0/macOS 10.10@
1578 cups_dest_t
* /* O - Destination or @code NULL@ */
1579 cupsGetDestWithURI(const char *name
, /* I - Desired printer name or @code NULL@ */
1580 const char *uri
) /* I - URI for the printer */
1582 cups_dest_t
*dest
; /* New destination */
1583 char temp
[1024], /* Temporary string */
1584 scheme
[256], /* Scheme from URI */
1585 userpass
[256], /* Username:password from URI */
1586 hostname
[256], /* Hostname from URI */
1587 resource
[1024], /* Resource path from URI */
1588 *ptr
; /* Pointer into string */
1589 const char *info
; /* printer-info string */
1590 int port
; /* Port number from URI */
1594 * Range check input...
1599 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1603 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
||
1604 (strncmp(uri
, "ipp://", 6) && strncmp(uri
, "ipps://", 7)))
1606 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1618 * Create the name from the URI...
1621 if (strstr(hostname
, "._tcp"))
1624 * Use the service instance name...
1627 if ((ptr
= strstr(hostname
, "._")) != NULL
)
1630 cups_queue_name(temp
, hostname
, sizeof(temp
));
1634 else if (!strncmp(resource
, "/classes/", 9))
1636 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 9, hostname
);
1637 name
= resource
+ 9;
1640 else if (!strncmp(resource
, "/printers/", 10))
1642 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 10, hostname
);
1643 name
= resource
+ 10;
1654 * Create the destination...
1657 if ((dest
= calloc(1, sizeof(cups_dest_t
))) == NULL
)
1659 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1663 dest
->name
= _cupsStrAlloc(name
);
1664 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &(dest
->options
));
1665 dest
->num_options
= cupsAddOption("printer-info", info
, dest
->num_options
, &(dest
->options
));
1672 * '_cupsGetDests()' - Get destinations from a server.
1674 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1675 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1678 * "name" is the name of an existing printer and is only used when "op" is
1679 * IPP_OP_GET_PRINTER_ATTRIBUTES.
1681 * "dest" is initialized to point to the array of destinations.
1683 * 0 is returned if there are no printers, no default printer, or the named
1684 * printer does not exist, respectively.
1686 * Free the memory used by the destination array using the @link cupsFreeDests@
1689 * Note: On macOS this function also gets the default paper from the system
1690 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1691 * options array for each destination that supports it.
1694 int /* O - Number of destinations */
1695 _cupsGetDests(http_t
*http
, /* I - Connection to server or
1696 * @code CUPS_HTTP_DEFAULT@ */
1697 ipp_op_t op
, /* I - IPP operation */
1698 const char *name
, /* I - Name of destination */
1699 cups_dest_t
**dests
, /* IO - Destinations */
1700 cups_ptype_t type
, /* I - Printer type bits */
1701 cups_ptype_t mask
) /* I - Printer type mask */
1703 int num_dests
= 0; /* Number of destinations */
1704 cups_dest_t
*dest
; /* Current destination */
1705 ipp_t
*request
, /* IPP Request */
1706 *response
; /* IPP Response */
1707 ipp_attribute_t
*attr
; /* Current attribute */
1708 const char *printer_name
; /* printer-name attribute */
1709 char uri
[1024]; /* printer-uri value */
1710 int num_options
; /* Number of options */
1711 cups_option_t
*options
; /* Options */
1713 char media_default
[41]; /* Default paper size */
1714 #endif /* __APPLE__ */
1715 char optname
[1024], /* Option name */
1716 value
[2048], /* Option value */
1717 *ptr
; /* Pointer into name/value */
1718 static const char * const pattrs
[] = /* Attributes we're interested in */
1720 "auth-info-required",
1722 "job-sheets-default",
1723 "marker-change-time",
1725 "marker-high-levels",
1727 "marker-low-levels",
1733 #endif /* __APPLE__ */
1737 "printer-is-accepting-jobs",
1738 "printer-is-shared",
1740 "printer-make-and-model",
1741 "printer-mandatory-job-attributes",
1744 "printer-state-change-time",
1745 "printer-state-reasons",
1747 "printer-uri-supported"
1751 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
));
1755 * Get the default paper size...
1758 appleGetPaperSize(media_default
, sizeof(media_default
));
1759 #endif /* __APPLE__ */
1762 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
1763 * require the following attributes:
1765 * attributes-charset
1766 * attributes-natural-language
1767 * requesting-user-name
1768 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
1771 request
= ippNewRequest(op
);
1773 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1774 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1777 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1778 "requesting-user-name", NULL
, cupsUser());
1780 if (name
&& op
!= IPP_OP_CUPS_GET_DEFAULT
)
1782 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1783 "localhost", ippPort(), "/printers/%s", name
);
1784 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1789 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type", (int)type
);
1790 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask", (int)mask
);
1794 * Do the request and get back a response...
1797 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1799 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1802 * Skip leading attributes until we hit a printer...
1805 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1812 * Pull the needed attributes from this printer...
1815 printer_name
= NULL
;
1819 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1821 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1822 attr
->value_tag
!= IPP_TAG_ENUM
&&
1823 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1824 attr
->value_tag
!= IPP_TAG_TEXT
&&
1825 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1826 attr
->value_tag
!= IPP_TAG_NAME
&&
1827 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1828 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1829 attr
->value_tag
!= IPP_TAG_RANGE
&&
1830 attr
->value_tag
!= IPP_TAG_URI
)
1833 if (!strcmp(attr
->name
, "auth-info-required") ||
1834 !strcmp(attr
->name
, "device-uri") ||
1835 !strcmp(attr
->name
, "marker-change-time") ||
1836 !strcmp(attr
->name
, "marker-colors") ||
1837 !strcmp(attr
->name
, "marker-high-levels") ||
1838 !strcmp(attr
->name
, "marker-levels") ||
1839 !strcmp(attr
->name
, "marker-low-levels") ||
1840 !strcmp(attr
->name
, "marker-message") ||
1841 !strcmp(attr
->name
, "marker-names") ||
1842 !strcmp(attr
->name
, "marker-types") ||
1843 !strcmp(attr
->name
, "printer-commands") ||
1844 !strcmp(attr
->name
, "printer-info") ||
1845 !strcmp(attr
->name
, "printer-is-shared") ||
1846 !strcmp(attr
->name
, "printer-make-and-model") ||
1847 !strcmp(attr
->name
, "printer-mandatory-job-attributes") ||
1848 !strcmp(attr
->name
, "printer-state") ||
1849 !strcmp(attr
->name
, "printer-state-change-time") ||
1850 !strcmp(attr
->name
, "printer-type") ||
1851 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1852 !strcmp(attr
->name
, "printer-location") ||
1853 !strcmp(attr
->name
, "printer-state-reasons") ||
1854 !strcmp(attr
->name
, "printer-uri-supported"))
1857 * Add a printer description attribute...
1860 num_options
= cupsAddOption(attr
->name
,
1861 cups_make_string(attr
, value
,
1863 num_options
, &options
);
1866 else if (!strcmp(attr
->name
, "media-supported"))
1869 * See if we can set a default media size...
1872 int i
; /* Looping var */
1874 for (i
= 0; i
< attr
->num_values
; i
++)
1875 if (!_cups_strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1877 num_options
= cupsAddOption("media", media_default
, num_options
,
1882 #endif /* __APPLE__ */
1883 else if (!strcmp(attr
->name
, "printer-name") &&
1884 attr
->value_tag
== IPP_TAG_NAME
)
1885 printer_name
= attr
->values
[0].string
.text
;
1886 else if (strncmp(attr
->name
, "notify-", 7) &&
1887 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1888 attr
->value_tag
== IPP_TAG_ENUM
||
1889 attr
->value_tag
== IPP_TAG_INTEGER
||
1890 attr
->value_tag
== IPP_TAG_KEYWORD
||
1891 attr
->value_tag
== IPP_TAG_NAME
||
1892 attr
->value_tag
== IPP_TAG_RANGE
) &&
1893 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1896 * Add a default option...
1899 strlcpy(optname
, attr
->name
, sizeof(optname
));
1900 optname
[ptr
- attr
->name
] = '\0';
1902 if (_cups_strcasecmp(optname
, "media") ||
1903 !cupsGetOption("media", num_options
, options
))
1904 num_options
= cupsAddOption(optname
,
1905 cups_make_string(attr
, value
,
1907 num_options
, &options
);
1912 * See if we have everything needed...
1917 cupsFreeOptions(num_options
, options
);
1925 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1927 dest
->num_options
= num_options
;
1928 dest
->options
= options
;
1931 cupsFreeOptions(num_options
, options
);
1937 ippDelete(response
);
1941 * Return the count...
1949 * 'cupsGetDests()' - Get the list of destinations from the default server.
1951 * Starting with CUPS 1.2, the returned list of destinations include the
1952 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1953 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1954 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1955 * attributes as options.
1957 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1958 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1959 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1961 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1962 * be used. The "printer-uri-supported" option will be present for those IPP
1963 * printers that have been recently used.
1965 * Use the @link cupsFreeDests@ function to free the destination list and
1966 * the @link cupsGetDest@ function to find a particular destination.
1971 int /* O - Number of destinations */
1972 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
1974 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
1979 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
1981 * Starting with CUPS 1.2, the returned list of destinations include the
1982 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1983 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1984 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1985 * attributes as options.
1987 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1988 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1989 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1991 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1992 * be used. The "printer-uri-supported" option will be present for those IPP
1993 * printers that have been recently used.
1995 * Use the @link cupsFreeDests@ function to free the destination list and
1996 * the @link cupsGetDest@ function to find a particular destination.
1998 * @since CUPS 1.1.21/macOS 10.4@
2001 int /* O - Number of destinations */
2002 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2003 cups_dest_t
**dests
) /* O - Destinations */
2005 _cups_getdata_t data
; /* Enumeration data */
2006 cups_dest_t
*dest
; /* Current destination */
2007 const char *home
; /* HOME environment variable */
2008 char filename
[1024]; /* Local ~/.cups/lpoptions file */
2009 const char *defprinter
; /* Default printer */
2010 char name
[1024], /* Copy of printer name */
2011 *instance
, /* Pointer to instance name */
2012 *user_default
; /* User default printer */
2013 int num_reals
; /* Number of real queues */
2014 cups_dest_t
*reals
; /* Real queues */
2015 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2019 * Range check the input...
2024 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad NULL dests pointer"), 1);
2029 * Grab the printers and classes...
2035 cupsEnumDests(0, 1000, NULL
, 0, 0, (cups_dest_cb_t
)cups_get_cb
, &data
);
2037 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2039 cupsFreeDests(data
.num_dests
, data
.dests
);
2041 *dests
= (cups_dest_t
*)0;
2047 * Make a copy of the "real" queues for a later sanity check...
2050 if (data
.num_dests
> 0)
2052 num_reals
= data
.num_dests
;
2053 reals
= calloc((size_t)num_reals
, sizeof(cups_dest_t
));
2056 memcpy(reals
, data
.dests
, (size_t)num_reals
* sizeof(cups_dest_t
));
2067 * Grab the default destination...
2070 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
2072 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
2074 strlcpy(name
, defprinter
, sizeof(name
));
2081 * Separate printer and instance name...
2084 if ((instance
= strchr(name
, '/')) != NULL
)
2088 * Lookup the printer and instance and make it the default...
2091 if ((dest
= cupsGetDest(name
, instance
, data
.num_dests
, data
.dests
)) != NULL
)
2092 dest
->is_default
= 1;
2098 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
2101 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2102 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
2104 if ((home
= getenv("HOME")) != NULL
)
2106 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2108 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
2112 * Validate the current default destination - this prevents old
2113 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
2114 * pointing to a non-existent printer or class...
2120 * See if we have a default printer...
2123 if ((dest
= cupsGetDest(NULL
, NULL
, data
.num_dests
, data
.dests
)) != NULL
)
2126 * Have a default; see if it is real...
2129 if (!cupsGetDest(dest
->name
, NULL
, num_reals
, reals
))
2132 * Remove the non-real printer from the list, since we don't want jobs
2133 * going to an unexpected printer... (<rdar://problem/14216472>)
2136 data
.num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
.num_dests
, &data
.dests
);
2148 * Return the number of destinations...
2151 *dests
= data
.dests
;
2153 if (data
.num_dests
> 0)
2154 _cupsSetError(IPP_STATUS_OK
, NULL
, 0);
2156 return (data
.num_dests
);
2161 * 'cupsGetNamedDest()' - Get options for the named destination.
2163 * This function is optimized for retrieving a single destination and should
2164 * be used instead of @link cupsGetDests2@ and @link cupsGetDest@ when you
2165 * either know the name of the destination or want to print to the default
2166 * destination. If @code NULL@ is returned, the destination does not exist or
2167 * there is no default destination.
2169 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
2170 * server will be used.
2172 * If "name" is @code NULL@, the default printer for the current user will be
2175 * The returned destination must be freed using @link cupsFreeDests@ with a
2176 * "num_dests" value of 1.
2178 * @since CUPS 1.4/macOS 10.6@
2181 cups_dest_t
* /* O - Destination or @code NULL@ */
2182 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2183 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
2184 const char *instance
) /* I - Instance name or @code NULL@ */
2186 cups_dest_t
*dest
; /* Destination */
2187 char filename
[1024], /* Path to lpoptions */
2188 defname
[256]; /* Default printer name */
2189 const char *home
= getenv("HOME"); /* Home directory */
2190 int set_as_default
= 0; /* Set returned destination as default */
2191 ipp_op_t op
= IPP_OP_GET_PRINTER_ATTRIBUTES
;
2192 /* IPP operation to get server ops */
2193 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2196 DEBUG_printf(("cupsGetNamedDest(http=%p, name=\"%s\", instance=\"%s\")", (void *)http
, name
, instance
));
2199 * If "name" is NULL, find the default destination...
2205 name
= _cupsUserDefault(defname
, sizeof(defname
));
2209 char *ptr
; /* Temporary pointer... */
2211 if ((ptr
= strchr(defname
, '/')) != NULL
)
2222 * No default in the environment, try the user's lpoptions files...
2225 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2227 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
2233 * Still not there? Try the system lpoptions file...
2236 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
2237 cg
->cups_serverroot
);
2238 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
2244 * No locally-set default destination, ask the server...
2247 op
= IPP_OP_CUPS_GET_DEFAULT
;
2249 DEBUG_puts("1cupsGetNamedDest: Asking server for default printer...");
2252 DEBUG_printf(("1cupsGetNamedDest: Using name=\"%s\"...", name
));
2256 * Get the printer's attributes...
2259 if (!_cupsGetDests(http
, op
, name
, &dest
, 0, 0))
2263 _cups_namedata_t data
; /* Callback data */
2265 DEBUG_puts("1cupsGetNamedDest: No queue found for printer, looking on network...");
2270 cupsEnumDests(0, 1000, NULL
, 0, 0, (cups_dest_cb_t
)cups_name_cb
, &data
);
2281 DEBUG_printf(("1cupsGetNamedDest: Got dest=%p", (void *)dest
));
2284 dest
->instance
= _cupsStrAlloc(instance
);
2287 dest
->is_default
= 1;
2290 * Then add local options...
2293 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2294 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
2298 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2300 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
2304 * Return the result...
2312 * 'cupsRemoveDest()' - Remove a destination from the destination list.
2314 * Removing a destination/instance does not delete the class or printer
2315 * queue, merely the lpoptions for that destination/instance. Use the
2316 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
2317 * options for the user.
2319 * @since CUPS 1.3/macOS 10.5@
2322 int /* O - New number of destinations */
2323 cupsRemoveDest(const char *name
, /* I - Destination name */
2324 const char *instance
, /* I - Instance name or @code NULL@ */
2325 int num_dests
, /* I - Number of destinations */
2326 cups_dest_t
**dests
) /* IO - Destinations */
2328 int i
; /* Index into destinations */
2329 cups_dest_t
*dest
; /* Pointer to destination */
2333 * Find the destination...
2336 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
2343 _cupsStrFree(dest
->name
);
2344 _cupsStrFree(dest
->instance
);
2345 cupsFreeOptions(dest
->num_options
, dest
->options
);
2348 * Remove the destination from the array...
2353 i
= (int)(dest
- *dests
);
2356 memmove(dest
, dest
+ 1, (size_t)(num_dests
- i
) * sizeof(cups_dest_t
));
2363 * 'cupsSetDefaultDest()' - Set the default destination.
2365 * @since CUPS 1.3/macOS 10.5@
2370 const char *name
, /* I - Destination name */
2371 const char *instance
, /* I - Instance name or @code NULL@ */
2372 int num_dests
, /* I - Number of destinations */
2373 cups_dest_t
*dests
) /* I - Destinations */
2375 int i
; /* Looping var */
2376 cups_dest_t
*dest
; /* Current destination */
2380 * Range check input...
2383 if (!name
|| num_dests
<= 0 || !dests
)
2387 * Loop through the array and set the "is_default" flag for the matching
2391 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2392 dest
->is_default
= !_cups_strcasecmp(name
, dest
->name
) &&
2393 ((!instance
&& !dest
->instance
) ||
2394 (instance
&& dest
->instance
&&
2395 !_cups_strcasecmp(instance
, dest
->instance
)));
2400 * 'cupsSetDests()' - Save the list of destinations for the default server.
2402 * This function saves the destinations to /etc/cups/lpoptions when run
2403 * as root and ~/.cups/lpoptions when run as a normal user.
2409 cupsSetDests(int num_dests
, /* I - Number of destinations */
2410 cups_dest_t
*dests
) /* I - Destinations */
2412 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
2417 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
2419 * This function saves the destinations to /etc/cups/lpoptions when run
2420 * as root and ~/.cups/lpoptions when run as a normal user.
2422 * @since CUPS 1.1.21/macOS 10.4@
2425 int /* O - 0 on success, -1 on error */
2426 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2427 int num_dests
, /* I - Number of destinations */
2428 cups_dest_t
*dests
) /* I - Destinations */
2430 int i
, j
; /* Looping vars */
2431 int wrote
; /* Wrote definition? */
2432 cups_dest_t
*dest
; /* Current destination */
2433 cups_option_t
*option
; /* Current option */
2434 _ipp_option_t
*match
; /* Matching attribute for option */
2435 FILE *fp
; /* File pointer */
2437 const char *home
; /* HOME environment variable */
2439 char filename
[1024]; /* lpoptions file */
2440 int num_temps
; /* Number of temporary destinations */
2441 cups_dest_t
*temps
= NULL
, /* Temporary destinations */
2442 *temp
; /* Current temporary dest */
2443 const char *val
; /* Value of temporary option */
2444 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2448 * Range check the input...
2451 if (!num_dests
|| !dests
)
2455 * Get the server destinations...
2458 num_temps
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &temps
, 0, 0);
2460 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2462 cupsFreeDests(num_temps
, temps
);
2467 * Figure out which file to write to...
2470 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2476 * Merge in server defaults...
2479 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
2482 * Point to user defaults...
2485 if ((home
= getenv("HOME")) != NULL
)
2488 * Create ~/.cups subdirectory...
2491 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
2492 if (access(filename
, 0))
2493 mkdir(filename
, 0700);
2495 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2501 * Try to open the file...
2504 if ((fp
= fopen(filename
, "w")) == NULL
)
2506 cupsFreeDests(num_temps
, temps
);
2512 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2517 fchmod(fileno(fp
), 0644);
2521 * Write each printer; each line looks like:
2523 * Dest name[/instance] options
2524 * Default name[/instance] options
2527 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2528 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
2530 if (dest
->is_default
)
2532 fprintf(fp
, "Default %s", dest
->name
);
2534 fprintf(fp
, "/%s", dest
->instance
);
2541 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
2542 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
2544 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
2547 * See if this option is a printer attribute; if so, skip it...
2550 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
2551 match
->group_tag
== IPP_TAG_PRINTER
)
2555 * See if the server/global options match these; if so, don't
2560 (val
= cupsGetOption(option
->name
, temp
->num_options
,
2561 temp
->options
)) != NULL
&&
2562 !_cups_strcasecmp(val
, option
->value
))
2566 * Options don't match, write to the file...
2571 fprintf(fp
, "Dest %s", dest
->name
);
2573 fprintf(fp
, "/%s", dest
->instance
);
2577 if (option
->value
[0])
2579 if (strchr(option
->value
, ' ') ||
2580 strchr(option
->value
, '\\') ||
2581 strchr(option
->value
, '\"') ||
2582 strchr(option
->value
, '\''))
2585 * Quote the value...
2588 fprintf(fp
, " %s=\"", option
->name
);
2590 for (val
= option
->value
; *val
; val
++)
2592 if (strchr("\"\'\\", *val
))
2603 * Store the literal value...
2606 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
2610 fprintf(fp
, " %s", option
->name
);
2618 * Free the temporary destinations and close the file...
2621 cupsFreeDests(num_temps
, temps
);
2627 * Set the default printer for this location - this allows command-line
2628 * and GUI applications to share the same default destination...
2631 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
2633 CFStringRef name
= CFStringCreateWithCString(kCFAllocatorDefault
,
2635 kCFStringEncodingUTF8
);
2636 /* Default printer name */
2640 _cupsAppleSetDefaultPrinter(name
);
2644 #endif /* __APPLE__ */
2646 #ifdef HAVE_NOTIFY_POST
2648 * Send a notification so that macOS applications can know about the
2652 notify_post("com.apple.printerListChange");
2653 #endif /* HAVE_NOTIFY_POST */
2660 * '_cupsUserDefault()' - Get the user default printer from environment
2661 * variables and location information.
2664 char * /* O - Default printer or NULL */
2665 _cupsUserDefault(char *name
, /* I - Name buffer */
2666 size_t namesize
) /* I - Size of name buffer */
2668 const char *env
; /* LPDEST or PRINTER env variable */
2670 CFStringRef locprinter
; /* Last printer as this location */
2671 #endif /* __APPLE__ */
2674 if ((env
= getenv("LPDEST")) == NULL
)
2675 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
2680 strlcpy(name
, env
, namesize
);
2686 * Use location-based defaults if "use last printer" is selected in the
2687 * system preferences...
2690 if ((locprinter
= _cupsAppleCopyDefaultPrinter()) != NULL
)
2692 CFStringGetCString(locprinter
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
);
2693 CFRelease(locprinter
);
2698 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name
));
2700 return (*name
? name
: NULL
);
2704 * No location-based defaults on this platform...
2709 #endif /* __APPLE__ */
2713 #if _CUPS_LOCATION_DEFAULTS
2715 * 'appleCopyLocations()' - Copy the location history array.
2718 static CFArrayRef
/* O - Location array or NULL */
2719 appleCopyLocations(void)
2721 CFArrayRef locations
; /* Location array */
2725 * Look up the location array in the preferences...
2728 if ((locations
= CFPreferencesCopyAppValue(kLastUsedPrintersKey
,
2729 kCUPSPrintingPrefs
)) == NULL
)
2732 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
2734 CFRelease(locations
);
2743 * 'appleCopyNetwork()' - Get the network ID for the current location.
2746 static CFStringRef
/* O - Network ID */
2747 appleCopyNetwork(void)
2749 SCDynamicStoreRef dynamicStore
; /* System configuration data */
2750 CFStringRef key
; /* Current network configuration key */
2751 CFDictionaryRef ip_dict
; /* Network configuration data */
2752 CFStringRef network
= NULL
; /* Current network ID */
2755 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("libcups"), NULL
,
2759 * First use the IPv6 router address, if available, since that will generally
2760 * be a globally-unique link-local address.
2763 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2764 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv6
)) != NULL
)
2766 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2768 if ((network
= CFDictionaryGetValue(ip_dict
,
2769 kSCPropNetIPv6Router
)) != NULL
)
2779 * If that doesn't work, try the IPv4 router address. This isn't as unique
2780 * and will likely be a 10.x.y.z or 192.168.y.z address...
2785 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2786 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
2788 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2790 if ((network
= CFDictionaryGetValue(ip_dict
,
2791 kSCPropNetIPv4Router
)) != NULL
)
2801 CFRelease(dynamicStore
);
2806 #endif /* _CUPS_LOCATION_DEFAULTS */
2811 * 'appleGetPaperSize()' - Get the default paper size.
2814 static char * /* O - Default paper size */
2815 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
2816 size_t namesize
) /* I - Size of buffer */
2818 CFStringRef defaultPaperID
; /* Default paper ID */
2819 pwg_media_t
*pwgmedia
; /* PWG media size */
2822 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
2823 if (!defaultPaperID
||
2824 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
2825 !CFStringGetCString(defaultPaperID
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
))
2827 else if ((pwgmedia
= pwgMediaForLegacy(name
)) != NULL
)
2828 strlcpy(name
, pwgmedia
->pwg
, namesize
);
2831 CFRelease(defaultPaperID
);
2835 #endif /* __APPLE__ */
2838 #if _CUPS_LOCATION_DEFAULTS
2840 * 'appleGetPrinter()' - Get a printer from the history array.
2843 static CFStringRef
/* O - Printer name or NULL */
2844 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
2845 CFStringRef network
, /* I - Network name */
2846 CFIndex
*locindex
) /* O - Index in array */
2848 CFIndex i
, /* Looping var */
2849 count
; /* Number of locations */
2850 CFDictionaryRef location
; /* Current location */
2851 CFStringRef locnetwork
, /* Current network */
2852 locprinter
; /* Current printer */
2855 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
2856 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
2857 CFGetTypeID(location
) == CFDictionaryGetTypeID())
2859 if ((locnetwork
= CFDictionaryGetValue(location
,
2860 kLocationNetworkKey
)) != NULL
&&
2861 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
2862 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
2863 (locprinter
= CFDictionaryGetValue(location
,
2864 kLocationPrinterIDKey
)) != NULL
&&
2865 CFGetTypeID(locprinter
) == CFStringGetTypeID())
2870 return (locprinter
);
2876 #endif /* _CUPS_LOCATION_DEFAULTS */
2880 * 'cups_add_dest()' - Add a destination to the array.
2882 * Unlike cupsAddDest(), this function does not check for duplicates.
2885 static cups_dest_t
* /* O - New destination */
2886 cups_add_dest(const char *name
, /* I - Name of destination */
2887 const char *instance
, /* I - Instance or NULL */
2888 int *num_dests
, /* IO - Number of destinations */
2889 cups_dest_t
**dests
) /* IO - Destinations */
2891 int insert
, /* Insertion point */
2892 diff
; /* Result of comparison */
2893 cups_dest_t
*dest
; /* Destination pointer */
2897 * Add new destination...
2900 if (*num_dests
== 0)
2901 dest
= malloc(sizeof(cups_dest_t
));
2903 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (size_t)(*num_dests
+ 1));
2911 * Find where to insert the destination...
2914 if (*num_dests
== 0)
2918 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
2926 * Move the array elements as needed...
2929 if (insert
< *num_dests
)
2930 memmove(*dests
+ insert
+ 1, *dests
+ insert
, (size_t)(*num_dests
- insert
) * sizeof(cups_dest_t
));
2935 * Initialize the destination...
2938 dest
= *dests
+ insert
;
2939 dest
->name
= _cupsStrAlloc(name
);
2940 dest
->instance
= _cupsStrAlloc(instance
);
2941 dest
->is_default
= 0;
2942 dest
->num_options
= 0;
2943 dest
->options
= (cups_option_t
*)0;
2951 * 'cups_block_cb()' - Enumeration callback for block API.
2954 static int /* O - 1 to continue, 0 to stop */
2956 cups_dest_block_t block
, /* I - Block */
2957 unsigned flags
, /* I - Destination flags */
2958 cups_dest_t
*dest
) /* I - Destination */
2960 return ((block
)(flags
, dest
));
2962 # endif /* __BLOCKS__ */
2966 * 'cups_compare_dests()' - Compare two destinations.
2969 static int /* O - Result of comparison */
2970 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
2971 cups_dest_t
*b
) /* I - Second destination */
2973 int diff
; /* Difference */
2976 if ((diff
= _cups_strcasecmp(a
->name
, b
->name
)) != 0)
2978 else if (a
->instance
&& b
->instance
)
2979 return (_cups_strcasecmp(a
->instance
, b
->instance
));
2981 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
2985 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2988 * 'cups_dnssd_browse_cb()' - Browse for printers.
2992 cups_dnssd_browse_cb(
2993 DNSServiceRef sdRef
, /* I - Service reference */
2994 DNSServiceFlags flags
, /* I - Option flags */
2995 uint32_t interfaceIndex
, /* I - Interface number */
2996 DNSServiceErrorType errorCode
, /* I - Error, if any */
2997 const char *serviceName
, /* I - Name of service/device */
2998 const char *regtype
, /* I - Type of service */
2999 const char *replyDomain
, /* I - Service domain */
3000 void *context
) /* I - Enumeration data */
3002 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3003 /* Enumeration data */
3006 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
));
3009 * Don't do anything on error...
3012 if (errorCode
!= kDNSServiceErr_NoError
)
3019 cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
3023 # else /* HAVE_AVAHI */
3025 * 'cups_dnssd_browse_cb()' - Browse for printers.
3029 cups_dnssd_browse_cb(
3030 AvahiServiceBrowser
*browser
, /* I - Browser */
3031 AvahiIfIndex interface
, /* I - Interface index (unused) */
3032 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3033 AvahiBrowserEvent event
, /* I - What happened */
3034 const char *name
, /* I - Service name */
3035 const char *type
, /* I - Registration type */
3036 const char *domain
, /* I - Domain */
3037 AvahiLookupResultFlags flags
, /* I - Flags */
3038 void *context
) /* I - Devices array */
3041 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
3042 /* Client information */
3044 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3045 /* Enumeration data */
3052 DEBUG_printf(("cups_dnssd_browse_cb(..., name=\"%s\", type=\"%s\", domain=\"%s\", ...);", name
, type
, domain
));
3056 case AVAHI_BROWSER_FAILURE
:
3057 DEBUG_printf(("cups_dnssd_browse_cb: %s", avahi_strerror(avahi_client_errno(client
))));
3058 avahi_simple_poll_quit(data
->simple_poll
);
3061 case AVAHI_BROWSER_NEW
:
3063 * This object is new on the network.
3066 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
3069 * This comes from the local machine so ignore it.
3072 DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".", name
));
3077 * Create a device entry for it if it doesn't yet exist.
3080 cups_dnssd_get_device(data
, name
, type
, domain
);
3084 case AVAHI_BROWSER_REMOVE
:
3085 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
3088 case AVAHI_BROWSER_ALL_FOR_NOW
:
3089 DEBUG_puts("cups_dnssd_browse_cb: ALL_FOR_NOW");
3097 * 'cups_dnssd_client_cb()' - Avahi client callback function.
3101 cups_dnssd_client_cb(
3102 AvahiClient
*client
, /* I - Client information (unused) */
3103 AvahiClientState state
, /* I - Current state */
3104 void *context
) /* I - User data (unused) */
3106 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3107 /* Enumeration data */
3112 DEBUG_printf(("cups_dnssd_client_cb(client=%p, state=%d, context=%p)", client
, state
, context
));
3115 * If the connection drops, quit.
3118 if (state
== AVAHI_CLIENT_FAILURE
)
3120 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
3121 avahi_simple_poll_quit(data
->simple_poll
);
3124 # endif /* HAVE_DNSSD */
3128 * 'cups_dnssd_compare_device()' - Compare two devices.
3131 static int /* O - Result of comparison */
3132 cups_dnssd_compare_devices(
3133 _cups_dnssd_device_t
*a
, /* I - First device */
3134 _cups_dnssd_device_t
*b
) /* I - Second device */
3136 return (strcmp(a
->dest
.name
, b
->dest
.name
));
3141 * 'cups_dnssd_free_device()' - Free the memory used by a device.
3145 cups_dnssd_free_device(
3146 _cups_dnssd_device_t
*device
, /* I - Device */
3147 _cups_dnssd_data_t
*data
) /* I - Enumeration data */
3149 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", (void *)device
, device
->dest
.name
, (void *)data
));
3153 DNSServiceRefDeallocate(device
->ref
);
3154 # else /* HAVE_AVAHI */
3156 avahi_record_browser_free(device
->ref
);
3157 # endif /* HAVE_DNSSD */
3159 _cupsStrFree(device
->domain
);
3160 _cupsStrFree(device
->fullName
);
3161 _cupsStrFree(device
->regtype
);
3162 _cupsStrFree(device
->dest
.name
);
3164 cupsFreeOptions(device
->dest
.num_options
, device
->dest
.options
);
3171 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
3174 static _cups_dnssd_device_t
* /* O - Device */
3175 cups_dnssd_get_device(
3176 _cups_dnssd_data_t
*data
, /* I - Enumeration data */
3177 const char *serviceName
, /* I - Service name */
3178 const char *regtype
, /* I - Registration type */
3179 const char *replyDomain
) /* I - Domain name */
3181 _cups_dnssd_device_t key
, /* Search key */
3182 *device
; /* Device */
3183 char fullName
[kDNSServiceMaxDomainName
],
3184 /* Full name for query */
3185 name
[128]; /* Queue name */
3188 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\")", (void *)data
, serviceName
, regtype
, replyDomain
));
3191 * See if this is an existing device...
3194 cups_queue_name(name
, serviceName
, sizeof(name
));
3196 key
.dest
.name
= name
;
3198 if ((device
= cupsArrayFind(data
->devices
, &key
)) != NULL
)
3201 * Yes, see if we need to do anything with this...
3204 int update
= 0; /* Non-zero if we need to update */
3206 if (!_cups_strcasecmp(replyDomain
, "local.") &&
3207 _cups_strcasecmp(device
->domain
, replyDomain
))
3210 * Update the "global" listing to use the .local domain name instead.
3213 _cupsStrFree(device
->domain
);
3214 device
->domain
= _cupsStrAlloc(replyDomain
);
3216 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
3217 "domain.", device
->dest
.name
));
3222 if (!_cups_strcasecmp(regtype
, "_ipps._tcp") &&
3223 _cups_strcasecmp(device
->regtype
, regtype
))
3226 * Prefer IPPS over IPP.
3229 _cupsStrFree(device
->regtype
);
3230 device
->regtype
= _cupsStrAlloc(regtype
);
3232 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
3233 device
->dest
.name
));
3240 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
3241 device
->dest
.name
));
3248 * No, add the device...
3251 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
3252 "'%s'.", serviceName
,
3253 !strcmp(regtype
, "_ipps._tcp") ? "IPPS" : "IPP",
3256 device
= calloc(sizeof(_cups_dnssd_device_t
), 1);
3257 device
->dest
.name
= _cupsStrAlloc(name
);
3258 device
->domain
= _cupsStrAlloc(replyDomain
);
3259 device
->regtype
= _cupsStrAlloc(regtype
);
3261 device
->dest
.num_options
= cupsAddOption("printer-info", serviceName
, 0, &device
->dest
.options
);
3263 cupsArrayAdd(data
->devices
, device
);
3267 * Set the "full name" of this service, which is used for queries...
3271 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
3272 # else /* HAVE_AVAHI */
3273 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
, regtype
, replyDomain
);
3274 # endif /* HAVE_DNSSD */
3276 _cupsStrFree(device
->fullName
);
3277 device
->fullName
= _cupsStrAlloc(fullName
);
3282 DNSServiceRefDeallocate(device
->ref
);
3283 # else /* HAVE_AVAHI */
3284 avahi_record_browser_free(device
->ref
);
3285 # endif /* HAVE_DNSSD */
3290 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3292 DEBUG_printf(("6cups_dnssd_get_device: Remove callback for \"%s\".", device
->dest
.name
));
3294 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3295 device
->state
= _CUPS_DNSSD_NEW
;
3304 * 'cups_dnssd_local_cb()' - Browse for local printers.
3308 cups_dnssd_local_cb(
3309 DNSServiceRef sdRef
, /* I - Service reference */
3310 DNSServiceFlags flags
, /* I - Option flags */
3311 uint32_t interfaceIndex
, /* I - Interface number */
3312 DNSServiceErrorType errorCode
, /* I - Error, if any */
3313 const char *serviceName
, /* I - Name of service/device */
3314 const char *regtype
, /* I - Type of service */
3315 const char *replyDomain
, /* I - Service domain */
3316 void *context
) /* I - Devices array */
3318 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3319 /* Enumeration data */
3320 _cups_dnssd_device_t
*device
; /* Device */
3323 DEBUG_printf(("5cups_dnssd_local_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
));
3326 * Only process "add" data...
3329 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3336 device
= cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
3339 * Hide locally-registered devices...
3342 DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.",
3347 DNSServiceRefDeallocate(device
->ref
);
3351 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3353 DEBUG_printf(("6cups_dnssd_local_cb: Remove callback for \"%s\".", device
->dest
.name
));
3354 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3357 device
->state
= _CUPS_DNSSD_LOCAL
;
3359 # endif /* HAVE_DNSSD */
3364 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
3366 * Note: This function is needed because avahi_simple_poll_iterate is broken
3367 * and always uses a timeout of 0 (!) milliseconds.
3368 * (https://github.com/lathiat/avahi/issues/127)
3373 static int /* O - Number of file descriptors matching */
3375 struct pollfd
*pollfds
, /* I - File descriptors */
3376 unsigned int num_pollfds
, /* I - Number of file descriptors */
3377 int timeout
, /* I - Timeout in milliseconds (unused) */
3378 void *context
) /* I - User data (unused) */
3380 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3381 /* Enumeration data */
3382 int val
; /* Return value */
3385 DEBUG_printf(("cups_dnssd_poll_cb(pollfds=%p, num_pollfds=%d, timeout=%d, context=%p)", pollfds
, num_pollfds
, timeout
, context
));
3389 val
= poll(pollfds
, num_pollfds
, _CUPS_DNSSD_MAXTIME
);
3391 DEBUG_printf(("cups_dnssd_poll_cb: poll() returned %d", val
));
3395 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno
)));
3404 # endif /* HAVE_AVAHI */
3408 * 'cups_dnssd_query_cb()' - Process query data.
3413 cups_dnssd_query_cb(
3414 DNSServiceRef sdRef
, /* I - Service reference */
3415 DNSServiceFlags flags
, /* I - Data flags */
3416 uint32_t interfaceIndex
, /* I - Interface */
3417 DNSServiceErrorType errorCode
, /* I - Error, if any */
3418 const char *fullName
, /* I - Full service name */
3419 uint16_t rrtype
, /* I - Record type */
3420 uint16_t rrclass
, /* I - Record class */
3421 uint16_t rdlen
, /* I - Length of record data */
3422 const void *rdata
, /* I - Record data */
3423 uint32_t ttl
, /* I - Time-to-live */
3424 void *context
) /* I - Enumeration data */
3426 # else /* HAVE_AVAHI */
3428 cups_dnssd_query_cb(
3429 AvahiRecordBrowser
*browser
, /* I - Record browser */
3430 AvahiIfIndex interfaceIndex
,
3431 /* I - Interface index (unused) */
3432 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3433 AvahiBrowserEvent event
, /* I - What happened? */
3434 const char *fullName
, /* I - Service name */
3435 uint16_t rrclass
, /* I - Record class */
3436 uint16_t rrtype
, /* I - Record type */
3437 const void *rdata
, /* I - TXT record */
3438 size_t rdlen
, /* I - Length of TXT record */
3439 AvahiLookupResultFlags flags
, /* I - Flags */
3440 void *context
) /* I - Enumeration data */
3443 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
3444 /* Client information */
3446 # endif /* HAVE_DNSSD */
3447 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3448 /* Enumeration data */
3449 char serviceName
[256],/* Service name */
3450 name
[128], /* Queue name */
3451 *ptr
; /* Pointer into string */
3452 _cups_dnssd_device_t dkey
, /* Search key */
3453 *device
; /* Device */
3457 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
));
3460 * Only process "add" data...
3463 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3466 # else /* HAVE_AVAHI */
3467 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
));
3470 * Only process "add" data...
3473 if (event
!= AVAHI_BROWSER_NEW
)
3475 if (event
== AVAHI_BROWSER_FAILURE
)
3476 DEBUG_printf(("cups_dnssd_query_cb: %s", avahi_strerror(avahi_client_errno(client
))));
3480 # endif /* HAVE_DNSSD */
3483 * Lookup the service in the devices array.
3486 cups_dnssd_unquote(serviceName
, fullName
, sizeof(serviceName
));
3488 if ((ptr
= strstr(serviceName
, "._")) != NULL
)
3491 cups_queue_name(name
, serviceName
, sizeof(name
));
3493 dkey
.dest
.name
= name
;
3495 if ((device
= cupsArrayFind(data
->devices
, &dkey
)) != NULL
&& device
->state
== _CUPS_DNSSD_NEW
)
3498 * Found it, pull out the make and model from the TXT record and save it...
3501 const uint8_t *txt
, /* Pointer into data */
3502 *txtnext
, /* Next key/value pair */
3503 *txtend
; /* End of entire TXT record */
3504 uint8_t txtlen
; /* Length of current key/value pair */
3505 char key
[256], /* Key string */
3506 value
[256], /* Value string */
3507 make_and_model
[512],
3508 /* Manufacturer and model */
3509 model
[256], /* Model */
3510 uriname
[1024], /* Name for URI */
3511 uri
[1024]; /* Printer URI */
3512 cups_ptype_t type
= CUPS_PRINTER_DISCOVERED
| CUPS_PRINTER_BW
;
3514 int saw_printer_type
= 0;
3515 /* Did we see a printer-type key? */
3517 device
->state
= _CUPS_DNSSD_PENDING
;
3518 make_and_model
[0] = '\0';
3520 strlcpy(model
, "Unknown", sizeof(model
));
3522 for (txt
= rdata
, txtend
= txt
+ rdlen
;
3527 * Read a key/value pair starting with an 8-bit length. Since the
3528 * length is 8 bits and the size of the key/value buffers is 256, we
3529 * don't need to check for overflow...
3534 if (!txtlen
|| (txt
+ txtlen
) > txtend
)
3537 txtnext
= txt
+ txtlen
;
3539 for (ptr
= key
; txt
< txtnext
&& *txt
!= '='; txt
++)
3540 *ptr
++ = (char)*txt
;
3543 if (txt
< txtnext
&& *txt
== '=')
3548 memcpy(value
, txt
, (size_t)(txtnext
- txt
));
3549 value
[txtnext
- txt
] = '\0';
3551 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key
, value
));
3555 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key
));
3559 if (!_cups_strcasecmp(key
, "usb_MFG") ||
3560 !_cups_strcasecmp(key
, "usb_MANU") ||
3561 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
3562 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
3563 else if (!_cups_strcasecmp(key
, "usb_MDL") ||
3564 !_cups_strcasecmp(key
, "usb_MODEL"))
3565 strlcpy(model
, value
, sizeof(model
));
3566 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
3568 if (value
[0] == '(')
3571 * Strip parenthesis...
3574 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
3577 strlcpy(model
, value
+ 1, sizeof(model
));
3580 strlcpy(model
, value
, sizeof(model
));
3582 else if (!_cups_strcasecmp(key
, "ty"))
3584 strlcpy(model
, value
, sizeof(model
));
3586 if ((ptr
= strchr(model
, ',')) != NULL
)
3589 else if (!_cups_strcasecmp(key
, "note"))
3590 device
->dest
.num_options
= cupsAddOption("printer-location", value
,
3591 device
->dest
.num_options
,
3592 &device
->dest
.options
);
3593 else if (!_cups_strcasecmp(key
, "pdl"))
3596 * Look for PDF-capable printers; only PDF-capable printers are shown.
3599 const char *start
, *next
; /* Pointer into value */
3600 int have_pdf
= 0, /* Have PDF? */
3601 have_raster
= 0;/* Have raster format support? */
3603 for (start
= value
; start
&& *start
; start
= next
)
3605 if (!_cups_strncasecmp(start
, "application/pdf", 15) && (!start
[15] || start
[15] == ','))
3610 else if ((!_cups_strncasecmp(start
, "image/pwg-raster", 16) && (!start
[16] || start
[16] == ',')) ||
3611 (!_cups_strncasecmp(start
, "image/urf", 9) && (!start
[9] || start
[9] == ',')))
3617 if ((next
= strchr(start
, ',')) != NULL
)
3621 if (!have_pdf
&& !have_raster
)
3622 device
->state
= _CUPS_DNSSD_INCOMPATIBLE
;
3624 else if (!_cups_strcasecmp(key
, "printer-type"))
3627 * Value is either NNNN or 0xXXXX
3630 saw_printer_type
= 1;
3631 type
= (cups_ptype_t
)strtol(value
, NULL
, 0) | CUPS_PRINTER_DISCOVERED
;
3633 else if (!saw_printer_type
)
3635 if (!_cups_strcasecmp(key
, "air") &&
3636 !_cups_strcasecmp(value
, "t"))
3637 type
|= CUPS_PRINTER_AUTHENTICATED
;
3638 else if (!_cups_strcasecmp(key
, "bind") &&
3639 !_cups_strcasecmp(value
, "t"))
3640 type
|= CUPS_PRINTER_BIND
;
3641 else if (!_cups_strcasecmp(key
, "collate") &&
3642 !_cups_strcasecmp(value
, "t"))
3643 type
|= CUPS_PRINTER_COLLATE
;
3644 else if (!_cups_strcasecmp(key
, "color") &&
3645 !_cups_strcasecmp(value
, "t"))
3646 type
|= CUPS_PRINTER_COLOR
;
3647 else if (!_cups_strcasecmp(key
, "copies") &&
3648 !_cups_strcasecmp(value
, "t"))
3649 type
|= CUPS_PRINTER_COPIES
;
3650 else if (!_cups_strcasecmp(key
, "duplex") &&
3651 !_cups_strcasecmp(value
, "t"))
3652 type
|= CUPS_PRINTER_DUPLEX
;
3653 else if (!_cups_strcasecmp(key
, "fax") &&
3654 !_cups_strcasecmp(value
, "t"))
3655 type
|= CUPS_PRINTER_MFP
;
3656 else if (!_cups_strcasecmp(key
, "papercustom") &&
3657 !_cups_strcasecmp(value
, "t"))
3658 type
|= CUPS_PRINTER_VARIABLE
;
3659 else if (!_cups_strcasecmp(key
, "papermax"))
3661 if (!_cups_strcasecmp(value
, "legal-a4"))
3662 type
|= CUPS_PRINTER_SMALL
;
3663 else if (!_cups_strcasecmp(value
, "isoc-a2"))
3664 type
|= CUPS_PRINTER_MEDIUM
;
3665 else if (!_cups_strcasecmp(value
, ">isoc-a2"))
3666 type
|= CUPS_PRINTER_LARGE
;
3668 else if (!_cups_strcasecmp(key
, "punch") &&
3669 !_cups_strcasecmp(value
, "t"))
3670 type
|= CUPS_PRINTER_PUNCH
;
3671 else if (!_cups_strcasecmp(key
, "scan") &&
3672 !_cups_strcasecmp(value
, "t"))
3673 type
|= CUPS_PRINTER_MFP
;
3674 else if (!_cups_strcasecmp(key
, "sort") &&
3675 !_cups_strcasecmp(value
, "t"))
3676 type
|= CUPS_PRINTER_SORT
;
3677 else if (!_cups_strcasecmp(key
, "staple") &&
3678 !_cups_strcasecmp(value
, "t"))
3679 type
|= CUPS_PRINTER_STAPLE
;
3684 * Save the printer-xxx values...
3687 if (make_and_model
[0])
3689 strlcat(make_and_model
, " ", sizeof(make_and_model
));
3690 strlcat(make_and_model
, model
, sizeof(make_and_model
));
3692 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", make_and_model
, device
->dest
.num_options
, &device
->dest
.options
);
3695 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", model
, device
->dest
.num_options
, &device
->dest
.options
);
3697 device
->type
= type
;
3698 snprintf(value
, sizeof(value
), "%u", type
);
3699 device
->dest
.num_options
= cupsAddOption("printer-type", value
, device
->dest
.num_options
, &device
->dest
.options
);
3705 cups_dnssd_unquote(uriname
, device
->fullName
, sizeof(uriname
));
3706 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
3707 !strcmp(device
->regtype
, "_ipps._tcp") ? "ipps" : "ipp",
3708 NULL
, uriname
, 0, saw_printer_type
? "/cups" : "/");
3710 DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri
));
3712 device
->dest
.num_options
= cupsAddOption("device-uri", uri
, device
->dest
.num_options
, &device
->dest
.options
);
3715 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3721 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3724 static const char * /* O - Resolved URI or NULL */
3726 cups_dest_t
*dest
, /* I - Destination */
3727 const char *uri
, /* I - Current printer URI */
3728 int msec
, /* I - Time in milliseconds */
3729 int *cancel
, /* I - Pointer to "cancel" variable */
3730 cups_dest_cb_t cb
, /* I - Callback */
3731 void *user_data
) /* I - User data for callback */
3733 char tempuri
[1024]; /* Temporary URI buffer */
3734 _cups_dnssd_resolve_t resolve
; /* Resolve data */
3738 * Resolve the URI...
3741 resolve
.cancel
= cancel
;
3742 gettimeofday(&resolve
.end_time
, NULL
);
3745 resolve
.end_time
.tv_sec
+= msec
/ 1000;
3746 resolve
.end_time
.tv_usec
+= (msec
% 1000) * 1000;
3748 while (resolve
.end_time
.tv_usec
>= 1000000)
3750 resolve
.end_time
.tv_sec
++;
3751 resolve
.end_time
.tv_usec
-= 1000000;
3755 resolve
.end_time
.tv_sec
+= 75;
3758 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
3760 if ((uri
= _httpResolveURI(uri
, tempuri
, sizeof(tempuri
), _HTTP_RESOLVE_DEFAULT
, cups_dnssd_resolve_cb
, &resolve
)) == NULL
)
3762 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to resolve printer-uri."), 1);
3765 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
3771 * Save the resolved URI...
3774 dest
->num_options
= cupsAddOption("resolved-device-uri", uri
, dest
->num_options
, &dest
->options
);
3776 return (cupsGetOption("resolved-device-uri", dest
->num_options
, dest
->options
));
3781 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3784 static int /* O - 1 to continue, 0 to stop */
3785 cups_dnssd_resolve_cb(void *context
) /* I - Resolve data */
3787 _cups_dnssd_resolve_t
*resolve
= (_cups_dnssd_resolve_t
*)context
;
3789 struct timeval curtime
; /* Current time */
3793 * If the cancel variable is set, return immediately.
3796 if (resolve
->cancel
&& *(resolve
->cancel
))
3798 DEBUG_puts("4cups_dnssd_resolve_cb: Canceled.");
3803 * Otherwise check the end time...
3806 gettimeofday(&curtime
, NULL
);
3808 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
));
3810 return (curtime
.tv_sec
< resolve
->end_time
.tv_sec
||
3811 (curtime
.tv_sec
== resolve
->end_time
.tv_sec
&&
3812 curtime
.tv_usec
< resolve
->end_time
.tv_usec
));
3817 * 'cups_dnssd_unquote()' - Unquote a name string.
3821 cups_dnssd_unquote(char *dst
, /* I - Destination buffer */
3822 const char *src
, /* I - Source string */
3823 size_t dstsize
) /* I - Size of destination buffer */
3825 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
3828 while (*src
&& dst
< dstend
)
3833 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
3834 isdigit(src
[2] & 255))
3836 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';
3848 #endif /* HAVE_DNSSD */
3851 #if defined(HAVE_AVAHI) || defined(HAVE_DNSSD)
3853 * 'cups_elapsed()' - Return the elapsed time in milliseconds.
3856 static int /* O - Elapsed time in milliseconds */
3857 cups_elapsed(struct timeval
*t
) /* IO - Previous time */
3859 int msecs
; /* Milliseconds */
3860 struct timeval nt
; /* New time */
3863 gettimeofday(&nt
, NULL
);
3865 msecs
= (int)(1000 * (nt
.tv_sec
- t
->tv_sec
) + (nt
.tv_usec
- t
->tv_usec
) / 1000);
3871 #endif /* HAVE_AVAHI || HAVE_DNSSD */
3875 * 'cups_find_dest()' - Find a destination using a binary search.
3878 static int /* O - Index of match */
3879 cups_find_dest(const char *name
, /* I - Destination name */
3880 const char *instance
, /* I - Instance or NULL */
3881 int num_dests
, /* I - Number of destinations */
3882 cups_dest_t
*dests
, /* I - Destinations */
3883 int prev
, /* I - Previous index */
3884 int *rdiff
) /* O - Difference of match */
3886 int left
, /* Low mark for binary search */
3887 right
, /* High mark for binary search */
3888 current
, /* Current index */
3889 diff
; /* Result of comparison */
3890 cups_dest_t key
; /* Search key */
3893 key
.name
= (char *)name
;
3894 key
.instance
= (char *)instance
;
3899 * Start search on either side of previous...
3902 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
3903 (diff
< 0 && prev
== 0) ||
3904 (diff
> 0 && prev
== (num_dests
- 1)))
3912 * Start with previous on right side...
3921 * Start wih previous on left side...
3925 right
= num_dests
- 1;
3931 * Start search in the middle...
3935 right
= num_dests
- 1;
3940 current
= (left
+ right
) / 2;
3941 diff
= cups_compare_dests(&key
, dests
+ current
);
3950 while ((right
- left
) > 1);
3955 * Check the last 1 or 2 elements...
3958 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
3962 diff
= cups_compare_dests(&key
, dests
+ right
);
3968 * Return the closest destination and the difference...
3978 * 'cups_get_cb()' - Collect enumerated destinations.
3981 static int /* O - 1 to continue, 0 to stop */
3982 cups_get_cb(_cups_getdata_t
*data
, /* I - Data from cupsGetDests */
3983 unsigned flags
, /* I - Enumeration flags */
3984 cups_dest_t
*dest
) /* I - Destination */
3986 if (flags
& CUPS_DEST_FLAGS_REMOVED
)
3989 * Remove destination from array...
3992 data
->num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
->num_dests
, &data
->dests
);
3997 * Add destination to array...
4000 data
->num_dests
= cupsCopyDest(dest
, data
->num_dests
, &data
->dests
);
4008 * 'cups_get_default()' - Get the default destination from an lpoptions file.
4011 static char * /* O - Default destination or NULL */
4012 cups_get_default(const char *filename
, /* I - File to read */
4013 char *namebuf
, /* I - Name buffer */
4014 size_t namesize
, /* I - Size of name buffer */
4015 const char **instance
) /* I - Instance */
4017 cups_file_t
*fp
; /* lpoptions file */
4018 char line
[8192], /* Line from file */
4019 *value
, /* Value for line */
4020 *nameptr
; /* Pointer into name */
4021 int linenum
; /* Current line */
4026 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
4030 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4032 if (!_cups_strcasecmp(line
, "default") && value
)
4034 strlcpy(namebuf
, value
, namesize
);
4036 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
4038 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
4041 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
4044 *instance
= nameptr
;
4052 return (*namebuf
? namebuf
: NULL
);
4057 * 'cups_get_dests()' - Get destinations from a file.
4060 static int /* O - Number of destinations */
4062 const char *filename
, /* I - File to read from */
4063 const char *match_name
, /* I - Destination name we want */
4064 const char *match_inst
, /* I - Instance name we want */
4065 int user_default_set
, /* I - User default printer set? */
4066 int num_dests
, /* I - Number of destinations */
4067 cups_dest_t
**dests
) /* IO - Destinations */
4069 int i
; /* Looping var */
4070 cups_dest_t
*dest
; /* Current destination */
4071 cups_file_t
*fp
; /* File pointer */
4072 char line
[8192], /* Line from file */
4073 *lineptr
, /* Pointer into line */
4074 *name
, /* Name of destination/option */
4075 *instance
; /* Instance of destination */
4076 int linenum
; /* Current line number */
4079 DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", match_inst=\"%s\", user_default_set=%d, num_dests=%d, dests=%p)", filename
, match_name
, match_inst
, user_default_set
, num_dests
, (void *)dests
));
4082 * Try to open the file...
4085 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4089 * Read each printer; each line looks like:
4091 * Dest name[/instance] options
4092 * Default name[/instance] options
4097 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
4100 * See what type of line it is...
4103 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
4104 linenum
, line
, lineptr
));
4106 if ((_cups_strcasecmp(line
, "dest") && _cups_strcasecmp(line
, "default")) || !lineptr
)
4108 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
4115 * Search for an instance...
4118 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
4121 if (*lineptr
== '/')
4124 * Found an instance...
4131 * Search for an instance...
4134 while (!isspace(*lineptr
& 255) && *lineptr
)
4143 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
4147 * See if the primary instance of the destination exists; if not,
4148 * ignore this entry and move on...
4153 if (_cups_strcasecmp(name
, match_name
) ||
4154 (!instance
&& match_inst
) ||
4155 (instance
&& !match_inst
) ||
4156 (instance
&& _cups_strcasecmp(instance
, match_inst
)))
4161 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
4163 DEBUG_puts("9cups_get_dests: Not found!");
4169 * Add the destination...
4172 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
4174 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
4180 DEBUG_puts("9cups_get_dests: Out of memory!");
4186 * Add options until we hit the end of the line...
4189 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
4193 * If we found what we were looking for, stop now...
4200 * Set this as default if needed...
4203 if (!user_default_set
&& !_cups_strcasecmp(line
, "default"))
4205 DEBUG_puts("9cups_get_dests: Setting as default...");
4207 for (i
= 0; i
< num_dests
; i
++)
4208 (*dests
)[i
].is_default
= 0;
4210 dest
->is_default
= 1;
4215 * Close the file and return...
4225 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
4229 static char * /* O - New string */
4231 ipp_attribute_t
*attr
, /* I - Attribute to convert */
4232 char *buffer
, /* I - Buffer */
4233 size_t bufsize
) /* I - Size of buffer */
4235 int i
; /* Looping var */
4236 char *ptr
, /* Pointer into buffer */
4237 *end
, /* Pointer to end of buffer */
4238 *valptr
; /* Pointer into string attribute */
4242 * Return quickly if we have a single string value...
4245 if (attr
->num_values
== 1 &&
4246 attr
->value_tag
!= IPP_TAG_INTEGER
&&
4247 attr
->value_tag
!= IPP_TAG_ENUM
&&
4248 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
4249 attr
->value_tag
!= IPP_TAG_RANGE
)
4250 return (attr
->values
[0].string
.text
);
4253 * Copy the values to the string, separating with commas and escaping strings
4257 end
= buffer
+ bufsize
- 1;
4259 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
4264 switch (attr
->value_tag
)
4266 case IPP_TAG_INTEGER
:
4268 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].integer
);
4271 case IPP_TAG_BOOLEAN
:
4272 if (attr
->values
[i
].boolean
)
4273 strlcpy(ptr
, "true", (size_t)(end
- ptr
+ 1));
4275 strlcpy(ptr
, "false", (size_t)(end
- ptr
+ 1));
4278 case IPP_TAG_RANGE
:
4279 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
4280 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].range
.lower
);
4282 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d-%d", attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4286 for (valptr
= attr
->values
[i
].string
.text
;
4287 *valptr
&& ptr
< end
;)
4289 if (strchr(" \t\n\\\'\"", *valptr
))
4291 if (ptr
>= (end
- 1))
4314 * 'cups_name_cb()' - Find an enumerated destination.
4317 static int /* O - 1 to continue, 0 to stop */
4318 cups_name_cb(_cups_namedata_t
*data
, /* I - Data from cupsGetNamedDest */
4319 unsigned flags
, /* I - Enumeration flags */
4320 cups_dest_t
*dest
) /* I - Destination */
4322 DEBUG_printf(("2cups_name_cb(data=%p(%s), flags=%x, dest=%p(%s)", (void *)data
, data
->name
, flags
, (void *)dest
, dest
->name
));
4324 if (!(flags
& CUPS_DEST_FLAGS_REMOVED
) && !dest
->instance
&& !strcasecmp(data
->name
, dest
->name
))
4327 * Copy destination and stop enumeration...
4330 cupsCopyDest(dest
, 0, &data
->dest
);
4339 * 'cups_queue_name()' - Create a local queue name based on the service name.
4344 char *name
, /* I - Name buffer */
4345 const char *serviceName
, /* I - Service name */
4346 size_t namesize
) /* I - Size of name buffer */
4348 const char *ptr
; /* Pointer into serviceName */
4349 char *nameptr
; /* Pointer into name */
4352 for (nameptr
= name
, ptr
= serviceName
; *ptr
&& nameptr
< (name
+ namesize
- 1); ptr
++)
4355 * Sanitize the printer name...
4358 if (_cups_isalnum(*ptr
))
4360 else if (nameptr
== name
|| nameptr
[-1] != '_')