2 * Utility to find IPP printers via Bonjour/DNS-SD and optionally run
3 * commands such as IPP and Bonjour conformance tests. This tool is
4 * inspired by the UNIX "find" command, thus its name.
6 * Copyright © 2008-2018 by Apple Inc.
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more
13 * Include necessary headers.
16 #define _CUPS_NO_DEPRECATED
17 #include <cups/cups-private.h>
20 # include <sys/timeb.h>
22 # include <sys/wait.h>
27 #elif defined(HAVE_AVAHI)
28 # include <avahi-client/client.h>
29 # include <avahi-client/lookup.h>
30 # include <avahi-common/simple-watch.h>
31 # include <avahi-common/domain.h>
32 # include <avahi-common/error.h>
33 # include <avahi-common/malloc.h>
34 # define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
35 #endif /* HAVE_DNSSD */
38 extern char **environ
; /* Process environment variables */
46 typedef enum ippfind_exit_e
/* Exit codes */
48 IPPFIND_EXIT_TRUE
= 0, /* OK and result is true */
49 IPPFIND_EXIT_FALSE
, /* OK but result is false*/
50 IPPFIND_EXIT_BONJOUR
, /* Browse/resolve failure */
51 IPPFIND_EXIT_SYNTAX
, /* Bad option or syntax error */
52 IPPFIND_EXIT_MEMORY
/* Out of memory */
55 typedef enum ippfind_op_e
/* Operations for expressions */
57 /* "Evaluation" operations */
58 IPPFIND_OP_NONE
, /* No operation */
59 IPPFIND_OP_AND
, /* Logical AND of all children */
60 IPPFIND_OP_OR
, /* Logical OR of all children */
61 IPPFIND_OP_TRUE
, /* Always true */
62 IPPFIND_OP_FALSE
, /* Always false */
63 IPPFIND_OP_IS_LOCAL
, /* Is a local service */
64 IPPFIND_OP_IS_REMOTE
, /* Is a remote service */
65 IPPFIND_OP_DOMAIN_REGEX
, /* Domain matches regular expression */
66 IPPFIND_OP_NAME_REGEX
, /* Name matches regular expression */
67 IPPFIND_OP_NAME_LITERAL
, /* Name matches literal string */
68 IPPFIND_OP_HOST_REGEX
, /* Hostname matches regular expression */
69 IPPFIND_OP_PORT_RANGE
, /* Port matches range */
70 IPPFIND_OP_PATH_REGEX
, /* Path matches regular expression */
71 IPPFIND_OP_TXT_EXISTS
, /* TXT record key exists */
72 IPPFIND_OP_TXT_REGEX
, /* TXT record key matches regular expression */
73 IPPFIND_OP_URI_REGEX
, /* URI matches regular expression */
75 /* "Output" operations */
76 IPPFIND_OP_EXEC
, /* Execute when true */
77 IPPFIND_OP_LIST
, /* List when true */
78 IPPFIND_OP_PRINT_NAME
, /* Print URI when true */
79 IPPFIND_OP_PRINT_URI
, /* Print name when true */
80 IPPFIND_OP_QUIET
/* No output when true */
83 typedef struct ippfind_expr_s
/* Expression */
86 *prev
, /* Previous expression */
87 *next
, /* Next expression */
88 *parent
, /* Parent expressions */
89 *child
; /* Child expressions */
90 ippfind_op_t op
; /* Operation code (see above) */
91 int invert
; /* Invert the result */
92 char *name
; /* TXT record key or literal name */
93 regex_t re
; /* Regular expression for matching */
94 int range
[2]; /* Port number range */
95 int num_args
; /* Number of arguments for exec */
96 char **args
; /* Arguments for exec */
99 typedef struct ippfind_srv_s
/* Service information */
102 DNSServiceRef ref
; /* Service reference for query */
103 #elif defined(HAVE_AVAHI)
104 AvahiServiceResolver
*ref
; /* Resolver */
105 #endif /* HAVE_DNSSD */
106 char *name
, /* Service name */
107 *domain
, /* Domain name */
108 *regtype
, /* Registration type */
109 *fullName
, /* Full name */
110 *host
, /* Hostname */
111 *resource
, /* Resource path */
113 int num_txt
; /* Number of TXT record keys */
114 cups_option_t
*txt
; /* TXT record keys */
115 int port
, /* Port number */
116 is_local
, /* Is a local service? */
117 is_processed
, /* Did we process the service? */
118 is_resolved
; /* Got the resolve data? */
127 static DNSServiceRef dnssd_ref
; /* Master service reference */
128 #elif defined(HAVE_AVAHI)
129 static AvahiClient
*avahi_client
= NULL
;/* Client information */
130 static int avahi_got_data
= 0; /* Got data from poll? */
131 static AvahiSimplePoll
*avahi_poll
= NULL
;
132 /* Poll information */
133 #endif /* HAVE_DNSSD */
135 static int address_family
= AF_UNSPEC
;
136 /* Address family for LIST */
137 static int bonjour_error
= 0; /* Error browsing/resolving? */
138 static double bonjour_timeout
= 1.0; /* Timeout in seconds */
139 static int ipp_version
= 20; /* IPP version for LIST */
147 static void DNSSD_API
browse_callback(DNSServiceRef sdRef
, DNSServiceFlags flags
, uint32_t interfaceIndex
, DNSServiceErrorType errorCode
, const char *serviceName
, const char *regtype
, const char *replyDomain
, void *context
) _CUPS_NONNULL(1,5,6,7,8);
148 static void DNSSD_API
browse_local_callback(DNSServiceRef sdRef
, DNSServiceFlags flags
, uint32_t interfaceIndex
, DNSServiceErrorType errorCode
, const char *serviceName
, const char *regtype
, const char *replyDomain
, void *context
) _CUPS_NONNULL(1,5,6,7,8);
149 #elif defined(HAVE_AVAHI)
150 static void browse_callback(AvahiServiceBrowser
*browser
,
151 AvahiIfIndex interface
,
152 AvahiProtocol protocol
,
153 AvahiBrowserEvent event
,
154 const char *serviceName
,
156 const char *replyDomain
,
157 AvahiLookupResultFlags flags
,
159 static void client_callback(AvahiClient
*client
,
160 AvahiClientState state
,
162 #endif /* HAVE_AVAHI */
164 static int compare_services(ippfind_srv_t
*a
, ippfind_srv_t
*b
);
165 static const char *dnssd_error_string(int error
);
166 static int eval_expr(ippfind_srv_t
*service
,
167 ippfind_expr_t
*expressions
);
168 static int exec_program(ippfind_srv_t
*service
, int num_args
,
170 static ippfind_srv_t
*get_service(cups_array_t
*services
, const char *serviceName
, const char *regtype
, const char *replyDomain
) _CUPS_NONNULL(1,2,3,4);
171 static double get_time(void);
172 static int list_service(ippfind_srv_t
*service
);
173 static ippfind_expr_t
*new_expr(ippfind_op_t op
, int invert
,
174 const char *value
, const char *regex
,
177 static void DNSSD_API
resolve_callback(DNSServiceRef sdRef
, DNSServiceFlags flags
, uint32_t interfaceIndex
, DNSServiceErrorType errorCode
, const char *fullName
, const char *hostTarget
, uint16_t port
, uint16_t txtLen
, const unsigned char *txtRecord
, void *context
) _CUPS_NONNULL(1,5,6,9, 10);
178 #elif defined(HAVE_AVAHI)
179 static int poll_callback(struct pollfd
*pollfds
,
180 unsigned int num_pollfds
, int timeout
,
182 static void resolve_callback(AvahiServiceResolver
*res
,
183 AvahiIfIndex interface
,
184 AvahiProtocol protocol
,
185 AvahiResolverEvent event
,
186 const char *serviceName
,
188 const char *replyDomain
,
189 const char *host_name
,
190 const AvahiAddress
*address
,
192 AvahiStringList
*txt
,
193 AvahiLookupResultFlags flags
,
195 #endif /* HAVE_DNSSD */
196 static void set_service_uri(ippfind_srv_t
*service
);
197 static void show_usage(void) _CUPS_NORETURN
;
198 static void show_version(void) _CUPS_NORETURN
;
202 * 'main()' - Browse for printers.
205 int /* O - Exit status */
206 main(int argc
, /* I - Number of command-line args */
207 char *argv
[]) /* I - Command-line arguments */
209 int i
, /* Looping var */
210 have_output
= 0,/* Have output expression */
211 status
= IPPFIND_EXIT_FALSE
;
213 const char *opt
, /* Option character */
214 *search
; /* Current browse/resolve string */
215 cups_array_t
*searches
; /* Things to browse/resolve */
216 cups_array_t
*services
; /* Service array */
217 ippfind_srv_t
*service
; /* Current service */
218 ippfind_expr_t
*expressions
= NULL
,
219 /* Expression tree */
220 *temp
= NULL
, /* New expression */
221 *parent
= NULL
, /* Parent expression */
222 *current
= NULL
,/* Current expression */
223 *parens
[100]; /* Markers for parenthesis */
224 int num_parens
= 0; /* Number of parenthesis */
225 ippfind_op_t logic
= IPPFIND_OP_AND
;
226 /* Logic for next expression */
227 int invert
= 0; /* Invert expression? */
228 int err
; /* DNS-SD error */
230 fd_set sinput
; /* Input set for select() */
231 struct timeval stimeout
; /* Timeout for select() */
232 #endif /* HAVE_DNSSD */
233 double endtime
; /* End time */
234 static const char * const ops
[] = /* Node operation names */
261 * Initialize the locale...
264 _cupsSetLocale(argv
);
267 * Create arrays to track services and things we want to browse/resolve...
270 searches
= cupsArrayNew(NULL
, NULL
);
271 services
= cupsArrayNew((cups_array_func_t
)compare_services
, NULL
);
274 * Parse command-line...
277 if (getenv("IPPFIND_DEBUG"))
278 for (i
= 1; i
< argc
; i
++)
279 fprintf(stderr
, "argv[%d]=\"%s\"\n", i
, argv
[i
]);
281 for (i
= 1; i
< argc
; i
++)
283 if (argv
[i
][0] == '-')
285 if (argv
[i
][1] == '-')
288 * Parse --option options...
291 if (!strcmp(argv
[i
], "--and"))
293 if (logic
== IPPFIND_OP_OR
)
295 _cupsLangPuts(stderr
, _("ippfind: Cannot use --and after --or."));
301 _cupsLangPuts(stderr
,
302 _("ippfind: Missing expression before \"--and\"."));
308 else if (!strcmp(argv
[i
], "--domain"))
313 _cupsLangPrintf(stderr
,
314 _("ippfind: Missing regular expression after %s."),
319 if ((temp
= new_expr(IPPFIND_OP_DOMAIN_REGEX
, invert
, NULL
, argv
[i
],
321 return (IPPFIND_EXIT_MEMORY
);
323 else if (!strcmp(argv
[i
], "--exec"))
328 _cupsLangPrintf(stderr
, _("ippfind: Expected program after %s."),
333 if ((temp
= new_expr(IPPFIND_OP_EXEC
, invert
, NULL
, NULL
,
335 return (IPPFIND_EXIT_MEMORY
);
338 if (!strcmp(argv
[i
], ";"))
345 _cupsLangPrintf(stderr
, _("ippfind: Expected semi-colon after %s."),
352 else if (!strcmp(argv
[i
], "--false"))
354 if ((temp
= new_expr(IPPFIND_OP_FALSE
, invert
, NULL
, NULL
,
356 return (IPPFIND_EXIT_MEMORY
);
358 else if (!strcmp(argv
[i
], "--help"))
362 else if (!strcmp(argv
[i
], "--host"))
367 _cupsLangPrintf(stderr
,
368 _("ippfind: Missing regular expression after %s."),
373 if ((temp
= new_expr(IPPFIND_OP_HOST_REGEX
, invert
, NULL
, argv
[i
],
375 return (IPPFIND_EXIT_MEMORY
);
377 else if (!strcmp(argv
[i
], "--ls"))
379 if ((temp
= new_expr(IPPFIND_OP_LIST
, invert
, NULL
, NULL
,
381 return (IPPFIND_EXIT_MEMORY
);
385 else if (!strcmp(argv
[i
], "--local"))
387 if ((temp
= new_expr(IPPFIND_OP_IS_LOCAL
, invert
, NULL
, NULL
,
389 return (IPPFIND_EXIT_MEMORY
);
391 else if (!strcmp(argv
[i
], "--literal-name"))
396 _cupsLangPrintf(stderr
, _("ippfind: Missing name after %s."), "--literal-name");
400 if ((temp
= new_expr(IPPFIND_OP_NAME_LITERAL
, invert
, argv
[i
], NULL
, NULL
)) == NULL
)
401 return (IPPFIND_EXIT_MEMORY
);
403 else if (!strcmp(argv
[i
], "--name"))
408 _cupsLangPrintf(stderr
,
409 _("ippfind: Missing regular expression after %s."),
414 if ((temp
= new_expr(IPPFIND_OP_NAME_REGEX
, invert
, NULL
, argv
[i
],
416 return (IPPFIND_EXIT_MEMORY
);
418 else if (!strcmp(argv
[i
], "--not"))
422 else if (!strcmp(argv
[i
], "--or"))
426 _cupsLangPuts(stderr
,
427 _("ippfind: Missing expression before \"--or\"."));
431 logic
= IPPFIND_OP_OR
;
433 if (parent
&& parent
->op
== IPPFIND_OP_OR
)
436 * Already setup to do "foo --or bar --or baz"...
441 else if (!current
->prev
&& parent
)
444 * Change parent node into an OR node...
447 parent
->op
= IPPFIND_OP_OR
;
450 else if (!current
->prev
)
453 * Need to group "current" in a new OR node...
456 if ((temp
= new_expr(IPPFIND_OP_OR
, 0, NULL
, NULL
,
458 return (IPPFIND_EXIT_MEMORY
);
460 temp
->parent
= parent
;
461 temp
->child
= current
;
462 current
->parent
= temp
;
465 parent
->child
= temp
;
475 * Need to group previous expressions in an AND node, and then
476 * put that in an OR node...
479 if ((temp
= new_expr(IPPFIND_OP_AND
, 0, NULL
, NULL
,
481 return (IPPFIND_EXIT_MEMORY
);
483 while (current
->prev
)
485 current
->parent
= temp
;
486 current
= current
->prev
;
489 current
->parent
= temp
;
490 temp
->child
= current
;
493 if ((temp
= new_expr(IPPFIND_OP_OR
, 0, NULL
, NULL
,
495 return (IPPFIND_EXIT_MEMORY
);
497 temp
->parent
= parent
;
498 current
->parent
= temp
;
501 parent
->child
= temp
;
509 else if (!strcmp(argv
[i
], "--path"))
514 _cupsLangPrintf(stderr
,
515 _("ippfind: Missing regular expression after %s."),
520 if ((temp
= new_expr(IPPFIND_OP_PATH_REGEX
, invert
, NULL
, argv
[i
],
522 return (IPPFIND_EXIT_MEMORY
);
524 else if (!strcmp(argv
[i
], "--port"))
529 _cupsLangPrintf(stderr
,
530 _("ippfind: Expected port range after %s."),
535 if ((temp
= new_expr(IPPFIND_OP_PORT_RANGE
, invert
, argv
[i
], NULL
,
537 return (IPPFIND_EXIT_MEMORY
);
539 else if (!strcmp(argv
[i
], "--print"))
541 if ((temp
= new_expr(IPPFIND_OP_PRINT_URI
, invert
, NULL
, NULL
,
543 return (IPPFIND_EXIT_MEMORY
);
547 else if (!strcmp(argv
[i
], "--print-name"))
549 if ((temp
= new_expr(IPPFIND_OP_PRINT_NAME
, invert
, NULL
, NULL
,
551 return (IPPFIND_EXIT_MEMORY
);
555 else if (!strcmp(argv
[i
], "--quiet"))
557 if ((temp
= new_expr(IPPFIND_OP_QUIET
, invert
, NULL
, NULL
,
559 return (IPPFIND_EXIT_MEMORY
);
563 else if (!strcmp(argv
[i
], "--remote"))
565 if ((temp
= new_expr(IPPFIND_OP_IS_REMOTE
, invert
, NULL
, NULL
,
567 return (IPPFIND_EXIT_MEMORY
);
569 else if (!strcmp(argv
[i
], "--true"))
571 if ((temp
= new_expr(IPPFIND_OP_TRUE
, invert
, NULL
, argv
[i
],
573 return (IPPFIND_EXIT_MEMORY
);
575 else if (!strcmp(argv
[i
], "--txt"))
580 _cupsLangPrintf(stderr
, _("ippfind: Expected key name after %s."),
585 if ((temp
= new_expr(IPPFIND_OP_TXT_EXISTS
, invert
, argv
[i
], NULL
,
587 return (IPPFIND_EXIT_MEMORY
);
589 else if (!strncmp(argv
[i
], "--txt-", 6))
591 const char *key
= argv
[i
] + 6;/* TXT key */
596 _cupsLangPrintf(stderr
,
597 _("ippfind: Missing regular expression after %s."),
602 if ((temp
= new_expr(IPPFIND_OP_TXT_REGEX
, invert
, key
, argv
[i
],
604 return (IPPFIND_EXIT_MEMORY
);
606 else if (!strcmp(argv
[i
], "--uri"))
611 _cupsLangPrintf(stderr
,
612 _("ippfind: Missing regular expression after %s."),
617 if ((temp
= new_expr(IPPFIND_OP_URI_REGEX
, invert
, NULL
, argv
[i
],
619 return (IPPFIND_EXIT_MEMORY
);
621 else if (!strcmp(argv
[i
], "--version"))
627 _cupsLangPrintf(stderr
, _("%s: Unknown option \"%s\"."),
635 * Add new expression...
638 if (logic
== IPPFIND_OP_AND
&&
639 current
&& current
->prev
&&
640 parent
&& parent
->op
!= IPPFIND_OP_AND
)
643 * Need to re-group "current" in a new AND node...
646 ippfind_expr_t
*tempand
; /* Temporary AND node */
648 if ((tempand
= new_expr(IPPFIND_OP_AND
, 0, NULL
, NULL
,
650 return (IPPFIND_EXIT_MEMORY
);
653 * Replace "current" with new AND node at the end of this list...
656 current
->prev
->next
= tempand
;
657 tempand
->prev
= current
->prev
;
658 tempand
->parent
= parent
;
661 * Add "current to the new AND node...
664 tempand
->child
= current
;
665 current
->parent
= tempand
;
666 current
->prev
= NULL
;
671 * Add the new node at current level...
674 temp
->parent
= parent
;
675 temp
->prev
= current
;
678 current
->next
= temp
;
680 parent
->child
= temp
;
686 logic
= IPPFIND_OP_AND
;
696 for (opt
= argv
[i
] + 1; *opt
; opt
++)
701 address_family
= AF_INET
;
705 address_family
= AF_INET6
;
708 case 'N' : /* Literal name */
712 _cupsLangPrintf(stderr
, _("ippfind: Missing name after %s."), "-N");
716 if ((temp
= new_expr(IPPFIND_OP_NAME_LITERAL
, invert
, argv
[i
], NULL
, NULL
)) == NULL
)
717 return (IPPFIND_EXIT_MEMORY
);
724 _cupsLangPrintf(stderr
,
725 _("ippfind: Expected port range after %s."),
730 if ((temp
= new_expr(IPPFIND_OP_PORT_RANGE
, invert
, argv
[i
],
731 NULL
, NULL
)) == NULL
)
732 return (IPPFIND_EXIT_MEMORY
);
739 _cupsLangPrintf(stderr
,
740 _("%s: Missing timeout for \"-T\"."),
745 bonjour_timeout
= atof(argv
[i
]);
752 _cupsLangPrintf(stderr
,
753 _("%s: Missing version for \"-V\"."),
758 if (!strcmp(argv
[i
], "1.1"))
760 else if (!strcmp(argv
[i
], "2.0"))
762 else if (!strcmp(argv
[i
], "2.1"))
764 else if (!strcmp(argv
[i
], "2.2"))
768 _cupsLangPrintf(stderr
, _("%s: Bad version %s for \"-V\"."),
778 _cupsLangPrintf(stderr
,
779 _("ippfind: Missing regular expression after "
784 if ((temp
= new_expr(IPPFIND_OP_DOMAIN_REGEX
, invert
, NULL
,
785 argv
[i
], NULL
)) == NULL
)
786 return (IPPFIND_EXIT_MEMORY
);
793 _cupsLangPrintf(stderr
,
794 _("ippfind: Missing regular expression after "
799 if ((temp
= new_expr(IPPFIND_OP_HOST_REGEX
, invert
, NULL
,
800 argv
[i
], NULL
)) == NULL
)
801 return (IPPFIND_EXIT_MEMORY
);
805 if ((temp
= new_expr(IPPFIND_OP_LIST
, invert
, NULL
, NULL
,
807 return (IPPFIND_EXIT_MEMORY
);
816 _cupsLangPrintf(stderr
,
817 _("ippfind: Missing regular expression after "
822 if ((temp
= new_expr(IPPFIND_OP_NAME_REGEX
, invert
, NULL
,
823 argv
[i
], NULL
)) == NULL
)
824 return (IPPFIND_EXIT_MEMORY
);
828 if ((temp
= new_expr(IPPFIND_OP_PRINT_URI
, invert
, NULL
, NULL
,
830 return (IPPFIND_EXIT_MEMORY
);
836 if ((temp
= new_expr(IPPFIND_OP_QUIET
, invert
, NULL
, NULL
,
838 return (IPPFIND_EXIT_MEMORY
);
844 if ((temp
= new_expr(IPPFIND_OP_IS_REMOTE
, invert
, NULL
, NULL
,
846 return (IPPFIND_EXIT_MEMORY
);
850 if ((temp
= new_expr(IPPFIND_OP_PRINT_NAME
, invert
, NULL
, NULL
,
852 return (IPPFIND_EXIT_MEMORY
);
861 _cupsLangPrintf(stderr
,
862 _("ippfind: Missing key name after %s."),
867 if ((temp
= new_expr(IPPFIND_OP_TXT_EXISTS
, invert
, argv
[i
],
868 NULL
, NULL
)) == NULL
)
869 return (IPPFIND_EXIT_MEMORY
);
876 _cupsLangPrintf(stderr
,
877 _("ippfind: Missing regular expression after "
882 if ((temp
= new_expr(IPPFIND_OP_URI_REGEX
, invert
, NULL
,
883 argv
[i
], NULL
)) == NULL
)
884 return (IPPFIND_EXIT_MEMORY
);
891 _cupsLangPrintf(stderr
,
892 _("ippfind: Missing program after %s."),
897 if ((temp
= new_expr(IPPFIND_OP_EXEC
, invert
, NULL
, NULL
,
899 return (IPPFIND_EXIT_MEMORY
);
902 if (!strcmp(argv
[i
], ";"))
909 _cupsLangPrintf(stderr
,
910 _("ippfind: Missing semi-colon after %s."),
919 _cupsLangPrintf(stderr
, _("%s: Unknown option \"-%c\"."),
927 * Add new expression...
930 if (logic
== IPPFIND_OP_AND
&&
931 current
&& current
->prev
&&
932 parent
&& parent
->op
!= IPPFIND_OP_AND
)
935 * Need to re-group "current" in a new AND node...
938 ippfind_expr_t
*tempand
; /* Temporary AND node */
940 if ((tempand
= new_expr(IPPFIND_OP_AND
, 0, NULL
, NULL
,
942 return (IPPFIND_EXIT_MEMORY
);
945 * Replace "current" with new AND node at the end of this list...
948 current
->prev
->next
= tempand
;
949 tempand
->prev
= current
->prev
;
950 tempand
->parent
= parent
;
953 * Add "current to the new AND node...
956 tempand
->child
= current
;
957 current
->parent
= tempand
;
958 current
->prev
= NULL
;
963 * Add the new node at current level...
966 temp
->parent
= parent
;
967 temp
->prev
= current
;
970 current
->next
= temp
;
972 parent
->child
= temp
;
978 logic
= IPPFIND_OP_AND
;
984 else if (!strcmp(argv
[i
], "("))
986 if (num_parens
>= 100)
988 _cupsLangPuts(stderr
, _("ippfind: Too many parenthesis."));
992 if ((temp
= new_expr(IPPFIND_OP_AND
, invert
, NULL
, NULL
, NULL
)) == NULL
)
993 return (IPPFIND_EXIT_MEMORY
);
995 parens
[num_parens
++] = temp
;
999 temp
->parent
= current
->parent
;
1000 current
->next
= temp
;
1001 temp
->prev
= current
;
1009 logic
= IPPFIND_OP_AND
;
1011 else if (!strcmp(argv
[i
], ")"))
1013 if (num_parens
<= 0)
1015 _cupsLangPuts(stderr
, _("ippfind: Missing open parenthesis."));
1019 current
= parens
[--num_parens
];
1020 parent
= current
->parent
;
1022 logic
= IPPFIND_OP_AND
;
1024 else if (!strcmp(argv
[i
], "!"))
1031 * _regtype._tcp[,subtype][.domain]
1035 * service-name[._regtype._tcp[.domain]]
1038 cupsArrayAdd(searches
, argv
[i
]);
1044 _cupsLangPuts(stderr
, _("ippfind: Missing close parenthesis."));
1051 * Add an implicit --print-uri to the end...
1054 if ((temp
= new_expr(IPPFIND_OP_PRINT_URI
, 0, NULL
, NULL
, NULL
)) == NULL
)
1055 return (IPPFIND_EXIT_MEMORY
);
1059 while (current
->parent
)
1060 current
= current
->parent
;
1062 current
->next
= temp
;
1063 temp
->prev
= current
;
1069 if (cupsArrayCount(searches
) == 0)
1072 * Add an implicit browse for IPP printers ("_ipp._tcp")...
1075 cupsArrayAdd(searches
, "_ipp._tcp");
1078 if (getenv("IPPFIND_DEBUG"))
1080 int indent
= 4; /* Indentation */
1082 puts("Expression tree:");
1083 current
= expressions
;
1087 * Print the current node...
1090 printf("%*s%s%s\n", indent
, "", current
->invert
? "!" : "",
1094 * Advance to the next node...
1099 current
= current
->child
;
1102 else if (current
->next
)
1103 current
= current
->next
;
1104 else if (current
->parent
)
1106 while (current
->parent
)
1109 current
= current
->parent
;
1114 current
= current
->next
;
1120 puts("\nSearch items:");
1121 for (search
= (const char *)cupsArrayFirst(searches
);
1123 search
= (const char *)cupsArrayNext(searches
))
1124 printf(" %s\n", search
);
1128 * Start up browsing/resolving...
1132 if ((err
= DNSServiceCreateConnection(&dnssd_ref
)) != kDNSServiceErr_NoError
)
1134 _cupsLangPrintf(stderr
, _("ippfind: Unable to use Bonjour: %s"),
1135 dnssd_error_string(err
));
1136 return (IPPFIND_EXIT_BONJOUR
);
1139 #elif defined(HAVE_AVAHI)
1140 if ((avahi_poll
= avahi_simple_poll_new()) == NULL
)
1142 _cupsLangPrintf(stderr
, _("ippfind: Unable to use Bonjour: %s"),
1144 return (IPPFIND_EXIT_BONJOUR
);
1147 avahi_simple_poll_set_func(avahi_poll
, poll_callback
, NULL
);
1149 avahi_client
= avahi_client_new(avahi_simple_poll_get(avahi_poll
),
1150 0, client_callback
, avahi_poll
, &err
);
1153 _cupsLangPrintf(stderr
, _("ippfind: Unable to use Bonjour: %s"),
1154 dnssd_error_string(err
));
1155 return (IPPFIND_EXIT_BONJOUR
);
1157 #endif /* HAVE_DNSSD */
1159 for (search
= (const char *)cupsArrayFirst(searches
);
1161 search
= (const char *)cupsArrayNext(searches
))
1163 char buf
[1024], /* Full name string */
1164 *name
= NULL
, /* Service instance name */
1165 *regtype
, /* Registration type */
1166 *domain
; /* Domain, if any */
1168 strlcpy(buf
, search
, sizeof(buf
));
1170 if (!strncmp(buf
, "_http._", 7) || !strncmp(buf
, "_https._", 8) || !strncmp(buf
, "_ipp._", 6) || !strncmp(buf
, "_ipps._", 7))
1174 else if ((regtype
= strstr(buf
, "._")) != NULL
)
1176 if (strcmp(regtype
, "._tcp"))
1179 * "something._protocol._tcp" -> search for something with the given
1189 * "_protocol._tcp" -> search for everything with the given protocol...
1199 * "something" -> search for something with IPP protocol...
1203 regtype
= "_ipp._tcp";
1206 for (domain
= regtype
; *domain
; domain
++)
1208 if (*domain
== '.' && domain
[1] != '_')
1221 * Resolve the given service instance name, regtype, and domain...
1227 service
= get_service(services
, name
, regtype
, domain
);
1229 if (getenv("IPPFIND_DEBUG"))
1230 fprintf(stderr
, "Resolving name=\"%s\", regtype=\"%s\", domain=\"%s\"\n", name
, regtype
, domain
);
1233 service
->ref
= dnssd_ref
;
1234 err
= DNSServiceResolve(&(service
->ref
),
1235 kDNSServiceFlagsShareConnection
, 0, name
,
1236 regtype
, domain
, resolve_callback
,
1239 #elif defined(HAVE_AVAHI)
1240 service
->ref
= avahi_service_resolver_new(avahi_client
, AVAHI_IF_UNSPEC
,
1241 AVAHI_PROTO_UNSPEC
, name
,
1243 AVAHI_PROTO_UNSPEC
, 0,
1244 resolve_callback
, service
);
1248 err
= avahi_client_errno(avahi_client
);
1249 #endif /* HAVE_DNSSD */
1254 * Browse for services of the given type...
1257 if (getenv("IPPFIND_DEBUG"))
1258 fprintf(stderr
, "Browsing for regtype=\"%s\", domain=\"%s\"\n", regtype
, domain
);
1261 DNSServiceRef ref
; /* Browse reference */
1264 err
= DNSServiceBrowse(&ref
, kDNSServiceFlagsShareConnection
, 0, regtype
,
1265 domain
, browse_callback
, services
);
1270 err
= DNSServiceBrowse(&ref
, kDNSServiceFlagsShareConnection
,
1271 kDNSServiceInterfaceIndexLocalOnly
, regtype
,
1272 domain
, browse_local_callback
, services
);
1275 #elif defined(HAVE_AVAHI)
1276 if (avahi_service_browser_new(avahi_client
, AVAHI_IF_UNSPEC
,
1277 AVAHI_PROTO_UNSPEC
, regtype
, domain
, 0,
1278 browse_callback
, services
))
1281 err
= avahi_client_errno(avahi_client
);
1282 #endif /* HAVE_DNSSD */
1287 _cupsLangPrintf(stderr
, _("ippfind: Unable to browse or resolve: %s"),
1288 dnssd_error_string(err
));
1290 return (IPPFIND_EXIT_BONJOUR
);
1295 * Process browse/resolve requests...
1298 if (bonjour_timeout
> 1.0)
1299 endtime
= get_time() + bonjour_timeout
;
1301 endtime
= get_time() + 300.0;
1303 while (get_time() < endtime
)
1305 int process
= 0; /* Process services? */
1308 int fd
= DNSServiceRefSockFD(dnssd_ref
);
1309 /* File descriptor for DNS-SD */
1312 FD_SET(fd
, &sinput
);
1314 stimeout
.tv_sec
= 0;
1315 stimeout
.tv_usec
= 500000;
1317 if (select(fd
+ 1, &sinput
, NULL
, NULL
, &stimeout
) < 0)
1320 if (FD_ISSET(fd
, &sinput
))
1323 * Process responses...
1326 DNSServiceProcessResult(dnssd_ref
);
1331 * Time to process services...
1337 #elif defined(HAVE_AVAHI)
1340 if (avahi_simple_poll_iterate(avahi_poll
, 500) > 0)
1343 * We've been told to exit the loop. Perhaps the connection to
1347 return (IPPFIND_EXIT_BONJOUR
);
1350 if (!avahi_got_data
)
1353 * Time to process services...
1358 #endif /* HAVE_DNSSD */
1363 * Process any services that we have found...
1366 int active
= 0, /* Number of active resolves */
1367 resolved
= 0, /* Number of resolved services */
1368 processed
= 0; /* Number of processed services */
1370 for (service
= (ippfind_srv_t
*)cupsArrayFirst(services
);
1372 service
= (ippfind_srv_t
*)cupsArrayNext(services
))
1374 if (service
->is_processed
)
1377 if (service
->is_resolved
)
1380 if (!service
->ref
&& !service
->is_resolved
)
1383 * Found a service, now resolve it (but limit to 50 active resolves...)
1389 service
->ref
= dnssd_ref
;
1390 err
= DNSServiceResolve(&(service
->ref
),
1391 kDNSServiceFlagsShareConnection
, 0,
1392 service
->name
, service
->regtype
,
1393 service
->domain
, resolve_callback
,
1396 #elif defined(HAVE_AVAHI)
1397 service
->ref
= avahi_service_resolver_new(avahi_client
,
1403 AVAHI_PROTO_UNSPEC
, 0,
1409 err
= avahi_client_errno(avahi_client
);
1410 #endif /* HAVE_DNSSD */
1414 _cupsLangPrintf(stderr
,
1415 _("ippfind: Unable to browse or resolve: %s"),
1416 dnssd_error_string(err
));
1417 return (IPPFIND_EXIT_BONJOUR
);
1423 else if (service
->is_resolved
&& !service
->is_processed
)
1426 * Resolved, not process this service against the expressions...
1432 DNSServiceRefDeallocate(service
->ref
);
1434 avahi_service_resolver_free(service
->ref
);
1435 #endif /* HAVE_DNSSD */
1437 service
->ref
= NULL
;
1440 if (eval_expr(service
, expressions
))
1441 status
= IPPFIND_EXIT_TRUE
;
1443 service
->is_processed
= 1;
1445 else if (service
->ref
)
1450 * If we have processed all services we have discovered, then we are done.
1453 if (processed
== cupsArrayCount(services
) && bonjour_timeout
<= 1.0)
1459 return (IPPFIND_EXIT_BONJOUR
);
1467 * 'browse_callback()' - Browse devices.
1470 static void DNSSD_API
1472 DNSServiceRef sdRef
, /* I - Service reference */
1473 DNSServiceFlags flags
, /* I - Option flags */
1474 uint32_t interfaceIndex
, /* I - Interface number */
1475 DNSServiceErrorType errorCode
, /* I - Error, if any */
1476 const char *serviceName
, /* I - Name of service/device */
1477 const char *regtype
, /* I - Type of service */
1478 const char *replyDomain
, /* I - Service domain */
1479 void *context
) /* I - Services array */
1482 * Only process "add" data...
1486 (void)interfaceIndex
;
1488 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
1495 get_service((cups_array_t
*)context
, serviceName
, regtype
, replyDomain
);
1500 * 'browse_local_callback()' - Browse local devices.
1503 static void DNSSD_API
1504 browse_local_callback(
1505 DNSServiceRef sdRef
, /* I - Service reference */
1506 DNSServiceFlags flags
, /* I - Option flags */
1507 uint32_t interfaceIndex
, /* I - Interface number */
1508 DNSServiceErrorType errorCode
, /* I - Error, if any */
1509 const char *serviceName
, /* I - Name of service/device */
1510 const char *regtype
, /* I - Type of service */
1511 const char *replyDomain
, /* I - Service domain */
1512 void *context
) /* I - Services array */
1514 ippfind_srv_t
*service
; /* Service */
1518 * Only process "add" data...
1522 (void)interfaceIndex
;
1524 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
1531 service
= get_service((cups_array_t
*)context
, serviceName
, regtype
,
1533 service
->is_local
= 1;
1535 #endif /* HAVE_DNSSD */
1540 * 'browse_callback()' - Browse devices.
1545 AvahiServiceBrowser
*browser
, /* I - Browser */
1546 AvahiIfIndex interface
, /* I - Interface index (unused) */
1547 AvahiProtocol protocol
, /* I - Network protocol (unused) */
1548 AvahiBrowserEvent event
, /* I - What happened */
1549 const char *name
, /* I - Service name */
1550 const char *type
, /* I - Registration type */
1551 const char *domain
, /* I - Domain */
1552 AvahiLookupResultFlags flags
, /* I - Flags */
1553 void *context
) /* I - Services array */
1555 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
1556 /* Client information */
1557 ippfind_srv_t
*service
; /* Service information */
1566 case AVAHI_BROWSER_FAILURE
:
1567 fprintf(stderr
, "DEBUG: browse_callback: %s\n",
1568 avahi_strerror(avahi_client_errno(client
)));
1570 avahi_simple_poll_quit(avahi_poll
);
1573 case AVAHI_BROWSER_NEW
:
1575 * This object is new on the network. Create a device entry for it if
1576 * it doesn't yet exist.
1579 service
= get_service((cups_array_t
*)context
, name
, type
, domain
);
1581 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
1582 service
->is_local
= 1;
1585 case AVAHI_BROWSER_REMOVE
:
1586 case AVAHI_BROWSER_ALL_FOR_NOW
:
1587 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
1594 * 'client_callback()' - Avahi client callback function.
1599 AvahiClient
*client
, /* I - Client information (unused) */
1600 AvahiClientState state
, /* I - Current state */
1601 void *context
) /* I - User data (unused) */
1607 * If the connection drops, quit.
1610 if (state
== AVAHI_CLIENT_FAILURE
)
1612 fputs("DEBUG: Avahi connection failed.\n", stderr
);
1614 avahi_simple_poll_quit(avahi_poll
);
1617 #endif /* HAVE_AVAHI */
1621 * 'compare_services()' - Compare two devices.
1624 static int /* O - Result of comparison */
1625 compare_services(ippfind_srv_t
*a
, /* I - First device */
1626 ippfind_srv_t
*b
) /* I - Second device */
1628 return (strcmp(a
->name
, b
->name
));
1633 * 'dnssd_error_string()' - Return an error string for an error code.
1636 static const char * /* O - Error message */
1637 dnssd_error_string(int error
) /* I - Error number */
1642 case kDNSServiceErr_NoError
:
1646 case kDNSServiceErr_Unknown
:
1647 return ("Unknown error.");
1649 case kDNSServiceErr_NoSuchName
:
1650 return ("Service not found.");
1652 case kDNSServiceErr_NoMemory
:
1653 return ("Out of memory.");
1655 case kDNSServiceErr_BadParam
:
1656 return ("Bad parameter.");
1658 case kDNSServiceErr_BadReference
:
1659 return ("Bad service reference.");
1661 case kDNSServiceErr_BadState
:
1662 return ("Bad state.");
1664 case kDNSServiceErr_BadFlags
:
1665 return ("Bad flags.");
1667 case kDNSServiceErr_Unsupported
:
1668 return ("Unsupported.");
1670 case kDNSServiceErr_NotInitialized
:
1671 return ("Not initialized.");
1673 case kDNSServiceErr_AlreadyRegistered
:
1674 return ("Already registered.");
1676 case kDNSServiceErr_NameConflict
:
1677 return ("Name conflict.");
1679 case kDNSServiceErr_Invalid
:
1680 return ("Invalid name.");
1682 case kDNSServiceErr_Firewall
:
1683 return ("Firewall prevents registration.");
1685 case kDNSServiceErr_Incompatible
:
1686 return ("Client library incompatible.");
1688 case kDNSServiceErr_BadInterfaceIndex
:
1689 return ("Bad interface index.");
1691 case kDNSServiceErr_Refused
:
1692 return ("Server prevents registration.");
1694 case kDNSServiceErr_NoSuchRecord
:
1695 return ("Record not found.");
1697 case kDNSServiceErr_NoAuth
:
1698 return ("Authentication required.");
1700 case kDNSServiceErr_NoSuchKey
:
1701 return ("Encryption key not found.");
1703 case kDNSServiceErr_NATTraversal
:
1704 return ("Unable to traverse NAT boundary.");
1706 case kDNSServiceErr_DoubleNAT
:
1707 return ("Unable to traverse double-NAT boundary.");
1709 case kDNSServiceErr_BadTime
:
1710 return ("Bad system time.");
1712 case kDNSServiceErr_BadSig
:
1713 return ("Bad signature.");
1715 case kDNSServiceErr_BadKey
:
1716 return ("Bad encryption key.");
1718 case kDNSServiceErr_Transient
:
1719 return ("Transient error occurred - please try again.");
1721 case kDNSServiceErr_ServiceNotRunning
:
1722 return ("Server not running.");
1724 case kDNSServiceErr_NATPortMappingUnsupported
:
1725 return ("NAT doesn't support NAT-PMP or UPnP.");
1727 case kDNSServiceErr_NATPortMappingDisabled
:
1728 return ("NAT supports NAT-PNP or UPnP but it is disabled.");
1730 case kDNSServiceErr_NoRouter
:
1731 return ("No Internet/default router configured.");
1733 case kDNSServiceErr_PollingMode
:
1734 return ("Service polling mode error.");
1737 case kDNSServiceErr_Timeout
:
1738 return ("Service timeout.");
1739 #endif /* !_WIN32 */
1742 # elif defined(HAVE_AVAHI)
1743 return (avahi_strerror(error
));
1744 # endif /* HAVE_DNSSD */
1749 * 'eval_expr()' - Evaluate the expressions against the specified service.
1751 * Returns 1 for true and 0 for false.
1754 static int /* O - Result of evaluation */
1755 eval_expr(ippfind_srv_t
*service
, /* I - Service */
1756 ippfind_expr_t
*expressions
) /* I - Expressions */
1758 ippfind_op_t logic
; /* Logical operation */
1759 int result
; /* Result of current expression */
1760 ippfind_expr_t
*expression
; /* Current expression */
1761 const char *val
; /* TXT value */
1764 * Loop through the expressions...
1767 if (expressions
&& expressions
->parent
)
1768 logic
= expressions
->parent
->op
;
1770 logic
= IPPFIND_OP_AND
;
1772 for (expression
= expressions
; expression
; expression
= expression
->next
)
1774 switch (expression
->op
)
1777 case IPPFIND_OP_AND
:
1778 case IPPFIND_OP_OR
:
1779 if (expression
->child
)
1780 result
= eval_expr(service
, expression
->child
);
1782 result
= expression
->op
== IPPFIND_OP_AND
;
1784 case IPPFIND_OP_TRUE
:
1787 case IPPFIND_OP_FALSE
:
1790 case IPPFIND_OP_IS_LOCAL
:
1791 result
= service
->is_local
;
1793 case IPPFIND_OP_IS_REMOTE
:
1794 result
= !service
->is_local
;
1796 case IPPFIND_OP_DOMAIN_REGEX
:
1797 result
= !regexec(&(expression
->re
), service
->domain
, 0, NULL
, 0);
1799 case IPPFIND_OP_NAME_REGEX
:
1800 result
= !regexec(&(expression
->re
), service
->name
, 0, NULL
, 0);
1802 case IPPFIND_OP_NAME_LITERAL
:
1803 result
= !_cups_strcasecmp(expression
->name
, service
->name
);
1805 case IPPFIND_OP_HOST_REGEX
:
1806 result
= !regexec(&(expression
->re
), service
->host
, 0, NULL
, 0);
1808 case IPPFIND_OP_PORT_RANGE
:
1809 result
= service
->port
>= expression
->range
[0] &&
1810 service
->port
<= expression
->range
[1];
1812 case IPPFIND_OP_PATH_REGEX
:
1813 result
= !regexec(&(expression
->re
), service
->resource
, 0, NULL
, 0);
1815 case IPPFIND_OP_TXT_EXISTS
:
1816 result
= cupsGetOption(expression
->name
, service
->num_txt
,
1817 service
->txt
) != NULL
;
1819 case IPPFIND_OP_TXT_REGEX
:
1820 val
= cupsGetOption(expression
->name
, service
->num_txt
,
1823 result
= !regexec(&(expression
->re
), val
, 0, NULL
, 0);
1827 if (getenv("IPPFIND_DEBUG"))
1828 printf("TXT_REGEX of \"%s\": %d\n", val
, result
);
1830 case IPPFIND_OP_URI_REGEX
:
1831 result
= !regexec(&(expression
->re
), service
->uri
, 0, NULL
, 0);
1833 case IPPFIND_OP_EXEC
:
1834 result
= exec_program(service
, expression
->num_args
,
1837 case IPPFIND_OP_LIST
:
1838 result
= list_service(service
);
1840 case IPPFIND_OP_PRINT_NAME
:
1841 _cupsLangPuts(stdout
, service
->name
);
1844 case IPPFIND_OP_PRINT_URI
:
1845 _cupsLangPuts(stdout
, service
->uri
);
1848 case IPPFIND_OP_QUIET
:
1853 if (expression
->invert
)
1856 if (logic
== IPPFIND_OP_AND
&& !result
)
1858 else if (logic
== IPPFIND_OP_OR
&& result
)
1862 return (logic
== IPPFIND_OP_AND
);
1867 * 'exec_program()' - Execute a program for a service.
1870 static int /* O - 1 if program terminated
1871 successfully, 0 otherwise. */
1872 exec_program(ippfind_srv_t
*service
, /* I - Service */
1873 int num_args
, /* I - Number of command-line args */
1874 char **args
) /* I - Command-line arguments */
1876 char **myargv
, /* Command-line arguments */
1877 **myenvp
, /* Environment variables */
1878 *ptr
, /* Pointer into variable */
1879 domain
[1024], /* IPPFIND_SERVICE_DOMAIN */
1880 hostname
[1024], /* IPPFIND_SERVICE_HOSTNAME */
1881 name
[256], /* IPPFIND_SERVICE_NAME */
1882 port
[32], /* IPPFIND_SERVICE_PORT */
1883 regtype
[256], /* IPPFIND_SERVICE_REGTYPE */
1884 scheme
[128], /* IPPFIND_SERVICE_SCHEME */
1885 uri
[1024], /* IPPFIND_SERVICE_URI */
1886 txt
[100][256]; /* IPPFIND_TXT_foo */
1887 int i
, /* Looping var */
1888 myenvc
, /* Number of environment variables */
1889 status
; /* Exit status of program */
1891 char program
[1024]; /* Program to execute */
1892 int pid
; /* Process ID */
1893 #endif /* !_WIN32 */
1897 * Environment variables...
1900 snprintf(domain
, sizeof(domain
), "IPPFIND_SERVICE_DOMAIN=%s",
1902 snprintf(hostname
, sizeof(hostname
), "IPPFIND_SERVICE_HOSTNAME=%s",
1904 snprintf(name
, sizeof(name
), "IPPFIND_SERVICE_NAME=%s", service
->name
);
1905 snprintf(port
, sizeof(port
), "IPPFIND_SERVICE_PORT=%d", service
->port
);
1906 snprintf(regtype
, sizeof(regtype
), "IPPFIND_SERVICE_REGTYPE=%s",
1908 snprintf(scheme
, sizeof(scheme
), "IPPFIND_SERVICE_SCHEME=%s",
1909 !strncmp(service
->regtype
, "_http._tcp", 10) ? "http" :
1910 !strncmp(service
->regtype
, "_https._tcp", 11) ? "https" :
1911 !strncmp(service
->regtype
, "_ipp._tcp", 9) ? "ipp" :
1912 !strncmp(service
->regtype
, "_ipps._tcp", 10) ? "ipps" : "lpd");
1913 snprintf(uri
, sizeof(uri
), "IPPFIND_SERVICE_URI=%s", service
->uri
);
1914 for (i
= 0; i
< service
->num_txt
&& i
< 100; i
++)
1916 snprintf(txt
[i
], sizeof(txt
[i
]), "IPPFIND_TXT_%s=%s", service
->txt
[i
].name
,
1917 service
->txt
[i
].value
);
1918 for (ptr
= txt
[i
] + 12; *ptr
&& *ptr
!= '='; ptr
++)
1919 *ptr
= (char)_cups_toupper(*ptr
);
1922 for (i
= 0, myenvc
= 7 + service
->num_txt
; environ
[i
]; i
++)
1923 if (strncmp(environ
[i
], "IPPFIND_", 8))
1926 if ((myenvp
= calloc(sizeof(char *), (size_t)(myenvc
+ 1))) == NULL
)
1928 _cupsLangPuts(stderr
, _("ippfind: Out of memory."));
1929 exit(IPPFIND_EXIT_MEMORY
);
1932 for (i
= 0, myenvc
= 0; environ
[i
]; i
++)
1933 if (strncmp(environ
[i
], "IPPFIND_", 8))
1934 myenvp
[myenvc
++] = environ
[i
];
1936 myenvp
[myenvc
++] = domain
;
1937 myenvp
[myenvc
++] = hostname
;
1938 myenvp
[myenvc
++] = name
;
1939 myenvp
[myenvc
++] = port
;
1940 myenvp
[myenvc
++] = regtype
;
1941 myenvp
[myenvc
++] = scheme
;
1942 myenvp
[myenvc
++] = uri
;
1944 for (i
= 0; i
< service
->num_txt
&& i
< 100; i
++)
1945 myenvp
[myenvc
++] = txt
[i
];
1948 * Allocate and copy command-line arguments...
1951 if ((myargv
= calloc(sizeof(char *), (size_t)(num_args
+ 1))) == NULL
)
1953 _cupsLangPuts(stderr
, _("ippfind: Out of memory."));
1954 exit(IPPFIND_EXIT_MEMORY
);
1957 for (i
= 0; i
< num_args
; i
++)
1959 if (strchr(args
[i
], '{'))
1961 char temp
[2048], /* Temporary string */
1962 *tptr
, /* Pointer into temporary string */
1963 keyword
[256], /* {keyword} */
1964 *kptr
; /* Pointer into keyword */
1966 for (ptr
= args
[i
], tptr
= temp
; *ptr
; ptr
++)
1971 * Do a {var} substitution...
1974 for (kptr
= keyword
, ptr
++; *ptr
&& *ptr
!= '}'; ptr
++)
1975 if (kptr
< (keyword
+ sizeof(keyword
) - 1))
1980 _cupsLangPuts(stderr
,
1981 _("ippfind: Missing close brace in substitution."));
1982 exit(IPPFIND_EXIT_SYNTAX
);
1986 if (!keyword
[0] || !strcmp(keyword
, "service_uri"))
1987 strlcpy(tptr
, service
->uri
, sizeof(temp
) - (size_t)(tptr
- temp
));
1988 else if (!strcmp(keyword
, "service_domain"))
1989 strlcpy(tptr
, service
->domain
, sizeof(temp
) - (size_t)(tptr
- temp
));
1990 else if (!strcmp(keyword
, "service_hostname"))
1991 strlcpy(tptr
, service
->host
, sizeof(temp
) - (size_t)(tptr
- temp
));
1992 else if (!strcmp(keyword
, "service_name"))
1993 strlcpy(tptr
, service
->name
, sizeof(temp
) - (size_t)(tptr
- temp
));
1994 else if (!strcmp(keyword
, "service_path"))
1995 strlcpy(tptr
, service
->resource
, sizeof(temp
) - (size_t)(tptr
- temp
));
1996 else if (!strcmp(keyword
, "service_port"))
1997 strlcpy(tptr
, port
+ 21, sizeof(temp
) - (size_t)(tptr
- temp
));
1998 else if (!strcmp(keyword
, "service_scheme"))
1999 strlcpy(tptr
, scheme
+ 22, sizeof(temp
) - (size_t)(tptr
- temp
));
2000 else if (!strncmp(keyword
, "txt_", 4))
2002 const char *val
= cupsGetOption(keyword
+ 4, service
->num_txt
, service
->txt
);
2004 strlcpy(tptr
, val
, sizeof(temp
) - (size_t)(tptr
- temp
));
2010 _cupsLangPrintf(stderr
, _("ippfind: Unknown variable \"{%s}\"."),
2012 exit(IPPFIND_EXIT_SYNTAX
);
2015 tptr
+= strlen(tptr
);
2017 else if (tptr
< (temp
+ sizeof(temp
) - 1))
2022 myargv
[i
] = strdup(temp
);
2025 myargv
[i
] = strdup(args
[i
]);
2029 if (getenv("IPPFIND_DEBUG"))
2031 printf("\nProgram:\n %s\n", args
[0]);
2032 puts("\nArguments:");
2033 for (i
= 0; i
< num_args
; i
++)
2034 printf(" %s\n", myargv
[i
]);
2035 puts("\nEnvironment:");
2036 for (i
= 0; i
< myenvc
; i
++)
2037 printf(" %s\n", myenvp
[i
]);
2040 status
= _spawnvpe(_P_WAIT
, args
[0], myargv
, myenvp
);
2044 * Execute the program...
2047 if (strchr(args
[0], '/') && !access(args
[0], X_OK
))
2048 strlcpy(program
, args
[0], sizeof(program
));
2049 else if (!cupsFileFind(args
[0], getenv("PATH"), 1, program
, sizeof(program
)))
2051 _cupsLangPrintf(stderr
, _("ippfind: Unable to execute \"%s\": %s"),
2052 args
[0], strerror(ENOENT
));
2053 exit(IPPFIND_EXIT_SYNTAX
);
2056 if (getenv("IPPFIND_DEBUG"))
2058 printf("\nProgram:\n %s\n", program
);
2059 puts("\nArguments:");
2060 for (i
= 0; i
< num_args
; i
++)
2061 printf(" %s\n", myargv
[i
]);
2062 puts("\nEnvironment:");
2063 for (i
= 0; i
< myenvc
; i
++)
2064 printf(" %s\n", myenvp
[i
]);
2067 if ((pid
= fork()) == 0)
2070 * Child comes here...
2073 execve(program
, myargv
, myenvp
);
2078 _cupsLangPrintf(stderr
, _("ippfind: Unable to execute \"%s\": %s"),
2079 args
[0], strerror(errno
));
2080 exit(IPPFIND_EXIT_SYNTAX
);
2085 * Wait for it to complete...
2088 while (wait(&status
) != pid
)
2097 for (i
= 0; i
< num_args
; i
++)
2104 * Return whether the program succeeded or crashed...
2107 if (getenv("IPPFIND_DEBUG"))
2110 printf("Exit Status: %d\n", status
);
2112 if (WIFEXITED(status
))
2113 printf("Exit Status: %d\n", WEXITSTATUS(status
));
2115 printf("Terminating Signal: %d\n", WTERMSIG(status
));
2119 return (status
== 0);
2124 * 'get_service()' - Create or update a device.
2127 static ippfind_srv_t
* /* O - Service */
2128 get_service(cups_array_t
*services
, /* I - Service array */
2129 const char *serviceName
, /* I - Name of service/device */
2130 const char *regtype
, /* I - Type of service */
2131 const char *replyDomain
) /* I - Service domain */
2133 ippfind_srv_t key
, /* Search key */
2134 *service
; /* Service */
2135 char fullName
[kDNSServiceMaxDomainName
];
2136 /* Full name for query */
2140 * See if this is a new device...
2143 key
.name
= (char *)serviceName
;
2144 key
.regtype
= (char *)regtype
;
2146 for (service
= cupsArrayFind(services
, &key
);
2148 service
= cupsArrayNext(services
))
2149 if (_cups_strcasecmp(service
->name
, key
.name
))
2151 else if (!strcmp(service
->regtype
, key
.regtype
))
2155 * Yes, add the service...
2158 service
= calloc(sizeof(ippfind_srv_t
), 1);
2159 service
->name
= strdup(serviceName
);
2160 service
->domain
= strdup(replyDomain
);
2161 service
->regtype
= strdup(regtype
);
2163 cupsArrayAdd(services
, service
);
2166 * Set the "full name" of this service, which is used for queries and
2171 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
2172 #else /* HAVE_AVAHI */
2173 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
,
2174 regtype
, replyDomain
);
2175 #endif /* HAVE_DNSSD */
2177 service
->fullName
= strdup(fullName
);
2184 * 'get_time()' - Get the current time-of-day in seconds.
2191 struct _timeb curtime
; /* Current Windows time */
2195 return (curtime
.time
+ 0.001 * curtime
.millitm
);
2198 struct timeval curtime
; /* Current UNIX time */
2200 if (gettimeofday(&curtime
, NULL
))
2203 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
2209 * 'list_service()' - List the contents of a service.
2212 static int /* O - 1 if successful, 0 otherwise */
2213 list_service(ippfind_srv_t
*service
) /* I - Service */
2215 http_addrlist_t
*addrlist
; /* Address(es) of service */
2216 char port
[10]; /* Port number of service */
2219 snprintf(port
, sizeof(port
), "%d", service
->port
);
2221 if ((addrlist
= httpAddrGetList(service
->host
, address_family
, port
)) == NULL
)
2223 _cupsLangPrintf(stdout
, "%s unreachable", service
->uri
);
2227 if (!strncmp(service
->regtype
, "_ipp._tcp", 9) ||
2228 !strncmp(service
->regtype
, "_ipps._tcp", 10))
2234 http_t
*http
; /* HTTP connection */
2235 ipp_t
*request
, /* IPP request */
2236 *response
; /* IPP response */
2237 ipp_attribute_t
*attr
; /* IPP attribute */
2238 int i
, /* Looping var */
2239 count
, /* Number of values */
2240 version
, /* IPP version */
2241 paccepting
; /* printer-is-accepting-jobs value */
2242 ipp_pstate_t pstate
; /* printer-state value */
2243 char preasons
[1024], /* Comma-delimited printer-state-reasons */
2244 *ptr
, /* Pointer into reasons */
2245 *end
; /* End of reasons buffer */
2246 static const char * const rattrs
[] =/* Requested attributes */
2248 "printer-is-accepting-jobs",
2250 "printer-state-reasons"
2254 * Connect to the printer...
2257 http
= httpConnect2(service
->host
, service
->port
, addrlist
, address_family
,
2258 !strncmp(service
->regtype
, "_ipps._tcp", 10) ?
2259 HTTP_ENCRYPTION_ALWAYS
:
2260 HTTP_ENCRYPTION_IF_REQUESTED
,
2263 httpAddrFreeList(addrlist
);
2267 _cupsLangPrintf(stdout
, "%s unavailable", service
->uri
);
2272 * Get the current printer state...
2276 version
= ipp_version
;
2280 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
2281 ippSetVersion(request
, version
/ 10, version
% 10);
2282 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
2284 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2285 "requesting-user-name", NULL
, cupsUser());
2286 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2287 "requested-attributes",
2288 (int)(sizeof(rattrs
) / sizeof(rattrs
[0])), NULL
, rattrs
);
2290 response
= cupsDoRequest(http
, request
, service
->resource
);
2292 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
&& version
> 11)
2295 while (cupsLastError() > IPP_STATUS_OK_EVENTS_COMPLETE
&& version
> 11);
2301 if (cupsLastError() > IPP_STATUS_OK_EVENTS_COMPLETE
)
2303 _cupsLangPrintf(stdout
, "%s: unavailable", service
->uri
);
2307 if ((attr
= ippFindAttribute(response
, "printer-state",
2308 IPP_TAG_ENUM
)) != NULL
)
2309 pstate
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
2311 pstate
= IPP_PSTATE_STOPPED
;
2313 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
2314 IPP_TAG_BOOLEAN
)) != NULL
)
2315 paccepting
= ippGetBoolean(attr
, 0);
2319 if ((attr
= ippFindAttribute(response
, "printer-state-reasons",
2320 IPP_TAG_KEYWORD
)) != NULL
)
2322 strlcpy(preasons
, ippGetString(attr
, 0, NULL
), sizeof(preasons
));
2324 for (i
= 1, count
= ippGetCount(attr
), ptr
= preasons
+ strlen(preasons
),
2325 end
= preasons
+ sizeof(preasons
) - 1;
2326 i
< count
&& ptr
< end
;
2327 i
++, ptr
+= strlen(ptr
))
2330 strlcpy(ptr
, ippGetString(attr
, i
, NULL
), (size_t)(end
- ptr
+ 1));
2334 strlcpy(preasons
, "none", sizeof(preasons
));
2336 ippDelete(response
);
2339 _cupsLangPrintf(stdout
, "%s %s %s %s", service
->uri
, ippEnumString("printer-state", (int)pstate
), paccepting
? "accepting-jobs" : "not-accepting-jobs", preasons
);
2341 else if (!strncmp(service
->regtype
, "_http._tcp", 10) ||
2342 !strncmp(service
->regtype
, "_https._tcp", 11))
2345 * HTTP/HTTPS web page
2348 http_t
*http
; /* HTTP connection */
2349 http_status_t status
; /* HEAD status */
2353 * Connect to the web server...
2356 http
= httpConnect2(service
->host
, service
->port
, addrlist
, address_family
,
2357 !strncmp(service
->regtype
, "_ipps._tcp", 10) ?
2358 HTTP_ENCRYPTION_ALWAYS
:
2359 HTTP_ENCRYPTION_IF_REQUESTED
,
2362 httpAddrFreeList(addrlist
);
2366 _cupsLangPrintf(stdout
, "%s unavailable", service
->uri
);
2370 if (httpGet(http
, service
->resource
))
2372 _cupsLangPrintf(stdout
, "%s unavailable", service
->uri
);
2378 status
= httpUpdate(http
);
2380 while (status
== HTTP_STATUS_CONTINUE
);
2385 if (status
>= HTTP_STATUS_BAD_REQUEST
)
2387 _cupsLangPrintf(stdout
, "%s unavailable", service
->uri
);
2391 _cupsLangPrintf(stdout
, "%s available", service
->uri
);
2393 else if (!strncmp(service
->regtype
, "_printer._tcp", 13))
2399 int sock
; /* Socket */
2402 if (!httpAddrConnect(addrlist
, &sock
))
2404 _cupsLangPrintf(stdout
, "%s unavailable", service
->uri
);
2405 httpAddrFreeList(addrlist
);
2409 _cupsLangPrintf(stdout
, "%s available", service
->uri
);
2410 httpAddrFreeList(addrlist
);
2412 httpAddrClose(NULL
, sock
);
2416 _cupsLangPrintf(stdout
, "%s unsupported", service
->uri
);
2417 httpAddrFreeList(addrlist
);
2426 * 'new_expr()' - Create a new expression.
2429 static ippfind_expr_t
* /* O - New expression */
2430 new_expr(ippfind_op_t op
, /* I - Operation */
2431 int invert
, /* I - Invert result? */
2432 const char *value
, /* I - TXT key or port range */
2433 const char *regex
, /* I - Regular expression */
2434 char **args
) /* I - Pointer to argument strings */
2436 ippfind_expr_t
*temp
; /* New expression */
2439 if ((temp
= calloc(1, sizeof(ippfind_expr_t
))) == NULL
)
2443 temp
->invert
= invert
;
2445 if (op
== IPPFIND_OP_TXT_EXISTS
|| op
== IPPFIND_OP_TXT_REGEX
|| op
== IPPFIND_OP_NAME_LITERAL
)
2446 temp
->name
= (char *)value
;
2447 else if (op
== IPPFIND_OP_PORT_RANGE
)
2450 * Pull port number range of the form "number", "-number" (0-number),
2451 * "number-" (number-65535), and "number-number".
2456 temp
->range
[1] = atoi(value
+ 1);
2458 else if (strchr(value
, '-'))
2460 if (sscanf(value
, "%d-%d", temp
->range
, temp
->range
+ 1) == 1)
2461 temp
->range
[1] = 65535;
2465 temp
->range
[0] = temp
->range
[1] = atoi(value
);
2471 int err
= regcomp(&(temp
->re
), regex
, REG_NOSUB
| REG_ICASE
| REG_EXTENDED
);
2475 char message
[256]; /* Error message */
2477 regerror(err
, &(temp
->re
), message
, sizeof(message
));
2478 _cupsLangPrintf(stderr
, _("ippfind: Bad regular expression: %s"),
2480 exit(IPPFIND_EXIT_SYNTAX
);
2486 int num_args
; /* Number of arguments */
2488 for (num_args
= 1; args
[num_args
]; num_args
++)
2489 if (!strcmp(args
[num_args
], ";"))
2492 temp
->num_args
= num_args
;
2493 temp
->args
= malloc((size_t)num_args
* sizeof(char *));
2494 memcpy(temp
->args
, args
, (size_t)num_args
* sizeof(char *));
2503 * 'poll_callback()' - Wait for input on the specified file descriptors.
2505 * Note: This function is needed because avahi_simple_poll_iterate is broken
2506 * and always uses a timeout of 0 (!) milliseconds.
2507 * (Avahi Ticket #364)
2510 static int /* O - Number of file descriptors matching */
2512 struct pollfd
*pollfds
, /* I - File descriptors */
2513 unsigned int num_pollfds
, /* I - Number of file descriptors */
2514 int timeout
, /* I - Timeout in milliseconds (unused) */
2515 void *context
) /* I - User data (unused) */
2517 int val
; /* Return value */
2523 val
= poll(pollfds
, num_pollfds
, 500);
2530 #endif /* HAVE_AVAHI */
2534 * 'resolve_callback()' - Process resolve data.
2538 static void DNSSD_API
2540 DNSServiceRef sdRef
, /* I - Service reference */
2541 DNSServiceFlags flags
, /* I - Data flags */
2542 uint32_t interfaceIndex
, /* I - Interface */
2543 DNSServiceErrorType errorCode
, /* I - Error, if any */
2544 const char *fullName
, /* I - Full service name */
2545 const char *hostTarget
, /* I - Hostname */
2546 uint16_t port
, /* I - Port number (network byte order) */
2547 uint16_t txtLen
, /* I - Length of TXT record data */
2548 const unsigned char *txtRecord
, /* I - TXT record data */
2549 void *context
) /* I - Service */
2551 char key
[256], /* TXT key value */
2552 *value
; /* Value from TXT record */
2553 const unsigned char *txtEnd
; /* End of TXT record */
2554 uint8_t valueLen
; /* Length of value */
2555 ippfind_srv_t
*service
= (ippfind_srv_t
*)context
;
2560 * Only process "add" data...
2565 (void)interfaceIndex
;
2568 if (errorCode
!= kDNSServiceErr_NoError
)
2570 _cupsLangPrintf(stderr
, _("ippfind: Unable to browse or resolve: %s"),
2571 dnssd_error_string(errorCode
));
2576 service
->is_resolved
= 1;
2577 service
->host
= strdup(hostTarget
);
2578 service
->port
= ntohs(port
);
2580 value
= service
->host
+ strlen(service
->host
) - 1;
2581 if (value
>= service
->host
&& *value
== '.')
2585 * Loop through the TXT key/value pairs and add them to an array...
2588 for (txtEnd
= txtRecord
+ txtLen
; txtRecord
< txtEnd
; txtRecord
+= valueLen
)
2591 * Ignore bogus strings...
2594 valueLen
= *txtRecord
++;
2596 memcpy(key
, txtRecord
, valueLen
);
2597 key
[valueLen
] = '\0';
2599 if ((value
= strchr(key
, '=')) == NULL
)
2605 * Add to array of TXT values...
2608 service
->num_txt
= cupsAddOption(key
, value
, service
->num_txt
,
2612 set_service_uri(service
);
2616 #elif defined(HAVE_AVAHI)
2619 AvahiServiceResolver
*resolver
, /* I - Resolver */
2620 AvahiIfIndex interface
, /* I - Interface */
2621 AvahiProtocol protocol
, /* I - Address protocol */
2622 AvahiResolverEvent event
, /* I - Event */
2623 const char *serviceName
,/* I - Service name */
2624 const char *regtype
, /* I - Registration type */
2625 const char *replyDomain
,/* I - Domain name */
2626 const char *hostTarget
, /* I - FQDN */
2627 const AvahiAddress
*address
, /* I - Address */
2628 uint16_t port
, /* I - Port number */
2629 AvahiStringList
*txt
, /* I - TXT records */
2630 AvahiLookupResultFlags flags
, /* I - Lookup flags */
2631 void *context
) /* I - Service */
2633 char key
[256], /* TXT key */
2634 *value
; /* TXT value */
2635 ippfind_srv_t
*service
= (ippfind_srv_t
*)context
;
2637 AvahiStringList
*current
; /* Current TXT key/value pair */
2642 if (event
!= AVAHI_RESOLVER_FOUND
)
2646 avahi_service_resolver_free(resolver
);
2647 avahi_simple_poll_quit(avahi_poll
);
2651 service
->is_resolved
= 1;
2652 service
->host
= strdup(hostTarget
);
2653 service
->port
= port
;
2655 value
= service
->host
+ strlen(service
->host
) - 1;
2656 if (value
>= service
->host
&& *value
== '.')
2660 * Loop through the TXT key/value pairs and add them to an array...
2663 for (current
= txt
; current
; current
= current
->next
)
2666 * Ignore bogus strings...
2669 if (current
->size
> (sizeof(key
) - 1))
2672 memcpy(key
, current
->text
, current
->size
);
2673 key
[current
->size
] = '\0';
2675 if ((value
= strchr(key
, '=')) == NULL
)
2681 * Add to array of TXT values...
2684 service
->num_txt
= cupsAddOption(key
, value
, service
->num_txt
,
2688 set_service_uri(service
);
2690 #endif /* HAVE_DNSSD */
2694 * 'set_service_uri()' - Set the URI of the service.
2698 set_service_uri(ippfind_srv_t
*service
) /* I - Service */
2700 char uri
[1024]; /* URI */
2701 const char *path
, /* Resource path */
2702 *scheme
; /* URI scheme */
2705 if (!strncmp(service
->regtype
, "_http.", 6))
2708 path
= cupsGetOption("path", service
->num_txt
, service
->txt
);
2710 else if (!strncmp(service
->regtype
, "_https.", 7))
2713 path
= cupsGetOption("path", service
->num_txt
, service
->txt
);
2715 else if (!strncmp(service
->regtype
, "_ipp.", 5))
2718 path
= cupsGetOption("rp", service
->num_txt
, service
->txt
);
2720 else if (!strncmp(service
->regtype
, "_ipps.", 6))
2723 path
= cupsGetOption("rp", service
->num_txt
, service
->txt
);
2725 else if (!strncmp(service
->regtype
, "_printer.", 9))
2728 path
= cupsGetOption("rp", service
->num_txt
, service
->txt
);
2733 if (!path
|| !*path
)
2738 service
->resource
= strdup(path
);
2742 snprintf(uri
, sizeof(uri
), "/%s", path
);
2743 service
->resource
= strdup(uri
);
2746 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
,
2747 service
->host
, service
->port
, service
->resource
);
2748 service
->uri
= strdup(uri
);
2753 * 'show_usage()' - Show program usage.
2759 _cupsLangPuts(stderr
, _("Usage: ippfind [options] regtype[,subtype]"
2760 "[.domain.] ... [expression]\n"
2761 " ippfind [options] name[.regtype[.domain.]] "
2762 "... [expression]\n"
2764 " ippfind --version"));
2765 _cupsLangPuts(stderr
, _("Options:"));
2766 _cupsLangPuts(stderr
, _("-4 Connect using IPv4"));
2767 _cupsLangPuts(stderr
, _("-6 Connect using IPv6"));
2768 _cupsLangPuts(stderr
, _("-T seconds Set the browse timeout in seconds"));
2769 _cupsLangPuts(stderr
, _("-V version Set default IPP version"));
2770 _cupsLangPuts(stderr
, _("--version Show program version"));
2771 _cupsLangPuts(stderr
, _("Expressions:"));
2772 _cupsLangPuts(stderr
, _("-P number[-number] Match port to number or range"));
2773 _cupsLangPuts(stderr
, _("-d regex Match domain to regular expression"));
2774 _cupsLangPuts(stderr
, _("-h regex Match hostname to regular expression"));
2775 _cupsLangPuts(stderr
, _("-l List attributes"));
2776 _cupsLangPuts(stderr
, _("-n regex Match service name to regular expression"));
2777 _cupsLangPuts(stderr
, _("-p Print URI if true"));
2778 _cupsLangPuts(stderr
, _("-q Quietly report match via exit code"));
2779 _cupsLangPuts(stderr
, _("-r True if service is remote"));
2780 _cupsLangPuts(stderr
, _("-s Print service name if true"));
2781 _cupsLangPuts(stderr
, _("-t key True if the TXT record contains the key"));
2782 _cupsLangPuts(stderr
, _("-u regex Match URI to regular expression"));
2783 _cupsLangPuts(stderr
, _("-x utility [argument ...] ;\n"
2784 " Execute program if true"));
2785 _cupsLangPuts(stderr
, _("--domain regex Match domain to regular expression"));
2786 _cupsLangPuts(stderr
, _("--exec utility [argument ...] ;\n"
2787 " Execute program if true"));
2788 _cupsLangPuts(stderr
, _("--host regex Match hostname to regular expression"));
2789 _cupsLangPuts(stderr
, _("--ls List attributes"));
2790 _cupsLangPuts(stderr
, _("--local True if service is local"));
2791 _cupsLangPuts(stderr
, _("--name regex Match service name to regular expression"));
2792 _cupsLangPuts(stderr
, _("--path regex Match resource path to regular expression"));
2793 _cupsLangPuts(stderr
, _("--port number[-number] Match port to number or range"));
2794 _cupsLangPuts(stderr
, _("--print Print URI if true"));
2795 _cupsLangPuts(stderr
, _("--print-name Print service name if true"));
2796 _cupsLangPuts(stderr
, _("--quiet Quietly report match via exit code"));
2797 _cupsLangPuts(stderr
, _("--remote True if service is remote"));
2798 _cupsLangPuts(stderr
, _("--txt key True if the TXT record contains the key"));
2799 _cupsLangPuts(stderr
, _("--txt-* regex Match TXT record key to regular expression"));
2800 _cupsLangPuts(stderr
, _("--uri regex Match URI to regular expression"));
2801 _cupsLangPuts(stderr
, _("Modifiers:"));
2802 _cupsLangPuts(stderr
, _("( expressions ) Group expressions"));
2803 _cupsLangPuts(stderr
, _("! expression Unary NOT of expression"));
2804 _cupsLangPuts(stderr
, _("--not expression Unary NOT of expression"));
2805 _cupsLangPuts(stderr
, _("--false Always false"));
2806 _cupsLangPuts(stderr
, _("--true Always true"));
2807 _cupsLangPuts(stderr
, _("expression expression Logical AND"));
2808 _cupsLangPuts(stderr
, _("expression --and expression\n"
2810 _cupsLangPuts(stderr
, _("expression --or expression\n"
2812 _cupsLangPuts(stderr
, _("Substitutions:"));
2813 _cupsLangPuts(stderr
, _("{} URI"));
2814 _cupsLangPuts(stderr
, _("{service_domain} Domain name"));
2815 _cupsLangPuts(stderr
, _("{service_hostname} Fully-qualified domain name"));
2816 _cupsLangPuts(stderr
, _("{service_name} Service instance name"));
2817 _cupsLangPuts(stderr
, _("{service_port} Port number"));
2818 _cupsLangPuts(stderr
, _("{service_regtype} DNS-SD registration type"));
2819 _cupsLangPuts(stderr
, _("{service_scheme} URI scheme"));
2820 _cupsLangPuts(stderr
, _("{service_uri} URI"));
2821 _cupsLangPuts(stderr
, _("{txt_*} Value of TXT record key"));
2822 _cupsLangPuts(stderr
, _("Environment Variables:"));
2823 _cupsLangPuts(stderr
, _("IPPFIND_SERVICE_DOMAIN Domain name"));
2824 _cupsLangPuts(stderr
, _("IPPFIND_SERVICE_HOSTNAME\n"
2825 " Fully-qualified domain name"));
2826 _cupsLangPuts(stderr
, _("IPPFIND_SERVICE_NAME Service instance name"));
2827 _cupsLangPuts(stderr
, _("IPPFIND_SERVICE_PORT Port number"));
2828 _cupsLangPuts(stderr
, _("IPPFIND_SERVICE_REGTYPE DNS-SD registration type"));
2829 _cupsLangPuts(stderr
, _("IPPFIND_SERVICE_SCHEME URI scheme"));
2830 _cupsLangPuts(stderr
, _("IPPFIND_SERVICE_URI URI"));
2831 _cupsLangPuts(stderr
, _("IPPFIND_TXT_* Value of TXT record key"));
2833 exit(IPPFIND_EXIT_TRUE
);
2838 * 'show_version()' - Show program version.
2844 _cupsLangPuts(stderr
, CUPS_SVERSION
);
2846 exit(IPPFIND_EXIT_TRUE
);