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 <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>
51 static 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
61 extern char *optarg;
62 extern int optind, opterr, optopt;
63 #endif
64 #endif
65 
66 static void
usage(char * program,int error)67 usage (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 
95 int
main(int argc,char ** argv)96 main (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 
137     if (argv[i])
138     {
139 	pat = FcNameParse ((FcChar8 *) argv[i]);
140 	if (!pat)
141 	{
142 	    fputs ("Unable to parse the pattern\n", stderr);
143 	    return 1;
144 	}
145 	while (argv[++i])
146 	{
147 	    if (!os)
148 		os = FcObjectSetCreate ();
149 	    FcObjectSetAdd (os, argv[i]);
150 	}
151     }
152     else
153 	pat = FcPatternCreate ();
154 
155     if (!pat)
156 	return 1;
157 
158     if (do_config)
159       FcConfigSubstitute (0, pat, FcMatchPattern);
160     if (do_default)
161       FcDefaultSubstitute (pat);
162 
163     if (os)
164     {
165       FcPattern *new;
166       new = FcPatternFilter (pat, os);
167       FcPatternDestroy (pat);
168       pat = new;
169     }
170 
171     if (format)
172     {
173 	FcChar8 *s;
174 
175 	s = FcPatternFormat (pat, format);
176 	if (s)
177 	{
178 	    printf ("%s", s);
179 	    FcStrFree (s);
180 	}
181     }
182     else
183     {
184 	FcPatternPrint (pat);
185     }
186 
187     FcPatternDestroy (pat);
188 
189     if (os)
190 	FcObjectSetDestroy (os);
191 
192     FcFini ();
193 
194     return 0;
195 }
196