]> git.wh0rd.org - fontconfig.git/blob - fc-cache/fc-cache.c
Help message said -v was for --force
[fontconfig.git] / fc-cache / fc-cache.c
1 /*
2 * $XFree86: xc/lib/fontconfig/fc-cache/fc-cache.c,v 1.6 2002/07/01 05:11:20 keithp Exp $
3 *
4 * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
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>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #else
34 #ifdef linux
35 #define HAVE_GETOPT_LONG 1
36 #endif
37 #define HAVE_GETOPT 1
38 #endif
39
40 #ifndef HAVE_GETOPT
41 #define HAVE_GETOPT 0
42 #endif
43 #ifndef HAVE_GETOPT_LONG
44 #define HAVE_GETOPT_LONG 0
45 #endif
46
47 #if HAVE_GETOPT_LONG
48 #undef _GNU_SOURCE
49 #define _GNU_SOURCE
50 #include <getopt.h>
51 const struct option longopts[] = {
52 {"force", 0, 0, 'f'},
53 {"version", 0, 0, 'V'},
54 {"verbose", 0, 0, 'v'},
55 {"help", 0, 0, '?'},
56 {NULL,0,0,0},
57 };
58 #else
59 #if HAVE_GETOPT
60 extern char *optarg;
61 extern int optind, opterr, optopt;
62 #endif
63 #endif
64
65 static void
66 usage (char *program)
67 {
68 fprintf (stderr, "usage: %s [-fvV?] [--force] [--verbose] [--version] [--help] [dirs]\n",
69 program);
70 fprintf (stderr, "Build font information caches in [dirs]\n"
71 "(all directories in font configuration by default).\n");
72 fprintf (stderr, "\n");
73 fprintf (stderr, " -f, --force scan directories with apparently valid caches\n");
74 fprintf (stderr, " -v, --verbose display status information while busy\n");
75 fprintf (stderr, " -V, --version display font config version and exit\n");
76 fprintf (stderr, " -?, --help display this help and exit\n");
77 exit (1);
78 }
79
80 static int
81 nsubdirs (FcStrSet *set)
82 {
83 FcStrList *list;
84 int n = 0;
85
86 list = FcStrListCreate (set);
87 if (!list)
88 return 0;
89 while (FcStrListNext (list))
90 n++;
91 FcStrListDone (list);
92 return n;
93 }
94
95 static int
96 scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool verbose)
97 {
98 int ret = 0;
99 FcChar8 *dir;
100 FcFontSet *set;
101 FcStrSet *subdirs;
102 FcStrList *sublist;
103 struct stat statb;
104
105 /*
106 * Now scan all of the directories into separate databases
107 * and write out the results
108 */
109 while ((dir = FcStrListNext (list)))
110 {
111 if (verbose)
112 {
113 printf ("%s: \"%s\": ", program, dir);
114 fflush (stdout);
115 }
116 set = FcFontSetCreate ();
117 if (!set)
118 {
119 fprintf (stderr, "Can't create font set\n");
120 ret++;
121 continue;
122 }
123 subdirs = FcStrSetCreate ();
124 if (!subdirs)
125 {
126 fprintf (stderr, "Can't create directory set\n");
127 ret++;
128 continue;
129 }
130
131 if (stat ((char *) dir, &statb) == -1)
132 {
133 if (errno == ENOENT || errno == ENOTDIR)
134 {
135 fprintf (stderr, "\"%s\": no such directory, skipping\n", dir);
136 }
137 else
138 {
139 fprintf (stderr, "\"%s\": ", dir);
140 perror ("");
141 ret++;
142 }
143 continue;
144 }
145 if (!S_ISDIR (statb.st_mode))
146 {
147 fprintf (stderr, "\"%s\": not a directory, skipping\n", dir);
148 continue;
149 }
150 if (!FcDirScan (set, subdirs, 0, FcConfigGetBlanks (config), dir, force))
151 {
152 fprintf (stderr, "\"%s\": error scanning\n", dir);
153 ret++;
154 continue;
155 }
156 if (!force && FcDirCacheValid (dir))
157 {
158 if (verbose)
159 printf ("skipping, %d fonts, %d dirs\n",
160 set->nfont, nsubdirs(subdirs));
161 }
162 else
163 {
164 if (verbose)
165 printf ("caching, %d fonts, %d dirs\n",
166 set->nfont, nsubdirs (subdirs));
167 if (!FcDirSave (set, subdirs, dir))
168 {
169 fprintf (stderr, "Can't save cache in \"%s\"\n", dir);
170 ret++;
171 }
172 }
173 FcFontSetDestroy (set);
174 sublist = FcStrListCreate (subdirs);
175 if (!sublist)
176 {
177 fprintf (stderr, "Can't create subdir list in \"%s\"\n", dir);
178 ret++;
179 continue;
180 }
181 ret += scanDirs (sublist, config, program, force, verbose);
182 FcStrSetDestroy (subdirs);
183 }
184 FcStrListDone (list);
185 return ret;
186 }
187
188 int
189 main (int argc, char **argv)
190 {
191 FcStrSet *dirs;
192 FcStrList *list;
193 FcBool verbose = FcFalse;
194 FcBool force = FcFalse;
195 FcConfig *config;
196 int i;
197 int ret;
198 #if HAVE_GETOPT_LONG || HAVE_GETOPT
199 int c;
200
201 #if HAVE_GETOPT_LONG
202 while ((c = getopt_long (argc, argv, "fVv?", longopts, NULL)) != -1)
203 #else
204 while ((c = getopt (argc, argv, "fVv?")) != -1)
205 #endif
206 {
207 switch (c) {
208 case 'f':
209 force = FcTrue;
210 break;
211 case 'V':
212 fprintf (stderr, "fontconfig version %d.%d.%d\n",
213 FC_MAJOR, FC_MINOR, FC_REVISION);
214 exit (0);
215 case 'v':
216 verbose = FcTrue;
217 break;
218 default:
219 usage (argv[0]);
220 }
221 }
222 i = optind;
223 #else
224 i = 1;
225 #endif
226
227 config = FcInitLoadConfig ();
228 if (!config)
229 {
230 fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
231 return 1;
232 }
233 if (argv[i])
234 {
235 dirs = FcStrSetCreate ();
236 if (!dirs)
237 {
238 fprintf (stderr, "%s: Can't create list of directories\n",
239 argv[0]);
240 return 1;
241 }
242 while (argv[i])
243 {
244 if (!FcStrSetAdd (dirs, (FcChar8 *) argv[i]))
245 {
246 fprintf (stderr, "%s: Can't add directory\n", argv[0]);
247 return 1;
248 }
249 i++;
250 }
251 list = FcStrListCreate (dirs);
252 FcStrSetDestroy (dirs);
253 }
254 else
255 list = FcConfigGetConfigDirs (config);
256 ret = scanDirs (list, config, argv[0], force, verbose);
257 if (verbose)
258 printf ("%s: %s\n", argv[0], ret ? "failed" : "succeeded");
259 return ret;
260 }