]> git.wh0rd.org - fontconfig.git/blob - fc-cat/fc-cat.c
Migrate cache files from fonts directories to /var/cache/fontconfig. This
[fontconfig.git] / fc-cat / fc-cat.c
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 <libgen.h>
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
81 FcBool
82 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file);
83
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 static FcBool
171 FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
172 {
173 char name_buf[8192];
174 int fd;
175 char * current_arch_machine_name;
176 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
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 FcTrue);
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 {
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 if (!FcDirCacheConsume (fd, dir, set))
206 goto bail1;
207
208 FcCachePrintSet (set, dirs, name_buf);
209
210 FcFontSetDestroy (set);
211 set = FcFontSetCreate();
212 }
213
214 bail1:
215 close (fd);
216 bail:
217 return FcFalse;
218 }
219
220 /* read serialized state from the cache file */
221 static FcBool
222 FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
223 {
224 int fd;
225 char * current_arch_machine_name;
226 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
227 off_t current_arch_start = 0;
228 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
229 char name_buf[8192];
230
231 if (!cache_file)
232 goto bail;
233
234 current_arch_machine_name = FcCacheMachineSignature();
235 fd = open(cache_file, O_RDONLY);
236 if (fd == -1)
237 goto bail;
238
239 FcCacheReadString (fd, name_buf, sizeof (name_buf));
240 if (!strlen (name_buf))
241 goto bail;
242 printf ("fc-cat: printing directory cache contents for dir %s\n",
243 name_buf);
244
245 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name,
246 FcFalse);
247 if (current_arch_start < 0)
248 goto bail1;
249
250 lseek (fd, current_arch_start, SEEK_SET);
251 if (FcCacheReadString (fd, candidate_arch_machine_name,
252 sizeof (candidate_arch_machine_name)) == 0)
253 goto bail1;
254
255 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
256 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
257
258 if (!FcDirCacheConsume (fd, dir, set))
259 goto bail1;
260
261 close(fd);
262 return FcTrue;
263
264 bail1:
265 close (fd);
266 bail:
267 return FcFalse;
268 }
269
270 /*
271 * return the path from the directory containing 'cache' to 'file'
272 */
273
274 static const FcChar8 *
275 FcFileBaseName (const char *cache, const FcChar8 *file)
276 {
277 const FcChar8 *cache_slash;
278
279 cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
280 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
281 (cache_slash + 1) - (const FcChar8 *)cache))
282 return file + ((cache_slash + 1) - (const FcChar8 *)cache);
283 return file;
284 }
285
286 FcBool
287 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
288 {
289 FcPattern *font;
290 FcChar8 *name, *dir;
291 const FcChar8 *file, *base;
292 int ret;
293 int n;
294 int id;
295 FcStrList *list;
296
297 list = FcStrListCreate (dirs);
298 if (!list)
299 goto bail2;
300
301 while ((dir = FcStrListNext (list)))
302 {
303 base = FcFileBaseName (cache_file, dir);
304 if (!FcCacheWriteStringOld (stdout, base))
305 goto bail3;
306 if (PUTC (' ', stdout) == EOF)
307 goto bail3;
308 if (!FcCacheWriteInt (stdout, 0))
309 goto bail3;
310 if (PUTC (' ', stdout) == EOF)
311 goto bail3;
312 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
313 goto bail3;
314 if (PUTC ('\n', stdout) == EOF)
315 goto bail3;
316 }
317
318 for (n = 0; n < set->nfont; n++)
319 {
320 font = set->fonts[n];
321 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
322 goto bail3;
323 base = FcFileBaseName (cache_file, file);
324 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
325 goto bail3;
326 if (FcDebug () & FC_DBG_CACHEV)
327 printf (" write file \"%s\"\n", base);
328 if (!FcCacheWriteStringOld (stdout, base))
329 goto bail3;
330 if (PUTC (' ', stdout) == EOF)
331 goto bail3;
332 if (!FcCacheWriteInt (stdout, id))
333 goto bail3;
334 if (PUTC (' ', stdout) == EOF)
335 goto bail3;
336 name = FcNameUnparse (font);
337 if (!name)
338 goto bail3;
339 ret = FcCacheWriteStringOld (stdout, name);
340 FcStrFree (name);
341 if (!ret)
342 goto bail3;
343 if (PUTC ('\n', stdout) == EOF)
344 goto bail3;
345 }
346
347 FcStrListDone (list);
348
349 return FcTrue;
350
351 bail3:
352 FcStrListDone (list);
353 bail2:
354 return FcFalse;
355 }
356
357 int
358 main (int argc, char **argv)
359 {
360 int i;
361 #if HAVE_GETOPT_LONG || HAVE_GETOPT
362 int c;
363 FcFontSet *fs = FcFontSetCreate();
364 FcStrSet *dirs = FcStrSetCreate();
365
366 #if HAVE_GETOPT_LONG
367 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
368 #else
369 while ((c = getopt (argc, argv, "fsVv?")) != -1)
370 #endif
371 {
372 switch (c) {
373 case 'V':
374 fprintf (stderr, "fontconfig version %d.%d.%d\n",
375 FC_MAJOR, FC_MINOR, FC_REVISION);
376 exit (0);
377 default:
378 usage (argv[0]);
379 }
380 }
381 i = optind;
382 #else
383 i = 1;
384 #endif
385
386 if (i >= argc)
387 usage (argv[0]);
388
389 if (FcCacheFileRead (fs, dirs, dirname (strdup(argv[i])), argv[i]))
390 FcCachePrintSet (fs, dirs, argv[i]);
391 else
392 {
393 FcStrSetDestroy (dirs);
394 dirs = FcStrSetCreate ();
395 if (FcCacheGlobalFileReadAndPrint (fs, dirs, dirname (strdup (argv[i])),
396 argv[i]))
397 ;
398 }
399
400 FcStrSetDestroy (dirs);
401 FcFontSetDestroy (fs);
402
403 return 0;
404 }