]>
git.wh0rd.org - fontconfig.git/blob - fc-cat/fc-cat.c
2 * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $
4 * Copyright © 2002 Keith Packard
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.
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.
29 #define HAVE_GETOPT_LONG 1
34 #include "../src/fccache.c"
35 #include "../fc-arch/fcarch.h"
39 #include <sys/types.h>
46 #ifndef HAVE_GETOPT_LONG
47 #define HAVE_GETOPT_LONG 0
54 const struct option longopts
[] = {
55 {"version", 0, 0, 'V'},
56 {"verbose", 0, 0, 'v'},
57 {"recurse", 0, 0, 'r'},
64 extern int optind
, opterr
, optopt
;
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.
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)
79 #define GETC(f) getc(f)
80 #define PUTC(c,f) putc(c,f)
84 FcCacheWriteChars (FILE *f
, const FcChar8
*chars
)
87 while ((c
= *chars
++))
92 if (PUTC ('\\', f
) == EOF
)
96 if (PUTC (c
, f
) == EOF
)
104 FcCacheWriteUlong (FILE *f
, unsigned long t
)
107 unsigned long temp
, digit
;
120 if (PUTC ((char) digit
+ '0', f
) == EOF
)
122 temp
= temp
- pow
* digit
;
129 FcCacheWriteInt (FILE *f
, int i
)
131 return FcCacheWriteUlong (f
, (unsigned long) i
);
135 FcCacheWriteStringOld (FILE *f
, const FcChar8
*string
)
138 if (PUTC ('"', f
) == EOF
)
140 if (!FcCacheWriteChars (f
, string
))
142 if (PUTC ('"', f
) == EOF
)
148 usage (char *program
)
151 fprintf (stderr
, "usage: %s [-V?] [--version] [--help] {*-%s.cache-2|directory}...\n",
152 program
, FC_ARCHITECTURE
);
154 fprintf (stderr
, "usage: %s [-fsvV?] {*-%s.cache-2|directory}...\n",
155 program
, FC_ARCHITECTURE
);
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");
160 fprintf (stderr
, "\n");
162 fprintf (stderr
, " -V, --version display font config version and exit\n");
163 fprintf (stderr
, " -?, --help display this help and exit\n");
165 fprintf (stderr
, " -V (version) display font config version and exit\n");
166 fprintf (stderr
, " -? (help) display this help and exit\n");
172 FcCacheFileOpen (char *cache_file
, off_t
*size
)
175 struct stat file_stat
;
177 fd
= open(cache_file
, O_RDONLY
| O_BINARY
);
181 if (fstat (fd
, &file_stat
) < 0) {
185 *size
= file_stat
.st_size
;
190 * return the path from the directory containing 'cache' to 'file'
193 static const FcChar8
*
194 FcFileBaseName (const char *cache
, const FcChar8
*file
)
196 const FcChar8
*cache_slash
;
197 int cache_len
= strlen (cache
);
199 if (!strncmp (cache
, file
, cache_len
) && file
[cache_len
] == '/')
200 return file
+ cache_len
+ 1;
205 FcCachePrintSet (FcFontSet
*set
, FcStrSet
*dirs
, char *base_name
, FcBool verbose
)
209 const FcChar8
*file
, *base
;
216 list
= FcStrListCreate (dirs
);
220 while ((dir
= FcStrListNext (list
)))
222 base
= FcFileBaseName (base_name
, dir
);
223 if (!FcCacheWriteStringOld (stdout
, base
))
225 if (PUTC (' ', stdout
) == EOF
)
227 if (!FcCacheWriteInt (stdout
, 0))
229 if (PUTC (' ', stdout
) == EOF
)
231 if (!FcCacheWriteStringOld (stdout
, FC_FONT_FILE_DIR
))
233 if (PUTC ('\n', stdout
) == EOF
)
238 for (n
= 0; n
< set
->nfont
; n
++)
240 FcPattern
**fonts
= FcFontSetFonts (set
);
241 FcPattern
*encoded_font
= fonts
[n
];
242 FcPattern
*font
= FcEncodedOffsetToPtr (set
, encoded_font
, FcPattern
);
244 if (FcPatternGetString (font
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
246 base
= FcFileBaseName (base_name
, file
);
247 if (FcPatternGetInteger (font
, FC_INDEX
, 0, &id
) != FcResultMatch
)
249 if (!FcCacheWriteStringOld (stdout
, base
))
251 if (PUTC (' ', stdout
) == EOF
)
253 if (!FcCacheWriteInt (stdout
, id
))
255 if (PUTC (' ', stdout
) == EOF
)
257 name
= FcNameUnparse (font
);
260 ret
= FcCacheWriteStringOld (stdout
, name
);
264 if (PUTC ('\n', stdout
) == EOF
)
267 if (verbose
&& !set
->nfont
&& !ndir
)
268 printf ("<empty>\n");
270 FcStrListDone (list
);
275 FcStrListDone (list
);
281 FcCacheFileMap (const FcChar8
*file
)
285 struct stat file_stat
;
287 fd
= open (file
, O_RDONLY
| O_BINARY
);
290 if (fstat (fd
, &file_stat
) < 0) {
294 if (!FcDirCacheLoad (fd
, file_stat
.st_size
, &cache
)) {
303 main (int argc
, char **argv
)
309 FcStrSet
*args
= NULL
;
316 FcBool first
= FcTrue
;
317 #if HAVE_GETOPT_LONG || HAVE_GETOPT
321 while ((c
= getopt_long (argc
, argv
, "Vvr?", longopts
, NULL
)) != -1)
323 while ((c
= getopt (argc
, argv
, "Vvr?")) != -1)
328 fprintf (stderr
, "fontconfig version %d.%d.%d\n",
329 FC_MAJOR
, FC_MINOR
, FC_REVISION
);
346 config
= FcInitLoadConfig ();
349 fprintf (stderr
, "%s: Can't init font config library\n", argv
[0]);
352 FcConfigSetCurrent (config
);
354 args
= FcStrSetCreate ();
357 fprintf (stderr
, "%s: malloc failure\n", argv
[0]);
362 for (; i
< argc
; i
++)
364 if (!FcStrSetAdd (args
, argv
[i
]))
366 fprintf (stderr
, "%s: malloc failure\n", argv
[0]);
370 arglist
= FcStrListCreate (args
);
373 fprintf (stderr
, "%s: malloc failure\n", argv
[0]);
380 arglist
= FcConfigGetFontDirs (config
);
381 while ((arg
= FcStrListNext (arglist
)))
382 if (!FcStrSetAdd (args
, arg
))
384 fprintf (stderr
, "%s: malloc failure\n", argv
[0]);
387 FcStrListDone (arglist
);
389 arglist
= FcStrListCreate (args
);
392 fprintf (stderr
, "%s: malloc failure\n", argv
[0]);
396 while ((arg
= FcStrListNext (arglist
)))
400 intptr_t *cache_dirs
;
401 FcChar8
*cache_file
= NULL
;
403 if (FcFileIsDir (arg
))
404 cache
= FcDirCacheMap (arg
, config
, &cache_file
);
406 cache
= FcCacheFileMap (arg
);
409 perror ((char *) arg
);
414 dirs
= FcStrSetCreate ();
415 fs
= FcCacheSet (cache
);
416 cache_dirs
= FcCacheDirs (cache
);
417 for (j
= 0; j
< cache
->dirs_count
; j
++)
419 FcStrSetAdd (dirs
, FcOffsetToPtr (cache_dirs
,
423 FcStrSetAdd (args
, FcOffsetToPtr (cache_dirs
,
432 printf ("Directory: %s\nCache: %s\n--------\n",
433 FcCacheDir(cache
), cache_file
? cache_file
: arg
);
436 FcCachePrintSet (fs
, dirs
, FcCacheDir (cache
), verbose
);
438 FcStrSetDestroy (dirs
);
440 FcDirCacheUnmap (cache
);
442 FcStrFree (cache_file
);