]> git.wh0rd.org - fontconfig.git/blame - fc-cat/fc-cat.c
Hide private functions in shared library. Export functionality for utilities.
[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
f28f090d
PL
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
4984242e 34#include <fontconfig/fontconfig.h>
0a87ce71 35#include "../fc-arch/fcarch.h"
f045376c
PL
36#include <stdio.h>
37#include <stdlib.h>
4984242e 38#include <string.h>
f045376c
PL
39#include <unistd.h>
40#include <sys/types.h>
41#include <sys/stat.h>
42#include <errno.h>
43
f28f090d
PL
44#ifndef HAVE_GETOPT
45#define HAVE_GETOPT 0
46#endif
47#ifndef HAVE_GETOPT_LONG
48#define HAVE_GETOPT_LONG 0
49#endif
50
51#if HAVE_GETOPT_LONG
52#undef _GNU_SOURCE
53#define _GNU_SOURCE
54#include <getopt.h>
55const struct option longopts[] = {
56 {"version", 0, 0, 'V'},
c0288648 57 {"verbose", 0, 0, 'v'},
0a87ce71 58 {"recurse", 0, 0, 'r'},
f28f090d
PL
59 {"help", 0, 0, '?'},
60 {NULL,0,0,0},
61};
62#else
63#if HAVE_GETOPT
64extern char *optarg;
65extern int optind, opterr, optopt;
66#endif
67#endif
68
69/*
70 * POSIX has broken stdio so that getc must do thread-safe locking,
71 * this is a serious performance problem for applications doing large
72 * amounts of IO with getc (as is done here). If available, use
73 * the getc_unlocked varient instead.
74 */
75
76#if defined(getc_unlocked) || defined(_IO_getc_unlocked)
77#define GETC(f) getc_unlocked(f)
78#define PUTC(c,f) putc_unlocked(c,f)
79#else
80#define GETC(f) getc(f)
81#define PUTC(c,f) putc(c,f)
82#endif
83
84static FcBool
bc5e487f 85write_chars (FILE *f, const FcChar8 *chars)
f28f090d
PL
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
bc5e487f 105write_ulong (FILE *f, unsigned long t)
f28f090d
PL
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
bc5e487f 130write_int (FILE *f, int i)
f28f090d 131{
bc5e487f 132 return write_ulong (f, (unsigned long) i);
f28f090d
PL
133}
134
135static FcBool
bc5e487f 136write_string (FILE *f, const FcChar8 *string)
f28f090d
PL
137{
138
139 if (PUTC ('"', f) == EOF)
140 return FcFalse;
bc5e487f 141 if (!write_chars (f, string))
f28f090d
PL
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
0a87ce71
KP
152 fprintf (stderr, "usage: %s [-V?] [--version] [--help] {*-%s.cache-2|directory}...\n",
153 program, FC_ARCHITECTURE);
f28f090d 154#else
0a87ce71
KP
155 fprintf (stderr, "usage: %s [-fsvV?] {*-%s.cache-2|directory}...\n",
156 program, FC_ARCHITECTURE);
f28f090d 157#endif
0a87ce71
KP
158 fprintf (stderr, "Reads font information cache from:\n");
159 fprintf (stderr, " 1) specified fontconfig cache file\n");
160 fprintf (stderr, " 2) related to a particular font directory\n");
f28f090d
PL
161 fprintf (stderr, "\n");
162#if HAVE_GETOPT_LONG
163 fprintf (stderr, " -V, --version display font config version and exit\n");
164 fprintf (stderr, " -?, --help display this help and exit\n");
165#else
166 fprintf (stderr, " -V (version) display font config version and exit\n");
167 fprintf (stderr, " -? (help) display this help and exit\n");
168#endif
169 exit (1);
170}
171
f28f090d
PL
172/*
173 * return the path from the directory containing 'cache' to 'file'
174 */
175
176static const FcChar8 *
4984242e 177file_base_name (const FcChar8 *cache, const FcChar8 *file)
f28f090d 178{
4984242e 179 int cache_len = strlen ((char *) cache);
f28f090d 180
4984242e 181 if (!strncmp ((char *) cache, (char *) file, cache_len) && file[cache_len] == '/')
c0288648 182 return file + cache_len + 1;
f28f090d
PL
183 return file;
184}
185
4984242e
KP
186#define FC_FONT_FILE_DIR ((FcChar8 *) ".dir")
187
bc5e487f 188static FcBool
4984242e 189cache_print_set (FcFontSet *set, FcStrSet *dirs, const FcChar8 *base_name, FcBool verbose)
f28f090d 190{
f28f090d
PL
191 FcChar8 *name, *dir;
192 const FcChar8 *file, *base;
d2f45978 193 int ret;
f28f090d
PL
194 int n;
195 int id;
0a87ce71 196 int ndir = 0;
f28f090d
PL
197 FcStrList *list;
198
199 list = FcStrListCreate (dirs);
200 if (!list)
201 goto bail2;
202
203 while ((dir = FcStrListNext (list)))
204 {
bc5e487f
KP
205 base = file_base_name (base_name, dir);
206 if (!write_string (stdout, base))
f28f090d
PL
207 goto bail3;
208 if (PUTC (' ', stdout) == EOF)
209 goto bail3;
bc5e487f 210 if (!write_int (stdout, 0))
f28f090d
PL
211 goto bail3;
212 if (PUTC (' ', stdout) == EOF)
213 goto bail3;
bc5e487f 214 if (!write_string (stdout, FC_FONT_FILE_DIR))
f28f090d
PL
215 goto bail3;
216 if (PUTC ('\n', stdout) == EOF)
217 goto bail3;
0a87ce71 218 ndir++;
f28f090d
PL
219 }
220
221 for (n = 0; n < set->nfont; n++)
222 {
4984242e 223 FcPattern *font = set->fonts[n];
c0288648 224
f28f090d
PL
225 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
226 goto bail3;
bc5e487f 227 base = file_base_name (base_name, file);
f28f090d
PL
228 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
229 goto bail3;
bc5e487f 230 if (!write_string (stdout, base))
f28f090d
PL
231 goto bail3;
232 if (PUTC (' ', stdout) == EOF)
233 goto bail3;
bc5e487f 234 if (!write_int (stdout, id))
f28f090d
PL
235 goto bail3;
236 if (PUTC (' ', stdout) == EOF)
237 goto bail3;
238 name = FcNameUnparse (font);
239 if (!name)
240 goto bail3;
bc5e487f 241 ret = write_string (stdout, name);
f28f090d
PL
242 FcStrFree (name);
243 if (!ret)
244 goto bail3;
245 if (PUTC ('\n', stdout) == EOF)
246 goto bail3;
247 }
0a87ce71
KP
248 if (verbose && !set->nfont && !ndir)
249 printf ("<empty>\n");
f28f090d
PL
250
251 FcStrListDone (list);
252
253 return FcTrue;
254
255bail3:
256 FcStrListDone (list);
257bail2:
f28f090d
PL
258 return FcFalse;
259}
260
261int
262main (int argc, char **argv)
263{
264 int i;
c0288648
KP
265 int ret = 0;
266 FcFontSet *fs;
267 FcStrSet *dirs;
0a87ce71
KP
268 FcStrSet *args = NULL;
269 FcStrList *arglist;
c0288648
KP
270 FcCache *cache;
271 FcConfig *config;
0a87ce71 272 FcChar8 *arg;
c0288648 273 int verbose = 0;
0a87ce71
KP
274 int recurse = 0;
275 FcBool first = FcTrue;
f28f090d
PL
276#if HAVE_GETOPT_LONG || HAVE_GETOPT
277 int c;
f28f090d
PL
278
279#if HAVE_GETOPT_LONG
0a87ce71 280 while ((c = getopt_long (argc, argv, "Vvr?", longopts, NULL)) != -1)
f28f090d 281#else
0a87ce71 282 while ((c = getopt (argc, argv, "Vvr?")) != -1)
f28f090d
PL
283#endif
284 {
285 switch (c) {
286 case 'V':
287 fprintf (stderr, "fontconfig version %d.%d.%d\n",
288 FC_MAJOR, FC_MINOR, FC_REVISION);
289 exit (0);
c0288648
KP
290 case 'v':
291 verbose++;
292 break;
0a87ce71
KP
293 case 'r':
294 recurse++;
295 break;
f28f090d
PL
296 default:
297 usage (argv[0]);
298 }
299 }
300 i = optind;
301#else
302 i = 1;
303#endif
304
9769b43d
PL
305 config = FcInitLoadConfig ();
306 if (!config)
307 {
308 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
309 return 1;
310 }
311 FcConfigSetCurrent (config);
312
0a87ce71
KP
313 args = FcStrSetCreate ();
314 if (!args)
315 {
316 fprintf (stderr, "%s: malloc failure\n", argv[0]);
317 return 1;
318 }
319 if (i < argc)
320 {
321 for (; i < argc; i++)
322 {
4984242e 323 if (!FcStrSetAddFilename (args, (const FcChar8 *) argv[i]))
0a87ce71
KP
324 {
325 fprintf (stderr, "%s: malloc failure\n", argv[0]);
326 return 1;
327 }
328 }
329 arglist = FcStrListCreate (args);
330 if (!arglist)
331 {
332 fprintf (stderr, "%s: malloc failure\n", argv[0]);
333 return 1;
334 }
335 }
336 else
337 {
338 recurse++;
339 arglist = FcConfigGetFontDirs (config);
340 while ((arg = FcStrListNext (arglist)))
341 if (!FcStrSetAdd (args, arg))
342 {
343 fprintf (stderr, "%s: malloc failure\n", argv[0]);
344 return 1;
345 }
346 FcStrListDone (arglist);
347 }
348 arglist = FcStrListCreate (args);
349 if (!arglist)
350 {
351 fprintf (stderr, "%s: malloc failure\n", argv[0]);
352 return 1;
353 }
8f2a8078 354
0a87ce71 355 while ((arg = FcStrListNext (arglist)))
6059daed 356 {
0a87ce71 357 int j;
0a87ce71 358 FcChar8 *cache_file = NULL;
bc5e487f 359 struct stat file_stat;
c0288648 360
0a87ce71 361 if (FcFileIsDir (arg))
bc5e487f 362 cache = FcDirCacheLoad (arg, config, &cache_file);
c0288648 363 else
bc5e487f 364 cache = FcDirCacheLoadFile (arg, &file_stat);
76abb77f 365 if (!cache)
c0288648 366 {
0a87ce71 367 perror ((char *) arg);
c0288648
KP
368 ret++;
369 continue;
370 }
371
c0288648 372 dirs = FcStrSetCreate ();
4984242e
KP
373 fs = FcCacheCopySet (cache);
374 for (j = 0; j < FcCacheNumSubdir (cache); j++)
0a87ce71 375 {
4984242e 376 FcStrSetAdd (dirs, FcCacheSubdir (cache, j));
0a87ce71 377 if (recurse)
4984242e 378 FcStrSetAdd (args, FcCacheSubdir (cache, j));
0a87ce71 379 }
c0288648
KP
380
381 if (verbose)
0a87ce71
KP
382 {
383 if (!first)
384 printf ("\n");
385 printf ("Directory: %s\nCache: %s\n--------\n",
386 FcCacheDir(cache), cache_file ? cache_file : arg);
387 first = FcFalse;
388 }
bc5e487f 389 cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
f28f090d 390
c0288648
KP
391 FcStrSetDestroy (dirs);
392
4984242e 393 FcFontSetDestroy (fs);
bc5e487f 394 FcDirCacheUnload (cache);
0a87ce71
KP
395 if (cache_file)
396 FcStrFree (cache_file);
c0288648 397 }
f28f090d
PL
398
399 return 0;
400}