]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/dest.c
Fix some bugs in the implementation that have been around for a while...
[thirdparty/cups.git] / cups / dest.c
CommitLineData
ef416fc2 1/*
7e86f2f6 2 * User-defined destination (and option) support for CUPS.
ef416fc2 3 *
83f1674e 4 * Copyright 2007-2016 by Apple Inc.
7e86f2f6 5 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 6 *
7e86f2f6
MS
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 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 12 *
7e86f2f6 13 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 14 */
15
16/*
17 * Include necessary headers...
18 */
19
71e16022 20#include "cups-private.h"
b423cd4c 21#include <sys/stat.h>
ef416fc2 22
fa73b229 23#ifdef HAVE_NOTIFY_H
24# include <notify.h>
25#endif /* HAVE_NOTIFY_H */
26
dcb445bc
MS
27#ifdef HAVE_POLL
28# include <poll.h>
29#endif /* HAVE_POLL */
30
31#ifdef HAVE_DNSSD
32# include <dns_sd.h>
33#endif /* HAVE_DNSSD */
34
a29fd7dd
MS
35#ifdef HAVE_AVAHI
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 */
44
dcb445bc
MS
45
46/*
47 * Constants...
48 */
49
080811b1 50#ifdef __APPLE__
080811b1 51# include <SystemConfiguration/SystemConfiguration.h>
f14324a7
MS
52# define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
53# define kDefaultPaperIDKey CFSTR("DefaultPaperID")
54# define kLastUsedPrintersKey CFSTR("LastUsedPrinters")
55# define kLocationNetworkKey CFSTR("Network")
56# define kLocationPrinterIDKey CFSTR("PrinterID")
57# define kUseLastPrinter CFSTR("UseLastPrinter")
080811b1
MS
58#endif /* __APPLE__ */
59
ef416fc2 60
dcb445bc
MS
61/*
62 * Types...
63 */
64
a29fd7dd 65#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
dcb445bc
MS
66typedef enum _cups_dnssd_state_e /* Enumerated device state */
67{
68 _CUPS_DNSSD_NEW,
69 _CUPS_DNSSD_QUERY,
70 _CUPS_DNSSD_PENDING,
71 _CUPS_DNSSD_ACTIVE,
72 _CUPS_DNSSD_LOCAL,
a29fd7dd 73 _CUPS_DNSSD_INCOMPATIBLE,
dcb445bc
MS
74 _CUPS_DNSSD_ERROR
75} _cups_dnssd_state_t;
76
77typedef struct _cups_dnssd_data_s /* Enumeration data */
78{
a29fd7dd 79# ifdef HAVE_DNSSD
dcb445bc 80 DNSServiceRef main_ref; /* Main service reference */
a29fd7dd
MS
81# else /* HAVE_AVAHI */
82 AvahiSimplePoll *simple_poll; /* Polling interface */
83 AvahiClient *client; /* Client information */
84 int got_data; /* Did we get data? */
85# endif /* HAVE_DNSSD */
dcb445bc
MS
86 cups_dest_cb_t cb; /* Callback */
87 void *user_data; /* User data pointer */
88 cups_ptype_t type, /* Printer type filter */
89 mask; /* Printer type mask */
90 cups_array_t *devices; /* Devices found so far */
91} _cups_dnssd_data_t;
92
93typedef struct _cups_dnssd_device_s /* Enumerated device */
94{
95 _cups_dnssd_state_t state; /* State of device listing */
a29fd7dd 96# ifdef HAVE_DNSSD
dcb445bc 97 DNSServiceRef ref; /* Service reference for query */
a29fd7dd
MS
98# else /* HAVE_AVAHI */
99 AvahiRecordBrowser *ref; /* Browser for query */
100# endif /* HAVE_DNSSD */
dcb445bc
MS
101 char *domain, /* Domain name */
102 *fullName, /* Full name */
103 *regtype; /* Registration type */
104 cups_ptype_t type; /* Device registration type */
105 cups_dest_t dest; /* Destination record */
106} _cups_dnssd_device_t;
107
108typedef struct _cups_dnssd_resolve_s /* Data for resolving URI */
109{
110 int *cancel; /* Pointer to "cancel" variable */
111 struct timeval end_time; /* Ending time */
112} _cups_dnssd_resolve_t;
113#endif /* HAVE_DNSSD */
114
115
ef416fc2 116/*
117 * Local functions...
118 */
119
080811b1 120#ifdef __APPLE__
9c80ffa2
MS
121static CFArrayRef appleCopyLocations(void);
122static CFStringRef appleCopyNetwork(void);
d649be59 123static char *appleGetPaperSize(char *name, size_t namesize);
a29fd7dd
MS
124static CFStringRef appleGetPrinter(CFArrayRef locations,
125 CFStringRef network, CFIndex *locindex);
080811b1 126#endif /* __APPLE__ */
9c80ffa2
MS
127static cups_dest_t *cups_add_dest(const char *name, const char *instance,
128 int *num_dests, cups_dest_t **dests);
129#ifdef __BLOCKS__
dcb445bc
MS
130static int cups_block_cb(cups_dest_block_t block, unsigned flags,
131 cups_dest_t *dest);
9c80ffa2
MS
132#endif /* __BLOCKS__ */
133static int cups_compare_dests(cups_dest_t *a, cups_dest_t *b);
a29fd7dd
MS
134#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
135# ifdef HAVE_DNSSD
dcb445bc
MS
136static void cups_dnssd_browse_cb(DNSServiceRef sdRef,
137 DNSServiceFlags flags,
138 uint32_t interfaceIndex,
139 DNSServiceErrorType errorCode,
140 const char *serviceName,
141 const char *regtype,
142 const char *replyDomain,
143 void *context);
a29fd7dd
MS
144# else /* HAVE_AVAHI */
145static void cups_dnssd_browse_cb(AvahiServiceBrowser *browser,
146 AvahiIfIndex interface,
147 AvahiProtocol protocol,
148 AvahiBrowserEvent event,
149 const char *serviceName,
150 const char *regtype,
151 const char *replyDomain,
152 AvahiLookupResultFlags flags,
153 void *context);
154static void cups_dnssd_client_cb(AvahiClient *client,
155 AvahiClientState state,
156 void *context);
157# endif /* HAVE_DNSSD */
dcb445bc
MS
158static int cups_dnssd_compare_devices(_cups_dnssd_device_t *a,
159 _cups_dnssd_device_t *b);
160static void cups_dnssd_free_device(_cups_dnssd_device_t *device,
161 _cups_dnssd_data_t *data);
162static _cups_dnssd_device_t *
163 cups_dnssd_get_device(_cups_dnssd_data_t *data,
164 const char *serviceName,
165 const char *regtype,
166 const char *replyDomain);
a29fd7dd 167# ifdef HAVE_DNSSD
dcb445bc
MS
168static void cups_dnssd_local_cb(DNSServiceRef sdRef,
169 DNSServiceFlags flags,
170 uint32_t interfaceIndex,
171 DNSServiceErrorType errorCode,
172 const char *serviceName,
173 const char *regtype,
174 const char *replyDomain,
175 void *context);
176static void cups_dnssd_query_cb(DNSServiceRef sdRef,
177 DNSServiceFlags flags,
178 uint32_t interfaceIndex,
179 DNSServiceErrorType errorCode,
180 const char *fullName,
181 uint16_t rrtype, uint16_t rrclass,
182 uint16_t rdlen, const void *rdata,
183 uint32_t ttl, void *context);
a29fd7dd
MS
184# else /* HAVE_AVAHI */
185static int cups_dnssd_poll_cb(struct pollfd *pollfds,
186 unsigned int num_pollfds,
187 int timeout, void *context);
188static void cups_dnssd_query_cb(AvahiRecordBrowser *browser,
189 AvahiIfIndex interface,
190 AvahiProtocol protocol,
191 AvahiBrowserEvent event,
192 const char *name, uint16_t rrclass,
193 uint16_t rrtype, const void *rdata,
194 size_t rdlen,
195 AvahiLookupResultFlags flags,
196 void *context);
197# endif /* HAVE_DNSSD */
dcb445bc
MS
198static const char *cups_dnssd_resolve(cups_dest_t *dest, const char *uri,
199 int msec, int *cancel,
200 cups_dest_cb_t cb, void *user_data);
201static int cups_dnssd_resolve_cb(void *context);
202static void cups_dnssd_unquote(char *dst, const char *src,
203 size_t dstsize);
a29fd7dd 204#endif /* HAVE_DNSSD || HAVE_AVAHI */
9c80ffa2
MS
205static int cups_find_dest(const char *name, const char *instance,
206 int num_dests, cups_dest_t *dests, int prev,
207 int *rdiff);
208static char *cups_get_default(const char *filename, char *namebuf,
209 size_t namesize, const char **instance);
210static int cups_get_dests(const char *filename, const char *match_name,
211 const char *match_inst, int user_default_set,
212 int num_dests, cups_dest_t **dests);
213static char *cups_make_string(ipp_attribute_t *attr, char *buffer,
214 size_t bufsize);
ef416fc2 215
216
217/*
218 * 'cupsAddDest()' - Add a destination to the list of destinations.
219 *
2abf387c 220 * This function cannot be used to add a new class or printer queue,
221 * it only adds a new container of saved options for the named
222 * destination or instance.
223 *
224 * If the named destination already exists, the destination list is
225 * returned unchanged. Adding a new instance of a destination creates
226 * a copy of that destination's options.
227 *
5a738aea 228 * Use the @link cupsSaveDests@ function to save the updated list of
2abf387c 229 * destinations to the user's lpoptions file.
ef416fc2 230 */
231
232int /* O - New number of destinations */
2abf387c 233cupsAddDest(const char *name, /* I - Destination name */
5a738aea 234 const char *instance, /* I - Instance name or @code NULL@ for none/primary */
ef416fc2 235 int num_dests, /* I - Number of destinations */
236 cups_dest_t **dests) /* IO - Destinations */
237{
238 int i; /* Looping var */
239 cups_dest_t *dest; /* Destination pointer */
bf3816c7 240 cups_dest_t *parent = NULL; /* Parent destination */
426c6a59
MS
241 cups_option_t *doption, /* Current destination option */
242 *poption; /* Current parent option */
ef416fc2 243
244
2abf387c 245 if (!name || !dests)
ef416fc2 246 return (0);
247
426c6a59
MS
248 if (!cupsGetDest(name, instance, num_dests, *dests))
249 {
41681883 250 if (instance && !cupsGetDest(name, NULL, num_dests, *dests))
426c6a59 251 return (num_dests);
ef416fc2 252
5a9febac
MS
253 if ((dest = cups_add_dest(name, instance, &num_dests, dests)) == NULL)
254 return (num_dests);
2abf387c 255
5a662dc0
MS
256 /*
257 * Find the base dest again now the array has been realloc'd.
258 */
259
260 parent = cupsGetDest(name, NULL, num_dests, *dests);
261
426c6a59
MS
262 if (instance && parent && parent->num_options > 0)
263 {
264 /*
265 * Copy options from parent...
266 */
ef416fc2 267
7e86f2f6 268 dest->options = calloc(sizeof(cups_option_t), (size_t)parent->num_options);
2abf387c 269
426c6a59
MS
270 if (dest->options)
271 {
272 dest->num_options = parent->num_options;
ef416fc2 273
426c6a59
MS
274 for (i = dest->num_options, doption = dest->options,
275 poption = parent->options;
276 i > 0;
277 i --, doption ++, poption ++)
278 {
279 doption->name = _cupsStrRetain(poption->name);
280 doption->value = _cupsStrRetain(poption->value);
281 }
282 }
2abf387c 283 }
284 }
285
426c6a59 286 return (num_dests);
ef416fc2 287}
288
289
c8fef167
MS
290#ifdef __APPLE__
291/*
292 * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
293 */
294
295CFStringRef /* O - Default paper ID */
296_cupsAppleCopyDefaultPaperID(void)
297{
f14324a7
MS
298 return (CFPreferencesCopyAppValue(kDefaultPaperIDKey,
299 kCUPSPrintingPrefs));
300}
301
302
303/*
304 * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location.
305 */
306
307CFStringRef /* O - Default printer name */
308_cupsAppleCopyDefaultPrinter(void)
309{
310 CFStringRef network; /* Network location */
311 CFArrayRef locations; /* Location array */
312 CFStringRef locprinter; /* Current printer */
c8fef167
MS
313
314
f14324a7
MS
315 /*
316 * Use location-based defaults only if "use last printer" is selected in the
317 * system preferences...
318 */
c8fef167 319
f14324a7
MS
320 if (!_cupsAppleGetUseLastPrinter())
321 {
322 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as "
323 "default.");
324 return (NULL);
325 }
c8fef167 326
f14324a7
MS
327 /*
328 * Get the current location...
329 */
330
331 if ((network = appleCopyNetwork()) == NULL)
332 {
333 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current "
334 "network.");
335 return (NULL);
336 }
337
f14324a7
MS
338 /*
339 * Lookup the network in the preferences...
340 */
341
342 if ((locations = appleCopyLocations()) == NULL)
343 {
344 /*
345 * Missing or bad location array, so no location-based default...
346 */
347
348 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used "
349 "printer array.");
350
351 CFRelease(network);
352
353 return (NULL);
354 }
88f9aafc 355
f14324a7
MS
356 DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.",
357 (int)CFArrayGetCount(locations)));
358
359 if ((locprinter = appleGetPrinter(locations, network, NULL)) != NULL)
360 CFRetain(locprinter);
361
362 CFRelease(network);
363 CFRelease(locations);
364
365 return (locprinter);
c8fef167
MS
366}
367
368
369/*
370 * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
371 */
372
373int /* O - 1 to use last printer, 0 otherwise */
374_cupsAppleGetUseLastPrinter(void)
375{
376 Boolean uselast, /* Use last printer preference value */
377 uselast_set; /* Valid is set? */
378
379
380 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
381 return (0);
382
f14324a7
MS
383 uselast = CFPreferencesGetAppBooleanValue(kUseLastPrinter,
384 kCUPSPrintingPrefs,
c8fef167 385 &uselast_set);
c8fef167
MS
386 if (!uselast_set)
387 return (1);
388 else
389 return (uselast);
390}
391
392
393/*
394 * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
395 */
396
397void
398_cupsAppleSetDefaultPaperID(
399 CFStringRef name) /* I - New paper ID */
400{
f14324a7
MS
401 CFPreferencesSetAppValue(kDefaultPaperIDKey, name, kCUPSPrintingPrefs);
402 CFPreferencesAppSynchronize(kCUPSPrintingPrefs);
684d1464
MS
403
404# ifdef HAVE_NOTIFY_POST
f14324a7 405 notify_post("com.apple.printerPrefsChange");
684d1464 406# endif /* HAVE_NOTIFY_POST */
f14324a7
MS
407}
408
409
410/*
411 * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location.
412 */
413
414void
415_cupsAppleSetDefaultPrinter(
416 CFStringRef name) /* I - Default printer/class name */
417{
418 CFStringRef network; /* Current network */
419 CFArrayRef locations; /* Old locations array */
420 CFIndex locindex; /* Index in locations array */
421 CFStringRef locprinter; /* Current printer */
422 CFMutableArrayRef newlocations; /* New locations array */
423 CFMutableDictionaryRef newlocation; /* New location */
424
425
426 /*
427 * Get the current location...
428 */
429
430 if ((network = appleCopyNetwork()) == NULL)
431 {
432 DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network...");
433 return;
434 }
435
436 /*
437 * Lookup the network in the preferences...
438 */
439
440 if ((locations = appleCopyLocations()) != NULL)
441 locprinter = appleGetPrinter(locations, network, &locindex);
442 else
443 {
444 locprinter = NULL;
445 locindex = -1;
446 }
447
448 if (!locprinter || CFStringCompare(locprinter, name, 0) != kCFCompareEqualTo)
449 {
450 /*
451 * Need to change the locations array...
452 */
453
454 if (locations)
455 {
456 newlocations = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0,
457 locations);
458
459 if (locprinter)
460 CFArrayRemoveValueAtIndex(newlocations, locindex);
461 }
462 else
463 newlocations = CFArrayCreateMutable(kCFAllocatorDefault, 0,
464 &kCFTypeArrayCallBacks);
465
466 newlocation = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
467 &kCFTypeDictionaryKeyCallBacks,
468 &kCFTypeDictionaryValueCallBacks);
469
470 if (newlocation && newlocations)
471 {
472 /*
473 * Put the new location at the front of the array...
474 */
475
476 CFDictionaryAddValue(newlocation, kLocationNetworkKey, network);
477 CFDictionaryAddValue(newlocation, kLocationPrinterIDKey, name);
478 CFArrayInsertValueAtIndex(newlocations, 0, newlocation);
479
480 /*
481 * Limit the number of locations to 10...
482 */
483
484 while (CFArrayGetCount(newlocations) > 10)
485 CFArrayRemoveValueAtIndex(newlocations, 10);
486
487 /*
488 * Push the changes out...
489 */
490
491 CFPreferencesSetAppValue(kLastUsedPrintersKey, newlocations,
492 kCUPSPrintingPrefs);
493 CFPreferencesAppSynchronize(kCUPSPrintingPrefs);
684d1464
MS
494
495# ifdef HAVE_NOTIFY_POST
f14324a7 496 notify_post("com.apple.printerPrefsChange");
684d1464 497# endif /* HAVE_NOTIFY_POST */
f14324a7
MS
498 }
499
500 if (newlocations)
501 CFRelease(newlocations);
502
503 if (newlocation)
504 CFRelease(newlocation);
505 }
506
507 if (locations)
508 CFRelease(locations);
509
510 CFRelease(network);
c8fef167
MS
511}
512
513
514/*
515 * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
516 */
88f9aafc
MS
517
518void
c8fef167
MS
519_cupsAppleSetUseLastPrinter(
520 int uselast) /* O - 1 to use last printer, 0 otherwise */
521{
88f9aafc 522 CFPreferencesSetAppValue(kUseLastPrinter,
c8fef167 523 uselast ? kCFBooleanTrue : kCFBooleanFalse,
f14324a7
MS
524 kCUPSPrintingPrefs);
525 CFPreferencesAppSynchronize(kCUPSPrintingPrefs);
684d1464
MS
526
527# ifdef HAVE_NOTIFY_POST
f14324a7 528 notify_post("com.apple.printerPrefsChange");
684d1464 529# endif /* HAVE_NOTIFY_POST */
c8fef167
MS
530}
531#endif /* __APPLE__ */
532
533
9c80ffa2
MS
534/*
535 * 'cupsConnectDest()' - Connect to the server for a destination.
536 *
dcb445bc
MS
537 * Connect to the destination, returning a new http_t connection object and
538 * optionally the resource path to use for the destination. These calls will
539 * block until a connection is made, the timeout expires, the integer pointed
540 * to by "cancel" is non-zero, or the callback function (or block) returns 0,
541 * The caller is responsible for calling httpClose() on the returned object.
542 *
f3c17241 543 * @since CUPS 1.6/OS X 10.8@
9c80ffa2
MS
544 */
545
546http_t * /* O - Connection to server or @code NULL@ */
dcb445bc
MS
547cupsConnectDest(
548 cups_dest_t *dest, /* I - Destination */
549 unsigned flags, /* I - Connection flags */
550 int msec, /* I - Timeout in milliseconds */
551 int *cancel, /* I - Pointer to "cancel" variable */
552 char *resource, /* I - Resource buffer */
553 size_t resourcesize, /* I - Size of resource buffer */
554 cups_dest_cb_t cb, /* I - Callback function */
555 void *user_data) /* I - User data pointer */
9c80ffa2 556{
dcb445bc
MS
557 const char *uri; /* Printer URI */
558 char scheme[32], /* URI scheme */
559 userpass[256], /* Username and password (unused) */
560 hostname[256], /* Hostname */
561 tempresource[1024]; /* Temporary resource buffer */
562 int port; /* Port number */
563 char portstr[16]; /* Port number string */
12f89d24 564 http_encryption_t encryption; /* Encryption to use */
dcb445bc
MS
565 http_addrlist_t *addrlist; /* Address list for server */
566 http_t *http; /* Connection to server */
567
568
4db7fcee
MS
569 DEBUG_printf(("cupsConnectDest(dest=%p, flags=0x%x, msec=%d, cancel=%p(%d), resource=\"%s\", resourcesize=" CUPS_LLFMT ", cb=%p, user_data=%p)", dest, flags, msec, cancel, cancel ? *cancel : -1, resource, CUPS_LLCAST resourcesize, cb, user_data));
570
dcb445bc
MS
571 /*
572 * Range check input...
573 */
574
575 if (!dest)
576 {
577 if (resource)
578 *resource = '\0';
579
cb7f98ee 580 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
dcb445bc
MS
581 return (NULL);
582 }
583
584 if (!resource || resourcesize < 1)
585 {
586 resource = tempresource;
587 resourcesize = sizeof(tempresource);
588 }
589
590 /*
591 * Grab the printer URI...
592 */
593
46385a1a 594 if ((uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options)) == NULL)
dcb445bc 595 {
46385a1a
MS
596 if ((uri = cupsGetOption("resolved-device-uri", dest->num_options, dest->options)) == NULL)
597 {
598 if ((uri = cupsGetOption("device-uri", dest->num_options, dest->options)) != NULL)
599 {
600#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
601 if (strstr(uri, "._tcp"))
602 uri = cups_dnssd_resolve(dest, uri, msec, cancel, cb, user_data);
603 }
604#endif /* HAVE_DNSSD || HAVE_AVAHI */
605 }
dcb445bc 606
46385a1a
MS
607 if (uri)
608 uri = _cupsCreateDest(dest->name, cupsGetOption("printer-info", dest->num_options, dest->options), NULL, uri, tempresource, sizeof(tempresource));
dcb445bc 609
46385a1a
MS
610 if (uri)
611 {
612 dest->num_options = cupsAddOption("printer-uri-supported", uri, dest->num_options, &dest->options);
613
614 uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options);
615 }
dcb445bc
MS
616 }
617
46385a1a 618 if (!uri)
dcb445bc 619 {
46385a1a
MS
620 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0);
621
622 if (cb)
623 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, dest);
624
625 return (NULL);
dcb445bc 626 }
dcb445bc
MS
627
628 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
629 userpass, sizeof(userpass), hostname, sizeof(hostname),
7e86f2f6 630 &port, resource, (int)resourcesize) < HTTP_URI_STATUS_OK)
dcb445bc 631 {
f5f2e19e 632 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer-uri."), 1);
dcb445bc
MS
633
634 if (cb)
635 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR,
636 dest);
637
638 return (NULL);
639 }
640
641 /*
642 * Lookup the address for the server...
643 */
644
645 if (cb)
46385a1a 646 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_RESOLVING, dest);
dcb445bc
MS
647
648 snprintf(portstr, sizeof(portstr), "%d", port);
649
650 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portstr)) == NULL)
651 {
652 if (cb)
46385a1a 653 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, dest);
dcb445bc
MS
654
655 return (NULL);
656 }
657
658 if (cancel && *cancel)
659 {
660 httpAddrFreeList(addrlist);
661
662 if (cb)
46385a1a 663 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_CANCELED, dest);
dcb445bc
MS
664
665 return (NULL);
666 }
667
668 /*
669 * Create the HTTP object pointing to the server referenced by the URI...
670 */
671
672 if (!strcmp(scheme, "ipps") || port == 443)
cb7f98ee 673 encryption = HTTP_ENCRYPTION_ALWAYS;
dcb445bc 674 else
cb7f98ee 675 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
dcb445bc 676
46385a1a 677 http = httpConnect2(hostname, port, addrlist, AF_UNSPEC, encryption, 1, 0, NULL);
520fe1de 678 httpAddrFreeList(addrlist);
dcb445bc
MS
679
680 /*
681 * Connect if requested...
682 */
683
684 if (flags & CUPS_DEST_FLAGS_UNCONNECTED)
685 {
686 if (cb)
687 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED, dest);
688 }
689 else
690 {
691 if (cb)
46385a1a 692 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_CONNECTING, dest);
dcb445bc
MS
693
694 if (!httpReconnect2(http, msec, cancel) && cb)
695 {
696 if (cancel && *cancel)
46385a1a 697 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_CONNECTING, dest);
dcb445bc 698 else
46385a1a 699 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, dest);
dcb445bc
MS
700 }
701 else if (cb)
702 (*cb)(user_data, CUPS_DEST_FLAGS_NONE, dest);
703 }
704
705 return (http);
706}
707
708
709#ifdef __BLOCKS__
710/*
711 * 'cupsConnectDestBlock()' - Connect to the server for a destination.
712 *
713 * Connect to the destination, returning a new http_t connection object and
714 * optionally the resource path to use for the destination. These calls will
715 * block until a connection is made, the timeout expires, the integer pointed
716 * to by "cancel" is non-zero, or the callback function (or block) returns 0,
717 * The caller is responsible for calling httpClose() on the returned object.
718 *
f3c17241 719 * @since CUPS 1.6/OS X 10.8@
dcb445bc
MS
720 */
721
722http_t * /* O - Connection to server or @code NULL@ */
723cupsConnectDestBlock(
724 cups_dest_t *dest, /* I - Destination */
725 unsigned flags, /* I - Connection flags */
726 int msec, /* I - Timeout in milliseconds */
727 int *cancel, /* I - Pointer to "cancel" variable */
728 char *resource, /* I - Resource buffer */
729 size_t resourcesize, /* I - Size of resource buffer */
730 cups_dest_block_t block) /* I - Callback block */
731{
732 return (cupsConnectDest(dest, flags, msec, cancel, resource, resourcesize,
733 (cups_dest_cb_t)cups_block_cb, (void *)block));
734}
735#endif /* __BLOCKS__ */
736
737
738/*
739 * 'cupsCopyDest()' - Copy a destination.
740 *
741 * Make a copy of the destination to an array of destinations (or just a single
742 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
743 * for calling cupsFreeDests() on the returned object(s).
744 *
f3c17241 745 * @since CUPS 1.6/OS X 10.8@
dcb445bc
MS
746 */
747
748int
749cupsCopyDest(cups_dest_t *dest,
750 int num_dests,
751 cups_dest_t **dests)
752{
753 int i; /* Looping var */
754 cups_dest_t *new_dest; /* New destination pointer */
755 cups_option_t *new_option, /* Current destination option */
756 *option; /* Current parent option */
757
758
759 /*
760 * Range check input...
761 */
762
763 if (!dest || num_dests < 0 || !dests)
764 return (num_dests);
765
766 /*
767 * See if the destination already exists...
768 */
769
770 if ((new_dest = cupsGetDest(dest->name, dest->instance, num_dests,
771 *dests)) != NULL)
772 {
773 /*
774 * Protect against copying destination to itself...
775 */
776
777 if (new_dest == dest)
778 return (num_dests);
779
780 /*
781 * Otherwise, free the options...
782 */
783
784 cupsFreeOptions(new_dest->num_options, new_dest->options);
785
786 new_dest->num_options = 0;
787 new_dest->options = NULL;
788 }
789 else
790 new_dest = cups_add_dest(dest->name, dest->instance, &num_dests, dests);
791
792 if (new_dest)
793 {
7e86f2f6 794 if ((new_dest->options = calloc(sizeof(cups_option_t), (size_t)dest->num_options)) == NULL)
dcb445bc
MS
795 return (cupsRemoveDest(dest->name, dest->instance, num_dests, dests));
796
797 new_dest->num_options = dest->num_options;
798
799 for (i = dest->num_options, option = dest->options,
800 new_option = new_dest->options;
801 i > 0;
802 i --, option ++, new_option ++)
803 {
804 new_option->name = _cupsStrRetain(option->name);
805 new_option->value = _cupsStrRetain(option->value);
806 }
807 }
808
809 return (num_dests);
9c80ffa2
MS
810}
811
812
7ae00c35
MS
813/*
814 * '_cupsCreateDest()' - Create a local (temporary) queue.
815 */
816
817char * /* O - Printer URI or @code NULL@ on error */
818_cupsCreateDest(const char *name, /* I - Printer name */
819 const char *info, /* I - Printer description of @code NULL@ */
820 const char *device_id, /* I - 1284 Device ID or @code NULL@ */
821 const char *device_uri, /* I - Device URI */
822 char *uri, /* I - Printer URI buffer */
823 size_t urisize) /* I - Size of URI buffer */
824{
825 http_t *http; /* Connection to server */
826 ipp_t *request, /* CUPS-Create-Local-Printer request */
827 *response; /* CUPS-Create-Local-Printer response */
828 ipp_attribute_t *attr; /* printer-uri-supported attribute */
829
830
831 (void)info;
832 (void)device_id;
833
834 if (!name || !device_uri || !uri || urisize < 32)
835 return (NULL);
836
837 if ((http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL)) == NULL)
838 return (NULL);
839
840 request = ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER);
841
4db7fcee 842 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, "ipp://localhost/");
7ae00c35
MS
843 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
844
845 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL, device_uri);
846 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL, name);
847 if (info)
848 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", NULL, info);
849 if (device_id)
850 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-device-id", NULL, device_id);
851
852 response = cupsDoRequest(http, request, "/");
853
854 httpClose(http);
855
856 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
857 strlcpy(uri, ippGetString(attr, 0, NULL), urisize);
858
859 ippDelete(response);
860
861 return (attr ? uri : NULL);
862}
863
864
9c80ffa2
MS
865/*
866 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
867 *
dcb445bc
MS
868 * Destinations are enumerated from one or more sources. The callback function
869 * receives the @code user_data@ pointer, destination name, instance, number of
870 * options, and options which can be used as input to the @link cupsAddDest@
871 * function. The function must return 1 to continue enumeration or 0 to stop.
9c80ffa2 872 *
dcb445bc
MS
873 * Enumeration happens on the current thread and does not return until all
874 * destinations have been enumerated or the callback function returns 0.
9c80ffa2 875 *
f3c17241 876 * @since CUPS 1.6/OS X 10.8@
9c80ffa2
MS
877 */
878
879int /* O - 1 on success, 0 on failure */
dcb445bc
MS
880cupsEnumDests(
881 unsigned flags, /* I - Enumeration flags */
882 int msec, /* I - Timeout in milliseconds,
883 * -1 for indefinite */
884 int *cancel, /* I - Pointer to "cancel" variable */
885 cups_ptype_t type, /* I - Printer type bits */
886 cups_ptype_t mask, /* I - Mask for printer type bits */
887 cups_dest_cb_t cb, /* I - Callback function */
888 void *user_data) /* I - User data */
9c80ffa2 889{
dcb445bc
MS
890 int i, /* Looping var */
891 num_dests; /* Number of destinations */
82cc1f9a 892 cups_dest_t *dests = NULL, /* Destinations */
dcb445bc 893 *dest; /* Current destination */
4db41741
MS
894 const char *defprinter; /* Default printer */
895 char name[1024], /* Copy of printer name */
896 *instance, /* Pointer to instance name */
897 *user_default; /* User default printer */
a29fd7dd
MS
898#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
899 int count, /* Number of queries started */
dcb445bc
MS
900 remaining; /* Remainder of timeout */
901 _cups_dnssd_data_t data; /* Data for callback */
902 _cups_dnssd_device_t *device; /* Current device */
a29fd7dd
MS
903# ifdef HAVE_DNSSD
904 int nfds, /* Number of files responded */
905 main_fd; /* File descriptor for lookups */
dcb445bc
MS
906 DNSServiceRef ipp_ref, /* IPP browser */
907 local_ipp_ref; /* Local IPP browser */
a29fd7dd 908# ifdef HAVE_SSL
dcb445bc
MS
909 DNSServiceRef ipps_ref, /* IPPS browser */
910 local_ipps_ref; /* Local IPPS browser */
a29fd7dd
MS
911# endif /* HAVE_SSL */
912# ifdef HAVE_POLL
dcb445bc 913 struct pollfd pfd; /* Polling data */
a29fd7dd 914# else
dcb445bc
MS
915 fd_set input; /* Input set for select() */
916 struct timeval timeout; /* Timeout for select() */
a29fd7dd
MS
917# endif /* HAVE_POLL */
918# else /* HAVE_AVAHI */
919 int error; /* Error value */
920 AvahiServiceBrowser *ipp_ref; /* IPP browser */
921# ifdef HAVE_SSL
922 AvahiServiceBrowser *ipps_ref; /* IPPS browser */
923# endif /* HAVE_SSL */
924# endif /* HAVE_DNSSD */
925#endif /* HAVE_DNSSD || HAVE_AVAHI */
dcb445bc
MS
926
927 /*
928 * Range check input...
929 */
930
f3c17241
MS
931 (void)flags;
932
dcb445bc
MS
933 if (!cb)
934 return (0);
935
936 /*
937 * Get the list of local printers and pass them to the callback function...
938 */
939
5489ad43 940 num_dests = _cupsGetDests(CUPS_HTTP_DEFAULT, IPP_OP_CUPS_GET_PRINTERS, NULL,
5a08320a 941 &dests, type, mask | CUPS_PRINTER_3D);
dcb445bc 942
4db41741
MS
943 if ((user_default = _cupsUserDefault(name, sizeof(name))) != NULL)
944 defprinter = name;
945 else if ((defprinter = cupsGetDefault2(CUPS_HTTP_DEFAULT)) != NULL)
946 {
947 strlcpy(name, defprinter, sizeof(name));
948 defprinter = name;
949 }
950
951 if (defprinter)
952 {
953 /*
954 * Separate printer and instance name...
955 */
956
957 if ((instance = strchr(name, '/')) != NULL)
958 *instance++ = '\0';
959
960 /*
961 * Lookup the printer and instance and make it the default...
962 */
963
964 if ((dest = cupsGetDest(name, instance, num_dests, dests)) != NULL)
965 dest->is_default = 1;
966 }
967
dcb445bc
MS
968 for (i = num_dests, dest = dests;
969 i > 0 && (!cancel || !*cancel);
970 i --, dest ++)
971 if (!(*cb)(user_data, i > 1 ? CUPS_DEST_FLAGS_MORE : CUPS_DEST_FLAGS_NONE,
972 dest))
973 break;
974
975 cupsFreeDests(num_dests, dests);
976
977 if (i > 0 || msec == 0)
978 return (1);
979
a29fd7dd 980#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
dcb445bc
MS
981 /*
982 * Get Bonjour-shared printers...
983 */
984
c51dcb48
MS
985 data.type = type;
986 data.mask = mask;
987 data.cb = cb;
988 data.user_data = user_data;
989 data.devices = cupsArrayNew3((cups_array_func_t)cups_dnssd_compare_devices, NULL, NULL, 0, NULL, (cups_afree_func_t)cups_dnssd_free_device);
dcb445bc 990
a29fd7dd 991# ifdef HAVE_DNSSD
dcb445bc
MS
992 if (DNSServiceCreateConnection(&data.main_ref) != kDNSServiceErr_NoError)
993 return (0);
994
995 main_fd = DNSServiceRefSockFD(data.main_ref);
996
997 ipp_ref = data.main_ref;
998 DNSServiceBrowse(&ipp_ref, kDNSServiceFlagsShareConnection, 0,
a29fd7dd 999 "_ipp._tcp", NULL,
12f89d24 1000 (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data);
dcb445bc
MS
1001
1002 local_ipp_ref = data.main_ref;
1003 DNSServiceBrowse(&local_ipp_ref, kDNSServiceFlagsShareConnection,
1004 kDNSServiceInterfaceIndexLocalOnly,
a29fd7dd 1005 "_ipp._tcp", NULL,
12f89d24 1006 (DNSServiceBrowseReply)cups_dnssd_local_cb, &data);
dcb445bc 1007
a29fd7dd 1008# ifdef HAVE_SSL
dcb445bc
MS
1009 ipps_ref = data.main_ref;
1010 DNSServiceBrowse(&ipps_ref, kDNSServiceFlagsShareConnection, 0,
a29fd7dd 1011 "_ipps._tcp", NULL,
12f89d24 1012 (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data);
dcb445bc
MS
1013
1014 local_ipps_ref = data.main_ref;
1015 DNSServiceBrowse(&local_ipps_ref, kDNSServiceFlagsShareConnection,
1016 kDNSServiceInterfaceIndexLocalOnly,
a29fd7dd 1017 "_ipps._tcp", NULL,
12f89d24 1018 (DNSServiceBrowseReply)cups_dnssd_local_cb, &data);
a29fd7dd
MS
1019# endif /* HAVE_SSL */
1020
1021# else /* HAVE_AVAHI */
1022 if ((data.simple_poll = avahi_simple_poll_new()) == NULL)
1023 {
1024 DEBUG_puts("cupsEnumDests: Unable to create Avahi simple poll object.");
1025 return (1);
1026 }
1027
1028 avahi_simple_poll_set_func(data.simple_poll, cups_dnssd_poll_cb, &data);
1029
1030 data.client = avahi_client_new(avahi_simple_poll_get(data.simple_poll),
1031 0, cups_dnssd_client_cb, &data,
1032 &error);
1033 if (!data.client)
1034 {
1035 DEBUG_puts("cupsEnumDests: Unable to create Avahi client.");
1036 avahi_simple_poll_free(data.simple_poll);
1037 return (1);
1038 }
1039
1040 ipp_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC,
1041 AVAHI_PROTO_UNSPEC, "_ipp._tcp", NULL,
1042 0, cups_dnssd_browse_cb, &data);
1043# ifdef HAVE_SSL
1044 ipps_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC,
1045 AVAHI_PROTO_UNSPEC, "_ipps._tcp", NULL,
1046 0, cups_dnssd_browse_cb, &data);
1047# endif /* HAVE_SSL */
1048# endif /* HAVE_DNSSD */
dcb445bc
MS
1049
1050 if (msec < 0)
1051 remaining = INT_MAX;
1052 else
1053 remaining = msec;
1054
1055 while (remaining > 0 && (!cancel || !*cancel))
1056 {
1057 /*
1058 * Check for input...
1059 */
1060
a29fd7dd
MS
1061# ifdef HAVE_DNSSD
1062# ifdef HAVE_POLL
dcb445bc
MS
1063 pfd.fd = main_fd;
1064 pfd.events = POLLIN;
1065
1066 nfds = poll(&pfd, 1, remaining > 250 ? 250 : remaining);
1067
a29fd7dd 1068# else
dcb445bc
MS
1069 FD_ZERO(&input);
1070 FD_SET(main_fd, &input);
1071
1072 timeout.tv_sec = 0;
1073 timeout.tv_usec = remaining > 250 ? 250000 : remaining * 1000;
1074
1075 nfds = select(main_fd + 1, &input, NULL, NULL, &timeout);
a29fd7dd 1076# endif /* HAVE_POLL */
dcb445bc
MS
1077
1078 if (nfds > 0)
1079 DNSServiceProcessResult(data.main_ref);
1080 else if (nfds == 0)
1081 remaining -= 250;
1082
a29fd7dd
MS
1083# else /* HAVE_AVAHI */
1084 data.got_data = 0;
1085
1086 if ((error = avahi_simple_poll_iterate(data.simple_poll, 250)) > 0)
1087 {
1088 /*
1089 * We've been told to exit the loop. Perhaps the connection to
1090 * Avahi failed.
1091 */
1092
1093 break;
1094 }
1095
1096 if (!data.got_data)
1097 remaining -= 250;
1098# endif /* HAVE_DNSSD */
1099
dcb445bc
MS
1100 for (device = (_cups_dnssd_device_t *)cupsArrayFirst(data.devices),
1101 count = 0;
1102 device;
1103 device = (_cups_dnssd_device_t *)cupsArrayNext(data.devices))
1104 {
1105 if (device->ref)
1106 count ++;
1107
1108 if (!device->ref && device->state == _CUPS_DNSSD_NEW)
1109 {
dcb445bc
MS
1110 DEBUG_printf(("1cupsEnumDests: Querying '%s'.", device->fullName));
1111
a29fd7dd
MS
1112# ifdef HAVE_DNSSD
1113 device->ref = data.main_ref;
1114
dcb445bc
MS
1115 if (DNSServiceQueryRecord(&(device->ref),
1116 kDNSServiceFlagsShareConnection,
1117 0, device->fullName,
1118 kDNSServiceType_TXT,
12f89d24
MS
1119 kDNSServiceClass_IN,
1120 (DNSServiceQueryRecordReply)cups_dnssd_query_cb,
dcb445bc
MS
1121 &data) == kDNSServiceErr_NoError)
1122 {
1123 count ++;
1124 }
1125 else
1126 {
1127 device->ref = 0;
1128 device->state = _CUPS_DNSSD_ERROR;
1129
1130 DEBUG_puts("1cupsEnumDests: Query failed.");
1131 }
a29fd7dd
MS
1132
1133# else /* HAVE_AVAHI */
1134 if ((device->ref = avahi_record_browser_new(data.client,
1135 AVAHI_IF_UNSPEC,
1136 AVAHI_PROTO_UNSPEC,
1137 device->fullName,
1138 AVAHI_DNS_CLASS_IN,
1139 AVAHI_DNS_TYPE_TXT,
1140 0,
1141 cups_dnssd_query_cb,
1142 &data)) != NULL)
1143 {
1144 count ++;
1145 }
1146 else
1147 {
1148 device->state = _CUPS_DNSSD_ERROR;
1149
1150 DEBUG_printf(("1cupsEnumDests: Query failed: %s",
1151 avahi_strerror(avahi_client_errno(data.client))));
1152 }
1153# endif /* HAVE_DNSSD */
dcb445bc
MS
1154 }
1155 else if (device->ref && device->state == _CUPS_DNSSD_PENDING)
1156 {
a29fd7dd
MS
1157 if ((device->type & mask) == type)
1158 {
1159 if (!(*cb)(user_data, CUPS_DEST_FLAGS_NONE, &device->dest))
1160 {
1161 remaining = -1;
1162 break;
1163 }
1164 }
dcb445bc
MS
1165
1166 device->state = _CUPS_DNSSD_ACTIVE;
1167 }
1168 }
1169 }
1170
1171 cupsArrayDelete(data.devices);
1172
a29fd7dd 1173# ifdef HAVE_DNSSD
dcb445bc
MS
1174 DNSServiceRefDeallocate(ipp_ref);
1175 DNSServiceRefDeallocate(local_ipp_ref);
1176
a29fd7dd 1177# ifdef HAVE_SSL
dcb445bc
MS
1178 DNSServiceRefDeallocate(ipp_ref);
1179 DNSServiceRefDeallocate(local_ipp_ref);
a29fd7dd 1180# endif /* HAVE_SSL */
dcb445bc
MS
1181
1182 DNSServiceRefDeallocate(data.main_ref);
a29fd7dd
MS
1183
1184# else /* HAVE_AVAHI */
1185 avahi_service_browser_free(ipp_ref);
1186# ifdef HAVE_SSL
1187 avahi_service_browser_free(ipps_ref);
1188# endif /* HAVE_SSL */
1189
1190 avahi_client_free(data.client);
1191 avahi_simple_poll_free(data.simple_poll);
1192# endif /* HAVE_DNSSD */
1193#endif /* HAVE_DNSSD || HAVE_DNSSD */
dcb445bc
MS
1194
1195 return (1);
9c80ffa2
MS
1196}
1197
1198
1199# ifdef __BLOCKS__
1200/*
1201 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
1202 *
1203 * Destinations are enumerated from one or more sources. The block receives the
dcb445bc
MS
1204 * destination name, instance, number of options, and options which can be used
1205 * as input to the @link cupsAddDest@ function. The block must return 1 to
1206 * continue enumeration or 0 to stop.
9c80ffa2 1207 *
dcb445bc
MS
1208 * Enumeration happens on the current thread and does not return until all
1209 * destinations have been enumerated or the block returns 0.
9c80ffa2 1210 *
f3c17241 1211 * @since CUPS 1.6/OS X 10.8@
9c80ffa2
MS
1212 */
1213
1214int /* O - 1 on success, 0 on failure */
1215cupsEnumDestsBlock(
dcb445bc
MS
1216 unsigned flags, /* I - Enumeration flags */
1217 int timeout, /* I - Timeout in milliseconds, 0 for indefinite */
1218 int *cancel, /* I - Pointer to "cancel" variable */
1219 cups_ptype_t type, /* I - Printer type bits */
1220 cups_ptype_t mask, /* I - Mask for printer type bits */
9c80ffa2
MS
1221 cups_dest_block_t block) /* I - Block */
1222{
dcb445bc
MS
1223 return (cupsEnumDests(flags, timeout, cancel, type, mask,
1224 (cups_dest_cb_t)cups_block_cb, (void *)block));
9c80ffa2
MS
1225}
1226# endif /* __BLOCKS__ */
1227
1228
ef416fc2 1229/*
1230 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1231 */
1232
1233void
1234cupsFreeDests(int num_dests, /* I - Number of destinations */
1235 cups_dest_t *dests) /* I - Destinations */
1236{
1237 int i; /* Looping var */
1238 cups_dest_t *dest; /* Current destination */
1239
1240
1241 if (num_dests == 0 || dests == NULL)
1242 return;
1243
1244 for (i = num_dests, dest = dests; i > 0; i --, dest ++)
1245 {
2e4ff8af
MS
1246 _cupsStrFree(dest->name);
1247 _cupsStrFree(dest->instance);
ef416fc2 1248
1249 cupsFreeOptions(dest->num_options, dest->options);
1250 }
1251
1252 free(dests);
1253}
1254
1255
1256/*
1257 * 'cupsGetDest()' - Get the named destination from the list.
1258 *
5a738aea 1259 * Use the @link cupsGetDests@ or @link cupsGetDests2@ functions to get a
ef416fc2 1260 * list of supported destinations for the current user.
1261 */
1262
5a738aea
MS
1263cups_dest_t * /* O - Destination pointer or @code NULL@ */
1264cupsGetDest(const char *name, /* I - Destination name or @code NULL@ for the default destination */
1265 const char *instance, /* I - Instance name or @code NULL@ */
ef416fc2 1266 int num_dests, /* I - Number of destinations */
1267 cups_dest_t *dests) /* I - Destinations */
1268{
426c6a59
MS
1269 int diff, /* Result of comparison */
1270 match; /* Matching index */
ef416fc2 1271
1272
2abf387c 1273 if (num_dests <= 0 || !dests)
ef416fc2 1274 return (NULL);
1275
2abf387c 1276 if (!name)
ef416fc2 1277 {
1278 /*
1279 * NULL name for default printer.
1280 */
1281
1282 while (num_dests > 0)
1283 {
1284 if (dests->is_default)
1285 return (dests);
1286
1287 num_dests --;
1288 dests ++;
1289 }
1290 }
1291 else
1292 {
1293 /*
1294 * Lookup name and optionally the instance...
1295 */
1296
426c6a59 1297 match = cups_find_dest(name, instance, num_dests, dests, -1, &diff);
ef416fc2 1298
426c6a59
MS
1299 if (!diff)
1300 return (dests + match);
ef416fc2 1301 }
1302
1303 return (NULL);
1304}
1305
1306
dcb445bc
MS
1307/*
1308 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1309 */
1310
1311const char * /* O - Printer URI */
1312_cupsGetDestResource(
1313 cups_dest_t *dest, /* I - Destination */
1314 char *resource, /* I - Resource buffer */
1315 size_t resourcesize) /* I - Size of resource buffer */
1316{
1317 const char *uri; /* Printer URI */
1318 char scheme[32], /* URI scheme */
1319 userpass[256], /* Username and password (unused) */
1320 hostname[256]; /* Hostname */
1321 int port; /* Port number */
1322
1323
1324 /*
1325 * Range check input...
1326 */
1327
1328 if (!dest || !resource || resourcesize < 1)
1329 {
1330 if (resource)
1331 *resource = '\0';
1332
cb7f98ee 1333 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
dcb445bc
MS
1334 return (NULL);
1335 }
1336
1337 /*
1338 * Grab the printer URI...
1339 */
1340
1341 if ((uri = cupsGetOption("printer-uri-supported", dest->num_options,
1342 dest->options)) == NULL)
1343 {
1344 if (resource)
1345 *resource = '\0';
1346
cb7f98ee 1347 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0);
dcb445bc
MS
1348
1349 return (NULL);
1350 }
1351
1352#ifdef HAVE_DNSSD
1353 if (strstr(uri, "._tcp"))
1354 {
1355 if ((uri = cups_dnssd_resolve(dest, uri, 5000, NULL, NULL, NULL)) == NULL)
1356 return (NULL);
1357 }
1358#endif /* HAVE_DNSSD */
1359
1360 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
1361 userpass, sizeof(userpass), hostname, sizeof(hostname),
7e86f2f6 1362 &port, resource, (int)resourcesize) < HTTP_URI_STATUS_OK)
dcb445bc 1363 {
f5f2e19e 1364 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer-uri."), 1);
dcb445bc
MS
1365
1366 return (NULL);
1367 }
1368
1369 return (uri);
1370}
1371
1372
104fd4ae
MS
1373/*
1374 * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
1375 *
1376 * "name" is the desired name for the printer. If @code NULL@, a name will be
1377 * created using the URI.
1378 *
1379 * "uri" is the "ipp" or "ipps" URI for the printer.
1380 *
e1f19878 1381 * @since CUPS 2.0/OS X 10.10@
104fd4ae
MS
1382 */
1383
1384cups_dest_t * /* O - Destination or @code NULL@ */
1385cupsGetDestWithURI(const char *name, /* I - Desired printer name or @code NULL@ */
1386 const char *uri) /* I - URI for the printer */
1387{
1388 cups_dest_t *dest; /* New destination */
1389 char temp[1024], /* Temporary string */
1390 scheme[256], /* Scheme from URI */
1391 userpass[256], /* Username:password from URI */
1392 hostname[256], /* Hostname from URI */
1393 resource[1024], /* Resource path from URI */
1394 *ptr; /* Pointer into string */
1395 int port; /* Port number from URI */
1396
1397
1398 /*
1399 * Range check input...
1400 */
1401
1402 if (!uri)
1403 {
1404 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
1405 return (NULL);
1406 }
1407
1408 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK ||
1409 (strncmp(uri, "ipp://", 6) && strncmp(uri, "ipps://", 7)))
1410 {
f5f2e19e 1411 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer-uri."), 1);
104fd4ae
MS
1412
1413 return (NULL);
1414 }
1415
1416 if (!name)
1417 {
1418 /*
1419 * Create the name from the URI...
1420 */
1421
1422 if (strstr(hostname, "._tcp"))
1423 {
1424 /*
1425 * Use the service instance name...
1426 */
1427
1428 if ((ptr = strchr(hostname, '.')) != NULL)
1429 *ptr = '\0';
1430
1431 name = hostname;
1432 }
1433 else if (!strncmp(resource, "/classes/", 9))
1434 {
1435 snprintf(temp, sizeof(temp), "%s @ %s", resource + 9, hostname);
1436 name = temp;
1437 }
1438 else if (!strncmp(resource, "/printers/", 10))
1439 {
1440 snprintf(temp, sizeof(temp), "%s @ %s", resource + 10, hostname);
1441 name = temp;
1442 }
1443 else
1444 {
1445 name = hostname;
1446 }
1447 }
1448
1449 /*
1450 * Create the destination...
1451 */
1452
1453 if ((dest = calloc(1, sizeof(cups_dest_t))) == NULL)
1454 {
1455 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
1456 return (NULL);
1457 }
1458
1459 dest->name = _cupsStrAlloc(name);
4db7fcee 1460 dest->num_options = cupsAddOption("device-uri", uri, dest->num_options, &(dest->options));
104fd4ae
MS
1461 dest->num_options = cupsAddOption("printer-info", name, dest->num_options, &(dest->options));
1462
1463 return (dest);
1464}
1465
1466
c8fef167
MS
1467/*
1468 * '_cupsGetDests()' - Get destinations from a server.
1469 *
5489ad43
MS
1470 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1471 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1472 * a known printer.
c8fef167
MS
1473 *
1474 * "name" is the name of an existing printer and is only used when "op" is
cb7f98ee 1475 * IPP_OP_GET_PRINTER_ATTRIBUTES.
c8fef167
MS
1476 *
1477 * "dest" is initialized to point to the array of destinations.
1478 *
1479 * 0 is returned if there are no printers, no default printer, or the named
1480 * printer does not exist, respectively.
1481 *
1482 * Free the memory used by the destination array using the @link cupsFreeDests@
1483 * function.
1484 *
f3c17241 1485 * Note: On OS X this function also gets the default paper from the system
c8fef167
MS
1486 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1487 * options array for each destination that supports it.
1488 */
1489
dcb445bc
MS
1490int /* O - Number of destinations */
1491_cupsGetDests(http_t *http, /* I - Connection to server or
1492 * @code CUPS_HTTP_DEFAULT@ */
1493 ipp_op_t op, /* I - IPP operation */
1494 const char *name, /* I - Name of destination */
1495 cups_dest_t **dests, /* IO - Destinations */
1496 cups_ptype_t type, /* I - Printer type bits */
1497 cups_ptype_t mask) /* I - Printer type mask */
c8fef167
MS
1498{
1499 int num_dests = 0; /* Number of destinations */
1500 cups_dest_t *dest; /* Current destination */
1501 ipp_t *request, /* IPP Request */
1502 *response; /* IPP Response */
1503 ipp_attribute_t *attr; /* Current attribute */
1504 const char *printer_name; /* printer-name attribute */
1505 char uri[1024]; /* printer-uri value */
1506 int num_options; /* Number of options */
1507 cups_option_t *options; /* Options */
1508#ifdef __APPLE__
1509 char media_default[41]; /* Default paper size */
1510#endif /* __APPLE__ */
1511 char optname[1024], /* Option name */
1512 value[2048], /* Option value */
1513 *ptr; /* Pointer into name/value */
1514 static const char * const pattrs[] = /* Attributes we're interested in */
1515 {
1516 "auth-info-required",
1517 "device-uri",
1518 "job-sheets-default",
1519 "marker-change-time",
1520 "marker-colors",
1521 "marker-high-levels",
1522 "marker-levels",
1523 "marker-low-levels",
1524 "marker-message",
1525 "marker-names",
1526 "marker-types",
1527#ifdef __APPLE__
1528 "media-supported",
1529#endif /* __APPLE__ */
1530 "printer-commands",
1531 "printer-defaults",
1532 "printer-info",
1533 "printer-is-accepting-jobs",
1534 "printer-is-shared",
1535 "printer-location",
1536 "printer-make-and-model",
5a9febac 1537 "printer-mandatory-job-attributes",
c8fef167
MS
1538 "printer-name",
1539 "printer-state",
1540 "printer-state-change-time",
1541 "printer-state-reasons",
1542 "printer-type",
1543 "printer-uri-supported"
1544 };
1545
1546
1547#ifdef __APPLE__
1548 /*
1549 * Get the default paper size...
1550 */
1551
1552 appleGetPaperSize(media_default, sizeof(media_default));
1553#endif /* __APPLE__ */
1554
0cb67df3 1555 /*
cb7f98ee 1556 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
0cb67df3
MS
1557 * require the following attributes:
1558 *
1559 * attributes-charset
1560 * attributes-natural-language
1561 * requesting-user-name
cb7f98ee 1562 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
0cb67df3 1563 */
c1420c87 1564
0cb67df3 1565 request = ippNewRequest(op);
c1420c87 1566
0cb67df3
MS
1567 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1568 "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
1569 NULL, pattrs);
c1420c87 1570
0cb67df3
MS
1571 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1572 "requesting-user-name", NULL, cupsUser());
c1420c87 1573
cb7f98ee 1574 if (name && op != IPP_OP_CUPS_GET_DEFAULT)
0cb67df3
MS
1575 {
1576 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
1577 "localhost", ippPort(), "/printers/%s", name);
1578 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
1579 uri);
1580 }
1581 else if (mask)
1582 {
7e86f2f6
MS
1583 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type", (int)type);
1584 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask", (int)mask);
c1420c87 1585 }
c8fef167 1586
0cb67df3
MS
1587 /*
1588 * Do the request and get back a response...
1589 */
1590
1591 if ((response = cupsDoRequest(http, request, "/")) != NULL)
c8fef167
MS
1592 {
1593 for (attr = response->attrs; attr != NULL; attr = attr->next)
1594 {
1595 /*
1596 * Skip leading attributes until we hit a printer...
1597 */
1598
1599 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1600 attr = attr->next;
1601
1602 if (attr == NULL)
1603 break;
1604
1605 /*
1606 * Pull the needed attributes from this printer...
1607 */
1608
1609 printer_name = NULL;
1610 num_options = 0;
1611 options = NULL;
1612
1613 for (; attr && attr->group_tag == IPP_TAG_PRINTER; attr = attr->next)
1614 {
1615 if (attr->value_tag != IPP_TAG_INTEGER &&
1616 attr->value_tag != IPP_TAG_ENUM &&
1617 attr->value_tag != IPP_TAG_BOOLEAN &&
1618 attr->value_tag != IPP_TAG_TEXT &&
1619 attr->value_tag != IPP_TAG_TEXTLANG &&
1620 attr->value_tag != IPP_TAG_NAME &&
1621 attr->value_tag != IPP_TAG_NAMELANG &&
1622 attr->value_tag != IPP_TAG_KEYWORD &&
1623 attr->value_tag != IPP_TAG_RANGE &&
1624 attr->value_tag != IPP_TAG_URI)
1625 continue;
1626
1627 if (!strcmp(attr->name, "auth-info-required") ||
1628 !strcmp(attr->name, "device-uri") ||
1629 !strcmp(attr->name, "marker-change-time") ||
1630 !strcmp(attr->name, "marker-colors") ||
1631 !strcmp(attr->name, "marker-high-levels") ||
1632 !strcmp(attr->name, "marker-levels") ||
1633 !strcmp(attr->name, "marker-low-levels") ||
1634 !strcmp(attr->name, "marker-message") ||
1635 !strcmp(attr->name, "marker-names") ||
1636 !strcmp(attr->name, "marker-types") ||
1637 !strcmp(attr->name, "printer-commands") ||
1638 !strcmp(attr->name, "printer-info") ||
1639 !strcmp(attr->name, "printer-is-shared") ||
1640 !strcmp(attr->name, "printer-make-and-model") ||
5a9febac 1641 !strcmp(attr->name, "printer-mandatory-job-attributes") ||
c8fef167
MS
1642 !strcmp(attr->name, "printer-state") ||
1643 !strcmp(attr->name, "printer-state-change-time") ||
1644 !strcmp(attr->name, "printer-type") ||
1645 !strcmp(attr->name, "printer-is-accepting-jobs") ||
1646 !strcmp(attr->name, "printer-location") ||
1647 !strcmp(attr->name, "printer-state-reasons") ||
1648 !strcmp(attr->name, "printer-uri-supported"))
1649 {
1650 /*
1651 * Add a printer description attribute...
1652 */
1653
1654 num_options = cupsAddOption(attr->name,
1655 cups_make_string(attr, value,
1656 sizeof(value)),
1657 num_options, &options);
1658 }
1659#ifdef __APPLE__
1660 else if (!strcmp(attr->name, "media-supported"))
1661 {
1662 /*
1663 * See if we can set a default media size...
1664 */
1665
1666 int i; /* Looping var */
1667
1668 for (i = 0; i < attr->num_values; i ++)
88f9aafc 1669 if (!_cups_strcasecmp(media_default, attr->values[i].string.text))
c8fef167
MS
1670 {
1671 num_options = cupsAddOption("media", media_default, num_options,
1672 &options);
1673 break;
1674 }
1675 }
1676#endif /* __APPLE__ */
1677 else if (!strcmp(attr->name, "printer-name") &&
1678 attr->value_tag == IPP_TAG_NAME)
1679 printer_name = attr->values[0].string.text;
1680 else if (strncmp(attr->name, "notify-", 7) &&
1681 (attr->value_tag == IPP_TAG_BOOLEAN ||
1682 attr->value_tag == IPP_TAG_ENUM ||
1683 attr->value_tag == IPP_TAG_INTEGER ||
1684 attr->value_tag == IPP_TAG_KEYWORD ||
1685 attr->value_tag == IPP_TAG_NAME ||
1686 attr->value_tag == IPP_TAG_RANGE) &&
1687 (ptr = strstr(attr->name, "-default")) != NULL)
1688 {
1689 /*
1690 * Add a default option...
1691 */
1692
1693 strlcpy(optname, attr->name, sizeof(optname));
1694 optname[ptr - attr->name] = '\0';
1695
88f9aafc 1696 if (_cups_strcasecmp(optname, "media") ||
c8fef167
MS
1697 !cupsGetOption("media", num_options, options))
1698 num_options = cupsAddOption(optname,
1699 cups_make_string(attr, value,
1700 sizeof(value)),
1701 num_options, &options);
1702 }
1703 }
1704
1705 /*
1706 * See if we have everything needed...
1707 */
1708
1709 if (!printer_name)
1710 {
1711 cupsFreeOptions(num_options, options);
1712
1713 if (attr == NULL)
1714 break;
1715 else
1716 continue;
1717 }
1718
1719 if ((dest = cups_add_dest(printer_name, NULL, &num_dests, dests)) != NULL)
1720 {
1721 dest->num_options = num_options;
1722 dest->options = options;
1723 }
1724 else
1725 cupsFreeOptions(num_options, options);
1726
1727 if (attr == NULL)
1728 break;
1729 }
1730
1731 ippDelete(response);
1732 }
1733
1734 /*
1735 * Return the count...
1736 */
1737
1738 return (num_dests);
1739}
1740
1741
ef416fc2 1742/*
1743 * 'cupsGetDests()' - Get the list of destinations from the default server.
ecdc0628 1744 *
1745 * Starting with CUPS 1.2, the returned list of destinations include the
1746 * printer-info, printer-is-accepting-jobs, printer-is-shared,
1747 * printer-make-and-model, printer-state, printer-state-change-time,
426c6a59
MS
1748 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
1749 * adds the marker-change-time, marker-colors, marker-high-levels,
1750 * marker-levels, marker-low-levels, marker-message, marker-names,
1751 * marker-types, and printer-commands attributes as well.
2abf387c 1752 *
5a738aea
MS
1753 * Use the @link cupsFreeDests@ function to free the destination list and
1754 * the @link cupsGetDest@ function to find a particular destination.
ef416fc2 1755 */
1756
1757int /* O - Number of destinations */
1758cupsGetDests(cups_dest_t **dests) /* O - Destinations */
1759{
3d052e43 1760 return (cupsGetDests2(CUPS_HTTP_DEFAULT, dests));
ef416fc2 1761}
1762
1763
1764/*
1765 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
1766 *
ecdc0628 1767 * Starting with CUPS 1.2, the returned list of destinations include the
1768 * printer-info, printer-is-accepting-jobs, printer-is-shared,
1769 * printer-make-and-model, printer-state, printer-state-change-time,
426c6a59
MS
1770 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
1771 * adds the marker-change-time, marker-colors, marker-high-levels,
1772 * marker-levels, marker-low-levels, marker-message, marker-names,
1773 * marker-types, and printer-commands attributes as well.
ecdc0628 1774 *
5a738aea
MS
1775 * Use the @link cupsFreeDests@ function to free the destination list and
1776 * the @link cupsGetDest@ function to find a particular destination.
2abf387c 1777 *
f3c17241 1778 * @since CUPS 1.1.21/OS X 10.4@
ef416fc2 1779 */
1780
1781int /* O - Number of destinations */
568fa3fa 1782cupsGetDests2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
ef416fc2 1783 cups_dest_t **dests) /* O - Destinations */
1784{
ef416fc2 1785 int num_dests; /* Number of destinations */
1786 cups_dest_t *dest; /* Destination pointer */
1787 const char *home; /* HOME environment variable */
b423cd4c 1788 char filename[1024]; /* Local ~/.cups/lpoptions file */
ef416fc2 1789 const char *defprinter; /* Default printer */
1790 char name[1024], /* Copy of printer name */
38e73f87
MS
1791 *instance, /* Pointer to instance name */
1792 *user_default; /* User default printer */
ef416fc2 1793 int num_reals; /* Number of real queues */
1794 cups_dest_t *reals; /* Real queues */
3d052e43 1795 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
ef416fc2 1796
1797
1798 /*
1799 * Range check the input...
1800 */
1801
3d052e43 1802 if (!dests)
1ff0402e 1803 {
cb7f98ee 1804 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad NULL dests pointer"), 1);
ef416fc2 1805 return (0);
c8fef167 1806 }
ef416fc2 1807
1808 /*
1809 * Grab the printers and classes...
1810 */
1811
c8fef167 1812 *dests = (cups_dest_t *)0;
5a08320a 1813 num_dests = _cupsGetDests(http, IPP_OP_CUPS_GET_PRINTERS, NULL, dests, 0, CUPS_PRINTER_3D);
b19ccc9e 1814
cb7f98ee 1815 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE)
b19ccc9e
MS
1816 {
1817 cupsFreeDests(num_dests, *dests);
1818 *dests = (cups_dest_t *)0;
1819 return (0);
1820 }
ef416fc2 1821
1822 /*
1823 * Make a copy of the "real" queues for a later sanity check...
1824 */
1825
1826 if (num_dests > 0)
1827 {
1828 num_reals = num_dests;
7e86f2f6 1829 reals = calloc((size_t)num_reals, sizeof(cups_dest_t));
ef416fc2 1830
1831 if (reals)
7e86f2f6 1832 memcpy(reals, *dests, (size_t)num_reals * sizeof(cups_dest_t));
ef416fc2 1833 else
1834 num_reals = 0;
1835 }
1836 else
1837 {
1838 num_reals = 0;
1839 reals = NULL;
1840 }
1841
1842 /*
1843 * Grab the default destination...
1844 */
1845
38e73f87
MS
1846 if ((user_default = _cupsUserDefault(name, sizeof(name))) != NULL)
1847 defprinter = name;
1848 else if ((defprinter = cupsGetDefault2(http)) != NULL)
1849 {
1850 strlcpy(name, defprinter, sizeof(name));
1851 defprinter = name;
1852 }
080811b1
MS
1853
1854 if (defprinter)
ef416fc2 1855 {
1856 /*
38e73f87 1857 * Separate printer and instance name...
ef416fc2 1858 */
1859
ef416fc2 1860 if ((instance = strchr(name, '/')) != NULL)
1861 *instance++ = '\0';
1862
1863 /*
1864 * Lookup the printer and instance and make it the default...
1865 */
1866
1867 if ((dest = cupsGetDest(name, instance, num_dests, *dests)) != NULL)
1868 dest->is_default = 1;
1869 }
1870 else
ef416fc2 1871 instance = NULL;
ef416fc2 1872
1873 /*
b423cd4c 1874 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
ef416fc2 1875 */
1876
1877 snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
38e73f87
MS
1878 num_dests = cups_get_dests(filename, NULL, NULL, user_default != NULL,
1879 num_dests, dests);
ef416fc2 1880
1881 if ((home = getenv("HOME")) != NULL)
1882 {
b423cd4c 1883 snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
b423cd4c 1884
38e73f87
MS
1885 num_dests = cups_get_dests(filename, NULL, NULL, user_default != NULL,
1886 num_dests, dests);
ef416fc2 1887 }
1888
1889 /*
1890 * Validate the current default destination - this prevents old
b423cd4c 1891 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
ef416fc2 1892 * pointing to a non-existent printer or class...
1893 */
1894
1895 if (num_reals)
1896 {
1897 /*
1898 * See if we have a default printer...
1899 */
1900
1901 if ((dest = cupsGetDest(NULL, NULL, num_dests, *dests)) != NULL)
1902 {
1903 /*
1904 * Have a default; see if it is real...
1905 */
1906
5489ad43
MS
1907 if (!cupsGetDest(dest->name, NULL, num_reals, reals))
1908 {
1909 /*
1910 * Remove the non-real printer from the list, since we don't want jobs
1911 * going to an unexpected printer... (<rdar://problem/14216472>)
1912 */
ef416fc2 1913
5489ad43
MS
1914 num_dests = cupsRemoveDest(dest->name, dest->instance, num_dests,
1915 dests);
1916 }
ef416fc2 1917 }
1918
1919 /*
1920 * Free memory...
1921 */
1922
1923 free(reals);
1924 }
1925
1926 /*
1927 * Return the number of destinations...
1928 */
1929
1ff0402e 1930 if (num_dests > 0)
cb7f98ee 1931 _cupsSetError(IPP_STATUS_OK, NULL, 0);
1ff0402e 1932
ef416fc2 1933 return (num_dests);
1934}
1935
1936
a4924f6c
MS
1937/*
1938 * 'cupsGetNamedDest()' - Get options for the named destination.
1939 *
1940 * This function is optimized for retrieving a single destination and should
5a738aea
MS
1941 * be used instead of @link cupsGetDests@ and @link cupsGetDest@ when you either
1942 * know the name of the destination or want to print to the default destination.
1943 * If @code NULL@ is returned, the destination does not exist or there is no
1944 * default destination.
a4924f6c 1945 *
5a738aea
MS
1946 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
1947 * server will be used.
a4924f6c 1948 *
5a738aea
MS
1949 * If "name" is @code NULL@, the default printer for the current user will be
1950 * returned.
a4924f6c 1951 *
5a738aea
MS
1952 * The returned destination must be freed using @link cupsFreeDests@ with a
1953 * "num_dests" value of 1.
a4924f6c 1954 *
f3c17241 1955 * @since CUPS 1.4/OS X 10.6@
a4924f6c
MS
1956 */
1957
5a738aea 1958cups_dest_t * /* O - Destination or @code NULL@ */
568fa3fa
MS
1959cupsGetNamedDest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1960 const char *name, /* I - Destination name or @code NULL@ for the default destination */
5a738aea 1961 const char *instance) /* I - Instance name or @code NULL@ */
a4924f6c
MS
1962{
1963 cups_dest_t *dest; /* Destination */
1964 char filename[1024], /* Path to lpoptions */
1965 defname[256]; /* Default printer name */
1966 const char *home = getenv("HOME"); /* Home directory */
38e73f87 1967 int set_as_default = 0; /* Set returned destination as default */
cb7f98ee 1968 ipp_op_t op = IPP_OP_GET_PRINTER_ATTRIBUTES;
a4924f6c
MS
1969 /* IPP operation to get server ops */
1970 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
1971
1972
a4924f6c
MS
1973 /*
1974 * If "name" is NULL, find the default destination...
1975 */
1976
1977 if (!name)
1978 {
38e73f87
MS
1979 set_as_default = 1;
1980 name = _cupsUserDefault(defname, sizeof(defname));
a4924f6c 1981
7cf5915e
MS
1982 if (name)
1983 {
1984 char *ptr; /* Temporary pointer... */
1985
1986 if ((ptr = strchr(defname, '/')) != NULL)
1987 {
1988 *ptr++ = '\0';
1989 instance = ptr;
1990 }
1991 else
1992 instance = NULL;
1993 }
1994 else if (home)
a4924f6c
MS
1995 {
1996 /*
1997 * No default in the environment, try the user's lpoptions files...
1998 */
1999
2000 snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
2001
5a6b583a 2002 name = cups_get_default(filename, defname, sizeof(defname), &instance);
a4924f6c
MS
2003 }
2004
2005 if (!name)
2006 {
2007 /*
2008 * Still not there? Try the system lpoptions file...
2009 */
2010
2011 snprintf(filename, sizeof(filename), "%s/lpoptions",
2012 cg->cups_serverroot);
2013 name = cups_get_default(filename, defname, sizeof(defname), &instance);
2014 }
2015
2016 if (!name)
2017 {
2018 /*
2019 * No locally-set default destination, ask the server...
2020 */
2021
cb7f98ee 2022 op = IPP_OP_CUPS_GET_DEFAULT;
a4924f6c
MS
2023 }
2024 }
2025
2026 /*
2027 * Get the printer's attributes...
2028 */
2029
5a08320a 2030 if (!_cupsGetDests(http, op, name, &dest, 0, CUPS_PRINTER_3D))
c606bcae 2031 return (NULL);
a4924f6c
MS
2032
2033 if (instance)
2034 dest->instance = _cupsStrAlloc(instance);
2035
38e73f87
MS
2036 if (set_as_default)
2037 dest->is_default = 1;
2038
a4924f6c
MS
2039 /*
2040 * Then add local options...
2041 */
2042
2043 snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
38e73f87 2044 cups_get_dests(filename, name, instance, 1, 1, &dest);
a4924f6c
MS
2045
2046 if (home)
2047 {
2048 snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
2049
38e73f87 2050 cups_get_dests(filename, name, instance, 1, 1, &dest);
a4924f6c
MS
2051 }
2052
2053 /*
2054 * Return the result...
2055 */
2056
2057 return (dest);
2058}
2059
2060
f7deaa1a 2061/*
2062 * 'cupsRemoveDest()' - Remove a destination from the destination list.
2063 *
2064 * Removing a destination/instance does not delete the class or printer
2065 * queue, merely the lpoptions for that destination/instance. Use the
5a738aea
MS
2066 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
2067 * options for the user.
f7deaa1a 2068 *
f3c17241 2069 * @since CUPS 1.3/OS X 10.5@
f7deaa1a 2070 */
2071
2072int /* O - New number of destinations */
2073cupsRemoveDest(const char *name, /* I - Destination name */
5a738aea 2074 const char *instance, /* I - Instance name or @code NULL@ */
f7deaa1a 2075 int num_dests, /* I - Number of destinations */
2076 cups_dest_t **dests) /* IO - Destinations */
2077{
2078 int i; /* Index into destinations */
2079 cups_dest_t *dest; /* Pointer to destination */
2080
2081
2082 /*
2083 * Find the destination...
2084 */
2085
2086 if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL)
2087 return (num_dests);
2088
2089 /*
2090 * Free memory...
2091 */
2092
2e4ff8af
MS
2093 _cupsStrFree(dest->name);
2094 _cupsStrFree(dest->instance);
f7deaa1a 2095 cupsFreeOptions(dest->num_options, dest->options);
2096
2097 /*
2098 * Remove the destination from the array...
2099 */
2100
2101 num_dests --;
2102
7e86f2f6 2103 i = (int)(dest - *dests);
f7deaa1a 2104
2105 if (i < num_dests)
7e86f2f6 2106 memmove(dest, dest + 1, (size_t)(num_dests - i) * sizeof(cups_dest_t));
f7deaa1a 2107
2108 return (num_dests);
2109}
2110
2111
2112/*
3d052e43 2113 * 'cupsSetDefaultDest()' - Set the default destination.
f7deaa1a 2114 *
f3c17241 2115 * @since CUPS 1.3/OS X 10.5@
f7deaa1a 2116 */
2117
2118void
2119cupsSetDefaultDest(
2120 const char *name, /* I - Destination name */
5a738aea 2121 const char *instance, /* I - Instance name or @code NULL@ */
f7deaa1a 2122 int num_dests, /* I - Number of destinations */
2123 cups_dest_t *dests) /* I - Destinations */
2124{
2125 int i; /* Looping var */
2126 cups_dest_t *dest; /* Current destination */
2127
2128
2129 /*
2130 * Range check input...
2131 */
2132
2133 if (!name || num_dests <= 0 || !dests)
2134 return;
2135
2136 /*
2137 * Loop through the array and set the "is_default" flag for the matching
2138 * destination...
2139 */
2140
2141 for (i = num_dests, dest = dests; i > 0; i --, dest ++)
88f9aafc 2142 dest->is_default = !_cups_strcasecmp(name, dest->name) &&
f7deaa1a 2143 ((!instance && !dest->instance) ||
2144 (instance && dest->instance &&
88f9aafc 2145 !_cups_strcasecmp(instance, dest->instance)));
f7deaa1a 2146}
2147
2148
ef416fc2 2149/*
2150 * 'cupsSetDests()' - Save the list of destinations for the default server.
2151 *
2152 * This function saves the destinations to /etc/cups/lpoptions when run
b423cd4c 2153 * as root and ~/.cups/lpoptions when run as a normal user.
ef416fc2 2154 */
2155
2156void
2157cupsSetDests(int num_dests, /* I - Number of destinations */
2158 cups_dest_t *dests) /* I - Destinations */
2159{
3d052e43 2160 cupsSetDests2(CUPS_HTTP_DEFAULT, num_dests, dests);
ef416fc2 2161}
2162
2163
2164/*
2165 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
2166 *
2167 * This function saves the destinations to /etc/cups/lpoptions when run
b423cd4c 2168 * as root and ~/.cups/lpoptions when run as a normal user.
ef416fc2 2169 *
f3c17241 2170 * @since CUPS 1.1.21/OS X 10.4@
ef416fc2 2171 */
2172
2173int /* O - 0 on success, -1 on error */
568fa3fa 2174cupsSetDests2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
ef416fc2 2175 int num_dests, /* I - Number of destinations */
2176 cups_dest_t *dests) /* I - Destinations */
2177{
2178 int i, j; /* Looping vars */
2179 int wrote; /* Wrote definition? */
2180 cups_dest_t *dest; /* Current destination */
2181 cups_option_t *option; /* Current option */
8ca02f3c 2182 _ipp_option_t *match; /* Matching attribute for option */
ef416fc2 2183 FILE *fp; /* File pointer */
b86bc4cf 2184#ifndef WIN32
ef416fc2 2185 const char *home; /* HOME environment variable */
b86bc4cf 2186#endif /* WIN32 */
ef416fc2 2187 char filename[1024]; /* lpoptions file */
2188 int num_temps; /* Number of temporary destinations */
82cc1f9a 2189 cups_dest_t *temps = NULL, /* Temporary destinations */
ef416fc2 2190 *temp; /* Current temporary dest */
2191 const char *val; /* Value of temporary option */
3d052e43 2192 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
ef416fc2 2193
2194
2195 /*
2196 * Range check the input...
2197 */
2198
3d052e43 2199 if (!num_dests || !dests)
ef416fc2 2200 return (-1);
2201
2202 /*
2203 * Get the server destinations...
2204 */
2205
5a08320a 2206 num_temps = _cupsGetDests(http, IPP_OP_CUPS_GET_PRINTERS, NULL, &temps, 0, CUPS_PRINTER_3D);
426c6a59 2207
cb7f98ee 2208 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE)
426c6a59
MS
2209 {
2210 cupsFreeDests(num_temps, temps);
2211 return (-1);
2212 }
ef416fc2 2213
2214 /*
2215 * Figure out which file to write to...
2216 */
2217
2218 snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
2219
2220#ifndef WIN32
2221 if (getuid())
2222 {
2223 /*
2224 * Merge in server defaults...
2225 */
2226
38e73f87 2227 num_temps = cups_get_dests(filename, NULL, NULL, 0, num_temps, &temps);
ef416fc2 2228
2229 /*
2230 * Point to user defaults...
2231 */
2232
2233 if ((home = getenv("HOME")) != NULL)
b423cd4c 2234 {
b423cd4c 2235 /*
2236 * Create ~/.cups subdirectory...
2237 */
2238
2239 snprintf(filename, sizeof(filename), "%s/.cups", home);
2240 if (access(filename, 0))
2241 mkdir(filename, 0700);
2242
2243 snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
2244 }
ef416fc2 2245 }
2246#endif /* !WIN32 */
2247
2248 /*
2249 * Try to open the file...
2250 */
2251
2252 if ((fp = fopen(filename, "w")) == NULL)
2253 {
2254 cupsFreeDests(num_temps, temps);
2255 return (-1);
2256 }
2257
d6ae789d 2258#ifndef WIN32
2259 /*
2260 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2261 * file...
2262 */
2263
2264 if (!getuid())
2265 fchmod(fileno(fp), 0644);
2266#endif /* !WIN32 */
2267
ef416fc2 2268 /*
2269 * Write each printer; each line looks like:
2270 *
2271 * Dest name[/instance] options
2272 * Default name[/instance] options
2273 */
2274
2275 for (i = num_dests, dest = dests; i > 0; i --, dest ++)
2276 if (dest->instance != NULL || dest->num_options != 0 || dest->is_default)
2277 {
2278 if (dest->is_default)
2279 {
2280 fprintf(fp, "Default %s", dest->name);
2281 if (dest->instance)
2282 fprintf(fp, "/%s", dest->instance);
2283
2284 wrote = 1;
2285 }
2286 else
2287 wrote = 0;
2288
2289 if ((temp = cupsGetDest(dest->name, dest->instance, num_temps, temps)) == NULL)
2290 temp = cupsGetDest(dest->name, NULL, num_temps, temps);
2291
2292 for (j = dest->num_options, option = dest->options; j > 0; j --, option ++)
2293 {
8ca02f3c 2294 /*
2295 * See if this option is a printer attribute; if so, skip it...
2296 */
2297
2298 if ((match = _ippFindOption(option->name)) != NULL &&
2299 match->group_tag == IPP_TAG_PRINTER)
2300 continue;
2301
ef416fc2 2302 /*
2303 * See if the server/global options match these; if so, don't
2304 * write 'em.
2305 */
2306
8ca02f3c 2307 if (temp &&
2308 (val = cupsGetOption(option->name, temp->num_options,
2309 temp->options)) != NULL &&
88f9aafc 2310 !_cups_strcasecmp(val, option->value))
8ca02f3c 2311 continue;
ef416fc2 2312
2313 /*
2314 * Options don't match, write to the file...
2315 */
2316
2317 if (!wrote)
2318 {
2319 fprintf(fp, "Dest %s", dest->name);
2320 if (dest->instance)
2321 fprintf(fp, "/%s", dest->instance);
2322 wrote = 1;
2323 }
88f9aafc 2324
ef416fc2 2325 if (option->value[0])
2326 {
8ca02f3c 2327 if (strchr(option->value, ' ') ||
2328 strchr(option->value, '\\') ||
2329 strchr(option->value, '\"') ||
2330 strchr(option->value, '\''))
2331 {
2332 /*
2333 * Quote the value...
2334 */
2335
2336 fprintf(fp, " %s=\"", option->name);
2337
2338 for (val = option->value; *val; val ++)
2339 {
2340 if (strchr("\"\'\\", *val))
2341 putc('\\', fp);
2342
2343 putc(*val, fp);
2344 }
2345
2346 putc('\"', fp);
2347 }
2348 else
2349 {
2350 /*
2351 * Store the literal value...
2352 */
2353
ef416fc2 2354 fprintf(fp, " %s=%s", option->name, option->value);
8ca02f3c 2355 }
ef416fc2 2356 }
2357 else
2358 fprintf(fp, " %s", option->name);
2359 }
2360
2361 if (wrote)
2362 fputs("\n", fp);
2363 }
2364
2365 /*
fa73b229 2366 * Free the temporary destinations and close the file...
ef416fc2 2367 */
2368
2369 cupsFreeDests(num_temps, temps);
2370
fa73b229 2371 fclose(fp);
2372
080811b1
MS
2373#ifdef __APPLE__
2374 /*
2375 * Set the default printer for this location - this allows command-line
2376 * and GUI applications to share the same default destination...
2377 */
2378
2379 if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) != NULL)
f14324a7
MS
2380 {
2381 CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault,
2382 dest->name,
2383 kCFStringEncodingUTF8);
2384 /* Default printer name */
2385
2386 if (name)
2387 {
2388 _cupsAppleSetDefaultPrinter(name);
2389 CFRelease(name);
2390 }
2391 }
080811b1
MS
2392#endif /* __APPLE__ */
2393
fa73b229 2394#ifdef HAVE_NOTIFY_POST
ef416fc2 2395 /*
fa73b229 2396 * Send a notification so that MacOS X applications can know about the
2397 * change, too.
ef416fc2 2398 */
2399
fa73b229 2400 notify_post("com.apple.printerListChange");
2401#endif /* HAVE_NOTIFY_POST */
ef416fc2 2402
2403 return (0);
2404}
2405
2406
38e73f87
MS
2407/*
2408 * '_cupsUserDefault()' - Get the user default printer from environment
2409 * variables and location information.
2410 */
2411
2412char * /* O - Default printer or NULL */
2413_cupsUserDefault(char *name, /* I - Name buffer */
2414 size_t namesize) /* I - Size of name buffer */
2415{
2416 const char *env; /* LPDEST or PRINTER env variable */
2417#ifdef __APPLE__
f14324a7 2418 CFStringRef locprinter; /* Last printer as this location */
38e73f87
MS
2419#endif /* __APPLE__ */
2420
2421
2422 if ((env = getenv("LPDEST")) == NULL)
2423 if ((env = getenv("PRINTER")) != NULL && !strcmp(env, "lp"))
2424 env = NULL;
2425
2426 if (env)
2427 {
2428 strlcpy(name, env, namesize);
2429 return (name);
2430 }
2431
2432#ifdef __APPLE__
2433 /*
2434 * Use location-based defaults if "use last printer" is selected in the
2435 * system preferences...
2436 */
2437
f14324a7 2438 if ((locprinter = _cupsAppleCopyDefaultPrinter()) != NULL)
38e73f87 2439 {
7e86f2f6 2440 CFStringGetCString(locprinter, name, (CFIndex)namesize, kCFStringEncodingUTF8);
f14324a7
MS
2441 CFRelease(locprinter);
2442 }
38e73f87
MS
2443 else
2444 name[0] = '\0';
2445
f14324a7 2446 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name));
38e73f87
MS
2447
2448 return (*name ? name : NULL);
2449
2450#else
2451 /*
2452 * No location-based defaults on this platform...
2453 */
2454
2455 name[0] = '\0';
2456 return (NULL);
2457#endif /* __APPLE__ */
2458}
2459
2460
080811b1 2461#ifdef __APPLE__
06d4e77b
MS
2462/*
2463 * 'appleCopyLocations()' - Copy the location history array.
2464 */
2465
2466static CFArrayRef /* O - Location array or NULL */
2467appleCopyLocations(void)
2468{
2469 CFArrayRef locations; /* Location array */
2470
2471
2472 /*
2473 * Look up the location array in the preferences...
2474 */
2475
f14324a7
MS
2476 if ((locations = CFPreferencesCopyAppValue(kLastUsedPrintersKey,
2477 kCUPSPrintingPrefs)) == NULL)
06d4e77b
MS
2478 return (NULL);
2479
2480 if (CFGetTypeID(locations) != CFArrayGetTypeID())
2481 {
2482 CFRelease(locations);
2483 return (NULL);
2484 }
2485
2486 return (locations);
2487}
2488
2489
749b1e90
MS
2490/*
2491 * 'appleCopyNetwork()' - Get the network ID for the current location.
2492 */
2493
2494static CFStringRef /* O - Network ID */
2495appleCopyNetwork(void)
2496{
2497 SCDynamicStoreRef dynamicStore; /* System configuration data */
2498 CFStringRef key; /* Current network configuration key */
2499 CFDictionaryRef ip_dict; /* Network configuration data */
2500 CFStringRef network = NULL; /* Current network ID */
88f9aafc 2501
749b1e90 2502
f14324a7 2503 if ((dynamicStore = SCDynamicStoreCreate(NULL, CFSTR("libcups"), NULL,
749b1e90
MS
2504 NULL)) != NULL)
2505 {
f14324a7
MS
2506 /*
2507 * First use the IPv6 router address, if available, since that will generally
2508 * be a globally-unique link-local address.
2509 */
2510
749b1e90 2511 if ((key = SCDynamicStoreKeyCreateNetworkGlobalEntity(
f14324a7 2512 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6)) != NULL)
749b1e90
MS
2513 {
2514 if ((ip_dict = SCDynamicStoreCopyValue(dynamicStore, key)) != NULL)
2515 {
2516 if ((network = CFDictionaryGetValue(ip_dict,
f14324a7 2517 kSCPropNetIPv6Router)) != NULL)
749b1e90
MS
2518 CFRetain(network);
2519
2520 CFRelease(ip_dict);
2521 }
2522
2523 CFRelease(key);
2524 }
2525
f14324a7
MS
2526 /*
2527 * If that doesn't work, try the IPv4 router address. This isn't as unique
2528 * and will likely be a 10.x.y.z or 192.168.y.z address...
2529 */
2530
2531 if (!network)
2532 {
2533 if ((key = SCDynamicStoreKeyCreateNetworkGlobalEntity(
2534 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)) != NULL)
2535 {
2536 if ((ip_dict = SCDynamicStoreCopyValue(dynamicStore, key)) != NULL)
2537 {
2538 if ((network = CFDictionaryGetValue(ip_dict,
2539 kSCPropNetIPv4Router)) != NULL)
2540 CFRetain(network);
2541
2542 CFRelease(ip_dict);
2543 }
2544
2545 CFRelease(key);
2546 }
2547 }
2548
749b1e90
MS
2549 CFRelease(dynamicStore);
2550 }
2551
2552 return (network);
2553}
2554
2555
426c6a59
MS
2556/*
2557 * 'appleGetPaperSize()' - Get the default paper size.
2558 */
2559
5a9febac 2560static char * /* O - Default paper size */
d649be59
MS
2561appleGetPaperSize(char *name, /* I - Paper size name buffer */
2562 size_t namesize) /* I - Size of buffer */
426c6a59 2563{
54afec33 2564 CFStringRef defaultPaperID; /* Default paper ID */
6961465f 2565 pwg_media_t *pwgmedia; /* PWG media size */
426c6a59
MS
2566
2567
c8fef167 2568 defaultPaperID = _cupsAppleCopyDefaultPaperID();
426c6a59 2569 if (!defaultPaperID ||
557dde9f 2570 CFGetTypeID(defaultPaperID) != CFStringGetTypeID() ||
d649be59 2571 !CFStringGetCString(defaultPaperID, name, (CFIndex)namesize, kCFStringEncodingUTF8))
426c6a59 2572 name[0] = '\0';
6961465f 2573 else if ((pwgmedia = pwgMediaForLegacy(name)) != NULL)
c168a833 2574 strlcpy(name, pwgmedia->pwg, namesize);
426c6a59
MS
2575
2576 if (defaultPaperID)
2577 CFRelease(defaultPaperID);
2578
2579 return (name);
2580}
2581
2582
080811b1
MS
2583/*
2584 * 'appleGetPrinter()' - Get a printer from the history array.
2585 */
2586
2587static CFStringRef /* O - Printer name or NULL */
2588appleGetPrinter(CFArrayRef locations, /* I - Location array */
2589 CFStringRef network, /* I - Network name */
2590 CFIndex *locindex) /* O - Index in array */
2591{
2592 CFIndex i, /* Looping var */
2593 count; /* Number of locations */
2594 CFDictionaryRef location; /* Current location */
2595 CFStringRef locnetwork, /* Current network */
2596 locprinter; /* Current printer */
2597
2598
2599 for (i = 0, count = CFArrayGetCount(locations); i < count; i ++)
2600 if ((location = CFArrayGetValueAtIndex(locations, i)) != NULL &&
2601 CFGetTypeID(location) == CFDictionaryGetTypeID())
2602 {
2603 if ((locnetwork = CFDictionaryGetValue(location,
2604 kLocationNetworkKey)) != NULL &&
2605 CFGetTypeID(locnetwork) == CFStringGetTypeID() &&
2606 CFStringCompare(network, locnetwork, 0) == kCFCompareEqualTo &&
2607 (locprinter = CFDictionaryGetValue(location,
2608 kLocationPrinterIDKey)) != NULL &&
2609 CFGetTypeID(locprinter) == CFStringGetTypeID())
2610 {
2611 if (locindex)
2612 *locindex = i;
2613
2614 return (locprinter);
2615 }
2616 }
2617
2618 return (NULL);
2619}
080811b1
MS
2620#endif /* __APPLE__ */
2621
2622
426c6a59
MS
2623/*
2624 * 'cups_add_dest()' - Add a destination to the array.
2625 *
2626 * Unlike cupsAddDest(), this function does not check for duplicates.
2627 */
2628
2629static cups_dest_t * /* O - New destination */
2630cups_add_dest(const char *name, /* I - Name of destination */
2631 const char *instance, /* I - Instance or NULL */
2632 int *num_dests, /* IO - Number of destinations */
2633 cups_dest_t **dests) /* IO - Destinations */
2634{
2635 int insert, /* Insertion point */
2636 diff; /* Result of comparison */
2637 cups_dest_t *dest; /* Destination pointer */
2638
2639
2640 /*
2641 * Add new destination...
2642 */
2643
2644 if (*num_dests == 0)
2645 dest = malloc(sizeof(cups_dest_t));
2646 else
7e86f2f6 2647 dest = realloc(*dests, sizeof(cups_dest_t) * (size_t)(*num_dests + 1));
426c6a59
MS
2648
2649 if (!dest)
2650 return (NULL);
2651
2652 *dests = dest;
2653
2654 /*
2655 * Find where to insert the destination...
2656 */
2657
2658 if (*num_dests == 0)
2659 insert = 0;
2660 else
2661 {
2662 insert = cups_find_dest(name, instance, *num_dests, *dests, *num_dests - 1,
2663 &diff);
2664
2665 if (diff > 0)
2666 insert ++;
2667 }
2668
2669 /*
2670 * Move the array elements as needed...
2671 */
2672
2673 if (insert < *num_dests)
7e86f2f6 2674 memmove(*dests + insert + 1, *dests + insert, (size_t)(*num_dests - insert) * sizeof(cups_dest_t));
426c6a59
MS
2675
2676 (*num_dests) ++;
2677
2678 /*
2679 * Initialize the destination...
2680 */
2681
2682 dest = *dests + insert;
2683 dest->name = _cupsStrAlloc(name);
2684 dest->instance = _cupsStrAlloc(instance);
2685 dest->is_default = 0;
2686 dest->num_options = 0;
2687 dest->options = (cups_option_t *)0;
2688
2689 return (dest);
2690}
2691
2692
9c80ffa2
MS
2693# ifdef __BLOCKS__
2694/*
2695 * 'cups_block_cb()' - Enumeration callback for block API.
2696 */
2697
2698static int /* O - 1 to continue, 0 to stop */
2699cups_block_cb(
2700 cups_dest_block_t block, /* I - Block */
dcb445bc
MS
2701 unsigned flags, /* I - Destination flags */
2702 cups_dest_t *dest) /* I - Destination */
9c80ffa2 2703{
dcb445bc 2704 return ((block)(flags, dest));
9c80ffa2
MS
2705}
2706# endif /* __BLOCKS__ */
2707
2708
426c6a59
MS
2709/*
2710 * 'cups_compare_dests()' - Compare two destinations.
2711 */
2712
2713static int /* O - Result of comparison */
2714cups_compare_dests(cups_dest_t *a, /* I - First destination */
2715 cups_dest_t *b) /* I - Second destination */
2716{
2717 int diff; /* Difference */
2718
2719
88f9aafc 2720 if ((diff = _cups_strcasecmp(a->name, b->name)) != 0)
426c6a59
MS
2721 return (diff);
2722 else if (a->instance && b->instance)
88f9aafc 2723 return (_cups_strcasecmp(a->instance, b->instance));
426c6a59
MS
2724 else
2725 return ((a->instance && !b->instance) - (!a->instance && b->instance));
2726}
2727
2728
a29fd7dd
MS
2729#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2730# ifdef HAVE_DNSSD
dcb445bc
MS
2731/*
2732 * 'cups_dnssd_browse_cb()' - Browse for printers.
2733 */
2734
2735static void
2736cups_dnssd_browse_cb(
2737 DNSServiceRef sdRef, /* I - Service reference */
2738 DNSServiceFlags flags, /* I - Option flags */
2739 uint32_t interfaceIndex, /* I - Interface number */
2740 DNSServiceErrorType errorCode, /* I - Error, if any */
2741 const char *serviceName, /* I - Name of service/device */
2742 const char *regtype, /* I - Type of service */
2743 const char *replyDomain, /* I - Service domain */
2744 void *context) /* I - Enumeration data */
2745{
2746 _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context;
2747 /* Enumeration data */
2748
2749
2750 DEBUG_printf(("5cups_dnssd_browse_cb(sdRef=%p, flags=%x, "
2751 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
2752 "regtype=\"%s\", replyDomain=\"%s\", context=%p)",
2753 sdRef, flags, interfaceIndex, errorCode, serviceName, regtype,
2754 replyDomain, context));
2755
2756 /*
2757 * Don't do anything on error...
2758 */
2759
2760 if (errorCode != kDNSServiceErr_NoError)
2761 return;
2762
2763 /*
2764 * Get the device...
2765 */
2766
2767 cups_dnssd_get_device(data, serviceName, regtype, replyDomain);
2768}
2769
2770
a29fd7dd
MS
2771# else /* HAVE_AVAHI */
2772/*
2773 * 'cups_dnssd_browse_cb()' - Browse for printers.
2774 */
2775
2776static void
2777cups_dnssd_browse_cb(
2778 AvahiServiceBrowser *browser, /* I - Browser */
2779 AvahiIfIndex interface, /* I - Interface index (unused) */
2780 AvahiProtocol protocol, /* I - Network protocol (unused) */
2781 AvahiBrowserEvent event, /* I - What happened */
2782 const char *name, /* I - Service name */
2783 const char *type, /* I - Registration type */
2784 const char *domain, /* I - Domain */
2785 AvahiLookupResultFlags flags, /* I - Flags */
2786 void *context) /* I - Devices array */
2787{
37e7e6e0 2788#ifdef DEBUG
a29fd7dd
MS
2789 AvahiClient *client = avahi_service_browser_get_client(browser);
2790 /* Client information */
37e7e6e0 2791#endif /* DEBUG */
a29fd7dd
MS
2792 _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context;
2793 /* Enumeration data */
2794
2795
2796 (void)interface;
2797 (void)protocol;
2798 (void)context;
2799
2800 switch (event)
2801 {
2802 case AVAHI_BROWSER_FAILURE:
2803 DEBUG_printf(("cups_dnssd_browse_cb: %s",
2804 avahi_strerror(avahi_client_errno(client))));
2805 avahi_simple_poll_quit(data->simple_poll);
2806 break;
2807
2808 case AVAHI_BROWSER_NEW:
2809 /*
2810 * This object is new on the network.
2811 */
2812
2813 if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
2814 {
2815 /*
2816 * This comes from the local machine so ignore it.
2817 */
2818
2819 DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".",
2820 name));
2821 }
2822 else
2823 {
2824 /*
2825 * Create a device entry for it if it doesn't yet exist.
2826 */
2827
2828 cups_dnssd_get_device(data, name, type, domain);
2829 }
2830 break;
2831
2832 case AVAHI_BROWSER_REMOVE:
2833 case AVAHI_BROWSER_ALL_FOR_NOW:
2834 case AVAHI_BROWSER_CACHE_EXHAUSTED:
2835 break;
2836 }
2837}
2838
2839
2840/*
2841 * 'cups_dnssd_client_cb()' - Avahi client callback function.
2842 */
2843
2844static void
2845cups_dnssd_client_cb(
2846 AvahiClient *client, /* I - Client information (unused) */
2847 AvahiClientState state, /* I - Current state */
2848 void *context) /* I - User data (unused) */
2849{
2850 _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context;
2851 /* Enumeration data */
2852
2853
2854 (void)client;
2855
2856 /*
2857 * If the connection drops, quit.
2858 */
2859
2860 if (state == AVAHI_CLIENT_FAILURE)
2861 {
2862 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
2863 avahi_simple_poll_quit(data->simple_poll);
2864 }
2865}
2866# endif /* HAVE_DNSSD */
2867
2868
dcb445bc
MS
2869/*
2870 * 'cups_dnssd_compare_device()' - Compare two devices.
2871 */
2872
2873static int /* O - Result of comparison */
2874cups_dnssd_compare_devices(
2875 _cups_dnssd_device_t *a, /* I - First device */
2876 _cups_dnssd_device_t *b) /* I - Second device */
2877{
2878 return (strcmp(a->dest.name, b->dest.name));
2879}
2880
2881
2882/*
2883 * 'cups_dnssd_free_device()' - Free the memory used by a device.
2884 */
2885
2886static void
2887cups_dnssd_free_device(
2888 _cups_dnssd_device_t *device, /* I - Device */
2889 _cups_dnssd_data_t *data) /* I - Enumeration data */
2890{
2891 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", device,
2892 device->dest.name, data));
2893
a29fd7dd 2894# ifdef HAVE_DNSSD
dcb445bc
MS
2895 if (device->ref)
2896 DNSServiceRefDeallocate(device->ref);
a29fd7dd
MS
2897# else /* HAVE_AVAHI */
2898 if (device->ref)
2899 avahi_record_browser_free(device->ref);
2900# endif /* HAVE_DNSSD */
dcb445bc
MS
2901
2902 _cupsStrFree(device->domain);
2903 _cupsStrFree(device->fullName);
2904 _cupsStrFree(device->regtype);
2905 _cupsStrFree(device->dest.name);
2906
2907 cupsFreeOptions(device->dest.num_options, device->dest.options);
2908
2909 free(device);
2910}
2911
2912
2913/*
2914 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
2915 */
2916
2917static _cups_dnssd_device_t * /* O - Device */
2918cups_dnssd_get_device(
2919 _cups_dnssd_data_t *data, /* I - Enumeration data */
2920 const char *serviceName, /* I - Service name */
2921 const char *regtype, /* I - Registration type */
2922 const char *replyDomain) /* I - Domain name */
2923{
2924 _cups_dnssd_device_t key, /* Search key */
2925 *device; /* Device */
2926 char fullName[kDNSServiceMaxDomainName];
2927 /* Full name for query */
2928
2929
2930 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", "
2931 "regtype=\"%s\", replyDomain=\"%s\")", data, serviceName,
2932 regtype, replyDomain));
2933
2934 /*
2935 * See if this is an existing device...
2936 */
2937
2938 key.dest.name = (char *)serviceName;
2939
2940 if ((device = cupsArrayFind(data->devices, &key)) != NULL)
2941 {
2942 /*
2943 * Yes, see if we need to do anything with this...
2944 */
2945
2946 int update = 0; /* Non-zero if we need to update */
2947
2948 if (!_cups_strcasecmp(replyDomain, "local.") &&
2949 _cups_strcasecmp(device->domain, replyDomain))
2950 {
2951 /*
2952 * Update the "global" listing to use the .local domain name instead.
2953 */
2954
2955 _cupsStrFree(device->domain);
2956 device->domain = _cupsStrAlloc(replyDomain);
2957
2958 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
2959 "domain.", device->dest.name));
2960
2961 update = 1;
2962 }
2963
2964 if (!_cups_strcasecmp(regtype, "_ipps._tcp") &&
2965 _cups_strcasecmp(device->regtype, regtype))
2966 {
2967 /*
2968 * Prefer IPPS over IPP.
2969 */
2970
2971 _cupsStrFree(device->regtype);
2972 device->regtype = _cupsStrAlloc(regtype);
2973
2974 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
2975 device->dest.name));
2976
2977 update = 1;
2978 }
2979
2980 if (!update)
2981 {
2982 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
2983 device->dest.name));
2984 return (device);
2985 }
2986 }
2987 else
2988 {
2989 /*
2990 * No, add the device...
2991 */
2992
2993 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
2994 "'%s'.", serviceName,
2995 !strcmp(regtype, "_ipps._tcp") ? "IPPS" : "IPP",
2996 replyDomain));
2997
2998 device = calloc(sizeof(_cups_dnssd_device_t), 1);
2999 device->dest.name = _cupsStrAlloc(serviceName);
3000 device->domain = _cupsStrAlloc(replyDomain);
3001 device->regtype = _cupsStrAlloc(regtype);
3002
3003 cupsArrayAdd(data->devices, device);
3004 }
3005
3006 /*
3007 * Set the "full name" of this service, which is used for queries...
3008 */
3009
a29fd7dd 3010# ifdef HAVE_DNSSD
dcb445bc
MS
3011 DNSServiceConstructFullName(fullName, device->dest.name, device->regtype,
3012 device->domain);
a29fd7dd
MS
3013# else /* HAVE_AVAHI */
3014 avahi_service_name_join(fullName, kDNSServiceMaxDomainName, serviceName,
3015 regtype, replyDomain);
3016# endif /* HAVE_DNSSD */
3017
dcb445bc
MS
3018 _cupsStrFree(device->fullName);
3019 device->fullName = _cupsStrAlloc(fullName);
3020
3021 if (device->ref)
3022 {
a29fd7dd 3023# ifdef HAVE_DNSSD
dcb445bc 3024 DNSServiceRefDeallocate(device->ref);
a29fd7dd
MS
3025# else /* HAVE_AVAHI */
3026 avahi_record_browser_free(device->ref);
3027# endif /* HAVE_DNSSD */
3028
dcb445bc
MS
3029 device->ref = 0;
3030 }
3031
3032 if (device->state == _CUPS_DNSSD_ACTIVE)
3033 {
3034 (*data->cb)(data->user_data, CUPS_DEST_FLAGS_REMOVED, &device->dest);
3035 device->state = _CUPS_DNSSD_NEW;
3036 }
3037
3038 return (device);
3039}
3040
3041
a29fd7dd 3042# ifdef HAVE_DNSSD
dcb445bc
MS
3043/*
3044 * 'cups_dnssd_local_cb()' - Browse for local printers.
3045 */
3046
3047static void
3048cups_dnssd_local_cb(
3049 DNSServiceRef sdRef, /* I - Service reference */
3050 DNSServiceFlags flags, /* I - Option flags */
3051 uint32_t interfaceIndex, /* I - Interface number */
3052 DNSServiceErrorType errorCode, /* I - Error, if any */
3053 const char *serviceName, /* I - Name of service/device */
3054 const char *regtype, /* I - Type of service */
3055 const char *replyDomain, /* I - Service domain */
3056 void *context) /* I - Devices array */
3057{
3058 _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context;
3059 /* Enumeration data */
3060 _cups_dnssd_device_t *device; /* Device */
3061
3062
3063 DEBUG_printf(("5cups_dnssd_local_cb(sdRef=%p, flags=%x, "
3064 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
3065 "regtype=\"%s\", replyDomain=\"%s\", context=%p)",
3066 sdRef, flags, interfaceIndex, errorCode, serviceName,
3067 regtype, replyDomain, context));
3068
3069 /*
3070 * Only process "add" data...
3071 */
3072
3073 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
3074 return;
3075
3076 /*
3077 * Get the device...
3078 */
3079
3080 device = cups_dnssd_get_device(data, serviceName, regtype, replyDomain);
3081
3082 /*
3083 * Hide locally-registered devices...
3084 */
3085
3086 DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.",
3087 serviceName));
3088
3089 if (device->ref)
3090 {
3091 DNSServiceRefDeallocate(device->ref);
3092 device->ref = 0;
3093 }
3094
3095 if (device->state == _CUPS_DNSSD_ACTIVE)
3096 (*data->cb)(data->user_data, CUPS_DEST_FLAGS_REMOVED, &device->dest);
3097
3098 device->state = _CUPS_DNSSD_LOCAL;
3099}
a29fd7dd
MS
3100# endif /* HAVE_DNSSD */
3101
3102
3103# ifdef HAVE_AVAHI
3104/*
3105 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
3106 *
3107 * Note: This function is needed because avahi_simple_poll_iterate is broken
3108 * and always uses a timeout of 0 (!) milliseconds.
3109 * (Avahi Ticket #364)
3110 */
3111
3112static int /* O - Number of file descriptors matching */
3113cups_dnssd_poll_cb(
3114 struct pollfd *pollfds, /* I - File descriptors */
3115 unsigned int num_pollfds, /* I - Number of file descriptors */
3116 int timeout, /* I - Timeout in milliseconds (unused) */
3117 void *context) /* I - User data (unused) */
3118{
3119 _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context;
3120 /* Enumeration data */
3121 int val; /* Return value */
3122
3123
3124 (void)timeout;
3125
3126 val = poll(pollfds, num_pollfds, 250);
3127
3128 if (val < 0)
3129 {
3130 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno)));
3131 }
3132 else if (val > 0)
3133 data->got_data = 1;
3134
3135 return (val);
3136}
3137# endif /* HAVE_AVAHI */
dcb445bc
MS
3138
3139
3140/*
3141 * 'cups_dnssd_query_cb()' - Process query data.
3142 */
3143
a29fd7dd 3144# ifdef HAVE_DNSSD
dcb445bc
MS
3145static void
3146cups_dnssd_query_cb(
3147 DNSServiceRef sdRef, /* I - Service reference */
3148 DNSServiceFlags flags, /* I - Data flags */
3149 uint32_t interfaceIndex, /* I - Interface */
3150 DNSServiceErrorType errorCode, /* I - Error, if any */
3151 const char *fullName, /* I - Full service name */
3152 uint16_t rrtype, /* I - Record type */
3153 uint16_t rrclass, /* I - Record class */
3154 uint16_t rdlen, /* I - Length of record data */
3155 const void *rdata, /* I - Record data */
3156 uint32_t ttl, /* I - Time-to-live */
3157 void *context) /* I - Enumeration data */
3158{
a29fd7dd
MS
3159# else /* HAVE_AVAHI */
3160static void
3161cups_dnssd_query_cb(
3162 AvahiRecordBrowser *browser, /* I - Record browser */
3163 AvahiIfIndex interfaceIndex,
3164 /* I - Interface index (unused) */
3165 AvahiProtocol protocol, /* I - Network protocol (unused) */
3166 AvahiBrowserEvent event, /* I - What happened? */
3167 const char *fullName, /* I - Service name */
3168 uint16_t rrclass, /* I - Record class */
3169 uint16_t rrtype, /* I - Record type */
3170 const void *rdata, /* I - TXT record */
3171 size_t rdlen, /* I - Length of TXT record */
3172 AvahiLookupResultFlags flags, /* I - Flags */
3173 void *context) /* I - Enumeration data */
3174{
37e7e6e0 3175# ifdef DEBUG
a29fd7dd
MS
3176 AvahiClient *client = avahi_record_browser_get_client(browser);
3177 /* Client information */
37e7e6e0 3178# endif /* DEBUG */
a29fd7dd 3179# endif /* HAVE_DNSSD */
dcb445bc
MS
3180 _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context;
3181 /* Enumeration data */
3182 char name[1024], /* Service name */
3183 *ptr; /* Pointer into string */
3184 _cups_dnssd_device_t dkey, /* Search key */
3185 *device; /* Device */
3186
3187
a29fd7dd 3188# ifdef HAVE_DNSSD
dcb445bc
MS
3189 DEBUG_printf(("5cups_dnssd_query_cb(sdRef=%p, flags=%x, "
3190 "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
3191 "rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, "
3192 "context=%p)", sdRef, flags, interfaceIndex, errorCode,
3193 fullName, rrtype, rrclass, rdlen, rdata, ttl, context));
3194
3195 /*
3196 * Only process "add" data...
3197 */
3198
3199 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
3200 return;
3201
a29fd7dd
MS
3202# else /* HAVE_AVAHI */
3203 DEBUG_printf(("5cups_dnssd_query_cb(browser=%p, interfaceIndex=%d, "
3204 "protocol=%d, event=%d, fullName=\"%s\", rrclass=%u, "
3205 "rrtype=%u, rdata=%p, rdlen=%u, flags=%x, context=%p)",
3206 browser, interfaceIndex, protocol, event, fullName, rrclass,
3207 rrtype, rdata, (unsigned)rdlen, flags, context));
3208
3209 /*
3210 * Only process "add" data...
3211 */
3212
3213 if (event != AVAHI_BROWSER_NEW)
3214 {
3215 if (event == AVAHI_BROWSER_FAILURE)
3216 DEBUG_printf(("cups_dnssd_query_cb: %s",
3217 avahi_strerror(avahi_client_errno(client))));
3218
3219 return;
3220 }
3221# endif /* HAVE_DNSSD */
3222
dcb445bc
MS
3223 /*
3224 * Lookup the service in the devices array.
3225 */
3226
3227 dkey.dest.name = name;
3228
3229 cups_dnssd_unquote(name, fullName, sizeof(name));
3230
3231 if ((ptr = strstr(name, "._")) != NULL)
3232 *ptr = '\0';
3233
3234 if ((device = cupsArrayFind(data->devices, &dkey)) != NULL)
3235 {
3236 /*
a29fd7dd 3237 * Found it, pull out the make and model from the TXT record and save it...
dcb445bc
MS
3238 */
3239
3240 const uint8_t *txt, /* Pointer into data */
3241 *txtnext, /* Next key/value pair */
3242 *txtend; /* End of entire TXT record */
3243 uint8_t txtlen; /* Length of current key/value pair */
3244 char key[256], /* Key string */
3245 value[256], /* Value string */
3246 make_and_model[512],
3247 /* Manufacturer and model */
3248 model[256], /* Model */
3249 uriname[1024], /* Name for URI */
3250 uri[1024]; /* Printer URI */
a29fd7dd
MS
3251 cups_ptype_t type = CUPS_PRINTER_REMOTE | CUPS_PRINTER_BW;
3252 /* Printer type */
3253 int saw_printer_type = 0;
3254 /* Did we see a printer-type key? */
dcb445bc
MS
3255
3256 device->state = _CUPS_DNSSD_PENDING;
3257 make_and_model[0] = '\0';
dcb445bc 3258
5a9febac 3259 strlcpy(model, "Unknown", sizeof(model));
dcb445bc
MS
3260
3261 for (txt = rdata, txtend = txt + rdlen;
3262 txt < txtend;
3263 txt = txtnext)
3264 {
3265 /*
3266 * Read a key/value pair starting with an 8-bit length. Since the
3267 * length is 8 bits and the size of the key/value buffers is 256, we
3268 * don't need to check for overflow...
3269 */
3270
3271 txtlen = *txt++;
3272
3273 if (!txtlen || (txt + txtlen) > txtend)
3274 break;
3275
3276 txtnext = txt + txtlen;
3277
3278 for (ptr = key; txt < txtnext && *txt != '='; txt ++)
7e86f2f6 3279 *ptr++ = (char)*txt;
dcb445bc
MS
3280 *ptr = '\0';
3281
3282 if (txt < txtnext && *txt == '=')
3283 {
3284 txt ++;
3285
3286 if (txt < txtnext)
07623986 3287 memcpy(value, txt, (size_t)(txtnext - txt));
dcb445bc
MS
3288 value[txtnext - txt] = '\0';
3289
3290 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key, value));
3291 }
3292 else
3293 {
3294 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key));
3295 continue;
3296 }
3297
3298 if (!_cups_strcasecmp(key, "usb_MFG") ||
3299 !_cups_strcasecmp(key, "usb_MANU") ||
3300 !_cups_strcasecmp(key, "usb_MANUFACTURER"))
5a9febac 3301 strlcpy(make_and_model, value, sizeof(make_and_model));
dcb445bc
MS
3302 else if (!_cups_strcasecmp(key, "usb_MDL") ||
3303 !_cups_strcasecmp(key, "usb_MODEL"))
5a9febac 3304 strlcpy(model, value, sizeof(model));
dcb445bc
MS
3305 else if (!_cups_strcasecmp(key, "product") && !strstr(value, "Ghostscript"))
3306 {
3307 if (value[0] == '(')
3308 {
3309 /*
3310 * Strip parenthesis...
3311 */
3312
3313 if ((ptr = value + strlen(value) - 1) > value && *ptr == ')')
3314 *ptr = '\0';
3315
5a9febac 3316 strlcpy(model, value + 1, sizeof(model));
dcb445bc
MS
3317 }
3318 else
5a9febac 3319 strlcpy(model, value, sizeof(model));
dcb445bc
MS
3320 }
3321 else if (!_cups_strcasecmp(key, "ty"))
3322 {
5a9febac 3323 strlcpy(model, value, sizeof(model));
dcb445bc
MS
3324
3325 if ((ptr = strchr(model, ',')) != NULL)
3326 *ptr = '\0';
3327 }
a29fd7dd
MS
3328 else if (!_cups_strcasecmp(key, "note"))
3329 device->dest.num_options = cupsAddOption("printer-location", value,
3330 device->dest.num_options,
3331 &device->dest.options);
3332 else if (!_cups_strcasecmp(key, "pdl"))
3333 {
3334 /*
3335 * Look for PDF-capable printers; only PDF-capable printers are shown.
3336 */
3337
3338 const char *start, *next; /* Pointer into value */
46385a1a
MS
3339 int have_pdf = 0, /* Have PDF? */
3340 have_raster = 0;/* Have raster format support? */
a29fd7dd
MS
3341
3342 for (start = value; start && *start; start = next)
3343 {
46385a1a 3344 if (!_cups_strncasecmp(start, "application/pdf", 15) && (!start[15] || start[15] == ','))
a29fd7dd
MS
3345 {
3346 have_pdf = 1;
3347 break;
3348 }
46385a1a
MS
3349 else if ((!_cups_strncasecmp(start, "image/pwg-raster", 16) && (!start[16] || start[16] == ',')) ||
3350 (!_cups_strncasecmp(start, "image/urf", 9) && (!start[9] || start[9] == ',')))
3351 {
3352 have_raster = 1;
3353 break;
3354 }
a29fd7dd
MS
3355
3356 if ((next = strchr(start, ',')) != NULL)
3357 next ++;
3358 }
3359
46385a1a 3360 if (!have_pdf && !have_raster)
a29fd7dd
MS
3361 device->state = _CUPS_DNSSD_INCOMPATIBLE;
3362 }
dcb445bc
MS
3363 else if (!_cups_strcasecmp(key, "printer-type"))
3364 {
a29fd7dd
MS
3365 /*
3366 * Value is either NNNN or 0xXXXX
3367 */
3368
3369 saw_printer_type = 1;
7e86f2f6 3370 type = (cups_ptype_t)strtol(value, NULL, 0);
a29fd7dd
MS
3371 }
3372 else if (!saw_printer_type)
3373 {
3374 if (!_cups_strcasecmp(key, "air") &&
3375 !_cups_strcasecmp(value, "t"))
3376 type |= CUPS_PRINTER_AUTHENTICATED;
3377 else if (!_cups_strcasecmp(key, "bind") &&
3378 !_cups_strcasecmp(value, "t"))
3379 type |= CUPS_PRINTER_BIND;
3380 else if (!_cups_strcasecmp(key, "collate") &&
3381 !_cups_strcasecmp(value, "t"))
3382 type |= CUPS_PRINTER_COLLATE;
3383 else if (!_cups_strcasecmp(key, "color") &&
3384 !_cups_strcasecmp(value, "t"))
3385 type |= CUPS_PRINTER_COLOR;
3386 else if (!_cups_strcasecmp(key, "copies") &&
3387 !_cups_strcasecmp(value, "t"))
3388 type |= CUPS_PRINTER_COPIES;
3389 else if (!_cups_strcasecmp(key, "duplex") &&
3390 !_cups_strcasecmp(value, "t"))
3391 type |= CUPS_PRINTER_DUPLEX;
3392 else if (!_cups_strcasecmp(key, "fax") &&
3393 !_cups_strcasecmp(value, "t"))
3394 type |= CUPS_PRINTER_MFP;
3395 else if (!_cups_strcasecmp(key, "papercustom") &&
3396 !_cups_strcasecmp(value, "t"))
3397 type |= CUPS_PRINTER_VARIABLE;
3398 else if (!_cups_strcasecmp(key, "papermax"))
3399 {
3400 if (!_cups_strcasecmp(value, "legal-a4"))
3401 type |= CUPS_PRINTER_SMALL;
3402 else if (!_cups_strcasecmp(value, "isoc-a2"))
3403 type |= CUPS_PRINTER_MEDIUM;
3404 else if (!_cups_strcasecmp(value, ">isoc-a2"))
3405 type |= CUPS_PRINTER_LARGE;
3406 }
3407 else if (!_cups_strcasecmp(key, "punch") &&
3408 !_cups_strcasecmp(value, "t"))
3409 type |= CUPS_PRINTER_PUNCH;
3410 else if (!_cups_strcasecmp(key, "scan") &&
3411 !_cups_strcasecmp(value, "t"))
3412 type |= CUPS_PRINTER_MFP;
3413 else if (!_cups_strcasecmp(key, "sort") &&
3414 !_cups_strcasecmp(value, "t"))
3415 type |= CUPS_PRINTER_SORT;
3416 else if (!_cups_strcasecmp(key, "staple") &&
3417 !_cups_strcasecmp(value, "t"))
3418 type |= CUPS_PRINTER_STAPLE;
dcb445bc
MS
3419 }
3420 }
3421
3422 /*
a29fd7dd 3423 * Save the printer-xxx values...
dcb445bc
MS
3424 */
3425
46385a1a 3426 device->dest.num_options = cupsAddOption("printer-info", name, device->dest.num_options, &device->dest.options);
a29fd7dd 3427
dcb445bc
MS
3428 if (make_and_model[0])
3429 {
3430 strlcat(make_and_model, " ", sizeof(make_and_model));
3431 strlcat(make_and_model, model, sizeof(make_and_model));
3432
46385a1a 3433 device->dest.num_options = cupsAddOption("printer-make-and-model", make_and_model, device->dest.num_options, &device->dest.options);
dcb445bc
MS
3434 }
3435 else
46385a1a 3436 device->dest.num_options = cupsAddOption("printer-make-and-model", model, device->dest.num_options, &device->dest.options);
dcb445bc 3437
a29fd7dd
MS
3438 device->type = type;
3439 snprintf(value, sizeof(value), "%u", type);
46385a1a 3440 device->dest.num_options = cupsAddOption("printer-type", value, device->dest.num_options, &device->dest.options);
a29fd7dd 3441
dcb445bc
MS
3442 /*
3443 * Save the URI...
3444 */
3445
3446 cups_dnssd_unquote(uriname, device->fullName, sizeof(uriname));
3447 httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri),
3448 !strcmp(device->regtype, "_ipps._tcp") ? "ipps" : "ipp",
a29fd7dd 3449 NULL, uriname, 0, saw_printer_type ? "/cups" : "/");
dcb445bc 3450
46385a1a 3451 DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri));
dcb445bc 3452
46385a1a 3453 device->dest.num_options = cupsAddOption("device-uri", uri, device->dest.num_options, &device->dest.options);
dcb445bc
MS
3454 }
3455 else
3456 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3457 fullName));
3458}
3459
3460
3461/*
3462 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3463 */
3464
3465static const char * /* O - Resolved URI or NULL */
3466cups_dnssd_resolve(
3467 cups_dest_t *dest, /* I - Destination */
3468 const char *uri, /* I - Current printer URI */
3469 int msec, /* I - Time in milliseconds */
3470 int *cancel, /* I - Pointer to "cancel" variable */
3471 cups_dest_cb_t cb, /* I - Callback */
3472 void *user_data) /* I - User data for callback */
3473{
3474 char tempuri[1024]; /* Temporary URI buffer */
3475 _cups_dnssd_resolve_t resolve; /* Resolve data */
3476
3477
3478 /*
3479 * Resolve the URI...
3480 */
3481
3482 resolve.cancel = cancel;
3483 gettimeofday(&resolve.end_time, NULL);
3484 if (msec > 0)
3485 {
3486 resolve.end_time.tv_sec += msec / 1000;
3487 resolve.end_time.tv_usec += (msec % 1000) * 1000;
3488
3489 while (resolve.end_time.tv_usec >= 1000000)
3490 {
3491 resolve.end_time.tv_sec ++;
3492 resolve.end_time.tv_usec -= 1000000;
3493 }
3494 }
3495 else
3496 resolve.end_time.tv_sec += 75;
3497
3498 if (cb)
4db7fcee 3499 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_RESOLVING, dest);
dcb445bc 3500
46385a1a 3501 if ((uri = _httpResolveURI(uri, tempuri, sizeof(tempuri), _HTTP_RESOLVE_FQDN, cups_dnssd_resolve_cb, &resolve)) == NULL)
dcb445bc 3502 {
f5f2e19e 3503 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to resolve printer-uri."), 1);
dcb445bc
MS
3504
3505 if (cb)
46385a1a 3506 (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, dest);
dcb445bc
MS
3507
3508 return (NULL);
3509 }
3510
3511 /*
3512 * Save the resolved URI...
3513 */
3514
46385a1a 3515 dest->num_options = cupsAddOption("resolved-device-uri", uri, dest->num_options, &dest->options);
dcb445bc 3516
46385a1a 3517 return (cupsGetOption("resolved-device-uri", dest->num_options, dest->options));
dcb445bc
MS
3518}
3519
3520
3521/*
3522 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3523 */
3524
3525static int /* O - 1 to continue, 0 to stop */
3526cups_dnssd_resolve_cb(void *context) /* I - Resolve data */
3527{
3528 _cups_dnssd_resolve_t *resolve = (_cups_dnssd_resolve_t *)context;
3529 /* Resolve data */
3530 struct timeval curtime; /* Current time */
3531
3532
3533 /*
3534 * If the cancel variable is set, return immediately.
3535 */
3536
4db7fcee
MS
3537 if (resolve->cancel && *(resolve->cancel))
3538 {
3539 DEBUG_puts("4cups_dnssd_resolve_cb: Canceled.");
dcb445bc 3540 return (0);
4db7fcee 3541 }
dcb445bc
MS
3542
3543 /*
3544 * Otherwise check the end time...
3545 */
3546
3547 gettimeofday(&curtime, NULL);
3548
4db7fcee
MS
3549 DEBUG_printf(("4cups_dnssd_resolve_cb: curtime=%d.%06d, end_time=%d.%06d", (int)curtime.tv_sec, curtime.tv_usec, (int)resolve->end_time.tv_sec, resolve->end_time.tv_usec));
3550
3551 return (curtime.tv_sec < resolve->end_time.tv_sec ||
dcb445bc 3552 (curtime.tv_sec == resolve->end_time.tv_sec &&
4db7fcee 3553 curtime.tv_usec < resolve->end_time.tv_usec));
dcb445bc
MS
3554}
3555
3556
3557/*
3558 * 'cups_dnssd_unquote()' - Unquote a name string.
3559 */
3560
3561static void
3562cups_dnssd_unquote(char *dst, /* I - Destination buffer */
3563 const char *src, /* I - Source string */
3564 size_t dstsize) /* I - Size of destination buffer */
3565{
3566 char *dstend = dst + dstsize - 1; /* End of destination buffer */
3567
3568
3569 while (*src && dst < dstend)
3570 {
3571 if (*src == '\\')
3572 {
3573 src ++;
3574 if (isdigit(src[0] & 255) && isdigit(src[1] & 255) &&
3575 isdigit(src[2] & 255))
3576 {
3577 *dst++ = ((((src[0] - '0') * 10) + src[1] - '0') * 10) + src[2] - '0';
3578 src += 3;
3579 }
3580 else
3581 *dst++ = *src++;
3582 }
3583 else
3584 *dst++ = *src ++;
3585 }
3586
3587 *dst = '\0';
3588}
3589#endif /* HAVE_DNSSD */
3590
3591
426c6a59
MS
3592/*
3593 * 'cups_find_dest()' - Find a destination using a binary search.
3594 */
3595
3596static int /* O - Index of match */
3597cups_find_dest(const char *name, /* I - Destination name */
3598 const char *instance, /* I - Instance or NULL */
3599 int num_dests, /* I - Number of destinations */
3600 cups_dest_t *dests, /* I - Destinations */
3601 int prev, /* I - Previous index */
3602 int *rdiff) /* O - Difference of match */
3603{
3604 int left, /* Low mark for binary search */
3605 right, /* High mark for binary search */
3606 current, /* Current index */
3607 diff; /* Result of comparison */
3608 cups_dest_t key; /* Search key */
3609
3610
3611 key.name = (char *)name;
3612 key.instance = (char *)instance;
3613
3614 if (prev >= 0)
3615 {
3616 /*
3617 * Start search on either side of previous...
3618 */
3619
3620 if ((diff = cups_compare_dests(&key, dests + prev)) == 0 ||
3621 (diff < 0 && prev == 0) ||
3622 (diff > 0 && prev == (num_dests - 1)))
3623 {
3624 *rdiff = diff;
3625 return (prev);
3626 }
3627 else if (diff < 0)
3628 {
3629 /*
3630 * Start with previous on right side...
3631 */
3632
3633 left = 0;
3634 right = prev;
3635 }
3636 else
3637 {
3638 /*
3639 * Start wih previous on left side...
3640 */
3641
3642 left = prev;
3643 right = num_dests - 1;
3644 }
3645 }
3646 else
3647 {
3648 /*
3649 * Start search in the middle...
3650 */
3651
3652 left = 0;
3653 right = num_dests - 1;
3654 }
3655
3656 do
3657 {
3658 current = (left + right) / 2;
3659 diff = cups_compare_dests(&key, dests + current);
3660
3661 if (diff == 0)
3662 break;
3663 else if (diff < 0)
3664 right = current;
3665 else
3666 left = current;
3667 }
3668 while ((right - left) > 1);
3669
3670 if (diff != 0)
3671 {
3672 /*
3673 * Check the last 1 or 2 elements...
3674 */
3675
3676 if ((diff = cups_compare_dests(&key, dests + left)) <= 0)
3677 current = left;
3678 else
3679 {
3680 diff = cups_compare_dests(&key, dests + right);
3681 current = right;
3682 }
3683 }
3684
3685 /*
3686 * Return the closest destination and the difference...
3687 */
3688
3689 *rdiff = diff;
3690
3691 return (current);
3692}
3693
3694
a4924f6c
MS
3695/*
3696 * 'cups_get_default()' - Get the default destination from an lpoptions file.
3697 */
3698
080811b1 3699static char * /* O - Default destination or NULL */
a4924f6c
MS
3700cups_get_default(const char *filename, /* I - File to read */
3701 char *namebuf, /* I - Name buffer */
3702 size_t namesize, /* I - Size of name buffer */
3703 const char **instance) /* I - Instance */
3704{
3705 cups_file_t *fp; /* lpoptions file */
3706 char line[8192], /* Line from file */
3707 *value, /* Value for line */
3708 *nameptr; /* Pointer into name */
88f9aafc 3709 int linenum; /* Current line */
a4924f6c
MS
3710
3711
3712 *namebuf = '\0';
3713
3714 if ((fp = cupsFileOpen(filename, "r")) != NULL)
3715 {
3716 linenum = 0;
3717
3718 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
3719 {
88f9aafc 3720 if (!_cups_strcasecmp(line, "default") && value)
a4924f6c
MS
3721 {
3722 strlcpy(namebuf, value, namesize);
3723
3724 if ((nameptr = strchr(namebuf, ' ')) != NULL)
3725 *nameptr = '\0';
3726 if ((nameptr = strchr(namebuf, '\t')) != NULL)
3727 *nameptr = '\0';
3728
3729 if ((nameptr = strchr(namebuf, '/')) != NULL)
3730 *nameptr++ = '\0';
3731
3732 *instance = nameptr;
3733 break;
3734 }
3735 }
3736
3737 cupsFileClose(fp);
3738 }
3739
3740 return (*namebuf ? namebuf : NULL);
3741}
3742
3743
ef416fc2 3744/*
3745 * 'cups_get_dests()' - Get destinations from a file.
3746 */
3747
3748static int /* O - Number of destinations */
38e73f87
MS
3749cups_get_dests(
3750 const char *filename, /* I - File to read from */
3751 const char *match_name, /* I - Destination name we want */
3752 const char *match_inst, /* I - Instance name we want */
3753 int user_default_set, /* I - User default printer set? */
3754 int num_dests, /* I - Number of destinations */
3755 cups_dest_t **dests) /* IO - Destinations */
ef416fc2 3756{
3757 int i; /* Looping var */
3758 cups_dest_t *dest; /* Current destination */
a4924f6c 3759 cups_file_t *fp; /* File pointer */
ef416fc2 3760 char line[8192], /* Line from file */
3761 *lineptr, /* Pointer into line */
3762 *name, /* Name of destination/option */
3763 *instance; /* Instance of destination */
a4924f6c 3764 int linenum; /* Current line number */
ef416fc2 3765
3766
e07d4801 3767 DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", "
38e73f87 3768 "match_inst=\"%s\", user_default_set=%d, num_dests=%d, "
e07d4801 3769 "dests=%p)", filename, match_name, match_inst,
38e73f87 3770 user_default_set, num_dests, dests));
a4924f6c
MS
3771
3772 /*
3773 * Try to open the file...
3774 */
3775
3776 if ((fp = cupsFileOpen(filename, "r")) == NULL)
3777 return (num_dests);
3778
ef416fc2 3779 /*
3780 * Read each printer; each line looks like:
3781 *
3782 * Dest name[/instance] options
3783 * Default name[/instance] options
3784 */
3785
a4924f6c
MS
3786 linenum = 0;
3787
3788 while (cupsFileGetConf(fp, line, sizeof(line), &lineptr, &linenum))
ef416fc2 3789 {
3790 /*
3791 * See what type of line it is...
3792 */
3793
e07d4801 3794 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
38e73f87 3795 linenum, line, lineptr));
ef416fc2 3796
88f9aafc 3797 if ((_cups_strcasecmp(line, "dest") && _cups_strcasecmp(line, "default")) || !lineptr)
a4924f6c 3798 {
e07d4801 3799 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
ef416fc2 3800 continue;
a4924f6c 3801 }
ef416fc2 3802
3803 name = lineptr;
3804
3805 /*
3806 * Search for an instance...
3807 */
3808
3809 while (!isspace(*lineptr & 255) && *lineptr && *lineptr != '/')
3810 lineptr ++;
3811
ef416fc2 3812 if (*lineptr == '/')
3813 {
3814 /*
3815 * Found an instance...
3816 */
3817
3818 *lineptr++ = '\0';
3819 instance = lineptr;
3820
3821 /*
3822 * Search for an instance...
3823 */
3824
3825 while (!isspace(*lineptr & 255) && *lineptr)
3826 lineptr ++;
3827 }
3828 else
3829 instance = NULL;
3830
a4924f6c
MS
3831 if (*lineptr)
3832 *lineptr++ = '\0';
3833
e07d4801 3834 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name,
a4924f6c 3835 instance));
ef416fc2 3836
3837 /*
3838 * See if the primary instance of the destination exists; if not,
3839 * ignore this entry and move on...
3840 */
3841
a4924f6c
MS
3842 if (match_name)
3843 {
88f9aafc 3844 if (_cups_strcasecmp(name, match_name) ||
a4924f6c
MS
3845 (!instance && match_inst) ||
3846 (instance && !match_inst) ||
88f9aafc 3847 (instance && _cups_strcasecmp(instance, match_inst)))
a4924f6c 3848 continue;
ef416fc2 3849
a4924f6c
MS
3850 dest = *dests;
3851 }
3852 else if (cupsGetDest(name, NULL, num_dests, *dests) == NULL)
3853 {
e07d4801 3854 DEBUG_puts("9cups_get_dests: Not found!");
a4924f6c
MS
3855 continue;
3856 }
3857 else
ef416fc2 3858 {
3859 /*
a4924f6c 3860 * Add the destination...
ef416fc2 3861 */
3862
a4924f6c
MS
3863 num_dests = cupsAddDest(name, instance, num_dests, dests);
3864
3865 if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL)
3866 {
3867 /*
3868 * Out of memory!
3869 */
3870
e07d4801 3871 DEBUG_puts("9cups_get_dests: Out of memory!");
a4924f6c
MS
3872 break;
3873 }
ef416fc2 3874 }
3875
3876 /*
3877 * Add options until we hit the end of the line...
3878 */
3879
3880 dest->num_options = cupsParseOptions(lineptr, dest->num_options,
3881 &(dest->options));
3882
a4924f6c
MS
3883 /*
3884 * If we found what we were looking for, stop now...
3885 */
3886
3887 if (match_name)
3888 break;
3889
ef416fc2 3890 /*
3891 * Set this as default if needed...
3892 */
3893
88f9aafc 3894 if (!user_default_set && !_cups_strcasecmp(line, "default"))
ef416fc2 3895 {
e07d4801 3896 DEBUG_puts("9cups_get_dests: Setting as default...");
a4924f6c 3897
ef416fc2 3898 for (i = 0; i < num_dests; i ++)
3899 (*dests)[i].is_default = 0;
3900
3901 dest->is_default = 1;
3902 }
3903 }
3904
3905 /*
3906 * Close the file and return...
3907 */
3908
88f9aafc 3909 cupsFileClose(fp);
ef416fc2 3910
3911 return (num_dests);
3912}
3913
3914
426c6a59
MS
3915/*
3916 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
3917 * attribute.
3918 */
3919
3920static char * /* O - New string */
3921cups_make_string(
3922 ipp_attribute_t *attr, /* I - Attribute to convert */
3923 char *buffer, /* I - Buffer */
3924 size_t bufsize) /* I - Size of buffer */
3925{
3926 int i; /* Looping var */
3927 char *ptr, /* Pointer into buffer */
3928 *end, /* Pointer to end of buffer */
3929 *valptr; /* Pointer into string attribute */
3930
3931
3932 /*
3933 * Return quickly if we have a single string value...
3934 */
3935
3936 if (attr->num_values == 1 &&
3937 attr->value_tag != IPP_TAG_INTEGER &&
3938 attr->value_tag != IPP_TAG_ENUM &&
3939 attr->value_tag != IPP_TAG_BOOLEAN &&
3940 attr->value_tag != IPP_TAG_RANGE)
3941 return (attr->values[0].string.text);
3942
3943 /*
3944 * Copy the values to the string, separating with commas and escaping strings
3945 * as needed...
3946 */
3947
3948 end = buffer + bufsize - 1;
3949
3950 for (i = 0, ptr = buffer; i < attr->num_values && ptr < end; i ++)
3951 {
3952 if (i)
3953 *ptr++ = ',';
3954
3955 switch (attr->value_tag)
3956 {
3957 case IPP_TAG_INTEGER :
3958 case IPP_TAG_ENUM :
07623986 3959 snprintf(ptr, (size_t)(end - ptr + 1), "%d", attr->values[i].integer);
426c6a59
MS
3960 break;
3961
3962 case IPP_TAG_BOOLEAN :
3963 if (attr->values[i].boolean)
07623986 3964 strlcpy(ptr, "true", (size_t)(end - ptr + 1));
426c6a59 3965 else
07623986 3966 strlcpy(ptr, "false", (size_t)(end - ptr + 1));
426c6a59
MS
3967 break;
3968
3969 case IPP_TAG_RANGE :
3970 if (attr->values[i].range.lower == attr->values[i].range.upper)
07623986 3971 snprintf(ptr, (size_t)(end - ptr + 1), "%d", attr->values[i].range.lower);
426c6a59 3972 else
07623986 3973 snprintf(ptr, (size_t)(end - ptr + 1), "%d-%d", attr->values[i].range.lower, attr->values[i].range.upper);
426c6a59
MS
3974 break;
3975
3976 default :
3977 for (valptr = attr->values[i].string.text;
3978 *valptr && ptr < end;)
3979 {
3980 if (strchr(" \t\n\\\'\"", *valptr))
3981 {
3982 if (ptr >= (end - 1))
3983 break;
3984
3985 *ptr++ = '\\';
3986 }
3987
3988 *ptr++ = *valptr++;
3989 }
3990
3991 *ptr = '\0';
3992 break;
3993 }
3994
3995 ptr += strlen(ptr);
3996 }
3997
3998 *ptr = '\0';
3999
4000 return (buffer);
4001}