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