2005-01-19 00:10:48 +00:00
|
|
|
/*
|
2008-10-01 14:00:07 +00:00
|
|
|
* Copyright 2003-2007, Axel Dörfler, axeld@pinc-software.de.
|
2007-01-18 11:12:16 +00:00
|
|
|
* Copyright 2005-2007, François Revol, revol@free.fr.
|
2008-10-01 14:00:07 +00:00
|
|
|
* All rights reserved. Distributed under the terms of the MIT License.
|
2005-01-19 00:10:48 +00:00
|
|
|
*/
|
|
|
|
|
2007-01-18 11:12:16 +00:00
|
|
|
/*! Launches an application/document from the shell */
|
2005-01-19 00:10:48 +00:00
|
|
|
|
2003-02-25 18:00:40 +00:00
|
|
|
|
|
|
|
#include <Entry.h>
|
2004-10-31 12:11:45 +00:00
|
|
|
#include <List.h>
|
2008-10-01 16:27:15 +00:00
|
|
|
#include <Mime.h>
|
2007-01-18 11:12:16 +00:00
|
|
|
#include <Roster.h>
|
|
|
|
#include <String.h>
|
2003-02-25 18:00:40 +00:00
|
|
|
|
2007-01-18 11:12:16 +00:00
|
|
|
#include <errno.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>
|
|
|
|
|
|
|
|
|
2005-01-19 00:10:48 +00:00
|
|
|
const char *kTrackerSignature = "application/x-vnd.Be-TRAK";
|
|
|
|
|
2006-05-22 20:10:41 +00:00
|
|
|
|
2007-01-18 11:12:16 +00:00
|
|
|
status_t
|
|
|
|
open_file(const char* openWith, BEntry &entry, int32 line = -1, int32 col = -1)
|
2006-05-22 20:10:41 +00:00
|
|
|
{
|
|
|
|
entry_ref ref;
|
2007-01-18 11:12:16 +00:00
|
|
|
status_t status = entry.GetRef(&ref);
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
BMessenger target(openWith ? openWith : kTrackerSignature);
|
|
|
|
if (!target.IsValid())
|
|
|
|
return be_roster->Launch(&ref);
|
|
|
|
|
|
|
|
BMessage message(B_REFS_RECEIVED);
|
|
|
|
message.AddRef("refs", &ref);
|
|
|
|
if (line > -1)
|
|
|
|
message.AddInt32("be:line", line);
|
|
|
|
if (col > -1)
|
|
|
|
message.AddInt32("be:column", col);
|
|
|
|
|
|
|
|
// tell the app to open the file
|
|
|
|
return target.SendMessage(&message);
|
2006-05-22 20:10:41 +00:00
|
|
|
}
|
2005-01-19 00:10:48 +00:00
|
|
|
|
2007-01-18 11:12:16 +00:00
|
|
|
|
2003-02-25 18:00:40 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2005-11-09 13:46:50 +00:00
|
|
|
int exitcode = EXIT_SUCCESS;
|
2007-01-18 11:12:16 +00:00
|
|
|
const char *openWith = NULL;
|
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;
|
|
|
|
|
2008-10-01 14:00:07 +00:00
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr,"usage: %s <file[:line[:column]] or url or application "
|
|
|
|
"signature> ...\n", progName);
|
|
|
|
}
|
2003-02-25 18:00:40 +00:00
|
|
|
|
|
|
|
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()) {
|
2007-01-18 11:12:16 +00:00
|
|
|
status = open_file(openWith, entry);
|
2003-02-25 18:00:40 +00:00
|
|
|
} else if (!strncasecmp("application/", *argv, 12)) {
|
|
|
|
// maybe it's an application-mimetype?
|
2007-01-18 11:12:16 +00:00
|
|
|
|
2004-10-31 12:10:01 +00:00
|
|
|
// subsequent files are open with that app
|
|
|
|
openWith = *argv;
|
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)
|
2006-05-22 20:10:41 +00:00
|
|
|
be_roster->GetAppList(*argv, &teams);
|
2005-01-19 00:10:48 +00:00
|
|
|
|
2004-10-31 12:10:01 +00:00
|
|
|
if (teams.IsEmpty())
|
2006-05-22 20:10:41 +00:00
|
|
|
status = be_roster->Launch(*argv);
|
2007-01-18 11:12:16 +00:00
|
|
|
else
|
|
|
|
status = B_OK;
|
2005-01-19 00:10:48 +00:00
|
|
|
} 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);
|
2008-02-14 22:53:34 +00:00
|
|
|
mimeType.Append(arg, arg.FindFirst(":"));
|
|
|
|
|
|
|
|
// the protocol should be alphanum
|
|
|
|
// we just check if it's registered
|
|
|
|
// if not there is likely no supporting app anyway
|
|
|
|
if (BMimeType::IsValid(mimeType.String())) {
|
|
|
|
char *args[2] = { *argv, NULL };
|
2008-10-01 14:00:07 +00:00
|
|
|
status = be_roster->Launch(openWith ? openWith
|
|
|
|
: mimeType.String(), 1, args);
|
2008-02-14 22:53:34 +00:00
|
|
|
if (status == B_OK)
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-18 11:12:16 +00:00
|
|
|
|
2006-05-22 20:10:41 +00:00
|
|
|
// maybe it's "file:line" or "file:line:col"
|
|
|
|
int line = 0, col = 0, i;
|
2005-01-19 00:10:48 +00:00
|
|
|
status = B_ENTRY_NOT_FOUND;
|
2006-05-22 20:10:41 +00:00
|
|
|
// remove gcc error's last :
|
2007-01-18 11:12:16 +00:00
|
|
|
if (arg[arg.Length() - 1] == ':')
|
|
|
|
arg.Truncate(arg.Length() - 1);
|
|
|
|
|
2006-05-22 20:10:41 +00:00
|
|
|
i = arg.FindLast(':');
|
|
|
|
if (i > 0) {
|
2007-01-18 11:12:16 +00:00
|
|
|
line = atoi(arg.String() + i + 1);
|
2006-05-22 20:10:41 +00:00
|
|
|
arg.Truncate(i);
|
2007-01-18 11:12:16 +00:00
|
|
|
|
|
|
|
status = entry.SetTo(arg.String());
|
|
|
|
if (status == B_OK && entry.Exists())
|
2008-02-14 22:53:34 +00:00
|
|
|
status = open_file(openWith, entry, line);
|
2006-05-22 20:10:41 +00:00
|
|
|
if (status == B_OK)
|
|
|
|
continue;
|
2007-01-18 11:12:16 +00:00
|
|
|
|
|
|
|
// get the column
|
2006-05-22 20:10:41 +00:00
|
|
|
col = line;
|
|
|
|
i = arg.FindLast(':');
|
2007-01-18 11:12:16 +00:00
|
|
|
line = atoi(arg.String() + i + 1);
|
2006-05-22 20:10:41 +00:00
|
|
|
arg.Truncate(i);
|
2007-01-18 11:12:16 +00:00
|
|
|
|
|
|
|
status = entry.SetTo(arg.String());
|
|
|
|
if (status == B_OK && entry.Exists())
|
2008-02-14 22:53:34 +00:00
|
|
|
status = open_file(openWith, entry, line, col);
|
2006-05-22 20:10:41 +00:00
|
|
|
}
|
2007-01-18 11:12:16 +00:00
|
|
|
} else
|
2006-05-22 20:10:41 +00:00
|
|
|
status = B_ENTRY_NOT_FOUND;
|
2005-01-19 00:10:48 +00:00
|
|
|
|
2005-11-09 13:46:50 +00:00
|
|
|
if (status != B_OK && status != B_ALREADY_RUNNING) {
|
2008-10-01 14:00:07 +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
|
|
|
}
|