]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
4bd4418a | 2 | * $RCSId: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.8tsi Exp $ |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2002 Keith Packard |
24330d27 KP |
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 <stdio.h> | |
d1bec8c6 | 27 | #include <stdlib.h> |
24330d27 | 28 | #include <unistd.h> |
0c35c0fa KP |
29 | #include <sys/types.h> |
30 | #include <sys/stat.h> | |
31 | #include <errno.h> | |
24330d27 KP |
32 | #ifdef HAVE_CONFIG_H |
33 | #include <config.h> | |
34 | #else | |
c4bd0638 MALF |
35 | #ifdef linux |
36 | #define HAVE_GETOPT_LONG 1 | |
37 | #endif | |
24330d27 KP |
38 | #define HAVE_GETOPT 1 |
39 | #endif | |
40 | ||
c4bd0638 MALF |
41 | #ifndef HAVE_GETOPT |
42 | #define HAVE_GETOPT 0 | |
43 | #endif | |
44 | #ifndef HAVE_GETOPT_LONG | |
45 | #define HAVE_GETOPT_LONG 0 | |
46 | #endif | |
47 | ||
24330d27 | 48 | #if HAVE_GETOPT_LONG |
c4bd0638 | 49 | #undef _GNU_SOURCE |
24330d27 KP |
50 | #define _GNU_SOURCE |
51 | #include <getopt.h> | |
52 | const struct option longopts[] = { | |
179c3995 | 53 | {"force", 0, 0, 'f'}, |
ff3f1f98 | 54 | {"system-only", 0, 0, 's'}, |
24330d27 KP |
55 | {"version", 0, 0, 'V'}, |
56 | {"verbose", 0, 0, 'v'}, | |
57 | {"help", 0, 0, '?'}, | |
58 | {NULL,0,0,0}, | |
59 | }; | |
60 | #else | |
61 | #if HAVE_GETOPT | |
62 | extern char *optarg; | |
63 | extern int optind, opterr, optopt; | |
64 | #endif | |
65 | #endif | |
66 | ||
65018b4a KP |
67 | static void |
68 | usage (char *program) | |
24330d27 | 69 | { |
86b12431 KP |
70 | #if HAVE_GETOPT_LONG |
71 | fprintf (stderr, "usage: %s [-fsvV?] [--force] [--system-only] [--verbose] [--version] [--help] [dirs]\n", | |
72 | program); | |
73 | #else | |
74 | fprintf (stderr, "usage: %s [-fsvV?] [dirs]\n", | |
24330d27 | 75 | program); |
86b12431 | 76 | #endif |
24330d27 KP |
77 | fprintf (stderr, "Build font information caches in [dirs]\n" |
78 | "(all directories in font configuration by default).\n"); | |
79 | fprintf (stderr, "\n"); | |
86b12431 | 80 | #if HAVE_GETOPT_LONG |
80a7d664 | 81 | fprintf (stderr, " -f, --force scan directories with apparently valid caches\n"); |
ff3f1f98 | 82 | fprintf (stderr, " -s, --system-only scan system-wide directories only\n"); |
24330d27 KP |
83 | fprintf (stderr, " -v, --verbose display status information while busy\n"); |
84 | fprintf (stderr, " -V, --version display font config version and exit\n"); | |
85 | fprintf (stderr, " -?, --help display this help and exit\n"); | |
86b12431 KP |
86 | #else |
87 | fprintf (stderr, " -f (force) scan directories with apparently valid caches\n"); | |
88 | fprintf (stderr, " -s (system) scan system-wide directories only\n"); | |
89 | fprintf (stderr, " -v (verbose) display status information while busy\n"); | |
90 | fprintf (stderr, " -V (version) display font config version and exit\n"); | |
91 | fprintf (stderr, " -? (help) display this help and exit\n"); | |
92 | #endif | |
24330d27 KP |
93 | exit (1); |
94 | } | |
95 | ||
212c9f43 | 96 | #if 0 |
179c3995 KP |
97 | static int |
98 | nsubdirs (FcStrSet *set) | |
99 | { | |
100 | FcStrList *list; | |
101 | int n = 0; | |
102 | ||
103 | list = FcStrListCreate (set); | |
104 | if (!list) | |
105 | return 0; | |
106 | while (FcStrListNext (list)) | |
107 | n++; | |
108 | FcStrListDone (list); | |
109 | return n; | |
110 | } | |
111 | ||
112 | static int | |
ad07dcf4 | 113 | scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool verbose) |
24330d27 KP |
114 | { |
115 | int ret = 0; | |
179c3995 | 116 | FcChar8 *dir; |
24330d27 | 117 | FcFontSet *set; |
179c3995 KP |
118 | FcStrSet *subdirs; |
119 | FcStrList *sublist; | |
0c35c0fa | 120 | struct stat statb; |
179c3995 KP |
121 | |
122 | /* | |
123 | * Now scan all of the directories into separate databases | |
124 | * and write out the results | |
125 | */ | |
126 | while ((dir = FcStrListNext (list))) | |
127 | { | |
128 | if (verbose) | |
129 | { | |
130 | printf ("%s: \"%s\": ", program, dir); | |
131 | fflush (stdout); | |
132 | } | |
133 | set = FcFontSetCreate (); | |
134 | if (!set) | |
135 | { | |
136 | fprintf (stderr, "Can't create font set\n"); | |
137 | ret++; | |
138 | continue; | |
139 | } | |
140 | subdirs = FcStrSetCreate (); | |
141 | if (!subdirs) | |
142 | { | |
143 | fprintf (stderr, "Can't create directory set\n"); | |
144 | ret++; | |
5c1853cd | 145 | FcFontSetDestroy (set); |
179c3995 KP |
146 | continue; |
147 | } | |
0c35c0fa | 148 | |
565a919e | 149 | if (access ((char *) dir, W_OK) < 0) |
0c35c0fa | 150 | { |
565a919e KP |
151 | switch (errno) { |
152 | case ENOENT: | |
153 | case ENOTDIR: | |
5d43e799 | 154 | if (verbose) |
565a919e KP |
155 | printf ("skipping, no such directory\n"); |
156 | break; | |
157 | case EACCES: | |
158 | case EROFS: | |
159 | if (verbose) | |
160 | printf ("skipping, no write access\n"); | |
161 | break; | |
162 | default: | |
0c35c0fa KP |
163 | fprintf (stderr, "\"%s\": ", dir); |
164 | perror (""); | |
165 | ret++; | |
166 | } | |
5c1853cd KP |
167 | FcFontSetDestroy (set); |
168 | FcStrSetDestroy (subdirs); | |
0c35c0fa KP |
169 | continue; |
170 | } | |
565a919e KP |
171 | if (stat ((char *) dir, &statb) == -1) |
172 | { | |
173 | fprintf (stderr, "\"%s\": ", dir); | |
174 | perror (""); | |
5c1853cd KP |
175 | FcFontSetDestroy (set); |
176 | FcStrSetDestroy (subdirs); | |
565a919e KP |
177 | ret++; |
178 | continue; | |
179 | } | |
0c35c0fa KP |
180 | if (!S_ISDIR (statb.st_mode)) |
181 | { | |
182 | fprintf (stderr, "\"%s\": not a directory, skipping\n", dir); | |
5c1853cd KP |
183 | FcFontSetDestroy (set); |
184 | FcStrSetDestroy (subdirs); | |
0c35c0fa KP |
185 | continue; |
186 | } | |
ad07dcf4 | 187 | if (!FcDirScan (set, subdirs, 0, FcConfigGetBlanks (config), dir, force)) |
179c3995 | 188 | { |
0c35c0fa | 189 | fprintf (stderr, "\"%s\": error scanning\n", dir); |
5c1853cd KP |
190 | FcFontSetDestroy (set); |
191 | FcStrSetDestroy (subdirs); | |
179c3995 KP |
192 | ret++; |
193 | continue; | |
194 | } | |
195 | if (!force && FcDirCacheValid (dir)) | |
196 | { | |
197 | if (verbose) | |
198 | printf ("skipping, %d fonts, %d dirs\n", | |
199 | set->nfont, nsubdirs(subdirs)); | |
200 | } | |
201 | else | |
202 | { | |
203 | if (verbose) | |
204 | printf ("caching, %d fonts, %d dirs\n", | |
205 | set->nfont, nsubdirs (subdirs)); | |
206 | if (!FcDirSave (set, subdirs, dir)) | |
207 | { | |
208 | fprintf (stderr, "Can't save cache in \"%s\"\n", dir); | |
209 | ret++; | |
210 | } | |
211 | } | |
212 | FcFontSetDestroy (set); | |
213 | sublist = FcStrListCreate (subdirs); | |
5c1853cd | 214 | FcStrSetDestroy (subdirs); |
179c3995 KP |
215 | if (!sublist) |
216 | { | |
217 | fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir); | |
218 | ret++; | |
219 | continue; | |
220 | } | |
ad07dcf4 | 221 | ret += scanDirs (sublist, config, program, force, verbose); |
179c3995 KP |
222 | } |
223 | FcStrListDone (list); | |
224 | return ret; | |
225 | } | |
212c9f43 | 226 | #endif |
179c3995 KP |
227 | |
228 | int | |
229 | main (int argc, char **argv) | |
230 | { | |
231 | FcStrSet *dirs; | |
232 | FcStrList *list; | |
233 | FcBool verbose = FcFalse; | |
234 | FcBool force = FcFalse; | |
ff3f1f98 | 235 | FcBool systemOnly = FcFalse; |
179c3995 | 236 | FcConfig *config; |
24330d27 | 237 | int i; |
179c3995 | 238 | int ret; |
24330d27 KP |
239 | #if HAVE_GETOPT_LONG || HAVE_GETOPT |
240 | int c; | |
241 | ||
242 | #if HAVE_GETOPT_LONG | |
86b12431 | 243 | while ((c = getopt_long (argc, argv, "fsVv?", longopts, NULL)) != -1) |
24330d27 | 244 | #else |
86b12431 | 245 | while ((c = getopt (argc, argv, "fsVv?")) != -1) |
24330d27 KP |
246 | #endif |
247 | { | |
248 | switch (c) { | |
179c3995 KP |
249 | case 'f': |
250 | force = FcTrue; | |
251 | break; | |
ff3f1f98 KP |
252 | case 's': |
253 | systemOnly = FcTrue; | |
254 | break; | |
24330d27 KP |
255 | case 'V': |
256 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
257 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
258 | exit (0); | |
259 | case 'v': | |
179c3995 | 260 | verbose = FcTrue; |
24330d27 KP |
261 | break; |
262 | default: | |
263 | usage (argv[0]); | |
264 | } | |
265 | } | |
266 | i = optind; | |
267 | #else | |
268 | i = 1; | |
269 | #endif | |
270 | ||
ff3f1f98 KP |
271 | if (systemOnly) |
272 | FcConfigEnableHome (FcFalse); | |
212c9f43 PL |
273 | FcCacheForce (FcTrue); |
274 | /* need to use FcInitLoadConfig when we use dirs */ | |
275 | FcInit (); | |
276 | config = FcConfigGetCurrent (); | |
179c3995 | 277 | if (!config) |
24330d27 | 278 | { |
179c3995 | 279 | fprintf (stderr, "%s: Can't init font config library\n", argv[0]); |
24330d27 KP |
280 | return 1; |
281 | } | |
212c9f43 PL |
282 | |
283 | /* We don't yet have per-directory caches. */ | |
284 | ret = (FcCacheWrite (config) == FcFalse); | |
285 | ||
286 | #if 0 | |
24330d27 | 287 | if (argv[i]) |
24330d27 | 288 | { |
179c3995 KP |
289 | dirs = FcStrSetCreate (); |
290 | if (!dirs) | |
24330d27 | 291 | { |
179c3995 KP |
292 | fprintf (stderr, "%s: Can't create list of directories\n", |
293 | argv[0]); | |
294 | return 1; | |
24330d27 | 295 | } |
179c3995 | 296 | while (argv[i]) |
24330d27 | 297 | { |
179c3995 | 298 | if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i])) |
24330d27 | 299 | { |
179c3995 KP |
300 | fprintf (stderr, "%s: Can't add directory\n", argv[0]); |
301 | return 1; | |
24330d27 | 302 | } |
179c3995 | 303 | i++; |
24330d27 | 304 | } |
179c3995 KP |
305 | list = FcStrListCreate (dirs); |
306 | FcStrSetDestroy (dirs); | |
24330d27 | 307 | } |
179c3995 KP |
308 | else |
309 | list = FcConfigGetConfigDirs (config); | |
ad07dcf4 | 310 | ret = scanDirs (list, config, argv[0], force, verbose); |
212c9f43 | 311 | #endif |
54560b01 KP |
312 | /* |
313 | * Now we need to sleep a second (or two, to be extra sure), to make | |
314 | * sure that timestamps for changes after this run of fc-cache are later | |
315 | * then any timestamps we wrote. We don't use gettimeofday() because | |
316 | * sleep(3) can't be interrupted by a signal here -- this isn't in the | |
317 | * library, and there aren't any signals flying around here. | |
318 | */ | |
716ac8b8 | 319 | FcConfigDestroy (config); |
54560b01 | 320 | sleep (2); |
24330d27 KP |
321 | if (verbose) |
322 | printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded"); | |
323 | return ret; | |
324 | } |