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