Blame fc-cat/fc-cat.c

Packit 352660
/*
Packit 352660
 * fontconfig/fc-cat/fc-cat.c
Packit 352660
 *
Packit 352660
 * Copyright © 2002 Keith Packard
Packit 352660
 *
Packit 352660
 * Permission to use, copy, modify, distribute, and sell this software and its
Packit 352660
 * documentation for any purpose is hereby granted without fee, provided that
Packit 352660
 * the above copyright notice appear in all copies and that both that
Packit 352660
 * copyright notice and this permission notice appear in supporting
Packit 352660
 * documentation, and that the name of the author(s) not be used in
Packit 352660
 * advertising or publicity pertaining to distribution of the software without
Packit 352660
 * specific, written prior permission.  The authors make no
Packit 352660
 * representations about the suitability of this software for any purpose.  It
Packit 352660
 * is provided "as is" without express or implied warranty.
Packit 352660
 *
Packit 352660
 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
Packit 352660
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
Packit 352660
 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Packit 352660
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
Packit 352660
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
Packit 352660
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
Packit 352660
 * PERFORMANCE OF THIS SOFTWARE.
Packit 352660
 */
Packit 352660
Packit 352660
#ifdef HAVE_CONFIG_H
Packit 352660
#include <config.h>
Packit 352660
#else
Packit 352660
#ifdef linux
Packit 352660
#define HAVE_GETOPT_LONG 1
Packit 352660
#endif
Packit 352660
#define HAVE_GETOPT 1
Packit 352660
#endif
Packit 352660
Packit 352660
#include <fontconfig/fontconfig.h>
Packit 352660
#include "../src/fcarch.h"
Packit 352660
#include <stdio.h>
Packit 352660
#include <stdlib.h>
Packit 352660
#include <string.h>
Packit 352660
#include <unistd.h>
Packit 352660
#include <sys/types.h>
Packit 352660
#include <sys/stat.h>
Packit 352660
#include <errno.h>
Packit 352660
#include <locale.h>
Packit 352660
Packit 352660
#ifdef ENABLE_NLS
Packit 352660
#include <libintl.h>
Packit 352660
#define _(x)		(dgettext(GETTEXT_PACKAGE, x))
Packit 352660
#else
Packit 352660
#define dgettext(d, s)	(s)
Packit 352660
#define _(x)		(x)
Packit 352660
#endif
Packit 352660
Packit 352660
#ifndef HAVE_GETOPT
Packit 352660
#define HAVE_GETOPT 0
Packit 352660
#endif
Packit 352660
#ifndef HAVE_GETOPT_LONG
Packit 352660
#define HAVE_GETOPT_LONG 0
Packit 352660
#endif
Packit 352660
Packit 352660
#if HAVE_GETOPT_LONG
Packit 352660
#undef  _GNU_SOURCE
Packit 352660
#define _GNU_SOURCE
Packit 352660
#include <getopt.h>
Packit 352660
const struct option longopts[] = {
Packit 352660
    {"version", 0, 0, 'V'},
Packit 352660
    {"verbose", 0, 0, 'v'},
Packit 352660
    {"recurse", 0, 0, 'r'},
Packit 352660
    {"help", 0, 0, 'h'},
Packit 352660
    {NULL,0,0,0},
Packit 352660
};
Packit 352660
#else
Packit 352660
#if HAVE_GETOPT
Packit 352660
extern char *optarg;
Packit 352660
extern int optind, opterr, optopt;
Packit 352660
#endif
Packit 352660
#endif
Packit 352660
Packit 352660
/*
Packit 352660
 * POSIX has broken stdio so that putc must do thread-safe locking,
Packit 352660
 * this is a serious performance problem for applications doing large
Packit 352660
 * amounts of IO with putc (as is done here).  If available, use
Packit 352660
 * the putc_unlocked varient instead.
Packit 352660
 */
Packit 352660
 
