]> git.wh0rd.org - fontconfig.git/blame - fc-cat/fc-cat.c
src/fccfg.c (FcConfigAppFontAddFile, FcConfigAppFontAddDir)
[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];
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 {
c60ec7cc 199 char * dir, * ls;
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;
244 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
245 off_t current_arch_start = 0;
246 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
c60ec7cc
PL
247 static char name_buf[8192], *dir;
248 FcChar8 * ls;
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
f28f090d
PL
270 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
271 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
272
c60ec7cc
PL
273 dir = strdup(name_buf);
274 ls = FcStrLastSlash ((FcChar8 *)dir);
275 if (ls)
276 *ls = 0;
277
cd9bca69 278 if (!FcDirCacheConsume (fd, dir, set, 0))
c60ec7cc
PL
279 goto bail2;
280 free (dir);
281
f28f090d 282 close(fd);
c60ec7cc
PL
283 return name_buf;
284
285 bail2:
286 free (dir);
f28f090d
PL
287
288 bail1:
289 close (fd);
290 bail:
c60ec7cc 291 return 0;
f28f090d
PL
292}
293
294/*
295 * return the path from the directory containing 'cache' to 'file'
296 */
297
298static const FcChar8 *
299FcFileBaseName (const char *cache, const FcChar8 *file)
300{
301 const FcChar8 *cache_slash;
302
303 cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
304 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
305 (cache_slash + 1) - (const FcChar8 *)cache))
306 return file + ((cache_slash + 1) - (const FcChar8 *)cache);
307 return file;
308}
309
310FcBool
c60ec7cc 311FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
f28f090d
PL
312{
313 FcPattern *font;
314 FcChar8 *name, *dir;
315 const FcChar8 *file, *base;
d2f45978 316 int ret;
f28f090d
PL
317 int n;
318 int id;
f28f090d
PL
319 FcStrList *list;
320
321 list = FcStrListCreate (dirs);
322 if (!list)
323 goto bail2;
324
325 while ((dir = FcStrListNext (list)))
326 {
c60ec7cc 327 base = FcFileBaseName (base_name, dir);
f28f090d
PL
328 if (!FcCacheWriteStringOld (stdout, base))
329 goto bail3;
330 if (PUTC (' ', stdout) == EOF)
331 goto bail3;
332 if (!FcCacheWriteInt (stdout, 0))
333 goto bail3;
334 if (PUTC (' ', stdout) == EOF)
335 goto bail3;
336 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
337 goto bail3;
338 if (PUTC ('\n', stdout) == EOF)
339 goto bail3;
340 }
341
342 for (n = 0; n < set->nfont; n++)
343 {
344 font = set->fonts[n];
345 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
346 goto bail3;
c60ec7cc 347 base = FcFileBaseName (base_name, file);
f28f090d
PL
348 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
349 goto bail3;
350 if (FcDebug () & FC_DBG_CACHEV)
351 printf (" write file \"%s\"\n", base);
352 if (!FcCacheWriteStringOld (stdout, base))
353 goto bail3;
354 if (PUTC (' ', stdout) == EOF)
355 goto bail3;
356 if (!FcCacheWriteInt (stdout, id))
357 goto bail3;
358 if (PUTC (' ', stdout) == EOF)
359 goto bail3;
360 name = FcNameUnparse (font);
361 if (!name)
362 goto bail3;
363 ret = FcCacheWriteStringOld (stdout, name);
364 FcStrFree (name);
365 if (!ret)
366 goto bail3;
367 if (PUTC ('\n', stdout) == EOF)
368 goto bail3;
369 }
370
371 FcStrListDone (list);
372
373 return FcTrue;
374
375bail3:
376 FcStrListDone (list);
377bail2:
f28f090d
PL
378 return FcFalse;
379}
380
381int
382main (int argc, char **argv)
383{
384 int i;
f28f090d
PL
385#if HAVE_GETOPT_LONG || HAVE_GETOPT
386 int c;
387 FcFontSet *fs = FcFontSetCreate();
388 FcStrSet *dirs = FcStrSetCreate();
c60ec7cc 389 char *name_buf;
f28f090d
PL
390
391#if HAVE_GETOPT_LONG
392 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
393#else
394 while ((c = getopt (argc, argv, "fsVv?")) != -1)
395#endif
396 {
397 switch (c) {
398 case 'V':
399 fprintf (stderr, "fontconfig version %d.%d.%d\n",
400 FC_MAJOR, FC_MINOR, FC_REVISION);
401 exit (0);
402 default:
403 usage (argv[0]);
404 }
405 }
406 i = optind;
407#else
408 i = 1;
409#endif
410
8f2a8078
PL
411 if (i >= argc)
412 usage (argv[0]);
413
c60ec7cc
PL
414 if (name_buf = FcCacheFileRead (fs, dirs, argv[i]))
415 FcCachePrintSet (fs, dirs, name_buf);
6059daed
PL
416 else
417 {
418 FcStrSetDestroy (dirs);
419 dirs = FcStrSetCreate ();
c60ec7cc 420 if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i]))
6059daed
PL
421 ;
422 }
f28f090d
PL
423
424 FcStrSetDestroy (dirs);
425 FcFontSetDestroy (fs);
426
427 return 0;
428}