From a38e60f2231f5de38e2488c36a2070276448e9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 26 Apr 2005 20:29:30 +0000 Subject: [PATCH] Added "-f" option to only show real files. Some cleanups and minor fixes. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@12476 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/bin/query.cpp | 82 +++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 35 deletions(-) diff --git a/src/bin/query.cpp b/src/bin/query.cpp index aec07831b2..1edc6ef96a 100644 --- a/src/bin/query.cpp +++ b/src/bin/query.cpp @@ -1,45 +1,51 @@ -// query.cpp -// -// A shell utility for somewhat emulating the Tracker's "Find By Formula" -// functionality. -// -// by Ficus Kirkpatrick (ficus@ior.com) -// -// Modified by Jerome Duval on November 03, 2003 -// +/* + * Copyright 2005, Haiku Inc. All Rights Reserved. + * Distributed under the terms of the MIT license. + * + * Authors: + * Ficus Kirkpatrick (ficus@ior.com) + * Jérôme Duval + * Axel Dörfler, axeld@pinc-software.de + */ +/* A shell utility for somewhat emulating the Tracker's "Find By Formula" + * functionality. + */ + + +#include +#include +#include +#include +#include +#include #include #include #include #include -#include -#include -#include -#include -#include -#include -#include - - -// Option variables. -bool o_all_volumes = false; // Query all volumes? -bool o_escaping = true; // Escape metacharacters? extern const char *__progname; +static const char *kProgramName = __progname; + +// Option variables. +static bool sAllVolumes = false; // Query all volumes? +static bool sEscapeMetaChars = true; // Escape metacharacters? +static bool sFilesOnly = false; // Show only files? void usage(void) { - printf("usage: %s [ -e ] [ -a || -v ] expression\n" + printf("usage: %s [ -ef ] [ -a || -v ] expression\n" " -e\t\tdon't escape meta-characters\n" + " -f\t\tshow only files (ie. no directories or symbolic links)\n" " -a\t\tperform the query on all volumes\n" " -v \tperform the query on just one volume; can be any\n" "\t\tfile on that volume. Defaults to the current volume.\n" " Hint: '%s name=foo' will find files named \"foo\"\n", - __progname, __progname); + kProgramName, kProgramName); exit(0); } @@ -63,19 +69,22 @@ perform_query(BVolume &volume, const char *predicate) status = query.Fetch(); } if (status != B_OK) { - printf("query: bad query expression\n"); + fprintf(stderr, "%s: bad query expression\n", kProgramName); return; } BEntry entry; BPath path; while (query.GetNextEntry(&entry) == B_OK) { + if (sFilesOnly && !entry.IsFile()) + continue; + if (entry.GetPath(&path) < B_OK) { - fprintf(stderr, "%s: could not get path for entry\n", __progname); + fprintf(stderr, "%s: could not get path for entry\n", kProgramName); continue; } - printf("%s\n", o_escaping ? + printf("%s\n", sEscapeMetaChars ? BString().CharacterEscape(path.Path(), " ()?*&\"'[]^\\~|;!<>*$", '\\').String() : path.Path()); } @@ -96,13 +105,16 @@ main(int32 argc, char **argv) // Parse command-line arguments. int opt; - while ((opt = getopt(argc, argv, "eav:")) != -1) { + while ((opt = getopt(argc, argv, "efav:")) != -1) { switch(opt) { - case 'a': - o_all_volumes = true; - break; case 'e': - o_escaping = false; + sEscapeMetaChars = false; + break; + case 'f': + sFilesOnly = true; + break; + case 'a': + sAllVolumes = true; break; case 'v': strncpy(volumePath, optarg, B_FILE_NAME_LENGTH); @@ -116,23 +128,23 @@ main(int32 argc, char **argv) BVolume volume; - if (!o_all_volumes) { + if (!sAllVolumes) { // Find the volume that the query should be performed on, // and set the query to it. BEntry entry(volumePath); if (entry.InitCheck() != B_OK) { - fprintf(stderr, "%s: \"%s\" is not a valid file\n", __progname, volumePath); + fprintf(stderr, "%s: \"%s\" is not a valid file\n", kProgramName, volumePath); exit(1); } status_t status = entry.GetVolume(&volume); if (status != B_OK) { - fprintf(stderr, "%s: could not get volume: %s\n", __progname, strerror(status)); + fprintf(stderr, "%s: could not get volume: %s\n", kProgramName, strerror(status)); exit(1); } if (!volume.KnowsQuery()) - fprintf(stderr, "%s: volume containing %s is not query-enabled\n", __progname, volumePath); + fprintf(stderr, "%s: volume containing %s is not query-enabled\n", kProgramName, volumePath); else perform_query(volume, argv[optind]); } else {