]> git.wh0rd.org - fontconfig.git/blob - fc-cat/fc-cat.c
Don't crash when fc-cat invoked with no arguments.
[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 static FcBool
82 FcCacheWriteChars (FILE *f, const FcChar8 *chars)
83 {
84 FcChar8 c;
85 while ((c = *chars++))
86 {
87 switch (c) {
88 case '"':
89 case '\\':
90 if (PUTC ('\\', f) == EOF)
91 return FcFalse;
92 /* fall through */
93 default:
94 if (PUTC (c, f) == EOF)
95 return FcFalse;
96 }
97 }
98 return FcTrue;
99 }
100
101 static FcBool
102 FcCacheWriteUlong (FILE *f, unsigned long t)
103 {
104 int pow;
105 unsigned long temp, digit;
106
107 temp = t;
108 pow = 1;
109 while (temp >= 10)
110 {
111 temp /= 10;
112 pow *= 10;
113 }
114 temp = t;
115 while (pow)
116 {
117 digit = temp / pow;
118 if (PUTC ((char) digit + '0', f) == EOF)
119 return FcFalse;
120 temp = temp - pow * digit;
121 pow = pow / 10;
122 }
123 return FcTrue;
124 }
125
126 static FcBool
127 FcCacheWriteInt (FILE *f, int i)
128 {
129 return FcCacheWriteUlong (f, (unsigned long) i);
130 }
131
132 static FcBool
133 FcCacheWriteStringOld (FILE *f, const FcChar8 *string)
134 {
135
136 if (PUTC ('"', f) == EOF)
137 return FcFalse;
138 if (!FcCacheWriteChars (f, string))
139 return FcFalse;
140 if (PUTC ('"', f) == EOF)
141 return FcFalse;
142 return FcTrue;
143 }
144
145 static void
146 usage (char *program)
147 {
148 #if HAVE_GETOPT_LONG
149 fprintf (stderr, "usage: %s [-V?] [--version] [--help] <fonts.cache-2>\n",
150 program);
151 #else
152 fprintf (stderr, "usage: %s [-fsvV?] <fonts.cache-2>\n",
153 program);
154 #endif
155 fprintf (stderr, "Reads font information caches in <fonts.cache-2>\n");
156 fprintf (stderr, "\n");
157 #if HAVE_GETOPT_LONG
158 fprintf (stderr, " -V, --version display font config version and exit\n");
159 fprintf (stderr, " -?, --help display this help and exit\n");
160 #else
161 fprintf (stderr, " -V (version) display font config version and exit\n");
162 fprintf (stderr, " -? (help) display this help and exit\n");
163 #endif
164 exit (1);
165 }
166
167 /* read serialized state from the cache file */
168 static FcBool
169 FcCacheFileRead (FcFontSet * set, FcStrSet *dirs, char * dir, char *cache_file)
170 {
171 int fd;
172 char * current_arch_machine_name;
173 char candidate_arch_machine_name[9+MACHINE_SIGNATURE_SIZE];
174 off_t current_arch_start = 0;
175 char subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
176
177 if (!cache_file)
178 goto bail;
179
180 current_arch_machine_name = FcCacheMachineSignature();
181 fd = open(cache_file, O_RDONLY);
182 if (fd == -1)
183 goto bail;
184
185 current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
186 if (current_arch_start < 0)
187 goto bail1;
188
189 lseek (fd, current_arch_start, SEEK_SET);
190 if (FcCacheReadString (fd, candidate_arch_machine_name,
191 sizeof (candidate_arch_machine_name)) == 0)
192 goto bail1;
193
194 while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
195 FcStrSetAdd (dirs, (FcChar8 *)subdirName);
196
197 if (!FcDirCacheConsume (fd, dir, set))
198 goto bail1;
199
200 close(fd);
201 return FcTrue;
202
203 bail1:
204 close (fd);
205 bail:
206 return FcFalse;
207 }
208
209 /*
210 * return the path from the directory containing 'cache' to 'file'
211 */
212
213 static const FcChar8 *
214 FcFileBaseName (const char *cache, const FcChar8 *file)
215 {
216 const FcChar8 *cache_slash;
217
218 cache_slash = FcStrLastSlash ((const FcChar8 *)cache);
219 if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
220 (cache_slash + 1) - (const FcChar8 *)cache))
221 return file + ((cache_slash + 1) - (const FcChar8 *)cache);
222 return file;
223 }
224
225 FcBool
226 FcCachePrintSet (FcFontSet *set, FcStrSet *dirs, char *cache_file)
227 {
228 FcPattern *font;
229 FcChar8 *name, *dir;
230 const FcChar8 *file, *base;
231 int n;
232 int id;
233 FcBool ret;
234 FcStrList *list;
235
236 list = FcStrListCreate (dirs);
237 if (!list)
238 goto bail2;
239
240 while ((dir = FcStrListNext (list)))
241 {
242 base = FcFileBaseName (cache_file, dir);
243 if (!FcCacheWriteStringOld (stdout, base))
244 goto bail3;
245 if (PUTC (' ', stdout) == EOF)
246 goto bail3;
247 if (!FcCacheWriteInt (stdout, 0))
248 goto bail3;
249 if (PUTC (' ', stdout) == EOF)
250 goto bail3;
251 if (!FcCacheWriteStringOld (stdout, FC_FONT_FILE_DIR))
252 goto bail3;
253 if (PUTC ('\n', stdout) == EOF)
254 goto bail3;
255 }
256
257 for (n = 0; n < set->nfont; n++)
258 {
259 font = set->fonts[n];
260 if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
261 goto bail3;
262 base = FcFileBaseName (cache_file, file);
263 if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
264 goto bail3;
265 if (FcDebug () & FC_DBG_CACHEV)
266 printf (" write file \"%s\"\n", base);
267 if (!FcCacheWriteStringOld (stdout, base))
268 goto bail3;
269 if (PUTC (' ', stdout) == EOF)
270 goto bail3;
271 if (!FcCacheWriteInt (stdout, id))
272 goto bail3;
273 if (PUTC (' ', stdout) == EOF)
274 goto bail3;
275 name = FcNameUnparse (font);
276 if (!name)
277 goto bail3;
278 ret = FcCacheWriteStringOld (stdout, name);
279 FcStrFree (name);
280 if (!ret)
281 goto bail3;
282 if (PUTC ('\n', stdout) == EOF)
283 goto bail3;
284 }
285
286 FcStrListDone (list);
287
288 return FcTrue;
289
290 bail3:
291 FcStrListDone (list);
292 bail2:
293 bail1:
294 bail0:
295 return FcFalse;
296 }
297
298 int
299 main (int argc, char **argv)
300 {
301 int i;
302 int ret;
303 #if HAVE_GETOPT_LONG || HAVE_GETOPT
304 int c;
305 FcFontSet *fs = FcFontSetCreate();
306 FcStrSet *dirs = FcStrSetCreate();
307
308 #if HAVE_GETOPT_LONG
309 while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1)
310 #else
311 while ((c = getopt (argc, argv, "fsVv?")) != -1)
312 #endif
313 {
314 switch (c) {
315 case 'V':
316 fprintf (stderr, "fontconfig version %d.%d.%d\n",
317 FC_MAJOR, FC_MINOR, FC_REVISION);
318 exit (0);
319 default:
320 usage (argv[0]);
321 }
322 }
323 i = optind;
324 #else
325 i = 1;
326 #endif
327
328 if (i >= argc)
329 usage (argv[0]);
330
331 if (FcCacheFileRead (fs, dirs, dirname (strdup(argv[i])), argv[i]))
332 FcCachePrintSet (fs, dirs, argv[i]);
333
334 FcStrSetDestroy (dirs);
335 FcFontSetDestroy (fs);
336
337 return 0;
338 }