2 * fontconfig/fc-cat/fc-cat.c
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 <fontconfig/fontconfig.h>
35 #include "../fc-arch/fcarch.h"
40 #include <sys/types.h>
47 #ifndef HAVE_GETOPT_LONG
48 #define HAVE_GETOPT_LONG 0
55 const struct option longopts[] = {
56 {"version", 0, 0, 'V'},
57 {"verbose", 0, 0, 'v'},
58 {"recurse", 0, 0, 'r'},
65 extern int optind, opterr, optopt;
70 * POSIX has broken stdio so that getc must do thread-safe locking,
71 * this is a serious performance problem for applications doing large
72 * amounts of IO with getc (as is done here). If available, use
73 * the getc_unlocked varient instead.
76 #if defined(getc_unlocked) || defined(_IO_getc_unlocked)
77 #define GETC(f) getc_unlocked(f)
78 #define PUTC(c,f) putc_unlocked(c,f)
80 #define GETC(f) getc(f)
81 #define PUTC(c,f) putc(c,f)
85 write_chars (FILE *f, const FcChar8 *chars)
88 while ((c = *chars++))
93 if (PUTC ('\\', f) == EOF)
97 if (PUTC (c, f) == EOF)
105 write_ulong (FILE *f, unsigned long t)
108 unsigned long temp, digit;
121 if (PUTC ((char) digit + '0', f) == EOF)
123 temp = temp - pow * digit;
130 write_int (FILE *f, int i)
132 return write_ulong (f, (unsigned long) i);
136 write_string (FILE *f, const FcChar8 *string)
139 if (PUTC ('"', f) == EOF)
141 if (!write_chars (f, string))
143 if (PUTC ('"', f) == EOF)
149 usage (char *program)
152 fprintf (stderr, "usage: %s [-rv] [--recurse] [--verbose] [*-%s.cache-2|directory]...\n",
153 program, FC_ARCHITECTURE);
154 fprintf (stderr, " %s [-V?] [--version] [--help]\n", program);
156 fprintf (stderr, "usage: %s [-rvV?] [*-%s.cache-2|directory]...\n",
157 program, FC_ARCHITECTURE);
159 fprintf (stderr, "Reads font information cache from:\n");
160 fprintf (stderr, " 1) specified fontconfig cache file\n");
161 fprintf (stderr, " 2) related to a particular font directory\n");
162 fprintf (stderr, "\n");
164 fprintf (stderr, " -r, --recurse recurse into subdirectories\n");
165 fprintf (stderr, " -v, --verbose be verbose\n");
166 fprintf (stderr, " -V, --version display font config version and exit\n");
167 fprintf (stderr, " -?, --help display this help and exit\n");
169 fprintf (stderr, " -r (recurse) recurse into subdirectories\n");
170 fprintf (stderr, " -v (verbose) be verbose\n");
171 fprintf (stderr, " -V (version) display font config version and exit\n");
172 fprintf (stderr, " -? (help) display this help and exit\n");
178 * return the path from the directory containing 'cache' to 'file'
181 static const FcChar8 *
182 file_base_name (const FcChar8 *cache, const FcChar8 *file)
184 int cache_len = strlen ((char *) cache);
186 if (!strncmp ((char *) cache, (char *) file, cache_len) && file[cache_len] == '/')
187 return file + cache_len + 1;
191 #define FC_FONT_FILE_DIR ((FcChar8 *) ".dir")
194 cache_print_set (FcFontSet *set, FcStrSet *dirs, const FcChar8 *base_name, FcBool verbose)
197 const FcChar8 *file, *base;
204 list = FcStrListCreate (dirs);
208 while ((dir = FcStrListNext (list)))
210 base = file_base_name (base_name, dir);
211 if (!write_string (stdout, base))
213 if (PUTC (' ', stdout) == EOF)
215 if (!write_int (stdout, 0))
217 if (PUTC (' ', stdout) == EOF)
219 if (!write_string (stdout, FC_FONT_FILE_DIR))
221 if (PUTC ('\n', stdout) == EOF)
226 for (n = 0; n < set->nfont; n++)
228 FcPattern *font = set->fonts[n];
230 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
232 base = file_base_name (base_name, file);
233 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
235 if (!write_string (stdout, base))
237 if (PUTC (' ', stdout) == EOF)
239 if (!write_int (stdout, id))
241 if (PUTC (' ', stdout) == EOF)
243 name = FcNameUnparse (font);
246 ret = write_string (stdout, name);
250 if (PUTC ('\n', stdout) == EOF)
253 if (verbose && !set->nfont && !ndir)
254 printf ("<empty>\n");
256 FcStrListDone (list);
261 FcStrListDone (list);
267 main (int argc, char **argv)
273 FcStrSet *args = NULL;
280 FcBool first = FcTrue;
281 #if HAVE_GETOPT_LONG || HAVE_GETOPT
285 while ((c = getopt_long (argc, argv, "Vvr?", longopts, NULL)) != -1)
287 while ((c = getopt (argc, argv, "Vvr?")) != -1)
292 fprintf (stderr, "fontconfig version %d.%d.%d\n",
293 FC_MAJOR, FC_MINOR, FC_REVISION);
310 config = FcInitLoadConfig ();
313 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
316 FcConfigSetCurrent (config);
318 args = FcStrSetCreate ();
321 fprintf (stderr, "%s: malloc failure\n", argv[0]);
326 for (; i < argc; i++)
328 if (!FcStrSetAddFilename (args, (const FcChar8 *) argv[i]))
330 fprintf (stderr, "%s: malloc failure\n", argv[0]);
334 arglist = FcStrListCreate (args);
337 fprintf (stderr, "%s: malloc failure\n", argv[0]);
344 arglist = FcConfigGetFontDirs (config);
345 while ((arg = FcStrListNext (arglist)))
346 if (!FcStrSetAdd (args, arg))
348 fprintf (stderr, "%s: malloc failure\n", argv[0]);
351 FcStrListDone (arglist);
353 arglist = FcStrListCreate (args);
356 fprintf (stderr, "%s: malloc failure\n", argv[0]);
360 while ((arg = FcStrListNext (arglist)))
363 FcChar8 *cache_file = NULL;
364 struct stat file_stat;
366 if (FcFileIsDir (arg))
367 cache = FcDirCacheLoad (arg, config, &cache_file);
369 cache = FcDirCacheLoadFile (arg, &file_stat);
372 perror ((char *) arg);
377 dirs = FcStrSetCreate ();
378 fs = FcCacheCopySet (cache);
379 for (j = 0; j < FcCacheNumSubdir (cache); j++)
381 FcStrSetAdd (dirs, FcCacheSubdir (cache, j));
383 FcStrSetAdd (args, FcCacheSubdir (cache, j));
390 printf ("Directory: %s\nCache: %s\n--------\n",
391 FcCacheDir(cache), cache_file ? cache_file : arg);
394 cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
396 FcStrSetDestroy (dirs);
398 FcFontSetDestroy (fs);
399 FcDirCacheUnload (cache);
401 FcStrFree (cache_file);