2005-09-23 07:59:19 +02:00
|
|
|
/*
|
|
|
|
* $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
|
|
|
|
*
|
|
|
|
* Copyright © 2002 Keith Packard
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* 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
|
|
|
|
* advertising or publicity pertaining to distribution of the software without
|
|
|
|
* specific, written prior permission. Keith Packard makes 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,
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
|
|
* EVENT SHALL KEITH PACKARD 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
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#else
|
|
|
|
#ifdef linux
|
|
|
|
#define HAVE_GETOPT_LONG 1
|
|
|
|
#endif
|
|
|
|
#define HAVE_GETOPT 1
|
|
|
|
#endif
|
|
|
|
|
2006-08-31 18:42:49 +02:00
|
|
|
#include "../src/fccache.c"
|
2006-08-31 20:56:43 +02:00
|
|
|
#include "../fc-arch/fcarch.h"
|
2006-04-25 07:57:41 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2005-09-23 07:59:19 +02:00
|
|
|
#ifndef HAVE_GETOPT
|
|
|
|
#define HAVE_GETOPT 0
|
|
|
|
#endif
|
|
|
|
#ifndef HAVE_GETOPT_LONG
|
|
|
|
#define HAVE_GETOPT_LONG 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_GETOPT_LONG
|
|
|
|
#undef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <getopt.h>
|
|
|
|
const struct option longopts[] = {
|
|
|
|
{"version", 0, 0, 'V'},
|
2006-08-30 22:51:03 +02:00
|
|
|
{"verbose", 0, 0, 'v'},
|
2006-08-31 20:56:43 +02:00
|
|
|
{"recurse", 0, 0, 'r'},
|
2005-09-23 07:59:19 +02:00
|
|
|
{"help", 0, 0, '?'},
|
|
|
|
{NULL,0,0,0},
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
#if HAVE_GETOPT
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind, opterr, optopt;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* POSIX has broken stdio so that getc must do thread-safe locking,
|
|
|
|
* this is a serious performance problem for applications doing large
|
|
|
|
* amounts of IO with getc (as is done here). If available, use
|
|
|
|
* the getc_unlocked varient instead.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(getc_unlocked) || defined(_IO_getc_unlocked)
|
|
|
|
#define GETC(f) getc_unlocked(f)
|
|
|
|
#define PUTC(c,f) putc_unlocked(c,f)
|
|
|
|
#else
|
|
|
|
#define GETC(f) getc(f)
|
|
|
|
#define PUTC(c,f) putc(c,f)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
FcCacheWriteChars (FILE *f, const FcChar8 *chars)
|
|
|
|
{
|
|
|
|
FcChar8 c;
|
|
|
|
while ((c = *chars++))
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case '"':
|
|
|
|
case '\\':
|
|
|
|
if (PUTC ('\\', f) == EOF)
|
|
|
|
return FcFalse;
|
|
|
|
/* fall through */
|
|
|
|
default:
|
|
|
|
if (PUTC (c, f) == EOF)
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
FcCacheWriteUlong (FILE *f, unsigned long t)
|
|
|
|
{
|
|
|
|
int pow;
|
|
|
|
unsigned long temp, digit;
|
|
|
|
|
|
|
|
temp = t;
|
|
|
|
pow = 1;
|
|
|
|
while (temp >= 10)
|
|
|
|
{
|
|
|
|
temp /= 10;
|
|
|
|
pow *= 10;
|
|
|
|
}
|
|
|
|
temp = t;
|
|
|
|
while (pow)
|
|
|
|
{
|
|
|
|
digit = temp / pow;
|
|
|
|
if (PUTC ((char) digit + '0', f) == EOF)
|
|
|
|
return FcFalse;
|
|
|
|
temp = temp - pow * digit;
|
|
|
|
pow = pow / 10;
|
|
|
|
}
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
FcCacheWriteInt (FILE *f, int i)
|
|
|
|
{
|
|
|
|
return FcCacheWriteUlong (f, (unsigned long) i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (PUTC ('"', f) == EOF)
|
|
|
|
return FcFalse;
|
|
|
|
if (!FcCacheWriteChars (f, string))
|
|
|
|
return FcFalse;
|
|
|
|
if (PUTC ('"', f) == EOF)
|
|
|
|
return FcFalse;
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage (char *program)
|
|
|
|
{
|
|
|
|
#if HAVE_GETOPT_LONG
|
2006-08-31 20:56:43 +02:00
|
|
|
fprintf (stderr, "usage: %s [-V?] [--version] [--help] {*-%s.cache-2|directory}...\n",
|
|
|
|
program, FC_ARCHITECTURE);
|
2005-09-23 07:59:19 +02:00
|
|
|
#else
|
2006-08-31 20:56:43 +02:00
|
|
|
fprintf (stderr, "usage: %s [-fsvV?] {*-%s.cache-2|directory}...\n",
|
|
|
|
program, FC_ARCHITECTURE);
|
2005-09-23 07:59:19 +02:00
|
|
|
#endif
|
2006-08-31 20:56:43 +02:00
|
|
|
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");
|
2005-09-23 07:59:19 +02:00
|
|
|
fprintf (stderr, "\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");
|
|
|
|
#else
|
|
|
|
fprintf (stderr, " -V (version) display font config version and exit\n");
|
|
|
|
fprintf (stderr, " -? (help) display this help and exit\n");
|
|
|
|
#endif
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2006-08-30 22:51:03 +02:00
|
|
|
static int
|
|
|
|
FcCacheFileOpen (char *cache_file, off_t *size)
|
2005-09-23 07:59:19 +02:00
|
|
|
{
|
|
|
|
int fd;
|
2006-08-30 22:51:03 +02:00
|
|
|
struct stat file_stat;
|
2005-09-23 07:59:19 +02:00
|
|
|
|
2006-08-30 22:51:03 +02:00
|
|
|
fd = open(cache_file, O_RDONLY | O_BINARY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
2005-09-23 07:59:19 +02:00
|
|
|
|
2006-08-30 22:51:03 +02:00
|
|
|
if (fstat (fd, &file_stat) < 0) {
|
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*size = file_stat.st_size;
|
|
|
|
return fd;
|
2005-09-23 07:59:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return the path from the directory containing 'cache' to 'file'
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const FcChar8 *
|
|
|
|
FcFileBaseName (const char *cache, const FcChar8 *file)
|
|
|
|
{
|
|
|
|
const FcChar8 *cache_slash;
|
2006-08-30 22:51:03 +02:00
|
|
|
int cache_len = strlen (cache);
|
2005-09-23 07:59:19 +02:00
|
|
|
|
2006-08-30 22:51:03 +02:00
|
|
|
if (!strncmp (cache, file, cache_len) && file[cache_len] == '/')
|
|
|
|
return file + cache_len + 1;
|
2005-09-23 07:59:19 +02:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
FcBool
|
2006-08-31 20:56:43 +02:00
|
|
|
FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name, FcBool verbose)
|
2005-09-23 07:59:19 +02:00
|
|
|
{
|
|
|
|
FcPattern *font;
|
|
|
|
FcChar8 *name, *dir;
|
|
|
|
const FcChar8 *file, *base;
|
2005-11-23 18:01:27 +01:00
|
|
|
int ret;
|
2005-09-23 07:59:19 +02:00
|
|
|
int n;
|
|
|
|
int id;
|
2006-08-31 20:56:43 +02:00
|
|
|
int ndir = 0;
|
2005-09-23 07:59:19 +02:00
|
|
|
FcStrList *list;
|
|
|
|
|
|
|
|
list = FcStrListCreate (dirs);
|
|
|
|
if (!list)
|
|
|
|
goto bail2;
|
|
|
|
|
|
|
|
while ((dir = FcStrListNext (list)))
|
|
|
|
{
|
2006-01-05 16:12:22 +01:00
|
|
|
base = FcFileBaseName (base_name, dir);
|
2005-09-23 07:59:19 +02:00
|
|
|
if (!FcCacheWriteStringOld (stdout, base))
|
|
|
|
goto bail3;
|
|
|
|
if (PUTC (' ', stdout) == EOF)
|
|
|
|
goto bail3;
|
|
|
|
if (!FcCacheWriteInt (stdout, 0))
|
|
|
|
goto bail3;
|
|
|
|
if (PUTC (' ', stdout) == EOF)
|
|
|
|
goto bail3;
|
|
|
|
if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
|
|
|
|
goto bail3;
|
|
|
|
if (PUTC ('\n', stdout) == EOF)
|
|
|
|
goto bail3;
|
2006-08-31 20:56:43 +02:00
|
|
|
ndir++;
|
2005-09-23 07:59:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < set->nfont; n++)
|
|
|
|
{
|
2006-08-30 22:51:03 +02:00
|
|
|
FcPattern **fonts = FcFontSetFonts (set);
|
|
|
|
FcPattern *encoded_font = fonts[n];
|
|
|
|
FcPattern *font = FcEncodedOffsetToPtr (set, encoded_font, FcPattern);
|
|
|
|
|
2005-09-23 07:59:19 +02:00
|
|
|
if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
|
|
|
|
goto bail3;
|
2006-01-05 16:12:22 +01:00
|
|
|
base = FcFileBaseName (base_name, file);
|
2005-09-23 07:59:19 +02:00
|
|
|
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;
|
|
|
|
}
|
2006-08-31 20:56:43 +02:00
|
|
|
if (verbose && !set->nfont && !ndir)
|
|
|
|
printf ("<empty>\n");
|
2005-09-23 07:59:19 +02:00
|
|
|
|
|
|
|
FcStrListDone (list);
|
|
|
|
|
|
|
|
return FcTrue;
|
|
|
|
|
|
|
|
bail3:
|
|
|
|
FcStrListDone (list);
|
|
|
|
bail2:
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
|
2006-08-31 07:23:25 +02:00
|
|
|
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;
|
|
|
|
}
|
2006-08-31 20:56:43 +02:00
|
|
|
if (!FcDirCacheLoad (fd, file_stat.st_size, &cache)) {
|
2006-08-31 07:23:25 +02:00
|
|
|
close (fd);
|
2006-08-31 20:56:43 +02:00
|
|
|
return NULL;
|
2006-08-31 07:23:25 +02:00
|
|
|
}
|
|
|
|
close (fd);
|
2006-08-31 20:56:43 +02:00
|
|
|
return cache;
|
2006-08-31 07:23:25 +02:00
|
|
|
}
|
|
|
|
|
2005-09-23 07:59:19 +02:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
2006-08-30 22:51:03 +02:00
|
|
|
int ret = 0;
|
|
|
|
FcFontSet *fs;
|
|
|
|
FcStrSet *dirs;
|
2006-08-31 20:56:43 +02:00
|
|
|
FcStrSet *args = NULL;
|
|
|
|
FcStrList *arglist;
|
2006-08-30 22:51:03 +02:00
|
|
|
FcCache *cache;
|
|
|
|
FcConfig *config;
|
2006-08-31 20:56:43 +02:00
|
|
|
FcChar8 *arg;
|
2006-08-30 22:51:03 +02:00
|
|
|
int verbose = 0;
|
2006-08-31 20:56:43 +02:00
|
|
|
int recurse = 0;
|
|
|
|
FcBool first = FcTrue;
|
2005-09-23 07:59:19 +02:00
|
|
|
#if HAVE_GETOPT_LONG || HAVE_GETOPT
|
|
|
|
int c;
|
|
|
|
|
|
|
|
#if HAVE_GETOPT_LONG
|
2006-08-31 20:56:43 +02:00
|
|
|
while ((c = getopt_long (argc, argv, "Vvr?", longopts, NULL)) != -1)
|
2005-09-23 07:59:19 +02:00
|
|
|
#else
|
2006-08-31 20:56:43 +02:00
|
|
|
while ((c = getopt (argc, argv, "Vvr?")) != -1)
|
2005-09-23 07:59:19 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case 'V':
|
|
|
|
fprintf (stderr, "fontconfig version %d.%d.%d\n",
|
|
|
|
FC_MAJOR, FC_MINOR, FC_REVISION);
|
|
|
|
exit (0);
|
2006-08-30 22:51:03 +02:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
2006-08-31 20:56:43 +02:00
|
|
|
case 'r':
|
|
|
|
recurse++;
|
|
|
|
break;
|
2005-09-23 07:59:19 +02:00
|
|
|
default:
|
|
|
|
usage (argv[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = optind;
|
|
|
|
#else
|
|
|
|
i = 1;
|
|
|
|
#endif
|
|
|
|
|
2006-02-21 15:12:41 +01:00
|
|
|
config = FcInitLoadConfig ();
|
|
|
|
if (!config)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
FcConfigSetCurrent (config);
|
|
|
|
|
2006-08-31 20:56:43 +02:00
|
|
|
args = FcStrSetCreate ();
|
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2005-11-18 21:32:30 +01:00
|
|
|
|
2006-08-31 20:56:43 +02:00
|
|
|
while ((arg = FcStrListNext (arglist)))
|
2005-12-01 08:12:45 +01:00
|
|
|
{
|
2006-08-31 20:56:43 +02:00
|
|
|
int j;
|
|
|
|
off_t size;
|
|
|
|
intptr_t *cache_dirs;
|
|
|
|
FcChar8 *cache_file = NULL;
|
2006-08-30 22:51:03 +02:00
|
|
|
|
2006-08-31 20:56:43 +02:00
|
|
|
if (FcFileIsDir (arg))
|
|
|
|
cache = FcDirCacheMap (arg, config, &cache_file);
|
2006-08-30 22:51:03 +02:00
|
|
|
else
|
2006-08-31 20:56:43 +02:00
|
|
|
cache = FcCacheFileMap (arg);
|
2006-08-31 07:23:25 +02:00
|
|
|
if (!cache)
|
2006-08-30 22:51:03 +02:00
|
|
|
{
|
2006-08-31 20:56:43 +02:00
|
|
|
perror ((char *) arg);
|
2006-08-30 22:51:03 +02:00
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirs = FcStrSetCreate ();
|
|
|
|
fs = FcCacheSet (cache);
|
|
|
|
cache_dirs = FcCacheDirs (cache);
|
|
|
|
for (j = 0; j < cache->dirs_count; j++)
|
2006-08-31 20:56:43 +02:00
|
|
|
{
|
2006-08-30 22:51:03 +02:00
|
|
|
FcStrSetAdd (dirs, FcOffsetToPtr (cache_dirs,
|
|
|
|
cache_dirs[j],
|
|
|
|
FcChar8));
|
2006-08-31 20:56:43 +02:00
|
|
|
if (recurse)
|
|
|
|
FcStrSetAdd (args, FcOffsetToPtr (cache_dirs,
|
|
|
|
cache_dirs[j],
|
|
|
|
FcChar8));
|
|
|
|
}
|
2006-08-30 22:51:03 +02:00
|
|
|
|
|
|
|
if (verbose)
|
2006-08-31 20:56:43 +02:00
|
|
|
{
|
|
|
|
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);
|
2005-09-23 07:59:19 +02:00
|
|
|
|
2006-08-30 22:51:03 +02:00
|
|
|
FcStrSetDestroy (dirs);
|
|
|
|
|
|
|
|
FcDirCacheUnmap (cache);
|
2006-08-31 20:56:43 +02:00
|
|
|
if (cache_file)
|
|
|
|
FcStrFree (cache_file);
|
2006-08-30 22:51:03 +02:00
|
|
|
}
|
2005-09-23 07:59:19 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|