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