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