]> git.wh0rd.org - fontconfig.git/blame - fc-cat/fc-cat.c
Reinstate basename patch, but keep a hash table linking FcPatterns to their
[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
81static FcBool
82FcCacheWriteChars (FILE *f, const FcChar8 *chars)
83{
84 FcChar8 c;
85 while ((c = *chars++))
86 {
87 switch (c) {
88 case '"':
89 case '\\':
90 if (PUTC ('\\', f) == EOF)
91 return FcFalse;
92 /* fall through */
93 default:
94 if (PUTC (c, f) == EOF)
95 return FcFalse;
96 }
97 }
98 return FcTrue;
99}
100
101static FcBool
102FcCacheWriteUlong (FILE *f, unsigned long t)
103{
104 int pow;
105 unsigned long temp, digit;
106
107 temp = t;
108 pow = 1;
109 while (temp >= 10)
110 {
111 temp /= 10;
112 pow *= 10;
113 }
114 temp = t;
115 while (pow)
116 {
117 digit = temp / pow;
118 if (PUTC ((char) digit + '0', f) == EOF)
119 return FcFalse;
120 temp = temp - pow * digit;
121 pow = pow / 10;
122 }
123 return FcTrue;
124}
125
126static FcBool
127FcCacheWriteInt (FILE *f, int i)
128{
129 return FcCacheWriteUlong (f, (unsigned long) i);
130}
131
132static FcBool
133FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
134{
135
136 if (PUTC ('"', f) == EOF)
137 return FcFalse;
138 if (!FcCacheWriteChars (f, string))
139 return FcFalse;
140 if (PUTC ('"', f) == EOF)
141 return FcFalse;
142 return FcTrue;
143}
144
145static void
146usage (char *program)
147{
148#if HAVE_GETOPT_LONG
149 fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
150 program);
151#else
152 fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n",
153 program);
154#endif
155 fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n");
156 fprintf (stderr, "\n");
157#if HAVE_GETOPT_LONG
158 fprintf (stderr, " -V, --version display font config version and exit\n");
159 fprintf (stderr, " -?, --help display this help and exit\n");
160#else
161 fprintf (stderr, " -V (version) display font config version and exit\n");
162 fprintf (stderr, " -? (help) display this help and exit\n");
163#endif
164 exit (1);
165}
166
167/* read serialized state from the cache file */
168static FcBool
e77c1718 169FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
f28f090d
PL
170{
171 int fd;
172 char * current_arch_machine_name;
173 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
174 off_t current_arch_start = 0;
175 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
176
177 if (!cache_file)
178 goto bail;
179
180 current_arch_machine_name = FcCacheMachineSignature();
181 fd = open(cache_file, O_RDONLY);
182 if (fd == -1)
183 goto bail;
184
185 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
186 if (current_arch_start < 0)
187 goto bail1;
188
189 lseek (fd, current_arch_start, SEEK_SET);
190 if (FcCacheReadString (fd, candidate_arch_machine_name,
191 sizeof (candidate_arch_machine_name)) == 0)
192 goto bail1;
193
194 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
195 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
196
e77c1718 197 if (!FcDirCacheConsume (fd, dir, set))
f28f090d
PL
198 goto bail1;
199
200 close(fd);
201 free (cache_file);
202 return FcTrue;
203
204 bail1:
205 close (fd);
206 bail:
207 free (cache_file);
208 return FcFalse;
209}
210
211/*
212 * return the path from the directory containing 'cache' to 'file'
213 */
214
215static const FcChar8 *
216FcFileBaseName (const char *cache, const FcChar8 *file)
217{
218 const FcChar8 *cache_slash;
219
220 cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
221 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
222 (cache_slash + 1) - (const FcChar8 *)cache))
223 return file + ((cache_slash + 1) - (const FcChar8 *)cache);
224 return file;
225}
226
227FcBool
228FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
229{
230 FcPattern *font;
231 FcChar8 *name, *dir;
232 const FcChar8 *file, *base;
233 int n;
234 int id;
235 FcBool ret;
236 FcStrList *list;
237
238 list = FcStrListCreate (dirs);
239 if (!list)
240 goto bail2;
241
242 while ((dir = FcStrListNext (list)))
243 {
244 base = FcFileBaseName (cache_file, dir);
245 if (!FcCacheWriteStringOld (stdout, base))
246 goto bail3;
247 if (PUTC (' ', stdout) == EOF)
248 goto bail3;
249 if (!FcCacheWriteInt (stdout, 0))
250 goto bail3;
251 if (PUTC (' ', stdout) == EOF)
252 goto bail3;
253 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
254 goto bail3;
255 if (PUTC ('\n', stdout) == EOF)
256 goto bail3;
257 }
258
259 for (n = 0; n < set->nfont; n++)
260 {
261 font = set->fonts[n];
262 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
263 goto bail3;
264 base = FcFileBaseName (cache_file, file);
265 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
266 goto bail3;
267 if (FcDebug () & FC_DBG_CACHEV)
268 printf (" write file \"%s\"\n", base);
269 if (!FcCacheWriteStringOld (stdout, base))
270 goto bail3;
271 if (PUTC (' ', stdout) == EOF)
272 goto bail3;
273 if (!FcCacheWriteInt (stdout, id))
274 goto bail3;
275 if (PUTC (' ', stdout) == EOF)
276 goto bail3;
277 name = FcNameUnparse (font);
278 if (!name)
279 goto bail3;
280 ret = FcCacheWriteStringOld (stdout, name);
281 FcStrFree (name);
282 if (!ret)
283 goto bail3;
284 if (PUTC ('\n', stdout) == EOF)
285 goto bail3;
286 }
287
288 FcStrListDone (list);
289
290 return FcTrue;
291
292bail3:
293 FcStrListDone (list);
294bail2:
295bail1:
296bail0:
297 return FcFalse;
298}
299
300int
301main (int argc, char **argv)
302{
303 int i;
304 int ret;
305#if HAVE_GETOPT_LONG || HAVE_GETOPT
306 int c;
307 FcFontSet *fs = FcFontSetCreate();
308 FcStrSet *dirs = FcStrSetCreate();
309
310#if HAVE_GETOPT_LONG
311 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
312#else
313 while ((c = getopt (argc, argv, "fsVv?")) != -1)
314#endif
315 {
316 switch (c) {
317 case 'V':
318 fprintf (stderr, "fontconfig version %d.%d.%d\n",
319 FC_MAJOR, FC_MINOR, FC_REVISION);
320 exit (0);
321 default:
322 usage (argv[0]);
323 }
324 }
325 i = optind;
326#else
327 i = 1;
328#endif
329
e77c1718 330 if (FcCacheFileRead (fs, dirs, dirname (argv[i]), argv[i]))
f28f090d
PL
331 FcCachePrintSet (fs, dirs, argv[i]);
332
333 FcStrSetDestroy (dirs);
334 FcFontSetDestroy (fs);
335
336 return 0;
337}