X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=fc-cat%2Ffc-cat.c;h=bb804ab0271c1af7d8dfc2f59d976c138ddd9622;hb=9b511b290548ad2920cda94507a3311efc461e8a;hp=c033bdc776359ec1886ddacb39612d21750220f3;hpb=76abb77f26c43d069919f80e960c71c2242fb5c2;p=fontconfig.git diff --git a/fc-cat/fc-cat.c b/fc-cat/fc-cat.c index c033bdc..bb804ab 100644 --- a/fc-cat/fc-cat.c +++ b/fc-cat/fc-cat.c @@ -31,8 +31,8 @@ #define HAVE_GETOPT 1 #endif -#include -#include <../src/fccache.c> +#include "../src/fccache.c" +#include "../fc-arch/fcarch.h" #include #include #include @@ -54,6 +54,7 @@ const struct option longopts[] = { {"version", 0, 0, 'V'}, {"verbose", 0, 0, 'v'}, + {"recurse", 0, 0, 'r'}, {"help", 0, 0, '?'}, {NULL,0,0,0}, }; @@ -79,11 +80,8 @@ 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) +write_chars (FILE *f, const FcChar8 *chars) { FcChar8 c; while ((c = *chars++)) @@ -103,7 +101,7 @@ FcCacheWriteChars (FILE *f, const FcChar8 *chars) } static FcBool -FcCacheWriteUlong (FILE *f, unsigned long t) +write_ulong (FILE *f, unsigned long t) { int pow; unsigned long temp, digit; @@ -128,18 +126,18 @@ FcCacheWriteUlong (FILE *f, unsigned long t) } static FcBool -FcCacheWriteInt (FILE *f, int i) +write_int (FILE *f, int i) { - return FcCacheWriteUlong (f, (unsigned long) i); + return write_ulong (f, (unsigned long) i); } static FcBool -FcCacheWriteStringOld (FILE *f, const FcChar8 *string) +write_string (FILE *f, const FcChar8 *string) { if (PUTC ('"', f) == EOF) return FcFalse; - if (!FcCacheWriteChars (f, string)) + if (!write_chars (f, string)) return FcFalse; if (PUTC ('"', f) == EOF) return FcFalse; @@ -150,13 +148,15 @@ static void usage (char *program) { #if HAVE_GETOPT_LONG - fprintf (stderr, "usage: %s [-V?] [--version] [--help] \n", - program); + fprintf (stderr, "usage: %s [-V?] [--version] [--help] {*-%s.cache-2|directory}...\n", + program, FC_ARCHITECTURE); #else - fprintf (stderr, "usage: %s [-fsvV?] \n", - program); + fprintf (stderr, "usage: %s [-fsvV?] {*-%s.cache-2|directory}...\n", + program, FC_ARCHITECTURE); #endif - fprintf (stderr, "Reads font information caches in \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"); @@ -168,30 +168,12 @@ usage (char *program) exit (1); } -static int -FcCacheFileOpen (char *cache_file, off_t *size) -{ - int fd; - struct stat file_stat; - - fd = open(cache_file, O_RDONLY | O_BINARY); - if (fd < 0) - return -1; - - if (fstat (fd, &file_stat) < 0) { - close (fd); - return -1; - } - *size = file_stat.st_size; - return fd; -} - /* * return the path from the directory containing 'cache' to 'file' */ static const FcChar8 * -FcFileBaseName (const char *cache, const FcChar8 *file) +file_base_name (const char *cache, const FcChar8 *file) { const FcChar8 *cache_slash; int cache_len = strlen (cache); @@ -201,8 +183,8 @@ FcFileBaseName (const char *cache, const FcChar8 *file) return file; } -FcBool -FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name) +static FcBool +cache_print_set (FcFontSet *set, FcStrSet *dirs, char *base_name, FcBool verbose) { FcPattern *font; FcChar8 *name, *dir; @@ -210,6 +192,7 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name) int ret; int n; int id; + int ndir = 0; FcStrList *list; list = FcStrListCreate (dirs); @@ -218,19 +201,20 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name) while ((dir = FcStrListNext (list))) { - base = FcFileBaseName (base_name, dir); - if (!FcCacheWriteStringOld (stdout, base)) + base = file_base_name (base_name, dir); + if (!write_string (stdout, base)) goto bail3; if (PUTC (' ', stdout) == EOF) goto bail3; - if (!FcCacheWriteInt (stdout, 0)) + if (!write_int (stdout, 0)) goto bail3; if (PUTC (' ', stdout) == EOF) goto bail3; - if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR)) + if (!write_string (stdout, FC_FONT_FILE_DIR)) goto bail3; if (PUTC ('\n', stdout) == EOF) goto bail3; + ndir++; } for (n = 0; n < set->nfont; n++) @@ -241,27 +225,29 @@ FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name) if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch) goto bail3; - base = FcFileBaseName (base_name, file); + base = file_base_name (base_name, file); if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch) goto bail3; - if (!FcCacheWriteStringOld (stdout, base)) + if (!write_string (stdout, base)) goto bail3; if (PUTC (' ', stdout) == EOF) goto bail3; - if (!FcCacheWriteInt (stdout, id)) + if (!write_int (stdout, id)) goto bail3; if (PUTC (' ', stdout) == EOF) goto bail3; name = FcNameUnparse (font); if (!name) goto bail3; - ret = FcCacheWriteStringOld (stdout, name); + ret = write_string (stdout, name); FcStrFree (name); if (!ret) goto bail3; if (PUTC ('\n', stdout) == EOF) goto bail3; } + if (verbose && !set->nfont && !ndir) + printf ("\n"); FcStrListDone (list); @@ -273,28 +259,6 @@ 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 (FcCacheLoad (fd, file_stat.st_size, &cache)) { - close (fd); - return cache; - } - close (fd); - return NULL; -} - int main (int argc, char **argv) { @@ -302,16 +266,21 @@ main (int argc, char **argv) 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; #if HAVE_GETOPT_LONG - while ((c = getopt_long (argc, argv, "Vv?", longopts, NULL)) != -1) + while ((c = getopt_long (argc, argv, "Vvr?", longopts, NULL)) != -1) #else - while ((c = getopt (argc, argv, "Vv?")) != -1) + while ((c = getopt (argc, argv, "Vvr?")) != -1) #endif { switch (c) { @@ -322,6 +291,9 @@ main (int argc, char **argv) case 'v': verbose++; break; + case 'r': + recurse++; + break; default: usage (argv[0]); } @@ -339,22 +311,63 @@ main (int argc, char **argv) } FcConfigSetCurrent (config); - if (i >= argc) - usage (argv[0]); + args = FcStrSetCreate (); + if (!args) + { + fprintf (stderr, "%s: malloc failure\n", argv[0]); + return 1; + } + if (i < argc) + { + for (; i < argc; i++) + { + if (!FcStrSetAddFilename (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 + { + 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; + } - for (; i < argc; i++) + while ((arg = FcStrListNext (arglist))) { - int j; - off_t size; - intptr_t *cache_dirs; + int j; + off_t size; + intptr_t *cache_dirs; + FcChar8 *cache_file = NULL; + struct stat file_stat; - if (FcFileIsDir ((const FcChar8 *)argv[i])) - cache = FcDirCacheMap ((const FcChar8 *) argv[i], config); + if (FcFileIsDir (arg)) + cache = FcDirCacheLoad (arg, config, &cache_file); else - cache = FcCacheFileMap (argv[i]); + cache = FcDirCacheLoadFile (arg, &file_stat); if (!cache) { - perror (argv[i]); + perror ((char *) arg); ret++; continue; } @@ -363,17 +376,31 @@ main (int argc, char **argv) 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) - printf ("Name: %s\nDirectory: %s\n", argv[i], FcCacheDir(cache)); - FcCachePrintSet (fs, dirs, FcCacheDir (cache)); + { + if (!first) + printf ("\n"); + printf ("Directory: %s\nCache: %s\n--------\n", + FcCacheDir(cache), cache_file ? cache_file : arg); + first = FcFalse; + } + cache_print_set (fs, dirs, FcCacheDir (cache), verbose); FcStrSetDestroy (dirs); - FcDirCacheUnmap (cache); + FcDirCacheUnload (cache); + if (cache_file) + FcStrFree (cache_file); } return 0;