]> git.wh0rd.org - fontconfig.git/blob - fc-cat/fc-cat.c
FcCharSetSerialize was using wrong offset for leaves. Make fc-cat work.
[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 #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
34 #include <fontconfig/fontconfig.h>
35 #include <../src/fccache.c>
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
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 {"verbose", 0, 0, 'v'},
57 {"help", 0, 0, '?'},
58 {NULL,0,0,0},
59 };
60 #else
61 #if HAVE_GETOPT
62 extern char *optarg;
63 extern int optind, opterr, optopt;
64 #endif
65 #endif
66
67 /*
68 * POSIX has broken stdio so that getc must do thread-safe locking,
69 * this is a serious performance problem for applications doing large
70 * amounts of IO with getc (as is done here). If available, use
71 * the getc_unlocked varient instead.
72 */
73
74 #if defined(getc_unlocked) || defined(_IO_getc_unlocked)
75 #define GETC(f) getc_unlocked(f)
76 #define PUTC(c,f) putc_unlocked(c,f)
77 #else
78 #define GETC(f) getc(f)
79 #define PUTC(c,f) putc(c,f)
80 #endif
81
82 FcBool
83 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name);
84
85 static FcBool
86 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
87 {
88 FcChar8 c;
89 while ((c = *chars++))
90 {
91 switch (c) {
92 case '"':
93 case '\\':
94 if (PUTC ('\\', f) == EOF)
95 return FcFalse;
96 /* fall through */
97 default:
98 if (PUTC (c, f) == EOF)
99 return FcFalse;
100 }
101 }
102 return FcTrue;
103 }
104
105 static FcBool
106 FcCacheWriteUlong (FILE *f, unsigned long t)
107 {
108 int pow;
109 unsigned long temp, digit;
110
111 temp = t;
112 pow = 1;
113 while (temp >= 10)
114 {
115 temp /= 10;
116 pow *= 10;
117 }
118 temp = t;
119 while (pow)
120 {
121 digit = temp / pow;
122 if (PUTC ((char) digit + '0', f) == EOF)
123 return FcFalse;
124 temp = temp - pow * digit;
125 pow = pow / 10;
126 }
127 return FcTrue;
128 }
129
130 static FcBool
131 FcCacheWriteInt (FILE *f, int i)
132 {
133 return FcCacheWriteUlong (f, (unsigned long) i);
134 }
135
136 static FcBool
137 FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
138 {
139
140 if (PUTC ('"', f) == EOF)
141 return FcFalse;
142 if (!FcCacheWriteChars (f, string))
143 return FcFalse;
144 if (PUTC ('"', f) == EOF)
145 return FcFalse;
146 return FcTrue;
147 }
148
149 static void
150 usage (char *program)
151 {
152 #if HAVE_GETOPT_LONG
153 fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
154 program);
155 #else
156 fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n",
157 program);
158 #endif
159 fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n");
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
171 static int
172 FcCacheFileOpen (char *cache_file, off_t *size)
173 {
174 int fd;
175 struct stat file_stat;
176
177 fd = open(cache_file, O_RDONLY | O_BINARY);
178 if (fd < 0)
179 return -1;
180
181 if (fstat (fd, &file_stat) < 0) {
182 close (fd);
183 return -1;
184 }
185 *size = file_stat.st_size;
186 return fd;
187 }
188
189 /*
190 * return the path from the directory containing 'cache' to 'file'
191 */
192
193 static const FcChar8 *
194 FcFileBaseName (const char *cache, const FcChar8 *file)
195 {
196 const FcChar8 *cache_slash;
197 int cache_len = strlen (cache);
198
199 if (!strncmp (cache, file, cache_len) && file[cache_len] == '/')
200 return file + cache_len + 1;
201 return file;
202 }
203
204 FcBool
205 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *base_name)
206 {
207 FcPattern *font;
208 FcChar8 *name, *dir;
209 const FcChar8 *file, *base;
210 int ret;
211 int n;
212 int id;
213 FcStrList *list;
214
215 list = FcStrListCreate (dirs);
216 if (!list)
217 goto bail2;
218
219 while ((dir = FcStrListNext (list)))
220 {
221 base = FcFileBaseName (base_name, dir);
222 if (!FcCacheWriteStringOld (stdout, base))
223 goto bail3;
224 if (PUTC (' ', stdout) == EOF)
225 goto bail3;
226 if (!FcCacheWriteInt (stdout, 0))
227 goto bail3;
228 if (PUTC (' ', stdout) == EOF)
229 goto bail3;
230 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
231 goto bail3;
232 if (PUTC ('\n', stdout) == EOF)
233 goto bail3;
234 }
235
236 for (n = 0; n < set->nfont; n++)
237 {
238 FcPattern **fonts = FcFontSetFonts (set);
239 FcPattern *encoded_font = fonts[n];
240 FcPattern *font = FcEncodedOffsetToPtr (set, encoded_font, FcPattern);
241
242 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
243 goto bail3;
244 base = FcFileBaseName (base_name, file);
245 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
246 goto bail3;
247 if (!FcCacheWriteStringOld (stdout, base))
248 goto bail3;
249 if (PUTC (' ', stdout) == EOF)
250 goto bail3;
251 if (!FcCacheWriteInt (stdout, id))
252 goto bail3;
253 if (PUTC (' ', stdout) == EOF)
254 goto bail3;
255 name = FcNameUnparse (font);
256 if (!name)
257 goto bail3;
258 ret = FcCacheWriteStringOld (stdout, name);
259 FcStrFree (name);
260 if (!ret)
261 goto bail3;
262 if (PUTC ('\n', stdout) == EOF)
263 goto bail3;
264 }
265
266 FcStrListDone (list);
267
268 return FcTrue;
269
270 bail3:
271 FcStrListDone (list);
272 bail2:
273 return FcFalse;
274 }
275
276 int
277 main (int argc, char **argv)
278 {
279 int i;
280 int ret = 0;
281 FcFontSet *fs;
282 FcStrSet *dirs;
283 FcCache *cache;
284 FcConfig *config;
285 int verbose = 0;
286 #if HAVE_GETOPT_LONG || HAVE_GETOPT
287 int c;
288
289 #if HAVE_GETOPT_LONG
290 while ((c = getopt_long (argc, argv, "Vv?", longopts, NULL)) != -1)
291 #else
292 while ((c = getopt (argc, argv, "Vv?")) != -1)
293 #endif
294 {
295 switch (c) {
296 case 'V':
297 fprintf (stderr, "fontconfig version %d.%d.%d\n",
298 FC_MAJOR, FC_MINOR, FC_REVISION);
299 exit (0);
300 case 'v':
301 verbose++;
302 break;
303 default:
304 usage (argv[0]);
305 }
306 }
307 i = optind;
308 #else
309 i = 1;
310 #endif
311
312 config = FcInitLoadConfig ();
313 if (!config)
314 {
315 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
316 return 1;
317 }
318 FcConfigSetCurrent (config);
319
320 if (i >= argc)
321 usage (argv[0]);
322
323 for (; i < argc; i++)
324 {
325 int fd;
326 int j;
327 off_t size;
328 intptr_t *cache_dirs;
329
330 if (FcFileIsDir ((const FcChar8 *)argv[i]))
331 fd = FcDirCacheOpen (config, (const FcChar8 *) argv[i], &size);
332 else
333 fd = FcCacheFileOpen (argv[i], &size);
334 if (fd < 0)
335 {
336 perror (argv[i]);
337 ret++;
338 continue;
339 }
340
341 cache = FcDirCacheMap (fd, size);
342 close (fd);
343 if (!cache)
344 {
345 fprintf (stderr, "%s: cannot map cache\n", argv[i]);
346 ret++;
347 continue;
348 }
349 dirs = FcStrSetCreate ();
350 fs = FcCacheSet (cache);
351 cache_dirs = FcCacheDirs (cache);
352 for (j = 0; j < cache->dirs_count; j++)
353 FcStrSetAdd (dirs, FcOffsetToPtr (cache_dirs,
354 cache_dirs[j],
355 FcChar8));
356
357 if (verbose)
358 printf ("Name: %s\nDirectory: %s\n", argv[i], FcCacheDir(cache));
359 FcCachePrintSet (fs, dirs, FcCacheDir (cache));
360
361 FcStrSetDestroy (dirs);
362
363 FcDirCacheUnmap (cache);
364 }
365
366 return 0;
367 }