]> git.wh0rd.org - fontconfig.git/blame - fc-cat/fc-cat.c
Include $(top_srcdir), $(top_srcdir)/src before anything else.
[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
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>
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];
2c4e0124 177 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
6059daed 178 off_t current_arch_start = 0;
6059daed
PL
179
180 if (!cache_file)
181 goto bail;
182
183 current_arch_machine_name = FcCacheMachineSignature();
184 fd = open(cache_file, O_RDONLY);
185 if (fd == -1)
186 goto bail;
187
c60ec7cc 188 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
6059daed
PL
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 {
68355f38 199 char * dir;
6059daed
PL
200 FcCacheReadString (fd, name_buf, sizeof (name_buf));
201 if (!strlen(name_buf))
202 break;
203 printf ("fc-cat: printing global cache contents for dir %s\n",
204 name_buf);
205
2c4e0124
PL
206 do
207 {
208 if (!FcCacheReadString (fd, subdirName,
209 sizeof (subdirName)) ||
210 !strlen (subdirName))
211 break;
212 /* then don't do anything with subdirName. */
213 } while (1);
214
cd9bca69 215 if (!FcDirCacheConsume (fd, name_buf, set, 0))
6059daed
PL
216 goto bail1;
217
8a0b0ed6
PL
218 dir = malloc (strlen (name_buf) + 2);
219 if (!dir)
220 goto bail1;
221
222 strcpy (dir, name_buf);
c60ec7cc
PL
223 strcat (dir, "/");
224
225 FcCachePrintSet (set, dirs, dir);
226 free (dir);
6059daed
PL
227
228 FcFontSetDestroy (set);
229 set = FcFontSetCreate();
230 }
231
232 bail1:
233 close (fd);
234 bail:
235 return FcFalse;
236}
237
f28f090d 238/* read serialized state from the cache file */
c60ec7cc
PL
239static char *
240FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file)
f28f090d
PL
241{
242 int fd;
243 char * current_arch_machine_name;
f28f090d
PL
244 off_t current_arch_start = 0;
245 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
c60ec7cc
PL
246 static char name_buf[8192], *dir;
247 FcChar8 * ls;
04f7d3e7 248 char * buf;
f28f090d
PL
249
250 if (!cache_file)
251 goto bail;
252
253 current_arch_machine_name = FcCacheMachineSignature();
254 fd = open(cache_file, O_RDONLY);
255 if (fd == -1)
256 goto bail;
257
ea44e218
PL
258 FcCacheReadString (fd, name_buf, sizeof (name_buf));
259 if (!strlen (name_buf))
260 goto bail;
c60ec7cc
PL
261 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) == 0)
262 goto bail;
263 printf ("fc-cat: printing directory cache for cache which would be named %s\n",
ea44e218
PL
264 name_buf);
265
c60ec7cc 266 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
f28f090d
PL
267 if (current_arch_start < 0)
268 goto bail1;
269
04f7d3e7
PL
270 while ((buf = FcCacheReadString (fd, subdirName, sizeof (subdirName)))
271 && *buf)
f28f090d
PL
272 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
273
c60ec7cc
PL
274 dir = strdup(name_buf);
275 ls = FcStrLastSlash ((FcChar8 *)dir);
276 if (ls)
277 *ls = 0;
278
cd9bca69 279 if (!FcDirCacheConsume (fd, dir, set, 0))
c60ec7cc
PL
280 goto bail2;
281 free (dir);
282
f28f090d 283 close(fd);
c60ec7cc
PL
284 return name_buf;
285
286 bail2:
287 free (dir);
f28f090d
PL
288
289 bail1:
290 close (fd);
291 bail:
c60ec7cc 292 return 0;
f28f090d
PL
293}
294
295/*
296 * return the path from the directory containing 'cache' to 'file'
297 */
298
299static const FcChar8 *
300FcFileBaseName (const char *cache, const FcChar8 *file)
301{
302 const FcChar8 *cache_slash;
303
304 cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
305 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
306 (cache_slash + 1) - (const FcChar8 *)cache))
307 return file + ((cache_slash + 1) - (const FcChar8 *)cache);
308 return file;
309}
310
311FcBool
c60ec7cc 312FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
f28f090d
PL
313{
314 FcPattern *font;
315 FcChar8 *name, *dir;
316 const FcChar8 *file, *base;
d2f45978 317 int ret;
f28f090d
PL
318 int n;
319 int id;
f28f090d
PL
320 FcStrList *list;
321
322 list = FcStrListCreate (dirs);
323 if (!list)
324 goto bail2;
325
326 while ((dir = FcStrListNext (list)))
327 {
c60ec7cc 328 base = FcFileBaseName (base_name, dir);
f28f090d
PL
329 if (!FcCacheWriteStringOld (stdout, base))
330 goto bail3;
331 if (PUTC (' ', stdout) == EOF)
332 goto bail3;
333 if (!FcCacheWriteInt (stdout, 0))
334 goto bail3;
335 if (PUTC (' ', stdout) == EOF)
336 goto bail3;
337 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
338 goto bail3;
339 if (PUTC ('\n', stdout) == EOF)
340 goto bail3;
341 }
342
343 for (n = 0; n < set->nfont; n++)
344 {
345 font = set->fonts[n];
346 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
347 goto bail3;
c60ec7cc 348 base = FcFileBaseName (base_name, file);
f28f090d
PL
349 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
350 goto bail3;
351 if (FcDebug () & FC_DBG_CACHEV)
352 printf (" write file \"%s\"\n", base);
353 if (!FcCacheWriteStringOld (stdout, base))
354 goto bail3;
355 if (PUTC (' ', stdout) == EOF)
356 goto bail3;
357 if (!FcCacheWriteInt (stdout, id))
358 goto bail3;
359 if (PUTC (' ', stdout) == EOF)
360 goto bail3;
361 name = FcNameUnparse (font);
362 if (!name)
363 goto bail3;
364 ret = FcCacheWriteStringOld (stdout, name);
365 FcStrFree (name);
366 if (!ret)
367 goto bail3;
368 if (PUTC ('\n', stdout) == EOF)
369 goto bail3;
370 }
371
372 FcStrListDone (list);
373
374 return FcTrue;
375
376bail3:
377 FcStrListDone (list);
378bail2:
f28f090d
PL
379 return FcFalse;
380}
381
382int
383main (int argc, char **argv)
384{
385 int i;
f28f090d
PL
386#if HAVE_GETOPT_LONG || HAVE_GETOPT
387 int c;
388 FcFontSet *fs = FcFontSetCreate();
389 FcStrSet *dirs = FcStrSetCreate();
c60ec7cc 390 char *name_buf;
9769b43d 391 FcConfig *config;
f28f090d
PL
392
393#if HAVE_GETOPT_LONG
394 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
395#else
396 while ((c = getopt (argc, argv, "fsVv?")) != -1)
397#endif
398 {
399 switch (c) {
400 case 'V':
401 fprintf (stderr, "fontconfig version %d.%d.%d\n",
402 FC_MAJOR, FC_MINOR, FC_REVISION);
403 exit (0);
404 default:
405 usage (argv[0]);
406 }
407 }
408 i = optind;
409#else
410 i = 1;
411#endif
412
9769b43d
PL
413 config = FcInitLoadConfig ();
414 if (!config)
415 {
416 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
417 return 1;
418 }
419 FcConfigSetCurrent (config);
420
8f2a8078
PL
421 if (i >= argc)
422 usage (argv[0]);
423
12f46c42
PL
424 if (FcFileIsDir ((const FcChar8 *)argv[i]))
425 {
426 char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i],
427 (FcChar8 *)"/dummy");
428 if (!FcDirScanConfig (fs, dirs, 0, 0,
9769b43d 429 (const FcChar8 *)argv[i], FcFalse, config))
12f46c42
PL
430 fprintf (stderr, "couldn't load font dir %s\n", argv[i]);
431 else
432 {
433 /* sorry, we can't tell you where the cache file is. */
434 FcCachePrintSet (fs, dirs, dummy_name);
435 FcStrFree ((FcChar8 *)dummy_name);
436 }
437 }
438 else if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0)
c60ec7cc 439 FcCachePrintSet (fs, dirs, name_buf);
6059daed
PL
440 else
441 {
442 FcStrSetDestroy (dirs);
443 dirs = FcStrSetCreate ();
c60ec7cc 444 if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i]))
6059daed
PL
445 ;
446 }
f28f090d
PL
447
448 FcStrSetDestroy (dirs);
449 FcFontSetDestroy (fs);
450
451 return 0;
452}