2 * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
4 * Copyright © 2002 Keith Packard
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
29 #define HAVE_GETOPT_LONG 1
34 #include "../src/fccache.c"
35 #include "../fc-arch/fcarch.h"
39 #include <sys/types.h>
46 #ifndef HAVE_GETOPT_LONG
47 #define HAVE_GETOPT_LONG 0
54 const struct option longopts[] = {
55 {"version", 0, 0, 'V'},
56 {"verbose", 0, 0, 'v'},
57 {"recurse", 0, 0, 'r'},
64 extern int optind, opterr, optopt;
69 * POSIX has broken stdio so that getc must do thread-safe locking,
70 * this is a serious performance problem for applications doing large
71 * amounts of IO with getc (as is done here). If available, use
72 * the getc_unlocked varient instead.
75 #if defined(getc_unlocked) || defined(_IO_getc_unlocked)
76 #define GETC(f) getc_unlocked(f)
77 #define PUTC(c,f) putc_unlocked(c,f)
79 #define GETC(f) getc(f)
80 #define PUTC(c,f) putc(c,f)
84 write_chars (FILE *f, const FcChar8 *chars)
87 while ((c = *chars++))
92 if (PUTC ('\\', f) == EOF)
96 if (PUTC (c, f) == EOF)
104 write_ulong (FILE *f, unsigned long t)
107 unsigned long temp, digit;
120 if (PUTC ((char) digit + '0', f) == EOF)
122 temp = temp - pow * digit;
129 write_int (FILE *f, int i)
131 return write_ulong (f, (unsigned long) i);
135 write_string (FILE *f, const FcChar8 *string)
138 if (PUTC ('"', f) == EOF)
140 if (!write_chars (f, string))
142 if (PUTC ('"', f) == EOF)
148 usage (char *program)
151 fprintf (stderr, "usage: %s [-V?] [--version] [--help] {*-%s.cache-2|directory}...\n",
152 program, FC_ARCHITECTURE);
154 fprintf (stderr, "usage: %s [-fsvV?] {*-%s.cache-2|directory}...\n",
155 program, FC_ARCHITECTURE);
157 fprintf (stderr, "Reads font information cache from:\n");
158 fprintf (stderr, " 1) specified fontconfig cache file\n");
159 fprintf (stderr, " 2) related to a particular font directory\n");
160 fprintf (stderr, "\n");
162 fprintf (stderr, " -V, --version display font config version and exit\n");
163 fprintf (stderr, " -?, --help display this help and exit\n");
165 fprintf (stderr, " -V (version) display font config version and exit\n");
166 fprintf (stderr, " -? (help) display this help and exit\n");
172 * return the path from the directory containing 'cache' to 'file'
175 static const FcChar8 *
176 file_base_name (const char *cache, const FcChar8 *file)
178 const FcChar8 *cache_slash;
179 int cache_len = strlen (cache);
181 if (!strncmp (cache, file, cache_len) && file[cache_len] == '/')
182 return file + cache_len + 1;
187 cache_print_set (FcFontSet *set, FcStrSet *dirs, char *base_name, FcBool verbose)
191 const FcChar8 *file, *base;
198 list = FcStrListCreate (dirs);
202 while ((dir = FcStrListNext (list)))
204 base = file_base_name (base_name, dir);
205 if (!write_string (stdout, base))
207 if (PUTC (' ', stdout) == EOF)
209 if (!write_int (stdout, 0))
211 if (PUTC (' ', stdout) == EOF)
213 if (!write_string (stdout, FC_FONT_FILE_DIR))
215 if (PUTC ('\n', stdout) == EOF)
220 for (n = 0; n < set->nfont; n++)
222 FcPattern **fonts = FcFontSetFonts (set);
223 FcPattern *encoded_font = fonts[n];
224 FcPattern *font = FcEncodedOffsetToPtr (set, encoded_font, FcPattern);
226 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
228 base = file_base_name (base_name, file);
229 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
231 if (!write_string (stdout, base))
233 if (PUTC (' ', stdout) == EOF)
235 if (!write_int (stdout, id))
237 if (PUTC (' ', stdout) == EOF)
239 name = FcNameUnparse (font);
242 ret = write_string (stdout, name);
246 if (PUTC ('\n', stdout) == EOF)
249 if (verbose && !set->nfont && !ndir)
250 printf ("<empty>\n");
252 FcStrListDone (list);
257 FcStrListDone (list);
263 main (int argc, char **argv)
269 FcStrSet *args = NULL;
276 FcBool first = FcTrue;
277 #if HAVE_GETOPT_LONG || HAVE_GETOPT
281 while ((c = getopt_long (argc, argv, "Vvr?", longopts, NULL)) != -1)
283 while ((c = getopt (argc, argv, "Vvr?")) != -1)
288 fprintf (stderr, "fontconfig version %d.%d.%d\n",
289 FC_MAJOR, FC_MINOR, FC_REVISION);
306 config = FcInitLoadConfig ();
309 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
312 FcConfigSetCurrent (config);
314 args = FcStrSetCreate ();
317 fprintf (stderr, "%s: malloc failure\n", argv[0]);
322 for (; i < argc; i++)
324 if (!FcStrSetAddFilename (args, argv[i]))
326 fprintf (stderr, "%s: malloc failure\n", argv[0]);
330 arglist = FcStrListCreate (args);
333 fprintf (stderr, "%s: malloc failure\n", argv[0]);
340 arglist = FcConfigGetFontDirs (config);
341 while ((arg = FcStrListNext (arglist)))
342 if (!FcStrSetAdd (args, arg))
344 fprintf (stderr, "%s: malloc failure\n", argv[0]);
347 FcStrListDone (arglist);
349 arglist = FcStrListCreate (args);
352 fprintf (stderr, "%s: malloc failure\n", argv[0]);
356 while ((arg = FcStrListNext (arglist)))
360 intptr_t *cache_dirs;
361 FcChar8 *cache_file = NULL;
362 struct stat file_stat;
364 if (FcFileIsDir (arg))
365 cache = FcDirCacheLoad (arg, config, &cache_file);
367 cache = FcDirCacheLoadFile (arg, &file_stat);
370 perror ((char *) arg);
375 dirs = FcStrSetCreate ();
376 fs = FcCacheSet (cache);
377 cache_dirs = FcCacheDirs (cache);
378 for (j = 0; j < cache->dirs_count; j++)
380 FcStrSetAdd (dirs, FcOffsetToPtr (cache_dirs,
384 FcStrSetAdd (args, FcOffsetToPtr (cache_dirs,
393 printf ("Directory: %s\nCache: %s\n--------\n",
394 FcCacheDir(cache), cache_file ? cache_file : arg);
397 cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
399 FcStrSetDestroy (dirs);
401 FcDirCacheUnload (cache);
403 FcStrFree (cache_file);