Debian Codegraph @

subprojects/fontconfig/fc-cat/fc-cat.c

raw
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make 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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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 "../src/fcarch.h"
35 #include <fontconfig/fontconfig.h>
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #ifdef HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif
43 #include <errno.h>
44 #include <locale.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47
48 #ifdef ENABLE_NLS
49 # include <libintl.h>
50 # define _(x) (dgettext (GETTEXT_PACKAGE, x))
51 #else
52 # define dgettext(d, s) (s)
53 # define _(x) (x)
54 #endif
55
56 #ifndef HAVE_GETOPT
57 # define HAVE_GETOPT 0
58 #endif
59 #ifndef HAVE_GETOPT_LONG
60 # define HAVE_GETOPT_LONG 0
61 #endif
62
63 #if HAVE_GETOPT_LONG
64 # undef _GNU_SOURCE
65 # define _GNU_SOURCE
66 # include <getopt.h>
67 const struct option longopts[] = {
68 { "version", 0, 0, 'V' },
69 { "verbose", 0, 0, 'v' },
70 { "recurse", 0, 0, 'r' },
71 { "help", 0, 0, 'h' },
72 { NULL, 0, 0, 0 },
73 };
74 #else
75 # if HAVE_GETOPT
76 extern char *optarg;
77 extern int optind, opterr, optopt;
78 # endif
79 #endif
80
81 /*
82 * POSIX has broken stdio so that putc must do thread-safe locking,
83 * this is a serious performance problem for applications doing large
84 * amounts of IO with putc (as is done here). If available, use
85 * the putc_unlocked variant instead.
86 */
87
88 #if defined(putc_unlocked) || defined(_IO_putc_unlocked)
89 # define PUTC(c, f) putc_unlocked (c, f)
90 #else
91 # define PUTC(c, f) putc (c, f)
92 #endif
93
94 static FcBool
95 write_chars (FILE *f, const FcChar8 *chars)
96 {
97 FcChar8 c;
98 while ((c = *chars++)) {
99 switch (c) {
100 case '"':
101 case '\\':
102 if (PUTC ('\\', f) == EOF)
103 return FcFalse;
104 /* fall through */
105 default:
106 if (PUTC (c, f) == EOF)
107 return FcFalse;
108 }
109 }
110 return FcTrue;
111 }
112
113 static FcBool
114 write_ulong (FILE *f, unsigned long t)
115 {
116 int pow;
117 unsigned long temp, digit;
118
119 temp = t;
120 pow = 1;
121 while (temp >= 10) {
122 temp /= 10;
123 pow *= 10;
124 }
125 temp = t;
126 while (pow) {
127 digit = temp / pow;
128 if (PUTC ((char)digit + '0', f) == EOF)
129 return FcFalse;
130 temp = temp - pow * digit;
131 pow = pow / 10;
132 }
133 return FcTrue;
134 }
135
136 static FcBool
137 write_int (FILE *f, int i)
138 {
139 return write_ulong (f, (unsigned long)i);
140 }
141
142 static FcBool
143 write_string (FILE *f, const FcChar8 *string)
144 {
145 if (PUTC ('"', f) == EOF)
146 return FcFalse;
147 if (!write_chars (f, string))
148 return FcFalse;
149 if (PUTC ('"', f) == EOF)
150 return FcFalse;
151 return FcTrue;
152 }
153
154 static void
155 usage (char *program, int error)
156 {
157 FILE *file = error ? stderr : stdout;
158 #if HAVE_GETOPT_LONG
159 fprintf (file, _("usage: %s [-rv] [--recurse] [--verbose] [*-%s" FC_CACHE_SUFFIX "|directory]...\n"),
160 program, FC_ARCHITECTURE);
161 fprintf (file, " %s [-Vh] [--version] [--help]\n", program);
162 #else
163 fprintf (file, _("usage: %s [-rvVh] [*-%s" FC_CACHE_SUFFIX "|directory]...\n"),
164 program, FC_ARCHITECTURE);
165 #endif
166 fprintf (file, _("Reads font information cache from:\n"));
167 fprintf (file, _(" 1) specified fontconfig cache file\n"));
168 fprintf (file, _(" 2) related to a particular font directory\n"));
169 fprintf (file, "\n");
170 #if HAVE_GETOPT_LONG
171 fprintf (file, _(" -r, --recurse recurse into subdirectories\n"));
172 fprintf (file, _(" -v, --verbose be verbose\n"));
173 fprintf (file, _(" -V, --version display font config version and exit\n"));
174 fprintf (file, _(" -h, --help display this help and exit\n"));
175 #else
176 fprintf (file, _(" -r (recurse) recurse into subdirectories\n"));
177 fprintf (file, _(" -v (verbose) be verbose\n"));
178 fprintf (file, _(" -V (version) display font config version and exit\n"));
179 fprintf (file, _(" -h (help) display this help and exit\n"));
180 #endif
181 exit (error);
182 }
183
184 /*
185 * return the path from the directory containing 'cache' to 'file'
186 */
187
188 static const FcChar8 *
189 file_base_name (const FcChar8 *cache, const FcChar8 *file)
190 {
191 int cache_len = strlen ((char *)cache);
192
193 if (!strncmp ((char *)cache, (char *)file, cache_len) && file[cache_len] == '/')
194 return file + cache_len + 1;
195 return file;
196 }
197
198 #define FC_FONT_FILE_DIR ((FcChar8 *)".dir")
199
200 static FcBool
201 cache_print_set (FcFontSet *set, FcStrSet *dirs, const FcChar8 *base_name, FcBool verbose)
202 {
203 FcChar8 *dir;
204 const FcChar8 *base;
205 int n;
206 int ndir = 0;
207 FcStrList *list;
208
209 list = FcStrListCreate (dirs);
210 if (!list)
211 goto bail2;
212
213 while ((dir = FcStrListNext (list))) {
214 base = file_base_name (base_name, dir);
215 if (!write_string (stdout, base))
216 goto bail3;
217 if (PUTC (' ', stdout) == EOF)
218 goto bail3;
219 if (!write_int (stdout, 0))
220 goto bail3;
221 if (PUTC (' ', stdout) == EOF)
222 goto bail3;
223 if (!write_string (stdout, FC_FONT_FILE_DIR))
224 goto bail3;
225 if (PUTC ('\n', stdout) == EOF)
226 goto bail3;
227 ndir++;
228 }
229
230 for (n = 0; n < set->nfont; n++) {
231 FcPattern *font = set->fonts[n];
232 FcChar8 *s;
233
234 s = FcPatternFormat (font, (const FcChar8 *)"%{=fccat}\n");
235 if (s) {
236 printf ("%s", s);
237 FcStrFree (s);
238 }
239 }
240 if (verbose && !set->nfont && !ndir)
241 printf ("<empty>\n");
242
243 FcStrListDone (list);
244
245 return FcTrue;
246
247 bail3:
248 FcStrListDone (list);
249 bail2:
250 return FcFalse;
251 }
252
253 int
254 main (int argc, char **argv)
255 {
256 int i;
257 int ret = 0;
258 FcFontSet *fs;
259 FcStrSet *dirs;
260 FcStrSet *args = NULL;
261 FcStrList *arglist;
262 FcCache *cache;
263 FcConfig *config;
264 FcChar8 *arg;
265 int verbose = 0;
266 int recurse = 0;
267 FcBool first = FcTrue;
268 #if HAVE_GETOPT_LONG || HAVE_GETOPT
269 int c;
270
271 setlocale (LC_ALL, "");
272 # if HAVE_GETOPT_LONG
273 while ((c = getopt_long (argc, argv, "Vvrh", longopts, NULL)) != -1)
274 # else
275 while ((c = getopt (argc, argv, "Vvrh")) != -1)
276 # endif
277 {
278 switch (c) {
279 case 'V':
280 fprintf (stderr, "fontconfig version %d.%d.%d\n",
281 FC_MAJOR, FC_MINOR, FC_REVISION);
282 exit (0);
283 case 'v':
284 verbose++;
285 break;
286 case 'r':
287 recurse++;
288 break;
289 case 'h':
290 usage (argv[0], 0);
291 default:
292 usage (argv[0], 1);
293 }
294 }
295 i = optind;
296 #else
297 i = 1;
298 #endif
299
300 config = FcInitLoadConfig();
301 if (!config) {
302 fprintf (stderr, _("%s: Can't initialize font config library\n"), argv[0]);
303 return 1;
304 }
305 FcConfigSetCurrent (config);
306 FcConfigDestroy (config);
307
308 args = FcStrSetCreate();
309 if (!args) {
310 fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
311 return 1;
312 }
313 if (i < argc) {
314 for (; i < argc; i++) {
315 if (!FcStrSetAddFilename (args, (const FcChar8 *)argv[i])) {
316 fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
317 return 1;
318 }
319 }
320 } else {
321 recurse++;
322 arglist = FcConfigGetFontDirs (config);
323 while ((arg = FcStrListNext (arglist)))
324 if (!FcStrSetAdd (args, arg)) {
325 fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
326 return 1;
327 }
328 FcStrListDone (arglist);
329 }
330 arglist = FcStrListCreate (args);
331 if (!arglist) {
332 fprintf (stderr, _("%s: malloc failure\n"), argv[0]);
333 return 1;
334 }
335 FcStrSetDestroy (args);
336
337 while ((arg = FcStrListNext (arglist))) {
338 int j;
339 FcChar8 *cache_file = NULL;
340 struct stat file_stat;
341
342 /* reset errno */
343 errno = 0;
344 if (FcFileIsDir (arg))
345 cache = FcDirCacheLoad (arg, config, &cache_file);
346 else
347 cache = FcDirCacheLoadFile (arg, &file_stat);
348 if (!cache) {
349 if (errno != 0)
350 perror ((char *)arg);
351 else
352 fprintf (stderr, "%s: Unable to load the cache: %s\n", argv[0], arg);
353 ret++;
354 continue;
355 }
356
357 dirs = FcStrSetCreate();
358 fs = FcCacheCopySet (cache);
359 for (j = 0; j < FcCacheNumSubdir (cache); j++) {
360 FcStrSetAdd (dirs, FcCacheSubdir (cache, j));
361 if (recurse)
362 FcStrSetAdd (args, FcCacheSubdir (cache, j));
363 }
364
365 if (verbose) {
366 if (!first)
367 printf ("\n");
368 printf (_("Directory: %s\nCache: %s\n--------\n"),
369 FcCacheDir (cache), cache_file ? cache_file : arg);
370 first = FcFalse;
371 }
372 cache_print_set (fs, dirs, FcCacheDir (cache), verbose);
373
374 FcStrSetDestroy (dirs);
375
376 FcFontSetDestroy (fs);
377 FcDirCacheUnload (cache);
378 if (cache_file)
379 FcStrFree (cache_file);
380 }
381 FcStrListDone (arglist);
382
383 FcFini();
384 return 0;
385 }