Packit 352660
#if defined(putc_unlocked) || defined(_IO_putc_unlocked)
Packit 352660
#define PUTC(c,f) putc_unlocked(c,f)
Packit 352660
#else
Packit 352660
#define PUTC(c,f) putc(c,f)
Packit 352660
#endif
Packit 352660
Packit 352660
static FcBool
Packit 352660
write_chars (FILE *f, const FcChar8 *chars)
Packit 352660
{
Packit 352660
    FcChar8    c;
Packit 352660
    while ((c = *chars++))
Packit 352660
    {
Packit 352660
	switch (c) {
Packit 352660
	case '"':
Packit 352660
	case '\\':
Packit 352660
	    if (PUTC ('\\', f) == EOF)
Packit 352660
		return FcFalse;
Packit 352660
	    /* fall through */
Packit 352660
	default:
Packit 352660
	    if (PUTC (c, f) == EOF)
Packit 352660
		return FcFalse;
Packit 352660
	}
Packit 352660
    }
Packit 352660
    return FcTrue;
Packit 352660
}
Packit 352660
Packit 352660
static FcBool
Packit 352660
write_ulong (FILE *f, unsigned long t)
Packit 352660
{
Packit 352660
    int	    pow;
Packit 352660
    unsigned long   temp, digit;
Packit 352660
Packit 352660
    temp = t;
Packit 352660
    pow = 1;
Packit 352660
    while (temp >= 10)
Packit 352660
    {
Packit 352660
	temp /= 10;
Packit 352660
	pow *= 10;
Packit 352660
    }
Packit 352660
    temp = t;
Packit 352660
    while (pow)
Packit 352660
    {
Packit 352660
	digit = temp / pow;
Packit 352660
	if (PUTC ((char) digit + '0', f) == EOF)
Packit 352660
	    return FcFalse;
Packit 352660
	temp = temp - pow * digit;
Packit 352660
	pow = pow / 10;
Packit 352660
    }
Packit 352660
    return FcTrue;
Packit 352660
}
Packit 352660
Packit 352660
static FcBool
Packit 352660
write_int (FILE *f, int i)
Packit 352660
{
Packit 352660
    return write_ulong (f, (unsigned long) i);
Packit 352660
}
Packit 352660
Packit 352660
static FcBool
Packit 352660
write_string (FILE *f, const FcChar8 *string)
Packit 352660
{
Packit 352660
Packit 352660
    if (PUTC ('"', f) == EOF)
Packit 352660
	return FcFalse;
Packit 352660
    if (!write_chars (f, string))
Packit 352660
	return FcFalse;
Packit 352660
    if (PUTC ('"', f) == EOF)
Packit 352660
	return FcFalse;
Packit 352660
    return FcTrue;
Packit 352660
}
Packit 352660
Packit 352660
static void
Packit 352660
usage (char *program, int error)
Packit 352660
{
Packit 352660
    FILE *file = error ? stderr : stdout;
Packit 352660
#if HAVE_GETOPT_LONG
Packit 352660
    fprintf (file, _("usage: %s [-rv] [--recurse] [--verbose] [*-%s" FC_CACHE_SUFFIX "|directory]...\n"),
Packit 352660
	     program, FC_ARCHITECTURE);
Packit 352660
    fprintf (file, "       %s [-Vh] [--version] [--help]\n", program);
Packit 352660
#else
Packit 352660
    fprintf (file, _("usage: %s [-rvVh] [*-%s" FC_CACHE_SUFFIX "|directory]...\n"),
Packit 352660
	     program, FC_ARCHITECTURE);
Packit 352660
#endif
Packit 352660
    fprintf (file, _("Reads font information cache from:\n"));
Packit 352660
    fprintf (file, _(" 1) specified fontconfig cache file\n"));
Packit 352660
    fprintf (file, _(" 2) related to a particular font directory\n"));
Packit 352660
    fprintf (file, "\n");
Packit 352660
#if HAVE_GETOPT_LONG
Packit 352660
    fprintf (file, _("  -r, --recurse        recurse into subdirectories\n"));
Packit 352660
    fprintf (file, _("  -v, --verbose        be verbose\n"));
Packit 352660
    fprintf (file, _("  -V, --version        display font config version and exit\n"));
Packit 352660
    fprintf (file, _("  -h, --help           display this help and exit\n"));
Packit 352660
#else
Packit 352660
    fprintf (file, _("  -r         (recurse) recurse into subdirectories\n"));
Packit 352660
    fprintf (file, _("  -v         (verbose) be verbose\n"));
Packit 352660
    fprintf (file, _("  -V         (version) display font config version and exit\n"));
Packit 352660
    fprintf (file, _("  -h         (help)    display this help and exit\n"));
Packit 352660
#endif
Packit 352660
    exit (error);
Packit 352660
}
Packit 352660
Packit 352660
/*
Packit 352660
 * return the path from the directory containing 'cache' to 'file'
Packit 352660
 */
