Implement fc-list --quiet ala grep (bug #17141)

Exits 1 if no fonts matched, 0 otherwise.
This commit is contained in:
Behdad Esfahbod 2008-12-28 04:58:14 -05:00
parent 00c0972aca
commit f26062b277
1 changed files with 20 additions and 7 deletions

View File

@ -49,6 +49,7 @@
const struct option longopts[] = { const struct option longopts[] = {
{"version", 0, 0, 'V'}, {"version", 0, 0, 'V'},
{"verbose", 0, 0, 'v'}, {"verbose", 0, 0, 'v'},
{"quiet", 0, 0, 'q'},
{"help", 0, 0, 'h'}, {"help", 0, 0, 'h'},
{NULL,0,0,0}, {NULL,0,0,0},
}; };
@ -64,20 +65,22 @@ usage (char *program, int error)
{ {
FILE *file = error ? stderr : stdout; FILE *file = error ? stderr : stdout;
#if HAVE_GETOPT_LONG #if HAVE_GETOPT_LONG
fprintf (file, "usage: %s [-vVh] [--verbose] [--version] [--help] [pattern] {element ...} \n", fprintf (file, "usage: %s [-vqVh] [--verbose] [--quiet] [--version] [--help] [pattern] {element ...} \n",
program); program);
#else #else
fprintf (file, "usage: %s [-vVh] [pattern] {element ...} \n", fprintf (file, "usage: %s [-vqVh] [pattern] {element ...} \n",
program); program);
#endif #endif
fprintf (file, "List fonts matching [pattern]\n"); fprintf (file, "List fonts matching [pattern]\n");
fprintf (file, "\n"); fprintf (file, "\n");
#if HAVE_GETOPT_LONG #if HAVE_GETOPT_LONG
fprintf (file, " -v, --verbose display entire font pattern\n"); fprintf (file, " -v, --verbose display entire font pattern\n");
fprintf (file, " -q, --quiet suppress all normal output, exit 1 if no fonts matched\n");
fprintf (file, " -V, --version display font config version and exit\n"); fprintf (file, " -V, --version display font config version and exit\n");
fprintf (file, " -h, --help display this help and exit\n"); fprintf (file, " -h, --help display this help and exit\n");
#else #else
fprintf (file, " -v (verbose) display entire font pattern\n"); fprintf (file, " -v (verbose) display entire font pattern\n");
fprintf (file, " -q, (quiet) suppress all normal output, exit 1 if no fonts matched\n");
fprintf (file, " -V (version) display font config version and exit\n"); fprintf (file, " -V (version) display font config version and exit\n");
fprintf (file, " -h (help) display this help and exit\n"); fprintf (file, " -h (help) display this help and exit\n");
#endif #endif
@ -88,6 +91,8 @@ int
main (int argc, char **argv) main (int argc, char **argv)
{ {
int verbose = 0; int verbose = 0;
int quiet = 0;
int nfont = 0;
int i; int i;
FcObjectSet *os = 0; FcObjectSet *os = 0;
FcFontSet *fs; FcFontSet *fs;
@ -96,9 +101,9 @@ main (int argc, char **argv)
int c; int c;
#if HAVE_GETOPT_LONG #if HAVE_GETOPT_LONG
while ((c = getopt_long (argc, argv, "Vvh", longopts, NULL)) != -1) while ((c = getopt_long (argc, argv, "Vqvh", longopts, NULL)) != -1)
#else #else
while ((c = getopt (argc, argv, "Vvh")) != -1) while ((c = getopt (argc, argv, "Vqvh")) != -1)
#endif #endif
{ {
switch (c) { switch (c) {
@ -109,6 +114,9 @@ main (int argc, char **argv)
case 'v': case 'v':
verbose = 1; verbose = 1;
break; break;
case 'q':
quiet = 1;
break;
case 'h': case 'h':
usage (argv[0], 0); usage (argv[0], 0);
default: default:
@ -138,7 +146,8 @@ main (int argc, char **argv)
} }
else else
pat = FcPatternCreate (); pat = FcPatternCreate ();
if (quiet && !os)
os = FcObjectSetCreate ();
if (!verbose && !os) if (!verbose && !os)
os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *) 0); os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *) 0);
fs = FcFontList (0, pat, os); fs = FcFontList (0, pat, os);
@ -147,7 +156,7 @@ main (int argc, char **argv)
if (pat) if (pat)
FcPatternDestroy (pat); FcPatternDestroy (pat);
if (fs) if (!quiet && fs)
{ {
int j; int j;
@ -167,10 +176,14 @@ main (int argc, char **argv)
free (font); free (font);
} }
} }
}
if (fs) {
nfont = fs->nfont;
FcFontSetDestroy (fs); FcFontSetDestroy (fs);
} }
FcFini (); FcFini ();
return 0; return quiet ? (nfont == 0 ? 1 : 0) : 0;
} }