2 * User-defined destination (and option) support for CUPS.
4 * Copyright 2007-2016 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 */
128 #if _CUPS_LOCATION_DEFAULTS
129 static CFArrayRef
appleCopyLocations(void);
130 static CFStringRef
appleCopyNetwork(void);
131 #endif /* _CUPS_LOCATION_DEFAULTS */
133 static char *appleGetPaperSize(char *name
, size_t namesize
);
134 #endif /* __APPLE__ */
135 #if _CUPS_LOCATION_DEFAULTS
136 static CFStringRef
appleGetPrinter(CFArrayRef locations
,
137 CFStringRef network
, CFIndex
*locindex
);
138 #endif /* _CUPS_LOCATION_DEFAULTS */
139 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
140 int *num_dests
, cups_dest_t
**dests
);
142 static int cups_block_cb(cups_dest_block_t block
, unsigned flags
,
144 #endif /* __BLOCKS__ */
145 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
146 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
148 static void cups_dnssd_browse_cb(DNSServiceRef sdRef
,
149 DNSServiceFlags flags
,
150 uint32_t interfaceIndex
,
151 DNSServiceErrorType errorCode
,
152 const char *serviceName
,
154 const char *replyDomain
,
156 # else /* HAVE_AVAHI */
157 static void cups_dnssd_browse_cb(AvahiServiceBrowser
*browser
,
158 AvahiIfIndex interface
,
159 AvahiProtocol protocol
,
160 AvahiBrowserEvent event
,
161 const char *serviceName
,
163 const char *replyDomain
,
164 AvahiLookupResultFlags flags
,
166 static void cups_dnssd_client_cb(AvahiClient
*client
,
167 AvahiClientState state
,
169 # endif /* HAVE_DNSSD */
170 static int cups_dnssd_compare_devices(_cups_dnssd_device_t
*a
,
171 _cups_dnssd_device_t
*b
);
172 static void cups_dnssd_free_device(_cups_dnssd_device_t
*device
,
173 _cups_dnssd_data_t
*data
);
174 static _cups_dnssd_device_t
*
175 cups_dnssd_get_device(_cups_dnssd_data_t
*data
,
176 const char *serviceName
,
178 const char *replyDomain
);
180 static void cups_dnssd_local_cb(DNSServiceRef sdRef
,
181 DNSServiceFlags flags
,
182 uint32_t interfaceIndex
,
183 DNSServiceErrorType errorCode
,
184 const char *serviceName
,
186 const char *replyDomain
,
188 static void cups_dnssd_query_cb(DNSServiceRef sdRef
,
189 DNSServiceFlags flags
,
190 uint32_t interfaceIndex
,
191 DNSServiceErrorType errorCode
,
192 const char *fullName
,
193 uint16_t rrtype
, uint16_t rrclass
,
194 uint16_t rdlen
, const void *rdata
,
195 uint32_t ttl
, void *context
);
196 # else /* HAVE_AVAHI */
197 static int cups_dnssd_poll_cb(struct pollfd
*pollfds
,
198 unsigned int num_pollfds
,
199 int timeout
, void *context
);
200 static void cups_dnssd_query_cb(AvahiRecordBrowser
*browser
,
201 AvahiIfIndex interface
,
202 AvahiProtocol protocol
,
203 AvahiBrowserEvent event
,
204 const char *name
, uint16_t rrclass
,
205 uint16_t rrtype
, const void *rdata
,
207 AvahiLookupResultFlags flags
,
209 # endif /* HAVE_DNSSD */
210 static const char *cups_dnssd_resolve(cups_dest_t
*dest
, const char *uri
,
211 int msec
, int *cancel
,
212 cups_dest_cb_t cb
, void *user_data
);
213 static int cups_dnssd_resolve_cb(void *context
);
214 static void cups_dnssd_unquote(char *dst
, const char *src
,
216 #endif /* HAVE_DNSSD || HAVE_AVAHI */
217 static int cups_elapsed(struct timeval
*t
);
218 static int cups_find_dest(const char *name
, const char *instance
,
219 int num_dests
, cups_dest_t
*dests
, int prev
,
221 static char *cups_get_default(const char *filename
, char *namebuf
,
222 size_t namesize
, const char **instance
);
223 static int cups_get_dests(const char *filename
, const char *match_name
,
224 const char *match_inst
, int user_default_set
,
225 int num_dests
, cups_dest_t
**dests
);
226 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
228 static void cups_queue_name(char *name
, const char *serviceName
, size_t namesize
);
232 * 'cupsAddDest()' - Add a destination to the list of destinations.
234 * This function cannot be used to add a new class or printer queue,
235 * it only adds a new container of saved options for the named
236 * destination or instance.
238 * If the named destination already exists, the destination list is
239 * returned unchanged. Adding a new instance of a destination creates
240 * a copy of that destination's options.
242 * Use the @link cupsSaveDests@ function to save the updated list of
243 * destinations to the user's lpoptions file.
246 int /* O - New number of destinations */
247 cupsAddDest(const char *name
, /* I - Destination name */
248 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
249 int num_dests
, /* I - Number of destinations */
250 cups_dest_t
**dests
) /* IO - Destinations */
252 int i
; /* Looping var */
253 cups_dest_t
*dest
; /* Destination pointer */
254 cups_dest_t
*parent
= NULL
; /* Parent destination */
255 cups_option_t
*doption
, /* Current destination option */
256 *poption
; /* Current parent option */
262 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
264 if (instance
&& !cupsGetDest(name
, NULL
, num_dests
, *dests
))
267 if ((dest
= cups_add_dest(name
, instance
, &num_dests
, dests
)) == NULL
)
271 * Find the base dest again now the array has been realloc'd.
274 parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
);
276 if (instance
&& parent
&& parent
->num_options
> 0)
279 * Copy options from parent...
282 dest
->options
= calloc(sizeof(cups_option_t
), (size_t)parent
->num_options
);
286 dest
->num_options
= parent
->num_options
;
288 for (i
= dest
->num_options
, doption
= dest
->options
,
289 poption
= parent
->options
;
291 i
--, doption
++, poption
++)
293 doption
->name
= _cupsStrRetain(poption
->name
);
294 doption
->value
= _cupsStrRetain(poption
->value
);
306 * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
309 CFStringRef
/* O - Default paper ID */
310 _cupsAppleCopyDefaultPaperID(void)
312 return (CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
313 kCUPSPrintingPrefs
));
318 * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location.
321 CFStringRef
/* O - Default printer name */
322 _cupsAppleCopyDefaultPrinter(void)
324 # if _CUPS_LOCATION_DEFAULTS
325 CFStringRef network
; /* Network location */
326 CFArrayRef locations
; /* Location array */
327 CFStringRef locprinter
; /* Current printer */
331 * Use location-based defaults only if "use last printer" is selected in the
332 * system preferences...
335 if (!_cupsAppleGetUseLastPrinter())
337 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as "
343 * Get the current location...
346 if ((network
= appleCopyNetwork()) == NULL
)
348 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current "
354 * Lookup the network in the preferences...
357 if ((locations
= appleCopyLocations()) == NULL
)
360 * Missing or bad location array, so no location-based default...
363 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used "
371 DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.",
372 (int)CFArrayGetCount(locations
)));
374 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
375 CFRetain(locprinter
);
378 CFRelease(locations
);
384 # endif /* _CUPS_LOCATION_DEFAULTS */
389 * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
392 int /* O - 1 to use last printer, 0 otherwise */
393 _cupsAppleGetUseLastPrinter(void)
395 Boolean uselast
, /* Use last printer preference value */
396 uselast_set
; /* Valid is set? */
399 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
402 uselast
= CFPreferencesGetAppBooleanValue(kUseLastPrinter
,
413 * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
417 _cupsAppleSetDefaultPaperID(
418 CFStringRef name
) /* I - New paper ID */
420 CFPreferencesSetAppValue(kDefaultPaperIDKey
, name
, kCUPSPrintingPrefs
);
421 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
423 # ifdef HAVE_NOTIFY_POST
424 notify_post("com.apple.printerPrefsChange");
425 # endif /* HAVE_NOTIFY_POST */
430 * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location.
434 _cupsAppleSetDefaultPrinter(
435 CFStringRef name
) /* I - Default printer/class name */
437 # if _CUPS_LOCATION_DEFAULTS
438 CFStringRef network
; /* Current network */
439 CFArrayRef locations
; /* Old locations array */
440 CFIndex locindex
; /* Index in locations array */
441 CFStringRef locprinter
; /* Current printer */
442 CFMutableArrayRef newlocations
; /* New locations array */
443 CFMutableDictionaryRef newlocation
; /* New location */
447 * Get the current location...
450 if ((network
= appleCopyNetwork()) == NULL
)
452 DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network...");
457 * Lookup the network in the preferences...
460 if ((locations
= appleCopyLocations()) != NULL
)
461 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
468 if (!locprinter
|| CFStringCompare(locprinter
, name
, 0) != kCFCompareEqualTo
)
471 * Need to change the locations array...
476 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
480 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
483 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
484 &kCFTypeArrayCallBacks
);
486 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
487 &kCFTypeDictionaryKeyCallBacks
,
488 &kCFTypeDictionaryValueCallBacks
);
490 if (newlocation
&& newlocations
)
493 * Put the new location at the front of the array...
496 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
497 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, name
);
498 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
501 * Limit the number of locations to 10...
504 while (CFArrayGetCount(newlocations
) > 10)
505 CFArrayRemoveValueAtIndex(newlocations
, 10);
508 * Push the changes out...
511 CFPreferencesSetAppValue(kLastUsedPrintersKey
, newlocations
,
513 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
515 # ifdef HAVE_NOTIFY_POST
516 notify_post("com.apple.printerPrefsChange");
517 # endif /* HAVE_NOTIFY_POST */
521 CFRelease(newlocations
);
524 CFRelease(newlocation
);
528 CFRelease(locations
);
534 # endif /* _CUPS_LOCATION_DEFAULTS */
539 * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
543 _cupsAppleSetUseLastPrinter(
544 int uselast
) /* O - 1 to use last printer, 0 otherwise */
546 CFPreferencesSetAppValue(kUseLastPrinter
,
547 uselast
? kCFBooleanTrue
: kCFBooleanFalse
,
549 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
551 # ifdef HAVE_NOTIFY_POST
552 notify_post("com.apple.printerPrefsChange");
553 # endif /* HAVE_NOTIFY_POST */
555 #endif /* __APPLE__ */
559 * 'cupsConnectDest()' - Connect to the server for a destination.
561 * Connect to the destination, returning a new http_t connection object and
562 * optionally the resource path to use for the destination. These calls will
563 * block until a connection is made, the timeout expires, the integer pointed
564 * to by "cancel" is non-zero, or the callback function (or block) returns 0,
565 * The caller is responsible for calling httpClose() on the returned object.
567 * @since CUPS 1.6/macOS 10.8@
570 http_t
* /* O - Connection to server or @code NULL@ */
572 cups_dest_t
*dest
, /* I - Destination */
573 unsigned flags
, /* I - Connection flags */
574 int msec
, /* I - Timeout in milliseconds */
575 int *cancel
, /* I - Pointer to "cancel" variable */
576 char *resource
, /* I - Resource buffer */
577 size_t resourcesize
, /* I - Size of resource buffer */
578 cups_dest_cb_t cb
, /* I - Callback function */
579 void *user_data
) /* I - User data pointer */
581 const char *uri
; /* Printer URI */
582 char scheme
[32], /* URI scheme */
583 userpass
[256], /* Username and password (unused) */
584 hostname
[256], /* Hostname */
585 tempresource
[1024]; /* Temporary resource buffer */
586 int port
; /* Port number */
587 char portstr
[16]; /* Port number string */
588 http_encryption_t encryption
; /* Encryption to use */
589 http_addrlist_t
*addrlist
; /* Address list for server */
590 http_t
*http
; /* Connection to server */
593 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
));
596 * Range check input...
604 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
608 if (!resource
|| resourcesize
< 1)
610 resource
= tempresource
;
611 resourcesize
= sizeof(tempresource
);
615 * Grab the printer URI...
618 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
620 if ((uri
= cupsGetOption("resolved-device-uri", dest
->num_options
, dest
->options
)) == NULL
)
622 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
624 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
625 if (strstr(uri
, "._tcp"))
626 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
627 #endif /* HAVE_DNSSD || HAVE_AVAHI */
632 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, tempresource
, sizeof(tempresource
));
636 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
638 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
644 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
647 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
652 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
653 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
654 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
656 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
659 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
666 * Lookup the address for the server...
670 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
672 snprintf(portstr
, sizeof(portstr
), "%d", port
);
674 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portstr
)) == NULL
)
677 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
682 if (cancel
&& *cancel
)
684 httpAddrFreeList(addrlist
);
687 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CANCELED
, dest
);
693 * Create the HTTP object pointing to the server referenced by the URI...
696 if (!strcmp(scheme
, "ipps") || port
== 443)
697 encryption
= HTTP_ENCRYPTION_ALWAYS
;
699 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
701 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, encryption
, 1, 0, NULL
);
702 httpAddrFreeList(addrlist
);
705 * Connect if requested...
708 if (flags
& CUPS_DEST_FLAGS_UNCONNECTED
)
711 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
, dest
);
716 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
718 if (!httpReconnect2(http
, msec
, cancel
) && cb
)
720 if (cancel
&& *cancel
)
721 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
723 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
726 (*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
);
735 * 'cupsConnectDestBlock()' - Connect to the server for a destination.
737 * Connect to the destination, returning a new http_t connection object and
738 * optionally the resource path to use for the destination. These calls will
739 * block until a connection is made, the timeout expires, the integer pointed
740 * to by "cancel" is non-zero, or the callback function (or block) returns 0,
741 * The caller is responsible for calling httpClose() on the returned object.
743 * @since CUPS 1.6/macOS 10.8@
746 http_t
* /* O - Connection to server or @code NULL@ */
747 cupsConnectDestBlock(
748 cups_dest_t
*dest
, /* I - Destination */
749 unsigned flags
, /* I - Connection flags */
750 int msec
, /* I - Timeout in milliseconds */
751 int *cancel
, /* I - Pointer to "cancel" variable */
752 char *resource
, /* I - Resource buffer */
753 size_t resourcesize
, /* I - Size of resource buffer */
754 cups_dest_block_t block
) /* I - Callback block */
756 return (cupsConnectDest(dest
, flags
, msec
, cancel
, resource
, resourcesize
,
757 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
759 #endif /* __BLOCKS__ */
763 * 'cupsCopyDest()' - Copy a destination.
765 * Make a copy of the destination to an array of destinations (or just a single
766 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
767 * for calling cupsFreeDests() on the returned object(s).
769 * @since CUPS 1.6/macOS 10.8@
772 int /* O - New number of destinations */
773 cupsCopyDest(cups_dest_t
*dest
, /* I - Destination to copy */
774 int num_dests
, /* I - Number of destinations */
775 cups_dest_t
**dests
) /* IO - Destination array */
777 int i
; /* Looping var */
778 cups_dest_t
*new_dest
; /* New destination pointer */
779 cups_option_t
*new_option
, /* Current destination option */
780 *option
; /* Current parent option */
784 * Range check input...
787 if (!dest
|| num_dests
< 0 || !dests
)
791 * See if the destination already exists...
794 if ((new_dest
= cupsGetDest(dest
->name
, dest
->instance
, num_dests
,
798 * Protect against copying destination to itself...
801 if (new_dest
== dest
)
805 * Otherwise, free the options...
808 cupsFreeOptions(new_dest
->num_options
, new_dest
->options
);
810 new_dest
->num_options
= 0;
811 new_dest
->options
= NULL
;
814 new_dest
= cups_add_dest(dest
->name
, dest
->instance
, &num_dests
, dests
);
818 if ((new_dest
->options
= calloc(sizeof(cups_option_t
), (size_t)dest
->num_options
)) == NULL
)
819 return (cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
, dests
));
821 new_dest
->num_options
= dest
->num_options
;
823 for (i
= dest
->num_options
, option
= dest
->options
,
824 new_option
= new_dest
->options
;
826 i
--, option
++, new_option
++)
828 new_option
->name
= _cupsStrRetain(option
->name
);
829 new_option
->value
= _cupsStrRetain(option
->value
);
838 * '_cupsCreateDest()' - Create a local (temporary) queue.
841 char * /* O - Printer URI or @code NULL@ on error */
842 _cupsCreateDest(const char *name
, /* I - Printer name */
843 const char *info
, /* I - Printer description of @code NULL@ */
844 const char *device_id
, /* I - 1284 Device ID or @code NULL@ */
845 const char *device_uri
, /* I - Device URI */
846 char *uri
, /* I - Printer URI buffer */
847 size_t urisize
) /* I - Size of URI buffer */
849 http_t
*http
; /* Connection to server */
850 ipp_t
*request
, /* CUPS-Create-Local-Printer request */
851 *response
; /* CUPS-Create-Local-Printer response */
852 ipp_attribute_t
*attr
; /* printer-uri-supported attribute */
853 ipp_pstate_t state
= IPP_PSTATE_STOPPED
;
854 /* printer-state value */
857 if (!name
|| !device_uri
|| !uri
|| urisize
< 32)
860 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
)) == NULL
)
863 request
= ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER
);
865 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, "ipp://localhost/");
866 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
868 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
, device_uri
);
869 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
, name
);
871 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
, info
);
873 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
875 response
= cupsDoRequest(http
, request
, "/");
877 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
878 strlcpy(uri
, ippGetString(attr
, 0, NULL
), urisize
);
886 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
887 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
889 while (state
== IPP_PSTATE_STOPPED
&& cupsLastError() == IPP_STATUS_OK
)
894 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
896 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
897 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
898 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "printer-state");
900 response
= cupsDoRequest(http
, request
, "/");
902 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
903 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
915 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
917 * Destinations are enumerated from one or more sources. The callback function
918 * receives the @code user_data@ pointer, destination name, instance, number of
919 * options, and options which can be used as input to the @link cupsAddDest@
920 * function. The function must return 1 to continue enumeration or 0 to stop.
922 * Enumeration happens on the current thread and does not return until all
923 * destinations have been enumerated or the callback function returns 0.
925 * @since CUPS 1.6/macOS 10.8@
928 int /* O - 1 on success, 0 on failure */
930 unsigned flags
, /* I - Enumeration flags */
931 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
932 int *cancel
, /* I - Pointer to "cancel" variable */
933 cups_ptype_t type
, /* I - Printer type bits */
934 cups_ptype_t mask
, /* I - Mask for printer type bits */
935 cups_dest_cb_t cb
, /* I - Callback function */
936 void *user_data
) /* I - User data */
938 int i
, /* Looping var */
939 num_dests
; /* Number of destinations */
940 cups_dest_t
*dests
= NULL
, /* Destinations */
941 *dest
; /* Current destination */
942 const char *defprinter
; /* Default printer */
943 char name
[1024], /* Copy of printer name */
944 *instance
, /* Pointer to instance name */
945 *user_default
; /* User default printer */
946 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
947 int count
, /* Number of queries started */
948 completed
, /* Number of completed queries */
949 remaining
; /* Remainder of timeout */
950 struct timeval curtime
; /* Current time */
951 _cups_dnssd_data_t data
; /* Data for callback */
952 _cups_dnssd_device_t
*device
; /* Current device */
954 int nfds
, /* Number of files responded */
955 main_fd
; /* File descriptor for lookups */
956 DNSServiceRef ipp_ref
, /* IPP browser */
957 local_ipp_ref
; /* Local IPP browser */
959 DNSServiceRef ipps_ref
, /* IPPS browser */
960 local_ipps_ref
; /* Local IPPS browser */
961 # endif /* HAVE_SSL */
963 struct pollfd pfd
; /* Polling data */
965 fd_set input
; /* Input set for select() */
966 struct timeval timeout
; /* Timeout for select() */
967 # endif /* HAVE_POLL */
968 # else /* HAVE_AVAHI */
969 int error
; /* Error value */
970 AvahiServiceBrowser
*ipp_ref
; /* IPP browser */
972 AvahiServiceBrowser
*ipps_ref
; /* IPPS browser */
973 # endif /* HAVE_SSL */
974 # endif /* HAVE_DNSSD */
975 #endif /* HAVE_DNSSD || HAVE_AVAHI */
978 * Range check input...
987 * Get the list of local printers and pass them to the callback function...
990 num_dests
= _cupsGetDests(CUPS_HTTP_DEFAULT
, IPP_OP_CUPS_GET_PRINTERS
, NULL
,
991 &dests
, type
, mask
| CUPS_PRINTER_3D
);
993 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
995 else if ((defprinter
= cupsGetDefault2(CUPS_HTTP_DEFAULT
)) != NULL
)
997 strlcpy(name
, defprinter
, sizeof(name
));
1004 * Separate printer and instance name...
1007 if ((instance
= strchr(name
, '/')) != NULL
)
1011 * Lookup the printer and instance and make it the default...
1014 if ((dest
= cupsGetDest(name
, instance
, num_dests
, dests
)) != NULL
)
1015 dest
->is_default
= 1;
1018 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1022 data
.user_data
= user_data
;
1023 data
.devices
= cupsArrayNew3((cups_array_func_t
)cups_dnssd_compare_devices
, NULL
, NULL
, 0, NULL
, (cups_afree_func_t
)cups_dnssd_free_device
);
1024 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1026 for (i
= num_dests
, dest
= dests
;
1027 i
> 0 && (!cancel
|| !*cancel
);
1030 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1031 const char *device_uri
; /* Device URI */
1032 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1034 if (!(*cb
)(user_data
, i
> 1 ? CUPS_DEST_FLAGS_MORE
: CUPS_DEST_FLAGS_NONE
,
1038 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1039 if (!dest
->instance
&& (device_uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
&& !strncmp(device_uri
, "dnssd://", 8))
1042 * Add existing queue using service name, etc. so we don't list it again...
1045 char scheme
[32], /* URI scheme */
1046 userpass
[32], /* Username:password */
1047 serviceName
[256], /* Service name (host field) */
1048 resource
[256], /* Resource (options) */
1049 *regtype
, /* Registration type */
1050 *replyDomain
; /* Registration domain */
1051 int port
; /* Port number (not used) */
1053 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
)
1055 if ((regtype
= strstr(serviceName
, "._ipp")) != NULL
)
1059 if ((replyDomain
= strstr(regtype
, "._tcp.")) != NULL
)
1061 replyDomain
[5] = '\0';
1064 if ((device
= cups_dnssd_get_device(&data
, serviceName
, regtype
, replyDomain
)) != NULL
)
1065 device
->state
= _CUPS_DNSSD_ACTIVE
;
1070 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1073 cupsFreeDests(num_dests
, dests
);
1075 if (i
> 0 || msec
== 0)
1077 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1078 cupsArrayDelete(data
.devices
);
1079 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1084 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1086 * Get Bonjour-shared printers...
1090 if (DNSServiceCreateConnection(&data
.main_ref
) != kDNSServiceErr_NoError
)
1093 main_fd
= DNSServiceRefSockFD(data
.main_ref
);
1095 ipp_ref
= data
.main_ref
;
1096 DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0,
1098 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
1100 local_ipp_ref
= data
.main_ref
;
1101 DNSServiceBrowse(&local_ipp_ref
, kDNSServiceFlagsShareConnection
,
1102 kDNSServiceInterfaceIndexLocalOnly
,
1104 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
1107 ipps_ref
= data
.main_ref
;
1108 DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0,
1110 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
1112 local_ipps_ref
= data
.main_ref
;
1113 DNSServiceBrowse(&local_ipps_ref
, kDNSServiceFlagsShareConnection
,
1114 kDNSServiceInterfaceIndexLocalOnly
,
1116 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
1117 # endif /* HAVE_SSL */
1119 # else /* HAVE_AVAHI */
1120 if ((data
.simple_poll
= avahi_simple_poll_new()) == NULL
)
1122 DEBUG_puts("cupsEnumDests: Unable to create Avahi simple poll object.");
1126 avahi_simple_poll_set_func(data
.simple_poll
, cups_dnssd_poll_cb
, &data
);
1128 data
.client
= avahi_client_new(avahi_simple_poll_get(data
.simple_poll
),
1129 0, cups_dnssd_client_cb
, &data
,
1133 DEBUG_puts("cupsEnumDests: Unable to create Avahi client.");
1134 avahi_simple_poll_free(data
.simple_poll
);
1139 ipp_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipp._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
);
1143 ipps_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipps._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
);
1144 # endif /* HAVE_SSL */
1145 # endif /* HAVE_DNSSD */
1148 remaining
= INT_MAX
;
1152 while (remaining
> 0 && (!cancel
|| !*cancel
))
1155 * Check for input...
1158 DEBUG_printf(("1cupsEnumDests: remaining=%d", remaining
));
1160 cups_elapsed(&curtime
);
1165 pfd
.events
= POLLIN
;
1167 nfds
= poll(&pfd
, 1, remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
1171 FD_SET(main_fd
, &input
);
1174 timeout
.tv_usec
= 1000 * (remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
1176 nfds
= select(main_fd
+ 1, &input
, NULL
, NULL
, &timeout
);
1177 # endif /* HAVE_POLL */
1180 DNSServiceProcessResult(data
.main_ref
);
1182 # else /* HAVE_AVAHI */
1185 if ((error
= avahi_simple_poll_iterate(data
.simple_poll
, _CUPS_DNSSD_MAXTIME
)) > 0)
1188 * We've been told to exit the loop. Perhaps the connection to
1195 DEBUG_printf(("1cupsEnumDests: got_data=%d", data
.got_data
));
1196 # endif /* HAVE_DNSSD */
1198 remaining
-= cups_elapsed(&curtime
);
1200 for (device
= (_cups_dnssd_device_t
*)cupsArrayFirst(data
.devices
),
1201 count
= 0, completed
= 0;
1203 device
= (_cups_dnssd_device_t
*)cupsArrayNext(data
.devices
))
1208 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
1211 if (!device
->ref
&& device
->state
== _CUPS_DNSSD_NEW
)
1213 DEBUG_printf(("1cupsEnumDests: Querying '%s'.", device
->fullName
));
1216 device
->ref
= data
.main_ref
;
1218 if (DNSServiceQueryRecord(&(device
->ref
),
1219 kDNSServiceFlagsShareConnection
,
1220 0, device
->fullName
,
1221 kDNSServiceType_TXT
,
1222 kDNSServiceClass_IN
,
1223 (DNSServiceQueryRecordReply
)cups_dnssd_query_cb
,
1224 &data
) == kDNSServiceErr_NoError
)
1231 device
->state
= _CUPS_DNSSD_ERROR
;
1233 DEBUG_puts("1cupsEnumDests: Query failed.");
1236 # else /* HAVE_AVAHI */
1237 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
)
1239 DEBUG_printf(("1cupsEnumDests: Query ref=%p", device
->ref
));
1244 device
->state
= _CUPS_DNSSD_ERROR
;
1246 DEBUG_printf(("1cupsEnumDests: Query failed: %s", avahi_strerror(avahi_client_errno(data
.client
))));
1248 # endif /* HAVE_DNSSD */
1250 else if (device
->ref
&& device
->state
== _CUPS_DNSSD_PENDING
)
1254 DEBUG_printf(("1cupsEnumDests: Query for \"%s\" is complete.", device
->fullName
));
1256 if ((device
->type
& mask
) == type
)
1258 DEBUG_printf(("1cupsEnumDests: Add callback for \"%s\".", device
->dest
.name
));
1259 if (!(*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, &device
->dest
))
1266 device
->state
= _CUPS_DNSSD_ACTIVE
;
1271 DEBUG_printf(("1cupsEnumDests: remaining=%d, browsers=%d, completed=%d, count=%d, devices count=%d", remaining
, data
.browsers
, completed
, count
, cupsArrayCount(data
.devices
)));
1273 if (data
.browsers
== 0 && completed
== cupsArrayCount(data
.devices
))
1276 DEBUG_printf(("1cupsEnumDests: remaining=%d, completed=%d, count=%d, devices count=%d", remaining
, completed
, count
, cupsArrayCount(data
.devices
)));
1278 if (completed
== cupsArrayCount(data
.devices
))
1280 # endif /* HAVE_AVAHI */
1283 cupsArrayDelete(data
.devices
);
1286 DNSServiceRefDeallocate(ipp_ref
);
1287 DNSServiceRefDeallocate(local_ipp_ref
);
1290 DNSServiceRefDeallocate(ipp_ref
);
1291 DNSServiceRefDeallocate(local_ipp_ref
);
1292 # endif /* HAVE_SSL */
1294 DNSServiceRefDeallocate(data
.main_ref
);
1296 # else /* HAVE_AVAHI */
1297 avahi_service_browser_free(ipp_ref
);
1299 avahi_service_browser_free(ipps_ref
);
1300 # endif /* HAVE_SSL */
1302 avahi_client_free(data
.client
);
1303 avahi_simple_poll_free(data
.simple_poll
);
1304 # endif /* HAVE_DNSSD */
1305 #endif /* HAVE_DNSSD || HAVE_DNSSD */
1313 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
1315 * Destinations are enumerated from one or more sources. The block receives the
1316 * destination name, instance, number of options, and options which can be used
1317 * as input to the @link cupsAddDest@ function. The block must return 1 to
1318 * continue enumeration or 0 to stop.
1320 * Enumeration happens on the current thread and does not return until all
1321 * destinations have been enumerated or the block returns 0.
1323 * @since CUPS 1.6/macOS 10.8@
1326 int /* O - 1 on success, 0 on failure */
1328 unsigned flags
, /* I - Enumeration flags */
1329 int timeout
, /* I - Timeout in milliseconds, 0 for indefinite */
1330 int *cancel
, /* I - Pointer to "cancel" variable */
1331 cups_ptype_t type
, /* I - Printer type bits */
1332 cups_ptype_t mask
, /* I - Mask for printer type bits */
1333 cups_dest_block_t block
) /* I - Block */
1335 return (cupsEnumDests(flags
, timeout
, cancel
, type
, mask
,
1336 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
1338 # endif /* __BLOCKS__ */
1342 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1346 cupsFreeDests(int num_dests
, /* I - Number of destinations */
1347 cups_dest_t
*dests
) /* I - Destinations */
1349 int i
; /* Looping var */
1350 cups_dest_t
*dest
; /* Current destination */
1353 if (num_dests
== 0 || dests
== NULL
)
1356 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1358 _cupsStrFree(dest
->name
);
1359 _cupsStrFree(dest
->instance
);
1361 cupsFreeOptions(dest
->num_options
, dest
->options
);
1369 * 'cupsGetDest()' - Get the named destination from the list.
1371 * Use the @link cupsGetDests@ or @link cupsGetDests2@ functions to get a
1372 * list of supported destinations for the current user.
1375 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
1376 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1377 const char *instance
, /* I - Instance name or @code NULL@ */
1378 int num_dests
, /* I - Number of destinations */
1379 cups_dest_t
*dests
) /* I - Destinations */
1381 int diff
, /* Result of comparison */
1382 match
; /* Matching index */
1385 if (num_dests
<= 0 || !dests
)
1391 * NULL name for default printer.
1394 while (num_dests
> 0)
1396 if (dests
->is_default
)
1406 * Lookup name and optionally the instance...
1409 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
1412 return (dests
+ match
);
1420 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1423 const char * /* O - Printer URI */
1424 _cupsGetDestResource(
1425 cups_dest_t
*dest
, /* I - Destination */
1426 char *resource
, /* I - Resource buffer */
1427 size_t resourcesize
) /* I - Size of resource buffer */
1429 const char *uri
; /* Printer URI */
1430 char scheme
[32], /* URI scheme */
1431 userpass
[256], /* Username and password (unused) */
1432 hostname
[256]; /* Hostname */
1433 int port
; /* Port number */
1437 * Range check input...
1440 if (!dest
|| !resource
|| resourcesize
< 1)
1445 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1450 * Grab the printer URI...
1453 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
,
1454 dest
->options
)) == NULL
)
1459 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1465 if (strstr(uri
, "._tcp"))
1467 if ((uri
= cups_dnssd_resolve(dest
, uri
, 5000, NULL
, NULL
, NULL
)) == NULL
)
1470 #endif /* HAVE_DNSSD */
1472 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1473 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
1474 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
1476 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1486 * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
1488 * "name" is the desired name for the printer. If @code NULL@, a name will be
1489 * created using the URI.
1491 * "uri" is the "ipp" or "ipps" URI for the printer.
1493 * @since CUPS 2.0/macOS 10.10@
1496 cups_dest_t
* /* O - Destination or @code NULL@ */
1497 cupsGetDestWithURI(const char *name
, /* I - Desired printer name or @code NULL@ */
1498 const char *uri
) /* I - URI for the printer */
1500 cups_dest_t
*dest
; /* New destination */
1501 char temp
[1024], /* Temporary string */
1502 scheme
[256], /* Scheme from URI */
1503 userpass
[256], /* Username:password from URI */
1504 hostname
[256], /* Hostname from URI */
1505 resource
[1024], /* Resource path from URI */
1506 *ptr
; /* Pointer into string */
1507 const char *info
; /* printer-info string */
1508 int port
; /* Port number from URI */
1512 * Range check input...
1517 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1521 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
||
1522 (strncmp(uri
, "ipp://", 6) && strncmp(uri
, "ipps://", 7)))
1524 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1536 * Create the name from the URI...
1539 if (strstr(hostname
, "._tcp"))
1542 * Use the service instance name...
1545 if ((ptr
= strstr(hostname
, "._")) != NULL
)
1548 cups_queue_name(temp
, hostname
, sizeof(temp
));
1552 else if (!strncmp(resource
, "/classes/", 9))
1554 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 9, hostname
);
1555 name
= resource
+ 9;
1558 else if (!strncmp(resource
, "/printers/", 10))
1560 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 10, hostname
);
1561 name
= resource
+ 10;
1572 * Create the destination...
1575 if ((dest
= calloc(1, sizeof(cups_dest_t
))) == NULL
)
1577 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1581 dest
->name
= _cupsStrAlloc(name
);
1582 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &(dest
->options
));
1583 dest
->num_options
= cupsAddOption("printer-info", info
, dest
->num_options
, &(dest
->options
));
1590 * '_cupsGetDests()' - Get destinations from a server.
1592 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1593 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1596 * "name" is the name of an existing printer and is only used when "op" is
1597 * IPP_OP_GET_PRINTER_ATTRIBUTES.
1599 * "dest" is initialized to point to the array of destinations.
1601 * 0 is returned if there are no printers, no default printer, or the named
1602 * printer does not exist, respectively.
1604 * Free the memory used by the destination array using the @link cupsFreeDests@
1607 * Note: On macOS this function also gets the default paper from the system
1608 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1609 * options array for each destination that supports it.
1612 int /* O - Number of destinations */
1613 _cupsGetDests(http_t
*http
, /* I - Connection to server or
1614 * @code CUPS_HTTP_DEFAULT@ */
1615 ipp_op_t op
, /* I - IPP operation */
1616 const char *name
, /* I - Name of destination */
1617 cups_dest_t
**dests
, /* IO - Destinations */
1618 cups_ptype_t type
, /* I - Printer type bits */
1619 cups_ptype_t mask
) /* I - Printer type mask */
1621 int num_dests
= 0; /* Number of destinations */
1622 cups_dest_t
*dest
; /* Current destination */
1623 ipp_t
*request
, /* IPP Request */
1624 *response
; /* IPP Response */
1625 ipp_attribute_t
*attr
; /* Current attribute */
1626 const char *printer_name
; /* printer-name attribute */
1627 char uri
[1024]; /* printer-uri value */
1628 int num_options
; /* Number of options */
1629 cups_option_t
*options
; /* Options */
1631 char media_default
[41]; /* Default paper size */
1632 #endif /* __APPLE__ */
1633 char optname
[1024], /* Option name */
1634 value
[2048], /* Option value */
1635 *ptr
; /* Pointer into name/value */
1636 static const char * const pattrs
[] = /* Attributes we're interested in */
1638 "auth-info-required",
1640 "job-sheets-default",
1641 "marker-change-time",
1643 "marker-high-levels",
1645 "marker-low-levels",
1651 #endif /* __APPLE__ */
1655 "printer-is-accepting-jobs",
1656 "printer-is-shared",
1658 "printer-make-and-model",
1659 "printer-mandatory-job-attributes",
1662 "printer-state-change-time",
1663 "printer-state-reasons",
1665 "printer-uri-supported"
1671 * Get the default paper size...
1674 appleGetPaperSize(media_default
, sizeof(media_default
));
1675 #endif /* __APPLE__ */
1678 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
1679 * require the following attributes:
1681 * attributes-charset
1682 * attributes-natural-language
1683 * requesting-user-name
1684 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
1687 request
= ippNewRequest(op
);
1689 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1690 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1693 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1694 "requesting-user-name", NULL
, cupsUser());
1696 if (name
&& op
!= IPP_OP_CUPS_GET_DEFAULT
)
1698 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1699 "localhost", ippPort(), "/printers/%s", name
);
1700 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1705 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type", (int)type
);
1706 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask", (int)mask
);
1710 * Do the request and get back a response...
1713 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1715 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1718 * Skip leading attributes until we hit a printer...
1721 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1728 * Pull the needed attributes from this printer...
1731 printer_name
= NULL
;
1735 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1737 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1738 attr
->value_tag
!= IPP_TAG_ENUM
&&
1739 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1740 attr
->value_tag
!= IPP_TAG_TEXT
&&
1741 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1742 attr
->value_tag
!= IPP_TAG_NAME
&&
1743 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1744 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1745 attr
->value_tag
!= IPP_TAG_RANGE
&&
1746 attr
->value_tag
!= IPP_TAG_URI
)
1749 if (!strcmp(attr
->name
, "auth-info-required") ||
1750 !strcmp(attr
->name
, "device-uri") ||
1751 !strcmp(attr
->name
, "marker-change-time") ||
1752 !strcmp(attr
->name
, "marker-colors") ||
1753 !strcmp(attr
->name
, "marker-high-levels") ||
1754 !strcmp(attr
->name
, "marker-levels") ||
1755 !strcmp(attr
->name
, "marker-low-levels") ||
1756 !strcmp(attr
->name
, "marker-message") ||
1757 !strcmp(attr
->name
, "marker-names") ||
1758 !strcmp(attr
->name
, "marker-types") ||
1759 !strcmp(attr
->name
, "printer-commands") ||
1760 !strcmp(attr
->name
, "printer-info") ||
1761 !strcmp(attr
->name
, "printer-is-shared") ||
1762 !strcmp(attr
->name
, "printer-make-and-model") ||
1763 !strcmp(attr
->name
, "printer-mandatory-job-attributes") ||
1764 !strcmp(attr
->name
, "printer-state") ||
1765 !strcmp(attr
->name
, "printer-state-change-time") ||
1766 !strcmp(attr
->name
, "printer-type") ||
1767 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1768 !strcmp(attr
->name
, "printer-location") ||
1769 !strcmp(attr
->name
, "printer-state-reasons") ||
1770 !strcmp(attr
->name
, "printer-uri-supported"))
1773 * Add a printer description attribute...
1776 num_options
= cupsAddOption(attr
->name
,
1777 cups_make_string(attr
, value
,
1779 num_options
, &options
);
1782 else if (!strcmp(attr
->name
, "media-supported"))
1785 * See if we can set a default media size...
1788 int i
; /* Looping var */
1790 for (i
= 0; i
< attr
->num_values
; i
++)
1791 if (!_cups_strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1793 num_options
= cupsAddOption("media", media_default
, num_options
,
1798 #endif /* __APPLE__ */
1799 else if (!strcmp(attr
->name
, "printer-name") &&
1800 attr
->value_tag
== IPP_TAG_NAME
)
1801 printer_name
= attr
->values
[0].string
.text
;
1802 else if (strncmp(attr
->name
, "notify-", 7) &&
1803 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1804 attr
->value_tag
== IPP_TAG_ENUM
||
1805 attr
->value_tag
== IPP_TAG_INTEGER
||
1806 attr
->value_tag
== IPP_TAG_KEYWORD
||
1807 attr
->value_tag
== IPP_TAG_NAME
||
1808 attr
->value_tag
== IPP_TAG_RANGE
) &&
1809 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1812 * Add a default option...
1815 strlcpy(optname
, attr
->name
, sizeof(optname
));
1816 optname
[ptr
- attr
->name
] = '\0';
1818 if (_cups_strcasecmp(optname
, "media") ||
1819 !cupsGetOption("media", num_options
, options
))
1820 num_options
= cupsAddOption(optname
,
1821 cups_make_string(attr
, value
,
1823 num_options
, &options
);
1828 * See if we have everything needed...
1833 cupsFreeOptions(num_options
, options
);
1841 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1843 dest
->num_options
= num_options
;
1844 dest
->options
= options
;
1847 cupsFreeOptions(num_options
, options
);
1853 ippDelete(response
);
1857 * Return the count...
1865 * 'cupsGetDests()' - Get the list of destinations from the default server.
1867 * Starting with CUPS 1.2, the returned list of destinations include the
1868 * printer-info, printer-is-accepting-jobs, printer-is-shared,
1869 * printer-make-and-model, printer-state, printer-state-change-time,
1870 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
1871 * adds the marker-change-time, marker-colors, marker-high-levels,
1872 * marker-levels, marker-low-levels, marker-message, marker-names,
1873 * marker-types, and printer-commands attributes as well.
1875 * Use the @link cupsFreeDests@ function to free the destination list and
1876 * the @link cupsGetDest@ function to find a particular destination.
1879 int /* O - Number of destinations */
1880 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
1882 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
1887 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
1889 * Starting with CUPS 1.2, the returned list of destinations include the
1890 * printer-info, printer-is-accepting-jobs, printer-is-shared,
1891 * printer-make-and-model, printer-state, printer-state-change-time,
1892 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
1893 * adds the marker-change-time, marker-colors, marker-high-levels,
1894 * marker-levels, marker-low-levels, marker-message, marker-names,
1895 * marker-types, and printer-commands attributes as well.
1897 * Use the @link cupsFreeDests@ function to free the destination list and
1898 * the @link cupsGetDest@ function to find a particular destination.
1900 * @since CUPS 1.1.21/macOS 10.4@
1903 int /* O - Number of destinations */
1904 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1905 cups_dest_t
**dests
) /* O - Destinations */
1907 int num_dests
; /* Number of destinations */
1908 cups_dest_t
*dest
; /* Destination pointer */
1909 const char *home
; /* HOME environment variable */
1910 char filename
[1024]; /* Local ~/.cups/lpoptions file */
1911 const char *defprinter
; /* Default printer */
1912 char name
[1024], /* Copy of printer name */
1913 *instance
, /* Pointer to instance name */
1914 *user_default
; /* User default printer */
1915 int num_reals
; /* Number of real queues */
1916 cups_dest_t
*reals
; /* Real queues */
1917 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1921 * Range check the input...
1926 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad NULL dests pointer"), 1);
1931 * Grab the printers and classes...
1934 *dests
= (cups_dest_t
*)0;
1935 num_dests
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, dests
, 0, CUPS_PRINTER_3D
);
1937 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
1939 cupsFreeDests(num_dests
, *dests
);
1940 *dests
= (cups_dest_t
*)0;
1945 * Make a copy of the "real" queues for a later sanity check...
1950 num_reals
= num_dests
;
1951 reals
= calloc((size_t)num_reals
, sizeof(cups_dest_t
));
1954 memcpy(reals
, *dests
, (size_t)num_reals
* sizeof(cups_dest_t
));
1965 * Grab the default destination...
1968 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
1970 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
1972 strlcpy(name
, defprinter
, sizeof(name
));
1979 * Separate printer and instance name...
1982 if ((instance
= strchr(name
, '/')) != NULL
)
1986 * Lookup the printer and instance and make it the default...
1989 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
1990 dest
->is_default
= 1;
1996 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
1999 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2000 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
2003 if ((home
= getenv("HOME")) != NULL
)
2005 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2007 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
2012 * Validate the current default destination - this prevents old
2013 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
2014 * pointing to a non-existent printer or class...
2020 * See if we have a default printer...
2023 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, *dests
)) != NULL
)
2026 * Have a default; see if it is real...
2029 if (!cupsGetDest(dest
->name
, NULL
, num_reals
, reals
))
2032 * Remove the non-real printer from the list, since we don't want jobs
2033 * going to an unexpected printer... (<rdar://problem/14216472>)
2036 num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
,
2049 * Return the number of destinations...
2053 _cupsSetError(IPP_STATUS_OK
, NULL
, 0);
2060 * 'cupsGetNamedDest()' - Get options for the named destination.
2062 * This function is optimized for retrieving a single destination and should
2063 * be used instead of @link cupsGetDests@ and @link cupsGetDest@ when you either
2064 * know the name of the destination or want to print to the default destination.
2065 * If @code NULL@ is returned, the destination does not exist or there is no
2066 * default destination.
2068 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
2069 * server will be used.
2071 * If "name" is @code NULL@, the default printer for the current user will be
2074 * The returned destination must be freed using @link cupsFreeDests@ with a
2075 * "num_dests" value of 1.
2077 * @since CUPS 1.4/macOS 10.6@
2080 cups_dest_t
* /* O - Destination or @code NULL@ */
2081 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2082 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
2083 const char *instance
) /* I - Instance name or @code NULL@ */
2085 cups_dest_t
*dest
; /* Destination */
2086 char filename
[1024], /* Path to lpoptions */
2087 defname
[256]; /* Default printer name */
2088 const char *home
= getenv("HOME"); /* Home directory */
2089 int set_as_default
= 0; /* Set returned destination as default */
2090 ipp_op_t op
= IPP_OP_GET_PRINTER_ATTRIBUTES
;
2091 /* IPP operation to get server ops */
2092 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2096 * If "name" is NULL, find the default destination...
2102 name
= _cupsUserDefault(defname
, sizeof(defname
));
2106 char *ptr
; /* Temporary pointer... */
2108 if ((ptr
= strchr(defname
, '/')) != NULL
)
2119 * No default in the environment, try the user's lpoptions files...
2122 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2124 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
2130 * Still not there? Try the system lpoptions file...
2133 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
2134 cg
->cups_serverroot
);
2135 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
2141 * No locally-set default destination, ask the server...
2144 op
= IPP_OP_CUPS_GET_DEFAULT
;
2149 * Get the printer's attributes...
2152 if (!_cupsGetDests(http
, op
, name
, &dest
, 0, CUPS_PRINTER_3D
))
2156 dest
->instance
= _cupsStrAlloc(instance
);
2159 dest
->is_default
= 1;
2162 * Then add local options...
2165 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2166 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
2170 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2172 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
2176 * Return the result...
2184 * 'cupsRemoveDest()' - Remove a destination from the destination list.
2186 * Removing a destination/instance does not delete the class or printer
2187 * queue, merely the lpoptions for that destination/instance. Use the
2188 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
2189 * options for the user.
2191 * @since CUPS 1.3/macOS 10.5@
2194 int /* O - New number of destinations */
2195 cupsRemoveDest(const char *name
, /* I - Destination name */
2196 const char *instance
, /* I - Instance name or @code NULL@ */
2197 int num_dests
, /* I - Number of destinations */
2198 cups_dest_t
**dests
) /* IO - Destinations */
2200 int i
; /* Index into destinations */
2201 cups_dest_t
*dest
; /* Pointer to destination */
2205 * Find the destination...
2208 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
2215 _cupsStrFree(dest
->name
);
2216 _cupsStrFree(dest
->instance
);
2217 cupsFreeOptions(dest
->num_options
, dest
->options
);
2220 * Remove the destination from the array...
2225 i
= (int)(dest
- *dests
);
2228 memmove(dest
, dest
+ 1, (size_t)(num_dests
- i
) * sizeof(cups_dest_t
));
2235 * 'cupsSetDefaultDest()' - Set the default destination.
2237 * @since CUPS 1.3/macOS 10.5@
2242 const char *name
, /* I - Destination name */
2243 const char *instance
, /* I - Instance name or @code NULL@ */
2244 int num_dests
, /* I - Number of destinations */
2245 cups_dest_t
*dests
) /* I - Destinations */
2247 int i
; /* Looping var */
2248 cups_dest_t
*dest
; /* Current destination */
2252 * Range check input...
2255 if (!name
|| num_dests
<= 0 || !dests
)
2259 * Loop through the array and set the "is_default" flag for the matching
2263 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2264 dest
->is_default
= !_cups_strcasecmp(name
, dest
->name
) &&
2265 ((!instance
&& !dest
->instance
) ||
2266 (instance
&& dest
->instance
&&
2267 !_cups_strcasecmp(instance
, dest
->instance
)));
2272 * 'cupsSetDests()' - Save the list of destinations for the default server.
2274 * This function saves the destinations to /etc/cups/lpoptions when run
2275 * as root and ~/.cups/lpoptions when run as a normal user.
2279 cupsSetDests(int num_dests
, /* I - Number of destinations */
2280 cups_dest_t
*dests
) /* I - Destinations */
2282 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
2287 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
2289 * This function saves the destinations to /etc/cups/lpoptions when run
2290 * as root and ~/.cups/lpoptions when run as a normal user.
2292 * @since CUPS 1.1.21/macOS 10.4@
2295 int /* O - 0 on success, -1 on error */
2296 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2297 int num_dests
, /* I - Number of destinations */
2298 cups_dest_t
*dests
) /* I - Destinations */
2300 int i
, j
; /* Looping vars */
2301 int wrote
; /* Wrote definition? */
2302 cups_dest_t
*dest
; /* Current destination */
2303 cups_option_t
*option
; /* Current option */
2304 _ipp_option_t
*match
; /* Matching attribute for option */
2305 FILE *fp
; /* File pointer */
2307 const char *home
; /* HOME environment variable */
2309 char filename
[1024]; /* lpoptions file */
2310 int num_temps
; /* Number of temporary destinations */
2311 cups_dest_t
*temps
= NULL
, /* Temporary destinations */
2312 *temp
; /* Current temporary dest */
2313 const char *val
; /* Value of temporary option */
2314 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2318 * Range check the input...
2321 if (!num_dests
|| !dests
)
2325 * Get the server destinations...
2328 num_temps
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &temps
, 0, CUPS_PRINTER_3D
);
2330 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2332 cupsFreeDests(num_temps
, temps
);
2337 * Figure out which file to write to...
2340 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2346 * Merge in server defaults...
2349 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
2352 * Point to user defaults...
2355 if ((home
= getenv("HOME")) != NULL
)
2358 * Create ~/.cups subdirectory...
2361 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
2362 if (access(filename
, 0))
2363 mkdir(filename
, 0700);
2365 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2371 * Try to open the file...
2374 if ((fp
= fopen(filename
, "w")) == NULL
)
2376 cupsFreeDests(num_temps
, temps
);
2382 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2387 fchmod(fileno(fp
), 0644);
2391 * Write each printer; each line looks like:
2393 * Dest name[/instance] options
2394 * Default name[/instance] options
2397 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2398 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
2400 if (dest
->is_default
)
2402 fprintf(fp
, "Default %s", dest
->name
);
2404 fprintf(fp
, "/%s", dest
->instance
);
2411 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
2412 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
2414 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
2417 * See if this option is a printer attribute; if so, skip it...
2420 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
2421 match
->group_tag
== IPP_TAG_PRINTER
)
2425 * See if the server/global options match these; if so, don't
2430 (val
= cupsGetOption(option
->name
, temp
->num_options
,
2431 temp
->options
)) != NULL
&&
2432 !_cups_strcasecmp(val
, option
->value
))
2436 * Options don't match, write to the file...
2441 fprintf(fp
, "Dest %s", dest
->name
);
2443 fprintf(fp
, "/%s", dest
->instance
);
2447 if (option
->value
[0])
2449 if (strchr(option
->value
, ' ') ||
2450 strchr(option
->value
, '\\') ||
2451 strchr(option
->value
, '\"') ||
2452 strchr(option
->value
, '\''))
2455 * Quote the value...
2458 fprintf(fp
, " %s=\"", option
->name
);
2460 for (val
= option
->value
; *val
; val
++)
2462 if (strchr("\"\'\\", *val
))
2473 * Store the literal value...
2476 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
2480 fprintf(fp
, " %s", option
->name
);
2488 * Free the temporary destinations and close the file...
2491 cupsFreeDests(num_temps
, temps
);
2497 * Set the default printer for this location - this allows command-line
2498 * and GUI applications to share the same default destination...
2501 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
2503 CFStringRef name
= CFStringCreateWithCString(kCFAllocatorDefault
,
2505 kCFStringEncodingUTF8
);
2506 /* Default printer name */
2510 _cupsAppleSetDefaultPrinter(name
);
2514 #endif /* __APPLE__ */
2516 #ifdef HAVE_NOTIFY_POST
2518 * Send a notification so that macOS applications can know about the
2522 notify_post("com.apple.printerListChange");
2523 #endif /* HAVE_NOTIFY_POST */
2530 * '_cupsUserDefault()' - Get the user default printer from environment
2531 * variables and location information.
2534 char * /* O - Default printer or NULL */
2535 _cupsUserDefault(char *name
, /* I - Name buffer */
2536 size_t namesize
) /* I - Size of name buffer */
2538 const char *env
; /* LPDEST or PRINTER env variable */
2540 CFStringRef locprinter
; /* Last printer as this location */
2541 #endif /* __APPLE__ */
2544 if ((env
= getenv("LPDEST")) == NULL
)
2545 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
2550 strlcpy(name
, env
, namesize
);
2556 * Use location-based defaults if "use last printer" is selected in the
2557 * system preferences...
2560 if ((locprinter
= _cupsAppleCopyDefaultPrinter()) != NULL
)
2562 CFStringGetCString(locprinter
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
);
2563 CFRelease(locprinter
);
2568 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name
));
2570 return (*name
? name
: NULL
);
2574 * No location-based defaults on this platform...
2579 #endif /* __APPLE__ */
2583 #if _CUPS_LOCATION_DEFAULTS
2585 * 'appleCopyLocations()' - Copy the location history array.
2588 static CFArrayRef
/* O - Location array or NULL */
2589 appleCopyLocations(void)
2591 CFArrayRef locations
; /* Location array */
2595 * Look up the location array in the preferences...
2598 if ((locations
= CFPreferencesCopyAppValue(kLastUsedPrintersKey
,
2599 kCUPSPrintingPrefs
)) == NULL
)
2602 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
2604 CFRelease(locations
);
2613 * 'appleCopyNetwork()' - Get the network ID for the current location.
2616 static CFStringRef
/* O - Network ID */
2617 appleCopyNetwork(void)
2619 SCDynamicStoreRef dynamicStore
; /* System configuration data */
2620 CFStringRef key
; /* Current network configuration key */
2621 CFDictionaryRef ip_dict
; /* Network configuration data */
2622 CFStringRef network
= NULL
; /* Current network ID */
2625 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("libcups"), NULL
,
2629 * First use the IPv6 router address, if available, since that will generally
2630 * be a globally-unique link-local address.
2633 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2634 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv6
)) != NULL
)
2636 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2638 if ((network
= CFDictionaryGetValue(ip_dict
,
2639 kSCPropNetIPv6Router
)) != NULL
)
2649 * If that doesn't work, try the IPv4 router address. This isn't as unique
2650 * and will likely be a 10.x.y.z or 192.168.y.z address...
2655 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2656 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
2658 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2660 if ((network
= CFDictionaryGetValue(ip_dict
,
2661 kSCPropNetIPv4Router
)) != NULL
)
2671 CFRelease(dynamicStore
);
2676 #endif /* _CUPS_LOCATION_DEFAULTS */
2681 * 'appleGetPaperSize()' - Get the default paper size.
2684 static char * /* O - Default paper size */
2685 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
2686 size_t namesize
) /* I - Size of buffer */
2688 CFStringRef defaultPaperID
; /* Default paper ID */
2689 pwg_media_t
*pwgmedia
; /* PWG media size */
2692 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
2693 if (!defaultPaperID
||
2694 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
2695 !CFStringGetCString(defaultPaperID
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
))
2697 else if ((pwgmedia
= pwgMediaForLegacy(name
)) != NULL
)
2698 strlcpy(name
, pwgmedia
->pwg
, namesize
);
2701 CFRelease(defaultPaperID
);
2705 #endif /* __APPLE__ */
2708 #if _CUPS_LOCATION_DEFAULTS
2710 * 'appleGetPrinter()' - Get a printer from the history array.
2713 static CFStringRef
/* O - Printer name or NULL */
2714 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
2715 CFStringRef network
, /* I - Network name */
2716 CFIndex
*locindex
) /* O - Index in array */
2718 CFIndex i
, /* Looping var */
2719 count
; /* Number of locations */
2720 CFDictionaryRef location
; /* Current location */
2721 CFStringRef locnetwork
, /* Current network */
2722 locprinter
; /* Current printer */
2725 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
2726 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
2727 CFGetTypeID(location
) == CFDictionaryGetTypeID())
2729 if ((locnetwork
= CFDictionaryGetValue(location
,
2730 kLocationNetworkKey
)) != NULL
&&
2731 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
2732 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
2733 (locprinter
= CFDictionaryGetValue(location
,
2734 kLocationPrinterIDKey
)) != NULL
&&
2735 CFGetTypeID(locprinter
) == CFStringGetTypeID())
2740 return (locprinter
);
2746 #endif /* _CUPS_LOCATION_DEFAULTS */
2750 * 'cups_add_dest()' - Add a destination to the array.
2752 * Unlike cupsAddDest(), this function does not check for duplicates.
2755 static cups_dest_t
* /* O - New destination */
2756 cups_add_dest(const char *name
, /* I - Name of destination */
2757 const char *instance
, /* I - Instance or NULL */
2758 int *num_dests
, /* IO - Number of destinations */
2759 cups_dest_t
**dests
) /* IO - Destinations */
2761 int insert
, /* Insertion point */
2762 diff
; /* Result of comparison */
2763 cups_dest_t
*dest
; /* Destination pointer */
2767 * Add new destination...
2770 if (*num_dests
== 0)
2771 dest
= malloc(sizeof(cups_dest_t
));
2773 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (size_t)(*num_dests
+ 1));
2781 * Find where to insert the destination...
2784 if (*num_dests
== 0)
2788 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
2796 * Move the array elements as needed...
2799 if (insert
< *num_dests
)
2800 memmove(*dests
+ insert
+ 1, *dests
+ insert
, (size_t)(*num_dests
- insert
) * sizeof(cups_dest_t
));
2805 * Initialize the destination...
2808 dest
= *dests
+ insert
;
2809 dest
->name
= _cupsStrAlloc(name
);
2810 dest
->instance
= _cupsStrAlloc(instance
);
2811 dest
->is_default
= 0;
2812 dest
->num_options
= 0;
2813 dest
->options
= (cups_option_t
*)0;
2821 * 'cups_block_cb()' - Enumeration callback for block API.
2824 static int /* O - 1 to continue, 0 to stop */
2826 cups_dest_block_t block
, /* I - Block */
2827 unsigned flags
, /* I - Destination flags */
2828 cups_dest_t
*dest
) /* I - Destination */
2830 return ((block
)(flags
, dest
));
2832 # endif /* __BLOCKS__ */
2836 * 'cups_compare_dests()' - Compare two destinations.
2839 static int /* O - Result of comparison */
2840 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
2841 cups_dest_t
*b
) /* I - Second destination */
2843 int diff
; /* Difference */
2846 if ((diff
= _cups_strcasecmp(a
->name
, b
->name
)) != 0)
2848 else if (a
->instance
&& b
->instance
)
2849 return (_cups_strcasecmp(a
->instance
, b
->instance
));
2851 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
2855 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2858 * 'cups_dnssd_browse_cb()' - Browse for printers.
2862 cups_dnssd_browse_cb(
2863 DNSServiceRef sdRef
, /* I - Service reference */
2864 DNSServiceFlags flags
, /* I - Option flags */
2865 uint32_t interfaceIndex
, /* I - Interface number */
2866 DNSServiceErrorType errorCode
, /* I - Error, if any */
2867 const char *serviceName
, /* I - Name of service/device */
2868 const char *regtype
, /* I - Type of service */
2869 const char *replyDomain
, /* I - Service domain */
2870 void *context
) /* I - Enumeration data */
2872 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2873 /* Enumeration data */
2876 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
));
2879 * Don't do anything on error...
2882 if (errorCode
!= kDNSServiceErr_NoError
)
2889 cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
2893 # else /* HAVE_AVAHI */
2895 * 'cups_dnssd_browse_cb()' - Browse for printers.
2899 cups_dnssd_browse_cb(
2900 AvahiServiceBrowser
*browser
, /* I - Browser */
2901 AvahiIfIndex interface
, /* I - Interface index (unused) */
2902 AvahiProtocol protocol
, /* I - Network protocol (unused) */
2903 AvahiBrowserEvent event
, /* I - What happened */
2904 const char *name
, /* I - Service name */
2905 const char *type
, /* I - Registration type */
2906 const char *domain
, /* I - Domain */
2907 AvahiLookupResultFlags flags
, /* I - Flags */
2908 void *context
) /* I - Devices array */
2911 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
2912 /* Client information */
2914 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2915 /* Enumeration data */
2922 DEBUG_printf(("cups_dnssd_browse_cb(..., name=\"%s\", type=\"%s\", domain=\"%s\", ...);", name
, type
, domain
));
2926 case AVAHI_BROWSER_FAILURE
:
2927 DEBUG_printf(("cups_dnssd_browse_cb: %s", avahi_strerror(avahi_client_errno(client
))));
2928 avahi_simple_poll_quit(data
->simple_poll
);
2931 case AVAHI_BROWSER_NEW
:
2933 * This object is new on the network.
2936 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
2939 * This comes from the local machine so ignore it.
2942 DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".", name
));
2947 * Create a device entry for it if it doesn't yet exist.
2950 cups_dnssd_get_device(data
, name
, type
, domain
);
2954 case AVAHI_BROWSER_REMOVE
:
2955 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
2958 case AVAHI_BROWSER_ALL_FOR_NOW
:
2959 DEBUG_puts("cups_dnssd_browse_cb: ALL_FOR_NOW");
2967 * 'cups_dnssd_client_cb()' - Avahi client callback function.
2971 cups_dnssd_client_cb(
2972 AvahiClient
*client
, /* I - Client information (unused) */
2973 AvahiClientState state
, /* I - Current state */
2974 void *context
) /* I - User data (unused) */
2976 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2977 /* Enumeration data */
2982 DEBUG_printf(("cups_dnssd_client_cb(client=%p, state=%d, context=%p)", client
, state
, context
));
2985 * If the connection drops, quit.
2988 if (state
== AVAHI_CLIENT_FAILURE
)
2990 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
2991 avahi_simple_poll_quit(data
->simple_poll
);
2994 # endif /* HAVE_DNSSD */
2998 * 'cups_dnssd_compare_device()' - Compare two devices.
3001 static int /* O - Result of comparison */
3002 cups_dnssd_compare_devices(
3003 _cups_dnssd_device_t
*a
, /* I - First device */
3004 _cups_dnssd_device_t
*b
) /* I - Second device */
3006 return (strcmp(a
->dest
.name
, b
->dest
.name
));
3011 * 'cups_dnssd_free_device()' - Free the memory used by a device.
3015 cups_dnssd_free_device(
3016 _cups_dnssd_device_t
*device
, /* I - Device */
3017 _cups_dnssd_data_t
*data
) /* I - Enumeration data */
3019 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", (void *)device
, device
->dest
.name
, (void *)data
));
3023 DNSServiceRefDeallocate(device
->ref
);
3024 # else /* HAVE_AVAHI */
3026 avahi_record_browser_free(device
->ref
);
3027 # endif /* HAVE_DNSSD */
3029 _cupsStrFree(device
->domain
);
3030 _cupsStrFree(device
->fullName
);
3031 _cupsStrFree(device
->regtype
);
3032 _cupsStrFree(device
->dest
.name
);
3034 cupsFreeOptions(device
->dest
.num_options
, device
->dest
.options
);
3041 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
3044 static _cups_dnssd_device_t
* /* O - Device */
3045 cups_dnssd_get_device(
3046 _cups_dnssd_data_t
*data
, /* I - Enumeration data */
3047 const char *serviceName
, /* I - Service name */
3048 const char *regtype
, /* I - Registration type */
3049 const char *replyDomain
) /* I - Domain name */
3051 _cups_dnssd_device_t key
, /* Search key */
3052 *device
; /* Device */
3053 char fullName
[kDNSServiceMaxDomainName
],
3054 /* Full name for query */
3055 name
[128]; /* Queue name */
3058 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\")", (void *)data
, serviceName
, regtype
, replyDomain
));
3061 * See if this is an existing device...
3064 cups_queue_name(name
, serviceName
, sizeof(name
));
3066 key
.dest
.name
= name
;
3068 if ((device
= cupsArrayFind(data
->devices
, &key
)) != NULL
)
3071 * Yes, see if we need to do anything with this...
3074 int update
= 0; /* Non-zero if we need to update */
3076 if (!_cups_strcasecmp(replyDomain
, "local.") &&
3077 _cups_strcasecmp(device
->domain
, replyDomain
))
3080 * Update the "global" listing to use the .local domain name instead.
3083 _cupsStrFree(device
->domain
);
3084 device
->domain
= _cupsStrAlloc(replyDomain
);
3086 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
3087 "domain.", device
->dest
.name
));
3092 if (!_cups_strcasecmp(regtype
, "_ipps._tcp") &&
3093 _cups_strcasecmp(device
->regtype
, regtype
))
3096 * Prefer IPPS over IPP.
3099 _cupsStrFree(device
->regtype
);
3100 device
->regtype
= _cupsStrAlloc(regtype
);
3102 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
3103 device
->dest
.name
));
3110 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
3111 device
->dest
.name
));
3118 * No, add the device...
3121 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
3122 "'%s'.", serviceName
,
3123 !strcmp(regtype
, "_ipps._tcp") ? "IPPS" : "IPP",
3126 device
= calloc(sizeof(_cups_dnssd_device_t
), 1);
3127 device
->dest
.name
= _cupsStrAlloc(name
);
3128 device
->domain
= _cupsStrAlloc(replyDomain
);
3129 device
->regtype
= _cupsStrAlloc(regtype
);
3131 device
->dest
.num_options
= cupsAddOption("printer-info", serviceName
, 0, &device
->dest
.options
);
3133 cupsArrayAdd(data
->devices
, device
);
3137 * Set the "full name" of this service, which is used for queries...
3141 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
3142 # else /* HAVE_AVAHI */
3143 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
, regtype
, replyDomain
);
3144 # endif /* HAVE_DNSSD */
3146 _cupsStrFree(device
->fullName
);
3147 device
->fullName
= _cupsStrAlloc(fullName
);
3152 DNSServiceRefDeallocate(device
->ref
);
3153 # else /* HAVE_AVAHI */
3154 avahi_record_browser_free(device
->ref
);
3155 # endif /* HAVE_DNSSD */
3160 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3162 DEBUG_printf(("6cups_dnssd_get_device: Remove callback for \"%s\".", device
->dest
.name
));
3164 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3165 device
->state
= _CUPS_DNSSD_NEW
;
3174 * 'cups_dnssd_local_cb()' - Browse for local printers.
3178 cups_dnssd_local_cb(
3179 DNSServiceRef sdRef
, /* I - Service reference */
3180 DNSServiceFlags flags
, /* I - Option flags */
3181 uint32_t interfaceIndex
, /* I - Interface number */
3182 DNSServiceErrorType errorCode
, /* I - Error, if any */
3183 const char *serviceName
, /* I - Name of service/device */
3184 const char *regtype
, /* I - Type of service */
3185 const char *replyDomain
, /* I - Service domain */
3186 void *context
) /* I - Devices array */
3188 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3189 /* Enumeration data */
3190 _cups_dnssd_device_t
*device
; /* Device */
3193 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
));
3196 * Only process "add" data...
3199 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3206 device
= cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
3209 * Hide locally-registered devices...
3212 DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.",
3217 DNSServiceRefDeallocate(device
->ref
);
3221 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3223 DEBUG_printf(("6cups_dnssd_local_cb: Remove callback for \"%s\".", device
->dest
.name
));
3224 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3227 device
->state
= _CUPS_DNSSD_LOCAL
;
3229 # endif /* HAVE_DNSSD */
3234 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
3236 * Note: This function is needed because avahi_simple_poll_iterate is broken
3237 * and always uses a timeout of 0 (!) milliseconds.
3238 * (Avahi Ticket #364)
3241 static int /* O - Number of file descriptors matching */
3243 struct pollfd
*pollfds
, /* I - File descriptors */
3244 unsigned int num_pollfds
, /* I - Number of file descriptors */
3245 int timeout
, /* I - Timeout in milliseconds (unused) */
3246 void *context
) /* I - User data (unused) */
3248 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3249 /* Enumeration data */
3250 int val
; /* Return value */
3253 DEBUG_printf(("cups_dnssd_poll_cb(pollfds=%p, num_pollfds=%d, timeout=%d, context=%p)", pollfds
, num_pollfds
, timeout
, context
));
3257 val
= poll(pollfds
, num_pollfds
, _CUPS_DNSSD_MAXTIME
);
3259 DEBUG_printf(("cups_dnssd_poll_cb: poll() returned %d", val
));
3263 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno
)));
3272 # endif /* HAVE_AVAHI */
3276 * 'cups_dnssd_query_cb()' - Process query data.
3281 cups_dnssd_query_cb(
3282 DNSServiceRef sdRef
, /* I - Service reference */
3283 DNSServiceFlags flags
, /* I - Data flags */
3284 uint32_t interfaceIndex
, /* I - Interface */
3285 DNSServiceErrorType errorCode
, /* I - Error, if any */
3286 const char *fullName
, /* I - Full service name */
3287 uint16_t rrtype
, /* I - Record type */
3288 uint16_t rrclass
, /* I - Record class */
3289 uint16_t rdlen
, /* I - Length of record data */
3290 const void *rdata
, /* I - Record data */
3291 uint32_t ttl
, /* I - Time-to-live */
3292 void *context
) /* I - Enumeration data */
3294 # else /* HAVE_AVAHI */
3296 cups_dnssd_query_cb(
3297 AvahiRecordBrowser
*browser
, /* I - Record browser */
3298 AvahiIfIndex interfaceIndex
,
3299 /* I - Interface index (unused) */
3300 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3301 AvahiBrowserEvent event
, /* I - What happened? */
3302 const char *fullName
, /* I - Service name */
3303 uint16_t rrclass
, /* I - Record class */
3304 uint16_t rrtype
, /* I - Record type */
3305 const void *rdata
, /* I - TXT record */
3306 size_t rdlen
, /* I - Length of TXT record */
3307 AvahiLookupResultFlags flags
, /* I - Flags */
3308 void *context
) /* I - Enumeration data */
3311 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
3312 /* Client information */
3314 # endif /* HAVE_DNSSD */
3315 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3316 /* Enumeration data */
3317 char serviceName
[256],/* Service name */
3318 name
[128], /* Queue name */
3319 *ptr
; /* Pointer into string */
3320 _cups_dnssd_device_t dkey
, /* Search key */
3321 *device
; /* Device */
3325 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
));
3328 * Only process "add" data...
3331 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3334 # else /* HAVE_AVAHI */
3335 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
));
3338 * Only process "add" data...
3341 if (event
!= AVAHI_BROWSER_NEW
)
3343 if (event
== AVAHI_BROWSER_FAILURE
)
3344 DEBUG_printf(("cups_dnssd_query_cb: %s", avahi_strerror(avahi_client_errno(client
))));
3348 # endif /* HAVE_DNSSD */
3351 * Lookup the service in the devices array.
3354 cups_dnssd_unquote(serviceName
, fullName
, sizeof(serviceName
));
3356 if ((ptr
= strstr(serviceName
, "._")) != NULL
)
3359 cups_queue_name(name
, serviceName
, sizeof(name
));
3361 dkey
.dest
.name
= name
;
3363 if ((device
= cupsArrayFind(data
->devices
, &dkey
)) != NULL
&& device
->state
== _CUPS_DNSSD_NEW
)
3366 * Found it, pull out the make and model from the TXT record and save it...
3369 const uint8_t *txt
, /* Pointer into data */
3370 *txtnext
, /* Next key/value pair */
3371 *txtend
; /* End of entire TXT record */
3372 uint8_t txtlen
; /* Length of current key/value pair */
3373 char key
[256], /* Key string */
3374 value
[256], /* Value string */
3375 make_and_model
[512],
3376 /* Manufacturer and model */
3377 model
[256], /* Model */
3378 uriname
[1024], /* Name for URI */
3379 uri
[1024]; /* Printer URI */
3380 cups_ptype_t type
= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_BW
;
3382 int saw_printer_type
= 0;
3383 /* Did we see a printer-type key? */
3385 device
->state
= _CUPS_DNSSD_PENDING
;
3386 make_and_model
[0] = '\0';
3388 strlcpy(model
, "Unknown", sizeof(model
));
3390 for (txt
= rdata
, txtend
= txt
+ rdlen
;
3395 * Read a key/value pair starting with an 8-bit length. Since the
3396 * length is 8 bits and the size of the key/value buffers is 256, we
3397 * don't need to check for overflow...
3402 if (!txtlen
|| (txt
+ txtlen
) > txtend
)
3405 txtnext
= txt
+ txtlen
;
3407 for (ptr
= key
; txt
< txtnext
&& *txt
!= '='; txt
++)
3408 *ptr
++ = (char)*txt
;
3411 if (txt
< txtnext
&& *txt
== '=')
3416 memcpy(value
, txt
, (size_t)(txtnext
- txt
));
3417 value
[txtnext
- txt
] = '\0';
3419 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key
, value
));
3423 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key
));
3427 if (!_cups_strcasecmp(key
, "usb_MFG") ||
3428 !_cups_strcasecmp(key
, "usb_MANU") ||
3429 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
3430 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
3431 else if (!_cups_strcasecmp(key
, "usb_MDL") ||
3432 !_cups_strcasecmp(key
, "usb_MODEL"))
3433 strlcpy(model
, value
, sizeof(model
));
3434 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
3436 if (value
[0] == '(')
3439 * Strip parenthesis...
3442 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
3445 strlcpy(model
, value
+ 1, sizeof(model
));
3448 strlcpy(model
, value
, sizeof(model
));
3450 else if (!_cups_strcasecmp(key
, "ty"))
3452 strlcpy(model
, value
, sizeof(model
));
3454 if ((ptr
= strchr(model
, ',')) != NULL
)
3457 else if (!_cups_strcasecmp(key
, "note"))
3458 device
->dest
.num_options
= cupsAddOption("printer-location", value
,
3459 device
->dest
.num_options
,
3460 &device
->dest
.options
);
3461 else if (!_cups_strcasecmp(key
, "pdl"))
3464 * Look for PDF-capable printers; only PDF-capable printers are shown.
3467 const char *start
, *next
; /* Pointer into value */
3468 int have_pdf
= 0, /* Have PDF? */
3469 have_raster
= 0;/* Have raster format support? */
3471 for (start
= value
; start
&& *start
; start
= next
)
3473 if (!_cups_strncasecmp(start
, "application/pdf", 15) && (!start
[15] || start
[15] == ','))
3478 else if ((!_cups_strncasecmp(start
, "image/pwg-raster", 16) && (!start
[16] || start
[16] == ',')) ||
3479 (!_cups_strncasecmp(start
, "image/urf", 9) && (!start
[9] || start
[9] == ',')))
3485 if ((next
= strchr(start
, ',')) != NULL
)
3489 if (!have_pdf
&& !have_raster
)
3490 device
->state
= _CUPS_DNSSD_INCOMPATIBLE
;
3492 else if (!_cups_strcasecmp(key
, "printer-type"))
3495 * Value is either NNNN or 0xXXXX
3498 saw_printer_type
= 1;
3499 type
= (cups_ptype_t
)strtol(value
, NULL
, 0);
3501 else if (!saw_printer_type
)
3503 if (!_cups_strcasecmp(key
, "air") &&
3504 !_cups_strcasecmp(value
, "t"))
3505 type
|= CUPS_PRINTER_AUTHENTICATED
;
3506 else if (!_cups_strcasecmp(key
, "bind") &&
3507 !_cups_strcasecmp(value
, "t"))
3508 type
|= CUPS_PRINTER_BIND
;
3509 else if (!_cups_strcasecmp(key
, "collate") &&
3510 !_cups_strcasecmp(value
, "t"))
3511 type
|= CUPS_PRINTER_COLLATE
;
3512 else if (!_cups_strcasecmp(key
, "color") &&
3513 !_cups_strcasecmp(value
, "t"))
3514 type
|= CUPS_PRINTER_COLOR
;
3515 else if (!_cups_strcasecmp(key
, "copies") &&
3516 !_cups_strcasecmp(value
, "t"))
3517 type
|= CUPS_PRINTER_COPIES
;
3518 else if (!_cups_strcasecmp(key
, "duplex") &&
3519 !_cups_strcasecmp(value
, "t"))
3520 type
|= CUPS_PRINTER_DUPLEX
;
3521 else if (!_cups_strcasecmp(key
, "fax") &&
3522 !_cups_strcasecmp(value
, "t"))
3523 type
|= CUPS_PRINTER_MFP
;
3524 else if (!_cups_strcasecmp(key
, "papercustom") &&
3525 !_cups_strcasecmp(value
, "t"))
3526 type
|= CUPS_PRINTER_VARIABLE
;
3527 else if (!_cups_strcasecmp(key
, "papermax"))
3529 if (!_cups_strcasecmp(value
, "legal-a4"))
3530 type
|= CUPS_PRINTER_SMALL
;
3531 else if (!_cups_strcasecmp(value
, "isoc-a2"))
3532 type
|= CUPS_PRINTER_MEDIUM
;
3533 else if (!_cups_strcasecmp(value
, ">isoc-a2"))
3534 type
|= CUPS_PRINTER_LARGE
;
3536 else if (!_cups_strcasecmp(key
, "punch") &&
3537 !_cups_strcasecmp(value
, "t"))
3538 type
|= CUPS_PRINTER_PUNCH
;
3539 else if (!_cups_strcasecmp(key
, "scan") &&
3540 !_cups_strcasecmp(value
, "t"))
3541 type
|= CUPS_PRINTER_MFP
;
3542 else if (!_cups_strcasecmp(key
, "sort") &&
3543 !_cups_strcasecmp(value
, "t"))
3544 type
|= CUPS_PRINTER_SORT
;
3545 else if (!_cups_strcasecmp(key
, "staple") &&
3546 !_cups_strcasecmp(value
, "t"))
3547 type
|= CUPS_PRINTER_STAPLE
;
3552 * Save the printer-xxx values...
3555 if (make_and_model
[0])
3557 strlcat(make_and_model
, " ", sizeof(make_and_model
));
3558 strlcat(make_and_model
, model
, sizeof(make_and_model
));
3560 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", make_and_model
, device
->dest
.num_options
, &device
->dest
.options
);
3563 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", model
, device
->dest
.num_options
, &device
->dest
.options
);
3565 device
->type
= type
;
3566 snprintf(value
, sizeof(value
), "%u", type
);
3567 device
->dest
.num_options
= cupsAddOption("printer-type", value
, device
->dest
.num_options
, &device
->dest
.options
);
3573 cups_dnssd_unquote(uriname
, device
->fullName
, sizeof(uriname
));
3574 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
3575 !strcmp(device
->regtype
, "_ipps._tcp") ? "ipps" : "ipp",
3576 NULL
, uriname
, 0, saw_printer_type
? "/cups" : "/");
3578 DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri
));
3580 device
->dest
.num_options
= cupsAddOption("device-uri", uri
, device
->dest
.num_options
, &device
->dest
.options
);
3583 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3589 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3592 static const char * /* O - Resolved URI or NULL */
3594 cups_dest_t
*dest
, /* I - Destination */
3595 const char *uri
, /* I - Current printer URI */
3596 int msec
, /* I - Time in milliseconds */
3597 int *cancel
, /* I - Pointer to "cancel" variable */
3598 cups_dest_cb_t cb
, /* I - Callback */
3599 void *user_data
) /* I - User data for callback */
3601 char tempuri
[1024]; /* Temporary URI buffer */
3602 _cups_dnssd_resolve_t resolve
; /* Resolve data */
3606 * Resolve the URI...
3609 resolve
.cancel
= cancel
;
3610 gettimeofday(&resolve
.end_time
, NULL
);
3613 resolve
.end_time
.tv_sec
+= msec
/ 1000;
3614 resolve
.end_time
.tv_usec
+= (msec
% 1000) * 1000;
3616 while (resolve
.end_time
.tv_usec
>= 1000000)
3618 resolve
.end_time
.tv_sec
++;
3619 resolve
.end_time
.tv_usec
-= 1000000;
3623 resolve
.end_time
.tv_sec
+= 75;
3626 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
3628 if ((uri
= _httpResolveURI(uri
, tempuri
, sizeof(tempuri
), _HTTP_RESOLVE_DEFAULT
, cups_dnssd_resolve_cb
, &resolve
)) == NULL
)
3630 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to resolve printer-uri."), 1);
3633 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
3639 * Save the resolved URI...
3642 dest
->num_options
= cupsAddOption("resolved-device-uri", uri
, dest
->num_options
, &dest
->options
);
3644 return (cupsGetOption("resolved-device-uri", dest
->num_options
, dest
->options
));
3649 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3652 static int /* O - 1 to continue, 0 to stop */
3653 cups_dnssd_resolve_cb(void *context
) /* I - Resolve data */
3655 _cups_dnssd_resolve_t
*resolve
= (_cups_dnssd_resolve_t
*)context
;
3657 struct timeval curtime
; /* Current time */
3661 * If the cancel variable is set, return immediately.
3664 if (resolve
->cancel
&& *(resolve
->cancel
))
3666 DEBUG_puts("4cups_dnssd_resolve_cb: Canceled.");
3671 * Otherwise check the end time...
3674 gettimeofday(&curtime
, NULL
);
3676 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
));
3678 return (curtime
.tv_sec
< resolve
->end_time
.tv_sec
||
3679 (curtime
.tv_sec
== resolve
->end_time
.tv_sec
&&
3680 curtime
.tv_usec
< resolve
->end_time
.tv_usec
));
3685 * 'cups_dnssd_unquote()' - Unquote a name string.
3689 cups_dnssd_unquote(char *dst
, /* I - Destination buffer */
3690 const char *src
, /* I - Source string */
3691 size_t dstsize
) /* I - Size of destination buffer */
3693 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
3696 while (*src
&& dst
< dstend
)
3701 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
3702 isdigit(src
[2] & 255))
3704 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';
3716 #endif /* HAVE_DNSSD */
3720 * 'cups_elapsed()' - Return the elapsed time in milliseconds.
3723 static int /* O - Elapsed time in milliseconds */
3724 cups_elapsed(struct timeval
*t
) /* IO - Previous time */
3726 int msecs
; /* Milliseconds */
3727 struct timeval nt
; /* New time */
3730 gettimeofday(&nt
, NULL
);
3732 msecs
= 1000 * (nt
.tv_sec
- t
->tv_sec
) + (nt
.tv_usec
- t
->tv_usec
) / 1000;
3741 * 'cups_find_dest()' - Find a destination using a binary search.
3744 static int /* O - Index of match */
3745 cups_find_dest(const char *name
, /* I - Destination name */
3746 const char *instance
, /* I - Instance or NULL */
3747 int num_dests
, /* I - Number of destinations */
3748 cups_dest_t
*dests
, /* I - Destinations */
3749 int prev
, /* I - Previous index */
3750 int *rdiff
) /* O - Difference of match */
3752 int left
, /* Low mark for binary search */
3753 right
, /* High mark for binary search */
3754 current
, /* Current index */
3755 diff
; /* Result of comparison */
3756 cups_dest_t key
; /* Search key */
3759 key
.name
= (char *)name
;
3760 key
.instance
= (char *)instance
;
3765 * Start search on either side of previous...
3768 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
3769 (diff
< 0 && prev
== 0) ||
3770 (diff
> 0 && prev
== (num_dests
- 1)))
3778 * Start with previous on right side...
3787 * Start wih previous on left side...
3791 right
= num_dests
- 1;
3797 * Start search in the middle...
3801 right
= num_dests
- 1;
3806 current
= (left
+ right
) / 2;
3807 diff
= cups_compare_dests(&key
, dests
+ current
);
3816 while ((right
- left
) > 1);
3821 * Check the last 1 or 2 elements...
3824 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
3828 diff
= cups_compare_dests(&key
, dests
+ right
);
3834 * Return the closest destination and the difference...
3844 * 'cups_get_default()' - Get the default destination from an lpoptions file.
3847 static char * /* O - Default destination or NULL */
3848 cups_get_default(const char *filename
, /* I - File to read */
3849 char *namebuf
, /* I - Name buffer */
3850 size_t namesize
, /* I - Size of name buffer */
3851 const char **instance
) /* I - Instance */
3853 cups_file_t
*fp
; /* lpoptions file */
3854 char line
[8192], /* Line from file */
3855 *value
, /* Value for line */
3856 *nameptr
; /* Pointer into name */
3857 int linenum
; /* Current line */
3862 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
3866 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3868 if (!_cups_strcasecmp(line
, "default") && value
)
3870 strlcpy(namebuf
, value
, namesize
);
3872 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
3874 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
3877 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
3880 *instance
= nameptr
;
3888 return (*namebuf
? namebuf
: NULL
);
3893 * 'cups_get_dests()' - Get destinations from a file.
3896 static int /* O - Number of destinations */
3898 const char *filename
, /* I - File to read from */
3899 const char *match_name
, /* I - Destination name we want */
3900 const char *match_inst
, /* I - Instance name we want */
3901 int user_default_set
, /* I - User default printer set? */
3902 int num_dests
, /* I - Number of destinations */
3903 cups_dest_t
**dests
) /* IO - Destinations */
3905 int i
; /* Looping var */
3906 cups_dest_t
*dest
; /* Current destination */
3907 cups_file_t
*fp
; /* File pointer */
3908 char line
[8192], /* Line from file */
3909 *lineptr
, /* Pointer into line */
3910 *name
, /* Name of destination/option */
3911 *instance
; /* Instance of destination */
3912 int linenum
; /* Current line number */
3915 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
));
3918 * Try to open the file...
3921 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3925 * Read each printer; each line looks like:
3927 * Dest name[/instance] options
3928 * Default name[/instance] options
3933 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
3936 * See what type of line it is...
3939 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
3940 linenum
, line
, lineptr
));
3942 if ((_cups_strcasecmp(line
, "dest") && _cups_strcasecmp(line
, "default")) || !lineptr
)
3944 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
3951 * Search for an instance...
3954 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
3957 if (*lineptr
== '/')
3960 * Found an instance...
3967 * Search for an instance...
3970 while (!isspace(*lineptr
& 255) && *lineptr
)
3979 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
3983 * See if the primary instance of the destination exists; if not,
3984 * ignore this entry and move on...
3989 if (_cups_strcasecmp(name
, match_name
) ||
3990 (!instance
&& match_inst
) ||
3991 (instance
&& !match_inst
) ||
3992 (instance
&& _cups_strcasecmp(instance
, match_inst
)))
3997 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
3999 DEBUG_puts("9cups_get_dests: Not found!");
4005 * Add the destination...
4008 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
4010 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
4016 DEBUG_puts("9cups_get_dests: Out of memory!");
4022 * Add options until we hit the end of the line...
4025 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
4029 * If we found what we were looking for, stop now...
4036 * Set this as default if needed...
4039 if (!user_default_set
&& !_cups_strcasecmp(line
, "default"))
4041 DEBUG_puts("9cups_get_dests: Setting as default...");
4043 for (i
= 0; i
< num_dests
; i
++)
4044 (*dests
)[i
].is_default
= 0;
4046 dest
->is_default
= 1;
4051 * Close the file and return...
4061 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
4065 static char * /* O - New string */
4067 ipp_attribute_t
*attr
, /* I - Attribute to convert */
4068 char *buffer
, /* I - Buffer */
4069 size_t bufsize
) /* I - Size of buffer */
4071 int i
; /* Looping var */
4072 char *ptr
, /* Pointer into buffer */
4073 *end
, /* Pointer to end of buffer */
4074 *valptr
; /* Pointer into string attribute */
4078 * Return quickly if we have a single string value...
4081 if (attr
->num_values
== 1 &&
4082 attr
->value_tag
!= IPP_TAG_INTEGER
&&
4083 attr
->value_tag
!= IPP_TAG_ENUM
&&
4084 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
4085 attr
->value_tag
!= IPP_TAG_RANGE
)
4086 return (attr
->values
[0].string
.text
);
4089 * Copy the values to the string, separating with commas and escaping strings
4093 end
= buffer
+ bufsize
- 1;
4095 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
4100 switch (attr
->value_tag
)
4102 case IPP_TAG_INTEGER
:
4104 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].integer
);
4107 case IPP_TAG_BOOLEAN
:
4108 if (attr
->values
[i
].boolean
)
4109 strlcpy(ptr
, "true", (size_t)(end
- ptr
+ 1));
4111 strlcpy(ptr
, "false", (size_t)(end
- ptr
+ 1));
4114 case IPP_TAG_RANGE
:
4115 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
4116 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].range
.lower
);
4118 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d-%d", attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4122 for (valptr
= attr
->values
[i
].string
.text
;
4123 *valptr
&& ptr
< end
;)
4125 if (strchr(" \t\n\\\'\"", *valptr
))
4127 if (ptr
>= (end
- 1))
4150 * 'cups_queue_name()' - Create a local queue name based on the service name.
4155 char *name
, /* I - Name buffer */
4156 const char *serviceName
, /* I - Service name */
4157 size_t namesize
) /* I - Size of name buffer */
4159 const char *ptr
; /* Pointer into serviceName */
4160 char *nameptr
; /* Pointer into name */
4163 for (nameptr
= name
, ptr
= serviceName
; *ptr
&& nameptr
< (name
+ namesize
- 1); ptr
++)
4166 * Sanitize the printer name...
4169 if (_cups_isalnum(*ptr
))
4171 else if (nameptr
== name
|| nameptr
[-1] != '_')