X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=fc-cat%2Ffc-cat.c;h=5ee947ec24938c0d1ba5326bfe5090c32dc3b2dc;hb=HEAD;hp=b988a29309382b58decc32e2dd5ddd400803fb19;hpb=c02886485b293179e8492cad9a34eb431dd4bfc9;p=fontconfig.git diff --git a/fc-cat/fc-cat.c b/fc-cat/fc-cat.c index b988a29..5ee947e 100644 --- a/fc-cat/fc-cat.c +++ b/fc-cat/fc-cat.c @@ -1,5 +1,5 @@ /* - * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $ + * fontconfig/fc-cat/fc-cat.c * * Copyright © 2002 Keith Packard * @@ -7,15 +7,15 @@ * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting - * documentation, and that the name of Keith Packard not be used in + * documentation, and that the name of the author(s) not be used in * advertising or publicity pertaining to distribution of the software without - * specific, written prior permission. Keith Packard makes no + * specific, written prior permission. The authors make no * representations about the suitability of this software for any purpose. It * is provided "as is" without express or implied warranty. * - * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO - * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR @@ -32,9 +32,10 @@ #endif #include -#include <../src/fccache.c> +#include "../src/fcarch.h" #include #include +#include #include #include #include @@ -54,7 +55,8 @@ const struct option longopts[] = { {"version", 0, 0, 'V'}, {"verbose", 0, 0, 'v'}, - {"help", 0, 0, '?'}, + {"recurse", 0, 0, 'r'}, + {"help", 0, 0, 'h'}, {NULL,0,0,0}, }; #else @@ -79,11 +81,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 +102,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 +127,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; @@ -147,43 +146,33 @@ FcCacheWriteStringOld (FILE *f, const FcChar8 *string) } static void -usage (char *program) +usage (char *program, int error) { + FILE *file = error ? stderr : stdout; #if HAVE_GETOPT_LONG - fprintf (stderr, "usage: %s [-V?] [--version] [--help] \n", - program); + fprintf (file, "usage: %s [-rv] [--recurse] [--verbose] [*-%s.cache-2|directory]...\n", + program, FC_ARCHITECTURE); + fprintf (file, " %s [-Vh] [--version] [--help]\n", program); #else - fprintf (stderr, "usage: %s [-fsvV?] \n", - program); + fprintf (file, "usage: %s [-rvVh] [*-%s.cache-2|directory]...\n", + program, FC_ARCHITECTURE); #endif - fprintf (stderr, "Reads font information caches in \n"); - fprintf (stderr, "\n"); + fprintf (file, "Reads font information cache from:\n"); + fprintf (file, " 1) specified fontconfig cache file\n"); + fprintf (file, " 2) related to a particular font directory\n"); + fprintf (file, "\n"); #if HAVE_GETOPT_LONG - fprintf (stderr, " -V, --version display font config version and exit\n"); - fprintf (stderr, " -?, --help display this help and exit\n"); + fprintf (file, " -r, --recurse recurse into subdirectories\n"); + fprintf (file, " -v, --verbose be verbose\n"); + fprintf (file, " -V, --version display font config version and exit\n"); + fprintf (file, " -h, --help display this help and exit\n"); #else - fprintf (stderr, " -V (version) display font config version and exit\n"); - fprintf (stderr, " -? (help) display this help and exit\n"); + fprintf (file, " -r (recurse) recurse into subdirectories\n"); + fprintf (file, " -v (verbose) be verbose\n"); + fprintf (file, " -V (version) display font config version and exit\n"); + fprintf (file, " -h (help) display this help and exit\n"); #endif - 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; + exit (error); } /* @@ -191,25 +180,24 @@ FcCacheFileOpen (char *cache_file, off_t *size) */ static const FcChar8 * -FcFileBaseName (const char *cache, const FcChar8 *file) +file_base_name (const FcChar8 *cache, const FcChar8 *file) { - const FcChar8 *cache_slash; - int cache_len = strlen (cache); + int cache_len = strlen ((char *) cache); - if (!strncmp (cache, file, cache_len) && file[cache_len] == '/') + if (!strncmp ((char *) cache, (char *) file, cache_len) && file[cache_len] == '/') return file + cache_len + 1; return file; } -FcBool -FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name) +#define FC_FONT_FILE_DIR ((FcChar8 *) ".dir") + +static FcBool +cache_print_set (FcFontSet *set, FcStrSet *dirs, const FcChar8 *base_name, FcBool verbose) { - FcPattern *font; - FcChar8 *name, *dir; - const FcChar8 *file, *base; - int ret; + FcChar8 *dir; + const FcChar8 *base; int n; - int id; + int ndir = 0; FcStrList *list; list = FcStrListCreate (dirs); @@ -218,55 +206,41 @@ 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++) { - FcPattern **fonts = FcFontSetFonts (set); - FcPattern *encoded_font = fonts[n]; - FcPattern *font = FcEncodedOffsetToPtr (set, encoded_font, FcPattern); + FcPattern *font = set->fonts[n]; + FcChar8 *s; - 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 (!FcCacheWriteStringOld (stdout, base)) - goto bail3; - if (PUTC (' ', stdout) == EOF) - goto bail3; - if (!FcCacheWriteInt (stdout, id)) - goto bail3; - if (PUTC (' ', stdout) == EOF) - goto bail3; - name = FcNameUnparse (font); - if (!name) - goto bail3; - ret = FcCacheWriteStringOld (stdout, name); - FcStrFree (name); - if (!ret) - goto bail3; - if (PUTC ('\n', stdout) == EOF) - goto bail3; + s = FcPatternFormat (font, "%{=fccat}\n"); + if (s) + { + printf ("%s", s); + free (s); + } } - + if (verbose && !set->nfont && !ndir) + printf ("\n"); + FcStrListDone (list); return FcTrue; - + bail3: FcStrListDone (list); bail2: @@ -280,16 +254,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, "Vvrh", longopts, NULL)) != -1) #else - while ((c = getopt (argc, argv, "Vv?")) != -1) + while ((c = getopt (argc, argv, "Vvrh")) != -1) #endif { switch (c) { @@ -300,8 +279,13 @@ main (int argc, char **argv) case 'v': verbose++; break; + case 'r': + recurse++; + break; + case 'h': + usage (argv[0], 0); default: - usage (argv[0]); + usage (argv[0], 1); } } i = optind; @@ -317,51 +301,92 @@ 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, (const FcChar8 *) 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 fd; - int j; - off_t size; - intptr_t *cache_dirs; + int j; + FcChar8 *cache_file = NULL; + struct stat file_stat; - if (FcFileIsDir ((const FcChar8 *)argv[i])) - fd = FcDirCacheOpen (config, (const FcChar8 *) argv[i], &size); + if (FcFileIsDir (arg)) + cache = FcDirCacheLoad (arg, config, &cache_file); else - fd = FcCacheFileOpen (argv[i], &size); - if (fd < 0) + cache = FcDirCacheLoadFile (arg, &file_stat); + if (!cache) { - perror (argv[i]); + perror ((char *) arg); ret++; continue; } - cache = FcDirCacheMap (fd, size); - close (fd); - if (!cache) + dirs = FcStrSetCreate (); + fs = FcCacheCopySet (cache); + for (j = 0; j < FcCacheNumSubdir (cache); j++) { - fprintf (stderr, "%s: cannot map cache\n", argv[i]); - ret++; - continue; + FcStrSetAdd (dirs, FcCacheSubdir (cache, j)); + if (recurse) + FcStrSetAdd (args, FcCacheSubdir (cache, j)); } - 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 (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); + FcFontSetDestroy (fs); + FcDirCacheUnload (cache); + if (cache_file) + FcStrFree (cache_file); } + FcFini (); return 0; }