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