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 * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
583 * for the "flags" argument to connect directly to the device associated with
584 * the destination. Otherwise, the connection is made to the CUPS scheduler
585 * associated with the destination.
587 * @since CUPS 1.6/macOS 10.8@
590 http_t
* /* O - Connection to destination or @code NULL@ */
592 cups_dest_t
*dest
, /* I - Destination */
593 unsigned flags
, /* I - Connection flags */
594 int msec
, /* I - Timeout in milliseconds */
595 int *cancel
, /* I - Pointer to "cancel" variable */
596 char *resource
, /* I - Resource buffer */
597 size_t resourcesize
, /* I - Size of resource buffer */
598 cups_dest_cb_t cb
, /* I - Callback function */
599 void *user_data
) /* I - User data pointer */
601 const char *uri
; /* Printer URI */
602 char scheme
[32], /* URI scheme */
603 userpass
[256], /* Username and password (unused) */
604 hostname
[256], /* Hostname */
605 tempresource
[1024]; /* Temporary resource buffer */
606 int port
; /* Port number */
607 char portstr
[16]; /* Port number string */
608 http_encryption_t encryption
; /* Encryption to use */
609 http_addrlist_t
*addrlist
; /* Address list for server */
610 http_t
*http
; /* Connection to server */
613 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
));
616 * Range check input...
624 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
628 if (!resource
|| resourcesize
< 1)
630 resource
= tempresource
;
631 resourcesize
= sizeof(tempresource
);
635 * Grab the printer URI...
638 if (flags
& CUPS_DEST_FLAGS_DEVICE
)
640 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
642 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
643 if (strstr(uri
, "._tcp"))
644 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
645 #endif /* HAVE_DNSSD || HAVE_AVAHI */
648 else if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
650 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
652 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
653 if (strstr(uri
, "._tcp"))
654 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
655 #endif /* HAVE_DNSSD || HAVE_AVAHI */
659 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, tempresource
, sizeof(tempresource
));
663 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
665 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
671 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
674 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
679 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
680 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
681 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
683 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
686 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
693 * Lookup the address for the server...
697 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
699 snprintf(portstr
, sizeof(portstr
), "%d", port
);
701 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portstr
)) == NULL
)
704 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
709 if (cancel
&& *cancel
)
711 httpAddrFreeList(addrlist
);
714 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CANCELED
, dest
);
720 * Create the HTTP object pointing to the server referenced by the URI...
723 if (!strcmp(scheme
, "ipps") || port
== 443)
724 encryption
= HTTP_ENCRYPTION_ALWAYS
;
726 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
728 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, encryption
, 1, 0, NULL
);
729 httpAddrFreeList(addrlist
);
732 * Connect if requested...
735 if (flags
& CUPS_DEST_FLAGS_UNCONNECTED
)
738 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
, dest
);
743 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
745 if (!httpReconnect2(http
, msec
, cancel
) && cb
)
747 if (cancel
&& *cancel
)
748 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
750 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
753 (*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
);
762 * 'cupsConnectDestBlock()' - Open a connection to the destination.
764 * Connect to the destination, returning a new @code http_t@ connection object
765 * and optionally the resource path to use for the destination. These calls
766 * will block until a connection is made, the timeout expires, the integer
767 * pointed to by "cancel" is non-zero, or the block returns 0. The caller is
768 * responsible for calling @link httpClose@ on the returned connection.
770 * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
771 * for the "flags" argument to connect directly to the device associated with
772 * the destination. Otherwise, the connection is made to the CUPS scheduler
773 * associated with the destination.
775 * @since CUPS 1.6/macOS 10.8@ @exclude all@
778 http_t
* /* O - Connection to destination or @code NULL@ */
779 cupsConnectDestBlock(
780 cups_dest_t
*dest
, /* I - Destination */
781 unsigned flags
, /* I - Connection flags */
782 int msec
, /* I - Timeout in milliseconds */
783 int *cancel
, /* I - Pointer to "cancel" variable */
784 char *resource
, /* I - Resource buffer */
785 size_t resourcesize
, /* I - Size of resource buffer */
786 cups_dest_block_t block
) /* I - Callback block */
788 return (cupsConnectDest(dest
, flags
, msec
, cancel
, resource
, resourcesize
,
789 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
791 #endif /* __BLOCKS__ */
795 * 'cupsCopyDest()' - Copy a destination.
797 * Make a copy of the destination to an array of destinations (or just a single
798 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
799 * for calling cupsFreeDests() on the returned object(s).
801 * @since CUPS 1.6/macOS 10.8@
804 int /* O - New number of destinations */
805 cupsCopyDest(cups_dest_t
*dest
, /* I - Destination to copy */
806 int num_dests
, /* I - Number of destinations */
807 cups_dest_t
**dests
) /* IO - Destination array */
809 int i
; /* Looping var */
810 cups_dest_t
*new_dest
; /* New destination pointer */
811 cups_option_t
*new_option
, /* Current destination option */
812 *option
; /* Current parent option */
816 * Range check input...
819 if (!dest
|| num_dests
< 0 || !dests
)
823 * See if the destination already exists...
826 if ((new_dest
= cupsGetDest(dest
->name
, dest
->instance
, num_dests
,
830 * Protect against copying destination to itself...
833 if (new_dest
== dest
)
837 * Otherwise, free the options...
840 cupsFreeOptions(new_dest
->num_options
, new_dest
->options
);
842 new_dest
->num_options
= 0;
843 new_dest
->options
= NULL
;
846 new_dest
= cups_add_dest(dest
->name
, dest
->instance
, &num_dests
, dests
);
850 if ((new_dest
->options
= calloc(sizeof(cups_option_t
), (size_t)dest
->num_options
)) == NULL
)
851 return (cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
, dests
));
853 new_dest
->num_options
= dest
->num_options
;
855 for (i
= dest
->num_options
, option
= dest
->options
,
856 new_option
= new_dest
->options
;
858 i
--, option
++, new_option
++)
860 new_option
->name
= _cupsStrRetain(option
->name
);
861 new_option
->value
= _cupsStrRetain(option
->value
);
870 * '_cupsCreateDest()' - Create a local (temporary) queue.
873 char * /* O - Printer URI or @code NULL@ on error */
874 _cupsCreateDest(const char *name
, /* I - Printer name */
875 const char *info
, /* I - Printer description of @code NULL@ */
876 const char *device_id
, /* I - 1284 Device ID or @code NULL@ */
877 const char *device_uri
, /* I - Device URI */
878 char *uri
, /* I - Printer URI buffer */
879 size_t urisize
) /* I - Size of URI buffer */
881 http_t
*http
; /* Connection to server */
882 ipp_t
*request
, /* CUPS-Create-Local-Printer request */
883 *response
; /* CUPS-Create-Local-Printer response */
884 ipp_attribute_t
*attr
; /* printer-uri-supported attribute */
885 ipp_pstate_t state
= IPP_PSTATE_STOPPED
;
886 /* printer-state value */
889 if (!name
|| !device_uri
|| !uri
|| urisize
< 32)
892 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
)) == NULL
)
895 request
= ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER
);
897 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, "ipp://localhost/");
898 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
900 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
, device_uri
);
901 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
, name
);
903 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
, info
);
905 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
907 response
= cupsDoRequest(http
, request
, "/");
909 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
910 strlcpy(uri
, ippGetString(attr
, 0, NULL
), urisize
);
918 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
919 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
921 while (state
== IPP_PSTATE_STOPPED
&& cupsLastError() == IPP_STATUS_OK
)
926 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
928 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
929 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
930 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "printer-state");
932 response
= cupsDoRequest(http
, request
, "/");
934 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
935 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
947 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
949 * Destinations are enumerated from one or more sources. The callback function
950 * receives the @code user_data@ pointer and the destination pointer which can
951 * be used as input to the @link cupsCopyDest@ function. The function must
952 * return 1 to continue enumeration or 0 to stop.
954 * The @code type@ and @code mask@ arguments allow the caller to filter the
955 * destinations that are enumerated. Passing 0 for both will enumerate all
956 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
957 * destinations that are available but have not yet been added locally.
959 * Enumeration happens on the current thread and does not return until all
960 * destinations have been enumerated or the callback function returns 0.
962 * Note: The callback function will likely receive multiple updates for the same
963 * destinations - it is up to the caller to suppress any duplicate destinations.
965 * @since CUPS 1.6/macOS 10.8@
968 int /* O - 1 on success, 0 on failure */
970 unsigned flags
, /* I - Enumeration flags */
971 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
972 int *cancel
, /* I - Pointer to "cancel" variable */
973 cups_ptype_t type
, /* I - Printer type bits */
974 cups_ptype_t mask
, /* I - Mask for printer type bits */
975 cups_dest_cb_t cb
, /* I - Callback function */
976 void *user_data
) /* I - User data */
978 int i
, /* Looping var */
979 num_dests
; /* Number of destinations */
980 cups_dest_t
*dests
= NULL
, /* Destinations */
981 *dest
; /* Current destination */
982 const char *defprinter
; /* Default printer */
983 char name
[1024], /* Copy of printer name */
984 *instance
, /* Pointer to instance name */
985 *user_default
; /* User default printer */
986 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
987 int count
, /* Number of queries started */
988 completed
, /* Number of completed queries */
989 remaining
; /* Remainder of timeout */
990 struct timeval curtime
; /* Current time */
991 _cups_dnssd_data_t data
; /* Data for callback */
992 _cups_dnssd_device_t
*device
; /* Current device */
994 int nfds
, /* Number of files responded */
995 main_fd
; /* File descriptor for lookups */
996 DNSServiceRef ipp_ref
= NULL
, /* IPP browser */
997 local_ipp_ref
= NULL
; /* Local IPP browser */
999 DNSServiceRef ipps_ref
= NULL
,/* IPPS browser */
1000 local_ipps_ref
= NULL
; /* Local IPPS browser */
1001 # endif /* HAVE_SSL */
1003 struct pollfd pfd
; /* Polling data */
1005 fd_set input
; /* Input set for select() */
1006 struct timeval timeout
; /* Timeout for select() */
1007 # endif /* HAVE_POLL */
1008 # else /* HAVE_AVAHI */
1009 int error
; /* Error value */
1010 AvahiServiceBrowser
*ipp_ref
= NULL
;/* IPP browser */
1012 AvahiServiceBrowser
*ipps_ref
= NULL
; /* IPPS browser */
1013 # endif /* HAVE_SSL */
1014 # endif /* HAVE_DNSSD */
1015 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1019 * Range check input...
1028 * Get ready to enumerate...
1031 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1032 memset(&data
, 0, sizeof(data
));
1037 data
.user_data
= user_data
;
1038 data
.devices
= cupsArrayNew3((cups_array_func_t
)cups_dnssd_compare_devices
, NULL
, NULL
, 0, NULL
, (cups_afree_func_t
)cups_dnssd_free_device
);
1039 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1041 if (!(mask
& CUPS_PRINTER_DISCOVERED
) || !(type
& CUPS_PRINTER_DISCOVERED
))
1044 * Get the list of local printers and pass them to the callback function...
1047 num_dests
= _cupsGetDests(CUPS_HTTP_DEFAULT
, IPP_OP_CUPS_GET_PRINTERS
, NULL
,
1048 &dests
, type
, mask
);
1050 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
1052 else if ((defprinter
= cupsGetDefault2(CUPS_HTTP_DEFAULT
)) != NULL
)
1054 strlcpy(name
, defprinter
, sizeof(name
));
1061 * Separate printer and instance name...
1064 if ((instance
= strchr(name
, '/')) != NULL
)
1068 * Lookup the printer and instance and make it the default...
1071 if ((dest
= cupsGetDest(name
, instance
, num_dests
, dests
)) != NULL
)
1072 dest
->is_default
= 1;
1075 for (i
= num_dests
, dest
= dests
;
1076 i
> 0 && (!cancel
|| !*cancel
);
1079 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1080 const char *device_uri
; /* Device URI */
1081 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1083 if (!(*cb
)(user_data
, i
> 1 ? CUPS_DEST_FLAGS_MORE
: CUPS_DEST_FLAGS_NONE
,
1087 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1088 if (!dest
->instance
&& (device_uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
&& !strncmp(device_uri
, "dnssd://", 8))
1091 * Add existing queue using service name, etc. so we don't list it again...
1094 char scheme
[32], /* URI scheme */
1095 userpass
[32], /* Username:password */
1096 serviceName
[256], /* Service name (host field) */
1097 resource
[256], /* Resource (options) */
1098 *regtype
, /* Registration type */
1099 *replyDomain
; /* Registration domain */
1100 int port
; /* Port number (not used) */
1102 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
)
1104 if ((regtype
= strstr(serviceName
, "._ipp")) != NULL
)
1108 if ((replyDomain
= strstr(regtype
, "._tcp.")) != NULL
)
1110 replyDomain
[5] = '\0';
1113 if ((device
= cups_dnssd_get_device(&data
, serviceName
, regtype
, replyDomain
)) != NULL
)
1114 device
->state
= _CUPS_DNSSD_ACTIVE
;
1119 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1122 cupsFreeDests(num_dests
, dests
);
1124 if (i
> 0 || msec
== 0)
1129 * Return early if the caller doesn't want to do discovery...
1132 if ((mask
& CUPS_PRINTER_DISCOVERED
) && !(type
& CUPS_PRINTER_DISCOVERED
))
1135 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1137 * Get Bonjour-shared printers...
1140 gettimeofday(&curtime
, NULL
);
1143 if (DNSServiceCreateConnection(&data
.main_ref
) != kDNSServiceErr_NoError
)
1146 main_fd
= DNSServiceRefSockFD(data
.main_ref
);
1148 ipp_ref
= data
.main_ref
;
1149 DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0,
1151 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
1153 local_ipp_ref
= data
.main_ref
;
1154 DNSServiceBrowse(&local_ipp_ref
, kDNSServiceFlagsShareConnection
,
1155 kDNSServiceInterfaceIndexLocalOnly
,
1157 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
1160 ipps_ref
= data
.main_ref
;
1161 DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0,
1163 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
1165 local_ipps_ref
= data
.main_ref
;
1166 DNSServiceBrowse(&local_ipps_ref
, kDNSServiceFlagsShareConnection
,
1167 kDNSServiceInterfaceIndexLocalOnly
,
1169 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
1170 # endif /* HAVE_SSL */
1172 # else /* HAVE_AVAHI */
1173 if ((data
.simple_poll
= avahi_simple_poll_new()) == NULL
)
1175 DEBUG_puts("cupsEnumDests: Unable to create Avahi simple poll object.");
1179 avahi_simple_poll_set_func(data
.simple_poll
, cups_dnssd_poll_cb
, &data
);
1181 data
.client
= avahi_client_new(avahi_simple_poll_get(data
.simple_poll
),
1182 0, cups_dnssd_client_cb
, &data
,
1186 DEBUG_puts("cupsEnumDests: Unable to create Avahi client.");
1187 avahi_simple_poll_free(data
.simple_poll
);
1192 ipp_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipp._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
);
1196 ipps_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipps._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
);
1197 # endif /* HAVE_SSL */
1198 # endif /* HAVE_DNSSD */
1201 remaining
= INT_MAX
;
1205 while (remaining
> 0 && (!cancel
|| !*cancel
))
1208 * Check for input...
1211 DEBUG_printf(("1cupsEnumDests: remaining=%d", remaining
));
1213 cups_elapsed(&curtime
);
1218 pfd
.events
= POLLIN
;
1220 nfds
= poll(&pfd
, 1, remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
1224 FD_SET(main_fd
, &input
);
1227 timeout
.tv_usec
= 1000 * (remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
1229 nfds
= select(main_fd
+ 1, &input
, NULL
, NULL
, &timeout
);
1230 # endif /* HAVE_POLL */
1233 DNSServiceProcessResult(data
.main_ref
);
1235 # else /* HAVE_AVAHI */
1238 if ((error
= avahi_simple_poll_iterate(data
.simple_poll
, _CUPS_DNSSD_MAXTIME
)) > 0)
1241 * We've been told to exit the loop. Perhaps the connection to
1248 DEBUG_printf(("1cupsEnumDests: got_data=%d", data
.got_data
));
1249 # endif /* HAVE_DNSSD */
1251 remaining
-= cups_elapsed(&curtime
);
1253 for (device
= (_cups_dnssd_device_t
*)cupsArrayFirst(data
.devices
),
1254 count
= 0, completed
= 0;
1256 device
= (_cups_dnssd_device_t
*)cupsArrayNext(data
.devices
))
1261 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
1264 if (!device
->ref
&& device
->state
== _CUPS_DNSSD_NEW
)
1266 DEBUG_printf(("1cupsEnumDests: Querying '%s'.", device
->fullName
));
1269 device
->ref
= data
.main_ref
;
1271 if (DNSServiceQueryRecord(&(device
->ref
),
1272 kDNSServiceFlagsShareConnection
,
1273 0, device
->fullName
,
1274 kDNSServiceType_TXT
,
1275 kDNSServiceClass_IN
,
1276 (DNSServiceQueryRecordReply
)cups_dnssd_query_cb
,
1277 &data
) == kDNSServiceErr_NoError
)
1284 device
->state
= _CUPS_DNSSD_ERROR
;
1286 DEBUG_puts("1cupsEnumDests: Query failed.");
1289 # else /* HAVE_AVAHI */
1290 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
)
1292 DEBUG_printf(("1cupsEnumDests: Query ref=%p", device
->ref
));
1297 device
->state
= _CUPS_DNSSD_ERROR
;
1299 DEBUG_printf(("1cupsEnumDests: Query failed: %s", avahi_strerror(avahi_client_errno(data
.client
))));
1301 # endif /* HAVE_DNSSD */
1303 else if (device
->ref
&& device
->state
== _CUPS_DNSSD_PENDING
)
1307 DEBUG_printf(("1cupsEnumDests: Query for \"%s\" is complete.", device
->fullName
));
1309 if ((device
->type
& mask
) == type
)
1311 DEBUG_printf(("1cupsEnumDests: Add callback for \"%s\".", device
->dest
.name
));
1312 if (!(*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, &device
->dest
))
1319 device
->state
= _CUPS_DNSSD_ACTIVE
;
1324 DEBUG_printf(("1cupsEnumDests: remaining=%d, browsers=%d, completed=%d, count=%d, devices count=%d", remaining
, data
.browsers
, completed
, count
, cupsArrayCount(data
.devices
)));
1326 if (data
.browsers
== 0 && completed
== cupsArrayCount(data
.devices
))
1329 DEBUG_printf(("1cupsEnumDests: remaining=%d, completed=%d, count=%d, devices count=%d", remaining
, completed
, count
, cupsArrayCount(data
.devices
)));
1331 if (completed
== cupsArrayCount(data
.devices
))
1333 # endif /* HAVE_AVAHI */
1335 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1343 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1344 cupsArrayDelete(data
.devices
);
1348 DNSServiceRefDeallocate(ipp_ref
);
1350 DNSServiceRefDeallocate(local_ipp_ref
);
1354 DNSServiceRefDeallocate(ipps_ref
);
1356 DNSServiceRefDeallocate(local_ipps_ref
);
1357 # endif /* HAVE_SSL */
1360 DNSServiceRefDeallocate(data
.main_ref
);
1362 # else /* HAVE_AVAHI */
1364 avahi_service_browser_free(ipp_ref
);
1367 avahi_service_browser_free(ipps_ref
);
1368 # endif /* HAVE_SSL */
1371 avahi_client_free(data
.client
);
1372 if (data
.simple_poll
)
1373 avahi_simple_poll_free(data
.simple_poll
);
1374 # endif /* HAVE_DNSSD */
1375 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1383 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
1385 * Destinations are enumerated from one or more sources. The block receives the
1386 * @code user_data@ pointer and the destination pointer which can be used as
1387 * input to the @link cupsCopyDest@ function. The block must return 1 to
1388 * continue enumeration or 0 to stop.
1390 * The @code type@ and @code mask@ arguments allow the caller to filter the
1391 * destinations that are enumerated. Passing 0 for both will enumerate all
1392 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
1393 * destinations that are available but have not yet been added locally.
1395 * Enumeration happens on the current thread and does not return until all
1396 * destinations have been enumerated or the block returns 0.
1398 * Note: The block will likely receive multiple updates for the same
1399 * destinations - it is up to the caller to suppress any duplicate destinations.
1401 * @since CUPS 1.6/macOS 10.8@ @exclude all@
1404 int /* O - 1 on success, 0 on failure */
1406 unsigned flags
, /* I - Enumeration flags */
1407 int timeout
, /* I - Timeout in milliseconds, 0 for indefinite */
1408 int *cancel
, /* I - Pointer to "cancel" variable */
1409 cups_ptype_t type
, /* I - Printer type bits */
1410 cups_ptype_t mask
, /* I - Mask for printer type bits */
1411 cups_dest_block_t block
) /* I - Block */
1413 return (cupsEnumDests(flags
, timeout
, cancel
, type
, mask
,
1414 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
1416 # endif /* __BLOCKS__ */
1420 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1424 cupsFreeDests(int num_dests
, /* I - Number of destinations */
1425 cups_dest_t
*dests
) /* I - Destinations */
1427 int i
; /* Looping var */
1428 cups_dest_t
*dest
; /* Current destination */
1431 if (num_dests
== 0 || dests
== NULL
)
1434 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1436 _cupsStrFree(dest
->name
);
1437 _cupsStrFree(dest
->instance
);
1439 cupsFreeOptions(dest
->num_options
, dest
->options
);
1447 * 'cupsGetDest()' - Get the named destination from the list.
1449 * Use the @link cupsEnumDests@ or @link cupsGetDests2@ functions to get a
1450 * list of supported destinations for the current user.
1453 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
1454 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1455 const char *instance
, /* I - Instance name or @code NULL@ */
1456 int num_dests
, /* I - Number of destinations */
1457 cups_dest_t
*dests
) /* I - Destinations */
1459 int diff
, /* Result of comparison */
1460 match
; /* Matching index */
1463 if (num_dests
<= 0 || !dests
)
1469 * NULL name for default printer.
1472 while (num_dests
> 0)
1474 if (dests
->is_default
)
1484 * Lookup name and optionally the instance...
1487 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
1490 return (dests
+ match
);
1498 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1501 const char * /* O - Printer URI */
1502 _cupsGetDestResource(
1503 cups_dest_t
*dest
, /* I - Destination */
1504 char *resource
, /* I - Resource buffer */
1505 size_t resourcesize
) /* I - Size of resource buffer */
1507 const char *uri
; /* Printer URI */
1508 char scheme
[32], /* URI scheme */
1509 userpass
[256], /* Username and password (unused) */
1510 hostname
[256]; /* Hostname */
1511 int port
; /* Port number */
1514 DEBUG_printf(("_cupsGetDestResource(dest=%p(%s), resource=%p, resourcesize=%d)", (void *)dest
, dest
->name
, (void *)resource
, (int)resourcesize
));
1517 * Range check input...
1520 if (!dest
|| !resource
|| resourcesize
< 1)
1525 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1530 * Grab the printer URI...
1533 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
1535 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
1537 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1538 if (strstr(uri
, "._tcp"))
1539 uri
= cups_dnssd_resolve(dest
, uri
, 5000, NULL
, NULL
, NULL
);
1540 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1545 DEBUG_printf(("1_cupsGetDestResource: Resolved printer-uri-supported=\"%s\"", uri
));
1547 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, resource
, resourcesize
);
1552 DEBUG_printf(("1_cupsGetDestResource: Local printer-uri-supported=\"%s\"", uri
));
1554 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
1556 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
1560 DEBUG_puts("1_cupsGetDestResource: No printer-uri-supported found.");
1565 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1572 DEBUG_printf(("1_cupsGetDestResource: printer-uri-supported=\"%s\"", uri
));
1574 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1575 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
1576 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
1578 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1584 DEBUG_printf(("1_cupsGetDestResource: resource=\"%s\"", resource
));
1591 * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
1593 * "name" is the desired name for the printer. If @code NULL@, a name will be
1594 * created using the URI.
1596 * "uri" is the "ipp" or "ipps" URI for the printer.
1598 * @since CUPS 2.0/macOS 10.10@
1601 cups_dest_t
* /* O - Destination or @code NULL@ */
1602 cupsGetDestWithURI(const char *name
, /* I - Desired printer name or @code NULL@ */
1603 const char *uri
) /* I - URI for the printer */
1605 cups_dest_t
*dest
; /* New destination */
1606 char temp
[1024], /* Temporary string */
1607 scheme
[256], /* Scheme from URI */
1608 userpass
[256], /* Username:password from URI */
1609 hostname
[256], /* Hostname from URI */
1610 resource
[1024], /* Resource path from URI */
1611 *ptr
; /* Pointer into string */
1612 const char *info
; /* printer-info string */
1613 int port
; /* Port number from URI */
1617 * Range check input...
1622 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1626 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
||
1627 (strncmp(uri
, "ipp://", 6) && strncmp(uri
, "ipps://", 7)))
1629 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1641 * Create the name from the URI...
1644 if (strstr(hostname
, "._tcp"))
1647 * Use the service instance name...
1650 if ((ptr
= strstr(hostname
, "._")) != NULL
)
1653 cups_queue_name(temp
, hostname
, sizeof(temp
));
1657 else if (!strncmp(resource
, "/classes/", 9))
1659 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 9, hostname
);
1660 name
= resource
+ 9;
1663 else if (!strncmp(resource
, "/printers/", 10))
1665 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 10, hostname
);
1666 name
= resource
+ 10;
1677 * Create the destination...
1680 if ((dest
= calloc(1, sizeof(cups_dest_t
))) == NULL
)
1682 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1686 dest
->name
= _cupsStrAlloc(name
);
1687 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &(dest
->options
));
1688 dest
->num_options
= cupsAddOption("printer-info", info
, dest
->num_options
, &(dest
->options
));
1695 * '_cupsGetDests()' - Get destinations from a server.
1697 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1698 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1701 * "name" is the name of an existing printer and is only used when "op" is
1702 * IPP_OP_GET_PRINTER_ATTRIBUTES.
1704 * "dest" is initialized to point to the array of destinations.
1706 * 0 is returned if there are no printers, no default printer, or the named
1707 * printer does not exist, respectively.
1709 * Free the memory used by the destination array using the @link cupsFreeDests@
1712 * Note: On macOS this function also gets the default paper from the system
1713 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1714 * options array for each destination that supports it.
1717 int /* O - Number of destinations */
1718 _cupsGetDests(http_t
*http
, /* I - Connection to server or
1719 * @code CUPS_HTTP_DEFAULT@ */
1720 ipp_op_t op
, /* I - IPP operation */
1721 const char *name
, /* I - Name of destination */
1722 cups_dest_t
**dests
, /* IO - Destinations */
1723 cups_ptype_t type
, /* I - Printer type bits */
1724 cups_ptype_t mask
) /* I - Printer type mask */
1726 int num_dests
= 0; /* Number of destinations */
1727 cups_dest_t
*dest
; /* Current destination */
1728 ipp_t
*request
, /* IPP Request */
1729 *response
; /* IPP Response */
1730 ipp_attribute_t
*attr
; /* Current attribute */
1731 const char *printer_name
; /* printer-name attribute */
1732 char uri
[1024]; /* printer-uri value */
1733 int num_options
; /* Number of options */
1734 cups_option_t
*options
; /* Options */
1736 char media_default
[41]; /* Default paper size */
1737 #endif /* __APPLE__ */
1738 char optname
[1024], /* Option name */
1739 value
[2048], /* Option value */
1740 *ptr
; /* Pointer into name/value */
1741 static const char * const pattrs
[] = /* Attributes we're interested in */
1743 "auth-info-required",
1745 "job-sheets-default",
1746 "marker-change-time",
1748 "marker-high-levels",
1750 "marker-low-levels",
1756 #endif /* __APPLE__ */
1760 "printer-is-accepting-jobs",
1761 "printer-is-shared",
1762 "printer-is-temporary",
1764 "printer-make-and-model",
1765 "printer-mandatory-job-attributes",
1768 "printer-state-change-time",
1769 "printer-state-reasons",
1771 "printer-uri-supported"
1775 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
));
1779 * Get the default paper size...
1782 appleGetPaperSize(media_default
, sizeof(media_default
));
1783 #endif /* __APPLE__ */
1786 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
1787 * require the following attributes:
1789 * attributes-charset
1790 * attributes-natural-language
1791 * requesting-user-name
1792 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
1795 request
= ippNewRequest(op
);
1797 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1798 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1801 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1802 "requesting-user-name", NULL
, cupsUser());
1804 if (name
&& op
!= IPP_OP_CUPS_GET_DEFAULT
)
1806 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1807 "localhost", ippPort(), "/printers/%s", name
);
1808 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1813 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type", (int)type
);
1814 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask", (int)mask
);
1818 * Do the request and get back a response...
1821 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1823 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1826 * Skip leading attributes until we hit a printer...
1829 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1836 * Pull the needed attributes from this printer...
1839 printer_name
= NULL
;
1843 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1845 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1846 attr
->value_tag
!= IPP_TAG_ENUM
&&
1847 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1848 attr
->value_tag
!= IPP_TAG_TEXT
&&
1849 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1850 attr
->value_tag
!= IPP_TAG_NAME
&&
1851 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1852 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1853 attr
->value_tag
!= IPP_TAG_RANGE
&&
1854 attr
->value_tag
!= IPP_TAG_URI
)
1857 if (!strcmp(attr
->name
, "auth-info-required") ||
1858 !strcmp(attr
->name
, "device-uri") ||
1859 !strcmp(attr
->name
, "marker-change-time") ||
1860 !strcmp(attr
->name
, "marker-colors") ||
1861 !strcmp(attr
->name
, "marker-high-levels") ||
1862 !strcmp(attr
->name
, "marker-levels") ||
1863 !strcmp(attr
->name
, "marker-low-levels") ||
1864 !strcmp(attr
->name
, "marker-message") ||
1865 !strcmp(attr
->name
, "marker-names") ||
1866 !strcmp(attr
->name
, "marker-types") ||
1867 !strcmp(attr
->name
, "printer-commands") ||
1868 !strcmp(attr
->name
, "printer-info") ||
1869 !strcmp(attr
->name
, "printer-is-shared") ||
1870 !strcmp(attr
->name
, "printer-is-temporary") ||
1871 !strcmp(attr
->name
, "printer-make-and-model") ||
1872 !strcmp(attr
->name
, "printer-mandatory-job-attributes") ||
1873 !strcmp(attr
->name
, "printer-state") ||
1874 !strcmp(attr
->name
, "printer-state-change-time") ||
1875 !strcmp(attr
->name
, "printer-type") ||
1876 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1877 !strcmp(attr
->name
, "printer-location") ||
1878 !strcmp(attr
->name
, "printer-state-reasons") ||
1879 !strcmp(attr
->name
, "printer-uri-supported"))
1882 * Add a printer description attribute...
1885 num_options
= cupsAddOption(attr
->name
,
1886 cups_make_string(attr
, value
,
1888 num_options
, &options
);
1891 else if (!strcmp(attr
->name
, "media-supported"))
1894 * See if we can set a default media size...
1897 int i
; /* Looping var */
1899 for (i
= 0; i
< attr
->num_values
; i
++)
1900 if (!_cups_strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1902 num_options
= cupsAddOption("media", media_default
, num_options
,
1907 #endif /* __APPLE__ */
1908 else if (!strcmp(attr
->name
, "printer-name") &&
1909 attr
->value_tag
== IPP_TAG_NAME
)
1910 printer_name
= attr
->values
[0].string
.text
;
1911 else if (strncmp(attr
->name
, "notify-", 7) &&
1912 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1913 attr
->value_tag
== IPP_TAG_ENUM
||
1914 attr
->value_tag
== IPP_TAG_INTEGER
||
1915 attr
->value_tag
== IPP_TAG_KEYWORD
||
1916 attr
->value_tag
== IPP_TAG_NAME
||
1917 attr
->value_tag
== IPP_TAG_RANGE
) &&
1918 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1921 * Add a default option...
1924 strlcpy(optname
, attr
->name
, sizeof(optname
));
1925 optname
[ptr
- attr
->name
] = '\0';
1927 if (_cups_strcasecmp(optname
, "media") ||
1928 !cupsGetOption("media", num_options
, options
))
1929 num_options
= cupsAddOption(optname
,
1930 cups_make_string(attr
, value
,
1932 num_options
, &options
);
1937 * See if we have everything needed...
1942 cupsFreeOptions(num_options
, options
);
1950 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1952 dest
->num_options
= num_options
;
1953 dest
->options
= options
;
1956 cupsFreeOptions(num_options
, options
);
1962 ippDelete(response
);
1966 * Return the count...
1974 * 'cupsGetDests()' - Get the list of destinations from the default server.
1976 * Starting with CUPS 1.2, the returned list of destinations include the
1977 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1978 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1979 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1980 * attributes as options.
1982 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1983 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1984 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1986 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1987 * be used. The "printer-uri-supported" option will be present for those IPP
1988 * printers that have been recently used.
1990 * Use the @link cupsFreeDests@ function to free the destination list and
1991 * the @link cupsGetDest@ function to find a particular destination.
1996 int /* O - Number of destinations */
1997 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
1999 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
2004 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
2006 * Starting with CUPS 1.2, the returned list of destinations include the
2007 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
2008 * "printer-make-and-model", "printer-state", "printer-state-change-time",
2009 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
2010 * attributes as options.
2012 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
2013 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
2014 * "marker-names", "marker-types", and "printer-commands" attributes as options.
2016 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
2017 * be used. The "printer-uri-supported" option will be present for those IPP
2018 * printers that have been recently used.
2020 * Use the @link cupsFreeDests@ function to free the destination list and
2021 * the @link cupsGetDest@ function to find a particular destination.
2023 * @since CUPS 1.1.21/macOS 10.4@
2026 int /* O - Number of destinations */
2027 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2028 cups_dest_t
**dests
) /* O - Destinations */
2030 _cups_getdata_t data
; /* Enumeration data */
2031 cups_dest_t
*dest
; /* Current destination */
2032 const char *home
; /* HOME environment variable */
2033 char filename
[1024]; /* Local ~/.cups/lpoptions file */
2034 const char *defprinter
; /* Default printer */
2035 char name
[1024], /* Copy of printer name */
2036 *instance
, /* Pointer to instance name */
2037 *user_default
; /* User default printer */
2038 int num_reals
; /* Number of real queues */
2039 cups_dest_t
*reals
; /* Real queues */
2040 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2044 * Range check the input...
2049 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad NULL dests pointer"), 1);
2054 * Grab the printers and classes...
2060 cupsEnumDests(0, 1000, NULL
, 0, 0, (cups_dest_cb_t
)cups_get_cb
, &data
);
2062 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2064 cupsFreeDests(data
.num_dests
, data
.dests
);
2066 *dests
= (cups_dest_t
*)0;
2072 * Make a copy of the "real" queues for a later sanity check...
2075 if (data
.num_dests
> 0)
2077 num_reals
= data
.num_dests
;
2078 reals
= calloc((size_t)num_reals
, sizeof(cups_dest_t
));
2081 memcpy(reals
, data
.dests
, (size_t)num_reals
* sizeof(cups_dest_t
));
2092 * Grab the default destination...
2095 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
2097 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
2099 strlcpy(name
, defprinter
, sizeof(name
));
2106 * Separate printer and instance name...
2109 if ((instance
= strchr(name
, '/')) != NULL
)
2113 * Lookup the printer and instance and make it the default...
2116 if ((dest
= cupsGetDest(name
, instance
, data
.num_dests
, data
.dests
)) != NULL
)
2117 dest
->is_default
= 1;
2123 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
2126 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2127 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
2129 if ((home
= getenv("HOME")) != NULL
)
2131 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2133 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
2137 * Validate the current default destination - this prevents old
2138 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
2139 * pointing to a non-existent printer or class...
2145 * See if we have a default printer...
2148 if ((dest
= cupsGetDest(NULL
, NULL
, data
.num_dests
, data
.dests
)) != NULL
)
2151 * Have a default; see if it is real...
2154 if (!cupsGetDest(dest
->name
, NULL
, num_reals
, reals
))
2157 * Remove the non-real printer from the list, since we don't want jobs
2158 * going to an unexpected printer... (<rdar://problem/14216472>)
2161 data
.num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
.num_dests
, &data
.dests
);
2173 * Return the number of destinations...
2176 *dests
= data
.dests
;
2178 if (data
.num_dests
> 0)
2179 _cupsSetError(IPP_STATUS_OK
, NULL
, 0);
2181 return (data
.num_dests
);
2186 * 'cupsGetNamedDest()' - Get options for the named destination.
2188 * This function is optimized for retrieving a single destination and should
2189 * be used instead of @link cupsGetDests2@ and @link cupsGetDest@ when you
2190 * either know the name of the destination or want to print to the default
2191 * destination. If @code NULL@ is returned, the destination does not exist or
2192 * there is no default destination.
2194 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
2195 * server will be used.
2197 * If "name" is @code NULL@, the default printer for the current user will be
2200 * The returned destination must be freed using @link cupsFreeDests@ with a
2201 * "num_dests" value of 1.
2203 * @since CUPS 1.4/macOS 10.6@
2206 cups_dest_t
* /* O - Destination or @code NULL@ */
2207 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2208 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
2209 const char *instance
) /* I - Instance name or @code NULL@ */
2211 cups_dest_t
*dest
; /* Destination */
2212 char filename
[1024], /* Path to lpoptions */
2213 defname
[256]; /* Default printer name */
2214 const char *home
= getenv("HOME"); /* Home directory */
2215 int set_as_default
= 0; /* Set returned destination as default */
2216 ipp_op_t op
= IPP_OP_GET_PRINTER_ATTRIBUTES
;
2217 /* IPP operation to get server ops */
2218 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2221 DEBUG_printf(("cupsGetNamedDest(http=%p, name=\"%s\", instance=\"%s\")", (void *)http
, name
, instance
));
2224 * If "name" is NULL, find the default destination...
2230 name
= _cupsUserDefault(defname
, sizeof(defname
));
2234 char *ptr
; /* Temporary pointer... */
2236 if ((ptr
= strchr(defname
, '/')) != NULL
)
2247 * No default in the environment, try the user's lpoptions files...
2250 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2252 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
2258 * Still not there? Try the system lpoptions file...
2261 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
2262 cg
->cups_serverroot
);
2263 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
2269 * No locally-set default destination, ask the server...
2272 op
= IPP_OP_CUPS_GET_DEFAULT
;
2274 DEBUG_puts("1cupsGetNamedDest: Asking server for default printer...");
2277 DEBUG_printf(("1cupsGetNamedDest: Using name=\"%s\"...", name
));
2281 * Get the printer's attributes...
2284 if (!_cupsGetDests(http
, op
, name
, &dest
, 0, 0))
2288 _cups_namedata_t data
; /* Callback data */
2290 DEBUG_puts("1cupsGetNamedDest: No queue found for printer, looking on network...");
2295 cupsEnumDests(0, 1000, NULL
, 0, 0, (cups_dest_cb_t
)cups_name_cb
, &data
);
2306 DEBUG_printf(("1cupsGetNamedDest: Got dest=%p", (void *)dest
));
2309 dest
->instance
= _cupsStrAlloc(instance
);
2312 dest
->is_default
= 1;
2315 * Then add local options...
2318 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2319 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
2323 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2325 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
2329 * Return the result...
2337 * 'cupsRemoveDest()' - Remove a destination from the destination list.
2339 * Removing a destination/instance does not delete the class or printer
2340 * queue, merely the lpoptions for that destination/instance. Use the
2341 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
2342 * options for the user.
2344 * @since CUPS 1.3/macOS 10.5@
2347 int /* O - New number of destinations */
2348 cupsRemoveDest(const char *name
, /* I - Destination name */
2349 const char *instance
, /* I - Instance name or @code NULL@ */
2350 int num_dests
, /* I - Number of destinations */
2351 cups_dest_t
**dests
) /* IO - Destinations */
2353 int i
; /* Index into destinations */
2354 cups_dest_t
*dest
; /* Pointer to destination */
2358 * Find the destination...
2361 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
2368 _cupsStrFree(dest
->name
);
2369 _cupsStrFree(dest
->instance
);
2370 cupsFreeOptions(dest
->num_options
, dest
->options
);
2373 * Remove the destination from the array...
2378 i
= (int)(dest
- *dests
);
2381 memmove(dest
, dest
+ 1, (size_t)(num_dests
- i
) * sizeof(cups_dest_t
));
2388 * 'cupsSetDefaultDest()' - Set the default destination.
2390 * @since CUPS 1.3/macOS 10.5@
2395 const char *name
, /* I - Destination name */
2396 const char *instance
, /* I - Instance name or @code NULL@ */
2397 int num_dests
, /* I - Number of destinations */
2398 cups_dest_t
*dests
) /* I - Destinations */
2400 int i
; /* Looping var */
2401 cups_dest_t
*dest
; /* Current destination */
2405 * Range check input...
2408 if (!name
|| num_dests
<= 0 || !dests
)
2412 * Loop through the array and set the "is_default" flag for the matching
2416 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2417 dest
->is_default
= !_cups_strcasecmp(name
, dest
->name
) &&
2418 ((!instance
&& !dest
->instance
) ||
2419 (instance
&& dest
->instance
&&
2420 !_cups_strcasecmp(instance
, dest
->instance
)));
2425 * 'cupsSetDests()' - Save the list of destinations for the default server.
2427 * This function saves the destinations to /etc/cups/lpoptions when run
2428 * as root and ~/.cups/lpoptions when run as a normal user.
2434 cupsSetDests(int num_dests
, /* I - Number of destinations */
2435 cups_dest_t
*dests
) /* I - Destinations */
2437 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
2442 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
2444 * This function saves the destinations to /etc/cups/lpoptions when run
2445 * as root and ~/.cups/lpoptions when run as a normal user.
2447 * @since CUPS 1.1.21/macOS 10.4@
2450 int /* O - 0 on success, -1 on error */
2451 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2452 int num_dests
, /* I - Number of destinations */
2453 cups_dest_t
*dests
) /* I - Destinations */
2455 int i
, j
; /* Looping vars */
2456 int wrote
; /* Wrote definition? */
2457 cups_dest_t
*dest
; /* Current destination */
2458 cups_option_t
*option
; /* Current option */
2459 _ipp_option_t
*match
; /* Matching attribute for option */
2460 FILE *fp
; /* File pointer */
2462 const char *home
; /* HOME environment variable */
2464 char filename
[1024]; /* lpoptions file */
2465 int num_temps
; /* Number of temporary destinations */
2466 cups_dest_t
*temps
= NULL
, /* Temporary destinations */
2467 *temp
; /* Current temporary dest */
2468 const char *val
; /* Value of temporary option */
2469 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2473 * Range check the input...
2476 if (!num_dests
|| !dests
)
2480 * Get the server destinations...
2483 num_temps
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &temps
, 0, 0);
2485 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2487 cupsFreeDests(num_temps
, temps
);
2492 * Figure out which file to write to...
2495 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2501 * Merge in server defaults...
2504 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
2507 * Point to user defaults...
2510 if ((home
= getenv("HOME")) != NULL
)
2513 * Create ~/.cups subdirectory...
2516 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
2517 if (access(filename
, 0))
2518 mkdir(filename
, 0700);
2520 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2526 * Try to open the file...
2529 if ((fp
= fopen(filename
, "w")) == NULL
)
2531 cupsFreeDests(num_temps
, temps
);
2537 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2542 fchmod(fileno(fp
), 0644);
2546 * Write each printer; each line looks like:
2548 * Dest name[/instance] options
2549 * Default name[/instance] options
2552 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2553 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
2555 if (dest
->is_default
)
2557 fprintf(fp
, "Default %s", dest
->name
);
2559 fprintf(fp
, "/%s", dest
->instance
);
2566 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
2567 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
2569 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
2572 * See if this option is a printer attribute; if so, skip it...
2575 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
2576 match
->group_tag
== IPP_TAG_PRINTER
)
2580 * See if the server/global options match these; if so, don't
2585 (val
= cupsGetOption(option
->name
, temp
->num_options
,
2586 temp
->options
)) != NULL
&&
2587 !_cups_strcasecmp(val
, option
->value
))
2591 * Options don't match, write to the file...
2596 fprintf(fp
, "Dest %s", dest
->name
);
2598 fprintf(fp
, "/%s", dest
->instance
);
2602 if (option
->value
[0])
2604 if (strchr(option
->value
, ' ') ||
2605 strchr(option
->value
, '\\') ||
2606 strchr(option
->value
, '\"') ||
2607 strchr(option
->value
, '\''))
2610 * Quote the value...
2613 fprintf(fp
, " %s=\"", option
->name
);
2615 for (val
= option
->value
; *val
; val
++)
2617 if (strchr("\"\'\\", *val
))
2628 * Store the literal value...
2631 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
2635 fprintf(fp
, " %s", option
->name
);
2643 * Free the temporary destinations and close the file...
2646 cupsFreeDests(num_temps
, temps
);
2652 * Set the default printer for this location - this allows command-line
2653 * and GUI applications to share the same default destination...
2656 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
2658 CFStringRef name
= CFStringCreateWithCString(kCFAllocatorDefault
,
2660 kCFStringEncodingUTF8
);
2661 /* Default printer name */
2665 _cupsAppleSetDefaultPrinter(name
);
2669 #endif /* __APPLE__ */
2671 #ifdef HAVE_NOTIFY_POST
2673 * Send a notification so that macOS applications can know about the
2677 notify_post("com.apple.printerListChange");
2678 #endif /* HAVE_NOTIFY_POST */
2685 * '_cupsUserDefault()' - Get the user default printer from environment
2686 * variables and location information.
2689 char * /* O - Default printer or NULL */
2690 _cupsUserDefault(char *name
, /* I - Name buffer */
2691 size_t namesize
) /* I - Size of name buffer */
2693 const char *env
; /* LPDEST or PRINTER env variable */
2695 CFStringRef locprinter
; /* Last printer as this location */
2696 #endif /* __APPLE__ */
2699 if ((env
= getenv("LPDEST")) == NULL
)
2700 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
2705 strlcpy(name
, env
, namesize
);
2711 * Use location-based defaults if "use last printer" is selected in the
2712 * system preferences...
2715 if ((locprinter
= _cupsAppleCopyDefaultPrinter()) != NULL
)
2717 CFStringGetCString(locprinter
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
);
2718 CFRelease(locprinter
);
2723 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name
));
2725 return (*name
? name
: NULL
);
2729 * No location-based defaults on this platform...
2734 #endif /* __APPLE__ */
2738 #if _CUPS_LOCATION_DEFAULTS
2740 * 'appleCopyLocations()' - Copy the location history array.
2743 static CFArrayRef
/* O - Location array or NULL */
2744 appleCopyLocations(void)
2746 CFArrayRef locations
; /* Location array */
2750 * Look up the location array in the preferences...
2753 if ((locations
= CFPreferencesCopyAppValue(kLastUsedPrintersKey
,
2754 kCUPSPrintingPrefs
)) == NULL
)
2757 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
2759 CFRelease(locations
);
2768 * 'appleCopyNetwork()' - Get the network ID for the current location.
2771 static CFStringRef
/* O - Network ID */
2772 appleCopyNetwork(void)
2774 SCDynamicStoreRef dynamicStore
; /* System configuration data */
2775 CFStringRef key
; /* Current network configuration key */
2776 CFDictionaryRef ip_dict
; /* Network configuration data */
2777 CFStringRef network
= NULL
; /* Current network ID */
2780 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("libcups"), NULL
,
2784 * First use the IPv6 router address, if available, since that will generally
2785 * be a globally-unique link-local address.
2788 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2789 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv6
)) != NULL
)
2791 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2793 if ((network
= CFDictionaryGetValue(ip_dict
,
2794 kSCPropNetIPv6Router
)) != NULL
)
2804 * If that doesn't work, try the IPv4 router address. This isn't as unique
2805 * and will likely be a 10.x.y.z or 192.168.y.z address...
2810 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2811 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
2813 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2815 if ((network
= CFDictionaryGetValue(ip_dict
,
2816 kSCPropNetIPv4Router
)) != NULL
)
2826 CFRelease(dynamicStore
);
2831 #endif /* _CUPS_LOCATION_DEFAULTS */
2836 * 'appleGetPaperSize()' - Get the default paper size.
2839 static char * /* O - Default paper size */
2840 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
2841 size_t namesize
) /* I - Size of buffer */
2843 CFStringRef defaultPaperID
; /* Default paper ID */
2844 pwg_media_t
*pwgmedia
; /* PWG media size */
2847 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
2848 if (!defaultPaperID
||
2849 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
2850 !CFStringGetCString(defaultPaperID
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
))
2852 else if ((pwgmedia
= pwgMediaForLegacy(name
)) != NULL
)
2853 strlcpy(name
, pwgmedia
->pwg
, namesize
);
2856 CFRelease(defaultPaperID
);
2860 #endif /* __APPLE__ */
2863 #if _CUPS_LOCATION_DEFAULTS
2865 * 'appleGetPrinter()' - Get a printer from the history array.
2868 static CFStringRef
/* O - Printer name or NULL */
2869 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
2870 CFStringRef network
, /* I - Network name */
2871 CFIndex
*locindex
) /* O - Index in array */
2873 CFIndex i
, /* Looping var */
2874 count
; /* Number of locations */
2875 CFDictionaryRef location
; /* Current location */
2876 CFStringRef locnetwork
, /* Current network */
2877 locprinter
; /* Current printer */
2880 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
2881 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
2882 CFGetTypeID(location
) == CFDictionaryGetTypeID())
2884 if ((locnetwork
= CFDictionaryGetValue(location
,
2885 kLocationNetworkKey
)) != NULL
&&
2886 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
2887 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
2888 (locprinter
= CFDictionaryGetValue(location
,
2889 kLocationPrinterIDKey
)) != NULL
&&
2890 CFGetTypeID(locprinter
) == CFStringGetTypeID())
2895 return (locprinter
);
2901 #endif /* _CUPS_LOCATION_DEFAULTS */
2905 * 'cups_add_dest()' - Add a destination to the array.
2907 * Unlike cupsAddDest(), this function does not check for duplicates.
2910 static cups_dest_t
* /* O - New destination */
2911 cups_add_dest(const char *name
, /* I - Name of destination */
2912 const char *instance
, /* I - Instance or NULL */
2913 int *num_dests
, /* IO - Number of destinations */
2914 cups_dest_t
**dests
) /* IO - Destinations */
2916 int insert
, /* Insertion point */
2917 diff
; /* Result of comparison */
2918 cups_dest_t
*dest
; /* Destination pointer */
2922 * Add new destination...
2925 if (*num_dests
== 0)
2926 dest
= malloc(sizeof(cups_dest_t
));
2928 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (size_t)(*num_dests
+ 1));
2936 * Find where to insert the destination...
2939 if (*num_dests
== 0)
2943 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
2951 * Move the array elements as needed...
2954 if (insert
< *num_dests
)
2955 memmove(*dests
+ insert
+ 1, *dests
+ insert
, (size_t)(*num_dests
- insert
) * sizeof(cups_dest_t
));
2960 * Initialize the destination...
2963 dest
= *dests
+ insert
;
2964 dest
->name
= _cupsStrAlloc(name
);
2965 dest
->instance
= _cupsStrAlloc(instance
);
2966 dest
->is_default
= 0;
2967 dest
->num_options
= 0;
2968 dest
->options
= (cups_option_t
*)0;
2976 * 'cups_block_cb()' - Enumeration callback for block API.
2979 static int /* O - 1 to continue, 0 to stop */
2981 cups_dest_block_t block
, /* I - Block */
2982 unsigned flags
, /* I - Destination flags */
2983 cups_dest_t
*dest
) /* I - Destination */
2985 return ((block
)(flags
, dest
));
2987 # endif /* __BLOCKS__ */
2991 * 'cups_compare_dests()' - Compare two destinations.
2994 static int /* O - Result of comparison */
2995 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
2996 cups_dest_t
*b
) /* I - Second destination */
2998 int diff
; /* Difference */
3001 if ((diff
= _cups_strcasecmp(a
->name
, b
->name
)) != 0)
3003 else if (a
->instance
&& b
->instance
)
3004 return (_cups_strcasecmp(a
->instance
, b
->instance
));
3006 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
3010 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3013 * 'cups_dnssd_browse_cb()' - Browse for printers.
3017 cups_dnssd_browse_cb(
3018 DNSServiceRef sdRef
, /* I - Service reference */
3019 DNSServiceFlags flags
, /* I - Option flags */
3020 uint32_t interfaceIndex
, /* I - Interface number */
3021 DNSServiceErrorType errorCode
, /* I - Error, if any */
3022 const char *serviceName
, /* I - Name of service/device */
3023 const char *regtype
, /* I - Type of service */
3024 const char *replyDomain
, /* I - Service domain */
3025 void *context
) /* I - Enumeration data */
3027 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3028 /* Enumeration data */
3031 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
));
3034 * Don't do anything on error...
3037 if (errorCode
!= kDNSServiceErr_NoError
)
3044 cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
3048 # else /* HAVE_AVAHI */
3050 * 'cups_dnssd_browse_cb()' - Browse for printers.
3054 cups_dnssd_browse_cb(
3055 AvahiServiceBrowser
*browser
, /* I - Browser */
3056 AvahiIfIndex interface
, /* I - Interface index (unused) */
3057 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3058 AvahiBrowserEvent event
, /* I - What happened */
3059 const char *name
, /* I - Service name */
3060 const char *type
, /* I - Registration type */
3061 const char *domain
, /* I - Domain */
3062 AvahiLookupResultFlags flags
, /* I - Flags */
3063 void *context
) /* I - Devices array */
3066 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
3067 /* Client information */
3069 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3070 /* Enumeration data */
3077 DEBUG_printf(("cups_dnssd_browse_cb(..., name=\"%s\", type=\"%s\", domain=\"%s\", ...);", name
, type
, domain
));
3081 case AVAHI_BROWSER_FAILURE
:
3082 DEBUG_printf(("cups_dnssd_browse_cb: %s", avahi_strerror(avahi_client_errno(client
))));
3083 avahi_simple_poll_quit(data
->simple_poll
);
3086 case AVAHI_BROWSER_NEW
:
3088 * This object is new on the network.
3091 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
3094 * This comes from the local machine so ignore it.
3097 DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".", name
));
3102 * Create a device entry for it if it doesn't yet exist.
3105 cups_dnssd_get_device(data
, name
, type
, domain
);
3109 case AVAHI_BROWSER_REMOVE
:
3110 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
3113 case AVAHI_BROWSER_ALL_FOR_NOW
:
3114 DEBUG_puts("cups_dnssd_browse_cb: ALL_FOR_NOW");
3122 * 'cups_dnssd_client_cb()' - Avahi client callback function.
3126 cups_dnssd_client_cb(
3127 AvahiClient
*client
, /* I - Client information (unused) */
3128 AvahiClientState state
, /* I - Current state */
3129 void *context
) /* I - User data (unused) */
3131 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3132 /* Enumeration data */
3137 DEBUG_printf(("cups_dnssd_client_cb(client=%p, state=%d, context=%p)", client
, state
, context
));
3140 * If the connection drops, quit.
3143 if (state
== AVAHI_CLIENT_FAILURE
)
3145 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
3146 avahi_simple_poll_quit(data
->simple_poll
);
3149 # endif /* HAVE_DNSSD */
3153 * 'cups_dnssd_compare_device()' - Compare two devices.
3156 static int /* O - Result of comparison */
3157 cups_dnssd_compare_devices(
3158 _cups_dnssd_device_t
*a
, /* I - First device */
3159 _cups_dnssd_device_t
*b
) /* I - Second device */
3161 return (strcmp(a
->dest
.name
, b
->dest
.name
));
3166 * 'cups_dnssd_free_device()' - Free the memory used by a device.
3170 cups_dnssd_free_device(
3171 _cups_dnssd_device_t
*device
, /* I - Device */
3172 _cups_dnssd_data_t
*data
) /* I - Enumeration data */
3174 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", (void *)device
, device
->dest
.name
, (void *)data
));
3178 DNSServiceRefDeallocate(device
->ref
);
3179 # else /* HAVE_AVAHI */
3181 avahi_record_browser_free(device
->ref
);
3182 # endif /* HAVE_DNSSD */
3184 _cupsStrFree(device
->domain
);
3185 _cupsStrFree(device
->fullName
);
3186 _cupsStrFree(device
->regtype
);
3187 _cupsStrFree(device
->dest
.name
);
3189 cupsFreeOptions(device
->dest
.num_options
, device
->dest
.options
);
3196 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
3199 static _cups_dnssd_device_t
* /* O - Device */
3200 cups_dnssd_get_device(
3201 _cups_dnssd_data_t
*data
, /* I - Enumeration data */
3202 const char *serviceName
, /* I - Service name */
3203 const char *regtype
, /* I - Registration type */
3204 const char *replyDomain
) /* I - Domain name */
3206 _cups_dnssd_device_t key
, /* Search key */
3207 *device
; /* Device */
3208 char fullName
[kDNSServiceMaxDomainName
],
3209 /* Full name for query */
3210 name
[128]; /* Queue name */
3213 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\")", (void *)data
, serviceName
, regtype
, replyDomain
));
3216 * See if this is an existing device...
3219 cups_queue_name(name
, serviceName
, sizeof(name
));
3221 key
.dest
.name
= name
;
3223 if ((device
= cupsArrayFind(data
->devices
, &key
)) != NULL
)
3226 * Yes, see if we need to do anything with this...
3229 int update
= 0; /* Non-zero if we need to update */
3231 if (!_cups_strcasecmp(replyDomain
, "local.") &&
3232 _cups_strcasecmp(device
->domain
, replyDomain
))
3235 * Update the "global" listing to use the .local domain name instead.
3238 _cupsStrFree(device
->domain
);
3239 device
->domain
= _cupsStrAlloc(replyDomain
);
3241 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
3242 "domain.", device
->dest
.name
));
3247 if (!_cups_strcasecmp(regtype
, "_ipps._tcp") &&
3248 _cups_strcasecmp(device
->regtype
, regtype
))
3251 * Prefer IPPS over IPP.
3254 _cupsStrFree(device
->regtype
);
3255 device
->regtype
= _cupsStrAlloc(regtype
);
3257 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
3258 device
->dest
.name
));
3265 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
3266 device
->dest
.name
));
3273 * No, add the device...
3276 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
3277 "'%s'.", serviceName
,
3278 !strcmp(regtype
, "_ipps._tcp") ? "IPPS" : "IPP",
3281 device
= calloc(sizeof(_cups_dnssd_device_t
), 1);
3282 device
->dest
.name
= _cupsStrAlloc(name
);
3283 device
->domain
= _cupsStrAlloc(replyDomain
);
3284 device
->regtype
= _cupsStrAlloc(regtype
);
3286 device
->dest
.num_options
= cupsAddOption("printer-info", serviceName
, 0, &device
->dest
.options
);
3288 cupsArrayAdd(data
->devices
, device
);
3292 * Set the "full name" of this service, which is used for queries...
3296 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
3297 # else /* HAVE_AVAHI */
3298 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
, regtype
, replyDomain
);
3299 # endif /* HAVE_DNSSD */
3301 _cupsStrFree(device
->fullName
);
3302 device
->fullName
= _cupsStrAlloc(fullName
);
3307 DNSServiceRefDeallocate(device
->ref
);
3308 # else /* HAVE_AVAHI */
3309 avahi_record_browser_free(device
->ref
);
3310 # endif /* HAVE_DNSSD */
3315 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3317 DEBUG_printf(("6cups_dnssd_get_device: Remove callback for \"%s\".", device
->dest
.name
));
3319 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3320 device
->state
= _CUPS_DNSSD_NEW
;
3329 * 'cups_dnssd_local_cb()' - Browse for local printers.
3333 cups_dnssd_local_cb(
3334 DNSServiceRef sdRef
, /* I - Service reference */
3335 DNSServiceFlags flags
, /* I - Option flags */
3336 uint32_t interfaceIndex
, /* I - Interface number */
3337 DNSServiceErrorType errorCode
, /* I - Error, if any */
3338 const char *serviceName
, /* I - Name of service/device */
3339 const char *regtype
, /* I - Type of service */
3340 const char *replyDomain
, /* I - Service domain */
3341 void *context
) /* I - Devices array */
3343 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3344 /* Enumeration data */
3345 _cups_dnssd_device_t
*device
; /* Device */
3348 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
));
3351 * Only process "add" data...
3354 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3361 device
= cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
3364 * Hide locally-registered devices...
3367 DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.",
3372 DNSServiceRefDeallocate(device
->ref
);
3376 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3378 DEBUG_printf(("6cups_dnssd_local_cb: Remove callback for \"%s\".", device
->dest
.name
));
3379 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3382 device
->state
= _CUPS_DNSSD_LOCAL
;
3384 # endif /* HAVE_DNSSD */
3389 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
3391 * Note: This function is needed because avahi_simple_poll_iterate is broken
3392 * and always uses a timeout of 0 (!) milliseconds.
3393 * (https://github.com/lathiat/avahi/issues/127)
3398 static int /* O - Number of file descriptors matching */
3400 struct pollfd
*pollfds
, /* I - File descriptors */
3401 unsigned int num_pollfds
, /* I - Number of file descriptors */
3402 int timeout
, /* I - Timeout in milliseconds (unused) */
3403 void *context
) /* I - User data (unused) */
3405 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3406 /* Enumeration data */
3407 int val
; /* Return value */
3410 DEBUG_printf(("cups_dnssd_poll_cb(pollfds=%p, num_pollfds=%d, timeout=%d, context=%p)", pollfds
, num_pollfds
, timeout
, context
));
3414 val
= poll(pollfds
, num_pollfds
, _CUPS_DNSSD_MAXTIME
);
3416 DEBUG_printf(("cups_dnssd_poll_cb: poll() returned %d", val
));
3420 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno
)));
3429 # endif /* HAVE_AVAHI */
3433 * 'cups_dnssd_query_cb()' - Process query data.
3438 cups_dnssd_query_cb(
3439 DNSServiceRef sdRef
, /* I - Service reference */
3440 DNSServiceFlags flags
, /* I - Data flags */
3441 uint32_t interfaceIndex
, /* I - Interface */
3442 DNSServiceErrorType errorCode
, /* I - Error, if any */
3443 const char *fullName
, /* I - Full service name */
3444 uint16_t rrtype
, /* I - Record type */
3445 uint16_t rrclass
, /* I - Record class */
3446 uint16_t rdlen
, /* I - Length of record data */
3447 const void *rdata
, /* I - Record data */
3448 uint32_t ttl
, /* I - Time-to-live */
3449 void *context
) /* I - Enumeration data */
3451 # else /* HAVE_AVAHI */
3453 cups_dnssd_query_cb(
3454 AvahiRecordBrowser
*browser
, /* I - Record browser */
3455 AvahiIfIndex interfaceIndex
,
3456 /* I - Interface index (unused) */
3457 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3458 AvahiBrowserEvent event
, /* I - What happened? */
3459 const char *fullName
, /* I - Service name */
3460 uint16_t rrclass
, /* I - Record class */
3461 uint16_t rrtype
, /* I - Record type */
3462 const void *rdata
, /* I - TXT record */
3463 size_t rdlen
, /* I - Length of TXT record */
3464 AvahiLookupResultFlags flags
, /* I - Flags */
3465 void *context
) /* I - Enumeration data */
3468 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
3469 /* Client information */
3471 # endif /* HAVE_DNSSD */
3472 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3473 /* Enumeration data */
3474 char serviceName
[256],/* Service name */
3475 name
[128], /* Queue name */
3476 *ptr
; /* Pointer into string */
3477 _cups_dnssd_device_t dkey
, /* Search key */
3478 *device
; /* Device */
3482 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
));
3485 * Only process "add" data...
3488 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3491 # else /* HAVE_AVAHI */
3492 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
));
3495 * Only process "add" data...
3498 if (event
!= AVAHI_BROWSER_NEW
)
3500 if (event
== AVAHI_BROWSER_FAILURE
)
3501 DEBUG_printf(("cups_dnssd_query_cb: %s", avahi_strerror(avahi_client_errno(client
))));
3505 # endif /* HAVE_DNSSD */
3508 * Lookup the service in the devices array.
3511 cups_dnssd_unquote(serviceName
, fullName
, sizeof(serviceName
));
3513 if ((ptr
= strstr(serviceName
, "._")) != NULL
)
3516 cups_queue_name(name
, serviceName
, sizeof(name
));
3518 dkey
.dest
.name
= name
;
3520 if ((device
= cupsArrayFind(data
->devices
, &dkey
)) != NULL
&& device
->state
== _CUPS_DNSSD_NEW
)
3523 * Found it, pull out the make and model from the TXT record and save it...
3526 const uint8_t *txt
, /* Pointer into data */
3527 *txtnext
, /* Next key/value pair */
3528 *txtend
; /* End of entire TXT record */
3529 uint8_t txtlen
; /* Length of current key/value pair */
3530 char key
[256], /* Key string */
3531 value
[256], /* Value string */
3532 make_and_model
[512],
3533 /* Manufacturer and model */
3534 model
[256], /* Model */
3535 uriname
[1024], /* Name for URI */
3536 uri
[1024]; /* Printer URI */
3537 cups_ptype_t type
= CUPS_PRINTER_DISCOVERED
| CUPS_PRINTER_BW
;
3539 int saw_printer_type
= 0;
3540 /* Did we see a printer-type key? */
3542 device
->state
= _CUPS_DNSSD_PENDING
;
3543 make_and_model
[0] = '\0';
3545 strlcpy(model
, "Unknown", sizeof(model
));
3547 for (txt
= rdata
, txtend
= txt
+ rdlen
;
3552 * Read a key/value pair starting with an 8-bit length. Since the
3553 * length is 8 bits and the size of the key/value buffers is 256, we
3554 * don't need to check for overflow...
3559 if (!txtlen
|| (txt
+ txtlen
) > txtend
)
3562 txtnext
= txt
+ txtlen
;
3564 for (ptr
= key
; txt
< txtnext
&& *txt
!= '='; txt
++)
3565 *ptr
++ = (char)*txt
;
3568 if (txt
< txtnext
&& *txt
== '=')
3573 memcpy(value
, txt
, (size_t)(txtnext
- txt
));
3574 value
[txtnext
- txt
] = '\0';
3576 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key
, value
));
3580 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key
));
3584 if (!_cups_strcasecmp(key
, "usb_MFG") ||
3585 !_cups_strcasecmp(key
, "usb_MANU") ||
3586 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
3587 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
3588 else if (!_cups_strcasecmp(key
, "usb_MDL") ||
3589 !_cups_strcasecmp(key
, "usb_MODEL"))
3590 strlcpy(model
, value
, sizeof(model
));
3591 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
3593 if (value
[0] == '(')
3596 * Strip parenthesis...
3599 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
3602 strlcpy(model
, value
+ 1, sizeof(model
));
3605 strlcpy(model
, value
, sizeof(model
));
3607 else if (!_cups_strcasecmp(key
, "ty"))
3609 strlcpy(model
, value
, sizeof(model
));
3611 if ((ptr
= strchr(model
, ',')) != NULL
)
3614 else if (!_cups_strcasecmp(key
, "note"))
3615 device
->dest
.num_options
= cupsAddOption("printer-location", value
,
3616 device
->dest
.num_options
,
3617 &device
->dest
.options
);
3618 else if (!_cups_strcasecmp(key
, "pdl"))
3621 * Look for PDF-capable printers; only PDF-capable printers are shown.
3624 const char *start
, *next
; /* Pointer into value */
3625 int have_pdf
= 0, /* Have PDF? */
3626 have_raster
= 0;/* Have raster format support? */
3628 for (start
= value
; start
&& *start
; start
= next
)
3630 if (!_cups_strncasecmp(start
, "application/pdf", 15) && (!start
[15] || start
[15] == ','))
3635 else if ((!_cups_strncasecmp(start
, "image/pwg-raster", 16) && (!start
[16] || start
[16] == ',')) ||
3636 (!_cups_strncasecmp(start
, "image/urf", 9) && (!start
[9] || start
[9] == ',')))
3642 if ((next
= strchr(start
, ',')) != NULL
)
3646 if (!have_pdf
&& !have_raster
)
3647 device
->state
= _CUPS_DNSSD_INCOMPATIBLE
;
3649 else if (!_cups_strcasecmp(key
, "printer-type"))
3652 * Value is either NNNN or 0xXXXX
3655 saw_printer_type
= 1;
3656 type
= (cups_ptype_t
)strtol(value
, NULL
, 0) | CUPS_PRINTER_DISCOVERED
;
3658 else if (!saw_printer_type
)
3660 if (!_cups_strcasecmp(key
, "air") &&
3661 !_cups_strcasecmp(value
, "t"))
3662 type
|= CUPS_PRINTER_AUTHENTICATED
;
3663 else if (!_cups_strcasecmp(key
, "bind") &&
3664 !_cups_strcasecmp(value
, "t"))
3665 type
|= CUPS_PRINTER_BIND
;
3666 else if (!_cups_strcasecmp(key
, "collate") &&
3667 !_cups_strcasecmp(value
, "t"))
3668 type
|= CUPS_PRINTER_COLLATE
;
3669 else if (!_cups_strcasecmp(key
, "color") &&
3670 !_cups_strcasecmp(value
, "t"))
3671 type
|= CUPS_PRINTER_COLOR
;
3672 else if (!_cups_strcasecmp(key
, "copies") &&
3673 !_cups_strcasecmp(value
, "t"))
3674 type
|= CUPS_PRINTER_COPIES
;
3675 else if (!_cups_strcasecmp(key
, "duplex") &&
3676 !_cups_strcasecmp(value
, "t"))
3677 type
|= CUPS_PRINTER_DUPLEX
;
3678 else if (!_cups_strcasecmp(key
, "fax") &&
3679 !_cups_strcasecmp(value
, "t"))
3680 type
|= CUPS_PRINTER_MFP
;
3681 else if (!_cups_strcasecmp(key
, "papercustom") &&
3682 !_cups_strcasecmp(value
, "t"))
3683 type
|= CUPS_PRINTER_VARIABLE
;
3684 else if (!_cups_strcasecmp(key
, "papermax"))
3686 if (!_cups_strcasecmp(value
, "legal-a4"))
3687 type
|= CUPS_PRINTER_SMALL
;
3688 else if (!_cups_strcasecmp(value
, "isoc-a2"))
3689 type
|= CUPS_PRINTER_MEDIUM
;
3690 else if (!_cups_strcasecmp(value
, ">isoc-a2"))
3691 type
|= CUPS_PRINTER_LARGE
;
3693 else if (!_cups_strcasecmp(key
, "punch") &&
3694 !_cups_strcasecmp(value
, "t"))
3695 type
|= CUPS_PRINTER_PUNCH
;
3696 else if (!_cups_strcasecmp(key
, "scan") &&
3697 !_cups_strcasecmp(value
, "t"))
3698 type
|= CUPS_PRINTER_MFP
;
3699 else if (!_cups_strcasecmp(key
, "sort") &&
3700 !_cups_strcasecmp(value
, "t"))
3701 type
|= CUPS_PRINTER_SORT
;
3702 else if (!_cups_strcasecmp(key
, "staple") &&
3703 !_cups_strcasecmp(value
, "t"))
3704 type
|= CUPS_PRINTER_STAPLE
;
3709 * Save the printer-xxx values...
3712 if (make_and_model
[0])
3714 strlcat(make_and_model
, " ", sizeof(make_and_model
));
3715 strlcat(make_and_model
, model
, sizeof(make_and_model
));
3717 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", make_and_model
, device
->dest
.num_options
, &device
->dest
.options
);
3720 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", model
, device
->dest
.num_options
, &device
->dest
.options
);
3722 device
->type
= type
;
3723 snprintf(value
, sizeof(value
), "%u", type
);
3724 device
->dest
.num_options
= cupsAddOption("printer-type", value
, device
->dest
.num_options
, &device
->dest
.options
);
3730 cups_dnssd_unquote(uriname
, device
->fullName
, sizeof(uriname
));
3731 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
3732 !strcmp(device
->regtype
, "_ipps._tcp") ? "ipps" : "ipp",
3733 NULL
, uriname
, 0, saw_printer_type
? "/cups" : "/");
3735 DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri
));
3737 device
->dest
.num_options
= cupsAddOption("device-uri", uri
, device
->dest
.num_options
, &device
->dest
.options
);
3740 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3746 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3749 static const char * /* O - Resolved URI or NULL */
3751 cups_dest_t
*dest
, /* I - Destination */
3752 const char *uri
, /* I - Current printer URI */
3753 int msec
, /* I - Time in milliseconds */
3754 int *cancel
, /* I - Pointer to "cancel" variable */
3755 cups_dest_cb_t cb
, /* I - Callback */
3756 void *user_data
) /* I - User data for callback */
3758 char tempuri
[1024]; /* Temporary URI buffer */
3759 _cups_dnssd_resolve_t resolve
; /* Resolve data */
3763 * Resolve the URI...
3766 resolve
.cancel
= cancel
;
3767 gettimeofday(&resolve
.end_time
, NULL
);
3770 resolve
.end_time
.tv_sec
+= msec
/ 1000;
3771 resolve
.end_time
.tv_usec
+= (msec
% 1000) * 1000;
3773 while (resolve
.end_time
.tv_usec
>= 1000000)
3775 resolve
.end_time
.tv_sec
++;
3776 resolve
.end_time
.tv_usec
-= 1000000;
3780 resolve
.end_time
.tv_sec
+= 75;
3783 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
3785 if ((uri
= _httpResolveURI(uri
, tempuri
, sizeof(tempuri
), _HTTP_RESOLVE_DEFAULT
, cups_dnssd_resolve_cb
, &resolve
)) == NULL
)
3787 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to resolve printer-uri."), 1);
3790 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
3796 * Save the resolved URI...
3799 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &dest
->options
);
3801 return (cupsGetOption("device-uri", dest
->num_options
, dest
->options
));
3806 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3809 static int /* O - 1 to continue, 0 to stop */
3810 cups_dnssd_resolve_cb(void *context
) /* I - Resolve data */
3812 _cups_dnssd_resolve_t
*resolve
= (_cups_dnssd_resolve_t
*)context
;
3814 struct timeval curtime
; /* Current time */
3818 * If the cancel variable is set, return immediately.
3821 if (resolve
->cancel
&& *(resolve
->cancel
))
3823 DEBUG_puts("4cups_dnssd_resolve_cb: Canceled.");
3828 * Otherwise check the end time...
3831 gettimeofday(&curtime
, NULL
);
3833 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
));
3835 return (curtime
.tv_sec
< resolve
->end_time
.tv_sec
||
3836 (curtime
.tv_sec
== resolve
->end_time
.tv_sec
&&
3837 curtime
.tv_usec
< resolve
->end_time
.tv_usec
));
3842 * 'cups_dnssd_unquote()' - Unquote a name string.
3846 cups_dnssd_unquote(char *dst
, /* I - Destination buffer */
3847 const char *src
, /* I - Source string */
3848 size_t dstsize
) /* I - Size of destination buffer */
3850 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
3853 while (*src
&& dst
< dstend
)
3858 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
3859 isdigit(src
[2] & 255))
3861 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';
3873 #endif /* HAVE_DNSSD */
3876 #if defined(HAVE_AVAHI) || defined(HAVE_DNSSD)
3878 * 'cups_elapsed()' - Return the elapsed time in milliseconds.
3881 static int /* O - Elapsed time in milliseconds */
3882 cups_elapsed(struct timeval
*t
) /* IO - Previous time */
3884 int msecs
; /* Milliseconds */
3885 struct timeval nt
; /* New time */
3888 gettimeofday(&nt
, NULL
);
3890 msecs
= (int)(1000 * (nt
.tv_sec
- t
->tv_sec
) + (nt
.tv_usec
- t
->tv_usec
) / 1000);
3896 #endif /* HAVE_AVAHI || HAVE_DNSSD */
3900 * 'cups_find_dest()' - Find a destination using a binary search.
3903 static int /* O - Index of match */
3904 cups_find_dest(const char *name
, /* I - Destination name */
3905 const char *instance
, /* I - Instance or NULL */
3906 int num_dests
, /* I - Number of destinations */
3907 cups_dest_t
*dests
, /* I - Destinations */
3908 int prev
, /* I - Previous index */
3909 int *rdiff
) /* O - Difference of match */
3911 int left
, /* Low mark for binary search */
3912 right
, /* High mark for binary search */
3913 current
, /* Current index */
3914 diff
; /* Result of comparison */
3915 cups_dest_t key
; /* Search key */
3918 key
.name
= (char *)name
;
3919 key
.instance
= (char *)instance
;
3924 * Start search on either side of previous...
3927 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
3928 (diff
< 0 && prev
== 0) ||
3929 (diff
> 0 && prev
== (num_dests
- 1)))
3937 * Start with previous on right side...
3946 * Start wih previous on left side...
3950 right
= num_dests
- 1;
3956 * Start search in the middle...
3960 right
= num_dests
- 1;
3965 current
= (left
+ right
) / 2;
3966 diff
= cups_compare_dests(&key
, dests
+ current
);
3975 while ((right
- left
) > 1);
3980 * Check the last 1 or 2 elements...
3983 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
3987 diff
= cups_compare_dests(&key
, dests
+ right
);
3993 * Return the closest destination and the difference...
4003 * 'cups_get_cb()' - Collect enumerated destinations.
4006 static int /* O - 1 to continue, 0 to stop */
4007 cups_get_cb(_cups_getdata_t
*data
, /* I - Data from cupsGetDests */
4008 unsigned flags
, /* I - Enumeration flags */
4009 cups_dest_t
*dest
) /* I - Destination */
4011 if (flags
& CUPS_DEST_FLAGS_REMOVED
)
4014 * Remove destination from array...
4017 data
->num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
->num_dests
, &data
->dests
);
4022 * Add destination to array...
4025 data
->num_dests
= cupsCopyDest(dest
, data
->num_dests
, &data
->dests
);
4033 * 'cups_get_default()' - Get the default destination from an lpoptions file.
4036 static char * /* O - Default destination or NULL */
4037 cups_get_default(const char *filename
, /* I - File to read */
4038 char *namebuf
, /* I - Name buffer */
4039 size_t namesize
, /* I - Size of name buffer */
4040 const char **instance
) /* I - Instance */
4042 cups_file_t
*fp
; /* lpoptions file */
4043 char line
[8192], /* Line from file */
4044 *value
, /* Value for line */
4045 *nameptr
; /* Pointer into name */
4046 int linenum
; /* Current line */
4051 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
4055 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4057 if (!_cups_strcasecmp(line
, "default") && value
)
4059 strlcpy(namebuf
, value
, namesize
);
4061 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
4063 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
4066 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
4069 *instance
= nameptr
;
4077 return (*namebuf
? namebuf
: NULL
);
4082 * 'cups_get_dests()' - Get destinations from a file.
4085 static int /* O - Number of destinations */
4087 const char *filename
, /* I - File to read from */
4088 const char *match_name
, /* I - Destination name we want */
4089 const char *match_inst
, /* I - Instance name we want */
4090 int user_default_set
, /* I - User default printer set? */
4091 int num_dests
, /* I - Number of destinations */
4092 cups_dest_t
**dests
) /* IO - Destinations */
4094 int i
; /* Looping var */
4095 cups_dest_t
*dest
; /* Current destination */
4096 cups_file_t
*fp
; /* File pointer */
4097 char line
[8192], /* Line from file */
4098 *lineptr
, /* Pointer into line */
4099 *name
, /* Name of destination/option */
4100 *instance
; /* Instance of destination */
4101 int linenum
; /* Current line number */
4104 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
));
4107 * Try to open the file...
4110 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4114 * Read each printer; each line looks like:
4116 * Dest name[/instance] options
4117 * Default name[/instance] options
4122 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
4125 * See what type of line it is...
4128 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
4129 linenum
, line
, lineptr
));
4131 if ((_cups_strcasecmp(line
, "dest") && _cups_strcasecmp(line
, "default")) || !lineptr
)
4133 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
4140 * Search for an instance...
4143 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
4146 if (*lineptr
== '/')
4149 * Found an instance...
4156 * Search for an instance...
4159 while (!isspace(*lineptr
& 255) && *lineptr
)
4168 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
4172 * See if the primary instance of the destination exists; if not,
4173 * ignore this entry and move on...
4178 if (_cups_strcasecmp(name
, match_name
) ||
4179 (!instance
&& match_inst
) ||
4180 (instance
&& !match_inst
) ||
4181 (instance
&& _cups_strcasecmp(instance
, match_inst
)))
4186 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
4188 DEBUG_puts("9cups_get_dests: Not found!");
4194 * Add the destination...
4197 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
4199 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
4205 DEBUG_puts("9cups_get_dests: Out of memory!");
4211 * Add options until we hit the end of the line...
4214 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
4218 * If we found what we were looking for, stop now...
4225 * Set this as default if needed...
4228 if (!user_default_set
&& !_cups_strcasecmp(line
, "default"))
4230 DEBUG_puts("9cups_get_dests: Setting as default...");
4232 for (i
= 0; i
< num_dests
; i
++)
4233 (*dests
)[i
].is_default
= 0;
4235 dest
->is_default
= 1;
4240 * Close the file and return...
4250 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
4254 static char * /* O - New string */
4256 ipp_attribute_t
*attr
, /* I - Attribute to convert */
4257 char *buffer
, /* I - Buffer */
4258 size_t bufsize
) /* I - Size of buffer */
4260 int i
; /* Looping var */
4261 char *ptr
, /* Pointer into buffer */
4262 *end
, /* Pointer to end of buffer */
4263 *valptr
; /* Pointer into string attribute */
4267 * Return quickly if we have a single string value...
4270 if (attr
->num_values
== 1 &&
4271 attr
->value_tag
!= IPP_TAG_INTEGER
&&
4272 attr
->value_tag
!= IPP_TAG_ENUM
&&
4273 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
4274 attr
->value_tag
!= IPP_TAG_RANGE
)
4275 return (attr
->values
[0].string
.text
);
4278 * Copy the values to the string, separating with commas and escaping strings
4282 end
= buffer
+ bufsize
- 1;
4284 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
4289 switch (attr
->value_tag
)
4291 case IPP_TAG_INTEGER
:
4293 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].integer
);
4296 case IPP_TAG_BOOLEAN
:
4297 if (attr
->values
[i
].boolean
)
4298 strlcpy(ptr
, "true", (size_t)(end
- ptr
+ 1));
4300 strlcpy(ptr
, "false", (size_t)(end
- ptr
+ 1));
4303 case IPP_TAG_RANGE
:
4304 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
4305 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].range
.lower
);
4307 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d-%d", attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4311 for (valptr
= attr
->values
[i
].string
.text
;
4312 *valptr
&& ptr
< end
;)
4314 if (strchr(" \t\n\\\'\"", *valptr
))
4316 if (ptr
>= (end
- 1))
4339 * 'cups_name_cb()' - Find an enumerated destination.
4342 static int /* O - 1 to continue, 0 to stop */
4343 cups_name_cb(_cups_namedata_t
*data
, /* I - Data from cupsGetNamedDest */
4344 unsigned flags
, /* I - Enumeration flags */
4345 cups_dest_t
*dest
) /* I - Destination */
4347 DEBUG_printf(("2cups_name_cb(data=%p(%s), flags=%x, dest=%p(%s)", (void *)data
, data
->name
, flags
, (void *)dest
, dest
->name
));
4349 if (!(flags
& CUPS_DEST_FLAGS_REMOVED
) && !dest
->instance
&& !strcasecmp(data
->name
, dest
->name
))
4352 * Copy destination and stop enumeration...
4355 cupsCopyDest(dest
, 0, &data
->dest
);
4364 * 'cups_queue_name()' - Create a local queue name based on the service name.
4369 char *name
, /* I - Name buffer */
4370 const char *serviceName
, /* I - Service name */
4371 size_t namesize
) /* I - Size of name buffer */
4373 const char *ptr
; /* Pointer into serviceName */
4374 char *nameptr
; /* Pointer into name */
4377 for (nameptr
= name
, ptr
= serviceName
; *ptr
&& nameptr
< (name
+ namesize
- 1); ptr
++)
4380 * Sanitize the printer name...
4383 if (_cups_isalnum(*ptr
))
4385 else if (nameptr
== name
|| nameptr
[-1] != '_')