* Fixed all those style issue mmu_man introduced over the years - thanks, man!

* Some possible errors weren't propagated.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19854 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2007-01-18 11:12:16 +00:00
parent 1d03aaeea7
commit 6261c369af
+48 -43
View File
@@ -1,53 +1,55 @@
/* /*
* Copyright 2003-2005, Axel Dörfler, [email protected]. All rights reserved. * Copyright 2003-2007, Axel Dörfler, [email protected]. All rights reserved.
* Copyright 2005-2006, François Revol, [email protected]. * Copyright 2005-2007, François Revol, [email protected].
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
/* Launches an application/document from the shell */ /*! Launches an application/document from the shell */
#include <Roster.h>
#include <Entry.h> #include <Entry.h>
#include <String.h>
#include <List.h> #include <List.h>
#include <Roster.h>
#include <String.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
const char *kTrackerSignature = "application/x-vnd.Be-TRAK"; const char *kTrackerSignature = "application/x-vnd.Be-TRAK";
const char *openWith = NULL;
status_t
status_t OpenFile(BEntry &entry, int32 line=-1, int32 col=-1) open_file(const char* openWith, BEntry &entry, int32 line = -1, int32 col = -1)
{ {
status_t status;
entry_ref ref; entry_ref ref;
entry.GetRef(&ref); status_t status = entry.GetRef(&ref);
if (status < B_OK)
return status;
BMessenger target(openWith?openWith:kTrackerSignature); BMessenger target(openWith ? openWith : kTrackerSignature);
if (target.IsValid()) { if (!target.IsValid())
BMessage message(B_REFS_RECEIVED); return be_roster->Launch(&ref);
message.AddRef("refs", &ref);
if (line > -1) BMessage message(B_REFS_RECEIVED);
message.AddInt32("be:line", line); message.AddRef("refs", &ref);
if (col > -1) if (line > -1)
message.AddInt32("be:column", col); message.AddInt32("be:line", line);
// tell the app to open the file if (col > -1)
status = target.SendMessage(&message); message.AddInt32("be:column", col);
} else
status = be_roster->Launch(&ref); // tell the app to open the file
return status; return target.SendMessage(&message);
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int exitcode = EXIT_SUCCESS; int exitcode = EXIT_SUCCESS;
const char *openWith = NULL;
char *progName = argv[0]; char *progName = argv[0];
if (strrchr(progName, '/')) if (strrchr(progName, '/'))
@@ -62,14 +64,12 @@ main(int argc, char **argv)
BEntry entry(*argv); BEntry entry(*argv);
if ((status = entry.InitCheck()) == B_OK && entry.Exists()) { if ((status = entry.InitCheck()) == B_OK && entry.Exists()) {
status = OpenFile(entry); status = open_file(openWith, entry);
} else if (!strncasecmp("application/", *argv, 12)) { } else if (!strncasecmp("application/", *argv, 12)) {
// maybe it's an application-mimetype? // maybe it's an application-mimetype?
// subsequent files are open with that app // subsequent files are open with that app
openWith = *argv; openWith = *argv;
// clear possible ENOENT from above
status = B_OK;
// in the case the app is already started, // in the case the app is already started,
// don't start it twice if we have other args // don't start it twice if we have other args
@@ -79,6 +79,8 @@ main(int argc, char **argv)
if (teams.IsEmpty()) if (teams.IsEmpty())
status = be_roster->Launch(*argv); status = be_roster->Launch(*argv);
else
status = B_OK;
} else if (strchr(*argv, ':')) { } else if (strchr(*argv, ':')) {
// try to open it as an URI // try to open it as an URI
BString mimeType = "application/x-vnd.Be.URL."; BString mimeType = "application/x-vnd.Be.URL.";
@@ -89,37 +91,40 @@ main(int argc, char **argv)
mimeType.Append(arg, arg.FindFirst(":")); mimeType.Append(arg, arg.FindFirst(":"));
char *args[2] = { *argv, NULL }; char *args[2] = { *argv, NULL };
status = be_roster->Launch(openWith?openWith:mimeType.String(), 1, args); status = be_roster->Launch(openWith ? openWith : mimeType.String(), 1, args);
if (status == B_OK) if (status == B_OK)
continue; continue;
// maybe it's "file:line" or "file:line:col" // maybe it's "file:line" or "file:line:col"
int line = 0, col = 0, i; int line = 0, col = 0, i;
status = B_ENTRY_NOT_FOUND; status = B_ENTRY_NOT_FOUND;
// remove gcc error's last : // remove gcc error's last :
if (arg[arg.Length()-1] == ':') if (arg[arg.Length() - 1] == ':')
arg.Truncate(arg.Length()-1); arg.Truncate(arg.Length() - 1);
i = arg.FindLast(':'); i = arg.FindLast(':');
if (i > 0) { if (i > 0) {
line = atoi(arg.String()+i+1); line = atoi(arg.String() + i + 1);
arg.Truncate(i); arg.Truncate(i);
entry.SetTo(arg.String());
if (entry.Exists()) status = entry.SetTo(arg.String());
status = OpenFile(entry, line-1, col-1); if (status == B_OK && entry.Exists())
status = open_file(openWith, entry, line - 1, col - 1);
if (status == B_OK) if (status == B_OK)
continue; continue;
// get the col
// get the column
col = line; col = line;
i = arg.FindLast(':'); i = arg.FindLast(':');
line = atoi(arg.String()+i+1); line = atoi(arg.String() + i + 1);
arg.Truncate(i); arg.Truncate(i);
entry.SetTo(arg.String());
if (entry.Exists()) status = entry.SetTo(arg.String());
status = OpenFile(entry, line-1, col-1); if (status == B_OK && entry.Exists())
status = open_file(openWith, entry, line - 1, col - 1);
} }
} else { } else
status = B_ENTRY_NOT_FOUND; status = B_ENTRY_NOT_FOUND;
}
if (status != B_OK && status != B_ALREADY_RUNNING) { if (status != B_OK && status != B_ALREADY_RUNNING) {
fprintf(stderr, "%s: \"%s\": %s\n", progName, *argv, strerror(status)); fprintf(stderr, "%s: \"%s\": %s\n", progName, *argv, strerror(status));