]>
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> | |
e77c1718 | 30 | #include <libgen.h> |
f28f090d PL |
31 | #include <sys/types.h> |
32 | #include <sys/stat.h> | |
33 | #include <errno.h> | |
34 | #ifdef HAVE_CONFIG_H | |
35 | #include <config.h> | |
36 | #else | |
37 | #ifdef linux | |
38 | #define HAVE_GETOPT_LONG 1 | |
39 | #endif | |
40 | #define HAVE_GETOPT 1 | |
41 | #endif | |
42 | ||
43 | #ifndef HAVE_GETOPT | |
44 | #define HAVE_GETOPT 0 | |
45 | #endif | |
46 | #ifndef HAVE_GETOPT_LONG | |
47 | #define HAVE_GETOPT_LONG 0 | |
48 | #endif | |
49 | ||
50 | #if HAVE_GETOPT_LONG | |
51 | #undef _GNU_SOURCE | |
52 | #define _GNU_SOURCE | |
53 | #include <getopt.h> | |
54 | const struct option longopts[] = { | |
55 | {"version", 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 | ||
982b5982 | 81 | FcBool |
c60ec7cc | 82 | FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name); |
982b5982 | 83 | |
f28f090d PL |
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 | ||
6059daed | 170 | static FcBool |
c60ec7cc | 171 | FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char *cache_file) |
6059daed PL |
172 | { |
173 | char name_buf[8192]; | |
174 | int fd; | |
6059daed PL |
175 | char * current_arch_machine_name; |
176 | char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE]; | |
2c4e0124 | 177 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; |
6059daed | 178 | off_t current_arch_start = 0; |
6059daed PL |
179 | |
180 | if (!cache_file) | |
181 | goto bail; | |
182 | ||
183 | current_arch_machine_name = FcCacheMachineSignature(); | |
184 | fd = open(cache_file, O_RDONLY); | |
185 | if (fd == -1) | |
186 | goto bail; | |
187 | ||
c60ec7cc | 188 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); |
6059daed PL |
189 | if (current_arch_start < 0) |
190 | goto bail1; | |
191 | ||
192 | lseek (fd, current_arch_start, SEEK_SET); | |
193 | if (FcCacheReadString (fd, candidate_arch_machine_name, | |
194 | sizeof (candidate_arch_machine_name)) == 0) | |
195 | goto bail1; | |
196 | ||
197 | while (1) | |
198 | { | |
68355f38 | 199 | char * dir; |
6059daed PL |
200 | FcCacheReadString (fd, name_buf, sizeof (name_buf)); |
201 | if (!strlen(name_buf)) | |
202 | break; | |
203 | printf ("fc-cat: printing global cache contents for dir %s\n", | |
204 | name_buf); | |
205 | ||
2c4e0124 PL |
206 | do |
207 | { | |
208 | if (!FcCacheReadString (fd, subdirName, | |
209 | sizeof (subdirName)) || | |
210 | !strlen (subdirName)) | |
211 | break; | |
212 | /* then don't do anything with subdirName. */ | |
213 | } while (1); | |
214 | ||
cd9bca69 | 215 | if (!FcDirCacheConsume (fd, name_buf, set, 0)) |
6059daed PL |
216 | goto bail1; |
217 | ||
8a0b0ed6 PL |
218 | dir = malloc (strlen (name_buf) + 2); |
219 | if (!dir) | |
220 | goto bail1; | |
221 | ||
222 | strcpy (dir, name_buf); | |
c60ec7cc PL |
223 | strcat (dir, "/"); |
224 | ||
225 | FcCachePrintSet (set, dirs, dir); | |
226 | free (dir); | |
6059daed PL |
227 | |
228 | FcFontSetDestroy (set); | |
229 | set = FcFontSetCreate(); | |
230 | } | |
231 | ||
232 | bail1: | |
233 | close (fd); | |
234 | bail: | |
235 | return FcFalse; | |
236 | } | |
237 | ||
f28f090d | 238 | /* read serialized state from the cache file */ |
c60ec7cc PL |
239 | static char * |
240 | FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file) | |
f28f090d PL |
241 | { |
242 | int fd; | |
243 | char * current_arch_machine_name; | |
f28f090d PL |
244 | off_t current_arch_start = 0; |
245 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; | |
c60ec7cc PL |
246 | static char name_buf[8192], *dir; |
247 | FcChar8 * ls; | |
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 | ||
f28f090d PL |
269 | while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0) |
270 | FcStrSetAdd (dirs, (FcChar8 *)subdirName); | |
271 | ||
c60ec7cc PL |
272 | dir = strdup(name_buf); |
273 | ls = FcStrLastSlash ((FcChar8 *)dir); | |
274 | if (ls) | |
275 | *ls = 0; | |
276 | ||
cd9bca69 | 277 | if (!FcDirCacheConsume (fd, dir, set, 0)) |
c60ec7cc PL |
278 | goto bail2; |
279 | free (dir); | |
280 | ||
f28f090d | 281 | close(fd); |
c60ec7cc PL |
282 | return name_buf; |
283 | ||
284 | bail2: | |
285 | free (dir); | |
f28f090d PL |
286 | |
287 | bail1: | |
288 | close (fd); | |
289 | bail: | |
c60ec7cc | 290 | return 0; |
f28f090d PL |
291 | } |
292 | ||
293 | /* | |
294 | * return the path from the directory containing 'cache' to 'file' | |
295 | */ | |
296 | ||
297 | static const FcChar8 * | |
298 | FcFileBaseName (const char *cache, const FcChar8 *file) | |
299 | { | |
300 | const FcChar8 *cache_slash; | |
301 | ||
302 | cache_slash = FcStrLastSlash ((const FcChar8 *)cache); | |
303 | if (cache_slash && !strncmp ((const char *) cache, (const char *) file, | |
304 | (cache_slash + 1) - (const FcChar8 *)cache)) | |
305 | return file + ((cache_slash + 1) - (const FcChar8 *)cache); | |
306 | return file; | |
307 | } | |
308 | ||
309 | FcBool | |
c60ec7cc | 310 | FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name) |
f28f090d PL |
311 | { |
312 | FcPattern *font; | |
313 | FcChar8 *name, *dir; | |
314 | const FcChar8 *file, *base; | |
d2f45978 | 315 | int ret; |
f28f090d PL |
316 | int n; |
317 | int id; | |
f28f090d PL |
318 | FcStrList *list; |
319 | ||
320 | list = FcStrListCreate (dirs); | |
321 | if (!list) | |
322 | goto bail2; | |
323 | ||
324 | while ((dir = FcStrListNext (list))) | |
325 | { | |
c60ec7cc | 326 | base = FcFileBaseName (base_name, dir); |
f28f090d PL |
327 | if (!FcCacheWriteStringOld (stdout, base)) |
328 | goto bail3; | |
329 | if (PUTC (' ', stdout) == EOF) | |
330 | goto bail3; | |
331 | if (!FcCacheWriteInt (stdout, 0)) | |
332 | goto bail3; | |
333 | if (PUTC (' ', stdout) == EOF) | |
334 | goto bail3; | |
335 | if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR)) | |
336 | goto bail3; | |
337 | if (PUTC ('\n', stdout) == EOF) | |
338 | goto bail3; | |
339 | } | |
340 | ||
341 | for (n = 0; n < set->nfont; n++) | |
342 | { | |
343 | font = set->fonts[n]; | |
344 | if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch) | |
345 | goto bail3; | |
c60ec7cc | 346 | base = FcFileBaseName (base_name, file); |
f28f090d PL |
347 | if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch) |
348 | goto bail3; | |
349 | if (FcDebug () & FC_DBG_CACHEV) | |
350 | printf (" write file \"%s\"\n", base); | |
351 | if (!FcCacheWriteStringOld (stdout, base)) | |
352 | goto bail3; | |
353 | if (PUTC (' ', stdout) == EOF) | |
354 | goto bail3; | |
355 | if (!FcCacheWriteInt (stdout, id)) | |
356 | goto bail3; | |
357 | if (PUTC (' ', stdout) == EOF) | |
358 | goto bail3; | |
359 | name = FcNameUnparse (font); | |
360 | if (!name) | |
361 | goto bail3; | |
362 | ret = FcCacheWriteStringOld (stdout, name); | |
363 | FcStrFree (name); | |
364 | if (!ret) | |
365 | goto bail3; | |
366 | if (PUTC ('\n', stdout) == EOF) | |
367 | goto bail3; | |
368 | } | |
369 | ||
370 | FcStrListDone (list); | |
371 | ||
372 | return FcTrue; | |
373 | ||
374 | bail3: | |
375 | FcStrListDone (list); | |
376 | bail2: | |
f28f090d PL |
377 | return FcFalse; |
378 | } | |
379 | ||
380 | int | |
381 | main (int argc, char **argv) | |
382 | { | |
383 | int i; | |
f28f090d PL |
384 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
385 | int c; | |
386 | FcFontSet *fs = FcFontSetCreate(); | |
387 | FcStrSet *dirs = FcStrSetCreate(); | |
c60ec7cc | 388 | char *name_buf; |
f28f090d PL |
389 | |
390 | #if HAVE_GETOPT_LONG | |
391 | while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1) | |
392 | #else | |
393 | while ((c = getopt (argc, argv, "fsVv?")) != -1) | |
394 | #endif | |
395 | { | |
396 | switch (c) { | |
397 | case 'V': | |
398 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
399 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
400 | exit (0); | |
401 | default: | |
402 | usage (argv[0]); | |
403 | } | |
404 | } | |
405 | i = optind; | |
406 | #else | |
407 | i = 1; | |
408 | #endif | |
409 | ||
8f2a8078 PL |
410 | if (i >= argc) |
411 | usage (argv[0]); | |
412 | ||
68355f38 | 413 | if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0) |
c60ec7cc | 414 | FcCachePrintSet (fs, dirs, name_buf); |
6059daed PL |
415 | else |
416 | { | |
417 | FcStrSetDestroy (dirs); | |
418 | dirs = FcStrSetCreate (); | |
c60ec7cc | 419 | if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i])) |
6059daed PL |
420 | ; |
421 | } | |
f28f090d PL |
422 | |
423 | FcStrSetDestroy (dirs); | |
424 | FcFontSetDestroy (fs); | |
425 | ||
426 | return 0; | |
427 | } |