Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2374318821c221557f2000c37e9f19847827d761 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*******************************************************************************
 * Copyright (c) 2000, 2010 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
 *******************************************************************************/

/* Eclipse Mozilla Utility Methods */

#ifdef MOZILLA_FIX

#include "eclipseMozilla.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>


/* Filter function used by fixEnvForMozilla() for finding directories
 * with a desired prefix.
 */
int filter(const struct dirent *dir)
{
	char* prefixes[] = {
		"xulrunner\0",
		"xulrunner-1",
		"mozilla-seamonkey-1",
		"seamonkey-1",
		"mozilla-1",
		"mozilla-firefox-2",
		"firefox-2",
		"mozilla-firefox-3",
		"firefox-3",
		NULL
	};
	int XULRUNNER_INDEX = 0;
#if defined(__amd64__) || defined(__x86_64__) || defined(__powerpc64__)
	char* root = "/usr/lib64/";
#else
	char* root = "/usr/lib/";
#endif

#if defined (SOLARIS)
	/*
	 * The solaris compiler does not do static linking, so just check
	 * for a common lib to ensure that the install seems valid.
	 */
	char* testlib = "/libxpcom.so";
#else
	/* Ensure that the install is dynamically-linked and is built with GTK2 */
	char* testlib = "/components/libwidget_gtk2.so";
#endif

	struct stat buf;
	int index = 0;
	char* dirname = (char *)dir->d_name;

	char* prefix = prefixes [index];
	while (prefix != NULL)
	{
		int prefixLength = strlen(prefix);
		if (strncmp(dirname, prefix, prefixLength) == 0)
		{
			/* If a xulrunner install is found then success is immediate since
			 * xulrunner always provides an embeddable GRE.
			 */
			if (index == XULRUNNER_INDEX) return 1;	/* include in scandir result */

			int dirLength = strlen(dirname);
			char* testpath = malloc (strlen(root) + dirLength + strlen(testlib) + 1);
			strcpy(testpath, root);
			strcat(testpath, dirname);
			strcat(testpath, testlib);
			int success = stat(testpath, &buf) == 0;
			free(testpath);
			if (success)
			{
				return 1;	/* include in scandir result */
			}
		}
		prefix = prefixes [++index];
	}
	return 0;	/* exclude from scandir result */
}

#if defined (SOLARIS)
/*
 * A replacement for 
 * 			scandir(const char *dir, struct dirent ***namelist, filter, alphasort);
 * because scandir & alphasort don't exist on Solaris 9.
 * Return the dirent->d_name that was sorted the highest according to strcoll, 
 *            or NULL on error or if no entries matched the filter.
 * The caller is responsible for freeing the returned string
 */
char * scan(const char * path) {
	DIR *dir = NULL;
	struct dirent * entry = NULL;
	char * candidate = NULL;
	
	if ((dir = opendir(path)) == NULL) {
		return NULL;
	}

	while ((entry = readdir(dir)) != NULL) {
		if (filter(entry)) {
			if (candidate == NULL) {
				candidate = strdup(entry->d_name);
			} else if (strcoll(candidate, entry->d_name) < 0) {
				free(candidate);
				candidate = strdup(entry->d_name);
			}
		}
	}
	closedir(dir);
	
	return candidate;
}
#endif

/* Set the environmnent required by the SWT Browser widget to bind to Mozilla. 
 * The SWT Browser widget relies on Mozilla on Linux. The LD_LIBRARY_PATH
 * and the Mozilla environment variable MOZILLA_FIVE_HOME must point
 * to the installation directory of Mozilla.
 * 
 * 1. Use the location set by MOZILLA_FIVE_HOME if it is defined
 * 2. Parse the file /etc/gre.conf if it is defined. This file is
 *    set by the RedtHat RPM manager.
 * 3. Try some common installation locations.
 */
