]> git.wh0rd.org - fontconfig.git/blame - fc-cat/fc-cat.c
Fix the underlying cause of the below segfault (must usually call
[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 {
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;
f28f090d
PL
248
249 if (!cache_file)
250 goto bail;
251
252 current_arch_machine_name = FcCacheMachineSignature();
253 fd = open(cache_file, O_RDONLY);
254 if (fd == -1)
255 goto bail;
256
ea44e218
PL
257 FcCacheReadString (fd, name_buf, sizeof (name_buf));
258 if (!strlen (name_buf))
259 goto bail;
c60ec7cc
PL
260 if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) == 0)
261 goto bail;
262 printf ("fc-cat: printing directory cache for cache which would be named %s\n",
ea44e218
PL
263 name_buf);
264
c60ec7cc 265 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
f28f090d
PL
266 if (current_arch_start < 0)
267 goto bail1;
268
f28f090d
PL
269 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
270 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
271
c60ec7cc
PL
272 dir = strdup(name_buf);
273 ls = FcStrLastSlash ((FcChar8 *)dir);
274 if (ls)
275 *ls = 0;
276
cd9bca69 277 if (!FcDirCacheConsume (fd, dir, set, 0))
c60ec7cc
PL
278 goto bail2;
279 free (dir);
280
f28f090d 281 close(fd);
c60ec7cc
PL
282 return name_buf;
283
284 bail2:
285 free (dir);
f28f090d
PL
286
287 bail1:
288 close (fd);
289 bail:
c60ec7cc 290 return 0;
f28f090d
PL
291}
292
293/*
294 * return the path from the directory containing 'cache' to 'file'
295 */
296
297static const FcChar8 *
298FcFileBaseName (const char *cache, const FcChar8 *file)
299{
300 const FcChar8 *cache_slash;
301
302 cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
303 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
304 (cache_slash + 1) - (const FcChar8 *)cache))
305 return file + ((cache_slash + 1) - (const FcChar8 *)cache);
306 return file;
307}
308
309FcBool
c60ec7cc 310FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
f28f090d
PL
311{
312 FcPattern *font;
313 FcChar8 *name, *dir;
314 const FcChar8 *file, *base;
d2f45978 315 int ret;
f28f090d
PL
316 int n;
317 int id;
f28f090d
PL
318 FcStrList *list;
319
320 list = FcStrListCreate (dirs);
321 if (!list)
322 goto bail2;
323
324 while ((dir = FcStrListNext (list)))
325 {
c60ec7cc 326 base = FcFileBaseName (base_name, dir);
f28f090d
PL
327 if (!FcCacheWriteStringOld (stdout, base))
328 goto bail3;
329 if (PUTC (' ', stdout) == EOF)
330 goto bail3;
331 if (!FcCacheWriteInt (stdout, 0))
332 goto bail3;
333 if (PUTC (' ', stdout) == EOF)
334 goto bail3;
335 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
336 goto bail3;
337 if (PUTC ('\n', stdout) == EOF)
338 goto bail3;
339 }
340
341 for (n = 0; n < set->nfont; n++)
342 {
343 font = set->fonts[n];
344 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
345 goto bail3;
c60ec7cc 346 base = FcFileBaseName (base_name, file);
f28f090d
PL
347 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
348 goto bail3;
349 if (FcDebug () & FC_DBG_CACHEV)
350 printf (" write file \"%s\"\n", base);
351 if (!FcCacheWriteStringOld (stdout, base))
352 goto bail3;
353 if (PUTC (' ', stdout) == EOF)
354 goto bail3;
355 if (!FcCacheWriteInt (stdout, id))
356 goto bail3;
357 if (PUTC (' ', stdout) == EOF)
358 goto bail3;
359 name = FcNameUnparse (font);
360 if (!name)
361 goto bail3;
362 ret = FcCacheWriteStringOld (stdout, name);
363 FcStrFree (name);
364 if (!ret)
365 goto bail3;
366 if (PUTC ('\n', stdout) == EOF)
367 goto bail3;
368 }
369
370 FcStrListDone (list);
371
372 return FcTrue;
373
374bail3:
375 FcStrListDone (list);
376bail2:
f28f090d
PL
377 return FcFalse;
378}
379
12f46c42
PL
380FcBool
381FcFileIsDir (const FcChar8 *file)
382{
383 struct stat statb;
384
385 if (stat ((const char *) file, &statb) != 0)
386 return FcFalse;
387 return S_ISDIR(statb.st_mode);
388}
389
f28f090d
PL
390int
391main (int argc, char **argv)
392{
393 int i;
f28f090d
PL
394#if HAVE_GETOPT_LONG || HAVE_GETOPT
395 int c;
396 FcFontSet *fs = FcFontSetCreate();
397 FcStrSet *dirs = FcStrSetCreate();
c60ec7cc 398 char *name_buf;
f28f090d
PL
399
400#if HAVE_GETOPT_LONG
401 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
402#else
403 while ((c = getopt (argc, argv, "fsVv?")) != -1)
404#endif
405 {
406 switch (c) {
407 case 'V':
408 fprintf (stderr, "fontconfig version %d.%d.%d\n",
409 FC_MAJOR, FC_MINOR, FC_REVISION);
410 exit (0);
411 default:
412 usage (argv[0]);
413 }
414 }
415 i = optind;
416#else
417 i = 1;
418#endif
419
8f2a8078
PL
420 if (i >= argc)
421 usage (argv[0]);
422
12f46c42
PL
423 if (FcFileIsDir ((const FcChar8 *)argv[i]))
424 {
425 char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i],
426 (FcChar8 *)"/dummy");
427 if (!FcDirScanConfig (fs, dirs, 0, 0,
428 (const FcChar8 *)argv[i], FcFalse, 0))
429 fprintf (stderr, "couldn't load font dir %s\n", argv[i]);
430 else
431 {
432 /* sorry, we can't tell you where the cache file is. */
433 FcCachePrintSet (fs, dirs, dummy_name);
434 FcStrFree ((FcChar8 *)dummy_name);
435 }
436 }
437 else if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0)
c60ec7cc 438 FcCachePrintSet (fs, dirs, name_buf);
6059daed
PL
439 else
440 {
441 FcStrSetDestroy (dirs);
442 dirs = FcStrSetCreate ();
c60ec7cc 443 if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i]))
6059daed
PL
444 ;
445 }
f28f090d
PL
446
447 FcStrSetDestroy (dirs);
448 FcFontSetDestroy (fs);
449
450 return 0;
451}