]> git.wh0rd.org - fontconfig.git/blame - fc-list/fc-list.c
Implement fc-list --verbose (#13015)
[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'},
52 {"help", 0, 0, '?'},
53 {NULL,0,0,0},
54};
55#else
56#if HAVE_GETOPT
57extern char *optarg;
58extern int optind, opterr, optopt;
59#endif
60#endif
61
ccb3e93b 62static void usage (char *program)
24330d27 63{
86b12431 64#if HAVE_GETOPT_LONG
e6099fe9 65 fprintf (stderr, "usage: %s [-vV?] [--verbose] [--version] [--help] [pattern] {element ...} \n",
24330d27 66 program);
86b12431
KP
67#else
68 fprintf (stderr, "usage: %s [-vV?] [pattern] {element ...} \n",
69 program);
70#endif
e6099fe9 71 fprintf (stderr, "List fonts matching [pattern]\n");
24330d27 72 fprintf (stderr, "\n");
86b12431 73#if HAVE_GETOPT_LONG
29874098 74 fprintf (stderr, " -v, --verbose display entire font pattern\n");
24330d27
KP
75 fprintf (stderr, " -V, --version display font config version and exit\n");
76 fprintf (stderr, " -?, --help display this help and exit\n");
86b12431 77#else
29874098 78 fprintf (stderr, " -v (verbose) display entire font pattern\n");
86b12431
KP
79 fprintf (stderr, " -V (version) display font config version and exit\n");
80 fprintf (stderr, " -? (help) display this help and exit\n");
81#endif
24330d27
KP
82 exit (1);
83}
84
85int
86main (int argc, char **argv)
87{
29874098 88 int verbose = 0;
24330d27 89 int i;
e6099fe9 90 FcObjectSet *os = 0;
24330d27
KP
91 FcFontSet *fs;
92 FcPattern *pat;
93#if HAVE_GETOPT_LONG || HAVE_GETOPT
94 int c;
95
96#if HAVE_GETOPT_LONG
97 while ((c = getopt_long (argc, argv, "Vv?", longopts, NULL)) != -1)
98#else
99 while ((c = getopt (argc, argv, "Vv?")) != -1)
100#endif
101 {
102 switch (c) {
103 case 'V':
104 fprintf (stderr, "fontconfig version %d.%d.%d\n",
105 FC_MAJOR, FC_MINOR, FC_REVISION);
106 exit (0);
107 case 'v':
29874098 108 verbose = 1;
24330d27
KP
109 break;
110 default:
111 usage (argv[0]);
112 }
113 }
114 i = optind;
115#else
116 i = 1;
117#endif
118
119 if (!FcInit ())
120 {
121 fprintf (stderr, "Can't init font config library\n");
122 return 1;
123 }
124 if (argv[i])
e6099fe9 125 {
80c053b7 126 pat = FcNameParse ((FcChar8 *) argv[i]);
29874098
BE
127 if (!verbose)
128 while (argv[++i])
129 {
130 if (!os)
131 os = FcObjectSetCreate ();
132 FcObjectSetAdd (os, argv[i]);
133 }
e6099fe9 134 }
24330d27
KP
135 else
136 pat = FcPatternCreate ();
137
29874098 138 if (!verbose && !os)
18906a87 139 os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *) 0);
24330d27 140 fs = FcFontList (0, pat, os);
29874098
BE
141 if (os)
142 FcObjectSetDestroy (os);
24330d27
KP
143 if (pat)
144 FcPatternDestroy (pat);
145
146 if (fs)
147 {
148 int j;
149
150 for (j = 0; j < fs->nfont; j++)
151 {
152 FcChar8 *font;
e6099fe9 153 FcChar8 *file;
24330d27 154
29874098
BE
155 if (verbose)
156 FcPatternPrint (fs->fonts[j]);
157 else
158 {
159 font = FcNameUnparse (fs->fonts[j]);
160 if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
161 printf ("%s: ", file);
162 printf ("%s\n", font);
163 free (font);
164 }
24330d27
KP
165 }
166 FcFontSetDestroy (fs);
167 }
34cd0514
CW
168
169 FcFini ();
170
24330d27
KP
171 return 0;
172}