summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2007-04-12 22:14:40 +0000
committerLennart Poettering <lennart@poettering.net>2007-04-12 22:14:40 +0000
commitbc24853c6421b9374eb4825ae40178d14e0c0575 (patch)
treee2044a12745382d978ed5200c94d68ea4c55e359
parent7b2cfb7ec3c22b16615d4dfb37bdf08f85f31bad (diff)
add the ability to dump the service type database to avahi-browse
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@1414 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
-rw-r--r--avahi-utils/avahi-browse.c33
-rw-r--r--avahi-utils/stdb.c54
-rw-r--r--avahi-utils/stdb.h2
-rw-r--r--man/avahi-browse.1.xml.in6
4 files changed, 93 insertions, 2 deletions
diff --git a/avahi-utils/avahi-browse.c b/avahi-utils/avahi-browse.c
index faa8f8a..5d1a4c8 100644
--- a/avahi-utils/avahi-browse.c
+++ b/avahi-utils/avahi-browse.c
@@ -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 {
@@ -581,7 +584,11 @@ static void help(FILE *f, const char *argv0) {
fprintf(f,
"%s [options] <service type>\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",
argv0, argv0, argv0);
fprintf(f,
@@ -598,6 +605,7 @@ static void help(FILE *f, const char *argv0) {
" -f --no-fail Don't fail if the daemon is not available\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
);
}
@@ -619,6 +627,7 @@ static int parse_command_line(Config *c, const char *argv0, int argc, char *argv
{ "no-fail", no_argument, NULL, 'f' },
#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 }
};
@@ -641,7 +650,7 @@ static int parse_command_line(Config *c, const char *argv0, int argc, char *argv
opterr = 0;
while ((o = getopt_long(argc, argv, "hVd:avtclrDf"
#if defined(HAVE_GDBM) || defined(HAVE_DBM)
- "k"
+ "kb"
#endif
, long_options, NULL)) >= 0) {
@@ -684,6 +693,9 @@ static int parse_command_line(Config *c, const char *argv0, int argc, char *argv
case 'k':
c->no_db_lookup = 1;
break;
+ case 'b':
+ c->command = COMMAND_DUMP_STDB;
+ break;
#endif
default:
fprintf(stderr, "Invalid command line argument: %s\n", argv[optind-1]);
@@ -762,6 +774,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
}
diff --git a/avahi-utils/stdb.c b/avahi-utils/stdb.c
index 388bf6b..a420364 100644
--- a/avahi-utils/stdb.c
+++ b/avahi-utils/stdb.c
@@ -43,6 +43,7 @@ static GDBM_FILE gdbm_file = NULL;
static DBM *dbm_file = NULL;
#endif
static char *buffer = NULL;
+static char *enum_key = NULL;
static int init(void) {
@@ -150,11 +151,64 @@ void stdb_shutdown(void) {
#ifdef HAVE_GDBM
if (gdbm_file)
gdbm_close(gdbm_file);
+
+ gdbm_file = NULL;
#endif
#ifdef HAVE_DBM
if (dbm_file)
dbm_close(dbm_file);
+
+ dbm_file = NULL;
#endif
avahi_free(buffer);
+ avahi_free(enum_key);
+
+ buffer = enum_key = NULL;
+}
+
+char *stdb_getent(void) {
+ datum key;
+
+ if (init() < 0)
+ return NULL;
+
+ for (;;) {
+
+ if (!enum_key) {
+#ifdef HAVE_GDBM
+ key = gdbm_firstkey(gdbm_file);
+#endif
+#ifdef HAVE_DBM
+ key = dbm_firstkey(dbm_file);
+#endif
+ } else {
+ key.dptr = enum_key;
+ key.dsize = strlen(enum_key);
+
+#ifdef HAVE_GDBM
+ key = gdbm_nextkey(gdbm_file, key);
+#endif
+#ifdef HAVE_DBM
+ key = dbm_nextkey(dbm_file, key);
+#endif
+ }
+
+ avahi_free(enum_key);
+ enum_key = NULL;
+
+ if (!key.dptr)
+ return NULL;
+
+ enum_key = avahi_strndup(key.dptr, key.dsize);
+ free(key.dptr);
+
+ if (!strchr(enum_key, '['))
+ return enum_key;
+ }
+}
+
+void stdb_setent(void) {
+ avahi_free(enum_key);
+ enum_key = NULL;
}
diff --git a/avahi-utils/stdb.h b/avahi-utils/stdb.h
index 92c2651..3f8ccc0 100644
--- a/avahi-utils/stdb.h
+++ b/avahi-utils/stdb.h
@@ -26,5 +26,7 @@
const char* stdb_lookup(const char *name);
void stdb_shutdown(void);
+char *stdb_getent(void);
+void stdb_setent(void);
#endif
diff --git a/man/avahi-browse.1.xml.in b/man/avahi-browse.1.xml.in
index 07b7557..15856d2 100644
--- a/man/avahi-browse.1.xml.in
+++ b/man/avahi-browse.1.xml.in
@@ -29,6 +29,7 @@
<cmd>avahi-browse [<arg>options</arg>] <arg>service-type</arg></cmd>
<cmd>avahi-browse [<arg>options</arg>] <opt>--all</opt></cmd>
<cmd>avahi-browse [<arg>options</arg>] <opt>--browse-domains</opt></cmd>
+ <cmd>avahi-browse [<arg>options</arg>] <opt>--dump-db</opt></cmd>
<cmd>avahi-browse-domains [<arg>options</arg>]</cmd>
</synopsis>
@@ -96,6 +97,11 @@
</option>
<option>
+ <p><opt>-b | --dump-db</opt></p>
+ <optdesc><p>Dump the service type database (may be combined with -k)</p></optdesc>
+ </option>
+
+ <option>
<p><opt>-h | --help</opt></p>
<optdesc><p>Show help.</p></optdesc>
</option>