blob: 7989b816f5d97c5a9e84e6e26038a8c7804d689a [file] [log] [blame]
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -04001/*
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
Behdad Esfahbod5aaf4662010-11-10 16:45:42 -050010 * documentation, and that the name of the author(s) not be used in
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040011 * advertising or publicity pertaining to distribution of the software without
Behdad Esfahbod5aaf4662010-11-10 16:45:42 -050012 * specific, written prior permission. The authors make no
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040013 * 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>
Akira TAGOH2938e4d2018-03-15 12:54:02 +090039#include <locale.h>
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040040
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090041#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
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040049#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
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090080 fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [--config] [--default] [--verbose] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"),
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040081 program);
82#else
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090083 fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [pattern] {element...}\n"),
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040084 program);
85#endif
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090086 fprintf (file, _("List best font matching [pattern]\n"));
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040087 fprintf (file, "\n");
88#if HAVE_GETOPT_LONG
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090089 fprintf (file, _(" -c, --config perform config substitution on pattern\n"));
Behdad Esfahbod288d3342017-12-18 23:51:17 -050090 fprintf (file, _(" -d, --default perform default substitution on pattern\n"));
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090091 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"));
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040094#else
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090095 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"));
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400100#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
Akira TAGOH2938e4d2018-03-15 12:54:02 +0900115 setlocale (LC_ALL, "");
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400116#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
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400147 if (argv[i])
148 {
149 pat = FcNameParse ((FcChar8 *) argv[i]);
Akira TAGOH604c2a62013-10-03 19:59:30 +0900150 if (!pat)
151 {
Akira TAGOH9a0fcb92014-03-27 15:10:44 +0900152 fprintf (stderr, _("Unable to parse the pattern\n"));
Akira TAGOH604c2a62013-10-03 19:59:30 +0900153 return 1;
154 }
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400155 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);
Akira TAGOH1b692d82012-06-01 19:06:17 +0900189 FcStrFree (s);
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400190 }
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}