1 /*
2  * fontconfig/fc-scan/fc-scan.c
3  *
4  * Copyright © 2003 Keith Packard
5  * Copyright © 2008 Red Hat, Inc.
6  * Red Hat Author(s): Behdad Esfahbod
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that
11  * copyright notice and this permission notice appear in supporting
12  * documentation, and that the name of the author(s) not be used in
13  * advertising or publicity pertaining to distribution of the software without
14  * specific, written prior permission.  The authors make no
15  * representations about the suitability of this software for any purpose.  It
16  * is provided "as is" without express or implied warranty.
17  *
18  * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
24  * PERFORMANCE OF THIS SOFTWARE.
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #else
30 #ifdef linux
31 #define HAVE_GETOPT_LONG 1
32 #endif
33 #define HAVE_GETOPT 1
34 #endif
35 
36 #include <fontconfig/fontconfig.h>
37 #include <fontconfig/fcfreetype.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #ifndef HAVE_GETOPT
44 #define HAVE_GETOPT 0
45 #endif
46 #ifndef HAVE_GETOPT_LONG
47 #define HAVE_GETOPT_LONG 0
48 #endif
49 
50 #if HAVE_GETOPT_LONG
51 #undef  _GNU_SOURCE
52 #define _GNU_SOURCE
53 #include <getopt.h>
54 static const struct option longopts[] = {
55     {"format", 1, 0, 'f'},
56     {"version", 0, 0, 'V'},
57     {"help", 0, 0, 'h'},
58     {NULL,0,0,0},
59 };
60 #else
61 #if HAVE_GETOPT
62 extern char *optarg;
63 extern int optind, opterr, optopt;
64 #endif
65 #endif
66 
67 static void
usage(char * program,int error)68 usage (char *program, int error)
69 {
70     FILE *file = error ? stderr : stdout;
71 #if HAVE_GETOPT_LONG
72     fprintf (file, "usage: %s [-Vh] [-f FORMAT] [--format FORMAT] [--version] [--help] font-file...\n",
73 	     program);
74 #else
75     fprintf (file, "usage: %s [-Vh] [-f FORMAT] font-file...\n",
76 	     program);
77 #endif
78     fprintf (file, "Scan font files and directories, and print resulting pattern(s)\n");
79     fprintf (file, "\n");
80 #if HAVE_GETOPT_LONG
81     fprintf (file, "  -f, --format=FORMAT  use the given output format\n");
82     fprintf (file, "  -V, --version        display font config version and exit\n");
83     fprintf (file, "  -h, --help           display this help and exit\n");
84 #else
85     fprintf (file, "  -f FORMAT  (format)  use the given output format\n");
86     fprintf (file, "  -V         (version) display font config version and exit\n");
87     fprintf (file, "  -h         (help)    display this help and exit\n");
88 #endif
89     exit (error);
90 }
91 
92 int
main(int argc,char ** argv)93 main (int argc, char **argv)
94 {
95     FcChar8     *format = NULL;
96     int		i;
97     FcFontSet   *fs;
98 #if HAVE_GETOPT_LONG || HAVE_GETOPT
99     int		c;
100 
101 #if HAVE_GETOPT_LONG
102     while ((c = getopt_long (argc, argv, "f:Vh", longopts, NULL)) != -1)
103 #else
104     while ((c = getopt (argc, argv, "f:Vh")) != -1)
105 #endif
106     {
107 	switch (c) {
108 	case 'f':
109 	    format = (FcChar8 *) strdup (optarg);
110 	    break;
111 	case 'V':
112 	    fprintf (stderr, "fontconfig version %d.%d.%d\n",
113 		     FC_MAJOR, FC_MINOR, FC_REVISION);
114 	    exit (0);
115 	case 'h':
116 	    usage (argv[0], 0);
117 	default:
118 	    usage (argv[0], 1);
119 	}
120     }
121     i = optind;
122 #else
123     i = 1;
124 #endif
125 
126     if (i == argc)
127 	usage (argv[0], 1);
128 
129     fs = FcFontSetCreate ();
130 
131     for (; i < argc; i++)
132     {
133 	const FcChar8 *file = (FcChar8*) argv[i];
134 
135 	if (!FcFileIsDir (file))
136 	    FcFileScan (fs, NULL, NULL, NULL, file, FcTrue);
137 	else
138 	{
139 	    FcStrSet *dirs = FcStrSetCreate ();
140 	    FcStrList *strlist = FcStrListCreate (dirs);
141 	    do
142 	    {
143 		FcDirScan (fs, dirs, NULL, NULL, file, FcTrue);
144 	    }
145 	    while ((file = FcStrListNext (strlist)));
146 	    FcStrListDone (strlist);
147 	    FcStrSetDestroy (dirs);
148 	}
149     }
150 
151     for (i = 0; i < fs->nfont; i++)
152     {
153 	FcPattern *pat;
154 
155 	pat = fs->fonts[i];
156 
157 	if (format)
158 	{
159 	    FcChar8 *s;
160 
161 	    s = FcPatternFormat (pat, format);
162 	    if (s)
163 	    {
164 		printf ("%s", s);
165 		FcStrFree (s);
166 	    }
167 	}
168 	else
169 	{
170 	    FcPatternPrint (pat);
171 	}
172     }
173 
174     FcFontSetDestroy (fs);
175 
176     FcFini ();
177     return i > 0 ? 0 : 1;
178 }
179