fc-pattern.c revision a32e9e42
1/*
2 * fontconfig/fc-pattern/fc-pattern.c
3 *
4 * Copyright © 2003 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#ifdef HAVE_CONFIG_H
26#include <config.h>
27#else
28#ifdef linux
29#define HAVE_GETOPT_LONG 1
30#endif
31#define HAVE_GETOPT 1
32#endif
33
34#include <fontconfig/fontconfig.h>
35#include <stdio.h>
36#include <unistd.h>
37#include <stdlib.h>
38#include <string.h>
39#include <locale.h>
40
41#ifdef ENABLE_NLS
42#include <libintl.h>
43#define _(x)		(dgettext(GETTEXT_PACKAGE, x))
44#else
45#define dgettext(d, s)	(s)
46#define _(x)		(x)
47#endif
48
49#ifndef HAVE_GETOPT
50#define HAVE_GETOPT 0
51#endif
52#ifndef HAVE_GETOPT_LONG
53#define HAVE_GETOPT_LONG 0
54#endif
55
56#if HAVE_GETOPT_LONG
57#undef  _GNU_SOURCE
58#define _GNU_SOURCE
59#include <getopt.h>
60static const struct option longopts[] = {
61    {"config", 0, 0, 'c'},
62    {"default", 0, 0, 'd'},
63    {"format", 1, 0, 'f'},
64    {"version", 0, 0, 'V'},
65    {"help", 0, 0, 'h'},
66    {NULL,0,0,0},
67};
68#else
69#if HAVE_GETOPT
70extern char *optarg;
71extern int optind, opterr, optopt;
72#endif
73#endif
74
75static void
76usage (char *program, int error)
77{
78    FILE *file = error ? stderr : stdout;
79#if HAVE_GETOPT_LONG
80    fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [--config] [--default] [--verbose] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"),
81	     program);
82#else
83    fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [pattern] {element...}\n"),
84	     program);
85#endif
86    fprintf (file, _("List best font matching [pattern]\n"));
87    fprintf (file, "\n");
88#if HAVE_GETOPT_LONG
89    fprintf (file, _("  -c, --config         perform config substitution on pattern\n"));
90    fprintf (file, _("  -d, --default        perform default substitution on pattern\n"));
91    fprintf (file, _("  -f, --format=FORMAT  use the given output format\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, _("  -c,        (config)  perform config substitution on pattern\n"));
96    fprintf (file, _("  -d,        (default) perform default substitution on pattern\n"));
97    fprintf (file, _("  -f FORMAT  (format)  use the given output format\n"));
98    fprintf (file, _("  -V         (version) display font config version and exit\n"));
99    fprintf (file, _("  -h         (help)    display this help and exit\n"));
100#endif
101    exit (error);
102}
103
104int
105main (int argc, char **argv)
106{
107    int		do_config = 0, do_default = 0;
108    FcChar8     *format = NULL;
109    int		i;
110    FcObjectSet *os = 0;
111    FcPattern   *pat;
112#if HAVE_GETOPT_LONG || HAVE_GETOPT
113    int		c;
114
115    setlocale (LC_ALL, "");
116#if HAVE_GETOPT_LONG
117    while ((c = getopt_long (argc, argv, "cdf:Vh", longopts, NULL)) != -1)
118#else
119    while ((c = getopt (argc, argv, "cdf:Vh")) != -1)
120#endif
121    {
122	switch (c) {
123	case 'c':
124	    do_config = 1;
125	    break;
126	case 'd':
127	    do_default = 1;
128	    break;
129	case 'f':
130	    format = (FcChar8 *) strdup (optarg);
131	    break;
132	case 'V':
133	    fprintf (stderr, "fontconfig version %d.%d.%d\n",
134		     FC_MAJOR, FC_MINOR, FC_REVISION);
135	    exit (0);
136	case 'h':
137	    usage (argv[0], 0);
138	default:
139	    usage (argv[0], 1);
140	}
141    }
142    i = optind;
143#else
144    i = 1;
145#endif
146
147    if (argv[i])
148    {
149	pat = FcNameParse ((FcChar8 *) argv[i]);
150	if (!pat)
151	{
152	    fprintf (stderr, _("Unable to parse the pattern\n"));
153	    return 1;
154	}
155	while (argv[++i])
156	{
157	    if (!os)
158		os = FcObjectSetCreate ();
159	    FcObjectSetAdd (os, argv[i]);
160	}
161    }
162    else
163	pat = FcPatternCreate ();
164
165    if (!pat)
166	return 1;
167
168    if (do_config)
169      FcConfigSubstitute (0, pat, FcMatchPattern);
170    if (do_default)
171      FcDefaultSubstitute (pat);
172
173    if (os)
174    {
175      FcPattern *new;
176      new = FcPatternFilter (pat, os);
177      FcPatternDestroy (pat);
178      pat = new;
179    }
180
181    if (format)
182    {
183	FcChar8 *s;
184
185	s = FcPatternFormat (pat, format);
186	if (s)
187	{
188	    printf ("%s", s);
189	    FcStrFree (s);
190	}
191    }
192    else
193    {
194	FcPatternPrint (pat);
195    }
196
197    FcPatternDestroy (pat);
198
199    if (os)
200	FcObjectSetDestroy (os);
201
202    FcFini ();
203
204    return 0;
205}
206