more flexibility in naming attribute files,
authorKlaus Kaempf <kkaempf@suse.de>
Thu, 14 Feb 2008 11:58:19 +0000 (11:58 +0000)
committerKlaus Kaempf <kkaempf@suse.de>
Thu, 14 Feb 2008 11:58:19 +0000 (11:58 +0000)
one can even have multiple ones in a single directory now ;-)

tools/common_write.c
tools/common_write.h
tools/dumpsolv.c
tools/repo_susetags.c
tools/repo_susetags.h
tools/susetags2solv.c

index 7862338..f72670d 100644 (file)
@@ -70,8 +70,13 @@ keyfilter_attr(Repo *data, Repokey *key, void *kfdata)
   return KEY_STORAGE_INCORE;
 }
 
+/*
+ * Write <repo> to stdout
+ * If <attrname> is given, write attributes to <attrname>
+ */
+
 int
-tool_write(Repo *repo, const char *basename, int separate)
+tool_write(Repo *repo, const char *basename, const char *attrname)
 {
   Pool *pool = repo->pool;
   Repodatafile fileinfoa[1];
@@ -80,14 +85,14 @@ tool_write(Repo *repo, const char *basename, int separate)
 
   create_filter(pool);
   memset (fileinfoa, 0, sizeof fileinfoa);
-  if (separate)
+  if (attrname)
     {
       test_separate = 1;
       fileinfo = fileinfoa;
-      FILE *fp = fopen ("test.attr", "w");
+      FILE *fp = fopen (attrname, "w");
       repo_write(repo, fp, keyfilter_attr, 0, fileinfo, 0);
       fclose (fp);
-      fileinfo->location = strdup ("test.attr");
+      fileinfo->location = strdup (attrname);
       fileinfo++;
 
       nsubfiles = fileinfo - fileinfoa;
index d32e0f5..22676c9 100644 (file)
@@ -10,6 +10,6 @@
 
 #include "repo.h"
 
-int tool_write(Repo *repo, const char *basename, int separate);
+int tool_write(Repo *repo, const char *basename, const char *attrname);
 
 #endif
index 631e529..cc55250 100644 (file)
@@ -10,6 +10,8 @@
 #include <unistd.h>
 #include <string.h>
 
+static char *attrname = 0;
+
 #include "pool.h"
 #include "repo_solv.h"
 #if 0
@@ -211,27 +213,111 @@ dump_some_attrs(Repo *repo, Solvable *s)
 static FILE *
 loadcallback (Pool *pool, Repodata *data, void *vdata)
 {
-  FILE *fp;
+  FILE *fp = 0;
   fprintf (stderr, "Loading SOLV file %s\n", data->location);
-  fp = fopen ("test.attr", "r");
+  if (attrname)
+    {
+      fp = fopen (attrname, "r");
+      if (!fp)
+       perror(attrname);
+    }
   return fp;
 }
 
+
+static void
+usage( const char *err )
+{
+  if (err)
+    fprintf (stderr, "\n** Error:\n  %s\n", err);
+  fprintf( stderr, "\nUsage:\n"
+          "dumpsolv [-a] [-n <attrname>] [<solvfile>]\n"
+          "  -a            read attributes.\n"
+          "                If no attribute name (-n) is given,\n"
+          "                  it is deduced from the .solv name\n"
+          "                  by replacing '.solv' with '.attr'\n"
+          "                If neither an attribute name nor a solvfile are given,\n"
+          "                  the attribute name defaults to 'test.attr'\n"
+          "  -n <attrname> use <attrname> (evtl. suffixed by '.attr') for attributes\n"
+          );
+  exit(0);
+}
+
+
 int main(int argc, char **argv)
 {
   Repo *repo;
   Pool *pool;
   int i, n;
   Solvable *s;
+  const char *solvname = 0;
+  
+  argv++;
+  argc--;
+  while (argc--)
+    {
+      const char *s = argv[0];
+      if (*s++ == '-')
+        while (*s)
+          switch (*s++)
+           {
+             case 'h': usage(NULL); break;
+             case 'a':
+               if (!attrname)
+                 attrname = "";
+               break;
+             case 'n':
+               if (argc)
+                 {
+                   attrname = argv[1];
+                   argv++;
+                   argc--;
+                 }
+               else
+                 usage("argument required for '-n'");
+               break;
+             default : break;
+           }
+      else
+       {
+         solvname = argv[0];
+         if (freopen (solvname, "r", stdin) == 0)
+           {
+             perror(solvname);
+             exit(1);
+           }
+         break;
+       }
+      argv++;
+    }
 
-  if (argc != 1)
+  if (attrname) /* attributes wanted */
     {
-      if (freopen(argv[1], "r", stdin) == 0)
+      if (*attrname == 0) /* no attrname given */
+      {
+       if (solvname) /* solvname given -> deduce attrname from it */
        {
-         perror(argv[1]);
-         exit(1);
+         attrname = strdup (solvname);
+         char *dot = strrchr(attrname, '.');
+          if (dot && !strcmp(dot, ".solv")) /* if it ends in .solv, just keep the dot */
+           dot[1] = 0;
        }
+       else
+         attrname = "test.attr"; /* default to "test.attr" */
+      }
+      
+      /* ensure '.attr' suffix */
+      const char *dot = strrchr(attrname, '.');
+      if (!dot || strcmp(dot, ".attr"))
+      {
+       int len = strlen (attrname);
+       char *newname = (char *)malloc (len + 6); /* alloc for <attrname>+'.attr'+'\0' */
+       strcpy (newname, attrname);
+       strcpy (newname+len, ".attr");
+       attrname = newname;
+      }
     }
+
   pool = pool_create();
   pool_setdebuglevel(pool, 1);
   pool_setloadcallback(pool, loadcallback, 0);
index e52fa3d..7f25973 100644 (file)
@@ -369,7 +369,7 @@ tag_from_string (char *cs)
  */
 
 void
-repo_add_susetags(Repo *repo, FILE *fp, Id vendor, int with_attr)
+repo_add_susetags(Repo *repo, FILE *fp, Id vendor, const char *attrname)
 {
   Pool *pool = repo->pool;
   char *line, *linep;
@@ -383,7 +383,7 @@ repo_add_susetags(Repo *repo, FILE *fp, Id vendor, int with_attr)
   struct parsedata pd;
   Repodata *data = 0;
 
-  if (with_attr)
+  if (attrname)
     {
       data = repo_add_repodata(repo);
       init_attr_ids(pool);
@@ -616,7 +616,7 @@ repo_add_susetags(Repo *repo, FILE *fp, Id vendor, int with_attr)
            indesc++;
            continue;
        }
-      if (!with_attr)
+      if (!attrname)
         continue;
       switch (tag)
         {
index f764d0b..44dba8d 100644 (file)
@@ -5,4 +5,8 @@
  * for further information
  */
 
-extern void repo_add_susetags(Repo *repo, FILE *fp, Id vendor, int with_attr);
+/* read susetags file <fp> into <repo>
+ * if <attrname> given, write attributes as '<attrname>.attr'
+ */
+
+extern void repo_add_susetags(Repo *repo, FILE *fp, Id vendor, const char *attrname);
index f8b825e..5446a11 100644 (file)
 #include "common_write.h"
 
 static void
-usage(void)
+usage(const char *err)
 {
-  fprintf(stderr, "Usage:\n"
+  if (err)
+    fprintf(stderr, "\n** Error:\n  %s\n", err);
+  fprintf(stderr, "\nUsage:\n"
           "susetags2solv [-a][-s][-c <content>][-h]\n"
          "  reads a 'susetags' repository from <stdin> and writes a .solv file to <stdout>\n"
          "  -a : with attributes\n"
-         "  -c : parse given contentfile (for product information)\n"
+         "  -c <contenfile> : parse given contentfile (for product information)\n"
          "  -h : print help & exit\n"
-         "  -s : test separate\n"
+         "  -n <name>: save attributes as <name>.attr\n"
         );
+   exit(0);
 }
 
 int
 main(int argc, char **argv)
 {
-  int with_attr = 0;
-  int test_separate = 0;
   const char *contentfile = 0;
+  const char *attrname = 0;
   Id vendor = 0;
   argv++;
   argc--;
@@ -47,9 +49,21 @@ main(int argc, char **argv)
         while (*s)
           switch (*s++)
            {
-             case 'h': usage(); exit(0);
-             case 'a': with_attr = 1; break;
-             case 's': test_separate = 1; break;
+             case 'h': usage(NULL); break;
+             case 'a':
+               if (attrname == NULL)
+                 attrname = "test.attr";
+             break;
+             case 'n':
+               if (argc)
+                 {
+                   attrname = argv[1];
+                   argv++;
+                   argc--;
+                 }
+               else
+                 usage("argument required for '-n'");
+               break;
              case 'c':
                if (argc)
                  {
@@ -57,6 +71,8 @@ main(int argc, char **argv)
                    argv++;
                    argc--;
                  }
+               else
+                 usage("argument required for '-c'");
                break;
              default : break;
            }
@@ -77,8 +93,21 @@ main(int argc, char **argv)
         vendor = pool->solvables[repo->start].vendor;
       fclose (fp);
     }
-  repo_add_susetags(repo, stdin, vendor, with_attr);
-  tool_write(repo, 0, with_attr && test_separate);
+  if (attrname)
+    {
+      /* ensure '.attr' suffix */
+      const char *dot = strrchr(attrname, '.');
+      if (!dot || strcmp(dot, ".attr"))
+      {
+       int len = strlen (attrname);
+       char *newname = (char *)malloc (len + 6); /* alloc for <attrname>+'.attr'+'\0' */
+       strcpy (newname, attrname);
+       strcpy (newname+len, ".attr");
+       attrname = newname;
+      }
+    }
+  repo_add_susetags(repo, stdin, vendor, attrname);
+  tool_write(repo, 0, attrname);
   pool_free(pool);
   exit(0);
 }