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