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>
46 #define _(x) (dgettext(GETTEXT_PACKAGE, x))
48 #define dgettext(d, s) (s)
55 #ifndef HAVE_GETOPT_LONG
56 #define HAVE_GETOPT_LONG 0
63 const struct option longopts[] = {
64 {"version", 0, 0, 'V'},
65 {"verbose", 0, 0, 'v'},
66 {"recurse", 0, 0, 'r'},
73 extern int optind, opterr, optopt;
78 * POSIX has broken stdio so that putc must do thread-safe locking,
79 * this is a serious performance problem for applications doing large
80 * amounts of IO with putc (as is done here). If available, use
81 * the putc_unlocked varient instead.
84 #if defined(putc_unlocked) || defined(_IO_putc_unlocked)
85 #define PUTC(c,f) putc_unlocked(c,f)
87 #define PUTC(c,f) putc(c,f)
91 write_chars (FILE *f, const FcChar8 *chars)
94 while ((c = *chars++))
99 if (PUTC ('\\', f) == EOF)
103 if (PUTC (c, f) == EOF)
111 write_ulong (FILE *f, unsigned long t)
114 unsigned long temp, digit;
127 if (PUTC ((char) digit + '0', f) == EOF)
129 temp = temp - pow * digit;
136 write_int (FILE *f, int i)
138 return write_ulong (f, (unsigned long) i);
142 write_string (FILE *f, const FcChar8 *string)
145 if (PUTC ('"', f) == EOF)
147 if (!write_chars (f, string))
149 if (PUTC ('"', f) == EOF)
155 usage (char *program, int error)
157 FILE *file = error ? stderr : stdout;
159 fprintf (file, _("usage: %s [-rv] [--recurse] [--verbose] [*-%s" FC_CACHE_SUFFIX "|directory]...\n"),
160 program, FC_ARCHITECTURE);
161 fprintf (file, " %s [-Vh] [--version] [--help]\n", program);
163 fprintf (file, _("usage: %s [-rvVh] [*-%s" FC_CACHE_SUFFIX "|directory]...\n"),
164 program, FC_ARCHITECTURE);
166 fprintf (file, _("Reads font information cache from:\n"));
167 fprintf (file, _(" 1) specified fontconfig cache file\n"));
168 fprintf (file, _(" 2) related to a particular font directory\n"));
169 fprintf (file, "\n");
171 fprintf (file, _(" -r, --recurse recurse into subdirectories\n"));
172 fprintf (file, _(" -v, --verbose be verbose\n"));
173 fprintf (file, _(" -V, --version display font config version and exit\n"));
174 fprintf (file, _(" -h, --help display this help and exit\n"));
176 fprintf (file, _(" -r (recurse) recurse into subdirectories\n"));
177 fprintf (file, _(" -v (verbose) be verbose\n"));
178 fprintf (file, _(" -V (version) display font config version and exit\n"));
179 fprintf (file, _(" -h (help) display this help and exit\n"));
185 * return the path from the directory containing 'cache' to 'file'
188 static const FcChar8 *
189 file_base_name (const FcChar8 *cache, const FcChar8 *file)
191 int cache_len = strlen ((char *) cache);
193 if (!strncmp ((char *) cache, (char *) file, cache_len) && file[cache_len] == '/')
194 return file + cache_len + 1;
198 #define FC_FONT_FILE_DIR ((FcChar8 *) ".dir")
201 cache_print_set (FcFontSet *set, FcStrSet *dirs, const FcChar8 *base_name, FcBool verbose)
209 list = FcStrListCreate (dirs);
213 while ((dir = FcStrListNext (list)))
215 base = file_base_name (base_name, dir);
216 if (!write_string (stdout, base))
218 if (PUTC (' ', stdout) == EOF)
220 if (!write_int (stdout, 0))
222 if (PUTC (' ', stdout) == EOF)
224 if (!write_string (stdout, FC_FONT_FILE_DIR))
226 if (PUTC ('\n', stdout) == EOF)
231 for (n = 0; n < set->nfont; n++)
233 FcPattern *font = set->fonts[n];
236 s = FcPatternFormat (font, (const FcChar8 *) "%{=fccat}\n");
243 if (verbose && !set->nfont && !ndir)
244 printf ("<empty>\n");
246 FcStrListDone (list);
251 FcStrListDone (list);
257 main (int argc, char **argv)
263 FcStrSet *args = NULL;
270 FcBool first = FcTrue;
271 #if HAVE_GETOPT_LONG || HAVE_GETOPT
275 while ((c = getopt_long (argc, argv, "Vvrh", longopts, NULL)) != -1)
277 while ((c = getopt (argc, argv, "Vvrh")) != -1)
282 fprintf (stderr, "fontconfig version %d.%d.%d\n",
283 FC_MAJOR, FC_MINOR, FC_REVISION);
302 config = FcInitLoadConfig ();
305 fprintf (stderr, _("%s: Can't initialize font config library\n"), argv[0]);
308 FcConfigSetCurrent (config);
309 FcConfigDestroy (config);
311 args = FcStrSetCreate ();
314 fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
319 for (; i < argc; i++)
321 if (!FcStrSetAddFilename (args, (const FcChar8 *) argv[i]))
323 fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
331 arglist = FcConfigGetFontDirs (config);
332 while ((arg = FcStrListNext (arglist)))
333 if (!FcStrSetAdd (args, arg))
335 fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
338 FcStrListDone (arglist);
340 arglist = FcStrListCreate (args);
343 fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
346 FcStrSetDestroy (args);
348 while ((arg = FcStrListNext (arglist)))
351 FcChar8 *cache_file = NULL;
352 struct stat file_stat;
356 if (FcFileIsDir (arg))
357 cache = FcDirCacheLoad (arg, config, &cache_file);
359 cache = FcDirCacheLoadFile (arg, &file_stat);
363 perror ((char *) arg);
365 fprintf (stderr, "%s: Unable to load the cache: %s\n", argv[0], arg);
370 dirs = FcStrSetCreate ();
371 fs = FcCacheCopySet (cache);
372 for (j = 0; j < FcCacheNumSubdir (cache); j++)
374 FcStrSetAdd (dirs, FcCacheSubdir (cache, j));
376 FcStrSetAdd (args, FcCacheSubdir (cache, j));
383 printf (_("Directory: %s\nCache: %s\n--------\n"),
384 FcCacheDir(cache), cache_file ? cache_file : arg);
387 cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
389 FcStrSetDestroy (dirs);
391 FcFontSetDestroy (fs);
392 FcDirCacheUnload (cache);
394 FcStrFree (cache_file);
396 FcStrListDone (arglist);