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