]> git.wh0rd.org - fontconfig.git/blame - fc-match/fc-match.c
Handle -h and --help according to GNU Coding Standards (#17104)
[fontconfig.git] / fc-match / fc-match.c
CommitLineData
8bc4bc13 1/*
317b8492 2 * fontconfig/fc-match/fc-match.c
8bc4bc13 3 *
46b51147 4 * Copyright © 2003 Keith Packard
8bc4bc13
KP
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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes 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 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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
8bc4bc13
KP
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
f045376c
PL
34#include <fontconfig/fontconfig.h>
35#include <stdio.h>
36#include <unistd.h>
37#include <stdlib.h>
38#include <string.h>
39
8bc4bc13
KP
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>
0d745819 51static const struct option longopts[] = {
8bc4bc13 52 {"sort", 0, 0, 's'},
c014142a 53 {"all", 0, 0, 'a'},
8bc4bc13
KP
54 {"version", 0, 0, 'V'},
55 {"verbose", 0, 0, 'v'},
1439c8f2 56 {"help", 0, 0, 'h'},
8bc4bc13
KP
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
1439c8f2
BE
66static void
67usage (char *program, int error)
8bc4bc13 68{
1439c8f2 69 FILE *file = error ? stderr : stdout;
8bc4bc13 70#if HAVE_GETOPT_LONG
1439c8f2 71 fprintf (file, "usage: %s [-savVh] [--sort] [--all] [--verbose] [--version] [--help] [pattern]\n",
8bc4bc13
KP
72 program);
73#else
1439c8f2 74 fprintf (file, "usage: %s [-savVh] [pattern]\n",
8bc4bc13
KP
75 program);
76#endif
1439c8f2
BE
77 fprintf (file, "List fonts matching [pattern]\n");
78 fprintf (file, "\n");
8bc4bc13 79#if HAVE_GETOPT_LONG
1439c8f2
BE
80 fprintf (file, " -s, --sort display sorted list of matches\n");
81 fprintf (file, " -a, --all display unpruned sorted list of matches\n");
82 fprintf (file, " -v, --verbose display entire font pattern\n");
83 fprintf (file, " -V, --version display font config version and exit\n");
84 fprintf (file, " -h, --help display this help and exit\n");
8bc4bc13 85#else
1439c8f2
BE
86 fprintf (file, " -s, (sort) display sorted list of matches\n");
87 fprintf (file, " -a (all) display unpruned sorted list of matches\n");
88 fprintf (file, " -v (verbose) display entire font pattern\n");
89 fprintf (file, " -V (version) display font config version and exit\n");
90 fprintf (file, " -h (help) display this help and exit\n");
8bc4bc13 91#endif
1439c8f2 92 exit (error);
8bc4bc13
KP
93}
94
95int
96main (int argc, char **argv)
97{
98 int verbose = 0;
c014142a 99 int sort = 0, all = 0;
8bc4bc13 100 int i;
8bc4bc13
KP
101 FcFontSet *fs;
102 FcPattern *pat;
103 FcResult result;
104#if HAVE_GETOPT_LONG || HAVE_GETOPT
105 int c;
106
107#if HAVE_GETOPT_LONG
1439c8f2 108 while ((c = getopt_long (argc, argv, "asVvh", longopts, NULL)) != -1)
8bc4bc13 109#else
1439c8f2 110 while ((c = getopt (argc, argv, "asVvh")) != -1)
8bc4bc13
KP
111#endif
112 {
113 switch (c) {
c014142a
KP
114 case 'a':
115 all = 1;
116 break;
8bc4bc13
KP
117 case 's':
118 sort = 1;
119 break;
120 case 'V':
121 fprintf (stderr, "fontconfig version %d.%d.%d\n",
122 FC_MAJOR, FC_MINOR, FC_REVISION);
123 exit (0);
124 case 'v':
125 verbose = 1;
126 break;
1439c8f2
BE
127 case 'h':
128 usage (argv[0], 0);
8bc4bc13 129 default:
1439c8f2 130 usage (argv[0], 1);
8bc4bc13
KP
131 }
132 }
133 i = optind;
134#else
135 i = 1;
136#endif
137
138 if (!FcInit ())
139 {
140 fprintf (stderr, "Can't init font config library\n");
141 return 1;
142 }
143 if (argv[i])
144 pat = FcNameParse ((FcChar8 *) argv[i]);
145 else
146 pat = FcPatternCreate ();
147
ae2aafe6
PL
148 if (!pat)
149 return 1;
150
8bc4bc13
KP
151 FcConfigSubstitute (0, pat, FcMatchPattern);
152 FcDefaultSubstitute (pat);
153
0602c605
BE
154 fs = FcFontSetCreate ();
155
c014142a 156 if (sort || all)
0602c605
BE
157 {
158 FcFontSet *font_patterns;
159 int j;
c014142a 160 font_patterns = FcFontSort (0, pat, all ? FcFalse : FcTrue, 0, &result);
0602c605
BE
161
162 for (j = 0; j < font_patterns->nfont; j++)
163 {
164 FcPattern *font_pattern;
165
166 font_pattern = FcFontRenderPrepare (NULL, pat, font_patterns->fonts[j]);
167 if (font_pattern)
168 FcFontSetAdd (fs, font_pattern);
169 }
170
171 FcFontSetSortDestroy (font_patterns);
172 }
8bc4bc13
KP
173 else
174 {
175 FcPattern *match;
8bc4bc13
KP
176 match = FcFontMatch (0, pat, &result);
177 if (match)
178 FcFontSetAdd (fs, match);
179 }
ae2aafe6 180 FcPatternDestroy (pat);
8bc4bc13
KP
181
182 if (fs)
183 {
184 int j;
185
186 for (j = 0; j < fs->nfont; j++)
187 {
188 if (verbose)
189 {
190 FcPatternPrint (fs->fonts[j]);
191 }
192 else
193 {
194 FcChar8 *family;
195 FcChar8 *style;
196 FcChar8 *file;
197
198 if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) != FcResultMatch)
8245771d 199 file = (FcChar8 *) "<unknown filename>";
8bc4bc13
KP
200 else
201 {
8245771d 202 FcChar8 *slash = (FcChar8 *) strrchr ((char *) file, '/');
8bc4bc13
KP
203 if (slash)
204 file = slash+1;
205 }
206 if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch)
8245771d 207 family = (FcChar8 *) "<unknown family>";
8bc4bc13 208 if (FcPatternGetString (fs->fonts[j], FC_STYLE, 0, &style) != FcResultMatch)
bdbc26f3 209 style = (FcChar8 *) "<unknown style>";
8bc4bc13
KP
210
211 printf ("%s: \"%s\" \"%s\"\n", file, family, style);
212 }
213 }
214 FcFontSetDestroy (fs);
215 }
2d3387fd 216 FcFini ();
8bc4bc13
KP
217 return 0;
218}