play modified and query added

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5254 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval 2003-11-04 09:30:46 +00:00
parent f6774ab931
commit c05b770350
2 changed files with 199 additions and 77 deletions

View File

@ -1,6 +1,8 @@
/* ++++++++++
play.cpp
Copyright (C) 1995 Be Incorporated. All Rights Reserved.
modified on November 03, 2003 by Jerome Duval
+++++ */
/*
* was released as sample code; source:
@ -8,30 +10,27 @@
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <scsi.h>
#include <unistd.h>
#include <Directory.h>
#include <Drivers.h>
#include <Entry.h>
#include <Path.h>
#include <scsiprobe_driver.h>
#include <ide_calls.h>
/* ----------
play requested track
----- */
void
play(int id, scsi_play_track *track)
{
track->start_index = 1;
track->end_track = 99;
track->end_index = 1;
if (!ioctl(id, SCSI_PLAY_TRACK, track))
if (!ioctl(id, B_SCSI_PLAY_TRACK, track))
printf("Playing audio...\n");
else
printf("Play audio failed\n");
@ -40,69 +39,60 @@ play(int id, scsi_play_track *track)
/* ----------
show valid cd-rom id's
(from /boot/optional/sample-code/interface_kit/CDButton/CDEngine.cpp)
----- */
void try_dir(const char *directory, long *count, bool show)
{
bool add;
const char *name;
int fd;
BPath path;
BDirectory dir;
BEntry entry;
scsiprobe_inquiry inquiry;
ide_ctrl_info ide_info;
static void
try_dir(const char *directory, long *count, bool show)
{
bool add = false;
BDirectory dir;
dir.SetTo(directory);
if(dir.InitCheck() != B_NO_ERROR) {
return;
}
dir.Rewind();
BEntry entry;
while(dir.GetNextEntry(&entry) >= 0) {
BPath path;
const char *name;
entry_ref e;
if(entry.GetPath(&path) != B_NO_ERROR)
continue;
name = path.Path();
if(entry.GetRef(&e) != B_NO_ERROR)
continue;
dir.SetTo(directory);
if (dir.InitCheck() == B_NO_ERROR) {
dir.Rewind();
while (dir.GetNextEntry(&entry) >= 0) {
entry.GetPath(&path);
name = path.Path();
if (entry.IsDirectory())
try_dir(name, count, show);
else if (strstr(name, "/raw")) {
add = FALSE;
if (strstr(name, "/scsi")) {
if ((fd = open("/dev/scsiprobe", 0)) >= 0) {
inquiry.path = name[15] - '0';
inquiry.id = name[16] - '0';
inquiry.lun = name[17] - '0';
inquiry.len = 36;
if (ioctl(fd, SCSIPROBE_INQUIRY, &inquiry) == B_NO_ERROR)
add = ((inquiry.data[0] & 0x1f) == 5);
close(fd);
}
}
else if (strstr(name, "/ide")) {
if ((fd = open("/dev/disk/ide/rescan", 0)) >= 0) {
if (ioctl(fd, IDE_GET_DEVICES_INFO, &ide_info) == B_NO_ERROR) {
if (name[14] == '0') {
if ((strstr(name, "master")) &&
(ide_info.ide_0_master_type == B_CD))
add = true;
else if ((strstr(name, "slave")) &&
(ide_info.ide_0_slave_type == B_CD))
add = true;
}
else {
if ((strstr(name, "master")) &&
(ide_info.ide_1_master_type == B_CD))
add = true;
else if ((strstr(name, "slave")) &&
(ide_info.ide_1_slave_type == B_CD))
add = true;
}
}
close(fd);
}
}
if (add) {
*count += 1;
if (show)
printf(" %s\n", name);
if(entry.IsDirectory()) {
if(strcmp(e.name, "floppy") == 0)
continue; // ignore floppy (it is not silent)
try_dir(name, count, show);
}
else {
int devfd;
device_geometry g;
if(strcmp(e.name, "raw") != 0)
continue; // ignore partitions
devfd = open(name, O_RDONLY);
if(devfd < 0)
continue;
if(ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g)) >= 0) {
if(g.device_type == B_CD)
{
add = true;
}
}
close(devfd);
}
if (add) {
*count += 1;
if (show)
printf(" %s\n", name);
}
}
}
@ -180,7 +170,7 @@ main (int argc, char **argv)
if ((id = open(argv[1], 0)) >= 0) {
switch (command) {
case 0:
if (!ioctl(id, SCSI_GET_TOC, &toc)) {
if (!ioctl(id, B_SCSI_GET_TOC, &toc)) {
track.start_track = 0;
for (i = toc.toc_data[2]; i <= toc.toc_data[3]; i++) {
length = (toc.toc_data[(((i + 1) - toc.toc_data[2]) * 8) + 4 + 5] * 60) +
@ -221,22 +211,22 @@ main (int argc, char **argv)
case 1:
printf("Pausing audio\n");
ioctl(id, SCSI_PAUSE_AUDIO);
ioctl(id, B_SCSI_PAUSE_AUDIO);
break;
case 2:
printf("Resuming audio\n");
ioctl(id, SCSI_RESUME_AUDIO);
ioctl(id, B_SCSI_RESUME_AUDIO);
break;
case 3:
printf("Stopping audio\n");
ioctl(id, SCSI_STOP_AUDIO);
ioctl(id, B_SCSI_STOP_AUDIO);
break;
case 4:
printf("Ejecting CD\n");
ioctl(id, SCSI_EJECT);
ioctl(id, B_SCSI_EJECT);
break;
case 5:
@ -247,17 +237,17 @@ main (int argc, char **argv)
if ((vol < 0) || (vol > 255))
printf("Volume is out of range (0 - 255)\n");
else {
printf("Setting volume to %d\n", vol);
printf("Setting volume to %ld\n", vol);
volume.port0_volume = vol;
volume.port1_volume = vol;
volume.flags = scsi_port0_volume | scsi_port1_volume;
ioctl(id, SCSI_SET_VOLUME, &volume);
volume.flags = B_SCSI_PORT0_VOLUME | B_SCSI_PORT1_VOLUME;
ioctl(id, B_SCSI_SET_VOLUME, &volume);
}
}
break;
case 6:
if (ioctl(id, SCSI_GET_POSITION, &position) == B_ERROR)
if (ioctl(id, B_SCSI_GET_POSITION, &position) == B_ERROR)
printf("Could not get current position\n");
else {
switch(position.position[1]) {
@ -318,9 +308,9 @@ main (int argc, char **argv)
}
req_track = strtol (argv[3], 0, 0);
if (!ioctl(id, SCSI_GET_TOC, &toc)) {
if (!ioctl(id, B_SCSI_GET_TOC, &toc)) {
if (req_track > toc.toc_data[3]) {
printf("Track %d is out of range [%d-%d]\n", req_track,
printf("Track %ld is out of range [%d-%d]\n", req_track,
toc.toc_data[2], toc.toc_data[3]);
break;
}
@ -339,7 +329,7 @@ main (int argc, char **argv)
frames = min_c(1 * 75, (int) length);
read_cd.buffer = (char *)malloc(frames * 2352);
printf("Saving track %d to %s...\n", req_track, argv[4]);
printf("Saving track %ld to %s...\n", req_track, argv[4]);
while (length) {
index = start;
read_cd.start_m = index / (60 * 75);
@ -360,7 +350,7 @@ main (int argc, char **argv)
read_cd.length_f = index;
for (i = 0; i < 5; i++)
if (ioctl(id, SCSI_READ_CD, &read_cd) == B_NO_ERROR)
if (ioctl(id, B_SCSI_READ_CD, &read_cd) == B_NO_ERROR)
break;
if (i == 5) {
printf("Error reading CD-DA\n");
@ -395,7 +385,7 @@ main (int argc, char **argv)
else
scan.direction = 0;
scan.speed = 0;
if (ioctl(id, SCSI_SCAN, &scan) == B_ERROR)
if (ioctl(id, B_SCSI_SCAN, &scan) == B_ERROR)
printf("Error trying to scan\n");
else
printf("Scanning...\n");

132
src/apps/bin/query.cpp Normal file
View File

@ -0,0 +1,132 @@
// 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
//
#include <stdio.h>
#include <string.h>
#include <stdlib.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>
extern "C" {
int32 getopt(int32, const char **, const char *);
extern char *optarg;
extern int32 optind;
}
// Option variables.
bool o_all_volumes = false; // Query all volumes?
bool o_escaping = true; // Escape metacharacters?
void usage(void)
{
printf("usage: query [ -e ] [ -a || -v volume ] expression\n");
printf(" -e don't escape meta-characters\n");
printf(" -a perform the query on all volumes\n");
printf(" -v <file> perform the query on just one volume;\n");
printf(" <file> can be any file on that volume.\n");
printf(" defaults to the boot volume.\n");
printf(" hint: query 'name=foo' will find a file named \"foo\"\n");
exit(0);
}
void perform_query(BVolume *v, const char *predicate)
{
BQuery query;
// Set up the volume and predicate for the query.
query.SetVolume(v);
query.SetPredicate(predicate);
if (query.Fetch() != B_OK) {
printf("query: bad query expression\n");
return;
}
BEntry e;
BPath p;
while (query.GetNextEntry(&e) == B_OK) {
e.GetPath(&p);
printf("%s\n", o_escaping ?
BString().CharacterEscape(p.Path(), " ()?*&\"'[]^\\~|;!<>*$", '\\').String() :
p.Path());
}
return;
}
int main(int32 argc, const char **argv)
{
// Make sure we have the minimum number of arguments.
if (argc < 2) usage();
// Which volume do we make the query on?
char volume_path[B_FILE_NAME_LENGTH];
// Default to the boot volume.
strcpy(volume_path, "/boot");
// Parse command-line arguments.
int32 opt;
while ((opt = getopt(argc, argv, "ave:")) != -1) {
switch(opt) {
case 'a':
o_all_volumes = true;
break;
case 'e':
o_escaping = false;
break;
case 'v':
strncpy(volume_path, optarg, B_FILE_NAME_LENGTH);
break;
default:
usage();
break;
}
}
BVolume query_volume;
if (!o_all_volumes) {
// Find the volume that the query should be performed on,
// and set the query to it.
BEntry e(volume_path);
if (e.InitCheck() != B_OK) {
printf("query: %s is not a valid file\n", volume_path);
exit(0);
}
e.GetVolume(&query_volume);
if (!query_volume.KnowsQuery())
printf("query: volume containing %s is not query-enabled\n", volume_path);
else
perform_query(&query_volume, argv[optind]);
exit(0);
}
// Okay, we want to query all the disks -- so iterate over
// them, one by one, running the query.
BVolumeRoster volume_roster;
while (volume_roster.GetNextVolume(&query_volume) == B_OK) {
// We don't print errors here -- this will catch /pipe and
// other filesystems we don't care about.
if (query_volume.KnowsQuery())
perform_query(&query_volume, argv[optind]);
}
exit(0);
}