Packit 352660
Packit 352660
static const FcChar8 *
Packit 352660
file_base_name (const FcChar8 *cache, const FcChar8 *file)
Packit 352660
{
Packit 352660
    int		    cache_len = strlen ((char *) cache);
Packit 352660
Packit 352660
    if (!strncmp ((char *) cache, (char *) file, cache_len) && file[cache_len] == '/')
Packit 352660
	return file + cache_len + 1;
Packit 352660
    return file;
Packit 352660
}
Packit 352660
Packit 352660
#define FC_FONT_FILE_DIR	((FcChar8 *) ".dir")
Packit 352660
Packit 352660
static FcBool
Packit 352660
cache_print_set (FcFontSet *set, FcStrSet *dirs, const FcChar8 *base_name, FcBool verbose)
Packit 352660
{
Packit 352660
    FcChar8	    *dir;
Packit 352660
    const FcChar8   *base;
Packit 352660
    int		    n;
Packit 352660
    int		    ndir = 0;
Packit 352660
    FcStrList	    *list;
Packit 352660
Packit 352660
    list = FcStrListCreate (dirs);
Packit 352660
    if (!list)
Packit 352660
	goto bail2;
Packit 352660
    
Packit 352660
    while ((dir = FcStrListNext (list)))
Packit 352660
    {
Packit 352660
	base = file_base_name (base_name, dir);
Packit 352660
	if (!write_string (stdout, base))
Packit 352660
	    goto bail3;
Packit 352660
	if (PUTC (' ', stdout) == EOF)
Packit 352660
	    goto bail3;
Packit 352660
	if (!write_int (stdout, 0))
Packit 352660
	    goto bail3;
Packit 352660
        if (PUTC (' ', stdout) == EOF)
Packit 352660
	    goto bail3;
Packit 352660
	if (!write_string (stdout, FC_FONT_FILE_DIR))
Packit 352660
	    goto bail3;
Packit 352660
	if (PUTC ('\n', stdout) == EOF)
Packit 352660
	    goto bail3;
Packit 352660
	ndir++;
Packit 352660
    }
Packit 352660
    
Packit 352660
    for (n = 0; n < set->nfont; n++)
Packit 352660
    {
Packit 352660
	FcPattern   *font = set->fonts[n];
Packit 352660
	FcChar8 *s;
Packit 352660
Packit 352660
	s = FcPatternFormat (font, (const FcChar8 *) "%{=fccat}\n");
Packit 352660
	if (s)
Packit 352660
	{
Packit 352660
	    printf ("%s", s);
Packit 352660
	    FcStrFree (s);
Packit 352660
	}
Packit 352660
    }
Packit 352660
    if (verbose && !set->nfont && !ndir)
Packit 352660
	printf ("<empty>\n");
Packit 352660
Packit 352660
    FcStrListDone (list);
Packit 352660
Packit 352660
    return FcTrue;
Packit 352660
Packit 352660
bail3:
Packit 352660
    FcStrListDone (list);
Packit 352660
bail2:
Packit 352660
    return FcFalse;
Packit 352660
}
Packit 352660
Packit 352660
int
Packit 352660
main (int argc, char **argv)
Packit 352660
{
Packit 352660
    int		i;
Packit 352660
    int		ret = 0;
Packit 352660
    FcFontSet	*fs;
Packit 352660
    FcStrSet    *dirs;
Packit 352660
    FcStrSet	*args = NULL;
Packit 352660
    FcStrList	*arglist;
Packit 352660
    FcCache	*cache;
Packit 352660
    FcConfig	*config;
Packit 352660
    FcChar8	*arg;
Packit 352660
    int		verbose = 0;
Packit 352660
    int		recurse = 0;
Packit 352660
    FcBool	first = FcTrue;
Packit 352660
#if HAVE_GETOPT_LONG || HAVE_GETOPT
Packit 352660
    int		c;
Packit 352660
Packit 352660
    setlocale (LC_ALL, "");
Packit 352660
#if HAVE_GETOPT_LONG
Packit 352660
    while ((c = getopt_long (argc, argv, "Vvrh", longopts, NULL)) != -1)
Packit 352660
#else
Packit 352660
    while ((c = getopt (argc, argv, "Vvrh")) != -1)
Packit 352660
#endif
Packit 352660
    {
Packit 352660
	switch (c) {
Packit 352660
	case 'V':
Packit 352660
	    fprintf (stderr, "fontconfig version %d.%d.%d\n", 
Packit 352660
		     FC_MAJOR, FC_MINOR, FC_REVISION);
Packit 352660
	    exit (0);
Packit 352660
	case 'v':
Packit 352660
	    verbose++;
Packit 352660
	    break;
Packit 352660
	case 'r':
Packit 352660
	    recurse++;
Packit 352660
	    break;
Packit 352660
	case 'h':
Packit 352660
	    usage (argv[0], 0);
Packit 352660
	default:
Packit 352660
	    usage (argv[0], 1);
Packit 352660
	}
Packit 352660
    }
Packit 352660
    i = optind;
Packit 352660
#else
Packit 352660
    i = 1;
Packit 352660
#endif
Packit 352660
Packit 352660
    config = FcInitLoadConfig ();
Packit 352660
    if (!config)
Packit 352660
    {
Packit 352660
	fprintf (stderr, _("%s: Can't initialize font config library\n"), argv[0]);
Packit 352660
	return 1;
Packit 352660
    }
Packit 352660
    FcConfigSetCurrent (config);
Packit 352660
    FcConfigDestroy (config);
Packit 352660
    
Packit 352660
    args = FcStrSetCreate ();
Packit 352660
    if (!args)
Packit 352660
    {
Packit 352660
	fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
Packit 352660
	return 1;
Packit 352660
    }
Packit 352660
    if (i < argc)
Packit 352660
    {
Packit 352660
	for (; i < argc; i++)
Packit 352660
	{
Packit 352660
	    if (!FcStrSetAddFilename (args, (const FcChar8 *) argv[i]))
Packit 352660
	    {
Packit 352660
		fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
Packit 352660
		return 1;
Packit 352660
	    }
Packit 352660
	}
Packit 352660
    }
Packit 352660
    else
Packit 352660
    {
Packit 352660
	recurse++;
Packit 352660
	arglist = FcConfigGetFontDirs (config);
Packit 352660
	while ((arg = FcStrListNext (arglist)))
Packit 352660
	    if (!FcStrSetAdd (args, arg))
Packit 352660
	    {
Packit 352660
		fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
Packit 352660
		return 1;
Packit 352660
	    }
Packit 352660
	FcStrListDone (arglist);
Packit 352660
    }
Packit 352660
    arglist = FcStrListCreate (args);
Packit 352660
    if (!arglist)
Packit 352660
    {
Packit 352660
	fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
Packit 352660
	return 1;
Packit 352660
    }
Packit 352660
    FcStrSetDestroy (args);
Packit 352660
Packit 352660
    while ((arg = FcStrListNext (arglist)))
Packit 352660
    {
Packit 352660
	int	    j;
Packit 352660
	FcChar8	    *cache_file = NULL;
Packit 352660
	struct stat file_stat;
Packit 352660
Packit 352660
	/* reset errno */
Packit 352660
	errno = 0;
Packit 352660
	if (FcFileIsDir (arg))
Packit 352660
	    cache = FcDirCacheLoad (arg, config, &cache_file);
Packit 352660
	else
Packit 352660
	    cache = FcDirCacheLoadFile (arg, &file_stat);
Packit 352660
	if (!cache)
Packit 352660
	{
Packit 352660
	    if (errno != 0)
Packit 352660
		perror ((char *) arg);
Packit 352660
	    else
Packit 352660
		fprintf (stderr, "%s: Unable to load the cache: %s\n", argv[0], arg);
Packit 352660
	    ret++;
Packit 352660
	    continue;
Packit 352660
	}
Packit 352660
	
Packit 352660
	dirs = FcStrSetCreate ();
Packit 352660
	fs = FcCacheCopySet (cache);
Packit 352660
	for (j = 0; j < FcCacheNumSubdir (cache); j++) 
Packit 352660
	{
Packit 352660
	    FcStrSetAdd (dirs, FcCacheSubdir (cache, j));
Packit 352660
	    if (recurse)
Packit 352660
		FcStrSetAdd (args, FcCacheSubdir (cache, j));
Packit 352660
	}
Packit 352660
Packit 352660
	if (verbose)
Packit 352660
	{
Packit 352660
	    if (!first)
Packit 352660
		printf ("\n");
Packit 352660
	    printf (_("Directory: %s\nCache: %s\n--------\n"),
Packit 352660
		    FcCacheDir(cache), cache_file ? cache_file : arg);
Packit 352660
	    first = FcFalse;
Packit 352660
	}
Packit 352660
        cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
Packit 352660
Packit 352660
	FcStrSetDestroy (dirs);
Packit 352660
Packit 352660
	FcFontSetDestroy (fs);
Packit 352660
	FcDirCacheUnload (cache);
Packit 352660
	if (cache_file)
Packit 352660
	    FcStrFree (cache_file);
Packit 352660
    }
Packit 352660
    FcStrListDone (arglist);
Packit 352660
Packit 352660
    FcFini ();
Packit 352660
    return 0;
Packit 352660
}