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
This commit is contained in:
parent
7542e025d3
commit
a38e60f223
|
@ -1,45 +1,51 @@
|
||||||
// query.cpp
|
/*
|
||||||
//
|
* Copyright 2005, Haiku Inc. All Rights Reserved.
|
||||||
// A shell utility for somewhat emulating the Tracker's "Find By Formula"
|
* Distributed under the terms of the MIT license.
|
||||||
// functionality.
|
*
|
||||||
//
|
* Authors:
|
||||||
// by Ficus Kirkpatrick (ficus@ior.com)
|
* Ficus Kirkpatrick (ficus@ior.com)
|
||||||
//
|
* Jérôme Duval
|
||||||
// Modified by Jerome Duval on November 03, 2003
|
* Axel Dörfler, axeld@pinc-software.de
|
||||||
//
|
*/
|
||||||
|
|
||||||
|
/* A shell utility for somewhat emulating the Tracker's "Find By Formula"
|
||||||
|
* functionality.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <Path.h>
|
||||||
|
#include <Query.h>
|
||||||
|
#include <Entry.h>
|
||||||
|
#include <Volume.h>
|
||||||
|
#include <VolumeRoster.h>
|
||||||
|
#include <String.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <storage/Path.h>
|
|
||||||
#include <storage/Query.h>
|
|
||||||
#include <storage/Entry.h>
|
|
||||||
#include <storage/Volume.h>
|
|
||||||
#include <storage/VolumeRoster.h>
|
|
||||||
#include <support/SupportDefs.h>
|
|
||||||
#include <support/String.h>
|
|
||||||
|
|
||||||
|
|
||||||
// Option variables.
|
|
||||||
bool o_all_volumes = false; // Query all volumes?
|
|
||||||
bool o_escaping = true; // Escape metacharacters?
|
|
||||||
|
|
||||||
extern const char *__progname;
|
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
|
void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
printf("usage: %s [ -e ] [ -a || -v <path-to-volume> ] expression\n"
|
printf("usage: %s [ -ef ] [ -a || -v <path-to-volume> ] expression\n"
|
||||||
" -e\t\tdon't escape meta-characters\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"
|
" -a\t\tperform the query on all volumes\n"
|
||||||
" -v <file>\tperform the query on just one volume; <file> can be any\n"
|
" -v <file>\tperform the query on just one volume; <file> can be any\n"
|
||||||
"\t\tfile on that volume. Defaults to the current volume.\n"
|
"\t\tfile on that volume. Defaults to the current volume.\n"
|
||||||
" Hint: '%s name=foo' will find files named \"foo\"\n",
|
" Hint: '%s name=foo' will find files named \"foo\"\n",
|
||||||
__progname, __progname);
|
kProgramName, kProgramName);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,19 +69,22 @@ perform_query(BVolume &volume, const char *predicate)
|
||||||
status = query.Fetch();
|
status = query.Fetch();
|
||||||
}
|
}
|
||||||
if (status != B_OK) {
|
if (status != B_OK) {
|
||||||
printf("query: bad query expression\n");
|
fprintf(stderr, "%s: bad query expression\n", kProgramName);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
BEntry entry;
|
BEntry entry;
|
||||||
BPath path;
|
BPath path;
|
||||||
while (query.GetNextEntry(&entry) == B_OK) {
|
while (query.GetNextEntry(&entry) == B_OK) {
|
||||||
|
if (sFilesOnly && !entry.IsFile())
|
||||||
|
continue;
|
||||||
|
|
||||||
if (entry.GetPath(&path) < B_OK) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n", o_escaping ?
|
printf("%s\n", sEscapeMetaChars ?
|
||||||
BString().CharacterEscape(path.Path(), " ()?*&\"'[]^\\~|;!<>*$", '\\').String()
|
BString().CharacterEscape(path.Path(), " ()?*&\"'[]^\\~|;!<>*$", '\\').String()
|
||||||
: path.Path());
|
: path.Path());
|
||||||
}
|
}
|
||||||
|
@ -96,13 +105,16 @@ main(int32 argc, char **argv)
|
||||||
|
|
||||||
// Parse command-line arguments.
|
// Parse command-line arguments.
|
||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt(argc, argv, "eav:")) != -1) {
|
while ((opt = getopt(argc, argv, "efav:")) != -1) {
|
||||||
switch(opt) {
|
switch(opt) {
|
||||||
case 'a':
|
|
||||||
o_all_volumes = true;
|
|
||||||
break;
|
|
||||||
case 'e':
|
case 'e':
|
||||||
o_escaping = false;
|
sEscapeMetaChars = false;
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
sFilesOnly = true;
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
sAllVolumes = true;
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
strncpy(volumePath, optarg, B_FILE_NAME_LENGTH);
|
strncpy(volumePath, optarg, B_FILE_NAME_LENGTH);
|
||||||
|
@ -116,23 +128,23 @@ main(int32 argc, char **argv)
|
||||||
|
|
||||||
BVolume volume;
|
BVolume volume;
|
||||||
|
|
||||||
if (!o_all_volumes) {
|
if (!sAllVolumes) {
|
||||||
// Find the volume that the query should be performed on,
|
// Find the volume that the query should be performed on,
|
||||||
// and set the query to it.
|
// and set the query to it.
|
||||||
BEntry entry(volumePath);
|
BEntry entry(volumePath);
|
||||||
if (entry.InitCheck() != B_OK) {
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
status_t status = entry.GetVolume(&volume);
|
status_t status = entry.GetVolume(&volume);
|
||||||
if (status != B_OK) {
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!volume.KnowsQuery())
|
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
|
else
|
||||||
perform_query(volume, argv[optind]);
|
perform_query(volume, argv[optind]);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue