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