]>
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 | ||
80 | static FcBool | |
81 | FcCacheWriteChars (FILE *f, const FcChar8 *chars) | |
82 | { | |
83 | FcChar8 c; | |
84 | while ((c = *chars++)) | |
85 | { | |
86 | switch (c) { | |
87 | case '"': | |
88 | case '\\': | |
89 | if (PUTC ('\\', f) == EOF) | |
90 | return FcFalse; | |
91 | /* fall through */ | |
92 | default: | |
93 | if (PUTC (c, f) == EOF) | |
94 | return FcFalse; | |
95 | } | |
96 | } | |
97 | return FcTrue; | |
98 | } | |
99 | ||
100 | static FcBool | |
101 | FcCacheWriteUlong (FILE *f, unsigned long t) | |
102 | { | |
103 | int pow; | |
104 | unsigned long temp, digit; | |
105 | ||
106 | temp = t; | |
107 | pow = 1; | |
108 | while (temp >= 10) | |
109 | { | |
110 | temp /= 10; | |
111 | pow *= 10; | |
112 | } | |
113 | temp = t; | |
114 | while (pow) | |
115 | { | |
116 | digit = temp / pow; | |
117 | if (PUTC ((char) digit + '0', f) == EOF) | |
118 | return FcFalse; | |
119 | temp = temp - pow * digit; | |
120 | pow = pow / 10; | |
121 | } | |
122 | return FcTrue; | |
123 | } | |
124 | ||
125 | static FcBool | |
126 | FcCacheWriteInt (FILE *f, int i) | |
127 | { | |
128 | return FcCacheWriteUlong (f, (unsigned long) i); | |
129 | } | |
130 | ||
131 | static FcBool | |
132 | FcCacheWriteStringOld (FILE *f, const FcChar8 *string) | |
133 | { | |
134 | ||
135 | if (PUTC ('"', f) == EOF) | |
136 | return FcFalse; | |
137 | if (!FcCacheWriteChars (f, string)) | |
138 | return FcFalse; | |
139 | if (PUTC ('"', f) == EOF) | |
140 | return FcFalse; | |
141 | return FcTrue; | |
142 | } | |
143 | ||
144 | static void | |
145 | usage (char *program) | |
146 | { | |
147 | #if HAVE_GETOPT_LONG | |
148 | fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n", | |
149 | program); | |
150 | #else | |
151 | fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n", | |
152 | program); | |
153 | #endif | |
154 | fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n"); | |
155 | fprintf (stderr, "\n"); | |
156 | #if HAVE_GETOPT_LONG | |
157 | fprintf (stderr, " -V, --version display font config version and exit\n"); | |
158 | fprintf (stderr, " -?, --help display this help and exit\n"); | |
159 | #else | |
160 | fprintf (stderr, " -V (version) display font config version and exit\n"); | |
161 | fprintf (stderr, " -? (help) display this help and exit\n"); | |
162 | #endif | |
163 | exit (1); | |
164 | } | |
165 | ||
166 | /* read serialized state from the cache file */ | |
167 | static FcBool | |
168 | FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file) | |
169 | { | |
170 | int fd; | |
171 | char * current_arch_machine_name; | |
172 | char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE]; | |
173 | off_t current_arch_start = 0; | |
174 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; | |
175 | ||
176 | if (!cache_file) | |
177 | goto bail; | |
178 | ||
179 | current_arch_machine_name = FcCacheMachineSignature(); | |
180 | fd = open(cache_file, O_RDONLY); | |
181 | if (fd == -1) | |
182 | goto bail; | |
183 | ||
184 | current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name); | |
185 | if (current_arch_start < 0) | |
186 | goto bail1; | |
187 | ||
188 | lseek (fd, current_arch_start, SEEK_SET); | |
189 | if (FcCacheReadString (fd, candidate_arch_machine_name, | |
190 | sizeof (candidate_arch_machine_name)) == 0) | |
191 | goto bail1; | |
192 | ||
193 | while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0) | |
194 | FcStrSetAdd (dirs, (FcChar8 *)subdirName); | |
195 | ||
196 | if (!FcDirCacheConsume (fd, set)) | |
197 | goto bail1; | |
198 | ||
199 | close(fd); | |
200 | free (cache_file); | |
201 | return FcTrue; | |
202 | ||
203 | bail1: | |
204 | close (fd); | |
205 | bail: | |
206 | free (cache_file); | |
207 | return FcFalse; | |
208 | } | |
209 | ||
210 | /* | |
211 | * return the path from the directory containing 'cache' to 'file' | |
212 | */ | |
213 | ||
214 | static const FcChar8 * | |
215 | FcFileBaseName (const char *cache, const FcChar8 *file) | |
216 | { | |
217 | const FcChar8 *cache_slash; | |
218 | ||
219 | cache_slash = FcStrLastSlash ((const FcChar8 *)cache); | |
220 | if (cache_slash && !strncmp ((const char *) cache, (const char *) file, | |
221 | (cache_slash + 1) - (const FcChar8 *)cache)) | |
222 | return file + ((cache_slash + 1) - (const FcChar8 *)cache); | |
223 | return file; | |
224 | } | |
225 | ||
226 | FcBool | |
227 | FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file) | |
228 | { | |
229 | FcPattern *font; | |
230 | FcChar8 *name, *dir; | |
231 | const FcChar8 *file, *base; | |
232 | int n; | |
233 | int id; | |
234 | FcBool ret; | |
235 | FcStrList *list; | |
236 | ||
237 | list = FcStrListCreate (dirs); | |
238 | if (!list) | |
239 | goto bail2; | |
240 | ||
241 | while ((dir = FcStrListNext (list))) | |
242 | { | |
243 | base = FcFileBaseName (cache_file, dir); | |
244 | if (!FcCacheWriteStringOld (stdout, base)) | |
245 | goto bail3; | |
246 | if (PUTC (' ', stdout) == EOF) | |
247 | goto bail3; | |
248 | if (!FcCacheWriteInt (stdout, 0)) | |
249 | goto bail3; | |
250 | if (PUTC (' ', stdout) == EOF) | |
251 | goto bail3; | |
252 | if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR)) | |
253 | goto bail3; | |
254 | if (PUTC ('\n', stdout) == EOF) | |
255 | goto bail3; | |
256 | } | |
257 | ||
258 | for (n = 0; n < set->nfont; n++) | |
259 | { | |
260 | font = set->fonts[n]; | |
261 | if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch) | |
262 | goto bail3; | |
263 | base = FcFileBaseName (cache_file, file); | |
264 | if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch) | |
265 | goto bail3; | |
266 | if (FcDebug () & FC_DBG_CACHEV) | |
267 | printf (" write file \"%s\"\n", base); | |
268 | if (!FcCacheWriteStringOld (stdout, base)) | |
269 | goto bail3; | |
270 | if (PUTC (' ', stdout) == EOF) | |
271 | goto bail3; | |
272 | if (!FcCacheWriteInt (stdout, id)) | |
273 | goto bail3; | |
274 | if (PUTC (' ', stdout) == EOF) | |
275 | goto bail3; | |
276 | name = FcNameUnparse (font); | |
277 | if (!name) | |
278 | goto bail3; | |
279 | ret = FcCacheWriteStringOld (stdout, name); | |
280 | FcStrFree (name); | |
281 | if (!ret) | |
282 | goto bail3; | |
283 | if (PUTC ('\n', stdout) == EOF) | |
284 | goto bail3; | |
285 | } | |
286 | ||
287 | FcStrListDone (list); | |
288 | ||
289 | return FcTrue; | |
290 | ||
291 | bail3: | |
292 | FcStrListDone (list); | |
293 | bail2: | |
294 | bail1: | |
295 | bail0: | |
296 | return FcFalse; | |
297 | } | |
298 | ||
299 | int | |
300 | main (int argc, char **argv) | |
301 | { | |
302 | int i; | |
303 | int ret; | |
304 | #if HAVE_GETOPT_LONG || HAVE_GETOPT | |
305 | int c; | |
306 | FcFontSet *fs = FcFontSetCreate(); | |
307 | FcStrSet *dirs = FcStrSetCreate(); | |
308 | ||
309 | #if HAVE_GETOPT_LONG | |
310 | while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1) | |
311 | #else | |
312 | while ((c = getopt (argc, argv, "fsVv?")) != -1) | |
313 | #endif | |
314 | { | |
315 | switch (c) { | |
316 | case 'V': | |
317 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
318 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
319 | exit (0); | |
320 | default: | |
321 | usage (argv[0]); | |
322 | } | |
323 | } | |
324 | i = optind; | |
325 | #else | |
326 | i = 1; | |
327 | #endif | |
328 | ||
329 | if (FcCacheFileRead (fs, dirs, argv[i])) | |
330 | FcCachePrintSet (fs, dirs, argv[i]); | |
331 | ||
332 | FcStrSetDestroy (dirs); | |
333 | FcFontSetDestroy (fs); | |
334 | ||
335 | return 0; | |
336 | } |