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