]> git.wh0rd.org - fontconfig.git/blob - fc-cat/fc-cat.c
Replace RCS Id tags with the file name
[fontconfig.git] / fc-cat / fc-cat.c
1 /*
2 * fontconfig/fc-cat/fc-cat.c
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 "../fc-arch/fcarch.h"
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <errno.h>
43
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>
55 const struct option longopts[] = {
56 {"version", 0, 0, 'V'},
57 {"verbose", 0, 0, 'v'},
58 {"recurse", 0, 0, 'r'},
59 {"help", 0, 0, '?'},
60 {NULL,0,0,0},
61 };
62 #else
63 #if HAVE_GETOPT
64 extern char *optarg;
65 extern 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
84 static FcBool
85 write_chars (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 write_ulong (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 write_int (FILE *f, int i)
131 {
132 return write_ulong (f, (unsigned long) i);
133 }
134
135 static FcBool
136 write_string (FILE *f, const FcChar8 *string)
137 {
138
139 if (PUTC ('"', f) == EOF)
140 return FcFalse;
141 if (!write_chars (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 [-rv] [--recurse] [--verbose] [*-%s.cache-2|directory]...\n",
153 program, FC_ARCHITECTURE);
154 fprintf (stderr, " %s [-V?] [--version] [--help]\n", program);
155 #else
156 fprintf (stderr, "usage: %s [-rvV?] [*-%s.cache-2|directory]...\n",
157 program, FC_ARCHITECTURE);
158 #endif
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");
162 fprintf (stderr, "\n");
163 #if HAVE_GETOPT_LONG
164 fprintf (stderr, " -r, --recurse recurse into subdirectories\n");
165 fprintf (stderr, " -v, --verbose be verbose\n");
166 fprintf (stderr, " -V, --version display font config version and exit\n");
167 fprintf (stderr, " -?, --help display this help and exit\n");
168 #else
169 fprintf (stderr, " -r (recurse) recurse into subdirectories\n");
170 fprintf (stderr, " -v (verbose) be verbose\n");
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
177 /*
178 * return the path from the directory containing 'cache' to 'file'
179 */
180
181 static const FcChar8 *
182 file_base_name (const FcChar8 *cache, const FcChar8 *file)
183 {
184 int cache_len = strlen ((char *) cache);
185
186 if (!strncmp ((char *) cache, (char *) file, cache_len) && file[cache_len] == '/')
187 return file + cache_len + 1;
188 return file;
189 }
190
191 #define FC_FONT_FILE_DIR ((FcChar8 *) ".dir")
192
193 static FcBool
194 cache_print_set (FcFontSet *set, FcStrSet *dirs, const FcChar8 *base_name, FcBool verbose)
195 {
196 FcChar8 *name, *dir;
197 const FcChar8 *file, *base;
198 int ret;
199 int n;
200 int id;
201 int ndir = 0;
202 FcStrList *list;
203
204 list = FcStrListCreate (dirs);
205 if (!list)
206 goto bail2;
207
208 while ((dir = FcStrListNext (list)))
209 {
210 base = file_base_name (base_name, dir);
211 if (!write_string (stdout, base))
212 goto bail3;
213 if (PUTC (' ', stdout) == EOF)
214 goto bail3;
215 if (!write_int (stdout, 0))
216 goto bail3;
217 if (PUTC (' ', stdout) == EOF)
218 goto bail3;
219 if (!write_string (stdout, FC_FONT_FILE_DIR))
220 goto bail3;
221 if (PUTC ('\n', stdout) == EOF)
222 goto bail3;
223 ndir++;
224 }
225
226 for (n = 0; n < set->nfont; n++)
227 {
228 FcPattern *font = set->fonts[n];
229
230 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
231 goto bail3;
232 base = file_base_name (base_name, file);
233 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
234 goto bail3;
235 if (!write_string (stdout, base))
236 goto bail3;
237 if (PUTC (' ', stdout) == EOF)
238 goto bail3;
239 if (!write_int (stdout, id))
240 goto bail3;
241 if (PUTC (' ', stdout) == EOF)
242 goto bail3;
243 name = FcNameUnparse (font);
244 if (!name)
245 goto bail3;
246 ret = write_string (stdout, name);
247 FcStrFree (name);
248 if (!ret)
249 goto bail3;
250 if (PUTC ('\n', stdout) == EOF)
251 goto bail3;
252 }
253 if (verbose && !set->nfont && !ndir)
254 printf ("<empty>\n");
255
256 FcStrListDone (list);
257
258 return FcTrue;
259
260 bail3:
261 FcStrListDone (list);
262 bail2:
263 return FcFalse;
264 }
265
266 int
267 main (int argc, char **argv)
268 {
269 int i;
270 int ret = 0;
271 FcFontSet *fs;
272 FcStrSet *dirs;
273 FcStrSet *args = NULL;
274 FcStrList *arglist;
275 FcCache *cache;
276 FcConfig *config;
277 FcChar8 *arg;
278 int verbose = 0;
279 int recurse = 0;
280 FcBool first = FcTrue;
281 #if HAVE_GETOPT_LONG || HAVE_GETOPT
282 int c;
283
284 #if HAVE_GETOPT_LONG
285 while ((c = getopt_long (argc, argv, "Vvr?", longopts, NULL)) != -1)
286 #else
287 while ((c = getopt (argc, argv, "Vvr?")) != -1)
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);
295 case 'v':
296 verbose++;
297 break;
298 case 'r':
299 recurse++;
300 break;
301 default:
302 usage (argv[0]);
303 }
304 }
305 i = optind;
306 #else
307 i = 1;
308 #endif
309
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
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 {
328 if (!FcStrSetAddFilename (args, (const FcChar8 *) argv[i]))
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 }
359
360 while ((arg = FcStrListNext (arglist)))
361 {
362 int j;
363 FcChar8 *cache_file = NULL;
364 struct stat file_stat;
365
366 if (FcFileIsDir (arg))
367 cache = FcDirCacheLoad (arg, config, &cache_file);
368 else
369 cache = FcDirCacheLoadFile (arg, &file_stat);
370 if (!cache)
371 {
372 perror ((char *) arg);
373 ret++;
374 continue;
375 }
376
377 dirs = FcStrSetCreate ();
378 fs = FcCacheCopySet (cache);
379 for (j = 0; j < FcCacheNumSubdir (cache); j++)
380 {
381 FcStrSetAdd (dirs, FcCacheSubdir (cache, j));
382 if (recurse)
383 FcStrSetAdd (args, FcCacheSubdir (cache, j));
384 }
385
386 if (verbose)
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 }
394 cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
395
396 FcStrSetDestroy (dirs);
397
398 FcFontSetDestroy (fs);
399 FcDirCacheUnload (cache);
400 if (cache_file)
401 FcStrFree (cache_file);
402 }
403
404 return 0;
405 }