2 * Copyright 2003-2007, Axel Dörfler, axeld@pinc-software.de.
3 * Copyright 2005-2007, François Revol, revol@free.fr.
4 * Copyright 2009, Jonas Sundström, jonas@kirilla.com.
5 * Copyright 2014 Haiku, Inc. All rights reserved.
7 * Distributed under the terms of the MIT License.
10 * Axel Dörfler, axeld@pinc-software.de
11 * François Revol, revol@free.fr
12 * John Scipione, jscipione@gmail.com
13 * Jonas Sundström, jonas@kirilla.com
16 /*! Launches an application/document from the shell */
31 #include <tracker_private.h>
35 open_file(const char* openWith
, BEntry
& entry
, int32 line
= -1, int32 col
= -1)
38 status_t result
= entry
.GetRef(&ref
);
42 BMessenger
target(openWith
!= NULL
? openWith
: kTrackerSignature
);
43 if (!target
.IsValid())
44 return be_roster
->Launch(&ref
);
46 BMessage
message(B_REFS_RECEIVED
);
47 message
.AddRef("refs", &ref
);
49 message
.AddInt32("be:line", line
);
52 message
.AddInt32("be:column", col
);
54 // tell the app to open the file
55 return target
.SendMessage(&message
);
60 main(int argc
, char** argv
)
62 int exitCode
= EXIT_SUCCESS
;
63 const char* openWith
= NULL
;
65 char* progName
= argv
[0];
66 if (strrchr(progName
, '/'))
67 progName
= strrchr(progName
, '/') + 1;
70 fprintf(stderr
,"usage: %s <file[:line[:column]] or url or application "
71 "signature> ...\n", progName
);
75 status_t result
= B_OK
;
79 if ((result
= entry
.InitCheck()) == B_OK
&& entry
.Exists()) {
80 result
= open_file(openWith
, entry
);
81 } else if (!strncasecmp("application/", *argv
, 12)) {
82 // maybe it's an application-mimetype?
84 // subsequent files are open with that app
87 // in the case the app is already started,
88 // don't start it twice if we have other args
91 be_roster
->GetAppList(*argv
, &teams
);
94 result
= be_roster
->Launch(*argv
);
97 } else if (strchr(*argv
, ':')) {
98 // try to open it as an URI
100 if (url
.OpenWithPreferredApplication() == B_OK
)
103 // maybe it's "file:line" or "file:line:col"
104 int line
= 0, col
= 0, i
;
105 result
= B_ENTRY_NOT_FOUND
;
106 // remove gcc error's last :
108 if (arg
[arg
.Length() - 1] == ':')
109 arg
.Truncate(arg
.Length() - 1);
111 i
= arg
.FindLast(':');
113 line
= atoi(arg
.String() + i
+ 1);
116 result
= entry
.SetTo(arg
.String());
117 if (result
== B_OK
&& entry
.Exists()) {
118 result
= open_file(openWith
, entry
, line
);
125 i
= arg
.FindLast(':');
126 line
= atoi(arg
.String() + i
+ 1);
129 result
= entry
.SetTo(arg
.String());
130 if (result
== B_OK
&& entry
.Exists())
131 result
= open_file(openWith
, entry
, line
, col
);
134 result
= B_ENTRY_NOT_FOUND
;
136 if (result
!= B_OK
&& result
!= B_ALREADY_RUNNING
) {
137 fprintf(stderr
, "%s: \"%s\": %s\n", progName
, *argv
,
139 // make sure the shell knows this
140 exitCode
= EXIT_FAILURE
;