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