]> git.wh0rd.org - fontconfig.git/blame - fc-list/fc-list.c
Bug 26718 - "fc-match sans file" doesn't work
[fontconfig.git] / fc-list / fc-list.c
CommitLineData
24330d27 1/*
317b8492 2 * fontconfig/fc-list/fc-list.c
24330d27 3 *
46b51147 4 * Copyright © 2002 Keith Packard
24330d27
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
5aaf466d 10 * documentation, and that the name of the author(s) not be used in
24330d27 11 * advertising or publicity pertaining to distribution of the software without
5aaf466d 12 * specific, written prior permission. The authors make no
24330d27
KP
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
3074a73b 16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
24330d27 17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
3074a73b 18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24330d27
KP
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>
ccb3e93b 28#include <stdlib.h>
0c93b91d 29#include <string.h>
24330d27
KP
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#else
c4bd0638
MALF
33#ifdef linux
34#define HAVE_GETOPT_LONG 1
35#endif
24330d27
KP
36#define HAVE_GETOPT 1
37#endif
38
c4bd0638
MALF
39#ifndef HAVE_GETOPT
40#define HAVE_GETOPT 0
41#endif
42#ifndef HAVE_GETOPT_LONG
43#define HAVE_GETOPT_LONG 0
44#endif
45
24330d27 46#if HAVE_GETOPT_LONG
c4bd0638 47#undef _GNU_SOURCE
24330d27
KP
48#define _GNU_SOURCE
49#include <getopt.h>
50const struct option longopts[] = {
24330d27 51 {"verbose", 0, 0, 'v'},
0c93b91d 52 {"format", 1, 0, 'f'},
f26062b2 53 {"quiet", 0, 0, 'q'},
0c93b91d 54 {"version", 0, 0, 'V'},
1439c8f2 55 {"help", 0, 0, 'h'},
24330d27
KP
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
1439c8f2
BE
65static void
66usage (char *program, int error)
24330d27 67{
1439c8f2 68 FILE *file = error ? stderr : stdout;
86b12431 69#if HAVE_GETOPT_LONG
0c93b91d 70 fprintf (file, "usage: %s [-vqVh] [-f FORMAT] [--verbose] [--format=FORMAT] [--quiet] [--version] [--help] [pattern] {element ...} \n",
24330d27 71 program);
86b12431 72#else
0c93b91d 73 fprintf (file, "usage: %s [-vqVh] [-f FORMAT] [pattern] {element ...} \n",
86b12431
KP
74 program);
75#endif
1439c8f2
BE
76 fprintf (file, "List fonts matching [pattern]\n");
77 fprintf (file, "\n");
86b12431 78#if HAVE_GETOPT_LONG
41af588f 79 fprintf (file, " -v, --verbose display entire font pattern verbosely\n");
0c93b91d 80 fprintf (file, " -f, --format=FORMAT use the given output format\n");
f26062b2 81 fprintf (file, " -q, --quiet suppress all normal output, exit 1 if no fonts matched\n");
1439c8f2
BE
82 fprintf (file, " -V, --version display font config version and exit\n");
83 fprintf (file, " -h, --help display this help and exit\n");
86b12431 84#else
41af588f 85 fprintf (file, " -v (verbose) display entire font pattern verbosely\n");
0c93b91d 86 fprintf (file, " -f FORMAT (format) use the given output format\n");
f26062b2 87 fprintf (file, " -q, (quiet) suppress all normal output, exit 1 if no fonts matched\n");
1439c8f2
BE
88 fprintf (file, " -V (version) display font config version and exit\n");
89 fprintf (file, " -h (help) display this help and exit\n");
86b12431 90#endif
1439c8f2 91 exit (error);
24330d27
KP
92}
93
94int
95main (int argc, char **argv)
96{
29874098 97 int verbose = 0;
f26062b2 98 int quiet = 0;
0c93b91d 99 FcChar8 *format = NULL;
f26062b2 100 int nfont = 0;
24330d27 101 int i;
e6099fe9 102 FcObjectSet *os = 0;
24330d27
KP
103 FcFontSet *fs;
104 FcPattern *pat;
105#if HAVE_GETOPT_LONG || HAVE_GETOPT
106 int c;
107
108#if HAVE_GETOPT_LONG
0c93b91d 109 while ((c = getopt_long (argc, argv, "vf:qVh", longopts, NULL)) != -1)
24330d27 110#else
0c93b91d 111 while ((c = getopt (argc, argv, "vf:qVh")) != -1)
24330d27
KP
112#endif
113 {
114 switch (c) {
24330d27 115 case 'v':
29874098 116 verbose = 1;
24330d27 117 break;
0c93b91d
BE
118 case 'f':
119 format = (FcChar8 *) strdup (optarg);
120 break;
f26062b2
BE
121 case 'q':
122 quiet = 1;
123 break;
0c93b91d
BE
124 case 'V':
125 fprintf (stderr, "fontconfig version %d.%d.%d\n",
126 FC_MAJOR, FC_MINOR, FC_REVISION);
127 exit (0);
1439c8f2
BE
128 case 'h':
129 usage (argv[0], 0);
24330d27 130 default:
1439c8f2 131 usage (argv[0], 1);
24330d27
KP
132 }
133 }
134 i = optind;
135#else
136 i = 1;
137#endif
138
139 if (!FcInit ())
140 {
141 fprintf (stderr, "Can't init font config library\n");
142 return 1;
143 }
144 if (argv[i])
e6099fe9 145 {
80c053b7 146 pat = FcNameParse ((FcChar8 *) argv[i]);
41af588f
BE
147 while (argv[++i])
148 {
149 if (!os)
150 os = FcObjectSetCreate ();
151 FcObjectSetAdd (os, argv[i]);
152 }
e6099fe9 153 }
24330d27
KP
154 else
155 pat = FcPatternCreate ();
f26062b2
BE
156 if (quiet && !os)
157 os = FcObjectSetCreate ();
0c93b91d 158 if (!verbose && !format && !os)
e0be405a
BE
159 os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_FILE, (char *) 0);
160 if (!format)
161 format = "%{=fclist}\n";
24330d27 162 fs = FcFontList (0, pat, os);
29874098
BE
163 if (os)
164 FcObjectSetDestroy (os);
24330d27
KP
165 if (pat)
166 FcPatternDestroy (pat);
167
f26062b2 168 if (!quiet && fs)
24330d27
KP
169 {
170 int j;
171
172 for (j = 0; j < fs->nfont; j++)
173 {
29874098 174 if (verbose)
0c93b91d 175 {
29874098 176 FcPatternPrint (fs->fonts[j]);
0c93b91d 177 }
e0be405a 178 else
0c93b91d
BE
179 {
180 FcChar8 *s;
181
182 s = FcPatternFormat (fs->fonts[j], format);
8c31a243
BE
183 if (s)
184 {
185 printf ("%s", s);
186 free (s);
187 }
0c93b91d 188 }
24330d27 189 }
f26062b2
BE
190 }
191
192 if (fs) {
193 nfont = fs->nfont;
24330d27
KP
194 FcFontSetDestroy (fs);
195 }
34cd0514
CW
196
197 FcFini ();
198
f26062b2 199 return quiet ? (nfont == 0 ? 1 : 0) : 0;
24330d27 200}