]> git.wh0rd.org - fontconfig.git/blob - fc-cat/fc-cat.c
Fix warnings.
[fontconfig.git] / fc-cat / fc-cat.c
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
25 #include <fontconfig/fontconfig.h>
26 #include <../src/fccache.c>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <libgen.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #else
37 #ifdef linux
38 #define HAVE_GETOPT_LONG 1
39 #endif
40 #define HAVE_GETOPT 1
41 #endif
42
43 #ifndef HAVE_GETOPT
44 #define HAVE_GETOPT 0
45 #endif
46 #ifndef HAVE_GETOPT_LONG
47 #define HAVE_GETOPT_LONG 0
48 #endif
49
50 #if HAVE_GETOPT_LONG
51 #undef _GNU_SOURCE
52 #define _GNU_SOURCE
53 #include <getopt.h>
54 const struct option longopts[] = {
55 {"version", 0, 0, 'V'},
56 {"help", 0, 0, '?'},
57 {NULL,0,0,0},
58 };
59 #else
60 #if HAVE_GETOPT
61 extern char *optarg;
62 extern int optind, opterr, optopt;
63 #endif
64 #endif
65
66 /*
67 * POSIX has broken stdio so that getc must do thread-safe locking,
68 * this is a serious performance problem for applications doing large
69 * amounts of IO with getc (as is done here). If available, use
70 * the getc_unlocked varient instead.
71 */
72
73 #if defined(getc_unlocked) || defined(_IO_getc_unlocked)
74 #define GETC(f) getc_unlocked(f)
75 #define PUTC(c,f) putc_unlocked(c,f)
76 #else
77 #define GETC(f) getc(f)
78 #define PUTC(c,f) putc(c,f)
79 #endif
80
81 FcBool
82 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file);
83
84 static FcBool
85 FcCacheWriteChars (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 FcCacheWriteUlong (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 FcCacheWriteInt (FILE *f, int i)
131 {
132 return FcCacheWriteUlong (f, (unsigned long) i);
133 }
134
135 static FcBool
136 FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
137 {
138
139 if (PUTC ('"', f) == EOF)
140 return FcFalse;
141 if (!FcCacheWriteChars (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 [-V?] [--version] [--help] <fonts.cache-2>\n",
153 program);
154 #else
155 fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n",
156 program);
157 #endif
158 fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n");
159 fprintf (stderr, "\n");
160 #if HAVE_GETOPT_LONG
161 fprintf (stderr, " -V, --version display font config version and exit\n");
162 fprintf (stderr, " -?, --help display this help and exit\n");
163 #else
164 fprintf (stderr, " -V (version) display font config version and exit\n");
165 fprintf (stderr, " -? (help) display this help and exit\n");
166 #endif
167 exit (1);
168 }
169
170 static FcBool
171 FcCacheGlobalFileReadAndPrint (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
172 {
173 char name_buf[8192];
174 int fd;
175 char * current_arch_machine_name;
176 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
177 off_t current_arch_start = 0;
178
179 if (!cache_file)
180 goto bail;
181
182 current_arch_machine_name = FcCacheMachineSignature();
183 fd = open(cache_file, O_RDONLY);
184 if (fd == -1)
185 goto bail;
186
187 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
188 if (current_arch_start < 0)
189 goto bail1;
190
191 lseek (fd, current_arch_start, SEEK_SET);
192 if (FcCacheReadString (fd, candidate_arch_machine_name,
193 sizeof (candidate_arch_machine_name)) == 0)
194 goto bail1;
195
196 while (1)
197 {
198 FcCacheReadString (fd, name_buf, sizeof (name_buf));
199 if (!strlen(name_buf))
200 break;
201 printf ("fc-cat: printing global cache contents for dir %s\n",
202 name_buf);
203
204 if (!FcDirCacheConsume (fd, dir, set))
205 goto bail1;
206
207 FcCachePrintSet (set, dirs, name_buf);
208
209 FcFontSetDestroy (set);
210 set = FcFontSetCreate();
211 }
212
213 bail1:
214 close (fd);
215 bail:
216 return FcFalse;
217 }
218
219 /* read serialized state from the cache file */
220 static FcBool
221 FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
222 {
223 int fd;
224 char * current_arch_machine_name;
225 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
226 off_t current_arch_start = 0;
227 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
228
229 if (!cache_file)
230 goto bail;
231
232 current_arch_machine_name = FcCacheMachineSignature();
233 fd = open(cache_file, O_RDONLY);
234 if (fd == -1)
235 goto bail;
236
237 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
238 if (current_arch_start < 0)
239 goto bail1;
240
241 lseek (fd, current_arch_start, SEEK_SET);
242 if (FcCacheReadString (fd, candidate_arch_machine_name,
243 sizeof (candidate_arch_machine_name)) == 0)
244 goto bail1;
245
246 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
247 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
248
249 if (!FcDirCacheConsume (fd, dir, set))
250 goto bail1;
251
252 close(fd);
253 return FcTrue;
254
255 bail1:
256 close (fd);
257 bail:
258 return FcFalse;
259 }
260
261 /*
262 * return the path from the directory containing 'cache' to 'file'
263 */
264
265 static const FcChar8 *
266 FcFileBaseName (const char *cache, const FcChar8 *file)
267 {
268 const FcChar8 *cache_slash;
269
270 cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
271 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
272 (cache_slash + 1) - (const FcChar8 *)cache))
273 return file + ((cache_slash + 1) - (const FcChar8 *)cache);
274 return file;
275 }
276
277 FcBool
278 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
279 {
280 FcPattern *font;
281 FcChar8 *name, *dir;
282 const FcChar8 *file, *base;
283 int ret;
284 int n;
285 int id;
286 FcStrList *list;
287
288 list = FcStrListCreate (dirs);
289 if (!list)
290 goto bail2;
291
292 while ((dir = FcStrListNext (list)))
293 {
294 base = FcFileBaseName (cache_file, dir);
295 if (!FcCacheWriteStringOld (stdout, base))
296 goto bail3;
297 if (PUTC (' ', stdout) == EOF)
298 goto bail3;
299 if (!FcCacheWriteInt (stdout, 0))
300 goto bail3;
301 if (PUTC (' ', stdout) == EOF)
302 goto bail3;
303 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
304 goto bail3;
305 if (PUTC ('\n', stdout) == EOF)
306 goto bail3;
307 }
308
309 for (n = 0; n < set->nfont; n++)
310 {
311 font = set->fonts[n];
312 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
313 goto bail3;
314 base = FcFileBaseName (cache_file, file);
315 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
316 goto bail3;
317 if (FcDebug () & FC_DBG_CACHEV)
318 printf (" write file \"%s\"\n", base);
319 if (!FcCacheWriteStringOld (stdout, base))
320 goto bail3;
321 if (PUTC (' ', stdout) == EOF)
322 goto bail3;
323 if (!FcCacheWriteInt (stdout, id))
324 goto bail3;
325 if (PUTC (' ', stdout) == EOF)
326 goto bail3;
327 name = FcNameUnparse (font);
328 if (!name)
329 goto bail3;
330 ret = FcCacheWriteStringOld (stdout, name);
331 FcStrFree (name);
332 if (!ret)
333 goto bail3;
334 if (PUTC ('\n', stdout) == EOF)
335 goto bail3;
336 }
337
338 FcStrListDone (list);
339
340 return FcTrue;
341
342 bail3:
343 FcStrListDone (list);
344 bail2:
345 return FcFalse;
346 }
347
348 int
349 main (int argc, char **argv)
350 {
351 int i;
352 #if HAVE_GETOPT_LONG || HAVE_GETOPT
353 int c;
354 FcFontSet *fs = FcFontSetCreate();
355 FcStrSet *dirs = FcStrSetCreate();
356
357 #if HAVE_GETOPT_LONG
358 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
359 #else
360 while ((c = getopt (argc, argv, "fsVv?")) != -1)
361 #endif
362 {
363 switch (c) {
364 case 'V':
365 fprintf (stderr, "fontconfig version %d.%d.%d\n",
366 FC_MAJOR, FC_MINOR, FC_REVISION);
367 exit (0);
368 default:
369 usage (argv[0]);
370 }
371 }
372 i = optind;
373 #else
374 i = 1;
375 #endif
376
377 if (i >= argc)
378 usage (argv[0]);
379
380 if (FcCacheFileRead (fs, dirs, dirname (strdup(argv[i])), argv[i]))
381 FcCachePrintSet (fs, dirs, argv[i]);
382 else
383 {
384 FcStrSetDestroy (dirs);
385 dirs = FcStrSetCreate ();
386 if (FcCacheGlobalFileReadAndPrint (fs, dirs, dirname (strdup (argv[i])),
387 argv[i]))
388 ;
389 }
390
391 FcStrSetDestroy (dirs);
392 FcFontSetDestroy (fs);
393
394 return 0;
395 }