2013-03-05 13:16:36 +00:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2015-02-02 11:03:13 +01:00
|
|
|
#ifndef INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
|
|
|
|
#define INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
|
2014-07-28 20:23:03 +02:00
|
|
|
|
|
|
|
#include "LibreOfficeKit.h"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
2015-02-04 17:41:55 +01:00
|
|
|
#if defined(__linux__) || defined (__FreeBSD_kernel__) || defined(_AIX) || defined(_WIN32) || defined(__APPLE__)
|
2013-03-05 13:16:36 +00:00
|
|
|
|
2013-03-05 16:19:58 +00:00
|
|
|
#include <stdio.h>
|
2014-07-16 19:36:53 +02:00
|
|
|
#include <stdlib.h>
|
2013-03-05 16:19:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-11-14 10:56:40 +01:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include "dlfcn.h"
|
|
|
|
#ifdef _AIX
|
|
|
|
# include <sys/ldr.h>
|
|
|
|
#endif
|
2015-03-09 13:55:14 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#define TARGET_LIB "lib" "sofficeapp" ".dylib"
|
|
|
|
#define TARGET_MERGED_LIB "lib" "mergedlo" ".dylib"
|
|
|
|
#else
|
|
|
|
#define TARGET_LIB "lib" "sofficeapp" ".so"
|
|
|
|
#define TARGET_MERGED_LIB "lib" "mergedlo" ".so"
|
|
|
|
#endif
|
2014-11-14 10:56:40 +01:00
|
|
|
#define SEPERATOR '/'
|
|
|
|
|
|
|
|
void *_dlopen(const char *pFN)
|
|
|
|
{
|
2015-03-11 10:47:14 +01:00
|
|
|
return dlopen(pFN, RTLD_LAZY
|
|
|
|
#if defined __clang__ && defined __linux__ \
|
|
|
|
&& defined ENABLE_RUNTIME_OPTIMIZATIONS
|
|
|
|
#if !ENABLE_RUNTIME_OPTIMIZATIONS
|
|
|
|
| RTLD_GLOBAL
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
);
|
2014-11-14 10:56:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *_dlsym(void *Hnd, const char *pName)
|
|
|
|
{
|
|
|
|
return dlsym(Hnd, pName);
|
|
|
|
}
|
|
|
|
|
|
|
|
int _dlclose(void *Hnd)
|
|
|
|
{
|
|
|
|
return dlclose(Hnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void extendUnoPath(const char *pPath)
|
|
|
|
{
|
|
|
|
(void)pPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#define TARGET_LIB "sofficeapp" ".dll"
|
|
|
|
#define TARGET_MERGED_LIB "mergedlo" ".dll"
|
|
|
|
#define SEPERATOR '\\'
|
|
|
|
#define UNOPATH "\\..\\URE\\bin"
|
|
|
|
|
|
|
|
void *_dlopen(const char *pFN)
|
|
|
|
{
|
|
|
|
return (void *) LoadLibrary(pFN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *_dlsym(void *Hnd, const char *pName)
|
|
|
|
{
|
|
|
|
return GetProcAddress((HINSTANCE) Hnd, pName);
|
|
|
|
}
|
|
|
|
|
|
|
|
int _dlclose(void *Hnd)
|
|
|
|
{
|
|
|
|
return FreeLibrary((HINSTANCE) Hnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void extendUnoPath(const char *pPath)
|
|
|
|
{
|
|
|
|
if (!pPath)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char* sEnvPath = NULL;
|
|
|
|
DWORD cChars = GetEnvironmentVariable("PATH", sEnvPath, 0);
|
|
|
|
if (cChars > 0)
|
|
|
|
{
|
|
|
|
sEnvPath = new char[cChars];
|
|
|
|
cChars = GetEnvironmentVariable("PATH", sEnvPath, cChars);
|
|
|
|
//If PATH is not set then it is no error
|
|
|
|
if (cChars == 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND)
|
|
|
|
{
|
|
|
|
delete[] sEnvPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//prepare the new PATH. Add the Ure/bin directory at the front.
|
|
|
|
//note also adding ';'
|
|
|
|
char * sNewPath = new char[strlen(sEnvPath) + strlen(pPath) + strlen(UNOPATH) + 2];
|
|
|
|
sNewPath[0] = L'\0';
|
|
|
|
strcat(sNewPath, pPath);
|
|
|
|
strcat(sNewPath, UNOPATH);
|
|
|
|
if (strlen(sEnvPath))
|
|
|
|
{
|
|
|
|
strcat(sNewPath, ";");
|
|
|
|
strcat(sNewPath, sEnvPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetEnvironmentVariable("PATH", sNewPath);
|
|
|
|
|
|
|
|
delete[] sEnvPath;
|
|
|
|
delete[] sNewPath;
|
|
|
|
}
|
2013-03-05 13:16:36 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-17 19:57:25 +01:00
|
|
|
typedef LibreOfficeKit *(HookFunction)( const char *install_path);
|
2013-03-05 13:16:36 +00:00
|
|
|
|
2014-07-28 20:23:03 +02:00
|
|
|
static LibreOfficeKit *lok_init( const char *install_path )
|
2013-03-05 13:16:36 +00:00
|
|
|
{
|
2013-11-15 12:09:10 +00:00
|
|
|
char *imp_lib;
|
|
|
|
void *dlhandle;
|
|
|
|
HookFunction *pSym;
|
|
|
|
|
2015-02-04 17:41:55 +01:00
|
|
|
#if !(defined(__APPLE__) && defined(__arm__))
|
|
|
|
size_t partial_length;
|
|
|
|
|
2014-07-16 19:36:53 +02:00
|
|
|
if (!install_path)
|
2013-03-05 13:16:36 +00:00
|
|
|
return NULL;
|
2014-07-16 19:36:53 +02:00
|
|
|
|
|
|
|
// allocate large enough buffer
|
|
|
|
partial_length = strlen(install_path);
|
|
|
|
imp_lib = (char *) malloc(partial_length + sizeof(TARGET_LIB) + sizeof(TARGET_MERGED_LIB) + 2);
|
|
|
|
if (!imp_lib)
|
2013-05-13 18:11:22 -05:00
|
|
|
{
|
|
|
|
fprintf( stderr, "failed to open library : not enough memory\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-11-15 12:09:10 +00:00
|
|
|
|
2014-07-16 19:36:53 +02:00
|
|
|
strcpy(imp_lib, install_path);
|
|
|
|
|
2015-02-02 11:13:13 +01:00
|
|
|
extendUnoPath(install_path);
|
2014-11-14 10:56:40 +01:00
|
|
|
|
|
|
|
imp_lib[partial_length++] = SEPERATOR;
|
2014-07-16 19:36:53 +02:00
|
|
|
strcpy(imp_lib + partial_length, TARGET_LIB);
|
2013-11-15 12:09:10 +00:00
|
|
|
|
2014-11-14 10:56:40 +01:00
|
|
|
dlhandle = _dlopen(imp_lib);
|
2014-07-16 19:36:53 +02:00
|
|
|
if (!dlhandle)
|
2013-03-05 13:16:36 +00:00
|
|
|
{
|
2014-07-16 19:36:53 +02:00
|
|
|
strcpy(imp_lib + partial_length, TARGET_MERGED_LIB);
|
|
|
|
|
2014-11-14 10:56:40 +01:00
|
|
|
dlhandle = _dlopen(imp_lib);
|
2014-07-16 19:36:53 +02:00
|
|
|
if (!dlhandle)
|
|
|
|
{
|
2014-07-28 20:23:03 +02:00
|
|
|
fprintf(stderr, "failed to open library '%s' or '%s' in '%s/'\n",
|
|
|
|
TARGET_LIB, TARGET_MERGED_LIB, install_path);
|
2014-07-16 19:36:53 +02:00
|
|
|
free(imp_lib);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-03-05 13:16:36 +00:00
|
|
|
}
|
2015-02-04 17:41:55 +01:00
|
|
|
#else
|
|
|
|
imp_lib = strdup("the app executable");
|
|
|
|
dlhandle = RTLD_MAIN_ONLY;
|
|
|
|
#endif
|
2013-03-05 13:16:36 +00:00
|
|
|
|
2014-11-14 10:56:40 +01:00
|
|
|
pSym = (HookFunction *) _dlsym( dlhandle, "libreofficekit_hook" );
|
2014-07-16 19:36:53 +02:00
|
|
|
if (!pSym)
|
|
|
|
{
|
2013-03-05 13:16:36 +00:00
|
|
|
fprintf( stderr, "failed to find hook in library '%s'\n", imp_lib );
|
2014-11-14 10:56:40 +01:00
|
|
|
_dlclose( dlhandle );
|
2013-05-13 18:11:22 -05:00
|
|
|
free( imp_lib );
|
2013-03-05 13:16:36 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-03-05 16:40:01 +00:00
|
|
|
|
|
|
|
free( imp_lib );
|
2014-06-17 19:57:25 +01:00
|
|
|
return pSym( install_path );
|
2013-03-05 13:16:36 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 17:41:55 +01:00
|
|
|
#endif // defined(__linux__) || defined (__FreeBSD_kernel__) || defined(_AIX) || defined(_WIN32) || defined(__APPLE__)
|
2014-07-28 20:23:03 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-02 11:03:13 +01:00
|
|
|
#endif // INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKITINIT_H
|
2013-03-05 13:16:36 +00:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|