Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 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 |
Behdad Esfahbod | 5aaf466 | 2010-11-10 16:45:42 -0500 | [diff] [blame] | 10 | * documentation, and that the name of the author(s) not be used in |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 11 | * advertising or publicity pertaining to distribution of the software without |
Behdad Esfahbod | 5aaf466 | 2010-11-10 16:45:42 -0500 | [diff] [blame] | 12 | * specific, written prior permission. The authors make no |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 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> |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
Akira TAGOH | 2938e4d | 2018-03-15 12:54:02 +0900 | [diff] [blame] | 38 | #include <locale.h> |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 39 | |
Tim-Philipp Müller | a30e4db | 2020-07-07 03:48:34 +0000 | [diff] [blame] | 40 | #ifdef HAVE_UNISTD_H |
| 41 | #include <unistd.h> |
| 42 | #endif |
| 43 | |
Akira TAGOH | 9a0fcb9 | 2014-03-27 15:10:44 +0900 | [diff] [blame] | 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 | |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 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 |
| 79 | usage (char *program, int error) |
| 80 | { |
| 81 | FILE *file = error ? stderr : stdout; |
| 82 | #if HAVE_GETOPT_LONG |
Akira TAGOH | 9a0fcb9 | 2014-03-27 15:10:44 +0900 | [diff] [blame] | 83 | fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [--config] [--default] [--verbose] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"), |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 84 | program); |
| 85 | #else |
Akira TAGOH | 9a0fcb9 | 2014-03-27 15:10:44 +0900 | [diff] [blame] | 86 | fprintf (file, _("usage: %s [-cdVh] [-f FORMAT] [pattern] {element...}\n"), |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 87 | program); |
| 88 | #endif |
Akira TAGOH | 9a0fcb9 | 2014-03-27 15:10:44 +0900 | [diff] [blame] | 89 | fprintf (file, _("List best font matching [pattern]\n")); |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 90 | fprintf (file, "\n"); |
| 91 | #if HAVE_GETOPT_LONG |
Akira TAGOH | 9a0fcb9 | 2014-03-27 15:10:44 +0900 | [diff] [blame] | 92 | fprintf (file, _(" -c, --config perform config substitution on pattern\n")); |
Behdad Esfahbod | 288d334 | 2017-12-18 23:51:17 -0500 | [diff] [blame] | 93 | fprintf (file, _(" -d, --default perform default substitution on pattern\n")); |
Akira TAGOH | 9a0fcb9 | 2014-03-27 15:10:44 +0900 | [diff] [blame] | 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")); |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 97 | #else |
Akira TAGOH | 9a0fcb9 | 2014-03-27 15:10:44 +0900 | [diff] [blame] | 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")); |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 103 | #endif |
| 104 | exit (error); |
| 105 | } |
| 106 | |
| 107 | int |
| 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 | |
Akira TAGOH | 2938e4d | 2018-03-15 12:54:02 +0900 | [diff] [blame] | 118 | setlocale (LC_ALL, ""); |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 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 | |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 150 | if (argv[i]) |
| 151 | { |
| 152 | pat = FcNameParse ((FcChar8 *) argv[i]); |
Akira TAGOH | 604c2a6 | 2013-10-03 19:59:30 +0900 | [diff] [blame] | 153 | if (!pat) |
| 154 | { |
Akira TAGOH | 9a0fcb9 | 2014-03-27 15:10:44 +0900 | [diff] [blame] | 155 | fprintf (stderr, _("Unable to parse the pattern\n")); |
Akira TAGOH | 604c2a6 | 2013-10-03 19:59:30 +0900 | [diff] [blame] | 156 | return 1; |
| 157 | } |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 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); |
Akira TAGOH | 1b692d8 | 2012-06-01 19:06:17 +0900 | [diff] [blame] | 192 | FcStrFree (s); |
Behdad Esfahbod | ba7b50a | 2010-04-20 23:18:00 -0400 | [diff] [blame] | 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 | } |