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.
25 #include <fontconfig/fontconfig.h>
26 #include <../src/fccache.c>
31 #include <sys/types.h>
38 #define HAVE_GETOPT_LONG 1
46 #ifndef HAVE_GETOPT_LONG
47 #define HAVE_GETOPT_LONG 0
54 const struct option longopts[] = {
55 {"version", 0, 0, 'V'},
62 extern int optind, opterr, optopt;
67 * POSIX has broken stdio so that getc must do thread-safe locking,
68 * this is a serious performance problem for applications doing large
69 * amounts of IO with getc (as is done here). If available, use
70 * the getc_unlocked varient instead.
73 #if defined(getc_unlocked) || defined(_IO_getc_unlocked)
74 #define GETC(f) getc_unlocked(f)
75 #define PUTC(c,f) putc_unlocked(c,f)
77 #define GETC(f) getc(f)
78 #define PUTC(c,f) putc(c,f)
82 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
85 while ((c = *chars++))
90 if (PUTC ('\\', f) == EOF)
94 if (PUTC (c, f) == EOF)
102 FcCacheWriteUlong (FILE *f, unsigned long t)
105 unsigned long temp, digit;
118 if (PUTC ((char) digit + '0', f) == EOF)
120 temp = temp - pow * digit;
127 FcCacheWriteInt (FILE *f, int i)
129 return FcCacheWriteUlong (f, (unsigned long) i);
133 FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
136 if (PUTC ('"', f) == EOF)
138 if (!FcCacheWriteChars (f, string))
140 if (PUTC ('"', f) == EOF)
146 usage (char *program)
149 fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
152 fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n",
155 fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n");
156 fprintf (stderr, "\n");
158 fprintf (stderr, " -V, --version display font config version and exit\n");
159 fprintf (stderr, " -?, --help display this help and exit\n");
161 fprintf (stderr, " -V (version) display font config version and exit\n");
162 fprintf (stderr, " -? (help) display this help and exit\n");
168 FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
172 FcGlobalCacheDir *d, *next;
173 char * current_arch_machine_name;
174 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
175 off_t current_arch_start = 0;
176 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
181 current_arch_machine_name = FcCacheMachineSignature();
182 fd = open(cache_file, O_RDONLY);
186 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
187 if (current_arch_start < 0)
190 lseek (fd, current_arch_start, SEEK_SET);
191 if (FcCacheReadString (fd, candidate_arch_machine_name,
192 sizeof (candidate_arch_machine_name)) == 0)
199 FcCacheReadString (fd, name_buf, sizeof (name_buf));
200 if (!strlen(name_buf))
202 printf ("fc-cat: printing global cache contents for dir %s\n",
205 if (!FcDirCacheConsume (fd, dir, set))
208 FcCachePrintSet (set, dirs, name_buf);
210 FcFontSetDestroy (set);
211 set = FcFontSetCreate();
220 /* read serialized state from the cache file */
222 FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
225 char * current_arch_machine_name;
226 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
227 off_t current_arch_start = 0;
228 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
233 current_arch_machine_name = FcCacheMachineSignature();
234 fd = open(cache_file, O_RDONLY);
238 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
239 if (current_arch_start < 0)
242 lseek (fd, current_arch_start, SEEK_SET);
243 if (FcCacheReadString (fd, candidate_arch_machine_name,
244 sizeof (candidate_arch_machine_name)) == 0)
247 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
248 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
250 if (!FcDirCacheConsume (fd, dir, set))
263 * return the path from the directory containing 'cache' to 'file'
266 static const FcChar8 *
267 FcFileBaseName (const char *cache, const FcChar8 *file)
269 const FcChar8 *cache_slash;
271 cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
272 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
273 (cache_slash + 1) - (const FcChar8 *)cache))
274 return file + ((cache_slash + 1) - (const FcChar8 *)cache);
279 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
283 const FcChar8 *file, *base;
289 list = FcStrListCreate (dirs);
293 while ((dir = FcStrListNext (list)))
295 base = FcFileBaseName (cache_file, dir);
296 if (!FcCacheWriteStringOld (stdout, base))
298 if (PUTC (' ', stdout) == EOF)
300 if (!FcCacheWriteInt (stdout, 0))
302 if (PUTC (' ', stdout) == EOF)
304 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
306 if (PUTC ('\n', stdout) == EOF)
310 for (n = 0; n < set->nfont; n++)
312 font = set->fonts[n];
313 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
315 base = FcFileBaseName (cache_file, file);
316 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
318 if (FcDebug () & FC_DBG_CACHEV)
319 printf (" write file \"%s\"\n", base);
320 if (!FcCacheWriteStringOld (stdout, base))
322 if (PUTC (' ', stdout) == EOF)
324 if (!FcCacheWriteInt (stdout, id))
326 if (PUTC (' ', stdout) == EOF)
328 name = FcNameUnparse (font);
331 ret = FcCacheWriteStringOld (stdout, name);
335 if (PUTC ('\n', stdout) == EOF)
339 FcStrListDone (list);
344 FcStrListDone (list);
352 main (int argc, char **argv)
355 #if HAVE_GETOPT_LONG || HAVE_GETOPT
357 FcFontSet *fs = FcFontSetCreate();
358 FcStrSet *dirs = FcStrSetCreate();
361 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
363 while ((c = getopt (argc, argv, "fsVv?")) != -1)
368 fprintf (stderr, "fontconfig version %d.%d.%d\n",
369 FC_MAJOR, FC_MINOR, FC_REVISION);
383 if (FcCacheFileRead (fs, dirs, dirname (strdup(argv[i])), argv[i]))
384 FcCachePrintSet (fs, dirs, argv[i]);
387 FcStrSetDestroy (dirs);
388 dirs = FcStrSetCreate ();
389 if (FcCacheGlobalFileReadAndPrint (fs, dirs, dirname (strdup (argv[i])),
394 FcStrSetDestroy (dirs);
395 FcFontSetDestroy (fs);