]>
Commit | Line | Data |
---|---|---|
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 | ||
80 | FcBool | |
81 | FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name); | |
82 | ||
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 | ||
169 | static FcBool | |
170 | FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char *cache_file) | |
171 | { | |
172 | char name_buf[8192]; | |
173 | int fd; | |
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; | |
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 | ||
187 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); | |
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 | { | |
198 | char * dir; | |
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 | ||
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 | ||
214 | if (!FcDirCacheConsume (fd, name_buf, set, 0)) | |
215 | goto bail1; | |
216 | ||
217 | dir = malloc (strlen (name_buf) + 2); | |
218 | if (!dir) | |
219 | goto bail1; | |
220 | ||
221 | strcpy (dir, name_buf); | |
222 | strcat (dir, "/"); | |
223 | ||
224 | FcCachePrintSet (set, dirs, dir); | |
225 | free (dir); | |
226 | ||
227 | FcFontSetDestroy (set); | |
228 | set = FcFontSetCreate(); | |
229 | } | |
230 | ||
231 | bail1: | |
232 | close (fd); | |
233 | bail: | |
234 | return FcFalse; | |
235 | } | |
236 | ||
237 | /* read serialized state from the cache file */ | |
238 | static char * | |
239 | FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file) | |
240 | { | |
241 | int fd; | |
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; | |
246 | FcChar8 * ls; | |
247 | ||
248 | if (!cache_file) | |
249 | goto bail; | |
250 | ||
251 | current_arch_machine_name = FcCacheMachineSignature(); | |
252 | fd = open(cache_file, O_RDONLY); | |
253 | if (fd == -1) | |
254 | goto bail; | |
255 | ||
256 | FcCacheReadString (fd, name_buf, sizeof (name_buf)); | |
257 | if (!strlen (name_buf)) | |
258 | goto bail; | |
259 | if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) == 0) | |
260 | goto bail; | |
261 | printf ("fc-cat: printing directory cache for cache which would be named %s\n", | |
262 | name_buf); | |
263 | ||
264 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); | |
265 | if (current_arch_start < 0) | |
266 | goto bail1; | |
267 | ||
268 | while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0) | |
269 | FcStrSetAdd (dirs, (FcChar8 *)subdirName); | |
270 | ||
271 | dir = strdup(name_buf); | |
272 | ls = FcStrLastSlash ((FcChar8 *)dir); | |
273 | if (ls) | |
274 | *ls = 0; | |
275 | ||
276 | if (!FcDirCacheConsume (fd, dir, set, 0)) | |
277 | goto bail2; | |
278 | free (dir); | |
279 | ||
280 | close(fd); | |
281 | return name_buf; | |
282 | ||
283 | bail2: | |
284 | free (dir); | |
285 | ||
286 | bail1: | |
287 | close (fd); | |
288 | bail: | |
289 | return 0; | |
290 | } | |
291 | ||
292 | /* | |
293 | * return the path from the directory containing 'cache' to 'file' | |
294 | */ | |
295 | ||
296 | static const FcChar8 * | |
297 | FcFileBaseName (const char *cache, const FcChar8 *file) | |
298 | { | |
299 | const FcChar8 *cache_slash; | |
300 | ||
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); | |
305 | return file; | |
306 | } | |
307 | ||
308 | FcBool | |
309 | FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name) | |
310 | { | |
311 | FcPattern *font; | |
312 | FcChar8 *name, *dir; | |
313 | const FcChar8 *file, *base; | |
314 | int ret; | |
315 | int n; | |
316 | int id; | |
317 | FcStrList *list; | |
318 | ||
319 | list = FcStrListCreate (dirs); | |
320 | if (!list) | |
321 | goto bail2; | |
322 | ||
323 | while ((dir = FcStrListNext (list))) | |
324 | { | |
325 | base = FcFileBaseName (base_name, dir); | |
326 | if (!FcCacheWriteStringOld (stdout, base)) | |
327 | goto bail3; | |
328 | if (PUTC (' ', stdout) == EOF) | |
329 | goto bail3; | |
330 | if (!FcCacheWriteInt (stdout, 0)) | |
331 | goto bail3; | |
332 | if (PUTC (' ', stdout) == EOF) | |
333 | goto bail3; | |
334 | if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR)) | |
335 | goto bail3; | |
336 | if (PUTC ('\n', stdout) == EOF) | |
337 | goto bail3; | |
338 | } | |
339 | ||
340 | for (n = 0; n < set->nfont; n++) | |
341 | { | |
342 | font = set->fonts[n]; | |
343 | if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch) | |
344 | goto bail3; | |
345 | base = FcFileBaseName (base_name, file); | |
346 | if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch) | |
347 | goto bail3; | |
348 | if (FcDebug () & FC_DBG_CACHEV) | |
349 | printf (" write file \"%s\"\n", base); | |
350 | if (!FcCacheWriteStringOld (stdout, base)) | |
351 | goto bail3; | |
352 | if (PUTC (' ', stdout) == EOF) | |
353 | goto bail3; | |
354 | if (!FcCacheWriteInt (stdout, id)) | |
355 | goto bail3; | |
356 | if (PUTC (' ', stdout) == EOF) | |
357 | goto bail3; | |
358 | name = FcNameUnparse (font); | |
359 | if (!name) | |
360 | goto bail3; | |
361 | ret = FcCacheWriteStringOld (stdout, name); | |
362 | FcStrFree (name); | |
363 | if (!ret) | |
364 | goto bail3; | |
365 | if (PUTC ('\n', stdout) == EOF) | |
366 | goto bail3; | |
367 | } | |
368 | ||
369 | FcStrListDone (list); | |
370 | ||
371 | return FcTrue; | |
372 | ||
373 | bail3: | |
374 | FcStrListDone (list); | |
375 | bail2: | |
376 | return FcFalse; | |
377 | } | |
378 | ||
379 | int | |
380 | main (int argc, char **argv) | |
381 | { | |
382 | int i; | |
383 | #if HAVE_GETOPT_LONG || HAVE_GETOPT | |
384 | int c; | |
385 | FcFontSet *fs = FcFontSetCreate(); | |
386 | FcStrSet *dirs = FcStrSetCreate(); | |
387 | char *name_buf; | |
388 | FcConfig *config; | |
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 | ||
410 | config = FcInitLoadConfig (); | |
411 | if (!config) | |
412 | { | |
413 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); | |
414 | return 1; | |
415 | } | |
416 | FcConfigSetCurrent (config); | |
417 | ||
418 | if (i >= argc) | |
419 | usage (argv[0]); | |
420 | ||
421 | if (FcFileIsDir ((const FcChar8 *)argv[i])) | |
422 | { | |
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]); | |
428 | else | |
429 | { | |
430 | /* sorry, we can't tell you where the cache file is. */ | |
431 | FcCachePrintSet (fs, dirs, dummy_name); | |
432 | FcStrFree ((FcChar8 *)dummy_name); | |
433 | } | |
434 | } | |
435 | else if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0) | |
436 | FcCachePrintSet (fs, dirs, name_buf); | |
437 | else | |
438 | { | |
439 | FcStrSetDestroy (dirs); | |
440 | dirs = FcStrSetCreate (); | |
441 | if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i])) | |
442 | ; | |
443 | } | |
444 | ||
445 | FcStrSetDestroy (dirs); | |
446 | FcFontSetDestroy (fs); | |
447 | ||
448 | return 0; | |
449 | } |