]> git.meshlink.io Git - catta/commitdiff
add the ability to dump the service type database to avahi-browse
authorLennart Poettering <lennart@poettering.net>
Thu, 12 Apr 2007 22:14:40 +0000 (22:14 +0000)
committerLennart Poettering <lennart@poettering.net>
Thu, 12 Apr 2007 22:14:40 +0000 (22:14 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@1414 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-utils/avahi-browse.c
avahi-utils/stdb.c
avahi-utils/stdb.h
man/avahi-browse.1.xml.in

index faa8f8a2f6c0d4fbde6ffe6f50a6de849ae14a15..5d1a4c8641cd3b8f8b8416367ec3d62925959af3 100644 (file)
@@ -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
     }
     
     
index 388bf6b508860a0dd597e4405fd775c475ff637b..a4203647aca107a8bb80cde358d5fee8ed8e5f67 100644 (file)
@@ -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;
 }
index 92c2651461e79d8eb9cfcbdd3f16ee689bf119be..3f8ccc0db3f703c4aa8cc3d963dbf7c52ec3fb53 100644 (file)
@@ -26,5 +26,7 @@
 
 const char* stdb_lookup(const char *name);
 void stdb_shutdown(void);
+char *stdb_getent(void);
+void stdb_setent(void);
 
 #endif
index 07b75579f7d83eb7e0b922dc9db6df48b8965fb4..15856d26ec3ec16c7dcfd88a3b7f588453440df6 100644 (file)
@@ -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>
        
                <optdesc><p>Don't lookup services types in service type database.</p></optdesc>
          </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>