]> git.wh0rd.org - fontconfig.git/blame - fc-match/fc-match.c
Skip broken caches. Cache files are auto-written, don't rewrite in fc-cache.
[fontconfig.git] / fc-match / fc-match.c
CommitLineData
8bc4bc13
KP
1/*
2 * $RCSId: xc/lib/fontconfig/fc-list/fc-list.c,v 1.5 2002/06/30 23:45:40 keithp Exp $
3 *
46b51147 4 * Copyright © 2003 Keith Packard
8bc4bc13
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
8bc4bc13
KP
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#else
28#ifdef linux
29#define HAVE_GETOPT_LONG 1
30#endif
31#define HAVE_GETOPT 1
32#endif
33
f045376c
PL
34#include <fontconfig/fontconfig.h>
35#include <stdio.h>
36#include <unistd.h>
37#include <stdlib.h>
38#include <string.h>
39
8bc4bc13
KP
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>
0d745819 51static const struct option longopts[] = {
8bc4bc13
KP
52 {"sort", 0, 0, 's'},
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
60extern char *optarg;
61extern int optind, opterr, optopt;
62#endif
63#endif
64
65static void usage (char *program)
66{
67#if HAVE_GETOPT_LONG
68 fprintf (stderr, "usage: %s [-svV?] [--sort] [--verbose] [--version] [--help] [pattern]\n",
69 program);
70#else
71 fprintf (stderr, "usage: %s [-svV?] [pattern]\n",
72 program);
73#endif
74 fprintf (stderr, "List fonts matching [pattern]\n");
75 fprintf (stderr, "\n");
76#if HAVE_GETOPT_LONG
77 fprintf (stderr, " -s, --sort display sorted list of matches\n");
78 fprintf (stderr, " -v, --verbose display entire font pattern\n");
79 fprintf (stderr, " -V, --version display font config version and exit\n");
80 fprintf (stderr, " -?, --help display this help and exit\n");
81#else
82 fprintf (stderr, " -s, (sort) display sorted list of matches\n");
83 fprintf (stderr, " -v (verbose) display entire font pattern\n");
84 fprintf (stderr, " -V (version) display font config version and exit\n");
85 fprintf (stderr, " -? (help) display this help and exit\n");
86#endif
87 exit (1);
88}
89
90int
91main (int argc, char **argv)
92{
93 int verbose = 0;
94 int sort = 0;
95 int i;
8bc4bc13
KP
96 FcFontSet *fs;
97 FcPattern *pat;
98 FcResult result;
99#if HAVE_GETOPT_LONG || HAVE_GETOPT
100 int c;
101
102#if HAVE_GETOPT_LONG
435fc660 103 while ((c = getopt_long (argc, argv, "sVv?", longopts, NULL)) != -1)
8bc4bc13
KP
104#else
105 while ((c = getopt (argc, argv, "sVv?")) != -1)
106#endif
107 {
108 switch (c) {
109 case 's':
110 sort = 1;
111 break;
112 case 'V':
113 fprintf (stderr, "fontconfig version %d.%d.%d\n",
114 FC_MAJOR, FC_MINOR, FC_REVISION);
115 exit (0);
116 case 'v':
117 verbose = 1;
118 break;
119 default:
120 usage (argv[0]);
121 }
122 }
123 i = optind;
124#else
125 i = 1;
126#endif
127
128 if (!FcInit ())
129 {
130 fprintf (stderr, "Can't init font config library\n");
131 return 1;
132 }
133 if (argv[i])
134 pat = FcNameParse ((FcChar8 *) argv[i]);
135 else
136 pat = FcPatternCreate ();
137
ae2aafe6
PL
138 if (!pat)
139 return 1;
140
8bc4bc13
KP
141 FcConfigSubstitute (0, pat, FcMatchPattern);
142 FcDefaultSubstitute (pat);
143
144 if (sort)
145 fs = FcFontSort (0, pat, FcTrue, 0, &result);
146 else
147 {
148 FcPattern *match;
149 fs = FcFontSetCreate ();
150 match = FcFontMatch (0, pat, &result);
151 if (match)
152 FcFontSetAdd (fs, match);
153 }
ae2aafe6 154 FcPatternDestroy (pat);
8bc4bc13
KP
155
156 if (fs)
157 {
158 int j;
159
160 for (j = 0; j < fs->nfont; j++)
161 {
162 if (verbose)
163 {
164 FcPatternPrint (fs->fonts[j]);
165 }
166 else
167 {
168 FcChar8 *family;
169 FcChar8 *style;
170 FcChar8 *file;
171
172 if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) != FcResultMatch)
8245771d 173 file = (FcChar8 *) "<unknown filename>";
8bc4bc13
KP
174 else
175 {
8245771d 176 FcChar8 *slash = (FcChar8 *) strrchr ((char *) file, '/');
8bc4bc13
KP
177 if (slash)
178 file = slash+1;
179 }
180 if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch)
8245771d 181 family = (FcChar8 *) "<unknown family>";
8bc4bc13 182 if (FcPatternGetString (fs->fonts[j], FC_STYLE, 0, &style) != FcResultMatch)
8245771d 183 file = (FcChar8 *) "<unknown style>";
8bc4bc13
KP
184
185 printf ("%s: \"%s\" \"%s\"\n", file, family, style);
186 }
187 }
188 FcFontSetDestroy (fs);
189 }
2d3387fd 190 FcFini ();
8bc4bc13
KP
191 return 0;
192}