blob: 3f066c1e34303c6a6a9d73e8eb06154209bef3d5 [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
40#ifndef HAVE_GETOPT
41#define HAVE_GETOPT 0
42#endif
43#ifndef HAVE_GETOPT_LONG
44#define HAVE_GETOPT_LONG 0
45#endif
46
47#if HAVE_GETOPT_LONG
48#undef _GNU_SOURCE
49#define _GNU_SOURCE
50#include <getopt.h>
51static const struct option longopts[] = {
52 {"config", 0, 0, 'c'},
53 {"default", 0, 0, 'd'},
54 {"format", 1, 0, 'f'},
55 {"version", 0, 0, 'V'},
56 {"help", 0, 0, 'h'},
57 {NULL,0,0,0},
58};
59#else
60#if HAVE_GETOPT
61extern char *optarg;
62extern int optind, opterr, optopt;
63#endif
64#endif
65
66static void
67usage (char *program, int error)
68{
69 FILE *file = error ? stderr : stdout;
70#if HAVE_GETOPT_LONG
71 fprintf (file, "usage: %s [-cdVh] [-f FORMAT] [--config] [--default] [--verbose] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n",
72 program);
73#else
74 fprintf (file, "usage: %s [-cdVh] [-f FORMAT] [pattern] {element...}\n",
75 program);
76#endif
77 fprintf (file, "List best font matching [pattern]\n");
78 fprintf (file, "\n");
79#if HAVE_GETOPT_LONG
80 fprintf (file, " -c, --config perform config substitution on pattern\n");
81 fprintf (file, " -d, -default perform default substitution on pattern\n");
82 fprintf (file, " -f, --format=FORMAT use the given output format\n");
83 fprintf (file, " -V, --version display font config version and exit\n");
84 fprintf (file, " -h, --help display this help and exit\n");
85#else
86 fprintf (file, " -c, (config) perform config substitution on pattern\n");
87 fprintf (file, " -d, (default) perform default substitution on pattern\n");
88 fprintf (file, " -f FORMAT (format) use the given output format\n");
89 fprintf (file, " -V (version) display font config version and exit\n");
90 fprintf (file, " -h (help) display this help and exit\n");
91#endif
92 exit (error);
93}
94
95int
96main (int argc, char **argv)
97{
98 int do_config = 0, do_default = 0;
99 FcChar8 *format = NULL;
100 int i;
101 FcObjectSet *os = 0;
102 FcPattern *pat;
103#if HAVE_GETOPT_LONG || HAVE_GETOPT
104 int c;
105
106#if HAVE_GETOPT_LONG
107 while ((c = getopt_long (argc, argv, "cdf:Vh", longopts, NULL)) != -1)
108#else
109 while ((c = getopt (argc, argv, "cdf:Vh")) != -1)
110#endif
111 {
112 switch (c) {
113 case 'c':
114 do_config = 1;
115 break;
116 case 'd':
117 do_default = 1;
118 break;
119 case 'f':
120 format = (FcChar8 *) strdup (optarg);
121 break;
122 case 'V':
123 fprintf (stderr, "fontconfig version %d.%d.%d\n",
124 FC_MAJOR, FC_MINOR, FC_REVISION);
125 exit (0);
126 case 'h':
127 usage (argv[0], 0);
128 default:
129 usage (argv[0], 1);
130 }
131 }
132 i = optind;
133#else
134 i = 1;
135#endif
136
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400137 if (argv[i])
138 {
139 pat = FcNameParse ((FcChar8 *) argv[i]);
140 while (argv[++i])
141 {
142 if (!os)
143 os = FcObjectSetCreate ();
144 FcObjectSetAdd (os, argv[i]);
145 }
146 }
147 else
148 pat = FcPatternCreate ();
149
150 if (!pat)
151 return 1;
152
153 if (do_config)
154 FcConfigSubstitute (0, pat, FcMatchPattern);
155 if (do_default)
156 FcDefaultSubstitute (pat);
157
158 if (os)
159 {
160 FcPattern *new;
161 new = FcPatternFilter (pat, os);
162 FcPatternDestroy (pat);
163 pat = new;
164 }
165
166 if (format)
167 {
168 FcChar8 *s;
169
170 s = FcPatternFormat (pat, format);
171 if (s)
172 {
173 printf ("%s", s);
Akira TAGOH1b692d82012-06-01 19:06:17 +0900174 FcStrFree (s);
Behdad Esfahbodba7b50a2010-04-20 23:18:00 -0400175 }
176 }
177 else
178 {
179 FcPatternPrint (pat);
180 }
181
182 FcPatternDestroy (pat);
183
184 if (os)
185 FcObjectSetDestroy (os);
186
187 FcFini ();
188
189 return 0;
190}