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>
30 #include <sys/types.h>
37 #define HAVE_GETOPT_LONG 1
45 #ifndef HAVE_GETOPT_LONG
46 #define HAVE_GETOPT_LONG 0
53 const struct option longopts
[] = {
54 {"version", 0, 0, 'V'},
61 extern int optind
, opterr
, optopt
;
66 * POSIX has broken stdio so that getc must do thread-safe locking,
67 * this is a serious performance problem for applications doing large
68 * amounts of IO with getc (as is done here). If available, use
69 * the getc_unlocked varient instead.
72 #if defined(getc_unlocked) || defined(_IO_getc_unlocked)
73 #define GETC(f) getc_unlocked(f)
74 #define PUTC(c,f) putc_unlocked(c,f)
76 #define GETC(f) getc(f)
77 #define PUTC(c,f) putc(c,f)
81 FcCachePrintSet (FcFontSet
*set
, FcStrSet
*dirs
, char *base_name
);
84 FcCacheWriteChars (FILE *f
, const FcChar8
*chars
)
87 while ((c
= *chars
++))
92 if (PUTC ('\\', f
) == EOF
)
96 if (PUTC (c
, f
) == EOF
)
104 FcCacheWriteUlong (FILE *f
, unsigned long t
)
107 unsigned long temp
, digit
;
120 if (PUTC ((char) digit
+ '0', f
) == EOF
)
122 temp
= temp
- pow
* digit
;
129 FcCacheWriteInt (FILE *f
, int i
)
131 return FcCacheWriteUlong (f
, (unsigned long) i
);
135 FcCacheWriteStringOld (FILE *f
, const FcChar8
*string
)
138 if (PUTC ('"', f
) == EOF
)
140 if (!FcCacheWriteChars (f
, string
))
142 if (PUTC ('"', f
) == EOF
)
148 usage (char *program
)
151 fprintf (stderr
, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
154 fprintf (stderr
, "usage: %s [-fsvV?] <fonts.cache-2>\n",
157 fprintf (stderr
, "Reads font information caches in <fonts.cache-2>\n");
158 fprintf (stderr
, "\n");
160 fprintf (stderr
, " -V, --version display font config version and exit\n");
161 fprintf (stderr
, " -?, --help display this help and exit\n");
163 fprintf (stderr
, " -V (version) display font config version and exit\n");
164 fprintf (stderr
, " -? (help) display this help and exit\n");
170 FcCacheGlobalFileReadAndPrint (FcFontSet
* set
, FcStrSet
*dirs
, char *cache_file
)
174 char * current_arch_machine_name
;
175 char candidate_arch_machine_name
[9+MACHINE_SIGNATURE_SIZE
];
176 char subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
177 off_t current_arch_start
= 0;
182 current_arch_machine_name
= FcCacheMachineSignature();
183 fd
= open(cache_file
, O_RDONLY
);
187 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
188 if (current_arch_start
< 0)
191 lseek (fd
, current_arch_start
, SEEK_SET
);
192 if (FcCacheReadString (fd
, candidate_arch_machine_name
,
193 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",
207 if (!FcCacheReadString (fd
, subdirName
,
208 sizeof (subdirName
)) ||
209 !strlen (subdirName
))
211 /* then don't do anything with subdirName. */
214 if (!FcDirCacheConsume (fd
, name_buf
, set
, 0))
217 dir
= malloc (strlen (name_buf
) + 2);
221 strcpy (dir
, name_buf
);
224 FcCachePrintSet (set
, dirs
, dir
);
227 FcFontSetDestroy (set
);
228 set
= FcFontSetCreate();
237 /* read serialized state from the cache file */
239 FcCacheFileRead (FcFontSet
* set
, FcStrSet
*dirs
, char *cache_file
)
242 char * current_arch_machine_name
;
243 off_t current_arch_start
= 0;
244 char subdirName
[FC_MAX_FILE_LEN
+ 1 + 12 + 1];
245 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 ((buf
= FcCacheReadString (fd
, subdirName
, sizeof (subdirName
)))
271 FcStrSetAdd (dirs
, (FcChar8
*)subdirName
);
273 dir
= strdup(name_buf
);
274 ls
= FcStrLastSlash ((FcChar8
*)dir
);
278 if (!FcDirCacheConsume (fd
, dir
, set
, 0))
295 * return the path from the directory containing 'cache' to 'file'
298 static const FcChar8
*
299 FcFileBaseName (const char *cache
, const FcChar8
*file
)
301 const FcChar8
*cache_slash
;
303 cache_slash
= FcStrLastSlash ((const FcChar8
*)cache
);
304 if (cache_slash
&& !strncmp ((const char *) cache
, (const char *) file
,
305 (cache_slash
+ 1) - (const FcChar8
*)cache
))
306 return file
+ ((cache_slash
+ 1) - (const FcChar8
*)cache
);
311 FcCachePrintSet (FcFontSet
*set
, FcStrSet
*dirs
, char *base_name
)
315 const FcChar8
*file
, *base
;
321 list
= FcStrListCreate (dirs
);
325 while ((dir
= FcStrListNext (list
)))
327 base
= FcFileBaseName (base_name
, dir
);
328 if (!FcCacheWriteStringOld (stdout
, base
))
330 if (PUTC (' ', stdout
) == EOF
)
332 if (!FcCacheWriteInt (stdout
, 0))
334 if (PUTC (' ', stdout
) == EOF
)
336 if (!FcCacheWriteStringOld (stdout
, FC_FONT_FILE_DIR
))
338 if (PUTC ('\n', stdout
) == EOF
)
342 for (n
= 0; n
< set
->nfont
; n
++)
344 font
= set
->fonts
[n
];
345 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
347 base
= FcFileBaseName (base_name
, file
);
348 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
350 if (FcDebug () & FC_DBG_CACHEV
)
351 printf (" write file \"%s\"\n", base
);
352 if (!FcCacheWriteStringOld (stdout
, base
))
354 if (PUTC (' ', stdout
) == EOF
)
356 if (!FcCacheWriteInt (stdout
, id
))
358 if (PUTC (' ', stdout
) == EOF
)
360 name
= FcNameUnparse (font
);
363 ret
= FcCacheWriteStringOld (stdout
, name
);
367 if (PUTC ('\n', stdout
) == EOF
)
371 FcStrListDone (list
);
376 FcStrListDone (list
);
382 main (int argc
, char **argv
)
385 #if HAVE_GETOPT_LONG || HAVE_GETOPT
387 FcFontSet
*fs
= FcFontSetCreate();
388 FcStrSet
*dirs
= FcStrSetCreate();
393 while ((c
= getopt_long (argc
, argv
, "fsVv?", longopts
, NULL
)) != -1)
395 while ((c
= getopt (argc
, argv
, "fsVv?")) != -1)
400 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
401 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
412 config
= FcInitLoadConfig ();
415 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
418 FcConfigSetCurrent (config
);
423 if (FcFileIsDir ((const FcChar8
*)argv
[i
]))
425 char * dummy_name
= (char *)FcStrPlus ((FcChar8
*)argv
[i
],
426 (FcChar8
*)"/dummy");
427 if (!FcDirScanConfig (fs
, dirs
, 0, 0,
428 (const FcChar8
*)argv
[i
], FcFalse
, config
))
429 fprintf (stderr
, "couldn't load font dir %s\n", argv
[i
]);
432 /* sorry, we can't tell you where the cache file is. */
433 FcCachePrintSet (fs
, dirs
, dummy_name
);
434 FcStrFree ((FcChar8
*)dummy_name
);
437 else if ((name_buf
= FcCacheFileRead (fs
, dirs
, argv
[i
])) != 0)
438 FcCachePrintSet (fs
, dirs
, name_buf
);
441 FcStrSetDestroy (dirs
);
442 dirs
= FcStrSetCreate ();
443 if (FcCacheGlobalFileReadAndPrint (fs
, dirs
, argv
[i
]))
447 FcStrSetDestroy (dirs
);
448 FcFontSetDestroy (fs
);