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
;
251 current_arch_machine_name
= FcCacheMachineSignature();
252 fd
= open(cache_file
, O_RDONLY
);
256 FcCacheReadString (fd
, name_buf
, sizeof (name_buf
));
257 if (!strlen (name_buf
))
259 if (strcmp (name_buf
, FC_GLOBAL_MAGIC_COOKIE
) == 0)
261 printf ("fc-cat: printing directory cache for cache which would be named %s\n",
264 current_arch_start
= FcCacheSkipToArch(fd
, current_arch_machine_name
);
265 if (current_arch_start
< 0)
268 while (strlen(FcCacheReadString (fd
, subdirName
, sizeof (subdirName
))) > 0)
269 FcStrSetAdd (dirs
, (FcChar8
*)subdirName
);
271 dir
= strdup(name_buf
);
272 ls
= FcStrLastSlash ((FcChar8
*)dir
);
276 if (!FcDirCacheConsume (fd
, dir
, set
, 0))
293 * return the path from the directory containing 'cache' to 'file'
296 static const FcChar8
*
297 FcFileBaseName (const char *cache
, const FcChar8
*file
)
299 const FcChar8
*cache_slash
;
301 cache_slash
= FcStrLastSlash ((const FcChar8
*)cache
);
302 if (cache_slash
&& !strncmp ((const char *) cache
, (const char *) file
,
303 (cache_slash
+ 1) - (const FcChar8
*)cache
))
304 return file
+ ((cache_slash
+ 1) - (const FcChar8
*)cache
);
309 FcCachePrintSet (FcFontSet
*set
, FcStrSet
*dirs
, char *base_name
)
313 const FcChar8
*file
, *base
;
319 list
= FcStrListCreate (dirs
);
323 while ((dir
= FcStrListNext (list
)))
325 base
= FcFileBaseName (base_name
, dir
);
326 if (!FcCacheWriteStringOld (stdout
, base
))
328 if (PUTC (' ', stdout
) == EOF
)
330 if (!FcCacheWriteInt (stdout
, 0))
332 if (PUTC (' ', stdout
) == EOF
)
334 if (!FcCacheWriteStringOld (stdout
, FC_FONT_FILE_DIR
))
336 if (PUTC ('\n', stdout
) == EOF
)
340 for (n
= 0; n
< set
->nfont
; n
++)
342 font
= set
->fonts
[n
];
343 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
345 base
= FcFileBaseName (base_name
, file
);
346 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
348 if (FcDebug () & FC_DBG_CACHEV
)
349 printf (" write file \"%s\"\n", base
);
350 if (!FcCacheWriteStringOld (stdout
, base
))
352 if (PUTC (' ', stdout
) == EOF
)
354 if (!FcCacheWriteInt (stdout
, id
))
356 if (PUTC (' ', stdout
) == EOF
)
358 name
= FcNameUnparse (font
);
361 ret
= FcCacheWriteStringOld (stdout
, name
);
365 if (PUTC ('\n', stdout
) == EOF
)
369 FcStrListDone (list
);
374 FcStrListDone (list
);
380 main (int argc
, char **argv
)
383 #if HAVE_GETOPT_LONG || HAVE_GETOPT
385 FcFontSet
*fs
= FcFontSetCreate();
386 FcStrSet
*dirs
= FcStrSetCreate();
391 while ((c
= getopt_long (argc
, argv
, "fsVv?", longopts
, NULL
)) != -1)
393 while ((c
= getopt (argc
, argv
, "fsVv?")) != -1)
398 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
399 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
410 config
= FcInitLoadConfig ();
413 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
416 FcConfigSetCurrent (config
);
421 if (FcFileIsDir ((const FcChar8
*)argv
[i
]))
423 char * dummy_name
= (char *)FcStrPlus ((FcChar8
*)argv
[i
],
424 (FcChar8
*)"/dummy");
425 if (!FcDirScanConfig (fs
, dirs
, 0, 0,
426 (const FcChar8
*)argv
[i
], FcFalse
, config
))
427 fprintf (stderr
, "couldn't load font dir %s\n", argv
[i
]);
430 /* sorry, we can't tell you where the cache file is. */
431 FcCachePrintSet (fs
, dirs
, dummy_name
);
432 FcStrFree ((FcChar8
*)dummy_name
);
435 else if ((name_buf
= FcCacheFileRead (fs
, dirs
, argv
[i
])) != 0)
436 FcCachePrintSet (fs
, dirs
, name_buf
);
439 FcStrSetDestroy (dirs
);
440 dirs
= FcStrSetCreate ();
441 if (FcCacheGlobalFileReadAndPrint (fs
, dirs
, argv
[i
]))
445 FcStrSetDestroy (dirs
);
446 FcFontSetDestroy (fs
);