]> git.wh0rd.org - fontconfig.git/blobdiff - fc-cat/fc-cat.c
With no args, fc-cat now dumps all directories.
[fontconfig.git] / fc-cat / fc-cat.c
index 36f8bd9865047549fd1399cd969ffaa0b2a3dc9c..6074f1d92f1fe81670fa15e877979dccb82fb1fc 100644 (file)
@@ -31,8 +31,8 @@
 #define HAVE_GETOPT 1
 #endif
 
-#include <fontconfig/fontconfig.h>
-#include <../src/fccache.c>
+#include "../src/fccache.c"
+#include "../fc-arch/fcarch.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -53,6 +53,8 @@
 #include <getopt.h>
 const struct option longopts[] = {
     {"version", 0, 0, 'V'},
+    {"verbose", 0, 0, 'v'},
+    {"recurse", 0, 0, 'r'},
     {"help", 0, 0, '?'},
     {NULL,0,0,0},
 };
@@ -78,9 +80,6 @@ extern int optind, opterr, optopt;
 #define PUTC(c,f) putc(c,f)
 #endif
 
-FcBool
-FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name);
-
 static FcBool
 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
 {
@@ -149,13 +148,15 @@ static void
 usage (char *program)
 {
 #if HAVE_GETOPT_LONG
-    fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
-            program);
+    fprintf (stderr, "usage: %s [-V?] [--version] [--help] {*-%s.cache-2|directory}...\n",
+            program, FC_ARCHITECTURE);
 #else
-    fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n",
-            program);
+    fprintf (stderr, "usage: %s [-fsvV?] {*-%s.cache-2|directory}...\n",
+            program, FC_ARCHITECTURE);
 #endif
-    fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n");
+    fprintf (stderr, "Reads font information cache from:\n"); 
+    fprintf (stderr, " 1) specified fontconfig cache file\n");
+    fprintf (stderr, " 2) related to a particular font directory\n");
     fprintf (stderr, "\n");
 #if HAVE_GETOPT_LONG
     fprintf (stderr, "  -V, --version        display font config version and exit\n");
@@ -167,39 +168,22 @@ usage (char *program)
     exit (1);
 }
 
