fc-cat.c 8.83 KB
Newer Older
1
/*
2
 * fontconfig/fc-cat/fc-cat.c
3 4 5 6 7 8 9
 *
 * 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
10
 * documentation, and that the name of the author(s) not be used in
11
 * advertising or publicity pertaining to distribution of the software without
12
 * specific, written prior permission.  The authors make no
13 14 15
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
16
 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18
 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * 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

34
#include <fontconfig/fontconfig.h>
35
#include "../src/fcarch.h"
36 37
#include <stdio.h>
#include <stdlib.h>
38
#include <string.h>
39 40 41 42 43
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

44 45 46 47 48 49 50 51 52 53 54 55 56
#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'},
57
    {"verbose", 0, 0, 'v'},
58
    {"recurse", 0, 0, 'r'},
59
    {"help", 0, 0, 'h'},
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    {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
85
write_chars (FILE *f, const FcChar8 *chars)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
    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
105
write_ulong (FILE *f, unsigned long t)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
{
    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
130
write_int (FILE *f, int i)
131
{
132
    return write_ulong (f, (unsigned long) i);
133 134 135
}

static FcBool
136
write_string (FILE *f, const FcChar8 *string)
137 138 139 140
{

    if (PUTC ('"', f) == EOF)
	return FcFalse;
141
    if (!write_chars (f, string))
142 143 144 145 146 147 148
	return FcFalse;
    if (PUTC ('"', f) == EOF)
	return FcFalse;
    return FcTrue;
}

static void
149
usage (char *program, int error)
150
{
151
    FILE *file = error ? stderr : stdout;
152
#if HAVE_GETOPT_LONG
153
    fprintf (file, "usage: %s [-rv] [--recurse] [--verbose] [*-%s.cache-2|directory]...\n",
154
	     program, FC_ARCHITECTURE);
155
    fprintf (file, "       %s [-Vh] [--version] [--help]\n", program);
156
#else
157
    fprintf (file, "usage: %s [-rvVh] [*-%s.cache-2|directory]...\n",
158
	     program, FC_ARCHITECTURE);
159
#endif
160 161 162 163
    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");
164
#if HAVE_GETOPT_LONG
165 166 167 168
    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");
169
#else
170 171 172 173
    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");
174
#endif
175
    exit (error);
176 177 178 179 180 181 182
}

/*
 * return the path from the directory containing 'cache' to 'file'
 */

static const FcChar8 *
183
file_base_name (const FcChar8 *cache, const FcChar8 *file)
184
{
185
    int		    cache_len = strlen ((char *) cache);
186

187
    if (!strncmp ((char *) cache, (char *) file, cache_len) && file[cache_len] == '/')
188
	return file + cache_len + 1;
189 190 191
    return file;
}

192 193
#define FC_FONT_FILE_DIR	((FcChar8 *) ".dir")

194
static FcBool
195
cache_print_set (FcFontSet *set, FcStrSet *dirs, const FcChar8 *base_name, FcBool verbose)
196
{
197 198
    FcChar8	    *dir;
    const FcChar8   *base;
199
    int		    n;
200
    int		    ndir = 0;
201 202 203 204 205 206 207 208
    FcStrList	    *list;

    list = FcStrListCreate (dirs);
    if (!list)
	goto bail2;
    
    while ((dir = FcStrListNext (list)))
    {
209 210
	base = file_base_name (base_name, dir);
	if (!write_string (stdout, base))
211 212 213
	    goto bail3;
	if (PUTC (' ', stdout) == EOF)
	    goto bail3;
214
	if (!write_int (stdout, 0))
215 216 217
	    goto bail3;
        if (PUTC (' ', stdout) == EOF)
	    goto bail3;
218
	if (!write_string (stdout, FC_FONT_FILE_DIR))
219 220 221
	    goto bail3;
	if (PUTC ('\n', stdout) == EOF)
	    goto bail3;
222
	ndir++;
223 224 225 226
    }
    
    for (n = 0; n < set->nfont; n++)
    {
227
	FcPattern   *font = set->fonts[n];
228
	FcChar8 *s;
229

Mike Frysinger's avatar
Mike Frysinger committed
230
	s = FcPatternFormat (font, (const FcChar8 *) "%{=fccat}\n");
231 232 233 234 235
	if (s)
	{
	    printf ("%s", s);
	    free (s);
	}
236
    }
237 238
    if (verbose && !set->nfont && !ndir)
	printf ("<empty>\n");
239

240 241 242
    FcStrListDone (list);

    return FcTrue;
243

244 245 246 247 248 249 250 251 252 253
bail3:
    FcStrListDone (list);
bail2:
    return FcFalse;
}

