Files
haiku-beta6/src/bin/open.cpp
T

148 lines
3.4 KiB
C++
Raw Normal View History

/*
2008-10-01 14:00:07 +00:00
* Copyright 2003-2007, Axel Dörfler, [email protected].
* Copyright 2005-2007, François Revol, [email protected].
* Copyright 2009, Jonas Sundström, [email protected].
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel Dörfler, [email protected]
* François Revol, [email protected]
* John Scipione, [email protected]
* Jonas Sundström, [email protected]
*/
/*! Launches an application/document from the shell */
2003-02-25 18:00:40 +00:00
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
2014-08-07 23:12:54 +02:00
#include <strings.h>
2003-02-25 18:00:40 +00:00
#include <Entry.h>
2004-10-31 12:11:45 +00:00
#include <List.h>
#include <Mime.h>
#include <Roster.h>
#include <String.h>
#include <Url.h>
2003-02-25 18:00:40 +00:00
#include <tracker_private.h>
status_t
2014-06-25 19:33:18 -04:00
open_file(const char* openWith, BEntry& entry, int32 line = -1, int32 col = -1)
{
entry_ref ref;
2014-06-25 19:33:18 -04:00
status_t result = entry.GetRef(&ref);
if (result != B_OK)
return result;
2014-06-25 19:33:18 -04:00
BMessenger target(openWith != NULL ? 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);
2014-06-25 19:33:18 -04:00
if (col > -1)
message.AddInt32("be:column", col);
// tell the app to open the file
return target.SendMessage(&message);
}
2003-02-25 18:00:40 +00:00
int
2014-06-25 19:33:18 -04:00
main(int argc, char** argv)
2003-02-25 18:00:40 +00:00
{
2014-06-25 19:33:18 -04:00
int exitCode = EXIT_SUCCESS;
const char* openWith = NULL;
2014-06-25 19:33:18 -04:00
char* progName = argv[0];
2003-02-25 18:00:40 +00:00
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) {
2014-06-25 19:33:18 -04:00
status_t result = B_OK;
argc--;
2003-02-25 18:00:40 +00:00
BEntry entry(*argv);
2014-06-25 19:33:18 -04:00
if ((result = entry.InitCheck()) == B_OK && entry.Exists()) {
result = open_file(openWith, entry);
2003-02-25 18:00:40 +00:00
} else if (!strncasecmp("application/", *argv, 12)) {
// maybe it's an application-mimetype?
// subsequent files are open with that app
openWith = *argv;
// in the case the app is already started,
// don't start it twice if we have other args
BList teams;
if (argc > 1)
be_roster->GetAppList(*argv, &teams);
if (teams.IsEmpty())
2014-06-25 19:33:18 -04:00
result = be_roster->Launch(*argv);
else
2014-06-25 19:33:18 -04:00
result = B_OK;
} else if (strchr(*argv, ':')) {
// try to open it as an URI
2014-07-02 23:24:57 +02:00
BUrl url(*argv);
if (url.OpenWithPreferredApplication() == B_OK) {
result = B_OK;
continue;
}
// maybe it's "file:line" or "file:line:col"
int line = 0, col = 0, i;
2014-06-25 19:33:18 -04:00
result = B_ENTRY_NOT_FOUND;
// remove gcc error's last :
BString arg(*argv);
if (arg[arg.Length() - 1] == ':')
arg.Truncate(arg.Length() - 1);
i = arg.FindLast(':');
if (i > 0) {
line = atoi(arg.String() + i + 1);
arg.Truncate(i);
2014-06-25 19:33:18 -04:00
result = entry.SetTo(arg.String());
if (result == B_OK && entry.Exists()) {
result = open_file(openWith, entry, line);
if (result == B_OK)
continue;
}
// get the column
col = line;
i = arg.FindLast(':');
line = atoi(arg.String() + i + 1);
arg.Truncate(i);
2014-06-25 19:33:18 -04:00
result = entry.SetTo(arg.String());
if (result == B_OK && entry.Exists())
result = open_file(openWith, entry, line, col);
}
} else
2014-06-25 19:33:18 -04:00
result = B_ENTRY_NOT_FOUND;
2014-06-25 19:33:18 -04:00
if (result != B_OK && result != B_ALREADY_RUNNING) {
2008-10-01 14:00:07 +00:00
fprintf(stderr, "%s: \"%s\": %s\n", progName, *argv,
2014-06-25 19:33:18 -04:00
strerror(result));
// make sure the shell knows this
2014-06-25 19:33:18 -04:00
exitCode = EXIT_FAILURE;
}
2003-02-25 18:00:40 +00:00
}
2014-06-25 19:33:18 -04:00
return exitCode;
2003-02-25 18:00:40 +00:00
}