-/* read serialized state from the cache file */
-static char *
-FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file)
+static int
+FcCacheFileOpen (char *cache_file, off_t *size)
 {
-    FILE *file;
     int fd;
-    char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
-    static char name_buf[8192];
-    FcChar8 * ls;
-    char * buf;
-    int i;
-
-    if (!cache_file)
-        goto bail;
+    struct stat file_stat;
 
-    file = fopen(cache_file, "rb");
-    if (file == NULL)
-        goto bail;
+    fd = open(cache_file, O_RDONLY | O_BINARY);
+    if (fd < 0)
+        return -1;
 
-    if (!FcDirCacheConsume (file, set, dirs, NULL, name_buf))
-       goto bail1;
-    
-    fclose (file);
-    
-    printf ("fc-cat: printing directory cache for cache which would be named %s\n", 
-           name_buf);
-
-    return name_buf;
-
- bail1:
-    fclose (file);
- bail:
-    return 0;
+    if (fstat (fd, &file_stat) < 0) {
+       close (fd); 
+       return -1;
+    }
+    *size = file_stat.st_size;
+    return fd;
 }
 
 /*
@@ -210,16 +194,15 @@ static const FcChar8 *
 FcFileBaseName (const char *cache, const FcChar8 *file)
 {
     const FcChar8   *cache_slash;
+    int                    cache_len = strlen (cache);
 
-    cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
-    if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
-                                (cache_slash + 1) - (const FcChar8 *)cache))
-       return file + ((cache_slash + 1) - (const FcChar8 *)cache);
+    if (!strncmp (cache, file, cache_len) && file[cache_len] == '/')
+       return file + cache_len + 1;
     return file;
 }
 
 FcBool
-FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
+FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name, FcBool verbose)
 {
     FcPattern      *font;
     FcChar8        *name, *dir;
@@ -227,6 +210,7 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
     int                    ret;
     int                    n;
     int                    id;
+    int                    ndir = 0;
     FcStrList      *list;
 
     list = FcStrListCreate (dirs);
@@ -248,18 +232,20 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
            goto bail3;
        if (PUTC ('\n', stdout) == EOF)
            goto bail3;
+       ndir++;
     }
     
     for (n = 0; n < set->nfont; n++)
     {
-       font = set->fonts[n];
+       FcPattern   **fonts = FcFontSetFonts (set);
+       FcPattern   *encoded_font = fonts[n];
+       FcPattern   *font = FcEncodedOffsetToPtr (set, encoded_font, FcPattern);
+
        if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
            goto bail3;
        base = FcFileBaseName (base_name, file);
        if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
            goto bail3;
-       if (FcDebug () & FC_DBG_CACHEV)
-           printf (" write file \"%s\"\n", base);
        if (!FcCacheWriteStringOld (stdout, base))
            goto bail3;
        if (PUTC (' ', stdout) == EOF)
@@ -278,6 +264,8 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
        if (PUTC ('\n', stdout) == EOF)
            goto bail3;
     }
+    if (verbose && !set->nfont && !ndir)
+       printf ("<empty>\n");
     
     FcStrListDone (list);
 
@@ -289,21 +277,50 @@ bail2:
     return FcFalse;
 }
 
+FcCache *
+FcCacheFileMap (const FcChar8 *file)
+{
+    FcCache *cache;
+    int            fd;
+    struct stat file_stat;
+
+    fd = open (file, O_RDONLY | O_BINARY);
+    if (fd < 0)
+       return NULL;
+    if (fstat (fd, &file_stat) < 0) {
+       close (fd);
+       return NULL;
+    }
+    if (!FcDirCacheLoad (fd, file_stat.st_size, &cache)) {
+       close (fd);
+       return NULL;
+    }
+    close (fd);
+    return cache;
+}
+
 int
 main (int argc, char **argv)
 {
     int                i;
+    int                ret = 0;
+    FcFontSet  *fs;
+    FcStrSet    *dirs;
+    FcStrSet   *args = NULL;
+    FcStrList  *arglist;
+    FcCache    *cache;
+    FcConfig   *config;
+    FcChar8    *arg;
+    int                verbose = 0;
+    int                recurse = 0;
+    FcBool     first = FcTrue;
 #if HAVE_GETOPT_LONG || HAVE_GETOPT
     int                c;
-    FcFontSet  *fs = FcFontSetCreate();
-    FcStrSet    *dirs = FcStrSetCreate();
-    char       *name_buf;
-    FcConfig   *config;
 
 #if HAVE_GETOPT_LONG
-    while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
+    while ((c = getopt_long (argc, argv, "Vvr?", longopts, NULL)) != -1)
 #else
-    while ((c = getopt (argc, argv, "fsVv?")) != -1)
+    while ((c = getopt (argc, argv, "Vvr?")) != -1)
 #endif
     {
        switch (c) {
@@ -311,6 +328,12 @@ main (int argc, char **argv)
            fprintf (stderr, "fontconfig version %d.%d.%d\n", 
                     FC_MAJOR, FC_MINOR, FC_REVISION);
            exit (0);
+       case 'v':
+           verbose++;
+           break;
+       case 'r':
+           recurse++;
+           break;
        default:
            usage (argv[0]);
        }
@@ -328,32 +351,96 @@ main (int argc, char **argv)
     }
     FcConfigSetCurrent (config);
     
-    if (i >= argc)
-        usage (argv[0]);
-
-    if (FcFileIsDir ((const FcChar8 *)argv[i]))
+    args = FcStrSetCreate ();
+    if (!args)
     {
-        char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i], 
-                                               (FcChar8 *)"/dummy");
-        if (!FcDirScanConfig (fs, dirs, 0, 
-                              (const FcChar8 *)argv[i], FcFalse, config))
-            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);
-        }
+       fprintf (stderr, "%s: malloc failure\n", argv[0]);
+       return 1;
+    }
+    if (i < argc)
+    {
+       for (; i < argc; i++)
+       {
+           if (!FcStrSetAdd (args, argv[i]))
+           {
+               fprintf (stderr, "%s: malloc failure\n", argv[0]);
+               return 1;
+           }
+       }
+       arglist = FcStrListCreate (args);
+       if (!arglist)
+       {
+           fprintf (stderr, "%s: malloc failure\n", argv[0]);
+           return 1;
+       }
     }
-    else if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0)
-       FcCachePrintSet (fs, dirs, name_buf);
     else
     {
-       printf ("nothing to do\n");
+       recurse++;
+       arglist = FcConfigGetFontDirs (config);
+       while ((arg = FcStrListNext (arglist)))
+           if (!FcStrSetAdd (args, arg))
+           {
+               fprintf (stderr, "%s: malloc failure\n", argv[0]);
+               return 1;
+           }
+       FcStrListDone (arglist);
+    }
+    arglist = FcStrListCreate (args);
+    if (!arglist)
+    {
+       fprintf (stderr, "%s: malloc failure\n", argv[0]);
+       return 1;
     }
 
-    FcStrSetDestroy (dirs);
-    FcFontSetDestroy (fs);
+    while ((arg = FcStrListNext (arglist)))
+    {
+       int         j;
+       off_t       size;
+       intptr_t    *cache_dirs;
+       FcChar8     *cache_file = NULL;
+       
+       if (FcFileIsDir (arg))
+           cache = FcDirCacheMap (arg, config, &cache_file);
+       else
+           cache = FcCacheFileMap (arg);
+       if (!cache)
+       {
+           perror ((char *) arg);
+           ret++;
+           continue;
+       }
+       
+       dirs = FcStrSetCreate ();
+       fs = FcCacheSet (cache);
+       cache_dirs = FcCacheDirs (cache);
+       for (j = 0; j < cache->dirs_count; j++) 
+       {
+           FcStrSetAdd (dirs, FcOffsetToPtr (cache_dirs,
+                                             cache_dirs[j],
+                                             FcChar8));
+           if (recurse)
+               FcStrSetAdd (args, FcOffsetToPtr (cache_dirs,
+                                             cache_dirs[j],
+                                             FcChar8));
+       }
+
+       if (verbose)
+       {
+           if (!first)
+               printf ("\n");
+           printf ("Directory: %s\nCache: %s\n--------\n",
+                   FcCacheDir(cache), cache_file ? cache_file : arg);
+           first = FcFalse;
+       }
+        FcCachePrintSet (fs, dirs, FcCacheDir (cache), verbose);
+
+       FcStrSetDestroy (dirs);
+
+       FcDirCacheUnmap (cache);
+       if (cache_file)
+           FcStrFree (cache_file);
+    }
 
     return 0;
 }