]>
Commit | Line | Data |
---|---|---|
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 | ||
25 | #include <fontconfig/fontconfig.h> | |
26 | #include <../src/fccache.c> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <unistd.h> | |
30 | #include <sys/types.h> | |
31 | #include <sys/stat.h> | |
32 | #include <errno.h> | |
33 | #ifdef HAVE_CONFIG_H | |
34 | #include <config.h> | |
35 | #else | |
36 | #ifdef linux | |
37 | #define HAVE_GETOPT_LONG 1 | |
38 | #endif | |
39 | #define HAVE_GETOPT 1 | |
40 | #endif | |
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 | {"help", 0, 0, '?'}, | |
56 | {NULL,0,0,0}, | |
57 | }; | |
58 | #else | |
59 | #if HAVE_GETOPT | |
60 | extern char *optarg; | |
61 | extern int optind, opterr, optopt; | |
62 | #endif | |
63 | #endif | |
64 | ||
65 | /* | |
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. | |
70 | */ | |
71 | ||
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) | |
75 | #else | |
76 | #define GETC(f) getc(f) | |
77 | #define PUTC(c,f) putc(c,f) | |
78 | #endif | |
79 | ||
982b5982 | 80 | FcBool |
c60ec7cc | 81 | FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name); |
982b5982 | 82 | |
f28f090d PL |
83 | static FcBool |
84 | FcCacheWriteChars (FILE *f, const FcChar8 *chars) | |
85 | { | |
86 | FcChar8 c; | |
87 | while ((c = *chars++)) | |
88 | { | |
89 | switch (c) { | |
90 | case '"': | |
91 | case '\\': | |
92 | if (PUTC ('\\', f) == EOF) | |
93 | return FcFalse; | |
94 | /* fall through */ | |
95 | default: | |
96 | if (PUTC (c, f) == EOF) | |
97 | return FcFalse; | |
98 | } | |
99 | } | |
100 | return FcTrue; | |
101 | } | |
102 | ||
103 | static FcBool | |
104 | FcCacheWriteUlong (FILE *f, unsigned long t) | |
105 | { | |
106 | int pow; | |
107 | unsigned long temp, digit; | |
108 | ||
109 | temp = t; | |
110 | pow = 1; | |
111 | while (temp >= 10) | |
112 | { | |
113 | temp /= 10; | |
114 | pow *= 10; | |
115 | } | |
116 | temp = t; | |
117 | while (pow) | |
118 | { | |
119 | digit = temp / pow; | |
120 | if (PUTC ((char) digit + '0', f) == EOF) | |
121 | return FcFalse; | |
122 | temp = temp - pow * digit; | |
123 | pow = pow / 10; | |
124 | } | |
125 | return FcTrue; | |
126 | } | |
127 | ||
128 | static FcBool | |
129 | FcCacheWriteInt (FILE *f, int i) | |
130 | { | |
131 | return FcCacheWriteUlong (f, (unsigned long) i); | |
132 | } | |
133 | ||
134 | static FcBool | |
135 | FcCacheWriteStringOld (FILE *f, const FcChar8 *string) | |
136 | { | |
137 | ||
138 | if (PUTC ('"', f) == EOF) | |
139 | return FcFalse; | |
140 | if (!FcCacheWriteChars (f, string)) | |
141 | return FcFalse; | |
142 | if (PUTC ('"', f) == EOF) | |
143 | return FcFalse; | |
144 | return FcTrue; | |
145 | } | |
146 | ||
147 | static void | |
148 | usage (char *program) | |
149 | { | |
150 | #if HAVE_GETOPT_LONG | |
151 | fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n", | |
152 | program); | |
153 | #else | |
154 | fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n", | |
155 | program); | |
156 | #endif | |
157 | fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n"); | |
158 | fprintf (stderr, "\n"); | |
159 | #if HAVE_GETOPT_LONG | |
160 | fprintf (stderr, " -V, --version display font config version and exit\n"); | |
161 | fprintf (stderr, " -?, --help display this help and exit\n"); | |
162 | #else | |
163 | fprintf (stderr, " -V (version) display font config version and exit\n"); | |
164 | fprintf (stderr, " -? (help) display this help and exit\n"); | |
165 | #endif | |
166 | exit (1); | |
167 | } | |
168 | ||
6059daed | 169 | static FcBool |
c60ec7cc | 170 | FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char *cache_file) |
6059daed PL |
171 | { |
172 | char name_buf[8192]; | |
173 | int fd; | |
6059daed PL |
174 | char * current_arch_machine_name; |
175 | char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE]; | |
2c4e0124 | 176 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; |
6059daed | 177 | off_t current_arch_start = 0; |
6059daed PL |
178 | |
179 | if (!cache_file) | |
180 | goto bail; | |
181 | ||
182 | current_arch_machine_name = FcCacheMachineSignature(); | |
183 | fd = open(cache_file, O_RDONLY); | |
184 | if (fd == -1) | |
185 | goto bail; | |
186 | ||
c60ec7cc | 187 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); |
6059daed PL |
188 | if (current_arch_start < 0) |
189 | goto bail1; | |
190 | ||
191 | lseek (fd, current_arch_start, SEEK_SET); | |
192 | if (FcCacheReadString (fd, candidate_arch_machine_name, | |
193 | sizeof (candidate_arch_machine_name)) == 0) | |
194 | goto bail1; | |
195 | ||
196 | while (1) | |
197 | { | |
68355f38 | 198 | char * dir; |
6059daed PL |
199 | FcCacheReadString (fd, name_buf, sizeof (name_buf)); |
200 | if (!strlen(name_buf)) | |
201 | break; | |
202 | printf ("fc-cat: printing global cache contents for dir %s\n", | |
203 | name_buf); | |
204 | ||
2c4e0124 PL |
205 | do |
206 | { | |
207 | if (!FcCacheReadString (fd, subdirName, | |
208 | sizeof (subdirName)) || | |
209 | !strlen (subdirName)) | |
210 | break; | |
211 | /* then don't do anything with subdirName. */ | |
212 | } while (1); | |
213 | ||
cd9bca69 | 214 | if (!FcDirCacheConsume (fd, name_buf, set, 0)) |
6059daed PL |
215 | goto bail1; |
216 | ||
8a0b0ed6 PL |
217 | dir = malloc (strlen (name_buf) + 2); |
218 | if (!dir) | |
219 | goto bail1; | |
220 | ||
221 | strcpy (dir, name_buf); | |
c60ec7cc PL |
222 | strcat (dir, "/"); |
223 | ||
224 | FcCachePrintSet (set, dirs, dir); | |
225 | free (dir); | |
6059daed PL |
226 | |
227 | FcFontSetDestroy (set); | |
228 | set = FcFontSetCreate(); | |
229 | } | |
230 | ||
231 | bail1: | |
232 | close (fd); | |
233 | bail: | |
234 | return FcFalse; | |
235 | } | |
236 | ||
f28f090d | 237 | /* read serialized state from the cache file */ |
c60ec7cc PL |
238 | static char * |
239 | FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file) | |
f28f090d PL |
240 | { |
241 | int fd; | |
242 | char * current_arch_machine_name; | |
f28f090d PL |
243 | off_t current_arch_start = 0; |
244 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; | |
c60ec7cc PL |
245 | static char name_buf[8192], *dir; |
246 | FcChar8 * ls; | |
04f7d3e7 | 247 | char * buf; |
f28f090d PL |
248 | |
249 | if (!cache_file) | |
250 | goto bail; | |
251 | ||
252 | current_arch_machine_name = FcCacheMachineSignature(); | |
253 | fd = open(cache_file, O_RDONLY); | |
254 | if (fd == -1) | |
255 | goto bail; | |
256 | ||
ea44e218 PL |
257 | FcCacheReadString (fd, name_buf, sizeof (name_buf)); |
258 | if (!strlen (name_buf)) | |
259 | goto bail; | |
c60ec7cc PL |
260 | if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) == 0) |
261 | goto bail; | |
262 | printf ("fc-cat: printing directory cache for cache which would be named %s\n", | |
ea44e218 PL |
263 | name_buf); |
264 | ||
c60ec7cc | 265 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); |
f28f090d PL |
266 | if (current_arch_start < 0) |
267 | goto bail1; | |
268 | ||
04f7d3e7 PL |
269 | while ((buf = FcCacheReadString (fd, subdirName, sizeof (subdirName))) |
270 | && *buf) | |
f28f090d PL |
271 | FcStrSetAdd (dirs, (FcChar8 *)subdirName); |
272 | ||
c60ec7cc PL |
273 | dir = strdup(name_buf); |
274 | ls = FcStrLastSlash ((FcChar8 *)dir); | |
275 | if (ls) | |
276 | *ls = 0; | |
277 | ||
cd9bca69 | 278 | if (!FcDirCacheConsume (fd, dir, set, 0)) |
c60ec7cc PL |
279 | goto bail2; |
280 | free (dir); | |
281 | ||
f28f090d | 282 | close(fd); |
c60ec7cc PL |
283 | return name_buf; |
284 | ||
285 | bail2: | |
286 | free (dir); | |
f28f090d PL |
287 | |
288 | bail1: | |
289 | close (fd); | |
290 | bail: | |
c60ec7cc | 291 | return 0; |
f28f090d PL |
292 | } |
293 | ||
294 | /* | |
295 | * return the path from the directory containing 'cache' to 'file' | |
296 | */ | |
297 | ||
298 | static const FcChar8 * | |
299 | FcFileBaseName (const char *cache, const FcChar8 *file) | |
300 | { | |
301 | const FcChar8 *cache_slash; | |
302 | ||
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); | |
307 | return file; | |
308 | } | |
309 | ||
310 | FcBool | |
c60ec7cc | 311 | FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name) |
f28f090d PL |
312 | { |
313 | FcPattern *font; | |
314 | FcChar8 *name, *dir; | |
315 | const FcChar8 *file, *base; | |
d2f45978 | 316 | int ret; |
f28f090d PL |
317 | int n; |
318 | int id; | |
f28f090d PL |
319 | FcStrList *list; |
320 | ||
321 | list = FcStrListCreate (dirs); | |
322 | if (!list) | |
323 | goto bail2; | |
324 | ||
325 | while ((dir = FcStrListNext (list))) | |
326 | { | |
c60ec7cc | 327 | base = FcFileBaseName (base_name, dir); |
f28f090d PL |
328 | if (!FcCacheWriteStringOld (stdout, base)) |
329 | goto bail3; | |
330 | if (PUTC (' ', stdout) == EOF) | |
331 | goto bail3; | |
332 | if (!FcCacheWriteInt (stdout, 0)) | |
333 | goto bail3; | |
334 | if (PUTC (' ', stdout) == EOF) | |
335 | goto bail3; | |
336 | if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR)) | |
337 | goto bail3; | |
338 | if (PUTC ('\n', stdout) == EOF) | |
339 | goto bail3; | |
340 | } | |
341 | ||
342 | for (n = 0; n < set->nfont; n++) | |
343 | { | |
344 | font = set->fonts[n]; | |
345 | if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch) | |
346 | goto bail3; | |
c60ec7cc | 347 | base = FcFileBaseName (base_name, file); |
f28f090d PL |
348 | if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch) |
349 | goto bail3; | |
350 | if (FcDebug () & FC_DBG_CACHEV) | |
351 | printf (" write file \"%s\"\n", base); | |
352 | if (!FcCacheWriteStringOld (stdout, base)) | |
353 | goto bail3; | |
354 | if (PUTC (' ', stdout) == EOF) | |
355 | goto bail3; | |
356 | if (!FcCacheWriteInt (stdout, id)) | |
357 | goto bail3; | |
358 | if (PUTC (' ', stdout) == EOF) | |
359 | goto bail3; | |
360 | name = FcNameUnparse (font); | |
361 | if (!name) | |
362 | goto bail3; | |
363 | ret = FcCacheWriteStringOld (stdout, name); | |
364 | FcStrFree (name); | |
365 | if (!ret) | |
366 | goto bail3; | |
367 | if (PUTC ('\n', stdout) == EOF) | |
368 | goto bail3; | |
369 | } | |
370 | ||
371 | FcStrListDone (list); | |
372 | ||
373 | return FcTrue; | |
374 | ||
375 | bail3: | |
376 | FcStrListDone (list); | |
377 | bail2: | |
f28f090d PL |
378 | return FcFalse; |
379 | } | |
380 | ||
381 | int | |
382 | main (int argc, char **argv) | |
383 | { | |
384 | int i; | |
f28f090d PL |
385 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
386 | int c; | |
387 | FcFontSet *fs = FcFontSetCreate(); | |
388 | FcStrSet *dirs = FcStrSetCreate(); | |
c60ec7cc | 389 | char *name_buf; |
9769b43d | 390 | FcConfig *config; |
f28f090d PL |
391 | |
392 | #if HAVE_GETOPT_LONG | |
393 | while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1) | |
394 | #else | |
395 | while ((c = getopt (argc, argv, "fsVv?")) != -1) | |
396 | #endif | |
397 | { | |
398 | switch (c) { | |
399 | case 'V': | |
400 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
401 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
402 | exit (0); | |
403 | default: | |
404 | usage (argv[0]); | |
405 | } | |
406 | } | |
407 | i = optind; | |
408 | #else | |
409 | i = 1; | |
410 | #endif | |
411 | ||
9769b43d PL |
412 | config = FcInitLoadConfig (); |
413 | if (!config) | |
414 | { | |
415 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); | |
416 | return 1; | |
417 | } | |
418 | FcConfigSetCurrent (config); | |
419 | ||
8f2a8078 PL |
420 | if (i >= argc) |
421 | usage (argv[0]); | |
422 | ||
12f46c42 PL |
423 | if (FcFileIsDir ((const FcChar8 *)argv[i])) |
424 | { | |
425 | char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i], | |
426 | (FcChar8 *)"/dummy"); | |
427 | if (!FcDirScanConfig (fs, dirs, 0, 0, | |
9769b43d | 428 | (const FcChar8 *)argv[i], FcFalse, config)) |
12f46c42 PL |
429 | fprintf (stderr, "couldn't load font dir %s\n", argv[i]); |
430 | else | |
431 | { | |
432 | /* sorry, we can't tell you where the cache file is. */ | |
433 | FcCachePrintSet (fs, dirs, dummy_name); | |
434 | FcStrFree ((FcChar8 *)dummy_name); | |
435 | } | |
436 | } | |
437 | else if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0) | |
c60ec7cc | 438 | FcCachePrintSet (fs, dirs, name_buf); |
6059daed PL |
439 | else |
440 | { | |
441 | FcStrSetDestroy (dirs); | |
442 | dirs = FcStrSetCreate (); | |
c60ec7cc | 443 | if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i])) |
6059daed PL |
444 | ; |
445 | } | |
f28f090d PL |
446 | |
447 | FcStrSetDestroy (dirs); | |
448 | FcFontSetDestroy (fs); | |
449 | ||
450 | return 0; | |
451 | } |