2005-01-19 00:10:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Launches an application/document from the shell */
|
|
|
|
|
2003-02-25 18:00:40 +00:00
|
|
|
|
|
|
|
#include <Roster.h>
|
|
|
|
#include <Entry.h>
|
2004-08-15 16:37:30 +00:00
|
|
|
#include <String.h>
|
2004-10-31 12:11:45 +00:00
|
|
|
#include <List.h>
|
2003-02-25 18:00:40 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-11-09 13:46:50 +00:00
|
|
|
#include <stdlib.h>
|
2003-02-25 18:00:40 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
2005-01-19 00:10:48 +00:00
|
|
|
const char *kTrackerSignature = "application/x-vnd.Be-TRAK";
|
|
|
|
|
|
|
|
|
2003-02-25 18:00:40 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2005-01-19 00:10:48 +00:00
|
|
|
const char *openWith = kTrackerSignature;
|
2005-11-09 13:46:50 +00:00
|
|
|
int exitcode = EXIT_SUCCESS;
|
2005-01-19 00:10:48 +00:00
|
|
|
|
2003-02-25 18:00:40 +00:00
|
|
|
char *progName = argv[0];
|
|
|
|
if (strrchr(progName, '/'))
|
|
|
|
progName = strrchr(progName, '/') + 1;
|
|
|
|
|
|
|
|
if (argc < 2)
|
2004-08-15 16:37:30 +00:00
|
|
|
fprintf(stderr,"usage: %s <file or url or application signature> ...\n", progName);
|
2003-02-25 18:00:40 +00:00
|
|
|
|
|
|
|
BRoster roster;
|
|
|
|
|
|
|
|
while (*++argv) {
|
2005-01-19 00:10:48 +00:00
|
|
|
status_t status = B_OK;
|
2004-10-31 12:10:01 +00:00
|
|
|
argc--;
|
2003-02-25 18:00:40 +00:00
|
|
|
|
|
|
|
BEntry entry(*argv);
|
2005-01-19 00:10:48 +00:00
|
|
|
if ((status = entry.InitCheck()) == B_OK && entry.Exists()) {
|
2003-02-25 18:00:40 +00:00
|
|
|
entry_ref ref;
|
|
|
|
entry.GetRef(&ref);
|
|
|
|
|
2004-10-31 12:10:01 +00:00
|
|
|
BMessenger target(openWith);
|
|
|
|
if (target.IsValid()) {
|
2003-02-25 18:00:40 +00:00
|
|
|
BMessage message(B_REFS_RECEIVED);
|
|
|
|
message.AddRef("refs", &ref);
|
2005-01-19 00:10:48 +00:00
|
|
|
// tell the app to open the file
|
|
|
|
status = target.SendMessage(&message);
|
2003-02-25 18:00:40 +00:00
|
|
|
} else
|
2005-01-19 00:10:48 +00:00
|
|
|
status = roster.Launch(&ref);
|
2003-02-25 18:00:40 +00:00
|
|
|
} else if (!strncasecmp("application/", *argv, 12)) {
|
|
|
|
// maybe it's an application-mimetype?
|
2004-10-31 12:10:01 +00:00
|
|
|
|
|
|
|
// subsequent files are open with that app
|
|
|
|
openWith = *argv;
|
2005-11-09 13:46:50 +00:00
|
|
|
// clear possible ENOENT from above
|
|
|
|
status = B_OK;
|
2005-01-19 00:10:48 +00:00
|
|
|
|
2004-10-31 12:10:01 +00:00
|
|
|
// in the case the app is already started,
|
|
|
|
// don't start it twice if we have other args
|
|
|
|
BList teams;
|
2005-01-19 00:10:48 +00:00
|
|
|
if (argc > 1)
|
2004-10-31 12:10:01 +00:00
|
|
|
roster.GetAppList(*argv, &teams);
|
2005-01-19 00:10:48 +00:00
|
|
|
|
2004-10-31 12:10:01 +00:00
|
|
|
if (teams.IsEmpty())
|
2005-01-19 00:10:48 +00:00
|
|
|
status = roster.Launch(*argv);
|
|
|
|
} else if (strchr(*argv, ':')) {
|
|
|
|
// try to open it as an URI
|
|
|
|
BString mimeType = "application/x-vnd.Be.URL.";
|
2004-08-15 16:37:30 +00:00
|
|
|
BString arg(*argv);
|
2005-01-19 00:10:48 +00:00
|
|
|
if (arg.FindFirst("://") >= 0)
|
|
|
|
mimeType.Append(arg, arg.FindFirst("://"));
|
2004-10-28 22:29:12 +00:00
|
|
|
else
|
2005-01-19 00:10:48 +00:00
|
|
|
mimeType.Append(arg, arg.FindFirst(":"));
|
|
|
|
|
2004-08-15 16:37:30 +00:00
|
|
|
char *args[2] = { *argv, NULL };
|
2005-01-19 00:10:48 +00:00
|
|
|
status = roster.Launch(mimeType.String(), 1, args);
|
|
|
|
} else
|
|
|
|
status = B_ENTRY_NOT_FOUND;
|
|
|
|
|
2005-11-09 13:46:50 +00:00
|
|
|
if (status != B_OK && status != B_ALREADY_RUNNING) {
|
2005-01-19 00:10:48 +00:00
|
|
|
fprintf(stderr, "%s: \"%s\": %s\n", progName, *argv, strerror(status));
|
2005-11-09 13:46:50 +00:00
|
|
|
// make sure the shell knows this
|
|
|
|
exitcode = EXIT_FAILURE;
|
|
|
|
}
|
2003-02-25 18:00:40 +00:00
|
|
|
}
|
|
|
|
|
2005-11-09 13:46:50 +00:00
|
|
|
return exitcode;
|
2003-02-25 18:00:40 +00:00
|
|
|
}
|