X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-utils%2Favahi-browse.c;h=d75d1a965fe91ad24323b645968bc4def23b5239;hb=7ec1d734c64bc69fb467c88c99b130b8fc5eb9d7;hp=a5d9d25f06d2407cf8da5f06de24ae1c512334d8;hpb=93ac6c737b2566e76162ad70c35c0167de82c76b;p=catta diff --git a/avahi-utils/avahi-browse.c b/avahi-utils/avahi-browse.c index a5d9d25..d75d1a9 100644 --- a/avahi-utils/avahi-browse.c +++ b/avahi-utils/avahi-browse.c @@ -43,7 +43,7 @@ #include "sigint.h" -#ifdef HAVE_GDBM +#if defined(HAVE_GDBM) || defined(HAVE_DBM) #include "stdb.h" #endif @@ -53,6 +53,9 @@ typedef enum { COMMAND_BROWSE_SERVICES, COMMAND_BROWSE_ALL_SERVICES, COMMAND_BROWSE_DOMAINS +#if defined(HAVE_GDBM) || defined(HAVE_DBM) + , COMMAND_DUMP_STDB +#endif } Command; typedef struct Config { @@ -65,7 +68,8 @@ typedef struct Config { Command command; int resolve; int no_fail; -#ifdef HAVE_GDBM + int parsable; +#if defined(HAVE_GDBM) || defined(HAVE_DBM) int no_db_lookup; #endif } Config; @@ -99,7 +103,7 @@ static void check_terminate(Config *c) { if (n_all_for_now <= 0 && n_resolving <= 0) { - if (c->verbose) { + if (c->verbose && !c->parsable) { printf(": All for now\n"); n_all_for_now++; /* Make sure that this event is not repeated */ } @@ -110,7 +114,7 @@ static void check_terminate(Config *c) { if (n_cache_exhausted <= 0 && n_resolving <= 0) { - if (c->verbose) { + if (c->verbose && !c->parsable) { printf(": Cache exhausted\n"); n_cache_exhausted++; /* Make sure that this event is not repeated */ } @@ -127,27 +131,39 @@ static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol, if (i->interface == interface && i->protocol == protocol && strcasecmp(i->name, name) == 0 && - avahi_domain_equal(i->type, type) == 0 && - avahi_domain_equal(i->domain, domain) == 0) + avahi_domain_equal(i->type, type) && + avahi_domain_equal(i->domain, domain)) return i; return NULL; } -static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) { +static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, int nl) { char ifname[IF_NAMESIZE]; -#ifdef HAVE_GDBM +#if defined(HAVE_GDBM) || defined(HAVE_DBM) if (!config->no_db_lookup) type = stdb_lookup(type); #endif - - printf("%c %4s %4s %-*s %-20s %s\n", - c, - interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a", - protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a", - n_columns-35, name, type, domain); + + if (config->parsable) { + char sn[AVAHI_DOMAIN_NAME_MAX], *e = sn; + size_t l = sizeof(sn); + + printf("%c;%s;%s;%s;%s;%s%s", + c, + interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a", + protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a", + avahi_escape_label(name, strlen(name), &e, &l), type, domain, nl ? "\n" : ""); + + } else + printf("%c %4s %4s %-*s %-20s %s\n", + c, + interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a", + protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a", + n_columns-35, name, type, domain); + fflush(stdout); } static void service_resolver_callback( @@ -178,16 +194,24 @@ static void service_resolver_callback( t = avahi_string_list_to_string(txt); - print_service_line(i->config, '=', interface, protocol, name, type, domain); - - printf(" hostname = [%s]\n" - " address = [%s]\n" - " port = [%i]\n" - " txt = [%s]\n", - host_name, - address, - port, - t); + print_service_line(i->config, '=', interface, protocol, name, type, domain, 0); + + if (i->config->parsable) + printf(";%s;%s;%u;%s\n", + host_name, + address, + port, + t); + else + printf(" hostname = [%s]\n" + " address = [%s]\n" + " port = [%u]\n" + " txt = [%s]\n", + host_name, + address, + port, + t); + avahi_free(t); break; @@ -206,6 +230,7 @@ static void service_resolver_callback( assert(n_resolving > 0); n_resolving--; check_terminate(i->config); + fflush(stdout); } static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) { @@ -277,7 +302,7 @@ static void service_browser_callback( add_service(c, interface, protocol, name, type, domain); - print_service_line(c, '+', interface, protocol, name, type, domain); + print_service_line(c, '+', interface, protocol, name, type, domain, 1); break; } @@ -290,7 +315,7 @@ static void service_browser_callback( remove_service(c, info); - print_service_line(c, '-', interface, protocol, name, type, domain); + print_service_line(c, '-', interface, protocol, name, type, domain, 1); break; } @@ -426,12 +451,19 @@ static void domain_browser_callback( case AVAHI_BROWSER_NEW: case AVAHI_BROWSER_REMOVE: { char ifname[IF_NAMESIZE]; - - printf("%c %4s %4s %s\n", - event == AVAHI_BROWSER_NEW ? '+' : '-', - interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a", - protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a", - domain); + + if (c->parsable) + printf("%c;%s;%s;%s\n", + event == AVAHI_BROWSER_NEW ? '+' : '-', + interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "", + protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "", + domain); + else + printf("%c %4s %4s %s\n", + event == AVAHI_BROWSER_NEW ? '+' : '-', + interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a", + protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a", + domain); break; } @@ -479,7 +511,7 @@ static int start(Config *config) { assert(!browsing); - if (config->verbose) { + if (config->verbose && !config->parsable) { const char *version, *hn; if (!(version = avahi_client_get_version_string(client))) { @@ -565,7 +597,7 @@ static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UN case AVAHI_CLIENT_CONNECTING: - if (config->verbose) + if (config->verbose && !config->parsable) fprintf(stderr, "Waiting for daemon ...\n"); break; @@ -579,7 +611,14 @@ static void help(FILE *f, const char *argv0) { fprintf(f, "%s [options] \n" "%s [options] -a\n" - "%s [options] -D\n\n", + "%s [options] -D\n" +#if defined(HAVE_GDBM) || defined(HAVE_DBM) + "%s [options] -b\n" +#endif + "\n", +#if defined(HAVE_GDBM) || defined(HAVE_DBM) + argv0, +#endif argv0, argv0, argv0); fprintf(f, @@ -594,8 +633,10 @@ static void help(FILE *f, const char *argv0) { " -l --ignore-local Ignore local services\n" " -r --resolve Resolve services found\n" " -f --no-fail Don't fail if the daemon is not available\n" -#ifdef HAVE_GDBM + " -p --parsable Output in parsable format\n" +#if defined(HAVE_GDBM) || defined(HAVE_DBM) " -k --no-db-lookup Don't lookup service types\n" + " -b --dump-db Dump service type database\n" #endif ); } @@ -615,8 +656,10 @@ static int parse_command_line(Config *c, const char *argv0, int argc, char *argv { "ignore-local", no_argument, NULL, 'l' }, { "resolve", no_argument, NULL, 'r' }, { "no-fail", no_argument, NULL, 'f' }, -#ifdef HAVE_GDBM + { "parsable", no_argument, NULL, 'p' }, +#if defined(HAVE_GDBM) || defined(HAVE_DBM) { "no-db-lookup", no_argument, NULL, 'k' }, + { "dump-db", no_argument, NULL, 'b' }, #endif { NULL, 0, NULL, 0 } }; @@ -629,17 +672,17 @@ static int parse_command_line(Config *c, const char *argv0, int argc, char *argv c->terminate_on_all_for_now = c->ignore_local = c->resolve = - c->no_fail = 0; + c->no_fail = + c->parsable = 0; c->domain = c->stype = NULL; -#ifdef HAVE_GDBM +#if defined(HAVE_GDBM) || defined(HAVE_DBM) c->no_db_lookup = 0; #endif - opterr = 0; - while ((o = getopt_long(argc, argv, "hVd:avtclrDf" -#ifdef HAVE_GDBM - "k" + while ((o = getopt_long(argc, argv, "hVd:avtclrDfp" +#if defined(HAVE_GDBM) || defined(HAVE_DBM) + "kb" #endif , long_options, NULL)) >= 0) { @@ -678,13 +721,18 @@ static int parse_command_line(Config *c, const char *argv0, int argc, char *argv case 'f': c->no_fail = 1; break; -#ifdef HAVE_GDBM + case 'p': + c->parsable = 1; + break; +#if defined(HAVE_GDBM) || defined(HAVE_DBM) case 'k': c->no_db_lookup = 1; break; + case 'b': + c->command = COMMAND_DUMP_STDB; + break; #endif default: - fprintf(stderr, "Invalid command line argument: %c\n", o); return -1; } } @@ -760,6 +808,23 @@ int main(int argc, char *argv[]) { avahi_simple_poll_loop(simple_poll); ret = 0; break; + +#if defined(HAVE_GDBM) || defined(HAVE_DBM) + case COMMAND_DUMP_STDB: { + char *t; + stdb_setent(); + + while ((t = stdb_getent())) { + if (config.no_db_lookup) + printf("%s\n", t); + else + printf("%s\n", stdb_lookup(t)); + } + + ret = 0; + break; + } +#endif } @@ -781,7 +846,7 @@ fail: avahi_string_list_free(browsed_types); -#ifdef HAVE_GDBM +#if defined(HAVE_GDBM) || defined(HAVE_DBM) stdb_shutdown(); #endif