1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef Py_PYTHON_H
25 #if defined _MSC_VER
26 #pragma warning(push, 1)
27 #endif
28 #ifdef _DEBUG
29 #undef _DEBUG
30 #include <Python.h>
31 #define _DEBUG
32 #else
33 #include <Python.h>
34 #endif // #ifdef _DEBUG
35 #if defined _MSC_VER
36 #pragma warning(pop)
37 #endif
38 #endif // #ifdef Py_PYTHON_H
39 
40 #include <rtl/string.h>
41 
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #ifdef LINUX
46 #  ifndef __USE_GNU
47 #  define __USE_GNU
48 #  endif
49 #endif
50 #include <dlfcn.h>
51 
52 #if PY_MAJOR_VERSION >= 3
PyInit_pyuno(void)53 SAL_DLLPUBLIC_EXPORT void* PyInit_pyuno(void)
54 #else
55 SAL_DLLPUBLIC_EXPORT void initpyuno ()
56 #endif
57 {
58     Dl_info dl_info;
59 #if PY_MAJOR_VERSION >= 3
60     void* (*func)(void);
61 #else
62     void (*func)(void);
63 #endif
64 
65 #if PY_MAJOR_VERSION >= 3
66     if (dladdr((void*)&PyInit_pyuno, &dl_info) != 0) {
67 #else
68     if (dladdr((void*)&initpyuno, &dl_info) != 0) {
69 #endif
70         void* h = 0;
71 	size_t len = strrchr(dl_info.dli_fname, '/') - dl_info.dli_fname + 1;
72 	char* libname = malloc(len + RTL_CONSTASCII_LENGTH( SAL_DLLPREFIX "pyuno" SAL_DLLEXTENSION ) + 1);
73         strncpy(libname, dl_info.dli_fname, len);
74         strcpy(libname + (len), SAL_DLLPREFIX "pyuno" SAL_DLLEXTENSION);
75 
76         h = dlopen (libname, RTLD_NOW | RTLD_GLOBAL);
77 	free(libname);
78         if( h )
79         {
80 #if PY_MAJOR_VERSION >= 3
81             func = (void* (*)(void))dlsym (h, "PyInit_pyuno");
82 #else
83             func = (void (*)(void))dlsym (h, "initpyuno");
84 #endif
85             return (func) ();
86         }
87     }
88 #if PY_MAJOR_VERSION >= 3
89     return NULL;
90 #endif
91 }
92