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 <stdlib.h>
37 #include <string.h>
38 #include <locale.h>
39 
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 
44 #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 
52 #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>
63 static 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
73 extern char *optarg;
74 extern int optind, opterr, optopt;
75 #endif
76 #endif
77 
78 static void
usage(char * program,int error)79 usage (char *program, int error)
80 {
81     FILE *file = error ? stderr : stdout;
82 #if HAVE_GETOPT_LONG
83     fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [--config] [--default] [--verbose] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"),
84 	     program);
85 #else
86     fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [pattern] {element...}\n"),
87 	     program);
88 #endif
89     fprintf (file, _("List best font matching [pattern]\n"));
90     fprintf (file, "\n");
91 #if HAVE_GETOPT_LONG
92     fprintf (file, _("  -c, --config         perform config substitution on pattern\n"));
93     fprintf (file, _("  -d, --default        perform default substitution on pattern\n"));
94     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"));
97 #else
98     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"));
103 #endif
104     exit (error);
105 }
106 
107 int
main(int argc,char ** argv)108 main (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 
118     setlocale (LC_ALL, "");
119 #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 
150     if (argv[i])
151     {
152 	pat = FcNameParse ((FcChar8 *) argv[i]);
153 	if (!pat)
154 	{
155 	    fprintf (stderr, _("Unable to parse the pattern\n"));
156 	    return 1;
157 	}
158 	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);
192 	    FcStrFree (s);
193 	}
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 }
209