]> git.wh0rd.org - fontconfig.git/blob - fc-list/fc-list.c
89f4167b5fc44e6173cbc694bbf4ec6811b02320
[fontconfig.git] / fc-list / fc-list.c
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 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 <stdlib.h>
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #else
32 #ifdef linux
33 #define HAVE_GETOPT_LONG 1
34 #endif
35 #define HAVE_GETOPT 1
36 #endif
37
38 #ifndef HAVE_GETOPT
39 #define HAVE_GETOPT 0
40 #endif
41 #ifndef HAVE_GETOPT_LONG
42 #define HAVE_GETOPT_LONG 0
43 #endif
44
45 #if HAVE_GETOPT_LONG
46 #undef _GNU_SOURCE
47 #define _GNU_SOURCE
48 #include <getopt.h>
49 const struct option longopts[] = {
50 {"version", 0, 0, 'V'},
51 {"verbose", 0, 0, 'v'},
52 {"quiet", 0, 0, 'q'},
53 {"help", 0, 0, 'h'},
54 {NULL,0,0,0},
55 };
56 #else
57 #if HAVE_GETOPT
58 extern char *optarg;
59 extern int optind, opterr, optopt;
60 #endif
61 #endif
62
63 static void
64 usage (char *program, int error)
65 {
66 FILE *file = error ? stderr : stdout;
67 #if HAVE_GETOPT_LONG
68 fprintf (file, "usage: %s [-vqVh] [--verbose] [--quiet] [--version] [--help] [pattern] {element ...} \n",
69 program);
70 #else
71 fprintf (file, "usage: %s [-vqVh] [pattern] {element ...} \n",
72 program);
73 #endif
74 fprintf (file, "List fonts matching [pattern]\n");
75 fprintf (file, "\n");
76 #if HAVE_GETOPT_LONG
77 fprintf (file, " -v, --verbose display entire font pattern\n");
78 fprintf (file, " -q, --quiet suppress all normal output, exit 1 if no fonts matched\n");
79 fprintf (file, " -V, --version display font config version and exit\n");
80 fprintf (file, " -h, --help display this help and exit\n");
81 #else
82 fprintf (file, " -v (verbose) display entire font pattern\n");
83 fprintf (file, " -q, (quiet) suppress all normal output, exit 1 if no fonts matched\n");
84 fprintf (file, " -V (version) display font config version and exit\n");
85 fprintf (file, " -h (help) display this help and exit\n");
86 #endif
87 exit (error);
88 }
89
90 int
91 main (int argc, char **argv)
92 {
93 int verbose = 0;
94 int quiet = 0;
95 int nfont = 0;
96 int i;
97 FcObjectSet *os = 0;
98 FcFontSet *fs;
99 FcPattern *pat;
100 #if HAVE_GETOPT_LONG || HAVE_GETOPT
101 int c;
102
103 #if HAVE_GETOPT_LONG
104 while ((c = getopt_long (argc, argv, "Vqvh", longopts, NULL)) != -1)
105 #else
106 while ((c = getopt (argc, argv, "Vqvh")) != -1)
107 #endif
108 {
109 switch (c) {
110 case 'V':
111 fprintf (stderr, "fontconfig version %d.%d.%d\n",
112 FC_MAJOR, FC_MINOR, FC_REVISION);
113 exit (0);
114 case 'v':
115 verbose = 1;
116 break;
117 case 'q':
118 quiet = 1;
119 break;
120 case 'h':
121 usage (argv[0], 0);
122 default:
123 usage (argv[0], 1);
124 }
125 }
126 i = optind;
127 #else
128 i = 1;
129 #endif
130
131 if (!FcInit ())
132 {
133 fprintf (stderr, "Can't init font config library\n");
134 return 1;
135 }
136 if (argv[i])
137 {
138 pat = FcNameParse ((FcChar8 *) argv[i]);
139 if (!verbose)
140 while (argv[++i])
141 {
142 if (!os)
143 os = FcObjectSetCreate ();
144 FcObjectSetAdd (os, argv[i]);
145 }
146 }
147 else
148 pat = FcPatternCreate ();
149 if (quiet && !os)
150 os = FcObjectSetCreate ();
151 if (!verbose && !os)
152 os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *) 0);
153 fs = FcFontList (0, pat, os);
154 if (os)
155 FcObjectSetDestroy (os);
156 if (pat)
157 FcPatternDestroy (pat);
158
159 if (!quiet && fs)
160 {
161 int j;
162
163 for (j = 0; j < fs->nfont; j++)
164 {
165 FcChar8 *font;
166 FcChar8 *file;
167
168 if (verbose)
169 FcPatternPrint (fs->fonts[j]);
170 else
171 {
172 font = FcNameUnparse (fs->fonts[j]);
173 if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
174 printf ("%s: ", file);
175 printf ("%s\n", font);
176 free (font);
177 }
178 }
179 }
180
181 if (fs) {
182 nfont = fs->nfont;
183 FcFontSetDestroy (fs);
184 }
185
186 FcFini ();
187
188 return quiet ? (nfont == 0 ? 1 : 0) : 0;
189 }