LCOV - code coverage report
Current view: top level - extensions/source/nsplugin/source - so_env.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 0 142 0.0 %
Date: 2014-04-11 Functions: 0 10 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*************************************************************************
       3             :  *
       4             :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5             :  *
       6             :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7             :  *
       8             :  * OpenOffice.org - a multi-platform office productivity suite
       9             :  *
      10             :  * This file is part of OpenOffice.org.
      11             :  *
      12             :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13             :  * it under the terms of the GNU Lesser General Public License version 3
      14             :  * only, as published by the Free Software Foundation.
      15             :  *
      16             :  * OpenOffice.org is distributed in the hope that it will be useful,
      17             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :  * GNU Lesser General Public License version 3 for more details
      20             :  * (a copy is included in the LICENSE file that accompanied this code).
      21             :  *
      22             :  * You should have received a copy of the GNU Lesser General Public License
      23             :  * version 3 along with OpenOffice.org.  If not, see
      24             :  * <http://www.openoffice.org/license.html>
      25             :  * for a copy of the LGPLv3 License.
      26             :  *
      27             :  ************************************************************************/
      28             : 
      29             : #include <config_folders.h>
      30             : 
      31             : #include "sal/config.h"
      32             : 
      33             : #include "nsp_func.hxx"
      34             : 
      35             : #ifdef UNIX
      36             : #include <sys/types.h>
      37             : #include <strings.h>
      38             : #ifdef LINUX
      39             : #include <dlfcn.h>
      40             : #endif
      41             : #include <stdarg.h>
      42             : #endif // End UNIX
      43             : 
      44             : #ifdef WNT
      45             : 
      46             : #ifdef _MSC_VER
      47             : #pragma warning (push,1)
      48             : #pragma warning (disable:4668)
      49             : #pragma warning (disable:4917)
      50             : #endif
      51             : 
      52             : #include <windows.h>
      53             : #include <direct.h>
      54             : #include <stdlib.h>
      55             : #include <shlobj.h>
      56             : #include <objidl.h>
      57             : 
      58             : #ifdef _MSC_VER
      59             : #pragma warning (pop)
      60             : #endif
      61             : #endif // End WNT
      62             : 
      63             : #include <sys/stat.h>
      64             : #include <errno.h>
      65             : #include "so_env.hxx"
      66             : #include "ns_debug.hxx"
      67             : #include <sal/config.h>
      68             : 
      69             : // Tranform all strings like %20 in pPath to one char like space
      70             : /*int retoreUTF8(char* pPath)
      71             : {
      72             :     // Prepare buf
      73             :     int len = strlen(pPath) + 1;
      74             :     char* pBuf = (char*)malloc(len);
      75             :     memset(pBuf, 0, len);
      76             : 
      77             :     // Store the original pBuf and pPath
      78             :     char* pBufCur = pBuf;
      79             :     char* pPathCur = pPath;
      80             :     // ie, for %20, UTF8Numbers[0][0] = 2, UTF8Numbers[1][0] = 0
      81             :     char UTF8Numbers[2][2] = {{0, 0}, {0,0}};
      82             :     int temp;
      83             : 
      84             :     while (*pPathCur) {
      85             :         if (('%' == *pPathCur) && (0 != *(pPathCur + 1))
      86             :             && (0 != *(pPathCur + 2)))
      87             :         {
      88             :             UTF8Numbers[0][0] = *(pPathCur + 1);
      89             :             UTF8Numbers[1][0] = *(pPathCur + 2);
      90             :             temp = 0;
      91             :             temp = atoi(UTF8Numbers[0])*16 + atoi(UTF8Numbers[1]);
      92             :             *pBufCur = (char)temp;
      93             :             pBufCur++;
      94             :             pPathCur += 3;
      95             :         } else {
      96             :             *pBufCur++ = *pPathCur++;
      97             :         }
      98             :     }
      99             : 
     100             :     *pBufCur = 0;
     101             :     strcpy(pPath, pBuf);
     102             :     free(pBuf);
     103             :     return 0;
     104             : }*/
     105             : 
     106             : int
     107           0 : restoreUTF8(char *pPath)
     108             : {
     109             :     unsigned char *s, *d;
     110             : 
     111             : #define XDIGIT(c) ((c) <= '9' ? (c) - '0' : ((c) & 0x4F) - 'A' + 10)
     112             : 
     113           0 :     s = d = (unsigned char *)pPath;
     114           0 :     do {
     115           0 :         if (*s == '%' && s[1] && s[2]) {
     116           0 :             *d++ = (XDIGIT (s[1]) << 4) + XDIGIT (s[2]);
     117           0 :             s += 2;
     118             :         } else
     119           0 :             *d++ = *s;
     120             :     } while (*s++);
     121           0 :     debug_fprintf(NSP_LOG_APPEND, "after restoreUTF8, pPath is %s\n", pPath);
     122           0 :     return 0;
     123             : }
     124             : 
     125             : #ifdef LINUX
     126             : extern int nspluginOOoModuleHook (void** aResult);
     127           0 : int nspluginOOoModuleHook (void** aResult)
     128             : {
     129             :     void *dl_handle;
     130             : 
     131           0 :     dl_handle = dlopen(NULL, RTLD_NOW);
     132           0 :     if (!dl_handle)
     133             :     {
     134           0 :         fprintf (stderr, "Can't open myself '%s'\n", dlerror());
     135           0 :         return 1;
     136             :     }
     137           0 :     dlclose(dl_handle);
     138             : 
     139           0 :     Dl_info dl_info = { 0,0,0,0 };
     140           0 :     if(!dladdr((void *)nspluginOOoModuleHook, &dl_info))
     141             :     {
     142           0 :         fprintf (stderr, "Can't find my own address '%s'\n", dlerror());
     143           0 :         return 1;
     144             :     }
     145             : 
     146           0 :     if (!dl_info.dli_fname)
     147             :     {
     148           0 :         fprintf (stderr, "Can't find my own file name\n");
     149           0 :         return 1;
     150             :     }
     151             : 
     152             :     char cwdstr[NPP_PATH_MAX];
     153           0 :     if (!getcwd (cwdstr, sizeof(cwdstr)))
     154             :     {
     155           0 :         fprintf (stderr, "Can't get cwd\n");
     156           0 :         return 1;
     157             :     }
     158             : 
     159             :     char libFileName[NPP_PATH_MAX];
     160             : 
     161           0 :     if (dl_info.dli_fname[0] != '/')
     162             :     {
     163           0 :         if ((strlen(cwdstr) + 1 + strlen(dl_info.dli_fname)) >= NPP_PATH_MAX)
     164             :         {
     165           0 :             fprintf (stderr, "Plugin path too long\n");
     166           0 :             return 1;
     167             :         }
     168           0 :         strcpy (libFileName, cwdstr);
     169           0 :         strcat (libFileName, "/");
     170           0 :         strcat (libFileName, dl_info.dli_fname);
     171             :     }
     172             :     else
     173             :     {
     174           0 :         if (strlen(dl_info.dli_fname) >= NPP_PATH_MAX)
     175             :         {
     176           0 :             fprintf (stderr, "Plugin path too long\n");
     177           0 :             return 1;
     178             :         }
     179           0 :         strcpy (libFileName, dl_info.dli_fname);
     180             :     }
     181             : 
     182             :     char *clobber;
     183             :     static char realFileName[NPP_PATH_MAX] = {0};
     184             : #   define SEARCH_SUFFIX "/" LIBO_LIB_FOLDER "/libnpsoplug"
     185             : 
     186           0 :     if (!(clobber = strstr (libFileName, SEARCH_SUFFIX)))
     187             :     {
     188           0 :         ssize_t len = readlink(libFileName, realFileName, NPP_PATH_MAX-1);
     189           0 :         if (len == -1)
     190             :         {
     191           0 :             fprintf (stderr, "Couldn't read link '%s'\n", libFileName);
     192           0 :             return 1;
     193             :         }
     194           0 :         realFileName[len] = '\0';
     195           0 :         if (!(clobber = strstr (realFileName, SEARCH_SUFFIX)))
     196             :         {
     197           0 :                 fprintf (stderr, "Couldn't find suffix in '%s'\n", realFileName);
     198           0 :             return 1;
     199             :         }
     200           0 :         *clobber = '\0';
     201             :     }
     202             :     else
     203             :     {
     204           0 :         *clobber = '\0';
     205           0 :         strcpy (realFileName, libFileName);
     206             :     }
     207             : 
     208             : #if OSL_DEBUG_LEVEL > 0
     209             :     fprintf (stderr, "LibreOffice path before fixup is '%s'\n", realFileName);
     210             : #endif
     211             : 
     212           0 :     if (realFileName[0] != '/') {
     213             :         /* a relative sym-link and we need to get an absolute path */
     214           0 :         char scratch[NPP_PATH_MAX] = {0};
     215           0 :         if (strlen (realFileName) + strlen (libFileName) + 2 >= NPP_PATH_MAX - 1)
     216             :         {
     217           0 :             fprintf (stderr, "Paths too long to fix up.\n");
     218           0 :             return 1;
     219             :         }
     220           0 :         strcpy (scratch, libFileName);
     221           0 :         if (strrchr (scratch, '/')) /* remove the last element */
     222           0 :             *(strrchr (scratch, '/') + 1) = '\0';
     223           0 :         strcat (scratch, realFileName);
     224           0 :         strcpy (realFileName, scratch);
     225             :     }
     226             : 
     227           0 :     *aResult = realFileName;
     228             : 
     229             : #if OSL_DEBUG_LEVEL > 0
     230             :     fprintf (stderr, "LibreOffice path is '%s'\n", realFileName);
     231             : #endif
     232             : 
     233           0 :     return 0;
     234             : }
     235             : #endif
     236             : 
     237             : // *aResult points the static string holding "/opt/staroffice8"
     238           0 : int findReadSversion(void** aResult, int /*bWnt*/, const char* /*tag*/, const char* /*entry*/)
     239             : {
     240             : #ifdef UNIX
     241             :     // The real space to hold "/opt/staroffice8"
     242             :     static char realFileName[NPP_PATH_MAX] = {0};
     243           0 :     memset(realFileName, 0, NPP_PATH_MAX);
     244           0 :     *aResult = realFileName;
     245             : 
     246             :     // Filename of lnk file, eg. "soffice"
     247           0 :     char lnkFileName[NPP_PATH_MAX] = {0};
     248           0 :     char* pTempZero = NULL;
     249             : 
     250             : #ifdef LINUX
     251             :     /* try to fetch a 'self' pointer */
     252           0 :     if (!nspluginOOoModuleHook (aResult))
     253           0 :       return 0;
     254             : 
     255             :     /* .. now in $HOME */
     256             : #endif // LINUX
     257           0 :     snprintf(lnkFileName, NPP_PATH_MAX - 1, "%s/.mozilla/plugins/libnpsoplugin%s", getenv("HOME"), SAL_DLLEXTENSION);
     258             : #ifdef LINUX
     259           0 :     ssize_t len = readlink(lnkFileName, realFileName, NPP_PATH_MAX-1);
     260           0 :     if (-1 == len)
     261             :     {
     262           0 :         *realFileName = 0;
     263           0 :         return -1;
     264             :     }
     265           0 :     realFileName[len] = '\0';
     266             : 
     267           0 :     if (NULL == (pTempZero = strstr(realFileName, "/" LIBO_LIB_FOLDER "/libnpsoplugin" SAL_DLLEXTENSION)))
     268             : #else  // LINUX
     269             :     if ((0 > readlink(lnkFileName, realFileName, NPP_PATH_MAX)) ||
     270             :         (NULL == (pTempZero = strstr(realFileName, "/" LIBO_LIB_FOLDER "/libnpsoplugin" SAL_DLLEXTENSION))))
     271             : #endif // LINUX
     272             :     {
     273           0 :         *realFileName = 0;
     274           0 :         return -1;
     275             :     }
     276           0 :     *pTempZero = 0;
     277           0 :     return 0;
     278             : #elif defined WNT // UNIX
     279             :     static char realFileName[NPP_PATH_MAX] = {0};
     280             :     *aResult = realFileName;
     281             :     HKEY hKey;
     282             :     DWORD dwBufLen = NPP_PATH_MAX;
     283             :     LONG lRet;
     284             : 
     285             :     debug_fprintf(NSP_LOG_APPEND, "1 before before strstr realFileName is %s\n", realFileName);
     286             :     lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
     287             :        "SOFTWARE\\MozillaPlugins\\@sun.com/npsopluginmi;version=1.0",
     288             :        0, KEY_QUERY_VALUE, &hKey );
     289             :     if (lRet == ERROR_FILE_NOT_FOUND) {
     290             :         lRet = RegOpenKeyEx(
     291             :             HKEY_CURRENT_USER,
     292             :             "SOFTWARE\\MozillaPlugins\\@sun.com/npsopluginmi;version=1.0", 0,
     293             :             KEY_QUERY_VALUE, &hKey);
     294             :     }
     295             :     debug_fprintf(NSP_LOG_APPEND, "2 before before strstr realFileName is %s\n", realFileName);
     296             :     if( lRet != ERROR_SUCCESS )
     297             :        return FALSE;
     298             : 
     299             :     lRet = RegQueryValueEx( hKey, "Path", NULL, NULL,
     300             :        (LPBYTE) realFileName, &dwBufLen);
     301             :     debug_fprintf(NSP_LOG_APPEND, "3 before before strstr realFileName is %s\n", realFileName);
     302             :     if( (lRet != ERROR_SUCCESS) || (dwBufLen > NPP_PATH_MAX) )
     303             :        return FALSE;
     304             : 
     305             :     RegCloseKey( hKey );
     306             :     char* pTempZero = NULL;
     307             :     debug_fprintf(NSP_LOG_APPEND, "before strstr realFileName is %s\n", realFileName);
     308             :     if (NULL == (pTempZero = strstr(realFileName, "\\program")))
     309             :     {
     310             :         *realFileName = 0;
     311             :         return -1;
     312             :     }
     313             :     *pTempZero = 0;
     314             :     debug_fprintf(NSP_LOG_APPEND, "realFileName is %s\n", realFileName);
     315             :     return 0;
     316             : #endif // UNIX
     317             : }
     318             : 
     319             : // Return the install dir path of staroffice, return value like "/home/build/staroffice"
     320           0 : const char* findInstallDir()
     321             : {
     322             :     static char* pInstall = NULL;
     323           0 :     debug_fprintf(NSP_LOG_APPEND, "start of findInstallDir()\n");
     324           0 :     if (!pInstall)
     325             :     {
     326           0 :         findReadSversion((void**)&pInstall, 0, "[" SECTION_NAME "]", SOFFICE_VERSION "=");
     327           0 :         if (!pInstall)
     328           0 :             pInstall = const_cast< char* >( "" );
     329             :     }
     330           0 :     return pInstall;
     331             : }
     332             : 
     333             : // Return the program dir path of staroffice, return value like "/home/build/staroffice/program"
     334           0 : const char* findProgramDir()
     335             : {
     336             :     static char sProgram[NPP_BUFFER_SIZE] = {0};
     337           0 :     if (!sProgram[0])
     338             :     {
     339           0 :         sprintf(sProgram, "%s/" LIBO_BIN_FOLDER, findInstallDir());
     340             : #ifdef WNT
     341             :         UnixToDosPath(sProgram);
     342             : #endif
     343             :     }
     344           0 :     return sProgram;
     345             : }
     346             : 
     347             : #ifdef WNT
     348             : // Return SO executable absolute path, like "/home/build/staroffice/program/soffice"
     349             : const char* findSofficeExecutable()
     350             : {
     351             :     static char pSofficeExeccutable[NPP_PATH_MAX] = {0};
     352             :     if (!pSofficeExeccutable[0])
     353             :     {
     354             :         sprintf(pSofficeExeccutable, "%s/%s", findProgramDir(), STAROFFICE_EXE_FILE_NAME);
     355             : #ifdef WNT
     356             :         UnixToDosPath(pSofficeExeccutable);
     357             : #endif
     358             :     }
     359             : 
     360             :     return pSofficeExeccutable;
     361             : }
     362             : 
     363             : // Change Dos path such as c:\program\soffice to c:/program/soffice
     364             : int DosToUnixPath(char* sPath)
     365             : {
     366             :     if (!sPath)
     367             :         return -1;
     368             :     char* p = sPath;
     369             :     while (*p)
     370             :     {
     371             :         if(*p == '\\')
     372             :             *p = '/';
     373             :         p++;
     374             :     }
     375             :     return 0;
     376             : 
     377             : }
     378             : #endif
     379             : // Change Unix path such as program/soffice to program\soffice
     380           0 : int UnixToDosPath(char* sPath)
     381             : {
     382           0 :     if (!sPath)
     383           0 :         return -1;
     384           0 :     char* p = sPath;
     385           0 :     while (*p)
     386             :     {
     387           0 :         if(*p == '/')
     388           0 :             *p = '\\';
     389           0 :         p++;
     390             :     }
     391           0 :     return 0;
     392             : 
     393             : }
     394             : 
     395             : #ifdef UNIX
     396             : #define PLUGIN_NAME "LibreOffice"
     397             : char productName[128] = {0};
     398           0 : char* NSP_getProductName()
     399             : {
     400           0 :     if(productName[0])
     401           0 :         return productName;
     402           0 :     char fullBootstrapIniPath[1024] = {0};
     403           0 :     const char* pFullFilePath = findProgramDir();
     404           0 :     if(0 == *pFullFilePath)
     405             :     {
     406           0 :         strcpy(productName, PLUGIN_NAME);
     407           0 :         return productName;
     408             :     }
     409             :     sprintf(fullBootstrapIniPath, "%s/%s", pFullFilePath,
     410           0 :         "bootstraprc");
     411             : 
     412           0 :     FILE* fp = fopen(fullBootstrapIniPath, "r");
     413             : 
     414           0 :     if (NULL == fp)
     415             :     {
     416           0 :         strcpy(productName, PLUGIN_NAME);
     417           0 :         return productName;
     418             :     }
     419           0 :     char line[4096] = {0};
     420           0 :     char *pStart = 0;
     421           0 :     char *pEnd = 0;
     422           0 :     while(!feof(fp))
     423             :     {
     424           0 :         if (fgets( line, sizeof(line), fp ) == NULL)
     425           0 :             continue;
     426           0 :         if (NULL == (pStart = strstr( line, "ProductKey=" )))
     427           0 :             continue;
     428           0 :         pStart += strlen("ProductKey=");
     429           0 :         if (NULL == (pEnd = strchr( pStart, ' ' )) &&
     430             :            (NULL == (pEnd = strchr( pStart, '\r' ))))
     431           0 :             continue;
     432           0 :         *pEnd = 0;
     433           0 :         if (static_cast<size_t>(pEnd - pStart) <= sizeof(productName))
     434           0 :             strcpy(productName, pStart);
     435             :     }
     436           0 :     fclose(fp);
     437           0 :     if (*productName == 0)
     438             :     {
     439           0 :         strcpy(productName, PLUGIN_NAME);
     440           0 :         return productName;
     441             :     }
     442           0 :     return productName;
     443             : }
     444             : 
     445             : char PluginName[1024] = {0};
     446           0 : char* NSP_getPluginName()
     447             : {
     448           0 :     if(*PluginName)
     449           0 :         return PluginName;
     450           0 :     sprintf(PluginName, "%s Plug-in", NSP_getProductName());
     451           0 :     return PluginName;
     452             : }
     453             : 
     454             : char PluginDesc[1024] = {0};
     455           0 : char* NSP_getPluginDesc()
     456             : {
     457           0 :     if(*PluginDesc)
     458           0 :         return PluginDesc;
     459             : 
     460             :     sprintf(PluginDesc, "%s Plug-in handles all its documents",
     461           0 :         productName);
     462           0 :     return PluginDesc;
     463             : }
     464             : #endif //end of UNIX
     465             : 
     466           0 : void NSP_WriteLog(int level,  const char* pFormat, ...)
     467             : {
     468             :     (void)level;
     469             : #ifndef DEBUG
     470             :     (void)pFormat;
     471             : #else
     472             :     va_list      ap;
     473             :     char         msgBuf[NPP_BUFFER_SIZE];
     474             :     static char  logName[NPP_PATH_MAX] = {0};
     475             :     FILE *       fp = NULL;
     476             : 
     477             :     va_start(ap,pFormat);
     478             :     NSP_vsnprintf(msgBuf, NPP_BUFFER_SIZE, pFormat, ap);
     479             :     va_end(ap);
     480             : 
     481             :     if (!logName[0])
     482             :     {
     483             : #ifdef UNIX
     484             :         const char* homeDir = getenv("HOME");
     485             :         sprintf(logName,"%s/%s",homeDir,"nsplugin.log");
     486             : #endif // End UNIX
     487             : #ifdef WNT
     488             :         char szPath[MAX_PATH];
     489             :         if (!SHGetSpecialFolderPath(NULL, szPath, CSIDL_APPDATA, 0))
     490             :         {
     491             :              return;
     492             :         }
     493             :         char* homeDir = szPath;
     494             :         sprintf(logName,"%s\\%s", szPath, "nsplugin.log");
     495             : #endif // End WNT
     496             :     }
     497             :     else
     498             :         fp = fopen(logName, "a+");
     499             : 
     500             :     if (!fp)
     501             :         return;
     502             :     fputs(msgBuf, fp);
     503             :     fclose(fp);
     504             : #endif
     505           0 : }
     506             : 
     507             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10