| 1 |
/* |
| 2 |
* fontconfig/fc-list/fc-list.c |
| 3 |
* |
| 4 |
* Copyright © 2002 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 |
#include <fontconfig/fontconfig.h> |
| 34 |
|
| 35 |
#include <locale.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.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 |
const struct option longopts[] = { |
| 64 |
{ "verbose", 0, 0, 'v' }, |
| 65 |
{ "brief", 0, 0, 'b' }, |
| 66 |
{ "format", 1, 0, 'f' }, |
| 67 |
{ "quiet", 0, 0, 'q' }, |
| 68 |
{ "version", 0, 0, 'V' }, |
| 69 |
{ "help", 0, 0, 'h' }, |
| 70 |
{ NULL, 0, 0, 0 }, |
| 71 |
}; |
| 72 |
#else |
| 73 |
# if HAVE_GETOPT |
| 74 |
extern char *optarg; |
| 75 |
extern int optind, opterr, optopt; |
| 76 |
# endif |
| 77 |
#endif |
| 78 |
|
| 79 |
static void |
| 80 |
usage (char *program, int error) |
| 81 |
{ |
| 82 |
FILE *file = error ? stderr : stdout; |
| 83 |
#if HAVE_GETOPT_LONG |
| 84 |
fprintf (file, _("usage: %s [-vbqVh] [-f FORMAT] [--verbose] [--brief] [--format=FORMAT] [--quiet] [--version] [--help] [pattern] {element ...} \n"), |
| 85 |
program); |
| 86 |
#else |
| 87 |
fprintf (file, _("usage: %s [-vbqVh] [-f FORMAT] [pattern] {element ...} \n"), |
| 88 |
program); |
| 89 |
#endif |
| 90 |
fprintf (file, _("List fonts matching [pattern]\n")); |
| 91 |
fprintf (file, "\n"); |
| 92 |
#if HAVE_GETOPT_LONG |
| 93 |
fprintf (file, _(" -v, --verbose display entire font pattern verbosely\n")); |
| 94 |
fprintf (file, _(" -b, --brief display entire font pattern briefly\n")); |
| 95 |
fprintf (file, _(" -f, --format=FORMAT use the given output format\n")); |
| 96 |
fprintf (file, _(" -q, --quiet suppress all normal output, exit 1 if no fonts matched\n")); |
| 97 |
fprintf (file, _(" -V, --version display font config version and exit\n")); |
| 98 |
fprintf (file, _(" -h, --help display this help and exit\n")); |
| 99 |
#else |
| 100 |
fprintf (file, _(" -v (verbose) display entire font pattern verbosely\n")); |
| 101 |
fprintf (file, _(" -b (brief) display entire font pattern briefly\n")); |
| 102 |
fprintf (file, _(" -f FORMAT (format) use the given output format\n")); |
| 103 |
fprintf (file, _(" -q, (quiet) suppress all normal output, exit 1 if no fonts matched\n")); |
| 104 |
fprintf (file, _(" -V (version) display font config version and exit\n")); |
| 105 |
fprintf (file, _(" -h (help) display this help and exit\n")); |
| 106 |
#endif |
| 107 |
exit (error); |
| 108 |
} |
| 109 |
|
| 110 |
int |
| 111 |
main (int argc, char **argv) |
| 112 |
{ |
| 113 |
int verbose = 0; |
| 114 |
int brief = 0; |
| 115 |
int quiet = 0; |
| 116 |
const FcChar8 *format = NULL; |
| 117 |
const FcChar8 *format_optarg = NULL; |
| 118 |
int nfont = 0; |
| 119 |
int i; |
| 120 |
FcObjectSet *os = 0; |
| 121 |
FcFontSet *fs; |
| 122 |
FcPattern *pat; |
| 123 |
#if HAVE_GETOPT_LONG || HAVE_GETOPT |
| 124 |
int c; |
| 125 |
|
| 126 |
setlocale (LC_ALL, ""); |
| 127 |
# if HAVE_GETOPT_LONG |
| 128 |
while ((c = getopt_long (argc, argv, "vbf:qVh", longopts, NULL)) != -1) |
| 129 |
# else |
| 130 |
while ((c = getopt (argc, argv, "vbf:qVh")) != -1) |
| 131 |
# endif |
| 132 |
{ |
| 133 |
switch (c) { |
| 134 |
case 'v': |
| 135 |
verbose = 1; |
| 136 |
break; |
| 137 |
case 'b': |
| 138 |
brief = 1; |
| 139 |
break; |
| 140 |
case 'f': |
| 141 |
format_optarg = format = (FcChar8 *)strdup (optarg); |
| 142 |
break; |
| 143 |
case 'q': |
| 144 |
quiet = 1; |
| 145 |
break; |
| 146 |
case 'V': |
| 147 |
fprintf (stderr, "fontconfig version %d.%d.%d\n", |
| 148 |
FC_MAJOR, FC_MINOR, FC_REVISION); |
| 149 |
exit (0); |
| 150 |
case 'h': |
| 151 |
usage (argv[0], 0); |
| 152 |
default: |
| 153 |
usage (argv[0], 1); |
| 154 |
} |
| 155 |
} |
| 156 |
i = optind; |
| 157 |
#else |
| 158 |
i = 1; |
| 159 |
#endif |
| 160 |
|
| 161 |
if (argv[i]) { |
| 162 |
pat = FcNameParse ((FcChar8 *)argv[i]); |
| 163 |
if (!pat) { |
| 164 |
fprintf (stderr, _("Unable to parse the pattern\n")); |
| 165 |
return 1; |
| 166 |
} |
| 167 |
while (argv[++i]) { |
| 168 |
if (!os) |
| 169 |
os = FcObjectSetCreate(); |
| 170 |
FcObjectSetAdd (os, argv[i]); |
| 171 |
} |
| 172 |
} else |
| 173 |
pat = FcPatternCreate(); |
| 174 |
if (quiet && !os) |
| 175 |
os = FcObjectSetCreate(); |
| 176 |
if (!verbose && !brief && !format && !os) |
| 177 |
os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_FILE, (char *)0); |
| 178 |
if (!format) |
| 179 |
format = (const FcChar8 *)"%{=fclist}\n"; |
| 180 |
fs = FcFontList (0, pat, os); |
| 181 |
if (os) |
| 182 |
FcObjectSetDestroy (os); |
| 183 |
if (pat) |
| 184 |
FcPatternDestroy (pat); |
| 185 |
|
| 186 |
if (!quiet && fs) { |
| 187 |
int j; |
| 188 |
|
| 189 |
for (j = 0; j < fs->nfont; j++) { |
| 190 |
if (verbose || brief) { |
| 191 |
if (brief) { |
| 192 |
FcPatternDel (fs->fonts[j], FC_CHARSET); |
| 193 |
FcPatternDel (fs->fonts[j], FC_LANG); |
| 194 |
} |
| 195 |
FcPatternPrint (fs->fonts[j]); |
| 196 |
} else { |
| 197 |
FcChar8 *s; |
| 198 |
|
| 199 |
s = FcPatternFormat (fs->fonts[j], format); |
| 200 |
if (s) { |
| 201 |
printf ("%s", s); |
| 202 |
FcStrFree (s); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
if (fs) { |
| 209 |
nfont = fs->nfont; |
| 210 |
FcFontSetDestroy (fs); |
| 211 |
} |
| 212 |
if (format_optarg) { |
| 213 |
free ((void *)format_optarg); |
| 214 |
} |
| 215 |
|
| 216 |
FcFini(); |
| 217 |
|
| 218 |
return quiet ? (nfont == 0 ? 1 : 0) : 0; |
| 219 |
} |