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

Generated by: LCOV version 1.10