]> git.wh0rd.org - fontconfig.git/blobdiff - fc-cat/fc-cat.c
Enable fc-cat to print out old-style cache info when given a directory
[fontconfig.git] / fc-cat / fc-cat.c
index f088e4585a9220ff2003f190f02014b0b8e4fe11..f466e3ecbc62f3af09f1dd8feef1b698d5e44391 100644 (file)
@@ -79,7 +79,7 @@ extern int optind, opterr, optopt;
 #endif
 
 FcBool
-FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file);
+FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name);
 
 static FcBool
 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
@@ -168,12 +168,13 @@ usage (char *program)
 }
 
 static FcBool 
-FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
+FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char *cache_file)
 {
     char               name_buf[8192];
     int fd;
     char * current_arch_machine_name;
     char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
+    char               subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
     off_t current_arch_start = 0;
 
     if (!cache_file)
@@ -184,8 +185,7 @@ FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char * dir, char
     if (fd == -1)
        goto bail;
 
-    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name,
-                                          FcTrue);
+    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
     if (current_arch_start < 0)
        goto bail1;
 
@@ -196,16 +196,34 @@ FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char * dir, char
 
     while (1) 
     {
+       char * dir;
        FcCacheReadString (fd, name_buf, sizeof (name_buf));
        if (!strlen(name_buf))
            break;
        printf ("fc-cat: printing global cache contents for dir %s\n", 
                name_buf);
 
-       if (!FcDirCacheConsume (fd, dir, set))
+       do
+       {
+           if (!FcCacheReadString (fd, subdirName, 
+                                   sizeof (subdirName)) ||
+               !strlen (subdirName))
+               break;
+           /* then don't do anything with subdirName. */
+       } while (1);
+
+       if (!FcDirCacheConsume (fd, name_buf, set, 0))
+           goto bail1;
+
+       dir = malloc (strlen (name_buf) + 2);
+       if (!dir)
            goto bail1;
 
-       FcCachePrintSet (set, dirs, name_buf);
+       strcpy (dir, name_buf);
+       strcat (dir, "/");
+
+       FcCachePrintSet (set, dirs, dir);
+       free (dir);
 
        FcFontSetDestroy (set);
        set = FcFontSetCreate();
@@ -218,15 +236,15 @@ FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char * dir, char
 }
 
 /* read serialized state from the cache file */
-static FcBool
-FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
+static char *
+FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file)
 {
     int fd;
     char * current_arch_machine_name;
-    char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
     off_t current_arch_start = 0;
     char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
-    char name_buf[8192];
+    static char name_buf[8192], *dir;
+    FcChar8 * ls;
 
     if (!cache_file)
         goto bail;
@@ -239,32 +257,37 @@ FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
     FcCacheReadString (fd, name_buf, sizeof (name_buf));
     if (!strlen (name_buf))
        goto bail;
-    printf ("fc-cat: printing directory cache contents for dir %s\n", 
+    if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) == 0)
+       goto bail;
+    printf ("fc-cat: printing directory cache for cache which would be named %s\n", 
            name_buf);
 
-    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name,
-                                          FcFalse);
+    current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
     if (current_arch_start < 0)
         goto bail1;
 
-    lseek (fd, current_arch_start, SEEK_SET);
-    if (FcCacheReadString (fd, candidate_arch_machine_name, 
-                          sizeof (candidate_arch_machine_name)) == 0)
-       goto bail1;
-
     while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
         FcStrSetAdd (dirs, (FcChar8 *)subdirName);
 
-    if (!FcDirCacheConsume (fd, dir, set))
-       goto bail1;
-       
+    dir = strdup(name_buf);
+    ls = FcStrLastSlash ((FcChar8 *)dir);
+    if (ls)
+       *ls = 0;
+
+    if (!FcDirCacheConsume (fd, dir, set, 0))
+       goto bail2;
+    free (dir);
+
     close(fd);
-    return FcTrue;
+    return name_buf;
+
+ bail2:
+    free (dir);
 
  bail1:
     close (fd);
  bail:
-    return FcFalse;
+    return 0;
 }
 
 /*
@@ -284,7 +307,7 @@ FcFileBaseName (const char *cache, const FcChar8 *file)
 }
 
 FcBool
-FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
+FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
 {
     FcPattern      *font;
     FcChar8        *name, *dir;
@@ -300,7 +323,7 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
     
     while ((dir = FcStrListNext (list)))
     {
-       base = FcFileBaseName (cache_file, dir);
+       base = FcFileBaseName (base_name, dir);
        if (!FcCacheWriteStringOld (stdout, base))
            goto bail3;
        if (PUTC (' ', stdout) == EOF)
@@ -320,7 +343,7 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
        font = set->fonts[n];
        if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
            goto bail3;
-       base = FcFileBaseName (cache_file, file);
+       base = FcFileBaseName (base_name, file);
        if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
            goto bail3;
        if (FcDebug () & FC_DBG_CACHEV)
@@ -354,6 +377,16 @@ bail2:
     return FcFalse;
 }
 
+FcBool
+FcFileIsDir (const FcChar8 *file)
+{
+    struct stat            statb;
+
+    if (stat ((const char *) file, &statb) != 0)
+       return FcFalse;
+    return S_ISDIR(statb.st_mode);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -362,6 +395,7 @@ main (int argc, char **argv)
     int                c;
     FcFontSet  *fs = FcFontSetCreate();
     FcStrSet    *dirs = FcStrSetCreate();
+    char       *name_buf;
 
 #if HAVE_GETOPT_LONG
     while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
@@ -386,14 +420,27 @@ main (int argc, char **argv)
     if (i >= argc)
         usage (argv[0]);
 
-    if (FcCacheFileRead (fs, dirs, dirname (strdup(argv[i])), argv[i]))
-       FcCachePrintSet (fs, dirs, argv[i]);
+    if (FcFileIsDir ((const FcChar8 *)argv[i]))
+    {
+        char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i], 
+                                               (FcChar8 *)"/dummy");
+        if (!FcDirScanConfig (fs, dirs, 0, 0, 
+                              (const FcChar8 *)argv[i], FcFalse, 0))
+            fprintf (stderr, "couldn't load font dir %s\n", argv[i]);
+        else
+        {
+            /* sorry, we can't tell you where the cache file is. */
+            FcCachePrintSet (fs, dirs, dummy_name);
+            FcStrFree ((FcChar8 *)dummy_name);
+        }
+    }
+    else if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0)
+       FcCachePrintSet (fs, dirs, name_buf);
     else
     {
         FcStrSetDestroy (dirs);
         dirs = FcStrSetCreate ();
-        if (FcCacheGlobalFileReadAndPrint (fs, dirs, dirname (strdup (argv[i])),
-                                           argv[i]))
+        if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i]))
             ;
     }