1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #ifndef INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
11 #define INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
13 #include "LibreOfficeKit.h"
20 #if defined(__linux__) || defined (__FreeBSD_kernel__) || defined(_AIX) || defined(_WIN32) || defined(__APPLE__)
35 #define TARGET_LIB "lib" "sofficeapp" ".dylib"
36 #define TARGET_MERGED_LIB "lib" "mergedlo" ".dylib"
38 #define TARGET_LIB "lib" "sofficeapp" ".so"
39 #define TARGET_MERGED_LIB "lib" "mergedlo" ".so"
43 void *_dlopen(const char *pFN
)
45 return dlopen(pFN
, RTLD_LAZY
46 #if defined __clang__ && defined __linux__ \
47 && defined ENABLE_RUNTIME_OPTIMIZATIONS
48 #if !ENABLE_RUNTIME_OPTIMIZATIONS
60 void *_dlsym(void *Hnd
, const char *pName
)
62 return dlsym(Hnd
, pName
);
65 int _dlclose(void *Hnd
)
70 void extendUnoPath(const char *pPath
)
78 #define TARGET_LIB "sofficeapp" ".dll"
79 #define TARGET_MERGED_LIB "mergedlo" ".dll"
80 #define SEPARATOR '\\'
81 #define UNOPATH "\\..\\URE\\bin"
83 void *_dlopen(const char *pFN
)
85 return (void *) LoadLibrary(pFN
);
91 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, GetLastError(), 0, reinterpret_cast<LPSTR
>(&buf
), 0, NULL
);
95 void *_dlsym(void *Hnd
, const char *pName
)
97 return GetProcAddress((HINSTANCE
) Hnd
, pName
);
100 int _dlclose(void *Hnd
)
102 return FreeLibrary((HINSTANCE
) Hnd
);
105 void extendUnoPath(const char *pPath
)
110 char* sEnvPath
= NULL
;
111 DWORD cChars
= GetEnvironmentVariable("PATH", sEnvPath
, 0);
114 sEnvPath
= new char[cChars
];
115 cChars
= GetEnvironmentVariable("PATH", sEnvPath
, cChars
);
116 //If PATH is not set then it is no error
117 if (cChars
== 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND
)
123 //prepare the new PATH. Add the Ure/bin directory at the front.
124 //note also adding ';'
125 char * sNewPath
= new char[strlen(sEnvPath
) + strlen(pPath
) + strlen(UNOPATH
) + 2];
127 strcat(sNewPath
, pPath
);
128 strcat(sNewPath
, UNOPATH
);
129 if (strlen(sEnvPath
))
131 strcat(sNewPath
, ";");
132 strcat(sNewPath
, sEnvPath
);
135 SetEnvironmentVariable("PATH", sNewPath
);
142 typedef LibreOfficeKit
*(HookFunction
)( const char *install_path
);
144 typedef LibreOfficeKit
*(HookFunction2
)( const char *install_path
, const char *user_profile_path
);
146 static LibreOfficeKit
*lok_init_2( const char *install_path
, const char *user_profile_path
)
151 HookFunction2
*pSym2
;
153 #if !(defined(__APPLE__) && defined(__arm__))
154 size_t partial_length
;
159 // allocate large enough buffer
160 partial_length
= strlen(install_path
);
161 imp_lib
= (char *) malloc(partial_length
+ sizeof(TARGET_LIB
) + sizeof(TARGET_MERGED_LIB
) + 2);
164 fprintf( stderr
, "failed to open library : not enough memory\n");
168 strcpy(imp_lib
, install_path
);
170 extendUnoPath(install_path
);
172 imp_lib
[partial_length
++] = SEPARATOR
;
173 strcpy(imp_lib
+ partial_length
, TARGET_LIB
);
175 dlhandle
= _dlopen(imp_lib
);
178 // If TARGET_LIB exists, and likely is a real library (not a
179 // small one-line text stub as in the --enable-mergedlib
180 // case), but dlopen failed for some reason, don't try
181 // TARGET_MERGED_LIB.
183 if (stat(imp_lib
, &st
) == 0 && st
.st_size
> 100)
185 fprintf(stderr
, "failed to open library '%s': %s\n",
186 imp_lib
, _dlerror());
191 strcpy(imp_lib
+ partial_length
, TARGET_MERGED_LIB
);
193 dlhandle
= _dlopen(imp_lib
);
196 fprintf(stderr
, "failed to open library '%s': %s\n",
197 imp_lib
, _dlerror());
203 imp_lib
= strdup("the app executable");
204 dlhandle
= RTLD_MAIN_ONLY
;
207 pSym2
= (HookFunction2
*) _dlsym( dlhandle
, "libreofficekit_hook_2" );
210 if (user_profile_path
!= NULL
)
212 fprintf( stderr
, "the LibreOffice version in '%s' does not support passing a user profile to the hook function\n",
214 _dlclose( dlhandle
);
218 pSym
= (HookFunction
*) _dlsym( dlhandle
, "libreofficekit_hook" );
221 fprintf( stderr
, "failed to find hook in library '%s'\n", imp_lib
);
222 _dlclose( dlhandle
);
227 // dlhandle is "leaked"
228 // coverity[leaked_storage]
229 return pSym( install_path
);
233 // dlhandle is "leaked"
234 // coverity[leaked_storage]
235 return pSym2( install_path
, user_profile_path
);
240 __attribute__((used
))
242 LibreOfficeKit
*lok_init( const char *install_path
)
244 return lok_init_2( install_path
, NULL
);
247 #undef SEPARATOR // It is used at least in enum class MenuItemType
249 #endif // defined(__linux__) || defined (__FreeBSD_kernel__) || defined(_AIX) || defined(_WIN32) || defined(__APPLE__)
255 #endif // INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */