blob: f63761c914a0ddbd6461a903e370c7e0ba016fdf [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>
39
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090040#ifdef ENABLE_NLS
41#include <libintl.h>
42#define _(x) (dgettext(GETTEXT_PACKAGE, x))
43#else
44#define dgettext(d, s) (s)
45#define _(x) (x)
46#endif
47
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040048#ifndef HAVE_GETOPT
49#define HAVE_GETOPT 0
50#endif
51#ifndef HAVE_GETOPT_LONG
52#define HAVE_GETOPT_LONG 0
53#endif
54
55#if HAVE_GETOPT_LONG
56#undef _GNU_SOURCE
57#define _GNU_SOURCE
58#include <getopt.h>
59static const struct option longopts[] = {
60 {"config", 0, 0, 'c'},
61 {"default", 0, 0, 'd'},
62 {"format", 1, 0, 'f'},
63 {"version", 0, 0, 'V'},
64 {"help", 0, 0, 'h'},
65 {NULL,0,0,0},
66};
67#else
68#if HAVE_GETOPT
69extern char *optarg;
70extern int optind, opterr, optopt;
71#endif
72#endif
73
74static void
75usage (char *program, int error)
76{
77 FILE *file = error ? stderr : stdout;
78#if HAVE_GETOPT_LONG
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090079 fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [--config] [--default] [--verbose] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"),
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040080 program);
81#else
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090082 fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [pattern] {element...}\n"),
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040083 program);
84#endif
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090085 fprintf (file, _("List best font matching [pattern]\n"));
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040086 fprintf (file, "\n");
87#if HAVE_GETOPT_LONG
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090088 fprintf (file, _(" -c, --config perform config substitution on pattern\n"));
Behdad Esfahbod288d3342017-12-18 23:51:17 -050089 fprintf (file, _(" -d, --default perform default substitution on pattern\n"));
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090090 fprintf (file, _(" -f, --format=FORMAT use the given output format\n"));
91 fprintf (file, _(" -V, --version display font config version and exit\n"));
92 fprintf (file, _(" -h, --help display this help and exit\n"));
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040093#else
Akira TAGOH9a0fcb92014-03-27 15:10:44 +090094 fprintf (file, _(" -c, (config) perform config substitution on pattern\n"));
95 fprintf (file, _(" -d, (default) perform default substitution on pattern\n"));
96 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
97 fprintf (file, _(" -V (version) display font config version and exit\n"));
98 fprintf (file, _(" -h (help) display this help and exit\n"));
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -040099#endif
100 exit (error);
101}
102
103int
104main (int argc, char **argv)
105{
106 int do_config = 0, do_default = 0;
107 FcChar8 *format = NULL;
108 int i;
109 FcObjectSet *os = 0;
110 FcPattern *pat;
111#if HAVE_GETOPT_LONG || HAVE_GETOPT
112 int c;
113
114#if HAVE_GETOPT_LONG
115 while ((c = getopt_long (argc, argv, "cdf:Vh", longopts, NULL)) != -1)
116#else
117 while ((c = getopt (argc, argv, "cdf:Vh")) != -1)
118#endif
119 {
120 switch (c) {
121 case 'c':
122 do_config = 1;
123 break;
124 case 'd':
125 do_default = 1;
126 break;
127 case 'f':
128 format = (FcChar8 *) strdup (optarg);
129 break;
130 case 'V':
131 fprintf (stderr, "fontconfig version %d.%d.%d\n",
132 FC_MAJOR, FC_MINOR, FC_REVISION);
133 exit (0);
134 case 'h':
135 usage (argv[0], 0);
136 default:
137 usage (argv[0], 1);
138 }
139 }
140 i = optind;
141#else
142 i = 1;
143#endif
144
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400145 if (argv[i])
146 {
147 pat = FcNameParse ((FcChar8 *) argv[i]);
Akira TAGOH604c2a62013-10-03 19:59:30 +0900148 if (!pat)
149 {
Akira TAGOH9a0fcb92014-03-27 15:10:44 +0900150 fprintf (stderr, _("Unable to parse the pattern\n"));
Akira TAGOH604c2a62013-10-03 19:59:30 +0900151 return 1;
152 }
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400153 while (argv[++i])
154 {
155 if (!os)
156 os = FcObjectSetCreate ();
157 FcObjectSetAdd (os, argv[i]);
158 }
159 }
160 else
161 pat = FcPatternCreate ();
162
163 if (!pat)
164 return 1;
165
166 if (do_config)
167 FcConfigSubstitute (0, pat, FcMatchPattern);
168 if (do_default)
169 FcDefaultSubstitute (pat);
170
171 if (os)
172 {
173 FcPattern *new;
174 new = FcPatternFilter (pat, os);
175 FcPatternDestroy (pat);
176 pat = new;
177 }
178
179 if (format)
180 {
181 FcChar8 *s;
182
183 s = FcPatternFormat (pat, format);
184 if (s)
185 {
186 printf ("%s", s);
Akira TAGOH1b692d82012-06-01 19:06:17 +0900187 FcStrFree (s);
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400188 }
189 }
190 else
191 {
192 FcPatternPrint (pat);
193 }
194
195 FcPatternDestroy (pat);
196
197 if (os)
198 FcObjectSetDestroy (os);
199
200 FcFini ();
201
202 return 0;
203}