]> git.wh0rd.org - fontconfig.git/blame - fc-cat/fc-cat.c
Add WenQuanYi fonts to default conf (#17262, from Mandriva)
[fontconfig.git] / fc-cat / fc-cat.c
CommitLineData
f28f090d 1/*
317b8492 2 * fontconfig/fc-cat/fc-cat.c
f28f090d
PL
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
e3b65ee0 152 fprintf (stderr, "usage: %s [-rv] [--recurse] [--verbose] [*-%s.cache-2|directory]...\n",
0a87ce71 153 program, FC_ARCHITECTURE);
e3b65ee0 154 fprintf (stderr, " %s [-V?] [--version] [--help]\n", program);
f28f090d 155#else
e3b65ee0 156 fprintf (stderr, "usage: %s [-rvV?] [*-%s.cache-2|directory]...\n",
0a87ce71 157 program, FC_ARCHITECTURE);
f28f090d 158#endif
0a87ce71
KP
159 fprintf (stderr, "Reads font information cache from:\n");
160 fprintf (stderr, " 1) specified fontconfig cache file\n");
161 fprintf (stderr, " 2) related to a particular font directory\n");
f28f090d
PL
162 fprintf (stderr, "\n");
163#if HAVE_GETOPT_LONG
e3b65ee0
PB
164 fprintf (stderr, " -r, --recurse recurse into subdirectories\n");
165 fprintf (stderr, " -v, --verbose be verbose\n");
f28f090d
PL
166 fprintf (stderr, " -V, --version display font config version and exit\n");
167 fprintf (stderr, " -?, --help display this help and exit\n");
168#else
e3b65ee0
PB
169 fprintf (stderr, " -r (recurse) recurse into subdirectories\n");
170 fprintf (stderr, " -v (verbose) be verbose\n");
f28f090d
PL
171 fprintf (stderr, " -V (version) display font config version and exit\n");
172 fprintf (stderr, " -? (help) display this help and exit\n");
173#endif
174 exit (1);
175}
176
f28f090d
PL
177/*
178 * return the path from the directory containing 'cache' to 'file'
179 */
180
181static const FcChar8 *
4984242e 182file_base_name (const FcChar8 *cache, const FcChar8 *file)
f28f090d 183{
4984242e 184 int cache_len = strlen ((char *) cache);
f28f090d 185
4984242e 186 if (!strncmp ((char *) cache, (char *) file, cache_len) && file[cache_len] == '/')
c0288648 187 return file + cache_len + 1;
f28f090d
PL
188 return file;
189}
190
4984242e
KP
191#define FC_FONT_FILE_DIR ((FcChar8 *) ".dir")
192
bc5e487f 193static FcBool
4984242e 194cache_print_set (FcFontSet *set, FcStrSet *dirs, const FcChar8 *base_name, FcBool verbose)
f28f090d 195{
f28f090d
PL
196 FcChar8 *name, *dir;
197 const FcChar8 *file, *base;
d2f45978 198 int ret;
f28f090d
PL
199 int n;
200 int id;
0a87ce71 201 int ndir = 0;
f28f090d
PL
202 FcStrList *list;
203
204 list = FcStrListCreate (dirs);
205 if (!list)
206 goto bail2;
207
208 while ((dir = FcStrListNext (list)))
209 {
bc5e487f
KP
210 base = file_base_name (base_name, dir);
211 if (!write_string (stdout, base))
f28f090d
PL
212 goto bail3;
213 if (PUTC (' ', stdout) == EOF)
214 goto bail3;
bc5e487f 215 if (!write_int (stdout, 0))
f28f090d
PL
216 goto bail3;
217 if (PUTC (' ', stdout) == EOF)
218 goto bail3;
bc5e487f 219 if (!write_string (stdout, FC_FONT_FILE_DIR))
f28f090d
PL
220 goto bail3;
221 if (PUTC ('\n', stdout) == EOF)
222 goto bail3;
0a87ce71 223 ndir++;
f28f090d
PL
224 }
225
226 for (n = 0; n < set->nfont; n++)
227 {
4984242e 228 FcPattern *font = set->fonts[n];
c0288648 229
f28f090d
PL
230 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
231 goto bail3;
bc5e487f 232 base = file_base_name (base_name, file);
f28f090d
PL
233 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
234 goto bail3;
bc5e487f 235 if (!write_string (stdout, base))
f28f090d
PL
236 goto bail3;
237 if (PUTC (' ', stdout) == EOF)
238 goto bail3;
bc5e487f 239 if (!write_int (stdout, id))
f28f090d
PL
240 goto bail3;
241 if (PUTC (' ', stdout) == EOF)
242 goto bail3;
243 name = FcNameUnparse (font);
244 if (!name)
245 goto bail3;
bc5e487f 246 ret = write_string (stdout, name);
f28f090d
PL
247 FcStrFree (name);
248 if (!ret)
249 goto bail3;
250 if (PUTC ('\n', stdout) == EOF)
251 goto bail3;
252 }
0a87ce71
KP
253 if (verbose && !set->nfont && !ndir)
254 printf ("<empty>\n");
f28f090d
PL
255
256 FcStrListDone (list);
257
258 return FcTrue;
259
260bail3:
261 FcStrListDone (list);
262bail2:
f28f090d
PL
263 return FcFalse;
264}
265
266int
267main (int argc, char **argv)
268{
269 int i;
c0288648
KP
270 int ret = 0;
271 FcFontSet *fs;
272 FcStrSet *dirs;
0a87ce71
KP
273 FcStrSet *args = NULL;
274 FcStrList *arglist;
c0288648
KP
275 FcCache *cache;
276 FcConfig *config;
0a87ce71 277 FcChar8 *arg;
c0288648 278 int verbose = 0;
0a87ce71
KP
279 int recurse = 0;
280 FcBool first = FcTrue;
f28f090d
PL
281#if HAVE_GETOPT_LONG || HAVE_GETOPT
282 int c;
f28f090d
PL
283
284#if HAVE_GETOPT_LONG
0a87ce71 285 while ((c = getopt_long (argc, argv, "Vvr?", longopts, NULL)) != -1)
f28f090d 286#else
0a87ce71 287 while ((c = getopt (argc, argv, "Vvr?")) != -1)
f28f090d
PL
288#endif
289 {
290 switch (c) {
291 case 'V':
292 fprintf (stderr, "fontconfig version %d.%d.%d\n",
293 FC_MAJOR, FC_MINOR, FC_REVISION);
294 exit (0);
c0288648
KP
295 case 'v':
296 verbose++;
297 break;
0a87ce71
KP
298 case 'r':
299 recurse++;
300 break;
f28f090d
PL
301 default:
302 usage (argv[0]);
303 }
304 }
305 i = optind;
306#else
307 i = 1;
308#endif
309
9769b43d
PL
310 config = FcInitLoadConfig ();
311 if (!config)
312 {
313 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
314 return 1;
315 }
316 FcConfigSetCurrent (config);
317
0a87ce71
KP
318 args = FcStrSetCreate ();
319 if (!args)
320 {
321 fprintf (stderr, "%s: malloc failure\n", argv[0]);
322 return 1;
323 }
324 if (i < argc)
325 {
326 for (; i < argc; i++)
327 {
4984242e 328 if (!FcStrSetAddFilename (args, (const FcChar8 *) argv[i]))
0a87ce71
KP
329 {
330 fprintf (stderr, "%s: malloc failure\n", argv[0]);
331 return 1;
332 }
333 }
334 arglist = FcStrListCreate (args);
335 if (!arglist)
336 {
337 fprintf (stderr, "%s: malloc failure\n", argv[0]);
338 return 1;
339 }
340 }
341 else
342 {
343 recurse++;
344 arglist = FcConfigGetFontDirs (config);
345 while ((arg = FcStrListNext (arglist)))
346 if (!FcStrSetAdd (args, arg))
347 {
348 fprintf (stderr, "%s: malloc failure\n", argv[0]);
349 return 1;
350 }
351 FcStrListDone (arglist);
352 }
353 arglist = FcStrListCreate (args);
354 if (!arglist)
355 {
356 fprintf (stderr, "%s: malloc failure\n", argv[0]);
357 return 1;
358 }
8f2a8078 359
0a87ce71 360 while ((arg = FcStrListNext (arglist)))
6059daed 361 {
0a87ce71 362 int j;
0a87ce71 363 FcChar8 *cache_file = NULL;
bc5e487f 364 struct stat file_stat;
c0288648 365
0a87ce71 366 if (FcFileIsDir (arg))
bc5e487f 367 cache = FcDirCacheLoad (arg, config, &cache_file);
c0288648 368 else
bc5e487f 369 cache = FcDirCacheLoadFile (arg, &file_stat);
76abb77f 370 if (!cache)
c0288648 371 {
0a87ce71 372 perror ((char *) arg);
c0288648
KP
373 ret++;
374 continue;
375 }
376
c0288648 377 dirs = FcStrSetCreate ();
4984242e
KP
378 fs = FcCacheCopySet (cache);
379 for (j = 0; j < FcCacheNumSubdir (cache); j++)
0a87ce71 380 {
4984242e 381 FcStrSetAdd (dirs, FcCacheSubdir (cache, j));
0a87ce71 382 if (recurse)
4984242e 383 FcStrSetAdd (args, FcCacheSubdir (cache, j));
0a87ce71 384 }
c0288648
KP
385
386 if (verbose)
0a87ce71
KP
387 {
388 if (!first)
389 printf ("\n");
390 printf ("Directory: %s\nCache: %s\n--------\n",
391 FcCacheDir(cache), cache_file ? cache_file : arg);
392 first = FcFalse;
393 }
bc5e487f 394 cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
f28f090d 395
c0288648
KP
396 FcStrSetDestroy (dirs);
397
4984242e 398 FcFontSetDestroy (fs);
bc5e487f 399 FcDirCacheUnload (cache);
0a87ce71
KP
400 if (cache_file)
401 FcStrFree (cache_file);
c0288648 402 }
f28f090d
PL
403
404 return 0;
405}