]> git.wh0rd.org - fontconfig.git/blame - fc-list/fc-list.c
Implement fc-list --quiet ala grep (bug #17141)
[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
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>
ccb3e93b 28#include <stdlib.h>
24330d27
KP
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#else
c4bd0638
MALF
32#ifdef linux
33#define HAVE_GETOPT_LONG 1
34#endif
24330d27
KP
35#define HAVE_GETOPT 1
36#endif
37
c4bd0638
MALF
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
24330d27 45#if HAVE_GETOPT_LONG
c4bd0638 46#undef _GNU_SOURCE
24330d27
KP
47#define _GNU_SOURCE
48#include <getopt.h>
49const struct option longopts[] = {
50 {"version", 0, 0, 'V'},
51 {"verbose", 0, 0, 'v'},
f26062b2 52 {"quiet", 0, 0, 'q'},
1439c8f2 53 {"help", 0, 0, 'h'},
24330d27
KP
54 {NULL,0,0,0},
55};
56#else
57#if HAVE_GETOPT
58extern char *optarg;
59extern int optind, opterr, optopt;
60#endif
61#endif
62
1439c8f2
BE
63static void
64usage (char *program, int error)
24330d27 65{
1439c8f2 66 FILE *file = error ? stderr : stdout;
86b12431 67#if HAVE_GETOPT_LONG
f26062b2 68 fprintf (file, "usage: %s [-vqVh] [--verbose] [--quiet] [--version] [--help] [pattern] {element ...} \n",
24330d27 69 program);
86b12431 70#else
f26062b2 71 fprintf (file, "usage: %s [-vqVh] [pattern] {element ...} \n",
86b12431
KP
72 program);
73#endif
1439c8f2
BE
74 fprintf (file, "List fonts matching [pattern]\n");
75 fprintf (file, "\n");
86b12431 76#if HAVE_GETOPT_LONG
1439c8f2 77 fprintf (file, " -v, --verbose display entire font pattern\n");
f26062b2 78 fprintf (file, " -q, --quiet suppress all normal output, exit 1 if no fonts matched\n");
1439c8f2
BE
79 fprintf (file, " -V, --version display font config version and exit\n");
80 fprintf (file, " -h, --help display this help and exit\n");
86b12431 81#else
1439c8f2 82 fprintf (file, " -v (verbose) display entire font pattern\n");
f26062b2 83 fprintf (file, " -q, (quiet) suppress all normal output, exit 1 if no fonts matched\n");
1439c8f2
BE
84 fprintf (file, " -V (version) display font config version and exit\n");
85 fprintf (file, " -h (help) display this help and exit\n");
86b12431 86#endif
1439c8f2 87 exit (error);
24330d27
KP
88}
89
90int
91main (int argc, char **argv)
92{
29874098 93 int verbose = 0;
f26062b2
BE
94 int quiet = 0;
95 int nfont = 0;
24330d27 96 int i;
e6099fe9 97 FcObjectSet *os = 0;
24330d27
KP
98 FcFontSet *fs;
99 FcPattern *pat;
100#if HAVE_GETOPT_LONG || HAVE_GETOPT
101 int c;
102
103#if HAVE_GETOPT_LONG
f26062b2 104 while ((c = getopt_long (argc, argv, "Vqvh", longopts, NULL)) != -1)
24330d27 105#else
f26062b2 106 while ((c = getopt (argc, argv, "Vqvh")) != -1)
24330d27
KP
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':
29874098 115 verbose = 1;
24330d27 116 break;
f26062b2
BE
117 case 'q':
118 quiet = 1;
119 break;
1439c8f2
BE
120 case 'h':
121 usage (argv[0], 0);
24330d27 122 default:
1439c8f2 123 usage (argv[0], 1);
24330d27
KP
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])
e6099fe9 137 {
80c053b7 138 pat = FcNameParse ((FcChar8 *) argv[i]);
29874098
BE
139 if (!verbose)
140 while (argv[++i])
141 {
142 if (!os)
143 os = FcObjectSetCreate ();
144 FcObjectSetAdd (os, argv[i]);
145 }
e6099fe9 146 }
24330d27
KP
147 else
148 pat = FcPatternCreate ();
f26062b2
BE
149 if (quiet && !os)
150 os = FcObjectSetCreate ();
29874098 151 if (!verbose && !os)
18906a87 152 os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *) 0);
24330d27 153 fs = FcFontList (0, pat, os);
29874098
BE
154 if (os)
155 FcObjectSetDestroy (os);
24330d27
KP
156 if (pat)
157 FcPatternDestroy (pat);
158
f26062b2 159 if (!quiet && fs)
24330d27
KP
160 {
161 int j;
162
163 for (j = 0; j < fs->nfont; j++)
164 {
165 FcChar8 *font;
e6099fe9 166 FcChar8 *file;
24330d27 167
29874098
BE
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 }
24330d27 178 }
f26062b2
BE
179 }
180
181 if (fs) {
182 nfont = fs->nfont;
24330d27
KP
183 FcFontSetDestroy (fs);
184 }
34cd0514
CW
185
186 FcFini ();
187
f26062b2 188 return quiet ? (nfont == 0 ? 1 : 0) : 0;
24330d27 189}