tdf#164393 Change themes UI as per UX feedback
[LibreOffice.git] / odk / source / unoapploader / unx / unoapploader.c
blobcc83b88bfaf2c4f793791108e45a705aae5e453c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/stat.h>
26 #ifdef LINUX
27 #define __USE_GNU
28 #endif
29 #include <dlfcn.h>
31 #include <cppuhelper/findsofficepath.h>
32 #include <rtl/string.h>
33 #include <sal/types.h>
35 static char* getPath(void);
36 static char* createCommandName( char* argv0 );
38 static const int SEPARATOR = '/';
39 static const char* PATHSEPARATOR = ":";
43 * The main function implements a loader for applications which use UNO.
45 * <p>This code runs on the Unix/Linux platforms only.</p>
47 * <p>The main function detects a UNO installation on the system and adds the
48 * relevant directories of the installation to the LD_LIBRARY_PATH environment
49 * variable. After that, the application process is loaded and started, whereby
50 * the new process inherits the environment of the calling process, including
51 * the modified LD_LIBRARY_PATH environment variable. The application's
52 * executable name must be the same as the name of this executable, prefixed
53 * by '_'.</p>
54 * <p>On MACOSX DYLD_LIBRARY_PATH is used instead of LD_LIBRARY_PATH!<p>
56 * <p>A UNO installation can be specified by the user by setting the UNO_PATH
57 * environment variable to the program directory of the UNO installation.
58 * If no installation is specified by the user, the default installation on
59 * the system will be taken. The default installation is found from the
60 * PATH environment variable. This requires that the 'soffice' executable or
61 * a symbolic link is in one of the directories listed in the PATH environment
62 * variable.</p>
64 int main( int argc, char *argv[] )
66 char* path;
67 char* cmdname;
69 (void) argc; /* avoid warning about unused parameter */
71 /* get the path of the UNO installation */
72 path = getPath();
74 if ( path != NULL )
76 #if defined(MACOSX)
77 static const char* ENVVARNAME = "DYLD_LIBRARY_PATH";
78 #else
79 static const char* ENVVARNAME = "LD_LIBRARY_PATH";
80 #endif
81 char* libpath;
82 char* value;
83 char* envstr;
84 int size;
86 size_t pathlen = strlen(path);
87 struct stat stats;
88 int ret;
90 static char const unoinfoSuffix[] = "/unoinfo";
91 char * unoinfo = malloc(
92 pathlen + RTL_CONSTASCII_LENGTH(unoinfoSuffix) + 1);
93 /*TODO: overflow */
94 if (unoinfo == NULL) {
95 free(path);
96 fprintf(stderr, "Error: out of memory!\n");
97 exit(EXIT_FAILURE);
99 strcpy(unoinfo, path);
100 strcpy(
101 unoinfo + pathlen,
102 unoinfoSuffix + (pathlen == 0 || path[pathlen - 1] != '/' ? 0 : 1));
103 ret = lstat(unoinfo, &stats);
104 free(unoinfo);
106 if (ret == 0) {
107 char * cmd = malloc(
108 2 * pathlen + RTL_CONSTASCII_LENGTH("/unoinfo c++") + 1);
109 /*TODO: overflow */
110 char const * p;
111 char * q;
112 FILE * f;
113 size_t n = 1000;
114 size_t old = 0;
115 if (cmd == NULL) {
116 fprintf(stderr, "Error: out of memory!\n");
117 exit(EXIT_FAILURE);
119 p = path;
120 q = cmd;
121 while (*p != '\0') {
122 *q++ = '\\';
123 *q++ = *p++;
125 if (p == path || p[-1] != '/') {
126 *q++ = '/';
128 strcpy(q, "unoinfo c++");
129 f = popen(cmd, "r");
130 free(cmd);
131 if (f == NULL)
133 fprintf(stderr, "Error: calling unoinfo failed!\n");
134 exit(EXIT_FAILURE);
136 libpath = NULL;
137 for (;;) {
138 size_t m;
139 libpath = realloc(libpath, n);
140 if (libpath == NULL) {
141 fprintf(
142 stderr,
143 "Error: out of memory reading unoinfo output!\n");
144 exit(EXIT_FAILURE);
146 m = fread(libpath + old, 1, n - old - 1, f);
147 if (m != n - old - 1) {
148 if (ferror(f)) {
149 fprintf(stderr, "Error: cannot read unoinfo output!\n");
150 exit(EXIT_FAILURE);
152 libpath[old + m] = '\0';
153 break;
155 if (n >= SAL_MAX_SIZE / 2) {
156 fprintf(
157 stderr,
158 "Error: out of memory reading unoinfo output!\n");
159 exit(EXIT_FAILURE);
161 old = n - 1;
162 n *= 2;
164 if (pclose(f) != 0) {
165 fprintf(stderr, "Error: executing unoinfo failed!\n");
166 exit(EXIT_FAILURE);
168 free(path);
170 else
172 /* Assume an old OOo 2.x installation without unoinfo: */
173 libpath = path;
176 value = getenv( ENVVARNAME );
178 // workaround for finding wrong libsqlite3.dylib in the office installation
179 // For MacOS > 10.6 nss uses the system lib -> unresolved symbol _sqlite3_wal_checkpoint
180 #ifdef MACOSX
181 size = strlen( ENVVARNAME ) + strlen( "=/usr/lib:" ) + strlen( libpath ) + 1;
182 #else
183 size = strlen( ENVVARNAME ) + strlen( "=" ) + strlen( libpath ) + 1;
184 #endif
185 if ( value != NULL )
186 size += strlen( PATHSEPARATOR ) + strlen( value );
187 envstr = (char*) malloc( size );
188 strcpy( envstr, ENVVARNAME );
189 #ifdef MACOSX
190 strcat( envstr, "=/usr/lib:" );
191 #else
192 strcat( envstr, "=" );
193 #endif
194 strcat( envstr, libpath );
195 free( libpath );
196 if ( value != NULL )
198 strcat( envstr, PATHSEPARATOR );
199 strcat( envstr, value );
201 /* coverity[tainted_data : FALSE] */
202 putenv( envstr );
204 else
206 fprintf( stderr, "Warning: no office installation found!\n" );
207 fflush( stderr );
210 /* set the executable name for the application process */
211 cmdname = createCommandName( argv[0] );
212 argv[0] = cmdname;
215 * create the application process;
216 * if successful, execvp doesn't return to the calling process
218 /* coverity[tainted_string] - createCommandName creates a safe string */
219 execvp( cmdname, argv );
220 fprintf( stderr, "Error: execvp failed!\n" );
221 fflush( stderr );
223 return 0;
227 * Gets the path of a UNO installation.
229 * @return the installation path or NULL, if no installation was specified or
230 * found, or if an error occurred.
231 * Returned pointer must be released with free()
233 char* getPath(void)
235 char* path = cppuhelper_detail_findSofficePath();
237 if ( path == NULL )
239 fprintf( stderr, "Warning: getting path from PATH environment "
240 "variable failed!\n" );
241 fflush( stderr );
244 return path;
248 * Creates the application's executable file name.
250 * <p>The application's executable file name is the name of this executable
251 * prefixed by '_'.</p>
253 * @param argv0 specifies the argv[0] parameter of the main function
255 * @return the application's executable file name or NULL, if an error occurred
257 char* createCommandName( char* argv0 )
259 const char* CMDPREFIX = "_";
260 const char* prgname = NULL;
262 char* cmdname = NULL;
263 char* sep = NULL;
264 Dl_info dl_info;
266 /* get the executable file name from argv0 */
267 prgname = argv0;
270 * if argv0 doesn't contain an absolute path name, try to get the absolute
271 * path name from dladdr; note that this only works for Solaris, not for
272 * Linux
274 if ( argv0 != NULL && *argv0 != SEPARATOR &&
275 dladdr( (void*) &createCommandName, &dl_info ) &&
276 dl_info.dli_fname != NULL && *dl_info.dli_fname == SEPARATOR )
278 prgname = dl_info.dli_fname;
281 /* prefix the executable file name by '_' */
282 if ( prgname != NULL )
284 cmdname = (char*) malloc( strlen( prgname ) + strlen( CMDPREFIX ) + 1 );
285 sep = strrchr( prgname, SEPARATOR );
286 if ( sep != NULL )
288 int pos = ++sep - prgname;
289 strncpy( cmdname, prgname, pos );
290 cmdname[ pos ] = '\0';
291 strcat( cmdname, CMDPREFIX );
292 strcat( cmdname, sep );
294 else
296 strcpy( cmdname, CMDPREFIX );
297 strcat( cmdname, prgname );
301 return cmdname;
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */