]> git.wh0rd.org - fontconfig.git/blob - fc-cat/fc-cat.c
Initialize fontconfig library in fc-cat to avoid segfault.
[fontconfig.git] / fc-cat / fc-cat.c
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>
30 #include <libgen.h>
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>
54 const struct option longopts[] = {
55 {"version", 0, 0, 'V'},
56 {"help", 0, 0, '?'},
57 {NULL,0,0,0},
58 };
59 #else
60 #if HAVE_GETOPT
61 extern char *optarg;
62 extern 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
81 FcBool
82 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name);
83
84 static FcBool
85 FcCacheWriteChars (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
104 static FcBool
105 FcCacheWriteUlong (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
129 static FcBool
130 FcCacheWriteInt (FILE *f, int i)
131 {
132 return FcCacheWriteUlong (f, (unsigned long) i);
133 }
134
135 static FcBool
136 FcCacheWriteStringOld (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
148 static void
149 usage (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
170 static FcBool
171 FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char *cache_file)
172 {
173 char name_buf[8192];
174 int fd;
175 char * current_arch_machine_name;
176 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
177 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
178 off_t current_arch_start = 0;
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
188 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
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 {
199 char * dir;
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
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
215 if (!FcDirCacheConsume (fd, name_buf, set, 0))
216 goto bail1;
217
218 dir = malloc (strlen (name_buf) + 2);
219 if (!dir)
220 goto bail1;
221
222 strcpy (dir, name_buf);
223 strcat (dir, "/");
224
225 FcCachePrintSet (set, dirs, dir);
226 free (dir);
227
228 FcFontSetDestroy (set);
229 set = FcFontSetCreate();
230 }
231
232 bail1:
233 close (fd);
234 bail:
235 return FcFalse;
236 }
237
238 /* read serialized state from the cache file */
239 static char *
240 FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char *cache_file)
241 {
242 int fd;
243 char * current_arch_machine_name;
244 off_t current_arch_start = 0;
245 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
246 static char name_buf[8192], *dir;
247 FcChar8 * ls;
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
257 FcCacheReadString (fd, name_buf, sizeof (name_buf));
258 if (!strlen (name_buf))
259 goto bail;
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",
263 name_buf);
264
265 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
266 if (current_arch_start < 0)
267 goto bail1;
268
269 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
270 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
271
272 dir = strdup(name_buf);
273 ls = FcStrLastSlash ((FcChar8 *)dir);
274 if (ls)
275 *ls = 0;
276
277 if (!FcDirCacheConsume (fd, dir, set, 0))
278 goto bail2;
279 free (dir);
280
281 close(fd);
282 return name_buf;
283
284 bail2:
285 free (dir);
286
287 bail1:
288 close (fd);
289 bail:
290 return 0;
291 }
292
293 /*
294 * return the path from the directory containing 'cache' to 'file'
295 */
296
297 static const FcChar8 *
298 FcFileBaseName (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
309 FcBool
310 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
311 {
312 FcPattern *font;
313 FcChar8 *name, *dir;
314 const FcChar8 *file, *base;
315 int ret;
316 int n;
317 int id;
318 FcStrList *list;
319
320 list = FcStrListCreate (dirs);
321 if (!list)
322 goto bail2;
323
324 while ((dir = FcStrListNext (list)))
325 {
326 base = FcFileBaseName (base_name, dir);
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;
346 base = FcFileBaseName (base_name, file);
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
374 bail3:
375 FcStrListDone (list);
376 bail2:
377 return FcFalse;
378 }
379
380 FcBool
381 FcFileIsDir (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
390 int
391 main (int argc, char **argv)
392 {
393 int i;
394 #if HAVE_GETOPT_LONG || HAVE_GETOPT
395 int c;
396 FcFontSet *fs = FcFontSetCreate();
397 FcStrSet *dirs = FcStrSetCreate();
398 char *name_buf;
399 FcConfig *config;
400
401 #if HAVE_GETOPT_LONG
402 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
403 #else
404 while ((c = getopt (argc, argv, "fsVv?")) != -1)
405 #endif
406 {
407 switch (c) {
408 case 'V':
409 fprintf (stderr, "fontconfig version %d.%d.%d\n",
410 FC_MAJOR, FC_MINOR, FC_REVISION);
411 exit (0);
412 default:
413 usage (argv[0]);
414 }
415 }
416 i = optind;
417 #else
418 i = 1;
419 #endif
420
421 config = FcInitLoadConfig ();
422 if (!config)
423 {
424 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
425 return 1;
426 }
427 FcConfigSetCurrent (config);
428
429 if (i >= argc)
430 usage (argv[0]);
431
432 if (FcFileIsDir ((const FcChar8 *)argv[i]))
433 {
434 char * dummy_name = (char *)FcStrPlus ((FcChar8 *)argv[i],
435 (FcChar8 *)"/dummy");
436 if (!FcDirScanConfig (fs, dirs, 0, 0,
437 (const FcChar8 *)argv[i], FcFalse, config))
438 fprintf (stderr, "couldn't load font dir %s\n", argv[i]);
439 else
440 {
441 /* sorry, we can't tell you where the cache file is. */
442 FcCachePrintSet (fs, dirs, dummy_name);
443 FcStrFree ((FcChar8 *)dummy_name);
444 }
445 }
446 else if ((name_buf = FcCacheFileRead (fs, dirs, argv[i])) != 0)
447 FcCachePrintSet (fs, dirs, name_buf);
448 else
449 {
450 FcStrSetDestroy (dirs);
451 dirs = FcStrSetCreate ();
452 if (FcCacheGlobalFileReadAndPrint (fs, dirs, argv[i]))
453 ;
454 }
455
456 FcStrSetDestroy (dirs);
457 FcFontSetDestroy (fs);
458
459 return 0;
460 }