int
main (int argc, char **argv)
{
    int		i;
254 255 256
    int		ret = 0;
    FcFontSet	*fs;
    FcStrSet    *dirs;
257 258
    FcStrSet	*args = NULL;
    FcStrList	*arglist;
259 260
    FcCache	*cache;
    FcConfig	*config;
261
    FcChar8	*arg;
262
    int		verbose = 0;
263 264
    int		recurse = 0;
    FcBool	first = FcTrue;
265 266 267 268
#if HAVE_GETOPT_LONG || HAVE_GETOPT
    int		c;

#if HAVE_GETOPT_LONG
269
    while ((c = getopt_long (argc, argv, "Vvrh", longopts, NULL)) != -1)
270
#else
271
    while ((c = getopt (argc, argv, "Vvrh")) != -1)
272 273 274 275 276 277 278
#endif
    {
	switch (c) {
	case 'V':
	    fprintf (stderr, "fontconfig version %d.%d.%d\n", 
		     FC_MAJOR, FC_MINOR, FC_REVISION);
	    exit (0);
279 280 281
	case 'v':
	    verbose++;
	    break;
282 283 284
	case 'r':
	    recurse++;
	    break;
285 286
	case 'h':
	    usage (argv[0], 0);
287
	default:
288
	    usage (argv[0], 1);
289 290 291 292 293 294 295
	}
    }
    i = optind;
#else
    i = 1;
#endif

296 297 298 299 300 301 302 303
    config = FcInitLoadConfig ();
    if (!config)
    {
	fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
	return 1;
    }
    FcConfigSetCurrent (config);
    
304 305 306 307 308 309 310 311 312 313
    args = FcStrSetCreate ();
    if (!args)
    {
	fprintf (stderr, "%s: malloc failure\n", argv[0]);
	return 1;
    }
    if (i < argc)
    {
	for (; i < argc; i++)
	{
314
	    if (!FcStrSetAddFilename (args, (const FcChar8 *) argv[i]))
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	    {
		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;
    }
345

346
    while ((arg = FcStrListNext (arglist)))
347
    {
348 349
	int	    j;
	FcChar8	    *cache_file = NULL;
350
	struct stat file_stat;
351
	
352
	if (FcFileIsDir (arg))
353
	    cache = FcDirCacheLoad (arg, config, &cache_file);
354
	else
355
	    cache = FcDirCacheLoadFile (arg, &file_stat);
Keith Packard's avatar
Keith Packard committed
356
	if (!cache)
357
	{
358
	    perror ((char *) arg);
359 360 361 362 363
	    ret++;
	    continue;
	}
	
	dirs = FcStrSetCreate ();
364 365
	fs = FcCacheCopySet (cache);
	for (j = 0; j < FcCacheNumSubdir (cache); j++) 
366
	{
367
	    FcStrSetAdd (dirs, FcCacheSubdir (cache, j));
368
	    if (recurse)
369
		FcStrSetAdd (args, FcCacheSubdir (cache, j));
370
	}
371 372

	if (verbose)
373 374 375 376 377 378 379
	{
	    if (!first)
		printf ("\n");
	    printf ("Directory: %s\nCache: %s\n--------\n",
		    FcCacheDir(cache), cache_file ? cache_file : arg);
	    first = FcFalse;
	}
380
        cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
381

382 383
	FcStrSetDestroy (dirs);

384
	FcFontSetDestroy (fs);
385
	FcDirCacheUnload (cache);
386 387
	if (cache_file)
	    FcStrFree (cache_file);
388
    }
389

390
    FcFini ();
391 392
    return 0;
}