]> git.wh0rd.org - fontconfig.git/blob - fc-cat/fc-cat.c
Automatically remove invalid cache files.
[fontconfig.git] / fc-cat / fc-cat.c
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
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
34 #include "../src/fccache.c"
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
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>
53 const struct option longopts[] = {
54 {"version", 0, 0, 'V'},
55 {"verbose", 0, 0, 'v'},
56 {"help", 0, 0, '?'},
57 {NULL,0,0,0},
58 };
59 #else
60 #if HAVE_GETOPT
61 extern char *optarg;
62 extern 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
81 FcBool
82 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name);
83
84 static FcBool
85 FcCacheWriteChars (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
104 static FcBool
105 FcCacheWriteUlong (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
129 static FcBool
130 FcCacheWriteInt (FILE *f, int i)
131 {
132 return FcCacheWriteUlong (f, (unsigned long) i);
133 }
134
135 static FcBool
136 FcCacheWriteStringOld (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
148 static void
149 usage (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
170 static int
171 FcCacheFileOpen (char *cache_file, off_t *size)
172 {
173 int fd;
174 struct stat file_stat;
175
176 fd = open(cache_file, O_RDONLY | O_BINARY);
177 if (fd < 0)
178 return -1;
179
180 if (fstat (fd, &file_stat) < 0) {
181 close (fd);
182 return -1;
183 }
184 *size = file_stat.st_size;
185 return fd;
186 }
187
188 /*
189 * return the path from the directory containing 'cache' to 'file'
190 */
191
192 static const FcChar8 *
193 FcFileBaseName (const char *cache, const FcChar8 *file)
194 {
195 const FcChar8 *cache_slash;
196 int cache_len = strlen (cache);
197
198 if (!strncmp (cache, file, cache_len) && file[cache_len] == '/')
199 return file + cache_len + 1;
200 return file;
201 }
202
203 FcBool
204 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
205 {
206 FcPattern *font;
207 FcChar8 *name, *dir;
208 const FcChar8 *file, *base;
209 int ret;
210 int n;
211 int id;
212 FcStrList *list;
213
214 list = FcStrListCreate (dirs);
215 if (!list)
216 goto bail2;
217
218 while ((dir = FcStrListNext (list)))
219 {
220 base = FcFileBaseName (base_name, dir);
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 {
237 FcPattern **fonts = FcFontSetFonts (set);
238 FcPattern *encoded_font = fonts[n];
239 FcPattern *font = FcEncodedOffsetToPtr (set, encoded_font, FcPattern);
240
241 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
242 goto bail3;
243 base = FcFileBaseName (base_name, file);
244 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
245 goto bail3;
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
269 bail3:
270 FcStrListDone (list);
271 bail2:
272 return FcFalse;
273 }
274
275 FcCache *
276 FcCacheFileMap (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
297 int
298 main (int argc, char **argv)
299 {
300 int i;
301 int ret = 0;
302 FcFontSet *fs;
303 FcStrSet *dirs;
304 FcCache *cache;
305 FcConfig *config;
306 int verbose = 0;
307 #if HAVE_GETOPT_LONG || HAVE_GETOPT
308 int c;
309
310 #if HAVE_GETOPT_LONG
311 while ((c = getopt_long (argc, argv, "Vv?", longopts, NULL)) != -1)
312 #else
313 while ((c = getopt (argc, argv, "Vv?")) != -1)
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);
321 case 'v':
322 verbose++;
323 break;
324 default:
325 usage (argv[0]);
326 }
327 }
328 i = optind;
329 #else
330 i = 1;
331 #endif
332
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
341 if (i >= argc)
342 usage (argv[0]);
343
344 for (; i < argc; i++)
345 {
346 int j;
347 off_t size;
348 intptr_t *cache_dirs;
349
350 if (FcFileIsDir ((const FcChar8 *)argv[i]))
351 cache = FcDirCacheMap ((const FcChar8 *) argv[i], config);
352 else
353 cache = FcCacheFileMap (argv[i]);
354 if (!cache)
355 {
356 perror (argv[i]);
357 ret++;
358 continue;
359 }
360
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));
372
373 FcStrSetDestroy (dirs);
374
375 FcDirCacheUnmap (cache);
376 }
377
378 return 0;
379 }