void fixEnvForMozilla() {
	static int fixed = 0;
	if (fixed) return; 
	{
		char *ldPath = (char*)getenv("LD_LIBRARY_PATH");
		char *mozillaFiveHome = (char*)getenv("MOZILLA_FIVE_HOME");
		char *grePath = NULL; /* Gecko Runtime Environment Location */
		fixed = 1;
		/* Always dup the string so we can free later */
		if (ldPath != NULL) ldPath = strdup(ldPath);
		else ldPath = strdup("");
		
		/* MOZILLA_FIVE_HOME (if defined) points to the Mozilla
		 * install directory. Don't look any further if it is set.
		 */
		if (mozillaFiveHome != NULL) 
		{
			grePath = strdup(mozillaFiveHome);
		}

		/* The file gre.conf (if available) points to the
		 * Mozilla install directory. Don't look any further if 
		 * it is set.
		 */
		if (grePath == NULL)
		{
			struct stat buf;
			FILE *file = NULL;
#if defined(__amd64__) || defined(__x86_64__) || defined(__powerpc64__)
			if (stat("/etc/gre64.conf", &buf) == 0)
			{
				file = fopen("/etc/gre64.conf", "r");
			}
			else if (stat("/etc/gre.d/gre64.conf", &buf) == 0)
			{
				file = fopen("/etc/gre.d/gre64.conf", "r");
			} else
#endif
			if (stat("/etc/gre.conf", &buf) == 0)
			{
				file = fopen("/etc/gre.conf", "r");
			}
			else if (stat("/etc/gre.d/gre.conf", &buf) == 0)
			{
				file = fopen("/etc/gre.d/gre.conf", "r");
			}
			if (file != NULL)
			{
				char buffer[1024];
				char path[1024];
				while (fgets(buffer, 1024, file) != NULL)
				{
					if (sscanf(buffer, "GRE_PATH=%s", path) == 1)
					{
						grePath = strdup(path);
						break;
					}
				}
				fclose(file);
			}
		}

		/* Try some common installation locations. */
		if (grePath == NULL)
		{
			/* try xulrunner-1*, mozilla-1*, firefox-2/3*, seamonkey-1* directories in /usr/lib/ */
#if defined(__amd64__) || defined(__x86_64__) || defined(__powerpc64__)
			char* dir = "/usr/lib64/";
#else
			char* dir = "/usr/lib/";
#endif
#if defined (SOLARIS)
			char * name = scan(dir);
			if (name != NULL) {
#else
			struct dirent **namelist;
			int i;
			int count = scandir(dir, &namelist, filter, alphasort);
			if (count > 0)
			{
				/* count-1 is used below in an attempt to choose XULRunner
				 * any time one is found
				 */
				char* name = namelist [count - 1]->d_name;
#endif
				grePath = malloc (strlen(dir) + strlen(name) + 1);
				strcpy(grePath, dir);
				strcat(grePath, name);
#if defined (SOLARIS)
				free(name);
#else
				for (i = 0; i < count; i++) {
					free(namelist [i]);
				}
				free(namelist);
#endif
			}

			if (grePath == NULL)
			{
				/* some other typical installation locations */
				char* dirs[] = {
#if defined(__amd64__) || defined(__x86_64__) || defined(__powerpc64__)
					"/usr/lib64/xulrunner/",
					"/usr/lib64/mozilla-firefox/",
					"/usr/lib64/firefox/",
					"/usr/lib64/mozilla-seamonkey/",
					"/usr/lib64/seamonkey/",
					"/usr/lib64/mozilla/",
#endif
#if defined (SOLARIS)
					"/usr/sfw/lib/xulrunner/",
					"/usr/sfw/lib/mozilla-firefox/",
					"/usr/sfw/lib/firefox/",
					"/usr/sfw/lib/mozilla/",
					"/usr/sfw/lib/mozilla-seamonkey/",
					"/usr/sfw/lib/seamonkey/",
#endif
					"/usr/lib/xulrunner/",
					"/usr/lib/mozilla-firefox/",
					"/usr/lib/firefox/",
					"/usr/lib/mozilla-seamonkey/",
					"/usr/lib/seamonkey/",
					"/usr/lib/mozilla/",
					"/usr/local/xulrunner/",
					"/opt/xulrunner/",
					"/usr/local/mozilla-firefox/",
					"/usr/local/firefox/",
					"/opt/mozilla-firefox/",
					"/opt/firefox/",
					"/usr/local/mozilla-seamonkey/",
					"/usr/local/seamonkey/",
					"/opt/mozilla-seamonkey/",
					"/opt/seamonkey/",
					"/usr/local/mozilla/",
					"/opt/mozilla/",
					NULL
				};

#if defined (SOLARIS)
				/*
				 * The solaris compiler does not do static linking, so just check
				 * for a common lib to ensure that the install seems valid.
				 */
				char* testlib = "libxpcom.so";
#else
				/* Ensure that the install is dynamically-linked and is built with GTK2 */
				char* testlib = "components/libwidget_gtk2.so";
#endif

				struct stat buf;
				int index = 0;

				char* dir = dirs [index++];
				while (dir != NULL)
				{
					char* testpath = malloc (strlen(dir) + strlen(testlib) + 1);
					strcpy(testpath, dir);
					strcat(testpath, testlib);
					int success = stat(testpath, &buf) == 0;
					free(testpath);
					if (success)
					{
						grePath = strdup(dir);
						break;
					}
					dir = dirs [index++];
				}
			}
		}

		if (grePath != NULL)
		{
			/* If grePath contains "xul" then do not change the LD_LIBRARY_PATH,
			 * since it is likely that a xulrunner (not a mozilla or firefox)
			 * will be found at runtime.  Note that MOZILLA_FIVE_HOME is still
			 * updated if grePath contains "xul" since this variable can act as
			 * a backup GRE to try if an initially-detected one fails to load.
			 */
			char* current = strrchr(grePath, 'x');
			if (current == NULL || strncmp(current, "xul", 3) != 0) {
				ldPath = (char*)realloc(ldPath, strlen(ldPath) + strlen(grePath) + 2);
				if (strlen(ldPath) > 0) strcat(ldPath, ":");
				strcat(ldPath, grePath);
				setenv("LD_LIBRARY_PATH", ldPath, 1);
			}

			if (mozillaFiveHome == NULL) setenv("MOZILLA_FIVE_HOME", grePath, 1);
			free(grePath);
		}
		free(ldPath);
	}
}
#endif /* MOZILLA_FIX */

Back to the top