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 #endif /* HAVE_DNSSD || HAVE_AVAHI */
229 static int cups_elapsed(struct timeval
*t
);
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()' - Connect to the server for a destination.
575 * Connect to the destination, returning a new http_t connection object and
576 * optionally the resource path to use for the destination. These calls will
577 * block until a connection is made, the timeout expires, the integer pointed
578 * to by "cancel" is non-zero, or the callback function (or block) returns 0,
579 * The caller is responsible for calling httpClose() on the returned object.
581 * @since CUPS 1.6/macOS 10.8@
584 http_t
* /* O - Connection to server or @code NULL@ */
586 cups_dest_t
*dest
, /* I - Destination */
587 unsigned flags
, /* I - Connection flags */
588 int msec
, /* I - Timeout in milliseconds */
589 int *cancel
, /* I - Pointer to "cancel" variable */
590 char *resource
, /* I - Resource buffer */
591 size_t resourcesize
, /* I - Size of resource buffer */
592 cups_dest_cb_t cb
, /* I - Callback function */
593 void *user_data
) /* I - User data pointer */
595 const char *uri
; /* Printer URI */
596 char scheme
[32], /* URI scheme */
597 userpass
[256], /* Username and password (unused) */
598 hostname
[256], /* Hostname */
599 tempresource
[1024]; /* Temporary resource buffer */
600 int port
; /* Port number */
601 char portstr
[16]; /* Port number string */
602 http_encryption_t encryption
; /* Encryption to use */
603 http_addrlist_t
*addrlist
; /* Address list for server */
604 http_t
*http
; /* Connection to server */
607 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
));
610 * Range check input...
618 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
622 if (!resource
|| resourcesize
< 1)
624 resource
= tempresource
;
625 resourcesize
= sizeof(tempresource
);
629 * Grab the printer URI...
632 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
634 if ((uri
= cupsGetOption("resolved-device-uri", dest
->num_options
, dest
->options
)) == NULL
)
636 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
638 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
639 if (strstr(uri
, "._tcp"))
640 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
641 #endif /* HAVE_DNSSD || HAVE_AVAHI */
646 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, tempresource
, sizeof(tempresource
));
650 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
652 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
658 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
661 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
666 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
667 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
668 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
670 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
673 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
680 * Lookup the address for the server...
684 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
686 snprintf(portstr
, sizeof(portstr
), "%d", port
);
688 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portstr
)) == NULL
)
691 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
696 if (cancel
&& *cancel
)
698 httpAddrFreeList(addrlist
);
701 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CANCELED
, dest
);
707 * Create the HTTP object pointing to the server referenced by the URI...
710 if (!strcmp(scheme
, "ipps") || port
== 443)
711 encryption
= HTTP_ENCRYPTION_ALWAYS
;
713 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
715 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, encryption
, 1, 0, NULL
);
716 httpAddrFreeList(addrlist
);
719 * Connect if requested...
722 if (flags
& CUPS_DEST_FLAGS_UNCONNECTED
)
725 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
, dest
);
730 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
732 if (!httpReconnect2(http
, msec
, cancel
) && cb
)
734 if (cancel
&& *cancel
)
735 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
737 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
740 (*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
);
749 * 'cupsConnectDestBlock()' - Connect to the server for a destination.
751 * Connect to the destination, returning a new http_t connection object and
752 * optionally the resource path to use for the destination. These calls will
753 * block until a connection is made, the timeout expires, the integer pointed
754 * to by "cancel" is non-zero, or the callback function (or block) returns 0,
755 * The caller is responsible for calling httpClose() on the returned object.
757 * @since CUPS 1.6/macOS 10.8@
760 http_t
* /* O - Connection to server or @code NULL@ */
761 cupsConnectDestBlock(
762 cups_dest_t
*dest
, /* I - Destination */
763 unsigned flags
, /* I - Connection flags */
764 int msec
, /* I - Timeout in milliseconds */
765 int *cancel
, /* I - Pointer to "cancel" variable */
766 char *resource
, /* I - Resource buffer */
767 size_t resourcesize
, /* I - Size of resource buffer */
768 cups_dest_block_t block
) /* I - Callback block */
770 return (cupsConnectDest(dest
, flags
, msec
, cancel
, resource
, resourcesize
,
771 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
773 #endif /* __BLOCKS__ */
777 * 'cupsCopyDest()' - Copy a destination.
779 * Make a copy of the destination to an array of destinations (or just a single
780 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
781 * for calling cupsFreeDests() on the returned object(s).
783 * @since CUPS 1.6/macOS 10.8@
786 int /* O - New number of destinations */
787 cupsCopyDest(cups_dest_t
*dest
, /* I - Destination to copy */
788 int num_dests
, /* I - Number of destinations */
789 cups_dest_t
**dests
) /* IO - Destination array */
791 int i
; /* Looping var */
792 cups_dest_t
*new_dest
; /* New destination pointer */
793 cups_option_t
*new_option
, /* Current destination option */
794 *option
; /* Current parent option */
798 * Range check input...
801 if (!dest
|| num_dests
< 0 || !dests
)
805 * See if the destination already exists...
808 if ((new_dest
= cupsGetDest(dest
->name
, dest
->instance
, num_dests
,
812 * Protect against copying destination to itself...
815 if (new_dest
== dest
)
819 * Otherwise, free the options...
822 cupsFreeOptions(new_dest
->num_options
, new_dest
->options
);
824 new_dest
->num_options
= 0;
825 new_dest
->options
= NULL
;
828 new_dest
= cups_add_dest(dest
->name
, dest
->instance
, &num_dests
, dests
);
832 if ((new_dest
->options
= calloc(sizeof(cups_option_t
), (size_t)dest
->num_options
)) == NULL
)
833 return (cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
, dests
));
835 new_dest
->num_options
= dest
->num_options
;
837 for (i
= dest
->num_options
, option
= dest
->options
,
838 new_option
= new_dest
->options
;
840 i
--, option
++, new_option
++)
842 new_option
->name
= _cupsStrRetain(option
->name
);
843 new_option
->value
= _cupsStrRetain(option
->value
);
852 * '_cupsCreateDest()' - Create a local (temporary) queue.
855 char * /* O - Printer URI or @code NULL@ on error */
856 _cupsCreateDest(const char *name
, /* I - Printer name */
857 const char *info
, /* I - Printer description of @code NULL@ */
858 const char *device_id
, /* I - 1284 Device ID or @code NULL@ */
859 const char *device_uri
, /* I - Device URI */
860 char *uri
, /* I - Printer URI buffer */
861 size_t urisize
) /* I - Size of URI buffer */
863 http_t
*http
; /* Connection to server */
864 ipp_t
*request
, /* CUPS-Create-Local-Printer request */
865 *response
; /* CUPS-Create-Local-Printer response */
866 ipp_attribute_t
*attr
; /* printer-uri-supported attribute */
867 ipp_pstate_t state
= IPP_PSTATE_STOPPED
;
868 /* printer-state value */
871 if (!name
|| !device_uri
|| !uri
|| urisize
< 32)
874 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
)) == NULL
)
877 request
= ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER
);
879 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, "ipp://localhost/");
880 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
882 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
, device_uri
);
883 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
, name
);
885 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
, info
);
887 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
889 response
= cupsDoRequest(http
, request
, "/");
891 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
892 strlcpy(uri
, ippGetString(attr
, 0, NULL
), urisize
);
900 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
901 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
903 while (state
== IPP_PSTATE_STOPPED
&& cupsLastError() == IPP_STATUS_OK
)
908 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
910 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
911 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
912 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "printer-state");
914 response
= cupsDoRequest(http
, request
, "/");
916 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
917 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
929 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
931 * Destinations are enumerated from one or more sources. The callback function
932 * receives the @code user_data@ pointer and the destination pointer which can
933 * be used as input to the @link cupsCopyDest@ function. The function must
934 * return 1 to continue enumeration or 0 to stop.
936 * The @code type@ and @code mask@ arguments allow the caller to filter the
937 * destinations that are enumerated. Passing 0 for both will enumerate all
938 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
939 * destinations that are available but have not yet been added locally.
941 * Enumeration happens on the current thread and does not return until all
942 * destinations have been enumerated or the callback function returns 0.
944 * @since CUPS 1.6/macOS 10.8@
947 int /* O - 1 on success, 0 on failure */
949 unsigned flags
, /* I - Enumeration flags */
950 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
951 int *cancel
, /* I - Pointer to "cancel" variable */
952 cups_ptype_t type
, /* I - Printer type bits */
953 cups_ptype_t mask
, /* I - Mask for printer type bits */
954 cups_dest_cb_t cb
, /* I - Callback function */
955 void *user_data
) /* I - User data */
957 int i
, /* Looping var */
958 num_dests
; /* Number of destinations */
959 cups_dest_t
*dests
= NULL
, /* Destinations */
960 *dest
; /* Current destination */
961 const char *defprinter
; /* Default printer */
962 char name
[1024], /* Copy of printer name */
963 *instance
, /* Pointer to instance name */
964 *user_default
; /* User default printer */
965 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
966 int count
, /* Number of queries started */
967 completed
, /* Number of completed queries */
968 remaining
; /* Remainder of timeout */
969 struct timeval curtime
; /* Current time */
970 _cups_dnssd_data_t data
; /* Data for callback */
971 _cups_dnssd_device_t
*device
; /* Current device */
973 int nfds
, /* Number of files responded */
974 main_fd
; /* File descriptor for lookups */
975 DNSServiceRef ipp_ref
, /* IPP browser */
976 local_ipp_ref
; /* Local IPP browser */
978 DNSServiceRef ipps_ref
, /* IPPS browser */
979 local_ipps_ref
; /* Local IPPS browser */
980 # endif /* HAVE_SSL */
982 struct pollfd pfd
; /* Polling data */
984 fd_set input
; /* Input set for select() */
985 struct timeval timeout
; /* Timeout for select() */
986 # endif /* HAVE_POLL */
987 # else /* HAVE_AVAHI */
988 int error
; /* Error value */
989 AvahiServiceBrowser
*ipp_ref
; /* IPP browser */
991 AvahiServiceBrowser
*ipps_ref
; /* IPPS browser */
992 # endif /* HAVE_SSL */
993 # endif /* HAVE_DNSSD */
994 #endif /* HAVE_DNSSD || HAVE_AVAHI */
997 * Range check input...
1006 * Get ready to enumerate...
1009 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1013 data
.user_data
= user_data
;
1014 data
.devices
= cupsArrayNew3((cups_array_func_t
)cups_dnssd_compare_devices
, NULL
, NULL
, 0, NULL
, (cups_afree_func_t
)cups_dnssd_free_device
);
1015 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1017 if (!(mask
& CUPS_PRINTER_DISCOVERED
) || !(type
& CUPS_PRINTER_DISCOVERED
))
1020 * Get the list of local printers and pass them to the callback function...
1023 num_dests
= _cupsGetDests(CUPS_HTTP_DEFAULT
, IPP_OP_CUPS_GET_PRINTERS
, NULL
,
1024 &dests
, type
, mask
);
1026 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
1028 else if ((defprinter
= cupsGetDefault2(CUPS_HTTP_DEFAULT
)) != NULL
)
1030 strlcpy(name
, defprinter
, sizeof(name
));
1037 * Separate printer and instance name...
1040 if ((instance
= strchr(name
, '/')) != NULL
)
1044 * Lookup the printer and instance and make it the default...
1047 if ((dest
= cupsGetDest(name
, instance
, num_dests
, dests
)) != NULL
)
1048 dest
->is_default
= 1;
1051 for (i
= num_dests
, dest
= dests
;
1052 i
> 0 && (!cancel
|| !*cancel
);
1055 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1056 const char *device_uri
; /* Device URI */
1057 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1059 if (!(*cb
)(user_data
, i
> 1 ? CUPS_DEST_FLAGS_MORE
: CUPS_DEST_FLAGS_NONE
,
1063 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1064 if (!dest
->instance
&& (device_uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
&& !strncmp(device_uri
, "dnssd://", 8))
1067 * Add existing queue using service name, etc. so we don't list it again...
1070 char scheme
[32], /* URI scheme */
1071 userpass
[32], /* Username:password */
1072 serviceName
[256], /* Service name (host field) */
1073 resource
[256], /* Resource (options) */
1074 *regtype
, /* Registration type */
1075 *replyDomain
; /* Registration domain */
1076 int port
; /* Port number (not used) */
1078 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
)
1080 if ((regtype
= strstr(serviceName
, "._ipp")) != NULL
)
1084 if ((replyDomain
= strstr(regtype
, "._tcp.")) != NULL
)
1086 replyDomain
[5] = '\0';
1089 if ((device
= cups_dnssd_get_device(&data
, serviceName
, regtype
, replyDomain
)) != NULL
)
1090 device
->state
= _CUPS_DNSSD_ACTIVE
;
1095 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1098 cupsFreeDests(num_dests
, dests
);
1100 if (i
> 0 || msec
== 0)
1105 * Return early if the caller doesn't want to do discovery...
1108 if ((mask
& CUPS_PRINTER_DISCOVERED
) && !(type
& CUPS_PRINTER_DISCOVERED
))
1111 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1113 * Get Bonjour-shared printers...
1117 if (DNSServiceCreateConnection(&data
.main_ref
) != kDNSServiceErr_NoError
)
1120 main_fd
= DNSServiceRefSockFD(data
.main_ref
);
1122 ipp_ref
= data
.main_ref
;
1123 DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0,
1125 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
1127 local_ipp_ref
= data
.main_ref
;
1128 DNSServiceBrowse(&local_ipp_ref
, kDNSServiceFlagsShareConnection
,
1129 kDNSServiceInterfaceIndexLocalOnly
,
1131 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
1134 ipps_ref
= data
.main_ref
;
1135 DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0,
1137 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
1139 local_ipps_ref
= data
.main_ref
;
1140 DNSServiceBrowse(&local_ipps_ref
, kDNSServiceFlagsShareConnection
,
1141 kDNSServiceInterfaceIndexLocalOnly
,
1143 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
1144 # endif /* HAVE_SSL */
1146 # else /* HAVE_AVAHI */
1147 if ((data
.simple_poll
= avahi_simple_poll_new()) == NULL
)
1149 DEBUG_puts("cupsEnumDests: Unable to create Avahi simple poll object.");
1153 avahi_simple_poll_set_func(data
.simple_poll
, cups_dnssd_poll_cb
, &data
);
1155 data
.client
= avahi_client_new(avahi_simple_poll_get(data
.simple_poll
),
1156 0, cups_dnssd_client_cb
, &data
,
1160 DEBUG_puts("cupsEnumDests: Unable to create Avahi client.");
1161 avahi_simple_poll_free(data
.simple_poll
);
1166 ipp_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipp._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
);
1170 ipps_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipps._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
);
1171 # endif /* HAVE_SSL */
1172 # endif /* HAVE_DNSSD */
1175 remaining
= INT_MAX
;
1179 while (remaining
> 0 && (!cancel
|| !*cancel
))
1182 * Check for input...
1185 DEBUG_printf(("1cupsEnumDests: remaining=%d", remaining
));
1187 cups_elapsed(&curtime
);
1192 pfd
.events
= POLLIN
;
1194 nfds
= poll(&pfd
, 1, remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
1198 FD_SET(main_fd
, &input
);
1201 timeout
.tv_usec
= 1000 * (remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
1203 nfds
= select(main_fd
+ 1, &input
, NULL
, NULL
, &timeout
);
1204 # endif /* HAVE_POLL */
1207 DNSServiceProcessResult(data
.main_ref
);
1209 # else /* HAVE_AVAHI */
1212 if ((error
= avahi_simple_poll_iterate(data
.simple_poll
, _CUPS_DNSSD_MAXTIME
)) > 0)
1215 * We've been told to exit the loop. Perhaps the connection to
1222 DEBUG_printf(("1cupsEnumDests: got_data=%d", data
.got_data
));
1223 # endif /* HAVE_DNSSD */
1225 remaining
-= cups_elapsed(&curtime
);
1227 for (device
= (_cups_dnssd_device_t
*)cupsArrayFirst(data
.devices
),
1228 count
= 0, completed
= 0;
1230 device
= (_cups_dnssd_device_t
*)cupsArrayNext(data
.devices
))
1235 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
1238 if (!device
->ref
&& device
->state
== _CUPS_DNSSD_NEW
)
1240 DEBUG_printf(("1cupsEnumDests: Querying '%s'.", device
->fullName
));
1243 device
->ref
= data
.main_ref
;
1245 if (DNSServiceQueryRecord(&(device
->ref
),
1246 kDNSServiceFlagsShareConnection
,
1247 0, device
->fullName
,
1248 kDNSServiceType_TXT
,
1249 kDNSServiceClass_IN
,
1250 (DNSServiceQueryRecordReply
)cups_dnssd_query_cb
,
1251 &data
) == kDNSServiceErr_NoError
)
1258 device
->state
= _CUPS_DNSSD_ERROR
;
1260 DEBUG_puts("1cupsEnumDests: Query failed.");
1263 # else /* HAVE_AVAHI */
1264 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
)
1266 DEBUG_printf(("1cupsEnumDests: Query ref=%p", device
->ref
));
1271 device
->state
= _CUPS_DNSSD_ERROR
;
1273 DEBUG_printf(("1cupsEnumDests: Query failed: %s", avahi_strerror(avahi_client_errno(data
.client
))));
1275 # endif /* HAVE_DNSSD */
1277 else if (device
->ref
&& device
->state
== _CUPS_DNSSD_PENDING
)
1281 DEBUG_printf(("1cupsEnumDests: Query for \"%s\" is complete.", device
->fullName
));
1283 if ((device
->type
& mask
) == type
)
1285 DEBUG_printf(("1cupsEnumDests: Add callback for \"%s\".", device
->dest
.name
));
1286 if (!(*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, &device
->dest
))
1293 device
->state
= _CUPS_DNSSD_ACTIVE
;
1298 DEBUG_printf(("1cupsEnumDests: remaining=%d, browsers=%d, completed=%d, count=%d, devices count=%d", remaining
, data
.browsers
, completed
, count
, cupsArrayCount(data
.devices
)));
1300 if (data
.browsers
== 0 && completed
== cupsArrayCount(data
.devices
))
1303 DEBUG_printf(("1cupsEnumDests: remaining=%d, completed=%d, count=%d, devices count=%d", remaining
, completed
, count
, cupsArrayCount(data
.devices
)));
1305 if (completed
== cupsArrayCount(data
.devices
))
1307 # endif /* HAVE_AVAHI */
1311 DNSServiceRefDeallocate(ipp_ref
);
1312 DNSServiceRefDeallocate(local_ipp_ref
);
1315 DNSServiceRefDeallocate(ipp_ref
);
1316 DNSServiceRefDeallocate(local_ipp_ref
);
1317 # endif /* HAVE_SSL */
1319 DNSServiceRefDeallocate(data
.main_ref
);
1321 # else /* HAVE_AVAHI */
1322 avahi_service_browser_free(ipp_ref
);
1324 avahi_service_browser_free(ipps_ref
);
1325 # endif /* HAVE_SSL */
1327 avahi_client_free(data
.client
);
1328 avahi_simple_poll_free(data
.simple_poll
);
1329 # endif /* HAVE_DNSSD */
1330 #endif /* HAVE_DNSSD || HAVE_DNSSD */
1338 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1339 cupsArrayDelete(data
.devices
);
1340 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1348 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
1350 * Destinations are enumerated from one or more sources. The block receives the
1351 * @code user_data@ pointer and the destination pointer which can be used as
1352 * input to the @link cupsCopyDest@ function. The block must return 1 to
1353 * continue enumeration or 0 to stop.
1355 * The @code type@ and @code mask@ arguments allow the caller to filter the
1356 * destinations that are enumerated. Passing 0 for both will enumerate all
1357 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
1358 * destinations that are available but have not yet been added locally.
1360 * Enumeration happens on the current thread and does not return until all
1361 * destinations have been enumerated or the block returns 0.
1363 * @since CUPS 1.6/macOS 10.8@
1366 int /* O - 1 on success, 0 on failure */
1368 unsigned flags
, /* I - Enumeration flags */
1369 int timeout
, /* I - Timeout in milliseconds, 0 for indefinite */
1370 int *cancel
, /* I - Pointer to "cancel" variable */
1371 cups_ptype_t type
, /* I - Printer type bits */
1372 cups_ptype_t mask
, /* I - Mask for printer type bits */
1373 cups_dest_block_t block
) /* I - Block */
1375 return (cupsEnumDests(flags
, timeout
, cancel
, type
, mask
,
1376 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
1378 # endif /* __BLOCKS__ */
1382 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1386 cupsFreeDests(int num_dests
, /* I - Number of destinations */
1387 cups_dest_t
*dests
) /* I - Destinations */
1389 int i
; /* Looping var */
1390 cups_dest_t
*dest
; /* Current destination */
1393 if (num_dests
== 0 || dests
== NULL
)
1396 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1398 _cupsStrFree(dest
->name
);
1399 _cupsStrFree(dest
->instance
);
1401 cupsFreeOptions(dest
->num_options
, dest
->options
);
1409 * 'cupsGetDest()' - Get the named destination from the list.
1411 * Use the @link cupsGetDests@ or @link cupsGetDests2@ functions to get a
1412 * list of supported destinations for the current user.
1415 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
1416 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1417 const char *instance
, /* I - Instance name or @code NULL@ */
1418 int num_dests
, /* I - Number of destinations */
1419 cups_dest_t
*dests
) /* I - Destinations */
1421 int diff
, /* Result of comparison */
1422 match
; /* Matching index */
1425 if (num_dests
<= 0 || !dests
)
1431 * NULL name for default printer.
1434 while (num_dests
> 0)
1436 if (dests
->is_default
)
1446 * Lookup name and optionally the instance...
1449 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
1452 return (dests
+ match
);
1460 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1463 const char * /* O - Printer URI */
1464 _cupsGetDestResource(
1465 cups_dest_t
*dest
, /* I - Destination */
1466 char *resource
, /* I - Resource buffer */
1467 size_t resourcesize
) /* I - Size of resource buffer */
1469 const char *uri
; /* Printer URI */
1470 char scheme
[32], /* URI scheme */
1471 userpass
[256], /* Username and password (unused) */
1472 hostname
[256]; /* Hostname */
1473 int port
; /* Port number */
1476 DEBUG_printf(("_cupsGetDestResource(dest=%p(%s), resource=%p, resourcesize=%d)", dest
, dest
->name
, resource
, (int)resourcesize
));
1479 * Range check input...
1482 if (!dest
|| !resource
|| resourcesize
< 1)
1487 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1492 * Grab the printer URI...
1495 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
1497 if ((uri
= cupsGetOption("resolved-device-uri", dest
->num_options
, dest
->options
)) == NULL
)
1499 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
1501 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1502 if (strstr(uri
, "._tcp"))
1503 uri
= cups_dnssd_resolve(dest
, uri
, 5000, NULL
, NULL
, NULL
);
1504 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1510 DEBUG_printf(("1_cupsGetDestResource: Resolved printer-uri-supported=\"%s\"", uri
));
1512 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, resource
, resourcesize
);
1517 DEBUG_printf(("1_cupsGetDestResource: Local printer-uri-supported=\"%s\"", uri
));
1519 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
1521 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
1525 DEBUG_puts("1_cupsGetDestResource: No printer-uri-supported found.");
1530 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1537 DEBUG_printf(("1_cupsGetDestResource: printer-uri-supported=\"%s\"", uri
));
1539 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1540 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
1541 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
1543 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1549 DEBUG_printf(("1_cupsGetDestResource: resource=\"%s\"", resource
));
1556 * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
1558 * "name" is the desired name for the printer. If @code NULL@, a name will be
1559 * created using the URI.
1561 * "uri" is the "ipp" or "ipps" URI for the printer.
1563 * @since CUPS 2.0/macOS 10.10@
1566 cups_dest_t
* /* O - Destination or @code NULL@ */
1567 cupsGetDestWithURI(const char *name
, /* I - Desired printer name or @code NULL@ */
1568 const char *uri
) /* I - URI for the printer */
1570 cups_dest_t
*dest
; /* New destination */
1571 char temp
[1024], /* Temporary string */
1572 scheme
[256], /* Scheme from URI */
1573 userpass
[256], /* Username:password from URI */
1574 hostname
[256], /* Hostname from URI */
1575 resource
[1024], /* Resource path from URI */
1576 *ptr
; /* Pointer into string */
1577 const char *info
; /* printer-info string */
1578 int port
; /* Port number from URI */
1582 * Range check input...
1587 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1591 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
||
1592 (strncmp(uri
, "ipp://", 6) && strncmp(uri
, "ipps://", 7)))
1594 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1606 * Create the name from the URI...
1609 if (strstr(hostname
, "._tcp"))
1612 * Use the service instance name...
1615 if ((ptr
= strstr(hostname
, "._")) != NULL
)
1618 cups_queue_name(temp
, hostname
, sizeof(temp
));
1622 else if (!strncmp(resource
, "/classes/", 9))
1624 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 9, hostname
);
1625 name
= resource
+ 9;
1628 else if (!strncmp(resource
, "/printers/", 10))
1630 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 10, hostname
);
1631 name
= resource
+ 10;
1642 * Create the destination...
1645 if ((dest
= calloc(1, sizeof(cups_dest_t
))) == NULL
)
1647 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1651 dest
->name
= _cupsStrAlloc(name
);
1652 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &(dest
->options
));
1653 dest
->num_options
= cupsAddOption("printer-info", info
, dest
->num_options
, &(dest
->options
));
1660 * '_cupsGetDests()' - Get destinations from a server.
1662 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1663 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1666 * "name" is the name of an existing printer and is only used when "op" is
1667 * IPP_OP_GET_PRINTER_ATTRIBUTES.
1669 * "dest" is initialized to point to the array of destinations.
1671 * 0 is returned if there are no printers, no default printer, or the named
1672 * printer does not exist, respectively.
1674 * Free the memory used by the destination array using the @link cupsFreeDests@
1677 * Note: On macOS this function also gets the default paper from the system
1678 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1679 * options array for each destination that supports it.
1682 int /* O - Number of destinations */
1683 _cupsGetDests(http_t
*http
, /* I - Connection to server or
1684 * @code CUPS_HTTP_DEFAULT@ */
1685 ipp_op_t op
, /* I - IPP operation */
1686 const char *name
, /* I - Name of destination */
1687 cups_dest_t
**dests
, /* IO - Destinations */
1688 cups_ptype_t type
, /* I - Printer type bits */
1689 cups_ptype_t mask
) /* I - Printer type mask */
1691 int num_dests
= 0; /* Number of destinations */
1692 cups_dest_t
*dest
; /* Current destination */
1693 ipp_t
*request
, /* IPP Request */
1694 *response
; /* IPP Response */
1695 ipp_attribute_t
*attr
; /* Current attribute */
1696 const char *printer_name
; /* printer-name attribute */
1697 char uri
[1024]; /* printer-uri value */
1698 int num_options
; /* Number of options */
1699 cups_option_t
*options
; /* Options */
1701 char media_default
[41]; /* Default paper size */
1702 #endif /* __APPLE__ */
1703 char optname
[1024], /* Option name */
1704 value
[2048], /* Option value */
1705 *ptr
; /* Pointer into name/value */
1706 static const char * const pattrs
[] = /* Attributes we're interested in */
1708 "auth-info-required",
1710 "job-sheets-default",
1711 "marker-change-time",
1713 "marker-high-levels",
1715 "marker-low-levels",
1721 #endif /* __APPLE__ */
1725 "printer-is-accepting-jobs",
1726 "printer-is-shared",
1728 "printer-make-and-model",
1729 "printer-mandatory-job-attributes",
1732 "printer-state-change-time",
1733 "printer-state-reasons",
1735 "printer-uri-supported"
1739 DEBUG_printf(("_cupsGetDests(http=%p, op=%x(%s), name=\"%s\", dests=%p, type=%x, mask=%x)", http
, op
, ippOpString(op
), name
, dests
, type
, mask
));
1743 * Get the default paper size...
1746 appleGetPaperSize(media_default
, sizeof(media_default
));
1747 #endif /* __APPLE__ */
1750 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
1751 * require the following attributes:
1753 * attributes-charset
1754 * attributes-natural-language
1755 * requesting-user-name
1756 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
1759 request
= ippNewRequest(op
);
1761 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1762 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1765 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1766 "requesting-user-name", NULL
, cupsUser());
1768 if (name
&& op
!= IPP_OP_CUPS_GET_DEFAULT
)
1770 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1771 "localhost", ippPort(), "/printers/%s", name
);
1772 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1777 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type", (int)type
);
1778 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask", (int)mask
);
1782 * Do the request and get back a response...
1785 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1787 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1790 * Skip leading attributes until we hit a printer...
1793 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1800 * Pull the needed attributes from this printer...
1803 printer_name
= NULL
;
1807 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1809 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1810 attr
->value_tag
!= IPP_TAG_ENUM
&&
1811 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1812 attr
->value_tag
!= IPP_TAG_TEXT
&&
1813 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1814 attr
->value_tag
!= IPP_TAG_NAME
&&
1815 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1816 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1817 attr
->value_tag
!= IPP_TAG_RANGE
&&
1818 attr
->value_tag
!= IPP_TAG_URI
)
1821 if (!strcmp(attr
->name
, "auth-info-required") ||
1822 !strcmp(attr
->name
, "device-uri") ||
1823 !strcmp(attr
->name
, "marker-change-time") ||
1824 !strcmp(attr
->name
, "marker-colors") ||
1825 !strcmp(attr
->name
, "marker-high-levels") ||
1826 !strcmp(attr
->name
, "marker-levels") ||
1827 !strcmp(attr
->name
, "marker-low-levels") ||
1828 !strcmp(attr
->name
, "marker-message") ||
1829 !strcmp(attr
->name
, "marker-names") ||
1830 !strcmp(attr
->name
, "marker-types") ||
1831 !strcmp(attr
->name
, "printer-commands") ||
1832 !strcmp(attr
->name
, "printer-info") ||
1833 !strcmp(attr
->name
, "printer-is-shared") ||
1834 !strcmp(attr
->name
, "printer-make-and-model") ||
1835 !strcmp(attr
->name
, "printer-mandatory-job-attributes") ||
1836 !strcmp(attr
->name
, "printer-state") ||
1837 !strcmp(attr
->name
, "printer-state-change-time") ||
1838 !strcmp(attr
->name
, "printer-type") ||
1839 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1840 !strcmp(attr
->name
, "printer-location") ||
1841 !strcmp(attr
->name
, "printer-state-reasons") ||
1842 !strcmp(attr
->name
, "printer-uri-supported"))
1845 * Add a printer description attribute...
1848 num_options
= cupsAddOption(attr
->name
,
1849 cups_make_string(attr
, value
,
1851 num_options
, &options
);
1854 else if (!strcmp(attr
->name
, "media-supported"))
1857 * See if we can set a default media size...
1860 int i
; /* Looping var */
1862 for (i
= 0; i
< attr
->num_values
; i
++)
1863 if (!_cups_strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1865 num_options
= cupsAddOption("media", media_default
, num_options
,
1870 #endif /* __APPLE__ */
1871 else if (!strcmp(attr
->name
, "printer-name") &&
1872 attr
->value_tag
== IPP_TAG_NAME
)
1873 printer_name
= attr
->values
[0].string
.text
;
1874 else if (strncmp(attr
->name
, "notify-", 7) &&
1875 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1876 attr
->value_tag
== IPP_TAG_ENUM
||
1877 attr
->value_tag
== IPP_TAG_INTEGER
||
1878 attr
->value_tag
== IPP_TAG_KEYWORD
||
1879 attr
->value_tag
== IPP_TAG_NAME
||
1880 attr
->value_tag
== IPP_TAG_RANGE
) &&
1881 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1884 * Add a default option...
1887 strlcpy(optname
, attr
->name
, sizeof(optname
));
1888 optname
[ptr
- attr
->name
] = '\0';
1890 if (_cups_strcasecmp(optname
, "media") ||
1891 !cupsGetOption("media", num_options
, options
))
1892 num_options
= cupsAddOption(optname
,
1893 cups_make_string(attr
, value
,
1895 num_options
, &options
);
1900 * See if we have everything needed...
1905 cupsFreeOptions(num_options
, options
);
1913 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1915 dest
->num_options
= num_options
;
1916 dest
->options
= options
;
1919 cupsFreeOptions(num_options
, options
);
1925 ippDelete(response
);
1929 * Return the count...
1937 * 'cupsGetDests()' - Get the list of destinations from the default server.
1939 * Starting with CUPS 1.2, the returned list of destinations include the
1940 * printer-info, printer-is-accepting-jobs, printer-is-shared,
1941 * printer-make-and-model, printer-state, printer-state-change-time,
1942 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
1943 * adds the marker-change-time, marker-colors, marker-high-levels,
1944 * marker-levels, marker-low-levels, marker-message, marker-names,
1945 * marker-types, and printer-commands attributes as well.
1947 * Use the @link cupsFreeDests@ function to free the destination list and
1948 * the @link cupsGetDest@ function to find a particular destination.
1951 int /* O - Number of destinations */
1952 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
1954 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
1959 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
1961 * Starting with CUPS 1.2, the returned list of destinations include the
1962 * printer-info, printer-is-accepting-jobs, printer-is-shared,
1963 * printer-make-and-model, printer-state, printer-state-change-time,
1964 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
1965 * adds the marker-change-time, marker-colors, marker-high-levels,
1966 * marker-levels, marker-low-levels, marker-message, marker-names,
1967 * marker-types, and printer-commands attributes as well.
1969 * Use the @link cupsFreeDests@ function to free the destination list and
1970 * the @link cupsGetDest@ function to find a particular destination.
1972 * @since CUPS 1.1.21/macOS 10.4@
1975 int /* O - Number of destinations */
1976 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1977 cups_dest_t
**dests
) /* O - Destinations */
1979 _cups_getdata_t data
; /* Enumeration data */
1980 cups_dest_t
*dest
; /* Current destination */
1981 const char *home
; /* HOME environment variable */
1982 char filename
[1024]; /* Local ~/.cups/lpoptions file */
1983 const char *defprinter
; /* Default printer */
1984 char name
[1024], /* Copy of printer name */
1985 *instance
, /* Pointer to instance name */
1986 *user_default
; /* User default printer */
1987 int num_reals
; /* Number of real queues */
1988 cups_dest_t
*reals
; /* Real queues */
1989 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1993 * Range check the input...
1998 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad NULL dests pointer"), 1);
2003 * Grab the printers and classes...
2009 cupsEnumDests(0, 1000, NULL
, 0, 0, (cups_dest_cb_t
)cups_get_cb
, &data
);
2011 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2013 cupsFreeDests(data
.num_dests
, data
.dests
);
2015 *dests
= (cups_dest_t
*)0;
2021 * Make a copy of the "real" queues for a later sanity check...
2024 if (data
.num_dests
> 0)
2026 num_reals
= data
.num_dests
;
2027 reals
= calloc((size_t)num_reals
, sizeof(cups_dest_t
));
2030 memcpy(reals
, data
.dests
, (size_t)num_reals
* sizeof(cups_dest_t
));
2041 * Grab the default destination...
2044 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
2046 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
2048 strlcpy(name
, defprinter
, sizeof(name
));
2055 * Separate printer and instance name...
2058 if ((instance
= strchr(name
, '/')) != NULL
)
2062 * Lookup the printer and instance and make it the default...
2065 if ((dest
= cupsGetDest(name
, instance
, data
.num_dests
, data
.dests
)) != NULL
)
2066 dest
->is_default
= 1;
2072 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
2075 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2076 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
2078 if ((home
= getenv("HOME")) != NULL
)
2080 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2082 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
2086 * Validate the current default destination - this prevents old
2087 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
2088 * pointing to a non-existent printer or class...
2094 * See if we have a default printer...
2097 if ((dest
= cupsGetDest(NULL
, NULL
, data
.num_dests
, data
.dests
)) != NULL
)
2100 * Have a default; see if it is real...
2103 if (!cupsGetDest(dest
->name
, NULL
, num_reals
, reals
))
2106 * Remove the non-real printer from the list, since we don't want jobs
2107 * going to an unexpected printer... (<rdar://problem/14216472>)
2110 data
.num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
.num_dests
, &data
.dests
);
2122 * Return the number of destinations...
2125 *dests
= data
.dests
;
2127 if (data
.num_dests
> 0)
2128 _cupsSetError(IPP_STATUS_OK
, NULL
, 0);
2130 return (data
.num_dests
);
2135 * 'cupsGetNamedDest()' - Get options for the named destination.
2137 * This function is optimized for retrieving a single destination and should
2138 * be used instead of @link cupsGetDests@ and @link cupsGetDest@ when you either
2139 * know the name of the destination or want to print to the default destination.
2140 * If @code NULL@ is returned, the destination does not exist or there is no
2141 * default destination.
2143 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
2144 * server will be used.
2146 * If "name" is @code NULL@, the default printer for the current user will be
2149 * The returned destination must be freed using @link cupsFreeDests@ with a
2150 * "num_dests" value of 1.
2152 * @since CUPS 1.4/macOS 10.6@
2155 cups_dest_t
* /* O - Destination or @code NULL@ */
2156 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2157 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
2158 const char *instance
) /* I - Instance name or @code NULL@ */
2160 cups_dest_t
*dest
; /* Destination */
2161 char filename
[1024], /* Path to lpoptions */
2162 defname
[256]; /* Default printer name */
2163 const char *home
= getenv("HOME"); /* Home directory */
2164 int set_as_default
= 0; /* Set returned destination as default */
2165 ipp_op_t op
= IPP_OP_GET_PRINTER_ATTRIBUTES
;
2166 /* IPP operation to get server ops */
2167 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2170 DEBUG_printf(("cupsGetNamedDest(http=%p, name=\"%s\", instance=\"%s\")", http
, name
, instance
));
2173 * If "name" is NULL, find the default destination...
2179 name
= _cupsUserDefault(defname
, sizeof(defname
));
2183 char *ptr
; /* Temporary pointer... */
2185 if ((ptr
= strchr(defname
, '/')) != NULL
)
2196 * No default in the environment, try the user's lpoptions files...
2199 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2201 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
2207 * Still not there? Try the system lpoptions file...
2210 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
2211 cg
->cups_serverroot
);
2212 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
2218 * No locally-set default destination, ask the server...
2221 op
= IPP_OP_CUPS_GET_DEFAULT
;
2223 DEBUG_puts("1cupsGetNamedDest: Asking server for default printer...");
2226 DEBUG_printf(("1cupsGetNamedDest: Using name=\"%s\"...", name
));
2230 * Get the printer's attributes...
2233 if (!_cupsGetDests(http
, op
, name
, &dest
, 0, 0))
2237 _cups_namedata_t data
; /* Callback data */
2239 DEBUG_puts("1cupsGetNamedDest: No queue found for printer, looking on network...");
2244 cupsEnumDests(0, 1000, NULL
, 0, 0, (cups_dest_cb_t
)cups_name_cb
, &data
);
2255 DEBUG_printf(("1cupsGetNamedDest: Got dest=%p", dest
));
2258 dest
->instance
= _cupsStrAlloc(instance
);
2261 dest
->is_default
= 1;
2264 * Then add local options...
2267 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2268 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
2272 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2274 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
2278 * Return the result...
2286 * 'cupsRemoveDest()' - Remove a destination from the destination list.
2288 * Removing a destination/instance does not delete the class or printer
2289 * queue, merely the lpoptions for that destination/instance. Use the
2290 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
2291 * options for the user.
2293 * @since CUPS 1.3/macOS 10.5@
2296 int /* O - New number of destinations */
2297 cupsRemoveDest(const char *name
, /* I - Destination name */
2298 const char *instance
, /* I - Instance name or @code NULL@ */
2299 int num_dests
, /* I - Number of destinations */
2300 cups_dest_t
**dests
) /* IO - Destinations */
2302 int i
; /* Index into destinations */
2303 cups_dest_t
*dest
; /* Pointer to destination */
2307 * Find the destination...
2310 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
2317 _cupsStrFree(dest
->name
);
2318 _cupsStrFree(dest
->instance
);
2319 cupsFreeOptions(dest
->num_options
, dest
->options
);
2322 * Remove the destination from the array...
2327 i
= (int)(dest
- *dests
);
2330 memmove(dest
, dest
+ 1, (size_t)(num_dests
- i
) * sizeof(cups_dest_t
));
2337 * 'cupsSetDefaultDest()' - Set the default destination.
2339 * @since CUPS 1.3/macOS 10.5@
2344 const char *name
, /* I - Destination name */
2345 const char *instance
, /* I - Instance name or @code NULL@ */
2346 int num_dests
, /* I - Number of destinations */
2347 cups_dest_t
*dests
) /* I - Destinations */
2349 int i
; /* Looping var */
2350 cups_dest_t
*dest
; /* Current destination */
2354 * Range check input...
2357 if (!name
|| num_dests
<= 0 || !dests
)
2361 * Loop through the array and set the "is_default" flag for the matching
2365 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2366 dest
->is_default
= !_cups_strcasecmp(name
, dest
->name
) &&
2367 ((!instance
&& !dest
->instance
) ||
2368 (instance
&& dest
->instance
&&
2369 !_cups_strcasecmp(instance
, dest
->instance
)));
2374 * 'cupsSetDests()' - Save the list of destinations for the default server.
2376 * This function saves the destinations to /etc/cups/lpoptions when run
2377 * as root and ~/.cups/lpoptions when run as a normal user.
2381 cupsSetDests(int num_dests
, /* I - Number of destinations */
2382 cups_dest_t
*dests
) /* I - Destinations */
2384 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
2389 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
2391 * This function saves the destinations to /etc/cups/lpoptions when run
2392 * as root and ~/.cups/lpoptions when run as a normal user.
2394 * @since CUPS 1.1.21/macOS 10.4@
2397 int /* O - 0 on success, -1 on error */
2398 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2399 int num_dests
, /* I - Number of destinations */
2400 cups_dest_t
*dests
) /* I - Destinations */
2402 int i
, j
; /* Looping vars */
2403 int wrote
; /* Wrote definition? */
2404 cups_dest_t
*dest
; /* Current destination */
2405 cups_option_t
*option
; /* Current option */
2406 _ipp_option_t
*match
; /* Matching attribute for option */
2407 FILE *fp
; /* File pointer */
2409 const char *home
; /* HOME environment variable */
2411 char filename
[1024]; /* lpoptions file */
2412 int num_temps
; /* Number of temporary destinations */
2413 cups_dest_t
*temps
= NULL
, /* Temporary destinations */
2414 *temp
; /* Current temporary dest */
2415 const char *val
; /* Value of temporary option */
2416 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2420 * Range check the input...
2423 if (!num_dests
|| !dests
)
2427 * Get the server destinations...
2430 num_temps
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &temps
, 0, 0);
2432 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2434 cupsFreeDests(num_temps
, temps
);
2439 * Figure out which file to write to...
2442 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2448 * Merge in server defaults...
2451 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
2454 * Point to user defaults...
2457 if ((home
= getenv("HOME")) != NULL
)
2460 * Create ~/.cups subdirectory...
2463 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
2464 if (access(filename
, 0))
2465 mkdir(filename
, 0700);
2467 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2473 * Try to open the file...
2476 if ((fp
= fopen(filename
, "w")) == NULL
)
2478 cupsFreeDests(num_temps
, temps
);
2484 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2489 fchmod(fileno(fp
), 0644);
2493 * Write each printer; each line looks like:
2495 * Dest name[/instance] options
2496 * Default name[/instance] options
2499 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2500 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
2502 if (dest
->is_default
)
2504 fprintf(fp
, "Default %s", dest
->name
);
2506 fprintf(fp
, "/%s", dest
->instance
);
2513 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
2514 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
2516 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
2519 * See if this option is a printer attribute; if so, skip it...
2522 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
2523 match
->group_tag
== IPP_TAG_PRINTER
)
2527 * See if the server/global options match these; if so, don't
2532 (val
= cupsGetOption(option
->name
, temp
->num_options
,
2533 temp
->options
)) != NULL
&&
2534 !_cups_strcasecmp(val
, option
->value
))
2538 * Options don't match, write to the file...
2543 fprintf(fp
, "Dest %s", dest
->name
);
2545 fprintf(fp
, "/%s", dest
->instance
);
2549 if (option
->value
[0])
2551 if (strchr(option
->value
, ' ') ||
2552 strchr(option
->value
, '\\') ||
2553 strchr(option
->value
, '\"') ||
2554 strchr(option
->value
, '\''))
2557 * Quote the value...
2560 fprintf(fp
, " %s=\"", option
->name
);
2562 for (val
= option
->value
; *val
; val
++)
2564 if (strchr("\"\'\\", *val
))
2575 * Store the literal value...
2578 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
2582 fprintf(fp
, " %s", option
->name
);
2590 * Free the temporary destinations and close the file...
2593 cupsFreeDests(num_temps
, temps
);
2599 * Set the default printer for this location - this allows command-line
2600 * and GUI applications to share the same default destination...
2603 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
2605 CFStringRef name
= CFStringCreateWithCString(kCFAllocatorDefault
,
2607 kCFStringEncodingUTF8
);
2608 /* Default printer name */
2612 _cupsAppleSetDefaultPrinter(name
);
2616 #endif /* __APPLE__ */
2618 #ifdef HAVE_NOTIFY_POST
2620 * Send a notification so that macOS applications can know about the
2624 notify_post("com.apple.printerListChange");
2625 #endif /* HAVE_NOTIFY_POST */
2632 * '_cupsUserDefault()' - Get the user default printer from environment
2633 * variables and location information.
2636 char * /* O - Default printer or NULL */
2637 _cupsUserDefault(char *name
, /* I - Name buffer */
2638 size_t namesize
) /* I - Size of name buffer */
2640 const char *env
; /* LPDEST or PRINTER env variable */
2642 CFStringRef locprinter
; /* Last printer as this location */
2643 #endif /* __APPLE__ */
2646 if ((env
= getenv("LPDEST")) == NULL
)
2647 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
2652 strlcpy(name
, env
, namesize
);
2658 * Use location-based defaults if "use last printer" is selected in the
2659 * system preferences...
2662 if ((locprinter
= _cupsAppleCopyDefaultPrinter()) != NULL
)
2664 CFStringGetCString(locprinter
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
);
2665 CFRelease(locprinter
);
2670 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name
));
2672 return (*name
? name
: NULL
);
2676 * No location-based defaults on this platform...
2681 #endif /* __APPLE__ */
2685 #if _CUPS_LOCATION_DEFAULTS
2687 * 'appleCopyLocations()' - Copy the location history array.
2690 static CFArrayRef
/* O - Location array or NULL */
2691 appleCopyLocations(void)
2693 CFArrayRef locations
; /* Location array */
2697 * Look up the location array in the preferences...
2700 if ((locations
= CFPreferencesCopyAppValue(kLastUsedPrintersKey
,
2701 kCUPSPrintingPrefs
)) == NULL
)
2704 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
2706 CFRelease(locations
);
2715 * 'appleCopyNetwork()' - Get the network ID for the current location.
2718 static CFStringRef
/* O - Network ID */
2719 appleCopyNetwork(void)
2721 SCDynamicStoreRef dynamicStore
; /* System configuration data */
2722 CFStringRef key
; /* Current network configuration key */
2723 CFDictionaryRef ip_dict
; /* Network configuration data */
2724 CFStringRef network
= NULL
; /* Current network ID */
2727 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("libcups"), NULL
,
2731 * First use the IPv6 router address, if available, since that will generally
2732 * be a globally-unique link-local address.
2735 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2736 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv6
)) != NULL
)
2738 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2740 if ((network
= CFDictionaryGetValue(ip_dict
,
2741 kSCPropNetIPv6Router
)) != NULL
)
2751 * If that doesn't work, try the IPv4 router address. This isn't as unique
2752 * and will likely be a 10.x.y.z or 192.168.y.z address...
2757 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2758 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
2760 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2762 if ((network
= CFDictionaryGetValue(ip_dict
,
2763 kSCPropNetIPv4Router
)) != NULL
)
2773 CFRelease(dynamicStore
);
2778 #endif /* _CUPS_LOCATION_DEFAULTS */
2783 * 'appleGetPaperSize()' - Get the default paper size.
2786 static char * /* O - Default paper size */
2787 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
2788 size_t namesize
) /* I - Size of buffer */
2790 CFStringRef defaultPaperID
; /* Default paper ID */
2791 pwg_media_t
*pwgmedia
; /* PWG media size */
2794 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
2795 if (!defaultPaperID
||
2796 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
2797 !CFStringGetCString(defaultPaperID
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
))
2799 else if ((pwgmedia
= pwgMediaForLegacy(name
)) != NULL
)
2800 strlcpy(name
, pwgmedia
->pwg
, namesize
);
2803 CFRelease(defaultPaperID
);
2807 #endif /* __APPLE__ */
2810 #if _CUPS_LOCATION_DEFAULTS
2812 * 'appleGetPrinter()' - Get a printer from the history array.
2815 static CFStringRef
/* O - Printer name or NULL */
2816 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
2817 CFStringRef network
, /* I - Network name */
2818 CFIndex
*locindex
) /* O - Index in array */
2820 CFIndex i
, /* Looping var */
2821 count
; /* Number of locations */
2822 CFDictionaryRef location
; /* Current location */
2823 CFStringRef locnetwork
, /* Current network */
2824 locprinter
; /* Current printer */
2827 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
2828 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
2829 CFGetTypeID(location
) == CFDictionaryGetTypeID())
2831 if ((locnetwork
= CFDictionaryGetValue(location
,
2832 kLocationNetworkKey
)) != NULL
&&
2833 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
2834 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
2835 (locprinter
= CFDictionaryGetValue(location
,
2836 kLocationPrinterIDKey
)) != NULL
&&
2837 CFGetTypeID(locprinter
) == CFStringGetTypeID())
2842 return (locprinter
);
2848 #endif /* _CUPS_LOCATION_DEFAULTS */
2852 * 'cups_add_dest()' - Add a destination to the array.
2854 * Unlike cupsAddDest(), this function does not check for duplicates.
2857 static cups_dest_t
* /* O - New destination */
2858 cups_add_dest(const char *name
, /* I - Name of destination */
2859 const char *instance
, /* I - Instance or NULL */
2860 int *num_dests
, /* IO - Number of destinations */
2861 cups_dest_t
**dests
) /* IO - Destinations */
2863 int insert
, /* Insertion point */
2864 diff
; /* Result of comparison */
2865 cups_dest_t
*dest
; /* Destination pointer */
2869 * Add new destination...
2872 if (*num_dests
== 0)
2873 dest
= malloc(sizeof(cups_dest_t
));
2875 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (size_t)(*num_dests
+ 1));
2883 * Find where to insert the destination...
2886 if (*num_dests
== 0)
2890 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
2898 * Move the array elements as needed...
2901 if (insert
< *num_dests
)
2902 memmove(*dests
+ insert
+ 1, *dests
+ insert
, (size_t)(*num_dests
- insert
) * sizeof(cups_dest_t
));
2907 * Initialize the destination...
2910 dest
= *dests
+ insert
;
2911 dest
->name
= _cupsStrAlloc(name
);
2912 dest
->instance
= _cupsStrAlloc(instance
);
2913 dest
->is_default
= 0;
2914 dest
->num_options
= 0;
2915 dest
->options
= (cups_option_t
*)0;
2923 * 'cups_block_cb()' - Enumeration callback for block API.
2926 static int /* O - 1 to continue, 0 to stop */
2928 cups_dest_block_t block
, /* I - Block */
2929 unsigned flags
, /* I - Destination flags */
2930 cups_dest_t
*dest
) /* I - Destination */
2932 return ((block
)(flags
, dest
));
2934 # endif /* __BLOCKS__ */
2938 * 'cups_compare_dests()' - Compare two destinations.
2941 static int /* O - Result of comparison */
2942 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
2943 cups_dest_t
*b
) /* I - Second destination */
2945 int diff
; /* Difference */
2948 if ((diff
= _cups_strcasecmp(a
->name
, b
->name
)) != 0)
2950 else if (a
->instance
&& b
->instance
)
2951 return (_cups_strcasecmp(a
->instance
, b
->instance
));
2953 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
2957 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2960 * 'cups_dnssd_browse_cb()' - Browse for printers.
2964 cups_dnssd_browse_cb(
2965 DNSServiceRef sdRef
, /* I - Service reference */
2966 DNSServiceFlags flags
, /* I - Option flags */
2967 uint32_t interfaceIndex
, /* I - Interface number */
2968 DNSServiceErrorType errorCode
, /* I - Error, if any */
2969 const char *serviceName
, /* I - Name of service/device */
2970 const char *regtype
, /* I - Type of service */
2971 const char *replyDomain
, /* I - Service domain */
2972 void *context
) /* I - Enumeration data */
2974 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2975 /* Enumeration data */
2978 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
));
2981 * Don't do anything on error...
2984 if (errorCode
!= kDNSServiceErr_NoError
)
2991 cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
2995 # else /* HAVE_AVAHI */
2997 * 'cups_dnssd_browse_cb()' - Browse for printers.
3001 cups_dnssd_browse_cb(
3002 AvahiServiceBrowser
*browser
, /* I - Browser */
3003 AvahiIfIndex interface
, /* I - Interface index (unused) */
3004 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3005 AvahiBrowserEvent event
, /* I - What happened */
3006 const char *name
, /* I - Service name */
3007 const char *type
, /* I - Registration type */
3008 const char *domain
, /* I - Domain */
3009 AvahiLookupResultFlags flags
, /* I - Flags */
3010 void *context
) /* I - Devices array */
3013 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
3014 /* Client information */
3016 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3017 /* Enumeration data */
3024 DEBUG_printf(("cups_dnssd_browse_cb(..., name=\"%s\", type=\"%s\", domain=\"%s\", ...);", name
, type
, domain
));
3028 case AVAHI_BROWSER_FAILURE
:
3029 DEBUG_printf(("cups_dnssd_browse_cb: %s", avahi_strerror(avahi_client_errno(client
))));
3030 avahi_simple_poll_quit(data
->simple_poll
);
3033 case AVAHI_BROWSER_NEW
:
3035 * This object is new on the network.
3038 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
3041 * This comes from the local machine so ignore it.
3044 DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".", name
));
3049 * Create a device entry for it if it doesn't yet exist.
3052 cups_dnssd_get_device(data
, name
, type
, domain
);
3056 case AVAHI_BROWSER_REMOVE
:
3057 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
3060 case AVAHI_BROWSER_ALL_FOR_NOW
:
3061 DEBUG_puts("cups_dnssd_browse_cb: ALL_FOR_NOW");
3069 * 'cups_dnssd_client_cb()' - Avahi client callback function.
3073 cups_dnssd_client_cb(
3074 AvahiClient
*client
, /* I - Client information (unused) */
3075 AvahiClientState state
, /* I - Current state */
3076 void *context
) /* I - User data (unused) */
3078 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3079 /* Enumeration data */
3084 DEBUG_printf(("cups_dnssd_client_cb(client=%p, state=%d, context=%p)", client
, state
, context
));
3087 * If the connection drops, quit.
3090 if (state
== AVAHI_CLIENT_FAILURE
)
3092 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
3093 avahi_simple_poll_quit(data
->simple_poll
);
3096 # endif /* HAVE_DNSSD */
3100 * 'cups_dnssd_compare_device()' - Compare two devices.
3103 static int /* O - Result of comparison */
3104 cups_dnssd_compare_devices(
3105 _cups_dnssd_device_t
*a
, /* I - First device */
3106 _cups_dnssd_device_t
*b
) /* I - Second device */
3108 return (strcmp(a
->dest
.name
, b
->dest
.name
));
3113 * 'cups_dnssd_free_device()' - Free the memory used by a device.
3117 cups_dnssd_free_device(
3118 _cups_dnssd_device_t
*device
, /* I - Device */
3119 _cups_dnssd_data_t
*data
) /* I - Enumeration data */
3121 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", (void *)device
, device
->dest
.name
, (void *)data
));
3125 DNSServiceRefDeallocate(device
->ref
);
3126 # else /* HAVE_AVAHI */
3128 avahi_record_browser_free(device
->ref
);
3129 # endif /* HAVE_DNSSD */
3131 _cupsStrFree(device
->domain
);
3132 _cupsStrFree(device
->fullName
);
3133 _cupsStrFree(device
->regtype
);
3134 _cupsStrFree(device
->dest
.name
);
3136 cupsFreeOptions(device
->dest
.num_options
, device
->dest
.options
);
3143 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
3146 static _cups_dnssd_device_t
* /* O - Device */
3147 cups_dnssd_get_device(
3148 _cups_dnssd_data_t
*data
, /* I - Enumeration data */
3149 const char *serviceName
, /* I - Service name */
3150 const char *regtype
, /* I - Registration type */
3151 const char *replyDomain
) /* I - Domain name */
3153 _cups_dnssd_device_t key
, /* Search key */
3154 *device
; /* Device */
3155 char fullName
[kDNSServiceMaxDomainName
],
3156 /* Full name for query */
3157 name
[128]; /* Queue name */
3160 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\")", (void *)data
, serviceName
, regtype
, replyDomain
));
3163 * See if this is an existing device...
3166 cups_queue_name(name
, serviceName
, sizeof(name
));
3168 key
.dest
.name
= name
;
3170 if ((device
= cupsArrayFind(data
->devices
, &key
)) != NULL
)
3173 * Yes, see if we need to do anything with this...
3176 int update
= 0; /* Non-zero if we need to update */
3178 if (!_cups_strcasecmp(replyDomain
, "local.") &&
3179 _cups_strcasecmp(device
->domain
, replyDomain
))
3182 * Update the "global" listing to use the .local domain name instead.
3185 _cupsStrFree(device
->domain
);
3186 device
->domain
= _cupsStrAlloc(replyDomain
);
3188 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
3189 "domain.", device
->dest
.name
));
3194 if (!_cups_strcasecmp(regtype
, "_ipps._tcp") &&
3195 _cups_strcasecmp(device
->regtype
, regtype
))
3198 * Prefer IPPS over IPP.
3201 _cupsStrFree(device
->regtype
);
3202 device
->regtype
= _cupsStrAlloc(regtype
);
3204 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
3205 device
->dest
.name
));
3212 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
3213 device
->dest
.name
));
3220 * No, add the device...
3223 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
3224 "'%s'.", serviceName
,
3225 !strcmp(regtype
, "_ipps._tcp") ? "IPPS" : "IPP",
3228 device
= calloc(sizeof(_cups_dnssd_device_t
), 1);
3229 device
->dest
.name
= _cupsStrAlloc(name
);
3230 device
->domain
= _cupsStrAlloc(replyDomain
);
3231 device
->regtype
= _cupsStrAlloc(regtype
);
3233 device
->dest
.num_options
= cupsAddOption("printer-info", serviceName
, 0, &device
->dest
.options
);
3235 cupsArrayAdd(data
->devices
, device
);
3239 * Set the "full name" of this service, which is used for queries...
3243 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
3244 # else /* HAVE_AVAHI */
3245 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
, regtype
, replyDomain
);
3246 # endif /* HAVE_DNSSD */
3248 _cupsStrFree(device
->fullName
);
3249 device
->fullName
= _cupsStrAlloc(fullName
);
3254 DNSServiceRefDeallocate(device
->ref
);
3255 # else /* HAVE_AVAHI */
3256 avahi_record_browser_free(device
->ref
);
3257 # endif /* HAVE_DNSSD */
3262 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3264 DEBUG_printf(("6cups_dnssd_get_device: Remove callback for \"%s\".", device
->dest
.name
));
3266 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3267 device
->state
= _CUPS_DNSSD_NEW
;
3276 * 'cups_dnssd_local_cb()' - Browse for local printers.
3280 cups_dnssd_local_cb(
3281 DNSServiceRef sdRef
, /* I - Service reference */
3282 DNSServiceFlags flags
, /* I - Option flags */
3283 uint32_t interfaceIndex
, /* I - Interface number */
3284 DNSServiceErrorType errorCode
, /* I - Error, if any */
3285 const char *serviceName
, /* I - Name of service/device */
3286 const char *regtype
, /* I - Type of service */
3287 const char *replyDomain
, /* I - Service domain */
3288 void *context
) /* I - Devices array */
3290 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3291 /* Enumeration data */
3292 _cups_dnssd_device_t
*device
; /* Device */
3295 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
));
3298 * Only process "add" data...
3301 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3308 device
= cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
3311 * Hide locally-registered devices...
3314 DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.",
3319 DNSServiceRefDeallocate(device
->ref
);
3323 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3325 DEBUG_printf(("6cups_dnssd_local_cb: Remove callback for \"%s\".", device
->dest
.name
));
3326 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3329 device
->state
= _CUPS_DNSSD_LOCAL
;
3331 # endif /* HAVE_DNSSD */
3336 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
3338 * Note: This function is needed because avahi_simple_poll_iterate is broken
3339 * and always uses a timeout of 0 (!) milliseconds.
3340 * (Avahi Ticket #364)
3343 static int /* O - Number of file descriptors matching */
3345 struct pollfd
*pollfds
, /* I - File descriptors */
3346 unsigned int num_pollfds
, /* I - Number of file descriptors */
3347 int timeout
, /* I - Timeout in milliseconds (unused) */
3348 void *context
) /* I - User data (unused) */
3350 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3351 /* Enumeration data */
3352 int val
; /* Return value */
3355 DEBUG_printf(("cups_dnssd_poll_cb(pollfds=%p, num_pollfds=%d, timeout=%d, context=%p)", pollfds
, num_pollfds
, timeout
, context
));
3359 val
= poll(pollfds
, num_pollfds
, _CUPS_DNSSD_MAXTIME
);
3361 DEBUG_printf(("cups_dnssd_poll_cb: poll() returned %d", val
));
3365 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno
)));
3374 # endif /* HAVE_AVAHI */
3378 * 'cups_dnssd_query_cb()' - Process query data.
3383 cups_dnssd_query_cb(
3384 DNSServiceRef sdRef
, /* I - Service reference */
3385 DNSServiceFlags flags
, /* I - Data flags */
3386 uint32_t interfaceIndex
, /* I - Interface */
3387 DNSServiceErrorType errorCode
, /* I - Error, if any */
3388 const char *fullName
, /* I - Full service name */
3389 uint16_t rrtype
, /* I - Record type */
3390 uint16_t rrclass
, /* I - Record class */
3391 uint16_t rdlen
, /* I - Length of record data */
3392 const void *rdata
, /* I - Record data */
3393 uint32_t ttl
, /* I - Time-to-live */
3394 void *context
) /* I - Enumeration data */
3396 # else /* HAVE_AVAHI */
3398 cups_dnssd_query_cb(
3399 AvahiRecordBrowser
*browser
, /* I - Record browser */
3400 AvahiIfIndex interfaceIndex
,
3401 /* I - Interface index (unused) */
3402 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3403 AvahiBrowserEvent event
, /* I - What happened? */
3404 const char *fullName
, /* I - Service name */
3405 uint16_t rrclass
, /* I - Record class */
3406 uint16_t rrtype
, /* I - Record type */
3407 const void *rdata
, /* I - TXT record */
3408 size_t rdlen
, /* I - Length of TXT record */
3409 AvahiLookupResultFlags flags
, /* I - Flags */
3410 void *context
) /* I - Enumeration data */
3413 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
3414 /* Client information */
3416 # endif /* HAVE_DNSSD */
3417 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3418 /* Enumeration data */
3419 char serviceName
[256],/* Service name */
3420 name
[128], /* Queue name */
3421 *ptr
; /* Pointer into string */
3422 _cups_dnssd_device_t dkey
, /* Search key */
3423 *device
; /* Device */
3427 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
));
3430 * Only process "add" data...
3433 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3436 # else /* HAVE_AVAHI */
3437 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
));
3440 * Only process "add" data...
3443 if (event
!= AVAHI_BROWSER_NEW
)
3445 if (event
== AVAHI_BROWSER_FAILURE
)
3446 DEBUG_printf(("cups_dnssd_query_cb: %s", avahi_strerror(avahi_client_errno(client
))));
3450 # endif /* HAVE_DNSSD */
3453 * Lookup the service in the devices array.
3456 cups_dnssd_unquote(serviceName
, fullName
, sizeof(serviceName
));
3458 if ((ptr
= strstr(serviceName
, "._")) != NULL
)
3461 cups_queue_name(name
, serviceName
, sizeof(name
));
3463 dkey
.dest
.name
= name
;
3465 if ((device
= cupsArrayFind(data
->devices
, &dkey
)) != NULL
&& device
->state
== _CUPS_DNSSD_NEW
)
3468 * Found it, pull out the make and model from the TXT record and save it...
3471 const uint8_t *txt
, /* Pointer into data */
3472 *txtnext
, /* Next key/value pair */
3473 *txtend
; /* End of entire TXT record */
3474 uint8_t txtlen
; /* Length of current key/value pair */
3475 char key
[256], /* Key string */
3476 value
[256], /* Value string */
3477 make_and_model
[512],
3478 /* Manufacturer and model */
3479 model
[256], /* Model */
3480 uriname
[1024], /* Name for URI */
3481 uri
[1024]; /* Printer URI */
3482 cups_ptype_t type
= CUPS_PRINTER_DISCOVERED
| CUPS_PRINTER_BW
;
3484 int saw_printer_type
= 0;
3485 /* Did we see a printer-type key? */
3487 device
->state
= _CUPS_DNSSD_PENDING
;
3488 make_and_model
[0] = '\0';
3490 strlcpy(model
, "Unknown", sizeof(model
));
3492 for (txt
= rdata
, txtend
= txt
+ rdlen
;
3497 * Read a key/value pair starting with an 8-bit length. Since the
3498 * length is 8 bits and the size of the key/value buffers is 256, we
3499 * don't need to check for overflow...
3504 if (!txtlen
|| (txt
+ txtlen
) > txtend
)
3507 txtnext
= txt
+ txtlen
;
3509 for (ptr
= key
; txt
< txtnext
&& *txt
!= '='; txt
++)
3510 *ptr
++ = (char)*txt
;
3513 if (txt
< txtnext
&& *txt
== '=')
3518 memcpy(value
, txt
, (size_t)(txtnext
- txt
));
3519 value
[txtnext
- txt
] = '\0';
3521 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key
, value
));
3525 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key
));
3529 if (!_cups_strcasecmp(key
, "usb_MFG") ||
3530 !_cups_strcasecmp(key
, "usb_MANU") ||
3531 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
3532 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
3533 else if (!_cups_strcasecmp(key
, "usb_MDL") ||
3534 !_cups_strcasecmp(key
, "usb_MODEL"))
3535 strlcpy(model
, value
, sizeof(model
));
3536 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
3538 if (value
[0] == '(')
3541 * Strip parenthesis...
3544 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
3547 strlcpy(model
, value
+ 1, sizeof(model
));
3550 strlcpy(model
, value
, sizeof(model
));
3552 else if (!_cups_strcasecmp(key
, "ty"))
3554 strlcpy(model
, value
, sizeof(model
));
3556 if ((ptr
= strchr(model
, ',')) != NULL
)
3559 else if (!_cups_strcasecmp(key
, "note"))
3560 device
->dest
.num_options
= cupsAddOption("printer-location", value
,
3561 device
->dest
.num_options
,
3562 &device
->dest
.options
);
3563 else if (!_cups_strcasecmp(key
, "pdl"))
3566 * Look for PDF-capable printers; only PDF-capable printers are shown.
3569 const char *start
, *next
; /* Pointer into value */
3570 int have_pdf
= 0, /* Have PDF? */
3571 have_raster
= 0;/* Have raster format support? */
3573 for (start
= value
; start
&& *start
; start
= next
)
3575 if (!_cups_strncasecmp(start
, "application/pdf", 15) && (!start
[15] || start
[15] == ','))
3580 else if ((!_cups_strncasecmp(start
, "image/pwg-raster", 16) && (!start
[16] || start
[16] == ',')) ||
3581 (!_cups_strncasecmp(start
, "image/urf", 9) && (!start
[9] || start
[9] == ',')))
3587 if ((next
= strchr(start
, ',')) != NULL
)
3591 if (!have_pdf
&& !have_raster
)
3592 device
->state
= _CUPS_DNSSD_INCOMPATIBLE
;
3594 else if (!_cups_strcasecmp(key
, "printer-type"))
3597 * Value is either NNNN or 0xXXXX
3600 saw_printer_type
= 1;
3601 type
= (cups_ptype_t
)strtol(value
, NULL
, 0) | CUPS_PRINTER_DISCOVERED
;
3603 else if (!saw_printer_type
)
3605 if (!_cups_strcasecmp(key
, "air") &&
3606 !_cups_strcasecmp(value
, "t"))
3607 type
|= CUPS_PRINTER_AUTHENTICATED
;
3608 else if (!_cups_strcasecmp(key
, "bind") &&
3609 !_cups_strcasecmp(value
, "t"))
3610 type
|= CUPS_PRINTER_BIND
;
3611 else if (!_cups_strcasecmp(key
, "collate") &&
3612 !_cups_strcasecmp(value
, "t"))
3613 type
|= CUPS_PRINTER_COLLATE
;
3614 else if (!_cups_strcasecmp(key
, "color") &&
3615 !_cups_strcasecmp(value
, "t"))
3616 type
|= CUPS_PRINTER_COLOR
;
3617 else if (!_cups_strcasecmp(key
, "copies") &&
3618 !_cups_strcasecmp(value
, "t"))
3619 type
|= CUPS_PRINTER_COPIES
;
3620 else if (!_cups_strcasecmp(key
, "duplex") &&
3621 !_cups_strcasecmp(value
, "t"))
3622 type
|= CUPS_PRINTER_DUPLEX
;
3623 else if (!_cups_strcasecmp(key
, "fax") &&
3624 !_cups_strcasecmp(value
, "t"))
3625 type
|= CUPS_PRINTER_MFP
;
3626 else if (!_cups_strcasecmp(key
, "papercustom") &&
3627 !_cups_strcasecmp(value
, "t"))
3628 type
|= CUPS_PRINTER_VARIABLE
;
3629 else if (!_cups_strcasecmp(key
, "papermax"))
3631 if (!_cups_strcasecmp(value
, "legal-a4"))
3632 type
|= CUPS_PRINTER_SMALL
;
3633 else if (!_cups_strcasecmp(value
, "isoc-a2"))
3634 type
|= CUPS_PRINTER_MEDIUM
;
3635 else if (!_cups_strcasecmp(value
, ">isoc-a2"))
3636 type
|= CUPS_PRINTER_LARGE
;
3638 else if (!_cups_strcasecmp(key
, "punch") &&
3639 !_cups_strcasecmp(value
, "t"))
3640 type
|= CUPS_PRINTER_PUNCH
;
3641 else if (!_cups_strcasecmp(key
, "scan") &&
3642 !_cups_strcasecmp(value
, "t"))
3643 type
|= CUPS_PRINTER_MFP
;
3644 else if (!_cups_strcasecmp(key
, "sort") &&
3645 !_cups_strcasecmp(value
, "t"))
3646 type
|= CUPS_PRINTER_SORT
;
3647 else if (!_cups_strcasecmp(key
, "staple") &&
3648 !_cups_strcasecmp(value
, "t"))
3649 type
|= CUPS_PRINTER_STAPLE
;
3654 * Save the printer-xxx values...
3657 if (make_and_model
[0])
3659 strlcat(make_and_model
, " ", sizeof(make_and_model
));
3660 strlcat(make_and_model
, model
, sizeof(make_and_model
));
3662 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", make_and_model
, device
->dest
.num_options
, &device
->dest
.options
);
3665 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", model
, device
->dest
.num_options
, &device
->dest
.options
);
3667 device
->type
= type
;
3668 snprintf(value
, sizeof(value
), "%u", type
);
3669 device
->dest
.num_options
= cupsAddOption("printer-type", value
, device
->dest
.num_options
, &device
->dest
.options
);
3675 cups_dnssd_unquote(uriname
, device
->fullName
, sizeof(uriname
));
3676 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
3677 !strcmp(device
->regtype
, "_ipps._tcp") ? "ipps" : "ipp",
3678 NULL
, uriname
, 0, saw_printer_type
? "/cups" : "/");
3680 DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri
));
3682 device
->dest
.num_options
= cupsAddOption("device-uri", uri
, device
->dest
.num_options
, &device
->dest
.options
);
3685 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3691 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3694 static const char * /* O - Resolved URI or NULL */
3696 cups_dest_t
*dest
, /* I - Destination */
3697 const char *uri
, /* I - Current printer URI */
3698 int msec
, /* I - Time in milliseconds */
3699 int *cancel
, /* I - Pointer to "cancel" variable */
3700 cups_dest_cb_t cb
, /* I - Callback */
3701 void *user_data
) /* I - User data for callback */
3703 char tempuri
[1024]; /* Temporary URI buffer */
3704 _cups_dnssd_resolve_t resolve
; /* Resolve data */
3708 * Resolve the URI...
3711 resolve
.cancel
= cancel
;
3712 gettimeofday(&resolve
.end_time
, NULL
);
3715 resolve
.end_time
.tv_sec
+= msec
/ 1000;
3716 resolve
.end_time
.tv_usec
+= (msec
% 1000) * 1000;
3718 while (resolve
.end_time
.tv_usec
>= 1000000)
3720 resolve
.end_time
.tv_sec
++;
3721 resolve
.end_time
.tv_usec
-= 1000000;
3725 resolve
.end_time
.tv_sec
+= 75;
3728 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
3730 if ((uri
= _httpResolveURI(uri
, tempuri
, sizeof(tempuri
), _HTTP_RESOLVE_DEFAULT
, cups_dnssd_resolve_cb
, &resolve
)) == NULL
)
3732 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to resolve printer-uri."), 1);
3735 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
3741 * Save the resolved URI...
3744 dest
->num_options
= cupsAddOption("resolved-device-uri", uri
, dest
->num_options
, &dest
->options
);
3746 return (cupsGetOption("resolved-device-uri", dest
->num_options
, dest
->options
));
3751 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3754 static int /* O - 1 to continue, 0 to stop */
3755 cups_dnssd_resolve_cb(void *context
) /* I - Resolve data */
3757 _cups_dnssd_resolve_t
*resolve
= (_cups_dnssd_resolve_t
*)context
;
3759 struct timeval curtime
; /* Current time */
3763 * If the cancel variable is set, return immediately.
3766 if (resolve
->cancel
&& *(resolve
->cancel
))
3768 DEBUG_puts("4cups_dnssd_resolve_cb: Canceled.");
3773 * Otherwise check the end time...
3776 gettimeofday(&curtime
, NULL
);
3778 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
));
3780 return (curtime
.tv_sec
< resolve
->end_time
.tv_sec
||
3781 (curtime
.tv_sec
== resolve
->end_time
.tv_sec
&&
3782 curtime
.tv_usec
< resolve
->end_time
.tv_usec
));
3787 * 'cups_dnssd_unquote()' - Unquote a name string.
3791 cups_dnssd_unquote(char *dst
, /* I - Destination buffer */
3792 const char *src
, /* I - Source string */
3793 size_t dstsize
) /* I - Size of destination buffer */
3795 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
3798 while (*src
&& dst
< dstend
)
3803 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
3804 isdigit(src
[2] & 255))
3806 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';
3818 #endif /* HAVE_DNSSD */
3822 * 'cups_elapsed()' - Return the elapsed time in milliseconds.
3825 static int /* O - Elapsed time in milliseconds */
3826 cups_elapsed(struct timeval
*t
) /* IO - Previous time */
3828 int msecs
; /* Milliseconds */
3829 struct timeval nt
; /* New time */
3832 gettimeofday(&nt
, NULL
);
3834 msecs
= 1000 * (nt
.tv_sec
- t
->tv_sec
) + (nt
.tv_usec
- t
->tv_usec
) / 1000;
3843 * 'cups_find_dest()' - Find a destination using a binary search.
3846 static int /* O - Index of match */
3847 cups_find_dest(const char *name
, /* I - Destination name */
3848 const char *instance
, /* I - Instance or NULL */
3849 int num_dests
, /* I - Number of destinations */
3850 cups_dest_t
*dests
, /* I - Destinations */
3851 int prev
, /* I - Previous index */
3852 int *rdiff
) /* O - Difference of match */
3854 int left
, /* Low mark for binary search */
3855 right
, /* High mark for binary search */
3856 current
, /* Current index */
3857 diff
; /* Result of comparison */
3858 cups_dest_t key
; /* Search key */
3861 key
.name
= (char *)name
;
3862 key
.instance
= (char *)instance
;
3867 * Start search on either side of previous...
3870 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
3871 (diff
< 0 && prev
== 0) ||
3872 (diff
> 0 && prev
== (num_dests
- 1)))
3880 * Start with previous on right side...
3889 * Start wih previous on left side...
3893 right
= num_dests
- 1;
3899 * Start search in the middle...
3903 right
= num_dests
- 1;
3908 current
= (left
+ right
) / 2;
3909 diff
= cups_compare_dests(&key
, dests
+ current
);
3918 while ((right
- left
) > 1);
3923 * Check the last 1 or 2 elements...
3926 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
3930 diff
= cups_compare_dests(&key
, dests
+ right
);
3936 * Return the closest destination and the difference...
3946 * 'cups_get_cb()' - Collect enumerated destinations.
3949 static int /* O - 1 to continue, 0 to stop */
3950 cups_get_cb(_cups_getdata_t
*data
, /* I - Data from cupsGetDests */
3951 unsigned flags
, /* I - Enumeration flags */
3952 cups_dest_t
*dest
) /* I - Destination */
3954 if (flags
& CUPS_DEST_FLAGS_REMOVED
)
3957 * Remove destination from array...
3960 data
->num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
->num_dests
, &data
->dests
);
3965 * Add destination to array...
3968 data
->num_dests
= cupsCopyDest(dest
, data
->num_dests
, &data
->dests
);
3976 * 'cups_get_default()' - Get the default destination from an lpoptions file.
3979 static char * /* O - Default destination or NULL */
3980 cups_get_default(const char *filename
, /* I - File to read */
3981 char *namebuf
, /* I - Name buffer */
3982 size_t namesize
, /* I - Size of name buffer */
3983 const char **instance
) /* I - Instance */
3985 cups_file_t
*fp
; /* lpoptions file */
3986 char line
[8192], /* Line from file */
3987 *value
, /* Value for line */
3988 *nameptr
; /* Pointer into name */
3989 int linenum
; /* Current line */
3994 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
3998 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4000 if (!_cups_strcasecmp(line
, "default") && value
)
4002 strlcpy(namebuf
, value
, namesize
);
4004 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
4006 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
4009 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
4012 *instance
= nameptr
;
4020 return (*namebuf
? namebuf
: NULL
);
4025 * 'cups_get_dests()' - Get destinations from a file.
4028 static int /* O - Number of destinations */
4030 const char *filename
, /* I - File to read from */
4031 const char *match_name
, /* I - Destination name we want */
4032 const char *match_inst
, /* I - Instance name we want */
4033 int user_default_set
, /* I - User default printer set? */
4034 int num_dests
, /* I - Number of destinations */
4035 cups_dest_t
**dests
) /* IO - Destinations */
4037 int i
; /* Looping var */
4038 cups_dest_t
*dest
; /* Current destination */
4039 cups_file_t
*fp
; /* File pointer */
4040 char line
[8192], /* Line from file */
4041 *lineptr
, /* Pointer into line */
4042 *name
, /* Name of destination/option */
4043 *instance
; /* Instance of destination */
4044 int linenum
; /* Current line number */
4047 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
));
4050 * Try to open the file...
4053 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4057 * Read each printer; each line looks like:
4059 * Dest name[/instance] options
4060 * Default name[/instance] options
4065 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
4068 * See what type of line it is...
4071 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
4072 linenum
, line
, lineptr
));
4074 if ((_cups_strcasecmp(line
, "dest") && _cups_strcasecmp(line
, "default")) || !lineptr
)
4076 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
4083 * Search for an instance...
4086 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
4089 if (*lineptr
== '/')
4092 * Found an instance...
4099 * Search for an instance...
4102 while (!isspace(*lineptr
& 255) && *lineptr
)
4111 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
4115 * See if the primary instance of the destination exists; if not,
4116 * ignore this entry and move on...
4121 if (_cups_strcasecmp(name
, match_name
) ||
4122 (!instance
&& match_inst
) ||
4123 (instance
&& !match_inst
) ||
4124 (instance
&& _cups_strcasecmp(instance
, match_inst
)))
4129 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
4131 DEBUG_puts("9cups_get_dests: Not found!");
4137 * Add the destination...
4140 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
4142 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
4148 DEBUG_puts("9cups_get_dests: Out of memory!");
4154 * Add options until we hit the end of the line...
4157 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
4161 * If we found what we were looking for, stop now...
4168 * Set this as default if needed...
4171 if (!user_default_set
&& !_cups_strcasecmp(line
, "default"))
4173 DEBUG_puts("9cups_get_dests: Setting as default...");
4175 for (i
= 0; i
< num_dests
; i
++)
4176 (*dests
)[i
].is_default
= 0;
4178 dest
->is_default
= 1;
4183 * Close the file and return...
4193 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
4197 static char * /* O - New string */
4199 ipp_attribute_t
*attr
, /* I - Attribute to convert */
4200 char *buffer
, /* I - Buffer */
4201 size_t bufsize
) /* I - Size of buffer */
4203 int i
; /* Looping var */
4204 char *ptr
, /* Pointer into buffer */
4205 *end
, /* Pointer to end of buffer */
4206 *valptr
; /* Pointer into string attribute */
4210 * Return quickly if we have a single string value...
4213 if (attr
->num_values
== 1 &&
4214 attr
->value_tag
!= IPP_TAG_INTEGER
&&
4215 attr
->value_tag
!= IPP_TAG_ENUM
&&
4216 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
4217 attr
->value_tag
!= IPP_TAG_RANGE
)
4218 return (attr
->values
[0].string
.text
);
4221 * Copy the values to the string, separating with commas and escaping strings
4225 end
= buffer
+ bufsize
- 1;
4227 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
4232 switch (attr
->value_tag
)
4234 case IPP_TAG_INTEGER
:
4236 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].integer
);
4239 case IPP_TAG_BOOLEAN
:
4240 if (attr
->values
[i
].boolean
)
4241 strlcpy(ptr
, "true", (size_t)(end
- ptr
+ 1));
4243 strlcpy(ptr
, "false", (size_t)(end
- ptr
+ 1));
4246 case IPP_TAG_RANGE
:
4247 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
4248 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].range
.lower
);
4250 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d-%d", attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4254 for (valptr
= attr
->values
[i
].string
.text
;
4255 *valptr
&& ptr
< end
;)
4257 if (strchr(" \t\n\\\'\"", *valptr
))
4259 if (ptr
>= (end
- 1))
4282 * 'cups_name_cb()' - Find an enumerated destination.
4285 static int /* O - 1 to continue, 0 to stop */
4286 cups_name_cb(_cups_namedata_t
*data
, /* I - Data from cupsGetNamedDest */
4287 unsigned flags
, /* I - Enumeration flags */
4288 cups_dest_t
*dest
) /* I - Destination */
4290 DEBUG_printf(("2cups_name_cb(data=%p(%s), flags=%x, dest=%p(%s)", data
, data
->name
, flags
, dest
, dest
->name
));
4292 if (!(flags
& CUPS_DEST_FLAGS_REMOVED
) && !dest
->instance
&& !strcasecmp(data
->name
, dest
->name
))
4295 * Copy destination and stop enumeration...
4298 cupsCopyDest(dest
, 0, &data
->dest
);
4307 * 'cups_queue_name()' - Create a local queue name based on the service name.
4312 char *name
, /* I - Name buffer */
4313 const char *serviceName
, /* I - Service name */
4314 size_t namesize
) /* I - Size of name buffer */
4316 const char *ptr
; /* Pointer into serviceName */
4317 char *nameptr
; /* Pointer into name */
4320 for (nameptr
= name
, ptr
= serviceName
; *ptr
&& nameptr
< (name
+ namesize
- 1); ptr
++)
4323 * Sanitize the printer name...
4326 if (_cups_isalnum(*ptr
))
4328 else if (nameptr
== name
|| nameptr
[-1] != '_')