]> git.wh0rd.org - fontconfig.git/blame - fc-cat/fc-cat.c
Remove stale architecture signatures.
[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
d8ab9e6c 34#include "../src/fccache.c"
0a87ce71 35#include "../fc-arch/fcarch.h"
f045376c
PL
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'},
c0288648 56 {"verbose", 0, 0, 'v'},
0a87ce71 57 {"recurse", 0, 0, 'r'},
f28f090d
PL
58 {"help", 0, 0, '?'},
59 {NULL,0,0,0},
60};
61#else
62#if HAVE_GETOPT
63extern char *optarg;
64extern int optind, opterr, optopt;
65#endif
66#endif
67
68/*
69 * POSIX has broken stdio so that getc must do thread-safe locking,
70 * this is a serious performance problem for applications doing large
71 * amounts of IO with getc (as is done here). If available, use
72 * the getc_unlocked varient instead.
73 */
74
75#if defined(getc_unlocked) || defined(_IO_getc_unlocked)
76#define GETC(f) getc_unlocked(f)
77#define PUTC(c,f) putc_unlocked(c,f)
78#else
79#define GETC(f) getc(f)
80#define PUTC(c,f) putc(c,f)
81#endif
82
83static FcBool
84FcCacheWriteChars (FILE *f, const FcChar8 *chars)
85{
86 FcChar8 c;
87 while ((c = *chars++))
88 {
89 switch (c) {
90 case '"':
91 case '\\':
92 if (PUTC ('\\', f) == EOF)
93 return FcFalse;
94 /* fall through */
95 default:
96 if (PUTC (c, f) == EOF)
97 return FcFalse;
98 }
99 }
100 return FcTrue;
101}
102
103static FcBool
104FcCacheWriteUlong (FILE *f, unsigned long t)
105{
106 int pow;
107 unsigned long temp, digit;
108
109 temp = t;
110 pow = 1;
111 while (temp >= 10)
112 {
113 temp /= 10;
114 pow *= 10;
115 }
116 temp = t;
117 while (pow)
118 {
119 digit = temp / pow;
120 if (PUTC ((char) digit + '0', f) == EOF)
121 return FcFalse;
122 temp = temp - pow * digit;
123 pow = pow / 10;
124 }
125 return FcTrue;
126}
127
128static FcBool
129FcCacheWriteInt (FILE *f, int i)
130{
131 return FcCacheWriteUlong (f, (unsigned long) i);
132}
133
134static FcBool
135FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
136{
137
138 if (PUTC ('"', f) == EOF)
139 return FcFalse;
140 if (!FcCacheWriteChars (f, string))
141 return FcFalse;
142 if (PUTC ('"', f) == EOF)
143 return FcFalse;
144 return FcTrue;
145}
146
147static void
148usage (char *program)
149{
150#if HAVE_GETOPT_LONG
0a87ce71
KP
151 fprintf (stderr, "usage: %s [-V?] [--version] [--help] {*-%s.cache-2|directory}...\n",
152 program, FC_ARCHITECTURE);
f28f090d 153#else
0a87ce71
KP
154 fprintf (stderr, "usage: %s [-fsvV?] {*-%s.cache-2|directory}...\n",
155 program, FC_ARCHITECTURE);
f28f090d 156#endif
0a87ce71
KP
157 fprintf (stderr, "Reads font information cache from:\n");
158 fprintf (stderr, " 1) specified fontconfig cache file\n");
159 fprintf (stderr, " 2) related to a particular font directory\n");
f28f090d
PL
160 fprintf (stderr, "\n");
161#if HAVE_GETOPT_LONG
162 fprintf (stderr, " -V, --version display font config version and exit\n");
163 fprintf (stderr, " -?, --help display this help and exit\n");
164#else
165 fprintf (stderr, " -V (version) display font config version and exit\n");
166 fprintf (stderr, " -? (help) display this help and exit\n");
167#endif
168 exit (1);
169}
170
c0288648
KP
171static int
172FcCacheFileOpen (char *cache_file, off_t *size)
f28f090d
PL
173{
174 int fd;
c0288648 175 struct stat file_stat;
f28f090d 176
c0288648
KP
177 fd = open(cache_file, O_RDONLY | O_BINARY);
178 if (fd < 0)
179 return -1;
f28f090d 180
c0288648
KP
181 if (fstat (fd, &file_stat) < 0) {
182 close (fd);
183 return -1;
184 }
185 *size = file_stat.st_size;
186 return fd;
f28f090d
PL
187}
188
189/*
190 * return the path from the directory containing 'cache' to 'file'
191 */
192
193static const FcChar8 *
194FcFileBaseName (const char *cache, const FcChar8 *file)
195{
196 const FcChar8 *cache_slash;
c0288648 197 int cache_len = strlen (cache);
f28f090d 198
c0288648
KP
199 if (!strncmp (cache, file, cache_len) && file[cache_len] == '/')
200 return file + cache_len + 1;
f28f090d
PL
201 return file;
202}
203
204FcBool
0a87ce71 205FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name, FcBool verbose)
f28f090d
PL
206{
207 FcPattern *font;
208 FcChar8 *name, *dir;
209 const FcChar8 *file, *base;
d2f45978 210 int ret;
f28f090d
PL
211 int n;
212 int id;
0a87ce71 213 int ndir = 0;
f28f090d
PL
214 FcStrList *list;
215
216 list = FcStrListCreate (dirs);
217 if (!list)
218 goto bail2;
219
220 while ((dir = FcStrListNext (list)))
221 {
c60ec7cc 222 base = FcFileBaseName (base_name, dir);
f28f090d
PL
223 if (!FcCacheWriteStringOld (stdout, base))
224 goto bail3;
225 if (PUTC (' ', stdout) == EOF)
226 goto bail3;
227 if (!FcCacheWriteInt (stdout, 0))
228 goto bail3;
229 if (PUTC (' ', stdout) == EOF)
230 goto bail3;
231 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
232 goto bail3;
233 if (PUTC ('\n', stdout) == EOF)
234 goto bail3;
0a87ce71 235 ndir++;
f28f090d
PL
236 }
237
238 for (n = 0; n < set->nfont; n++)
239 {
c0288648
KP
240 FcPattern **fonts = FcFontSetFonts (set);
241 FcPattern *encoded_font = fonts[n];
242 FcPattern *font = FcEncodedOffsetToPtr (set, encoded_font, FcPattern);
243
f28f090d
PL
244 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
245 goto bail3;
c60ec7cc 246 base = FcFileBaseName (base_name, file);
f28f090d
PL
247 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
248 goto bail3;
f28f090d
PL
249 if (!FcCacheWriteStringOld (stdout, base))
250 goto bail3;
251 if (PUTC (' ', stdout) == EOF)
252 goto bail3;
253 if (!FcCacheWriteInt (stdout, id))
254 goto bail3;
255 if (PUTC (' ', stdout) == EOF)
256 goto bail3;
257 name = FcNameUnparse (font);
258 if (!name)
259 goto bail3;
260 ret = FcCacheWriteStringOld (stdout, name);
261 FcStrFree (name);
262 if (!ret)
263 goto bail3;
264 if (PUTC ('\n', stdout) == EOF)
265 goto bail3;
266 }
0a87ce71
KP
267 if (verbose && !set->nfont && !ndir)
268 printf ("<empty>\n");
f28f090d
PL
269
270 FcStrListDone (list);
271
272 return FcTrue;
273
274bail3:
275 FcStrListDone (list);
276bail2:
f28f090d
PL
277 return FcFalse;
278}
279
76abb77f
KP
280FcCache *
281FcCacheFileMap (const FcChar8 *file)
282{
283 FcCache *cache;
284 int fd;
285 struct stat file_stat;
286
287 fd = open (file, O_RDONLY | O_BINARY);
288 if (fd < 0)
289 return NULL;
290 if (fstat (fd, &file_stat) < 0) {
291 close (fd);
292 return NULL;
293 }
0a87ce71 294 if (!FcDirCacheLoad (fd, file_stat.st_size, &cache)) {
76abb77f 295 close (fd);
0a87ce71 296 return NULL;
76abb77f
KP
297 }
298 close (fd);
0a87ce71 299 return cache;
76abb77f
KP
300}
301
f28f090d
PL
302int
303main (int argc, char **argv)
304{
305 int i;
c0288648
KP
306 int ret = 0;
307 FcFontSet *fs;
308 FcStrSet *dirs;
0a87ce71
KP
309 FcStrSet *args = NULL;
310 FcStrList *arglist;
c0288648
KP
311 FcCache *cache;
312 FcConfig *config;
0a87ce71 313 FcChar8 *arg;
c0288648 314 int verbose = 0;
0a87ce71
KP
315 int recurse = 0;
316 FcBool first = FcTrue;
f28f090d
PL
317#if HAVE_GETOPT_LONG || HAVE_GETOPT
318 int c;
f28f090d
PL
319
320#if HAVE_GETOPT_LONG
0a87ce71 321 while ((c = getopt_long (argc, argv, "Vvr?", longopts, NULL)) != -1)
f28f090d 322#else
0a87ce71 323 while ((c = getopt (argc, argv, "Vvr?")) != -1)
f28f090d
PL
324#endif
325 {
326 switch (c) {
327 case 'V':
328 fprintf (stderr, "fontconfig version %d.%d.%d\n",
329 FC_MAJOR, FC_MINOR, FC_REVISION);
330 exit (0);
c0288648
KP
331 case 'v':
332 verbose++;
333 break;
0a87ce71
KP
334 case 'r':
335 recurse++;
336 break;
f28f090d
PL
337 default:
338 usage (argv[0]);
339 }
340 }
341 i = optind;
342#else
343 i = 1;
344#endif
345
9769b43d
PL
346 config = FcInitLoadConfig ();
347 if (!config)
348 {
349 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
350 return 1;
351 }
352 FcConfigSetCurrent (config);
353
0a87ce71
KP
354 args = FcStrSetCreate ();
355 if (!args)
356 {
357 fprintf (stderr, "%s: malloc failure\n", argv[0]);
358 return 1;
359 }
360 if (i < argc)
361 {
362 for (; i < argc; i++)
363 {
364 if (!FcStrSetAdd (args, argv[i]))
365 {
366 fprintf (stderr, "%s: malloc failure\n", argv[0]);
367 return 1;
368 }
369 }
370 arglist = FcStrListCreate (args);
371 if (!arglist)
372 {
373 fprintf (stderr, "%s: malloc failure\n", argv[0]);
374 return 1;
375 }
376 }
377 else
378 {
379 recurse++;
380 arglist = FcConfigGetFontDirs (config);
381 while ((arg = FcStrListNext (arglist)))
382 if (!FcStrSetAdd (args, arg))
383 {
384 fprintf (stderr, "%s: malloc failure\n", argv[0]);
385 return 1;
386 }
387 FcStrListDone (arglist);
388 }
389 arglist = FcStrListCreate (args);
390 if (!arglist)
391 {
392 fprintf (stderr, "%s: malloc failure\n", argv[0]);
393 return 1;
394 }
8f2a8078 395
0a87ce71 396 while ((arg = FcStrListNext (arglist)))
6059daed 397 {
0a87ce71
KP
398 int j;
399 off_t size;
400 intptr_t *cache_dirs;
401 FcChar8 *cache_file = NULL;
c0288648 402
0a87ce71
KP
403 if (FcFileIsDir (arg))
404 cache = FcDirCacheMap (arg, config, &cache_file);
c0288648 405 else
0a87ce71 406 cache = FcCacheFileMap (arg);
76abb77f 407 if (!cache)
c0288648 408 {
0a87ce71 409 perror ((char *) arg);
c0288648
KP
410 ret++;
411 continue;
412 }
413
c0288648
KP
414 dirs = FcStrSetCreate ();
415 fs = FcCacheSet (cache);
416 cache_dirs = FcCacheDirs (cache);
417 for (j = 0; j < cache->dirs_count; j++)
0a87ce71 418 {
c0288648
KP
419 FcStrSetAdd (dirs, FcOffsetToPtr (cache_dirs,
420 cache_dirs[j],
421 FcChar8));
0a87ce71
KP
422 if (recurse)
423 FcStrSetAdd (args, FcOffsetToPtr (cache_dirs,
424 cache_dirs[j],
425 FcChar8));
426 }
c0288648
KP
427
428 if (verbose)
0a87ce71
KP
429 {
430 if (!first)
431 printf ("\n");
432 printf ("Directory: %s\nCache: %s\n--------\n",
433 FcCacheDir(cache), cache_file ? cache_file : arg);
434 first = FcFalse;
435 }
436 FcCachePrintSet (fs, dirs, FcCacheDir (cache), verbose);
f28f090d 437
c0288648
KP
438 FcStrSetDestroy (dirs);
439
440 FcDirCacheUnmap (cache);
0a87ce71
KP
441 if (cache_file)
442 FcStrFree (cache_file);
c0288648 443 }
f28f090d
PL
444
445 return 0;
446}