Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dd2b2db0740367d2d6cdf1c9471d6975f53499ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (c) 2006, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    IBM Corporation - initial API and implementation
 *	  Andrew Niefer
 *    Mikael Barbero
 */

#include "eclipseCommon.h"

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ctype.h>
#include <pwd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include <CoreServices/CoreServices.h>
#include <mach-o/dyld.h>


#define APP_PACKAGE_PATTERN ".app/Contents/MacOS/"
#define APP_PACKAGE "APP_PACKAGE"
#define JAVAROOT "JAVAROOT"

static void debug(const char *fmt, ...);
static void dumpArgs(char *tag, int argc, char* argv[]);
static char *append(char *buffer, const char *s);
static char *appendc(char *buffer, char c);
static char *expandShell(char *arg, const char *appPackage, const char *javaRoot);
static char *my_strcasestr(const char *big, const char *little);

static FILE *fgConsoleLog;
static char *fgAppPackagePath;
static int fgPid;

extern int original_main(int argc, char* argv[]);
int main( int argc, char* argv[] ) {

	SInt32 systemVersion= 0;
	if (Gestalt(gestaltSystemVersion, &systemVersion) == noErr) {
		systemVersion &= 0xffff;
		if (systemVersion < 0x1050) {
			displayMessage("Error", "This application requires Mac OS X 10.5 (Leopard) or greater.");
			return 0;
		}
	}

	fgConsoleLog= fopen("/dev/console", "w");
	fgPid= getpid();

	dumpArgs("start", argc, argv);
	if ( (argc > 1 && strncmp(argv[1], "-psn_", 5) == 0) || argc == 1) {
		/* find path to application bundle (ignoring case) */
		char *pos= my_strcasestr(argv[0], APP_PACKAGE_PATTERN);
		if (pos != NULL) {
			int l= pos-argv[0] + 4;	// reserve space for ".app"
			fgAppPackagePath= malloc(l+1);
			strncpy(fgAppPackagePath, argv[0], l);
			fgAppPackagePath[l]= '\0';	// terminate result
		}

		/* Get the main bundle for the app */
		CFBundleRef mainBundle= CFBundleGetMainBundle();
		if (mainBundle != NULL) {

			/* Get an instance of the info plist.*/
			CFDictionaryRef bundleInfoDict= CFBundleGetInfoDictionary(mainBundle);

			/* If we succeeded, look for our property. */
			if (bundleInfoDict != NULL) {
				CFArrayRef ar= CFDictionaryGetValue(bundleInfoDict, CFSTR("Eclipse"));
				if (ar) {
					CFIndex size= CFArrayGetCount(ar);
					if (size > 0) {
						int i;
						char **old_argv= argv;
						argv= (char**) calloc(size+2, sizeof(char*));
						argc= 0;
						argv[argc++]= old_argv[0];
						for (i= 0; i < size; i++) {
							CFStringRef sr= (CFStringRef) CFArrayGetValueAtIndex (ar, i);
							CFIndex argStringSize= CFStringGetMaximumSizeForEncoding(CFStringGetLength(sr), kCFStringEncodingUTF8);
							char *s= malloc(argStringSize);
							if (CFStringGetCString(sr, s, argStringSize, kCFStringEncodingUTF8)) {
								argv[argc++]= expandShell(s, fgAppPackagePath, NULL);
							} else {
								fprintf(fgConsoleLog, "can't extract bytes\n");
							}
							//free(s);
						}
					}
				} else {
					fprintf(fgConsoleLog, "no Eclipse dict found\n");
				}
			} else {
				fprintf(fgConsoleLog, "no bundle dict found\n");
			}
		} else {
			fprintf(fgConsoleLog, "no bundle found\n");
		}
	}
	int exitcode= original_main(argc, argv);
	debug("<<<< exit(%d)\n", exitcode);
	fclose(fgConsoleLog);
	return exitcode;
}

static void debug(const char *fmt, ...) {
#if DEBUG
	va_list ap;
	va_start(ap, fmt);
	fprintf(fgConsoleLog, "%05d: ", fgPid);
	vfprintf(fgConsoleLog, fmt, ap);
	va_end(ap);
#endif
}

static void dumpArgs(char *tag, int argc, char* argv[]) {
#if DEBUG
	int i;
	if (argc < 0) {
		argc= 0;
		for (i= 0; argv[i] != NULL; i++)
			 argc++;
	}
	debug(">>>> %s:", tag);
	for (i= 0; i < argc && argv[i] != NULL; i++)
		fprintf(fgConsoleLog, " <%s>", argv[i]);
	fprintf(fgConsoleLog, "\n");
#endif
}

/*
 * Expand $APP_PACKAGE, $JAVA_HOME, and does tilde expansion.

	A word beginning with an unquoted tilde character (~) is
	subject to tilde expansion. All the characters up to a
	slash (/) or the end of the word are treated as a username
	and are replaced with the user's home directory. If the
	username is missing (as in ~/foobar), the tilde is
	replaced with the value of the HOME variable (the current
	user's home directory).
 */
static char *expandShell(char *arg, const char *appPackage, const char *javaRoot) {

	if (index(arg, '~') == NULL && index(arg, '$') == NULL)
		return arg;

	char *buffer= strdup("");
	char c, lastChar= ' ';
	const char *cp= arg;
	while ((c = *cp++) != 0) {
		if (isspace(lastChar) && c == '~') {
			char name[100], *dir= NULL;
			int j= 0;
			for (; (c = *cp) != 0; cp++) {
				if (! isalnum(c))
					break;
				name[j++]= c;
				lastChar= c;
			}
			name[j]= '\0';
			if (j > 0) {
				struct passwd *pw= getpwnam(name);
				if (pw != NULL)
					dir= pw->pw_dir;
			} else {
				dir= getenv("HOME");
			}
			if (dir != NULL)
				buffer= append(buffer, dir);

		} else if (c == '$') {
			int l= strlen(APP_PACKAGE);
			if (appPackage != NULL && strncmp(cp, APP_PACKAGE, l) == 0) {
				cp+= l;
				buffer= append(buffer, appPackage);
			} else {
				int l= strlen(JAVAROOT);
				if (javaRoot != NULL && strncmp(cp, JAVAROOT, l) == 0) {
					cp+= l;
					buffer= append(buffer, javaRoot);
				} else {
					buffer= appendc(buffer, c);
				}
			}
		} else
			buffer= appendc(buffer, c);
		lastChar= c;
	}
	return buffer;
}

static char *my_strcasestr(const char *big, const char *little) {
    char *cp, *s, *t;
    for (cp= (char*) big; *cp; cp++) {
        for (s= cp, t= (char*) little; *s && *t; s++, t++)
            if (toupper(*s) != toupper(*t))
                break;
        if (*t == '\0')
            return cp;
    }
    return NULL;
}

static char *append(char *buffer, const char *s) {
	int bl= strlen(buffer);
	int sl= strlen(s);
	buffer= realloc(buffer, bl+sl+1);
	strcpy(&buffer[bl], s);
	return buffer;
}

static char *appendc(char *buffer, char c) {
	int bl= strlen(buffer);
	buffer= realloc(buffer, bl+2);
	buffer[bl++]= c;
	buffer[bl]= '\0';
	return buffer;
}

Back to the top