update emoji autocorrect entries from po-files
[LibreOffice.git] / include / LibreOfficeKit / LibreOfficeKitInit.h
blobf1966c764605b4cc841785adae0494c962d7b376
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/.
8 */
10 #ifndef INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
11 #define INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
13 #include "LibreOfficeKit.h"
15 #ifdef __cplusplus
16 extern "C"
18 #endif
20 #if defined(__linux__) || defined (__FreeBSD_kernel__) || defined(_AIX) || defined(_WIN32) || defined(__APPLE__)
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
27 #ifndef _WIN32
29 #include "dlfcn.h"
31 #ifdef _AIX
32 # include <sys/ldr.h>
33 #endif
34 #ifdef __APPLE__
35 #define TARGET_LIB "lib" "sofficeapp" ".dylib"
36 #define TARGET_MERGED_LIB "lib" "mergedlo" ".dylib"
37 #else
38 #define TARGET_LIB "lib" "sofficeapp" ".so"
39 #define TARGET_MERGED_LIB "lib" "mergedlo" ".so"
40 #endif
41 #define SEPARATOR '/'
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
49 | RTLD_GLOBAL
50 #endif
51 #endif
55 char *_dlerror(void)
57 return dlerror();
60 void *_dlsym(void *Hnd, const char *pName)
62 return dlsym(Hnd, pName);
65 int _dlclose(void *Hnd)
67 return dlclose(Hnd);
70 void extendUnoPath(const char *pPath)
72 (void)pPath;
75 #else
77 #include <windows.h>
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);
88 char *_dlerror(void)
90 LPSTR buf = NULL;
91 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, reinterpret_cast<LPSTR>(&buf), 0, NULL);
92 return buf;
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)
107 if (!pPath)
108 return;
110 char* sEnvPath = NULL;
111 DWORD cChars = GetEnvironmentVariable("PATH", sEnvPath, 0);
112 if (cChars > 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)
119 delete[] sEnvPath;
120 return;
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];
126 sNewPath[0] = L'\0';
127 strcat(sNewPath, pPath);
128 strcat(sNewPath, UNOPATH);
129 if (strlen(sEnvPath))
131 strcat(sNewPath, ";");
132 strcat(sNewPath, sEnvPath);
135 SetEnvironmentVariable("PATH", sNewPath);
137 delete[] sEnvPath;
138 delete[] sNewPath;
140 #endif
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 )
148 char *imp_lib;
149 void *dlhandle;
150 HookFunction *pSym;
151 HookFunction2 *pSym2;
153 #if !(defined(__APPLE__) && defined(__arm__))
154 size_t partial_length;
156 if (!install_path)
157 return NULL;
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);
162 if (!imp_lib)
164 fprintf( stderr, "failed to open library : not enough memory\n");
165 return NULL;
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);
176 if (!dlhandle)
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.
182 struct stat st;
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());
187 free(imp_lib);
188 return NULL;
191 strcpy(imp_lib + partial_length, TARGET_MERGED_LIB);
193 dlhandle = _dlopen(imp_lib);
194 if (!dlhandle)
196 fprintf(stderr, "failed to open library '%s': %s\n",
197 imp_lib, _dlerror());
198 free(imp_lib);
199 return NULL;
202 #else
203 imp_lib = strdup("the app executable");
204 dlhandle = RTLD_MAIN_ONLY;
205 #endif
207 pSym2 = (HookFunction2 *) _dlsym( dlhandle, "libreofficekit_hook_2" );
208 if (!pSym2)
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",
213 imp_lib );
214 _dlclose( dlhandle );
215 free( imp_lib );
216 return NULL;
218 pSym = (HookFunction *) _dlsym( dlhandle, "libreofficekit_hook" );
219 if (!pSym)
221 fprintf( stderr, "failed to find hook in library '%s'\n", imp_lib );
222 _dlclose( dlhandle );
223 free( imp_lib );
224 return NULL;
226 free( imp_lib );
227 // dlhandle is "leaked"
228 // coverity[leaked_storage]
229 return pSym( install_path );
232 free( imp_lib );
233 // dlhandle is "leaked"
234 // coverity[leaked_storage]
235 return pSym2( install_path, user_profile_path );
238 static
239 #ifdef __GNUC__
240 __attribute__((used))
241 #endif
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__)
251 #ifdef __cplusplus
253 #endif
255 #endif // INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */