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