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 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 * representations about the suitability of this software for any purpose. It
14 * 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 * 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 "../src/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 putc must do thread-safe locking,
71 * this is a serious performance problem for applications doing large
72 * amounts of IO with putc (as is done here). If available, use
73 * the putc_unlocked varient instead.
76 #if defined(putc_unlocked) || defined(_IO_putc_unlocked)
77 #define PUTC(c,f) putc_unlocked(c,f)
79 #define PUTC(c,f) putc(c,f)
83 write_chars (FILE *f, const FcChar8 *chars)
86 while ((c = *chars++))
91 if (PUTC ('\\', f) == EOF)
95 if (PUTC (c, f) == EOF)
103 write_ulong (FILE *f, unsigned long t)
106 unsigned long temp, digit;
119 if (PUTC ((char) digit + '0', f) == EOF)
121 temp = temp - pow * digit;
128 write_int (FILE *f, int i)
130 return write_ulong (f, (unsigned long) i);
134 write_string (FILE *f, const FcChar8 *string)
137 if (PUTC ('"', f) == EOF)
139 if (!write_chars (f, string))
141 if (PUTC ('"', f) == EOF)
147 usage (char *program, int error)
149 FILE *file = error ? stderr : stdout;
151 fprintf (file, "usage: %s [-rv] [--recurse] [--verbose] [*-%s" FC_CACHE_SUFFIX "|directory]...\n",
152 program, FC_ARCHITECTURE);
153 fprintf (file, " %s [-Vh] [--version] [--help]\n", program);
155 fprintf (file, "usage: %s [-rvVh] [*-%s" FC_CACHE_SUFFIX "|directory]...\n",
156 program, FC_ARCHITECTURE);
158 fprintf (file, "Reads font information cache from:\n");
159 fprintf (file, " 1) specified fontconfig cache file\n");
160 fprintf (file, " 2) related to a particular font directory\n");
161 fprintf (file, "\n");
163 fprintf (file, " -r, --recurse recurse into subdirectories\n");
164 fprintf (file, " -v, --verbose be verbose\n");
165 fprintf (file, " -V, --version display font config version and exit\n");
166 fprintf (file, " -h, --help display this help and exit\n");
168 fprintf (file, " -r (recurse) recurse into subdirectories\n");
169 fprintf (file, " -v (verbose) be verbose\n");
170 fprintf (file, " -V (version) display font config version and exit\n");
171 fprintf (file, " -h (help) display this help and exit\n");
177 * return the path from the directory containing 'cache' to 'file'
180 static const FcChar8 *
181 file_base_name (const FcChar8 *cache, const FcChar8 *file)
183 int cache_len = strlen ((char *) cache);
185 if (!strncmp ((char *) cache, (char *) file, cache_len) && file[cache_len] == '/')
186 return file + cache_len + 1;
190 #define FC_FONT_FILE_DIR ((FcChar8 *) ".dir")
193 cache_print_set (FcFontSet *set, FcStrSet *dirs, const FcChar8 *base_name, FcBool verbose)
201 list = FcStrListCreate (dirs);
205 while ((dir = FcStrListNext (list)))
207 base = file_base_name (base_name, dir);
208 if (!write_string (stdout, base))
210 if (PUTC (' ', stdout) == EOF)
212 if (!write_int (stdout, 0))
214 if (PUTC (' ', stdout) == EOF)
216 if (!write_string (stdout, FC_FONT_FILE_DIR))
218 if (PUTC ('\n', stdout) == EOF)
223 for (n = 0; n < set->nfont; n++)
225 FcPattern *font = set->fonts[n];
228 s = FcPatternFormat (font, (const FcChar8 *) "%{=fccat}\n");
235 if (verbose && !set->nfont && !ndir)
236 printf ("<empty>\n");
238 FcStrListDone (list);
243 FcStrListDone (list);
249 main (int argc, char **argv)
255 FcStrSet *args = NULL;
262 FcBool first = FcTrue;
263 #if HAVE_GETOPT_LONG || HAVE_GETOPT
267 while ((c = getopt_long (argc, argv, "Vvrh", longopts, NULL)) != -1)
269 while ((c = getopt (argc, argv, "Vvrh")) != -1)
274 fprintf (stderr, "fontconfig version %d.%d.%d\n",
275 FC_MAJOR, FC_MINOR, FC_REVISION);
294 config = FcInitLoadConfig ();
297 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
300 FcConfigSetCurrent (config);
302 args = FcStrSetCreate ();
305 fprintf (stderr, "%s: malloc failure\n", argv[0]);
310 for (; i < argc; i++)
312 if (!FcStrSetAddFilename (args, (const FcChar8 *) argv[i]))
314 fprintf (stderr, "%s: malloc failure\n", argv[0]);
318 arglist = FcStrListCreate (args);
321 fprintf (stderr, "%s: malloc failure\n", argv[0]);
328 arglist = FcConfigGetFontDirs (config);
329 while ((arg = FcStrListNext (arglist)))
330 if (!FcStrSetAdd (args, arg))
332 fprintf (stderr, "%s: malloc failure\n", argv[0]);
335 FcStrListDone (arglist);
337 arglist = FcStrListCreate (args);
340 fprintf (stderr, "%s: malloc failure\n", argv[0]);
344 while ((arg = FcStrListNext (arglist)))
347 FcChar8 *cache_file = NULL;
348 struct stat file_stat;
350 if (FcFileIsDir (arg))
351 cache = FcDirCacheLoad (arg, config, &cache_file);
353 cache = FcDirCacheLoadFile (arg, &file_stat);
356 perror ((char *) arg);
361 dirs = FcStrSetCreate ();
362 fs = FcCacheCopySet (cache);
363 for (j = 0; j < FcCacheNumSubdir (cache); j++)
365 FcStrSetAdd (dirs, FcCacheSubdir (cache, j));
367 FcStrSetAdd (args, FcCacheSubdir (cache, j));
374 printf ("Directory: %s\nCache: %s\n--------\n",
375 FcCacheDir(cache), cache_file ? cache_file : arg);
378 cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
380 FcStrSetDestroy (dirs);
382 FcFontSetDestroy (fs);
383 FcDirCacheUnload (cache);
385 FcStrFree (cache_file);