]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-browse.c
get rid of a lot of old svn cruft
[catta] / avahi-utils / avahi-browse.c
1 /***
2   This file is part of avahi.
3
4   avahi is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   avahi is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with avahi; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <getopt.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <net/if.h>
32 #include <locale.h>
33
34 #include <avahi-common/simple-watch.h>
35 #include <avahi-common/error.h>
36 #include <avahi-common/malloc.h>
37 #include <avahi-common/domain.h>
38 #include <avahi-common/llist.h>
39 #include <avahi-common/i18n.h>
40 #include <avahi-client/client.h>
41 #include <avahi-client/lookup.h>
42
43 #include "sigint.h"
44
45 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
46 #include "stdb.h"
47 #endif
48
49 typedef enum {
50     COMMAND_HELP,
51     COMMAND_VERSION,
52     COMMAND_BROWSE_SERVICES,
53     COMMAND_BROWSE_ALL_SERVICES,
54     COMMAND_BROWSE_DOMAINS
55 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
56     , COMMAND_DUMP_STDB
57 #endif
58 } Command;
59
60 typedef struct Config {
61     int verbose;
62     int terminate_on_all_for_now;
63     int terminate_on_cache_exhausted;
64     char *domain;
65     char *stype;
66     int ignore_local;
67     Command command;
68     int resolve;
69     int no_fail;
70     int parsable;
71 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
72     int no_db_lookup;
73 #endif
74 } Config;
75
76 typedef struct ServiceInfo ServiceInfo;
77
78 struct ServiceInfo {
79     AvahiIfIndex interface;
80     AvahiProtocol protocol;
81     char *name, *type, *domain;
82
83     AvahiServiceResolver *resolver;
84     Config *config;
85
86     AVAHI_LLIST_FIELDS(ServiceInfo, info);
87 };
88
89 static AvahiSimplePoll *simple_poll = NULL;
90 static AvahiClient *client = NULL;
91 static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
92 static AvahiStringList *browsed_types = NULL;
93 static ServiceInfo *services = NULL;
94 static int n_columns = 80;
95 static int browsing = 0;
96
97 static void check_terminate(Config *c) {
98
99     assert(n_all_for_now >= 0);
100     assert(n_cache_exhausted >= 0);
101     assert(n_resolving >= 0);
102
103     if (n_all_for_now <= 0 && n_resolving <= 0) {
104
105         if (c->verbose && !c->parsable) {
106             printf(_(": All for now\n"));
107             n_all_for_now++; /* Make sure that this event is not repeated */
108         }
109
110         if (c->terminate_on_all_for_now)
111             avahi_simple_poll_quit(simple_poll);
112     }
113
114     if (n_cache_exhausted <= 0 && n_resolving <= 0) {
115
116         if (c->verbose && !c->parsable) {
117             printf(_(": Cache exhausted\n"));
118             n_cache_exhausted++; /* Make sure that this event is not repeated */
119         }
120
121         if (c->terminate_on_cache_exhausted)
122             avahi_simple_poll_quit(simple_poll);
123     }
124 }
125
126 static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
127     ServiceInfo *i;
128
129     for (i = services; i; i = i->info_next)
130         if (i->interface == interface &&
131             i->protocol == protocol &&
132             strcasecmp(i->name, name) == 0 &&
133             avahi_domain_equal(i->type, type) &&
134             avahi_domain_equal(i->domain, domain))
135
136             return i;
137
138     return NULL;
139 }
140
141 static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, int nl) {
142     char ifname[IF_NAMESIZE];
143
144 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
145     if (!config->no_db_lookup)
146         type = stdb_lookup(type);
147 #endif
148
149     if (config->parsable) {
150         char sn[AVAHI_DOMAIN_NAME_MAX], *e = sn;
151         size_t l = sizeof(sn);
152
153         printf("%c;%s;%s;%s;%s;%s%s",
154                c,
155                interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : _("n/a"),
156                protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : _("n/a"),
157                avahi_escape_label(name, strlen(name), &e, &l), type, domain, nl ? "\n" : "");
158
159     } else
160         printf("%c %4s %4s %-*s %-20s %s\n",
161                c,
162                interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : _("n/a"),
163                protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : _("n/a"),
164                n_columns-35, name, type, domain);
165     fflush(stdout);
166 }
167
168 static void service_resolver_callback(
169     AvahiServiceResolver *r,
170     AvahiIfIndex interface,
171     AvahiProtocol protocol,
172     AvahiResolverEvent event,
173     const char *name,
174     const char *type,
175     const char *domain,
176     const char *host_name,
177     const AvahiAddress *a,
178     uint16_t port,
179     AvahiStringList *txt,
180     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
181     void *userdata) {
182
183     ServiceInfo *i = userdata;
184
185     assert(r);
186     assert(i);
187
188     switch (event) {
189         case AVAHI_RESOLVER_FOUND: {
190             char address[AVAHI_ADDRESS_STR_MAX], *t;
191
192             avahi_address_snprint(address, sizeof(address), a);
193
194             t = avahi_string_list_to_string(txt);
195
196             print_service_line(i->config, '=', interface, protocol, name, type, domain, 0);
197
198             if (i->config->parsable)
199                 printf(";%s;%s;%u;%s\n",
200                        host_name,
201                        address,
202                        port,
203                        t);
204             else
205                 printf("   hostname = [%s]\n"
206                        "   address = [%s]\n"
207                        "   port = [%u]\n"
208                        "   txt = [%s]\n",
209                        host_name,
210                        address,
211                        port,
212                        t);
213
214             avahi_free(t);
215
216             break;
217         }
218
219         case AVAHI_RESOLVER_FAILURE:
220
221             fprintf(stderr, _("Failed to resolve service '%s' of type '%s' in domain '%s': %s\n"), name, type, domain, avahi_strerror(avahi_client_errno(client)));
222             break;
223     }
224
225
226     avahi_service_resolver_free(i->resolver);
227     i->resolver = NULL;
228
229     assert(n_resolving > 0);
230     n_resolving--;
231     check_terminate(i->config);
232     fflush(stdout);
233 }
234
235 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
236     ServiceInfo *i;
237
238     i = avahi_new(ServiceInfo, 1);
239
240     if (c->resolve) {
241         if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
242             avahi_free(i);
243             fprintf(stderr, _("Failed to resolve service '%s' of type '%s' in domain '%s': %s\n"), name, type, domain, avahi_strerror(avahi_client_errno(client)));
244             return NULL;
245         }
246
247         n_resolving++;
248     } else
249         i->resolver = NULL;
250
251     i->interface = interface;
252     i->protocol = protocol;
253     i->name = avahi_strdup(name);
254     i->type = avahi_strdup(type);
255     i->domain = avahi_strdup(domain);
256     i->config = c;
257
258     AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);
259
260     return i;
261 }
262
263 static void remove_service(Config *c, ServiceInfo *i) {
264     assert(c);
265     assert(i);
266
267     AVAHI_LLIST_REMOVE(ServiceInfo, info, services, i);
268
269     if (i->resolver)
270         avahi_service_resolver_free(i->resolver);
271
272     avahi_free(i->name);
273     avahi_free(i->type);
274     avahi_free(i->domain);
275     avahi_free(i);
276 }
277
278 static void service_browser_callback(
279     AvahiServiceBrowser *b,
280     AvahiIfIndex interface,
281     AvahiProtocol protocol,
282     AvahiBrowserEvent event,
283     const char *name,
284     const char *type,
285     const char *domain,
286     AvahiLookupResultFlags flags,
287     void *userdata) {
288
289     Config *c = userdata;
290
291     assert(b);
292     assert(c);
293
294     switch (event) {
295         case AVAHI_BROWSER_NEW: {
296             if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
297                 break;
298
299             if (find_service(interface, protocol, name, type, domain))
300                 return;
301
302             add_service(c, interface, protocol, name, type, domain);
303
304             print_service_line(c, '+', interface, protocol, name, type, domain, 1);
305             break;
306
307         }
308
309         case AVAHI_BROWSER_REMOVE: {
310             ServiceInfo *info;
311
312             if (!(info = find_service(interface, protocol, name, type, domain)))
313                 return;
314
315             remove_service(c, info);
316
317             print_service_line(c, '-', interface, protocol, name, type, domain, 1);
318             break;
319         }
320
321         case AVAHI_BROWSER_FAILURE:
322             fprintf(stderr, _("service_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
323             avahi_simple_poll_quit(simple_poll);
324             break;
325
326         case AVAHI_BROWSER_CACHE_EXHAUSTED:
327             n_cache_exhausted --;
328             check_terminate(c);
329             break;
330
331         case AVAHI_BROWSER_ALL_FOR_NOW:
332             n_all_for_now --;
333             check_terminate(c);
334             break;
335     }
336 }
337
338 static void browse_service_type(Config *c, const char *stype, const char *domain) {
339     AvahiServiceBrowser *b;
340     AvahiStringList *i;
341
342     assert(c);
343     assert(client);
344     assert(stype);
345
346     for (i = browsed_types; i; i = i->next)
347         if (avahi_domain_equal(stype, (char*) i->text))
348             return;
349
350     if (!(b = avahi_service_browser_new(
351               client,
352               AVAHI_IF_UNSPEC,
353               AVAHI_PROTO_UNSPEC,
354               stype,
355               domain,
356               0,
357               service_browser_callback,
358               c))) {
359
360         fprintf(stderr, _("avahi_service_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
361         avahi_simple_poll_quit(simple_poll);
362     }
363
364     browsed_types = avahi_string_list_add(browsed_types, stype);
365
366     n_all_for_now++;
367     n_cache_exhausted++;
368 }
369
370 static void service_type_browser_callback(
371     AvahiServiceTypeBrowser *b,
372     AVAHI_GCC_UNUSED AvahiIfIndex interface,
373     AVAHI_GCC_UNUSED AvahiProtocol protocol,
374     AvahiBrowserEvent event,
375     const char *type,
376     const char *domain,
377     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
378     void *userdata) {
379
380     Config *c = userdata;
381
382     assert(b);
383     assert(c);
384
385     switch (event) {
386
387         case AVAHI_BROWSER_NEW:
388             browse_service_type(c, type, domain);
389             break;
390
391         case AVAHI_BROWSER_REMOVE:
392             /* We're dirty and never remove the browser again */
393             break;
394
395         case AVAHI_BROWSER_FAILURE:
396             fprintf(stderr, _("service_type_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
397             avahi_simple_poll_quit(simple_poll);
398             break;
399
400         case AVAHI_BROWSER_CACHE_EXHAUSTED:
401             n_cache_exhausted --;
402             check_terminate(c);
403             break;
404
405         case AVAHI_BROWSER_ALL_FOR_NOW:
406             n_all_for_now --;
407             check_terminate(c);
408             break;
409     }
410 }
411
412 static void browse_all(Config *c) {
413     AvahiServiceTypeBrowser *b;
414
415     assert(c);
416
417     if (!(b = avahi_service_type_browser_new(
418               client,
419               AVAHI_IF_UNSPEC,
420               AVAHI_PROTO_UNSPEC,
421               c->domain,
422               0,
423               service_type_browser_callback,
424               c))) {
425
426         fprintf(stderr, _("avahi_service_type_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
427         avahi_simple_poll_quit(simple_poll);
428     }
429
430     n_cache_exhausted++;
431     n_all_for_now++;
432 }
433
434 static void domain_browser_callback(
435     AvahiDomainBrowser *b,
436     AVAHI_GCC_UNUSED AvahiIfIndex interface,
437     AVAHI_GCC_UNUSED AvahiProtocol protocol,
438     AvahiBrowserEvent event,
439     const char *domain,
440     AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
441     void *userdata) {
442
443     Config *c = userdata;
444
445     assert(b);
446     assert(c);
447
448     switch (event) {
449
450         case AVAHI_BROWSER_NEW:
451         case AVAHI_BROWSER_REMOVE: {
452             char ifname[IF_NAMESIZE];
453
454             if (c->parsable)
455                 printf("%c;%s;%s;%s\n",
456                        event == AVAHI_BROWSER_NEW ? '+' : '-',
457                        interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "",
458                        protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "",
459                        domain);
460             else
461                 printf("%c %4s %4s %s\n",
462                        event == AVAHI_BROWSER_NEW ? '+' : '-',
463                        interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
464                        protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
465                        domain);
466             break;
467         }
468
469         case AVAHI_BROWSER_FAILURE:
470             fprintf(stderr, ("domain_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
471             avahi_simple_poll_quit(simple_poll);
472             break;
473
474         case AVAHI_BROWSER_CACHE_EXHAUSTED:
475             n_cache_exhausted --;
476             check_terminate(c);
477             break;
478
479         case AVAHI_BROWSER_ALL_FOR_NOW:
480             n_all_for_now --;
481             check_terminate(c);
482             break;
483     }
484 }
485
486 static void browse_domains(Config *c) {
487     AvahiDomainBrowser *b;
488
489     assert(c);
490
491     if (!(b = avahi_domain_browser_new(
492               client,
493               AVAHI_IF_UNSPEC,
494               AVAHI_PROTO_UNSPEC,
495               c->domain,
496               AVAHI_DOMAIN_BROWSER_BROWSE,
497               0,
498               domain_browser_callback,
499               c))) {
500
501         fprintf(stderr, _("avahi_domain_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
502         avahi_simple_poll_quit(simple_poll);
503     }
504
505     n_cache_exhausted++;
506     n_all_for_now++;
507 }
508
509 static int start(Config *config) {
510
511     assert(!browsing);
512
513     if (config->verbose && !config->parsable) {
514         const char *version, *hn;
515
516         if (!(version = avahi_client_get_version_string(client))) {
517             fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client)));
518             return -1;
519         }
520
521         if (!(hn = avahi_client_get_host_name_fqdn(client))) {
522             fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client)));
523             return -1;
524         }
525
526         fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn);
527
528         if (config->command == COMMAND_BROWSE_DOMAINS) {
529             /* Translators: This is a column heading with abbreviations for
530              *   Event (+/-), Network Interface, Protocol (IPv4/v6), Domain */
531             fprintf(stderr, _("E Ifce Prot Domain\n"));
532         } else {
533             /* Translators: This is a column heading with abbreviations for
534              *   Event (+/-), Network Interface, Protocol (IPv4/v6), Domain */
535             fprintf(stderr, _("E Ifce Prot %-*s %-20s Domain\n"), n_columns-35, _("Name"), _("Type"));
536         }
537     }
538
539     if (config->command == COMMAND_BROWSE_SERVICES)
540         browse_service_type(config, config->stype, config->domain);
541     else if (config->command == COMMAND_BROWSE_ALL_SERVICES)
542         browse_all(config);
543     else {
544         assert(config->command == COMMAND_BROWSE_DOMAINS);
545         browse_domains(config);
546     }
547
548     browsing = 1;
549     return 0;
550 }
551
552 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
553     Config *config = userdata;
554
555     /* This function might be called when avahi_client_new() has not
556      * returned yet.*/
557     client = c;
558
559     switch (state) {
560         case AVAHI_CLIENT_FAILURE:
561
562             if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
563                 int error;
564
565                 /* We have been disconnected, so let reconnect */
566
567                 fprintf(stderr, _("Disconnected, reconnecting ...\n"));
568
569                 avahi_client_free(client);
570                 client = NULL;
571
572                 avahi_string_list_free(browsed_types);
573                 browsed_types = NULL;
574
575                 while (services)
576                     remove_service(config, services);
577
578                 browsing = 0;
579
580                 if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
581                     fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
582                     avahi_simple_poll_quit(simple_poll);
583                 }
584
585             } else {
586                 fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
587                 avahi_simple_poll_quit(simple_poll);
588             }
589
590             break;
591
592         case AVAHI_CLIENT_S_REGISTERING:
593         case AVAHI_CLIENT_S_RUNNING:
594         case AVAHI_CLIENT_S_COLLISION:
595
596             if (!browsing)
597                 if (start(config) < 0)
598                     avahi_simple_poll_quit(simple_poll);
599
600             break;
601
602         case AVAHI_CLIENT_CONNECTING:
603
604             if (config->verbose && !config->parsable)
605                 fprintf(stderr, _("Waiting for daemon ...\n"));
606
607             break;
608     }
609 }
610
611 static void help(FILE *f, const char *argv0) {
612     if (strstr(argv0, "domain"))
613         fprintf(f, "%s [options] \n\n", argv0);
614     else
615         fprintf(f,
616                 "%s [options] <service type>\n"
617                 "%s [options] -a\n"
618                 "%s [options] -D\n"
619 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
620                 "%s [options] -b\n"
621 #endif
622                 "\n",
623 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
624                 argv0,
625 #endif
626                 argv0, argv0, argv0);
627
628     fprintf(f, "%s%s",
629             _("    -h --help            Show this help\n"
630               "    -V --version         Show version\n"
631               "    -D --browse-domains  Browse for browsing domains instead of services\n"
632               "    -a --all             Show all services, regardless of the type\n"
633               "    -d --domain=DOMAIN   The domain to browse in\n"
634               "    -v --verbose         Enable verbose mode\n"
635               "    -t --terminate       Terminate after dumping a more or less complete list\n"
636               "    -c --cache           Terminate after dumping all entries from the cache\n"
637               "    -l --ignore-local    Ignore local services\n"
638               "    -r --resolve         Resolve services found\n"
639               "    -f --no-fail         Don't fail if the daemon is not available\n"
640               "    -p --parsable        Output in parsable format\n"),
641 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
642             _("    -k --no-db-lookup    Don't lookup service types\n"
643               "    -b --dump-db         Dump service type database\n")
644 #else
645             ""
646 #endif
647             );
648 }
649
650 static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
651     int o;
652
653     static const struct option long_options[] = {
654         { "help",           no_argument,       NULL, 'h' },
655         { "version",        no_argument,       NULL, 'V' },
656         { "browse-domains", no_argument,       NULL, 'D' },
657         { "domain",         required_argument, NULL, 'd' },
658         { "all",            no_argument,       NULL, 'a' },
659         { "verbose",        no_argument,       NULL, 'v' },
660         { "terminate",      no_argument,       NULL, 't' },
661         { "cache",          no_argument,       NULL, 'c' },
662         { "ignore-local",   no_argument,       NULL, 'l' },
663         { "resolve",        no_argument,       NULL, 'r' },
664         { "no-fail",        no_argument,       NULL, 'f' },
665         { "parsable",      no_argument,       NULL, 'p' },
666 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
667         { "no-db-lookup",   no_argument,       NULL, 'k' },
668         { "dump-db",        no_argument,       NULL, 'b' },
669 #endif
670         { NULL, 0, NULL, 0 }
671     };
672
673     assert(c);
674
675     c->command = strstr(argv0, "domain") ? COMMAND_BROWSE_DOMAINS : COMMAND_BROWSE_SERVICES;
676     c->verbose =
677         c->terminate_on_cache_exhausted =
678         c->terminate_on_all_for_now =
679         c->ignore_local =
680         c->resolve =
681         c->no_fail =
682         c->parsable = 0;
683     c->domain = c->stype = NULL;
684
685 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
686     c->no_db_lookup = 0;
687 #endif
688
689     while ((o = getopt_long(argc, argv, "hVd:avtclrDfp"
690 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
691                             "kb"
692 #endif
693                             , long_options, NULL)) >= 0) {
694
695         switch(o) {
696             case 'h':
697                 c->command = COMMAND_HELP;
698                 break;
699             case 'V':
700                 c->command = COMMAND_VERSION;
701                 break;
702             case 'a':
703                 c->command = COMMAND_BROWSE_ALL_SERVICES;
704                 break;
705             case 'D':
706                 c->command = COMMAND_BROWSE_DOMAINS;
707                 break;
708             case 'd':
709                 avahi_free(c->domain);
710                 c->domain = avahi_strdup(optarg);
711                 break;
712             case 'v':
713                 c->verbose = 1;
714                 break;
715             case 't':
716                 c->terminate_on_all_for_now = 1;
717                 break;
718             case 'c':
719                 c->terminate_on_cache_exhausted = 1;
720                 break;
721             case 'l':
722                 c->ignore_local = 1;
723                 break;
724             case 'r':
725                 c->resolve = 1;
726                 break;
727             case 'f':
728                 c->no_fail = 1;
729                 break;
730             case 'p':
731                 c->parsable = 1;
732                 break;
733 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
734             case 'k':
735                 c->no_db_lookup = 1;
736                 break;
737             case 'b':
738                 c->command = COMMAND_DUMP_STDB;
739                 break;
740 #endif
741             default:
742                 return -1;
743         }
744     }
745
746     if (c->command == COMMAND_BROWSE_SERVICES) {
747         if (optind >= argc) {
748             fprintf(stderr, _("Too few arguments\n"));
749             return -1;
750         }
751
752         c->stype = avahi_strdup(argv[optind]);
753         optind++;
754     }
755
756     if (optind < argc) {
757         fprintf(stderr, _("Too many arguments\n"));
758         return -1;
759     }
760
761     return 0;
762 }
763
764 int main(int argc, char *argv[]) {
765     int ret = 1, error;
766     Config config;
767     const char *argv0;
768     char *ec;
769
770     avahi_init_i18n();
771     setlocale(LC_ALL, "");
772
773     if ((argv0 = strrchr(argv[0], '/')))
774         argv0++;
775     else
776         argv0 = argv[0];
777
778     if ((ec = getenv("COLUMNS")))
779         n_columns = atoi(ec);
780
781     if (n_columns < 40)
782         n_columns = 40;
783
784     if (parse_command_line(&config, argv0, argc, argv) < 0)
785         goto fail;
786
787     switch (config.command) {
788         case COMMAND_HELP:
789             help(stdout, argv0);
790             ret = 0;
791             break;
792
793         case COMMAND_VERSION:
794             printf("%s "PACKAGE_VERSION"\n", argv0);
795             ret = 0;
796             break;
797
798         case COMMAND_BROWSE_SERVICES:
799         case COMMAND_BROWSE_ALL_SERVICES:
800         case COMMAND_BROWSE_DOMAINS:
801
802             if (!(simple_poll = avahi_simple_poll_new())) {
803                 fprintf(stderr, _("Failed to create simple poll object.\n"));
804                 goto fail;
805             }
806
807             if (sigint_install(simple_poll) < 0)
808                 goto fail;
809
810             if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
811                 fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error));
812                 goto fail;
813             }
814
815             avahi_simple_poll_loop(simple_poll);
816             ret = 0;
817             break;
818
819 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
820         case COMMAND_DUMP_STDB: {
821             char *t;
822             stdb_setent();
823
824             while ((t = stdb_getent())) {
825                 if (config.no_db_lookup)
826                     printf("%s\n", t);
827                 else
828                     printf("%s\n", stdb_lookup(t));
829             }
830
831             ret = 0;
832             break;
833         }
834 #endif
835     }
836
837
838 fail:
839
840     while (services)
841         remove_service(&config, services);
842
843     if (client)
844         avahi_client_free(client);
845
846     sigint_uninstall();
847
848     if (simple_poll)
849         avahi_simple_poll_free(simple_poll);
850
851     avahi_free(config.domain);
852     avahi_free(config.stype);
853
854     avahi_string_list_free(browsed_types);
855
856 #if defined(HAVE_GDBM) || defined(HAVE_DBM)
857     stdb_shutdown();
858 #endif
859
860     return ret;
861 }