| 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 |
|
| 36 |
#include <locale.h> |
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
|
| 41 |
#ifdef HAVE_UNISTD_H |
| 42 |
# include <unistd.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
#ifdef ENABLE_NLS |
| 46 |
# include <libintl.h> |
| 47 |
# define _(x) (dgettext (GETTEXT_PACKAGE, x)) |
| 48 |
#else |
| 49 |
# define dgettext(d, s) (s) |
| 50 |
# define _(x) (x) |
| 51 |
#endif |
| 52 |
|
| 53 |
#ifndef HAVE_GETOPT |
| 54 |
# define HAVE_GETOPT 0 |
| 55 |
#endif |
| 56 |
#ifndef HAVE_GETOPT_LONG |
| 57 |
# define HAVE_GETOPT_LONG 0 |
| 58 |
#endif |
| 59 |
|
| 60 |
#if HAVE_GETOPT_LONG |
| 61 |
# undef _GNU_SOURCE |
| 62 |
# define _GNU_SOURCE |
| 63 |
# include <getopt.h> |
| 64 |
static const struct option longopts[] = { |
| 65 |
{ "config", 0, 0, 'c' }, |
| 66 |
{ "default", 0, 0, 'd' }, |
| 67 |
{ "format", 1, 0, 'f' }, |
| 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 [-cdVh] [-f FORMAT] [--config] [--default] [--verbose] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"), |
| 85 |
program); |
| 86 |
#else |
| 87 |
fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [pattern] {element...}\n"), |
| 88 |
program); |
| 89 |
#endif |
| 90 |
fprintf (file, _("List best font matching [pattern]\n")); |
| 91 |
fprintf (file, "\n"); |
| 92 |
#if HAVE_GETOPT_LONG |
| 93 |
fprintf (file, _(" -c, --config perform config substitution on pattern\n")); |
| 94 |
fprintf (file, _(" -d, --default perform default substitution on pattern\n")); |
| 95 |
fprintf (file, _(" -f, --format=FORMAT use the given output format\n")); |
| 96 |
fprintf (file, _(" -V, --version display font config version and exit\n")); |
| 97 |
fprintf (file, _(" -h, --help display this help and exit\n")); |
| 98 |
#else |
| 99 |
fprintf (file, _(" -c, (config) perform config substitution on pattern\n")); |
| 100 |
fprintf (file, _(" -d, (default) perform default substitution on pattern\n")); |
| 101 |
fprintf (file, _(" -f FORMAT (format) use the given output format\n")); |
| 102 |
fprintf (file, _(" -V (version) display font config version and exit\n")); |
| 103 |
fprintf (file, _(" -h (help) display this help and exit\n")); |
| 104 |
#endif |
| 105 |
exit (error); |
| 106 |
} |
| 107 |
|
| 108 |
int |
| 109 |
main (int argc, char **argv) |
| 110 |
{ |
| 111 |
int do_config = 0, do_default = 0; |
| 112 |
FcChar8 *format = NULL; |
| 113 |
int i; |
| 114 |
FcObjectSet *os = 0; |
| 115 |
FcPattern *pat; |
| 116 |
#if HAVE_GETOPT_LONG || HAVE_GETOPT |
| 117 |
int c; |
| 118 |
|
| 119 |
setlocale (LC_ALL, ""); |
| 120 |
# if HAVE_GETOPT_LONG |
| 121 |
while ((c = getopt_long (argc, argv, "cdf:Vh", longopts, NULL)) != -1) |
| 122 |
# else |
| 123 |
while ((c = getopt (argc, argv, "cdf:Vh")) != -1) |
| 124 |
# endif |
| 125 |
{ |
| 126 |
switch (c) { |
| 127 |
case 'c': |
| 128 |
do_config = 1; |
| 129 |
break; |
| 130 |
case 'd': |
| 131 |
do_default = 1; |
| 132 |
break; |
| 133 |
case 'f': |
| 134 |
format = (FcChar8 *)strdup (optarg); |
| 135 |
break; |
| 136 |
case 'V': |
| 137 |
fprintf (stderr, "fontconfig version %d.%d.%d\n", |
| 138 |
FC_MAJOR, FC_MINOR, FC_REVISION); |
| 139 |
exit (0); |
| 140 |
case 'h': |
| 141 |
usage (argv[0], 0); |
| 142 |
default: |
| 143 |
usage (argv[0], 1); |
| 144 |
} |
| 145 |
} |
| 146 |
i = optind; |
| 147 |
#else |
| 148 |
i = 1; |
| 149 |
#endif |
| 150 |
|
| 151 |
if (argv[i]) { |
| 152 |
pat = FcNameParse ((FcChar8 *)argv[i]); |
| 153 |
if (!pat) { |
| 154 |
fprintf (stderr, _("Unable to parse the pattern\n")); |
| 155 |
return 1; |
| 156 |
} |
| 157 |
while (argv[++i]) { |
| 158 |
if (!os) |
| 159 |
os = FcObjectSetCreate(); |
| 160 |
FcObjectSetAdd (os, argv[i]); |
| 161 |
} |
| 162 |
} else |
| 163 |
pat = FcPatternCreate(); |
| 164 |
|
| 165 |
if (!pat) |
| 166 |
return 1; |
| 167 |
|
| 168 |
if (do_config) |
| 169 |
FcConfigSubstitute (0, pat, FcMatchPattern); |
| 170 |
if (do_default) |
| 171 |
FcConfigSetDefaultSubstitute (0, pat); |
| 172 |
|
| 173 |
if (os) { |
| 174 |
FcPattern *new; |
| 175 |
new = FcPatternFilter (pat, os); |
| 176 |
FcPatternDestroy (pat); |
| 177 |
pat = new; |
| 178 |
} |
| 179 |
|
| 180 |
if (format) { |
| 181 |
FcChar8 *s; |
| 182 |
|
| 183 |
s = FcPatternFormat (pat, format); |
| 184 |
if (s) { |
| 185 |
printf ("%s", s); |
| 186 |
FcStrFree (s); |
| 187 |
} |
| 188 |
} else { |
| 189 |
FcPatternPrint (pat); |
| 190 |
} |
| 191 |
|
| 192 |
FcPatternDestroy (pat); |
| 193 |
|
| 194 |
if (os) |
| 195 |
FcObjectSetDestroy (os); |
| 196 |
|
| 197 |
FcFini(); |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |