diff --git a/src/bin/open.cpp b/src/bin/open.cpp index af3d3398a5..b93feaf3d0 100644 --- a/src/bin/open.cpp +++ b/src/bin/open.cpp @@ -1,5 +1,6 @@ /* * Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Copyright 2005-2006, François Revol, revol@free.fr. * Distributed under the terms of the MIT License. */ @@ -19,11 +20,33 @@ const char *kTrackerSignature = "application/x-vnd.Be-TRAK"; +const char *openWith = kTrackerSignature; + + +status_t OpenFile(BEntry &entry, int32 line=-1, int32 col=-1) +{ + status_t status; + entry_ref ref; + entry.GetRef(&ref); + + BMessenger target(openWith); + if (target.IsValid()) { + 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 + status = target.SendMessage(&message); + } else + status = be_roster->Launch(&ref); + return status; +} int main(int argc, char **argv) { - const char *openWith = kTrackerSignature; int exitcode = EXIT_SUCCESS; char *progName = argv[0]; @@ -31,9 +54,7 @@ main(int argc, char **argv) progName = strrchr(progName, '/') + 1; if (argc < 2) - fprintf(stderr,"usage: %s ...\n", progName); - - BRoster roster; + fprintf(stderr,"usage: %s ...\n", progName); while (*++argv) { status_t status = B_OK; @@ -41,17 +62,7 @@ main(int argc, char **argv) BEntry entry(*argv); if ((status = entry.InitCheck()) == B_OK && entry.Exists()) { - entry_ref ref; - entry.GetRef(&ref); - - BMessenger target(openWith); - if (target.IsValid()) { - BMessage message(B_REFS_RECEIVED); - message.AddRef("refs", &ref); - // tell the app to open the file - status = target.SendMessage(&message); - } else - status = roster.Launch(&ref); + status = OpenFile(entry); } else if (!strncasecmp("application/", *argv, 12)) { // maybe it's an application-mimetype? @@ -64,10 +75,10 @@ main(int argc, char **argv) // don't start it twice if we have other args BList teams; if (argc > 1) - roster.GetAppList(*argv, &teams); + be_roster->GetAppList(*argv, &teams); if (teams.IsEmpty()) - status = roster.Launch(*argv); + status = be_roster->Launch(*argv); } else if (strchr(*argv, ':')) { // try to open it as an URI BString mimeType = "application/x-vnd.Be.URL."; @@ -78,9 +89,37 @@ main(int argc, char **argv) mimeType.Append(arg, arg.FindFirst(":")); char *args[2] = { *argv, NULL }; - status = roster.Launch(mimeType.String(), 1, args); - } else + status = be_roster->Launch(mimeType.String(), 1, args); + if (status == B_OK) + continue; + + // maybe it's "file:line" or "file:line:col" + int line = 0, col = 0, i; status = B_ENTRY_NOT_FOUND; + // remove gcc error's last : + 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); + entry.SetTo(arg.String()); + if (entry.Exists()) + status = OpenFile(entry, line-1, col-1); + if (status == B_OK) + continue; + // get the col + col = line; + i = arg.FindLast(':'); + line = atoi(arg.String()+i+1); + arg.Truncate(i); + entry.SetTo(arg.String()); + if (entry.Exists()) + status = OpenFile(entry, line-1, col-1); + } + } else { + status = B_ENTRY_NOT_FOUND; + } if (status != B_OK && status != B_ALREADY_RUNNING) { fprintf(stderr, "%s: \"%s\": %s\n", progName, *argv, strerror(status));