]> git.wh0rd.org - fontconfig.git/blame - fc-cat/fc-cat.c
Automatically remove invalid cache files.
[fontconfig.git] / fc-cat / fc-cat.c
CommitLineData
f28f090d
PL
1/*
2 * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
3 *
4 * Copyright © 2002 Keith Packard
5 *
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.
15 *
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.
23 */
24
f28f090d
PL
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#else
28#ifdef linux
29#define HAVE_GETOPT_LONG 1
30#endif
31#define HAVE_GETOPT 1
32#endif
33
d8ab9e6c 34#include "../src/fccache.c"
f045376c
PL
35#include <stdio.h>
36#include <stdlib.h>
37#include <unistd.h>
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <errno.h>
41
f28f090d
PL
42#ifndef HAVE_GETOPT
43#define HAVE_GETOPT 0
44#endif
45#ifndef HAVE_GETOPT_LONG
46#define HAVE_GETOPT_LONG 0
47#endif
48
49#if HAVE_GETOPT_LONG
50#undef _GNU_SOURCE
51#define _GNU_SOURCE
52#include <getopt.h>
53const struct option longopts[] = {
54 {"version", 0, 0, 'V'},
c0288648 55 {"verbose", 0, 0, 'v'},
f28f090d
PL
56 {"help", 0, 0, '?'},
57 {NULL,0,0,0},
58};
59#else
60#if HAVE_GETOPT
61extern char *optarg;
62extern int optind, opterr, optopt;
63#endif
64#endif
65
66/*
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.
71 */
72
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)
76#else
77#define GETC(f) getc(f)
78#define PUTC(c,f) putc(c,f)
79#endif
80
982b5982 81FcBool
c60ec7cc 82FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name);
982b5982 83
f28f090d
PL
84static FcBool
85FcCacheWriteChars (FILE *f, const FcChar8 *chars)
86{
87 FcChar8 c;
88 while ((c = *chars++))
89 {
90 switch (c) {
91 case '"':
92 case '\\':
93 if (PUTC ('\\', f) == EOF)
94 return FcFalse;
95 /* fall through */
96 default:
97 if (PUTC (c, f) == EOF)
98 return FcFalse;
99 }
100 }
101 return FcTrue;
102}
103
104static FcBool
105FcCacheWriteUlong (FILE *f, unsigned long t)
106{
107 int pow;
108 unsigned long temp, digit;
109
110 temp = t;
111 pow = 1;
112 while (temp >= 10)
113 {
114 temp /= 10;
115 pow *= 10;
116 }
117 temp = t;
118 while (pow)
119 {
120 digit = temp / pow;
121 if (PUTC ((char) digit + '0', f) == EOF)
122 return FcFalse;
123 temp = temp - pow * digit;
124 pow = pow / 10;
125 }
126 return FcTrue;
127}
128
129static FcBool
130FcCacheWriteInt (FILE *f, int i)
131{
132 return FcCacheWriteUlong (f, (unsigned long) i);
133}
134
135static FcBool
136FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
137{
138
139 if (PUTC ('"', f) == EOF)
140 return FcFalse;
141 if (!FcCacheWriteChars (f, string))
142 return FcFalse;
143 if (PUTC ('"', f) == EOF)
144 return FcFalse;
145 return FcTrue;
146}
147
148static void
149usage (char *program)
150{
151#if HAVE_GETOPT_LONG
152 fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
153 program);
154#else
155 fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n",
156 program);
157#endif
158 fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n");
159 fprintf (stderr, "\n");
160#if HAVE_GETOPT_LONG
161 fprintf (stderr, " -V, --version display font config version and exit\n");
162 fprintf (stderr, " -?, --help display this help and exit\n");
163#else
164 fprintf (stderr, " -V (version) display font config version and exit\n");
165 fprintf (stderr, " -? (help) display this help and exit\n");
166#endif
167 exit (1);
168}
169
c0288648
KP
170static int
171FcCacheFileOpen (char *cache_file, off_t *size)
f28f090d
PL
172{
173 int fd;
c0288648 174 struct stat file_stat;
f28f090d 175
c0288648
KP
176 fd = open(cache_file, O_RDONLY | O_BINARY);
177 if (fd < 0)
178 return -1;
f28f090d 179
c0288648
KP
180 if (fstat (fd, &file_stat) < 0) {
181 close (fd);
182 return -1;
183 }
184 *size = file_stat.st_size;
185 return fd;
f28f090d
PL
186}
187
188/*
189 * return the path from the directory containing 'cache' to 'file'
190 */
191
192static const FcChar8 *
193FcFileBaseName (const char *cache, const FcChar8 *file)
194{
195 const FcChar8 *cache_slash;
c0288648 196 int cache_len = strlen (cache);
f28f090d 197
c0288648
KP
198 if (!strncmp (cache, file, cache_len) && file[cache_len] == '/')
199 return file + cache_len + 1;
f28f090d
PL
200 return file;
201}
202
203FcBool
c60ec7cc 204FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
f28f090d
PL
205{
206 FcPattern *font;
207 FcChar8 *name, *dir;
208 const FcChar8 *file, *base;
d2f45978 209 int ret;
f28f090d
PL
210 int n;
211 int id;
f28f090d
PL
212 FcStrList *list;
213
214 list = FcStrListCreate (dirs);
215 if (!list)
216 goto bail2;
217
218 while ((dir = FcStrListNext (list)))
219 {
c60ec7cc 220 base = FcFileBaseName (base_name, dir);
f28f090d
PL
221 if (!FcCacheWriteStringOld (stdout, base))
222 goto bail3;
223 if (PUTC (' ', stdout) == EOF)
224 goto bail3;
225 if (!FcCacheWriteInt (stdout, 0))
226 goto bail3;
227 if (PUTC (' ', stdout) == EOF)
228 goto bail3;
229 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
230 goto bail3;
231 if (PUTC ('\n', stdout) == EOF)
232 goto bail3;
233 }
234
235 for (n = 0; n < set->nfont; n++)
236 {
c0288648
KP
237 FcPattern **fonts = FcFontSetFonts (set);
238 FcPattern *encoded_font = fonts[n];
239 FcPattern *font = FcEncodedOffsetToPtr (set, encoded_font, FcPattern);
240
f28f090d
PL
241 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
242 goto bail3;
c60ec7cc 243 base = FcFileBaseName (base_name, file);
f28f090d
PL
244 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
245 goto bail3;
f28f090d
PL
246 if (!FcCacheWriteStringOld (stdout, base))
247 goto bail3;
248 if (PUTC (' ', stdout) == EOF)
249 goto bail3;
250 if (!FcCacheWriteInt (stdout, id))
251 goto bail3;
252 if (PUTC (' ', stdout) == EOF)
253 goto bail3;
254 name = FcNameUnparse (font);
255 if (!name)
256 goto bail3;
257 ret = FcCacheWriteStringOld (stdout, name);
258 FcStrFree (name);
259 if (!ret)
260 goto bail3;
261 if (PUTC ('\n', stdout) == EOF)
262 goto bail3;
263 }
264
265 FcStrListDone (list);
266
267 return FcTrue;
268
269bail3:
270 FcStrListDone (list);
271bail2:
f28f090d
PL
272 return FcFalse;
273}
274
76abb77f
KP
275FcCache *
276FcCacheFileMap (const FcChar8 *file)
277{
278 FcCache *cache;
279 int fd;
280 struct stat file_stat;
281
282 fd = open (file, O_RDONLY | O_BINARY);
283 if (fd < 0)
284 return NULL;
285 if (fstat (fd, &file_stat) < 0) {
286 close (fd);
287 return NULL;
288 }
289 if (FcCacheLoad (fd, file_stat.st_size, &cache)) {
290 close (fd);
291 return cache;
292 }
293 close (fd);
294 return NULL;
295}
296
f28f090d
PL
297int
298main (int argc, char **argv)
299{
300 int i;
c0288648
KP
301 int ret = 0;
302 FcFontSet *fs;
303 FcStrSet *dirs;
304 FcCache *cache;
305 FcConfig *config;
306 int verbose = 0;
f28f090d
PL
307#if HAVE_GETOPT_LONG || HAVE_GETOPT
308 int c;
f28f090d
PL
309
310#if HAVE_GETOPT_LONG
c0288648 311 while ((c = getopt_long (argc, argv, "Vv?", longopts, NULL)) != -1)
f28f090d 312#else
c0288648 313 while ((c = getopt (argc, argv, "Vv?")) != -1)
f28f090d
PL
314#endif
315 {
316 switch (c) {
317 case 'V':
318 fprintf (stderr, "fontconfig version %d.%d.%d\n",
319 FC_MAJOR, FC_MINOR, FC_REVISION);
320 exit (0);
c0288648
KP
321 case 'v':
322 verbose++;
323 break;
f28f090d
PL
324 default:
325 usage (argv[0]);
326 }
327 }
328 i = optind;
329#else
330 i = 1;
331#endif
332
9769b43d
PL
333 config = FcInitLoadConfig ();
334 if (!config)
335 {
336 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
337 return 1;
338 }
339 FcConfigSetCurrent (config);
340
8f2a8078
PL
341 if (i >= argc)
342 usage (argv[0]);
343
c0288648 344 for (; i < argc; i++)
6059daed 345 {
c0288648
KP
346 int j;
347 off_t size;
348 intptr_t *cache_dirs;
349
350 if (FcFileIsDir ((const FcChar8 *)argv[i]))
76abb77f 351 cache = FcDirCacheMap ((const FcChar8 *) argv[i], config);
c0288648 352 else
76abb77f
KP
353 cache = FcCacheFileMap (argv[i]);
354 if (!cache)
c0288648
KP
355 {
356 perror (argv[i]);
357 ret++;
358 continue;
359 }
360
c0288648
KP
361 dirs = FcStrSetCreate ();
362 fs = FcCacheSet (cache);
363 cache_dirs = FcCacheDirs (cache);
364 for (j = 0; j < cache->dirs_count; j++)
365 FcStrSetAdd (dirs, FcOffsetToPtr (cache_dirs,
366 cache_dirs[j],
367 FcChar8));
368
369 if (verbose)
370 printf ("Name: %s\nDirectory: %s\n", argv[i], FcCacheDir(cache));
371 FcCachePrintSet (fs, dirs, FcCacheDir (cache));
f28f090d 372
c0288648
KP
373 FcStrSetDestroy (dirs);
374
375 FcDirCacheUnmap (cache);
376 }
f28f090d
PL
377
378 return 0;
379}