fc-list.c revision a32e9e42
12c393a42Smrg/*
2a6844aabSmrg * fontconfig/fc-list/fc-list.c
32c393a42Smrg *
42c393a42Smrg * Copyright © 2002 Keith Packard
52c393a42Smrg *
62c393a42Smrg * Permission to use, copy, modify, distribute, and sell this software and its
72c393a42Smrg * documentation for any purpose is hereby granted without fee, provided that
82c393a42Smrg * the above copyright notice appear in all copies and that both that
92c393a42Smrg * copyright notice and this permission notice appear in supporting
10ca08ab68Smrg * documentation, and that the name of the author(s) not be used in
112c393a42Smrg * advertising or publicity pertaining to distribution of the software without
12ca08ab68Smrg * specific, written prior permission.  The authors make no
132c393a42Smrg * representations about the suitability of this software for any purpose.  It
142c393a42Smrg * is provided "as is" without express or implied warranty.
152c393a42Smrg *
16a6844aabSmrg * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
172c393a42Smrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18a6844aabSmrg * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
192c393a42Smrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
202c393a42Smrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
212c393a42Smrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
222c393a42Smrg * PERFORMANCE OF THIS SOFTWARE.
232c393a42Smrg */
242c393a42Smrg
252c393a42Smrg#include <fontconfig/fontconfig.h>
262c393a42Smrg#include <stdio.h>
272c393a42Smrg#include <unistd.h>
282c393a42Smrg#include <stdlib.h>
29a6844aabSmrg#include <string.h>
30a32e9e42Smrg#include <locale.h>
312c393a42Smrg#ifdef HAVE_CONFIG_H
322c393a42Smrg#include <config.h>
332c393a42Smrg#else
342c393a42Smrg#ifdef linux
352c393a42Smrg#define HAVE_GETOPT_LONG 1
362c393a42Smrg#endif
372c393a42Smrg#define HAVE_GETOPT 1
382c393a42Smrg#endif
392c393a42Smrg
40a32e9e42Smrg#ifdef ENABLE_NLS
41a32e9e42Smrg#include <libintl.h>
42a32e9e42Smrg#define _(x)		(dgettext(GETTEXT_PACKAGE, x))
43a32e9e42Smrg#else
44a32e9e42Smrg#define dgettext(d, s)	(s)
45a32e9e42Smrg#define _(x)		(x)
46a32e9e42Smrg#endif
47a32e9e42Smrg
482c393a42Smrg#ifndef HAVE_GETOPT
492c393a42Smrg#define HAVE_GETOPT 0
502c393a42Smrg#endif
512c393a42Smrg#ifndef HAVE_GETOPT_LONG
522c393a42Smrg#define HAVE_GETOPT_LONG 0
532c393a42Smrg#endif
542c393a42Smrg
552c393a42Smrg#if HAVE_GETOPT_LONG
562c393a42Smrg#undef  _GNU_SOURCE
572c393a42Smrg#define _GNU_SOURCE
582c393a42Smrg#include <getopt.h>
592c393a42Smrgconst struct option longopts[] = {
602c393a42Smrg    {"verbose", 0, 0, 'v'},
61a32e9e42Smrg    {"brief", 0, 0, 'b'},
62a6844aabSmrg    {"format", 1, 0, 'f'},
63a6844aabSmrg    {"quiet", 0, 0, 'q'},
64a6844aabSmrg    {"version", 0, 0, 'V'},
65a6844aabSmrg    {"help", 0, 0, 'h'},
662c393a42Smrg    {NULL,0,0,0},
672c393a42Smrg};
682c393a42Smrg#else
692c393a42Smrg#if HAVE_GETOPT
702c393a42Smrgextern char *optarg;
712c393a42Smrgextern int optind, opterr, optopt;
722c393a42Smrg#endif
732c393a42Smrg#endif
742c393a42Smrg
75a6844aabSmrgstatic void
76a6844aabSmrgusage (char *program, int error)
772c393a42Smrg{
78a6844aabSmrg    FILE *file = error ? stderr : stdout;
792c393a42Smrg#if HAVE_GETOPT_LONG
80a32e9e42Smrg    fprintf (file, _("usage: %s [-vbqVh] [-f FORMAT] [--verbose] [--brief] [--format=FORMAT] [--quiet] [--version] [--help] [pattern] {element ...} \n"),
812c393a42Smrg	     program);
822c393a42Smrg#else
83a32e9e42Smrg    fprintf (file, _("usage: %s [-vbqVh] [-f FORMAT] [pattern] {element ...} \n"),
842c393a42Smrg	     program);
852c393a42Smrg#endif
86a32e9e42Smrg    fprintf (file, _("List fonts matching [pattern]\n"));
87a6844aabSmrg    fprintf (file, "\n");
882c393a42Smrg#if HAVE_GETOPT_LONG
89a32e9e42Smrg    fprintf (file, _("  -v, --verbose        display entire font pattern verbosely\n"));
90a32e9e42Smrg    fprintf (file, _("  -b, --brief          display entire font pattern briefly\n"));
91a32e9e42Smrg    fprintf (file, _("  -f, --format=FORMAT  use the given output format\n"));
92a32e9e42Smrg    fprintf (file, _("  -q, --quiet          suppress all normal output, exit 1 if no fonts matched\n"));
93a32e9e42Smrg    fprintf (file, _("  -V, --version        display font config version and exit\n"));
94a32e9e42Smrg    fprintf (file, _("  -h, --help           display this help and exit\n"));
952c393a42Smrg#else
96a32e9e42Smrg    fprintf (file, _("  -v         (verbose) display entire font pattern verbosely\n"));
97a32e9e42Smrg    fprintf (file, _("  -b         (brief)   display entire font pattern briefly\n"));
98a32e9e42Smrg    fprintf (file, _("  -f FORMAT  (format)  use the given output format\n"));
99a32e9e42Smrg    fprintf (file, _("  -q,        (quiet)   suppress all normal output, exit 1 if no fonts matched\n"));
100a32e9e42Smrg    fprintf (file, _("  -V         (version) display font config version and exit\n"));
101a32e9e42Smrg    fprintf (file, _("  -h         (help)    display this help and exit\n"));
1022c393a42Smrg#endif
103a6844aabSmrg    exit (error);
1042c393a42Smrg}
1052c393a42Smrg
1062c393a42Smrgint
1072c393a42Smrgmain (int argc, char **argv)
1082c393a42Smrg{
109ca08ab68Smrg    int			verbose = 0;
110a32e9e42Smrg    int			brief = 0;
111ca08ab68Smrg    int			quiet = 0;
112ca08ab68Smrg    const FcChar8	*format = NULL;
113ca08ab68Smrg    int			nfont = 0;
114ca08ab68Smrg    int			i;
115ca08ab68Smrg    FcObjectSet		*os = 0;
116ca08ab68Smrg    FcFontSet		*fs;
117ca08ab68Smrg    FcPattern		*pat;
1182c393a42Smrg#if HAVE_GETOPT_LONG || HAVE_GETOPT
119ca08ab68Smrg    int			c;
1202c393a42Smrg
121a32e9e42Smrg    setlocale (LC_ALL, "");
1222c393a42Smrg#if HAVE_GETOPT_LONG
123a32e9e42Smrg    while ((c = getopt_long (argc, argv, "vbf:qVh", longopts, NULL)) != -1)
1242c393a42Smrg#else
125a32e9e42Smrg    while ((c = getopt (argc, argv, "vbf:qVh")) != -1)
1262c393a42Smrg#endif
1272c393a42Smrg    {
1282c393a42Smrg	switch (c) {
129a6844aabSmrg	case 'v':
130a6844aabSmrg	    verbose = 1;
131a6844aabSmrg	    break;
132a32e9e42Smrg	case 'b':
133a32e9e42Smrg	    brief = 1;
134a32e9e42Smrg	    break;
135a6844aabSmrg	case 'f':
136a6844aabSmrg	    format = (FcChar8 *) strdup (optarg);
137a6844aabSmrg	    break;
138a6844aabSmrg	case 'q':
139a6844aabSmrg	    quiet = 1;
140a6844aabSmrg	    break;
1412c393a42Smrg	case 'V':
142a6844aabSmrg	    fprintf (stderr, "fontconfig version %d.%d.%d\n",
1432c393a42Smrg		     FC_MAJOR, FC_MINOR, FC_REVISION);
1442c393a42Smrg	    exit (0);
145a6844aabSmrg	case 'h':
146a6844aabSmrg	    usage (argv[0], 0);
1472c393a42Smrg	default:
148a6844aabSmrg	    usage (argv[0], 1);
1492c393a42Smrg	}
1502c393a42Smrg    }
1512c393a42Smrg    i = optind;
1522c393a42Smrg#else
1532c393a42Smrg    i = 1;
1542c393a42Smrg#endif
1552c393a42Smrg
1562c393a42Smrg    if (argv[i])
1572c393a42Smrg    {
1582c393a42Smrg	pat = FcNameParse ((FcChar8 *) argv[i]);
1596fc018e4Smrg	if (!pat)
1606fc018e4Smrg	{
161a32e9e42Smrg	    fprintf (stderr, _("Unable to parse the pattern\n"));
1626fc018e4Smrg	    return 1;
1636fc018e4Smrg	}
1642c393a42Smrg	while (argv[++i])
1652c393a42Smrg	{
1662c393a42Smrg	    if (!os)
1672c393a42Smrg		os = FcObjectSetCreate ();
1682c393a42Smrg	    FcObjectSetAdd (os, argv[i]);
1692c393a42Smrg	}
1702c393a42Smrg    }
1712c393a42Smrg    else
1722c393a42Smrg	pat = FcPatternCreate ();
173a6844aabSmrg    if (quiet && !os)
174a6844aabSmrg	os = FcObjectSetCreate ();
175a32e9e42Smrg    if (!verbose && !brief && !format && !os)
176ca08ab68Smrg	os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_FILE, (char *) 0);
177ca08ab68Smrg    if (!format)
178ca08ab68Smrg        format = (const FcChar8 *) "%{=fclist}\n";
1792c393a42Smrg    fs = FcFontList (0, pat, os);
180a6844aabSmrg    if (os)
181a6844aabSmrg	FcObjectSetDestroy (os);
1822c393a42Smrg    if (pat)
1832c393a42Smrg	FcPatternDestroy (pat);
1842c393a42Smrg
185a6844aabSmrg    if (!quiet && fs)
1862c393a42Smrg    {
1872c393a42Smrg	int	j;
1882c393a42Smrg
1892c393a42Smrg	for (j = 0; j < fs->nfont; j++)
1902c393a42Smrg	{
191a32e9e42Smrg	    if (verbose || brief)
192a6844aabSmrg	    {
193a32e9e42Smrg		if (brief)
194a32e9e42Smrg		{
195a32e9e42Smrg		    FcPatternDel (fs->fonts[j], FC_CHARSET);
196a32e9e42Smrg		    FcPatternDel (fs->fonts[j], FC_LANG);
197a32e9e42Smrg		}
198a6844aabSmrg		FcPatternPrint (fs->fonts[j]);
199a6844aabSmrg	    }
200ca08ab68Smrg	    else
201a6844aabSmrg	    {
202a6844aabSmrg	        FcChar8 *s;
203a6844aabSmrg
204a6844aabSmrg		s = FcPatternFormat (fs->fonts[j], format);
205a6844aabSmrg		if (s)
206a6844aabSmrg		{
207a6844aabSmrg		    printf ("%s", s);
208ca08ab68Smrg		    FcStrFree (s);
209a6844aabSmrg		}
210a6844aabSmrg	    }
2112c393a42Smrg	}
212a6844aabSmrg    }
213a6844aabSmrg
214a6844aabSmrg    if (fs) {
215a6844aabSmrg	nfont = fs->nfont;
2162c393a42Smrg	FcFontSetDestroy (fs);
2172c393a42Smrg    }
2182c393a42Smrg
2192c393a42Smrg    FcFini ();
2202c393a42Smrg
221a6844aabSmrg    return quiet ? (nfont == 0 ? 1 : 0) : 0;
2222c393a42Smrg}
223