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 FcCachePrintSet (FcFontSet
*set
, FcStrSet
*dirs
, char *base_name
);
85 FcCacheWriteChars (FILE *f
, const FcChar8
*chars
)
88 while ((c
= *chars
++))
93 if (PUTC ('\\', f
) == EOF
)
97 if (PUTC (c
, f
) == EOF
)
105 FcCacheWriteUlong (FILE *f
, unsigned long t
)
108 unsigned long temp
, digit
;
121 if (PUTC ((char) digit
+ '0', f
) == EOF
)
123 temp
= temp
- pow
* digit
;
130 FcCacheWriteInt (FILE *f
, int i
)
132 return FcCacheWriteUlong (f
, (unsigned long) i
);
136 FcCacheWriteStringOld (FILE *f
, const FcChar8
*string
)
139 if (PUTC ('"', f
) == EOF
)
141 if (!FcCacheWriteChars (f
, string
))
143 if (PUTC ('"', f
) == EOF
)
149 usage (char *program
)
152 fprintf (stderr
, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
155 fprintf (stderr
, "usage: %s [-fsvV?] <fonts.cache-2>\n",
158 fprintf (stderr
, "Reads font information caches in <fonts.cache-2>\n");
159 fprintf (stderr
, "\n");
161 fprintf (stderr
, " -V, --version display font config version and exit\n");
162 fprintf (stderr
, " -?, --help display this help and exit\n");
164 fprintf (stderr
, " -V (version) display font config version and exit\n");
165 fprintf (stderr
, " -? (help) display this help and exit\n");
171 FcCacheGlobalFileReadAndPrint (FcFontSet
* set
, FcStrSet
*dirs
, char *cache_file
)
175 char * current_arch_machine_name
;
176 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
177 char subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
178 off_t current_arch_start
= 0;
183 current_arch_machine_name
= FcCacheMachineSignature();
184 fd
= open(cache_file
, O_RDONLY
);
188 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
189 if (current_arch_start
< 0)
192 lseek (fd
, current_arch_start
, SEEK_SET
);
193 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
194 sizeof (candidate_arch_machine_name
)) == 0)
200 FcCacheReadString (fd
, name_buf
, sizeof (name_buf
));
201 if (!strlen(name_buf
))
203 printf ("fc-cat: printing global cache contents for dir %s\n",
208 if (!FcCacheReadString (fd
, subdirName
,
209 sizeof (subdirName
)) ||
210 !strlen (subdirName
))
212 /* then don't do anything with subdirName. */
215 if (!FcDirCacheConsume (fd
, name_buf
, set
, 0))
218 dir
= malloc (strlen (name_buf
) + 2);
222 strcpy (dir
, name_buf
);
225 FcCachePrintSet (set
, dirs
, dir
);
228 FcFontSetDestroy (set
);
229 set
= FcFontSetCreate();
238 /* read serialized state from the cache file */
240 FcCacheFileRead (FcFontSet
* set
, FcStrSet
*dirs
, char *cache_file
)
243 char * current_arch_machine_name
;
244 off_t current_arch_start
= 0;
245 char subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
246 static char name_buf
[8192], *dir
;
252 current_arch_machine_name
= FcCacheMachineSignature();
253 fd
= open(cache_file
, O_RDONLY
);
257 FcCacheReadString (fd
, name_buf
, sizeof (name_buf
));
258 if (!strlen (name_buf
))
260 if (strcmp (name_buf
, FC_GLOBAL_MAGIC_COOKIE
) == 0)
262 printf ("fc-cat: printing directory cache for cache which would be named %s\n",
265 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
266 if (current_arch_start
< 0)
269 while (strlen(FcCacheReadString (fd
, subdirName
, sizeof (subdirName
))) > 0)
270 FcStrSetAdd (dirs
, (FcChar8
*)subdirName
);
272 dir
= strdup(name_buf
);
273 ls
= FcStrLastSlash ((FcChar8
*)dir
);
277 if (!FcDirCacheConsume (fd
, dir
, set
, 0))
294 * return the path from the directory containing 'cache' to 'file'
297 static const FcChar8
*
298 FcFileBaseName (const char *cache
, const FcChar8
*file
)
300 const FcChar8
*cache_slash
;
302 cache_slash
= FcStrLastSlash ((const FcChar8
*)cache
);
303 if (cache_slash
&& !strncmp ((const char *) cache
, (const char *) file
,
304 (cache_slash
+ 1) - (const FcChar8
*)cache
))
305 return file
+ ((cache_slash
+ 1) - (const FcChar8
*)cache
);
310 FcCachePrintSet (FcFontSet
*set
, FcStrSet
*dirs
, char *base_name
)
314 const FcChar8
*file
, *base
;
320 list
= FcStrListCreate (dirs
);
324 while ((dir
= FcStrListNext (list
)))
326 base
= FcFileBaseName (base_name
, dir
);
327 if (!FcCacheWriteStringOld (stdout
, base
))
329 if (PUTC (' ', stdout
) == EOF
)
331 if (!FcCacheWriteInt (stdout
, 0))
333 if (PUTC (' ', stdout
) == EOF
)
335 if (!FcCacheWriteStringOld (stdout
, FC_FONT_FILE_DIR
))
337 if (PUTC ('\n', stdout
) == EOF
)
341 for (n
= 0; n
< set
->nfont
; n
++)
343 font
= set
->fonts
[n
];
344 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
346 base
= FcFileBaseName (base_name
, file
);
347 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
349 if (FcDebug () & FC_DBG_CACHEV
)
350 printf (" write file \"%s\"\n", base
);
351 if (!FcCacheWriteStringOld (stdout
, base
))
353 if (PUTC (' ', stdout
) == EOF
)
355 if (!FcCacheWriteInt (stdout
, id
))
357 if (PUTC (' ', stdout
) == EOF
)
359 name
= FcNameUnparse (font
);
362 ret
= FcCacheWriteStringOld (stdout
, name
);
366 if (PUTC ('\n', stdout
) == EOF
)
370 FcStrListDone (list
);
375 FcStrListDone (list
);
381 main (int argc
, char **argv
)
384 #if HAVE_GETOPT_LONG || HAVE_GETOPT
386 FcFontSet
*fs
= FcFontSetCreate();
387 FcStrSet
*dirs
= FcStrSetCreate();
392 while ((c
= getopt_long (argc
, argv
, "fsVv?", longopts
, NULL
)) != -1)
394 while ((c
= getopt (argc
, argv
, "fsVv?")) != -1)
399 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
400 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
411 config
= FcInitLoadConfig ();
414 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
417 FcConfigSetCurrent (config
);
422 if (FcFileIsDir ((const FcChar8
*)argv
[i
]))
424 char * dummy_name
= (char *)FcStrPlus ((FcChar8
*)argv
[i
],
425 (FcChar8
*)"/dummy");
426 if (!FcDirScanConfig (fs
, dirs
, 0, 0,
427 (const FcChar8
*)argv
[i
], FcFalse
, config
))
428 fprintf (stderr
, "couldn't load font dir %s\n", argv
[i
]);
431 /* sorry, we can't tell you where the cache file is. */
432 FcCachePrintSet (fs
, dirs
, dummy_name
);
433 FcStrFree ((FcChar8
*)dummy_name
);
436 else if ((name_buf
= FcCacheFileRead (fs
, dirs
, argv
[i
])) != 0)
437 FcCachePrintSet (fs
, dirs
, name_buf
);
440 FcStrSetDestroy (dirs
);
441 dirs
= FcStrSetCreate ();
442 if (FcCacheGlobalFileReadAndPrint (fs
, dirs
, argv
[i
]))
446 FcStrSetDestroy (dirs
);
447 FcFontSetDestroy (fs
);