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