]>
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'}, | |
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 | ||
170 | /* read serialized state from the cache file */ | |
c60ec7cc PL |
171 | static char * |
172 | FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file) | |
f28f090d | 173 | { |
af180c40 | 174 | FILE *file; |
f28f090d | 175 | int fd; |
f28f090d | 176 | char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1]; |
af180c40 | 177 | static char name_buf[8192]; |
c60ec7cc | 178 | FcChar8 * ls; |
04f7d3e7 | 179 | char * buf; |
af180c40 | 180 | int i; |
f28f090d PL |
181 | |
182 | if (!cache_file) | |
183 | goto bail; | |
184 | ||
af180c40 KP |
185 | file = fopen(cache_file, "rb"); |
186 | if (file == NULL) | |
f28f090d PL |
187 | goto bail; |
188 | ||
af180c40 KP |
189 | if (!FcDirCacheConsume (file, set, dirs, NULL, name_buf)) |
190 | goto bail1; | |
191 | ||
192 | fclose (file); | |
193 | ||
c60ec7cc | 194 | printf ("fc-cat: printing directory cache for cache which would be named %s\n", |
ea44e218 PL |
195 | name_buf); |
196 | ||
c60ec7cc PL |
197 | return name_buf; |
198 | ||
f28f090d | 199 | bail1: |
af180c40 | 200 | fclose (file); |
f28f090d | 201 | bail: |
c60ec7cc | 202 | return 0; |
f28f090d PL |
203 | } |
204 | ||
205 | /* | |
206 | * return the path from the directory containing 'cache' to 'file' | |
207 | */ | |
208 | ||
209 | static const FcChar8 * | |
210 | FcFileBaseName (const char *cache, const FcChar8 *file) | |
211 | { | |
212 | const FcChar8 *cache_slash; | |
213 | ||
214 | cache_slash = FcStrLastSlash ((const FcChar8 *)cache); | |
215 | if (cache_slash && !strncmp ((const char *) cache, (const char *) file, | |
216 | (cache_slash + 1) - (const FcChar8 *)cache)) | |
217 | return file + ((cache_slash + 1) - (const FcChar8 *)cache); | |
218 | return file; | |
219 | } | |
220 | ||
221 | FcBool | |
c60ec7cc | 222 | FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name) |
f28f090d PL |
223 | { |
224 | FcPattern *font; | |
225 | FcChar8 *name, *dir; | |
226 | const FcChar8 *file, *base; | |
d2f45978 | 227 | int ret; |
f28f090d PL |
228 | int n; |
229 | int id; | |
f28f090d PL |
230 | FcStrList *list; |
231 | ||
232 | list = FcStrListCreate (dirs); | |
233 | if (!list) | |
234 | goto bail2; | |
235 | ||
236 | while ((dir = FcStrListNext (list))) | |
237 | { | |
c60ec7cc | 238 | base = FcFileBaseName (base_name, dir); |
f28f090d PL |
239 | if (!FcCacheWriteStringOld (stdout, base)) |
240 | goto bail3; | |
241 | if (PUTC (' ', stdout) == EOF) | |
242 | goto bail3; | |
243 | if (!FcCacheWriteInt (stdout, 0)) | |
244 | goto bail3; | |
245 | if (PUTC (' ', stdout) == EOF) | |
246 | goto bail3; | |
247 | if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR)) | |
248 | goto bail3; | |
249 | if (PUTC ('\n', stdout) == EOF) | |
250 | goto bail3; | |
251 | } | |
252 | ||
253 | for (n = 0; n < set->nfont; n++) | |
254 | { | |
255 | font = set->fonts[n]; | |
256 | if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch) | |
257 | goto bail3; | |
c60ec7cc | 258 | base = FcFileBaseName (base_name, file); |
f28f090d PL |
259 | if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch) |
260 | goto bail3; | |
261 | if (FcDebug () & FC_DBG_CACHEV) | |
262 | printf (" write file \"%s\"\n", base); | |
263 | if (!FcCacheWriteStringOld (stdout, base)) | |
264 | goto bail3; | |
265 | if (PUTC (' ', stdout) == EOF) | |
266 | goto bail3; | |
267 | if (!FcCacheWriteInt (stdout, id)) | |
268 | goto bail3; | |
269 | if (PUTC (' ', stdout) == EOF) | |
270 | goto bail3; | |
271 | name = FcNameUnparse (font); | |
272 | if (!name) | |
273 | goto bail3; | |
274 | ret = FcCacheWriteStringOld (stdout, name); | |
275 | FcStrFree (name); | |
276 | if (!ret) | |
277 | goto bail3; | |
278 | if (PUTC ('\n', stdout) == EOF) | |
279 | goto bail3; | |
280 | } | |
281 | ||
282 | FcStrListDone (list); | |
283 | ||
284 | return FcTrue; | |
285 | ||
286 | bail3: | |
287 | FcStrListDone (list); | |
288 | bail2: | |
f28f090d PL |
289 | return FcFalse; |
290 | } | |
291 | ||
292 | int | |
293 | main (int argc, char **argv) | |
294 | { | |
295 | int i; | |
f28f090d PL |
296 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
297 | int c; | |
298 | FcFontSet *fs = FcFontSetCreate(); | |
299 | FcStrSet *dirs = FcStrSetCreate(); | |
c60ec7cc | 300 | char *name_buf; |
9769b43d | 301 | FcConfig *config; |
f28f090d PL |
302 | |
303 | #if HAVE_GETOPT_LONG | |
304 | while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1) | |
305 | #else | |
306 | while ((c = getopt (argc, argv, "fsVv?")) != -1) | |
307 | #endif | |
308 | { | |
309 | switch (c) { | |
310 | case 'V': | |
311 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
312 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
313 | exit (0); | |
314 | default: | |
315 | usage (argv[0]); | |
316 | } | |
317 | } | |
318 | i = optind; | |
319 | #else | |
320 | i = 1; | |
321 | #endif | |
322 | ||
9769b43d PL |
323 | config = FcInitLoadConfig (); |
324 | if (!config) | |
325 | { | |
326 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); | |
327 | return 1; | |
328 | } | |
329 | FcConfigSetCurrent (config); | |
330 | ||
8f2a8078 PL |
331 | if (i >= argc) |
332 | usage (argv[0]); | |
333 | ||
12f46c42 PL |
334 | if (FcFileIsDir ((const FcChar8 *)argv[i])) |
335 | { | |
336 | char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i], | |
337 | (FcChar8 *)"/dummy"); | |
af180c40 | 338 | if (!FcDirScanConfig (fs, dirs, 0, |
9769b43d | 339 | (const FcChar8 *)argv[i], FcFalse, config)) |
12f46c42 PL |
340 | fprintf (stderr, "couldn't load font dir %s\n", argv[i]); |
341 | else | |
342 | { | |
343 | /* sorry, we can't tell you where the cache file is. */ | |
344 | FcCachePrintSet (fs, dirs, dummy_name); | |
345 | FcStrFree ((FcChar8 *)dummy_name); | |
346 | } | |
347 | } | |
348 | else if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0) | |
c60ec7cc | 349 | FcCachePrintSet (fs, dirs, name_buf); |
6059daed PL |
350 | else |
351 | { | |
af180c40 | 352 | printf ("nothing to do\n"); |
6059daed | 353 | } |
f28f090d PL |
354 | |
355 | FcStrSetDestroy (dirs); | |
356 | FcFontSetDestroy (fs); | |
357 | ||
358 | return 0; | |
359 | } |