1 /*
2 * fontconfig/fc-list/fc-list.c
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make 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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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 <stdlib.h>
29 #include <string.h>
30 #include <locale.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 #ifdef ENABLE_NLS
41 #include <libintl.h>
42 #define _(x) (dgettext(GETTEXT_PACKAGE, x))
43 #else
44 #define dgettext(d, s) (s)
45 #define _(x) (x)
46 #endif
47
48 #ifndef HAVE_GETOPT
49 #define HAVE_GETOPT 0
50 #endif
51 #ifndef HAVE_GETOPT_LONG
52 #define HAVE_GETOPT_LONG 0
53 #endif
54
55 #if HAVE_GETOPT_LONG
56 #undef _GNU_SOURCE
57 #define _GNU_SOURCE
58 #include <getopt.h>
59 const struct option longopts[] = {
60 {"verbose", 0, 0, 'v'},
61 {"brief", 0, 0, 'b'},
62 {"format", 1, 0, 'f'},
63 {"quiet", 0, 0, 'q'},
64 {"version", 0, 0, 'V'},
65 {"help", 0, 0, 'h'},
66 {NULL,0,0,0},
67 };
68 #else
69 #if HAVE_GETOPT
70 extern char *optarg;
71 extern int optind, opterr, optopt;
72 #endif
73 #endif
74
75 static void
usage(char * program,int error)76 usage (char *program, int error)
77 {
78 FILE *file = error ? stderr : stdout;
79 #if HAVE_GETOPT_LONG
80 fprintf (file, _("usage: %s [-vbqVh] [-f FORMAT] [--verbose] [--brief] [--format=FORMAT] [--quiet] [--version] [--help] [pattern] {element ...} \n"),
81 program);
82 #else
83 fprintf (file, _("usage: %s [-vbqVh] [-f FORMAT] [pattern] {element ...} \n"),
84 program);
85 #endif
86 fprintf (file, _("List fonts matching [pattern]\n"));
87 fprintf (file, "\n");
88 #if HAVE_GETOPT_LONG
89 fprintf (file, _(" -v, --verbose display entire font pattern verbosely\n"));
90 fprintf (file, _(" -b, --brief display entire font pattern briefly\n"));
91 fprintf (file, _(" -f, --format=FORMAT use the given output format\n"));
92 fprintf (file, _(" -q, --quiet suppress all normal output, exit 1 if no fonts matched\n"));
93 fprintf (file, _(" -V, --version display font config version and exit\n"));
94 fprintf (file, _(" -h, --help display this help and exit\n"));
95 #else
96 fprintf (file, _(" -v (verbose) display entire font pattern verbosely\n"));
97 fprintf (file, _(" -b (brief) display entire font pattern briefly\n"));
98 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
99 fprintf (file, _(" -q, (quiet) suppress all normal output, exit 1 if no fonts matched\n"));
100 fprintf (file, _(" -V (version) display font config version and exit\n"));
101 fprintf (file, _(" -h (help) display this help and exit\n"));
102 #endif
103 exit (error);
104 }
105
106 int
main(int argc,char ** argv)107 main (int argc, char **argv)
108 {
109 int verbose = 0;
110 int brief = 0;
111 int quiet = 0;
112 const FcChar8 *format = NULL;
113 int nfont = 0;
114 int i;
115 FcObjectSet *os = 0;
116 FcFontSet *fs;
117 FcPattern *pat;
118 #if HAVE_GETOPT_LONG || HAVE_GETOPT
119 int c;
120
121 setlocale (LC_ALL, "");
122 #if HAVE_GETOPT_LONG
123 while ((c = getopt_long (argc, argv, "vbf:qVh", longopts, NULL)) != -1)
124 #else
125 while ((c = getopt (argc, argv, "vbf:qVh")) != -1)
126 #endif
127 {
128 switch (c) {
129 case 'v':
130 verbose = 1;
131 break;
132 case 'b':
133 brief = 1;
134 break;
135 case 'f':
136 format = (FcChar8 *) strdup (optarg);
137 break;
138 case 'q':
139 quiet = 1;
140 break;
141 case 'V':
142 fprintf (stderr, "fontconfig version %d.%d.%d\n",
143 FC_MAJOR, FC_MINOR, FC_REVISION);
144 exit (0);
145 case 'h':
146 usage (argv[0], 0);
147 default:
148 usage (argv[0], 1);
149 }
150 }
151 i = optind;
152 #else
153 i = 1;
154 #endif
155
156 if (argv[i])
157 {
158 pat = FcNameParse ((FcChar8 *) argv[i]);
159 if (!pat)
160 {
161 fprintf (stderr, _("Unable to parse the pattern\n"));
162 return 1;
163 }
164 while (argv[++i])
165 {
166 if (!os)
167 os = FcObjectSetCreate ();
168 FcObjectSetAdd (os, argv[i]);
169 }
170 }
171 else
172 pat = FcPatternCreate ();
173 if (quiet && !os)
174 os = FcObjectSetCreate ();
175 if (!verbose && !brief && !format && !os)
176 os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_FILE, (char *) 0);
177 if (!format)
178 format = (const FcChar8 *) "%{=fclist}\n";
179 fs = FcFontList (0, pat, os);
180 if (os)
181 FcObjectSetDestroy (os);
182 if (pat)
183 FcPatternDestroy (pat);
184
185 if (!quiet && fs)
186 {
187 int j;
188
189 for (j = 0; j < fs->nfont; j++)
190 {
191 if (verbose || brief)
192 {
193 if (brief)
194 {
195 FcPatternDel (fs->fonts[j], FC_CHARSET);
196 FcPatternDel (fs->fonts[j], FC_LANG);
197 }
198 FcPatternPrint (fs->fonts[j]);
199 }
200 else
201 {
202 FcChar8 *s;
203
204 s = FcPatternFormat (fs->fonts[j], format);
205 if (s)
206 {
207 printf ("%s", s);
208 FcStrFree (s);
209 }
210 }
211 }
212 }
213
214 if (fs) {
215 nfont = fs->nfont;
216 FcFontSetDestroy (fs);
217 }
218
219 FcFini ();
220
221 return quiet ? (nfont == 0 ? 1 : 0) : 0;
222 }
223