blob: 1e9b1ba7a1db90c30f8fc828b31fc09ca075183c [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>
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040036#include <stdlib.h>
37#include <string.h>
Akira TAGOH2938e4d2018-03-15 12:54:02 +090038#include <locale.h>
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040039
Tim-Philipp Müllera30e4db2020-07-07 03:48:34 +000040#ifdef HAVE_UNISTD_H
41#include <unistd.h>
42#endif
43
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090044#ifdef ENABLE_NLS
45#include <libintl.h>
46#define _(x) (dgettext(GETTEXT_PACKAGE, x))
47#else
48#define dgettext(d, s) (s)
49#define _(x) (x)
50#endif
51
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040052#ifndef HAVE_GETOPT
53#define HAVE_GETOPT 0
54#endif
55#ifndef HAVE_GETOPT_LONG
56#define HAVE_GETOPT_LONG 0
57#endif
58
59#if HAVE_GETOPT_LONG
60#undef _GNU_SOURCE
61#define _GNU_SOURCE
62#include <getopt.h>
63static const struct option longopts[] = {
64 {"config", 0, 0, 'c'},
65 {"default", 0, 0, 'd'},
66 {"format", 1, 0, 'f'},
67 {"version", 0, 0, 'V'},
68 {"help", 0, 0, 'h'},
69 {NULL,0,0,0},
70};
71#else
72#if HAVE_GETOPT
73extern char *optarg;
74extern int optind, opterr, optopt;
75#endif
76#endif
77
78static void
79usage (char *program, int error)
80{
81 FILE *file = error ? stderr : stdout;
82#if HAVE_GETOPT_LONG
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090083 fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [--config] [--default] [--verbose] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"),
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040084 program);
85#else
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090086 fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [pattern] {element...}\n"),
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040087 program);
88#endif
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090089 fprintf (file, _("List best font matching [pattern]\n"));
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040090 fprintf (file, "\n");
91#if HAVE_GETOPT_LONG
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090092 fprintf (file, _(" -c, --config perform config substitution on pattern\n"));
Behdad Esfahbod288d3342017-12-18 23:51:17 -050093 fprintf (file, _(" -d, --default perform default substitution on pattern\n"));
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090094 fprintf (file, _(" -f, --format=FORMAT use the given output format\n"));
95 fprintf (file, _(" -V, --version display font config version and exit\n"));
96 fprintf (file, _(" -h, --help display this help and exit\n"));
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040097#else
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090098 fprintf (file, _(" -c, (config) perform config substitution on pattern\n"));
99 fprintf (file, _(" -d, (default) perform default substitution on pattern\n"));
100 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
101 fprintf (file, _(" -V (version) display font config version and exit\n"));
102 fprintf (file, _(" -h (help) display this help and exit\n"));
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400103#endif
104 exit (error);
105}
106
107int
108main (int argc, char **argv)
109{
110 int do_config = 0, do_default = 0;
111 FcChar8 *format = NULL;
112 int i;
113 FcObjectSet *os = 0;
114 FcPattern *pat;
115#if HAVE_GETOPT_LONG || HAVE_GETOPT
116 int c;
117
Akira TAGOH2938e4d2018-03-15 12:54:02 +0900118 setlocale (LC_ALL, "");
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400119#if HAVE_GETOPT_LONG
120 while ((c = getopt_long (argc, argv, "cdf:Vh", longopts, NULL)) != -1)
121#else
122 while ((c = getopt (argc, argv, "cdf:Vh")) != -1)
123#endif
124 {
125 switch (c) {
126 case 'c':
127 do_config = 1;
128 break;
129 case 'd':
130 do_default = 1;
131 break;
132 case 'f':
133 format = (FcChar8 *) strdup (optarg);
134 break;
135 case 'V':
136 fprintf (stderr, "fontconfig version %d.%d.%d\n",
137 FC_MAJOR, FC_MINOR, FC_REVISION);
138 exit (0);
139 case 'h':
140 usage (argv[0], 0);
141 default:
142 usage (argv[0], 1);
143 }
144 }
145 i = optind;
146#else
147 i = 1;
148#endif
149
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400150 if (argv[i])
151 {
152 pat = FcNameParse ((FcChar8 *) argv[i]);
Akira TAGOH604c2a62013-10-03 19:59:30 +0900153 if (!pat)
154 {
Akira TAGOH9a0fcb92014-03-27 15:10:44 +0900155 fprintf (stderr, _("Unable to parse the pattern\n"));
Akira TAGOH604c2a62013-10-03 19:59:30 +0900156 return 1;
157 }
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400158 while (argv[++i])
159 {
160 if (!os)
161 os = FcObjectSetCreate ();
162 FcObjectSetAdd (os, argv[i]);
163 }
164 }
165 else
166 pat = FcPatternCreate ();
167
168 if (!pat)
169 return 1;
170
171 if (do_config)
172 FcConfigSubstitute (0, pat, FcMatchPattern);
173 if (do_default)
174 FcDefaultSubstitute (pat);
175
176 if (os)
177 {
178 FcPattern *new;
179 new = FcPatternFilter (pat, os);
180 FcPatternDestroy (pat);
181 pat = new;
182 }
183
184 if (format)
185 {
186 FcChar8 *s;
187
188 s = FcPatternFormat (pat, format);
189 if (s)
190 {
191 printf ("%s", s);
Akira TAGOH1b692d82012-06-01 19:06:17 +0900192 FcStrFree (s);
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400193 }
194 }
195 else
196 {
197 FcPatternPrint (pat);
198 }
199
200 FcPatternDestroy (pat);
201
202 if (os)
203 FcObjectSetDestroy (os);
204
205 FcFini ();
206
207 return 0;
208}