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