<commit_before>/*************************************************************************
 *
 *  $RCSfile: npshell.cxx,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: mba $ $Date: 2004-09-02 14:01:05 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRUNTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRUNTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc..
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifdef UNIX

#define XP_UNIX 1

#include <strings.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#endif //end of UNIX

#ifdef WNT

#define _WINDOWS

#include <windows.h>
#include <direct.h>
#include <stdlib.h>
#include <io.h>
#include <sys/types.h>
#include <fcntl.h>


#endif //end of WNT


#include <stdio.h>

#include <string.h>
#include <errno.h>


#include "npapi.h"
#include "npshell.hxx"
#include "so_env.hxx"
#include "so_msg.hxx"
#include "ns_debug.hxx"


#include "nsp_func.hxx"

/***********************************************************************
 *
 * Implementations of plugin API functions
 *
 ***********************************************************************/

static NSP_Mute_Obj send_lock = NSP_New_Mute_Obj();
static NSP_PIPE_FD write_fd = 0;


long int NSP_WriteToPipe(NSP_PIPE_FD fp, void* buf, unsigned long int len)
{
    unsigned long int len_unix = 0, len_wnt = 0;

    len_unix = NSP_Write_Pipe(fp, buf, len, &len_wnt);
#ifdef UNIX
    return  len_unix;
#endif //end of UNIX
#ifdef WNT
    return  len_wnt;
#endif //end of WNT

}

// chdir to staroffice's program dir, start nspluin executable in child process, and use pipe to talk with it
int do_init_pipe()
{
    debug_fprintf(NSP_LOG_APPEND, "enter do_init_pipe 1\n");
    NSP_CHDIR(findProgramDir());
    NSP_PIPE_FD fd[2];

    if( 0 != NSP_Inherited_Pipe(fd) )
        return NPERR_GENERIC_ERROR;

    write_fd = fd[1];   // write fd
#ifdef UNIX
    int nChildPID = fork();

    if( ! nChildPID )  // child process
#endif //end of UNIX
    {
        char s_read_fd[16] = {0};
        char s_write_fd[16] = {0};
        sprintf(s_read_fd,  "%d", fd[0]);
        sprintf(s_write_fd, "%d", fd[1]);
        // transfer pipe fds to nsplugin process
        debug_fprintf(NSP_LOG_APPEND, "try to start plugin exe: %s %s %s.\n", findNsExeFile(), s_read_fd, s_write_fd);

#ifdef UNIX
        execl(findNsExeFile(), findNsExeFile(), s_read_fd, s_write_fd, NULL);
        _exit(255);
#endif //end of UNIX
#ifdef WNT
        char sArgc[NPP_BUFFER_SIZE];
        sprintf(sArgc, "%s %s", s_read_fd, s_write_fd);
        STARTUPINFO NSP_StarInfo;
        memset((void*) &NSP_StarInfo, 0, sizeof(STARTUPINFO));
        NSP_StarInfo.cb = sizeof(STARTUPINFO);
        PROCESS_INFORMATION NSP_ProcessInfo;
        memset((void*)&NSP_ProcessInfo, 0, sizeof(PROCESS_INFORMATION));
        if(!CreateProcess(findNsExeFile(), sArgc, NULL, NULL, TRUE,
                      CREATE_NO_WINDOW, NULL, NULL, &NSP_StarInfo, &NSP_ProcessInfo))
        {
            DWORD Err = GetLastError();
        }
#endif //end of WNT
    }
    NSP_Close_Pipe(fd[0]);
    return NPERR_NO_ERROR;
}

int sendMsg( PLUGIN_MSG* pMsg, unsigned int len, int iEnsure)
{
    NSP_Lock_Mute_Obj(send_lock);
    long int len_w = 0;

    debug_fprintf(NSP_LOG_APPEND, "try to send message type:%d; len: %d\n", pMsg->msg_id, len);
    /*
    debug_fprintf(NSP_LOG_APPEND, "NSPlugin Message: msg_id:%d; instance_id:%d;
        wnd_id:%d;wnd_x:%d;wnd_y:%d;wnd_w:%d;wnd_h:%d; url:%s\n",
        pMsg->msg_id, pMsg->instance_id, pMsg->wnd_id,
        pMsg->wnd_x, pMsg->wnd_y, pMsg->wnd_w, pMsg->wnd_h, pMsg->url);*/
    len_w = NSP_WriteToPipe(write_fd, (void*) pMsg, len);
    if (len_w != len){
        if(errno == EPIPE) // If pipe breaks, then init pipe again and resend the msg
        {
            if(iEnsure){
                debug_fprintf(NSP_LOG_APPEND, "send message error, plugin exec need to be restart\n");
                NSP_Close_Pipe(write_fd);
                do_init_pipe();
                len_w = NSP_WriteToPipe(write_fd, (void*) pMsg, len);
            }
        }
        else if(errno == EINTR) // If interrupted by signal, then continue to send
        {
            long  new_len;
            debug_fprintf(NSP_LOG_APPEND, "send message error, send intrrupted by singal, resend again\n");
            new_len = NSP_WriteToPipe(write_fd, (char*)pMsg+len_w, len-len_w);
            len_w = len_w + new_len;
        }
        else  // else return error
        {
            debug_fprintf(NSP_LOG_APPEND, "send message error :%s.\n", strerror(errno));
            len_w = -1;
        }
    }
    NSP_Unlock_Mute_Obj(send_lock);
    debug_fprintf(NSP_LOG_APPEND, "send message success!\n");
    return len_w;
}

const char* pMimeTypes = "application/vnd.stardivision.calc:sdc:StarCalc 3.0 - 5.0;"
"application/vnd.stardivision.chart:sds:StarChart 3.0 - 5.0;"
"application/vnd.stardivision.draw:sda:StarDraw 3.0 - 5.0;"
"application/vnd.stardivision.impress:sdd:StarImpress 3.0 - 5.0;"
"application/vnd.stardivision.impress-packed:sdp:StarImpress-packed 3.0 - 5.0;"
"application/vnd.stardivision.math:smf:StarMath 3.0 - 5.0;"
"application/vnd.stardivision.writer:vor:StarWriter Template 3.0 - 5.0;"
"application/vnd.stardivision.writer-global:sgl:StarWriter Global 3.0 - 5.0;"
"application/vnd.staroffice.writer:sdw:StarWriter 3.0 - 5.0;"
"application/vnd.sun.xml.calc:sxc:StarCalc;"
"application/vnd.sun.xml.calc.template:stc:StarCalc Template;"
"application/vnd.sun.xml.draw:sxd:StarDraw;"
"application/vnd.sun.xml.draw.template:std:StarDraw Template;"
"application/vnd.sun.xml.impress:sxi:StarImpress;"
"application/vnd.sun.xml.impress.template:sti:StarImpress Template;"
"application/vnd.sun.xml.math:sxm:StarMath;"
"application/vnd.sun.xml.writer:sxw:StarWriter;"
"application/vnd.sun.xml.writer.global:sxg:StarWriter Global;"
"application/vnd.sun.xml.writer.template:stw:StarWriter Template;"
"application/msword:doc:Microsoft Word Document;"
"application/vnd.ms-excel:xls:Microsoft Excel Document;"
"application/vnd.ms-powerpoint:ppt:Microsoft Powerpoint Document;"
"application/x-vnd.oasis.openoffice.text:oot:Open Office Text;"
"application/x-vnd.oasis.openoffice-template:ott:Open Office Text Template;"
"application/x-vnd.oasis.openoffice.text-master:oom:Open Office Master Document;"
"application/x-vnd.oasis.openoffice.text-web:oth:HTML Document Template;"
"application/x-vnd.oasis.openoffice.spreadsheet:oos:Open Office Spreadsheet;"
"application/x-vnd.oasis.openoffice.spreadsheet-template:ots:Open Office Spreadsheet Template;"
"application/x-vnd.oasis.openoffice.drawing:ood:Open Office Drawing;"
"application/x-vnd.oasis.openoffice.drawing-template:otd:Open Office Drawing Template;"
"application/x-vnd.oasis.openoffice.presentation:oop:Open Office Presentation;"
"application/x-vnd.oasis.openoffice.presentation-template:otp:Open Office Presentation Template;"
"application/x-vnd.oasis.openoffice.formula:oof:Open Office Formula";

extern "C"
{
const char*
NPP_GetMIMEDescription(void)
{
    debug_fprintf(NSP_LOG_APPEND, "print by Netscape Plugin,  NPP_GetMIMEDescription:%s.\n", pMimeTypes);
    return(pMimeTypes);
}

#ifdef UNIX
NPError
NPP_GetValue(void * instance, NPPVariable variable, void *value)
{<commit_msg>#i10000# do not use const (MBA)<commit_after>/*************************************************************************
 *
 *  $RCSfile: npshell.cxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: kz $ $Date: 2004-09-02 14:08:39 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRUNTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRUNTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc..
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifdef UNIX

#define XP_UNIX 1

#include <strings.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#endif //end of UNIX

#ifdef WNT

#define _WINDOWS

#include <windows.h>
#include <direct.h>
#include <stdlib.h>
#include <io.h>
#include <sys/types.h>
#include <fcntl.h>


#endif //end of WNT


#include <stdio.h>

#include <string.h>
#include <errno.h>


#include "npapi.h"
#include "npshell.hxx"
#include "so_env.hxx"
#include "so_msg.hxx"
#include "ns_debug.hxx"


#include "nsp_func.hxx"

/***********************************************************************
 *
 * Implementations of plugin API functions
 *
 ***********************************************************************/

static NSP_Mute_Obj send_lock = NSP_New_Mute_Obj();
static NSP_PIPE_FD write_fd = 0;


long int NSP_WriteToPipe(NSP_PIPE_FD fp, void* buf, unsigned long int len)
{
    unsigned long int len_unix = 0, len_wnt = 0;

    len_unix = NSP_Write_Pipe(fp, buf, len, &len_wnt);
#ifdef UNIX
    return  len_unix;
#endif //end of UNIX
#ifdef WNT
    return  len_wnt;
#endif //end of WNT

}

// chdir to staroffice's program dir, start nspluin executable in child process, and use pipe to talk with it
int do_init_pipe()
{
    debug_fprintf(NSP_LOG_APPEND, "enter do_init_pipe 1\n");
    NSP_CHDIR(findProgramDir());
    NSP_PIPE_FD fd[2];

    if( 0 != NSP_Inherited_Pipe(fd) )
        return NPERR_GENERIC_ERROR;

    write_fd = fd[1];   // write fd
#ifdef UNIX
    int nChildPID = fork();

    if( ! nChildPID )  // child process
#endif //end of UNIX
    {
        char s_read_fd[16] = {0};
        char s_write_fd[16] = {0};
        sprintf(s_read_fd,  "%d", fd[0]);
        sprintf(s_write_fd, "%d", fd[1]);
        // transfer pipe fds to nsplugin process
        debug_fprintf(NSP_LOG_APPEND, "try to start plugin exe: %s %s %s.\n", findNsExeFile(), s_read_fd, s_write_fd);

#ifdef UNIX
        execl(findNsExeFile(), findNsExeFile(), s_read_fd, s_write_fd, NULL);
        _exit(255);
#endif //end of UNIX
#ifdef WNT
        char sArgc[NPP_BUFFER_SIZE];
        sprintf(sArgc, "%s %s", s_read_fd, s_write_fd);
        STARTUPINFO NSP_StarInfo;
        memset((void*) &NSP_StarInfo, 0, sizeof(STARTUPINFO));
        NSP_StarInfo.cb = sizeof(STARTUPINFO);
        PROCESS_INFORMATION NSP_ProcessInfo;
        memset((void*)&NSP_ProcessInfo, 0, sizeof(PROCESS_INFORMATION));
        if(!CreateProcess(findNsExeFile(), sArgc, NULL, NULL, TRUE,
                      CREATE_NO_WINDOW, NULL, NULL, &NSP_StarInfo, &NSP_ProcessInfo))
        {
            DWORD Err = GetLastError();
        }
#endif //end of WNT
    }
    NSP_Close_Pipe(fd[0]);
    return NPERR_NO_ERROR;
}

int sendMsg( PLUGIN_MSG* pMsg, unsigned int len, int iEnsure)
{
    NSP_Lock_Mute_Obj(send_lock);
    long int len_w = 0;

    debug_fprintf(NSP_LOG_APPEND, "try to send message type:%d; len: %d\n", pMsg->msg_id, len);
    /*
    debug_fprintf(NSP_LOG_APPEND, "NSPlugin Message: msg_id:%d; instance_id:%d;
        wnd_id:%d;wnd_x:%d;wnd_y:%d;wnd_w:%d;wnd_h:%d; url:%s\n",
        pMsg->msg_id, pMsg->instance_id, pMsg->wnd_id,
        pMsg->wnd_x, pMsg->wnd_y, pMsg->wnd_w, pMsg->wnd_h, pMsg->url);*/
    len_w = NSP_WriteToPipe(write_fd, (void*) pMsg, len);
    if (len_w != len){
        if(errno == EPIPE) // If pipe breaks, then init pipe again and resend the msg
        {
            if(iEnsure){
                debug_fprintf(NSP_LOG_APPEND, "send message error, plugin exec need to be restart\n");
                NSP_Close_Pipe(write_fd);
                do_init_pipe();
                len_w = NSP_WriteToPipe(write_fd, (void*) pMsg, len);
            }
        }
        else if(errno == EINTR) // If interrupted by signal, then continue to send
        {
            long  new_len;
            debug_fprintf(NSP_LOG_APPEND, "send message error, send intrrupted by singal, resend again\n");
            new_len = NSP_WriteToPipe(write_fd, (char*)pMsg+len_w, len-len_w);
            len_w = len_w + new_len;
        }
        else  // else return error
        {
            debug_fprintf(NSP_LOG_APPEND, "send message error :%s.\n", strerror(errno));
            len_w = -1;
        }
    }
    NSP_Unlock_Mute_Obj(send_lock);
    debug_fprintf(NSP_LOG_APPEND, "send message success!\n");
    return len_w;
}

char* pMimeTypes = "application/vnd.stardivision.calc:sdc:StarCalc 3.0 - 5.0;"
"application/vnd.stardivision.chart:sds:StarChart 3.0 - 5.0;"
"application/vnd.stardivision.draw:sda:StarDraw 3.0 - 5.0;"
"application/vnd.stardivision.impress:sdd:StarImpress 3.0 - 5.0;"
"application/vnd.stardivision.impress-packed:sdp:StarImpress-packed 3.0 - 5.0;"
"application/vnd.stardivision.math:smf:StarMath 3.0 - 5.0;"
"application/vnd.stardivision.writer:vor:StarWriter Template 3.0 - 5.0;"
"application/vnd.stardivision.writer-global:sgl:StarWriter Global 3.0 - 5.0;"
"application/vnd.staroffice.writer:sdw:StarWriter 3.0 - 5.0;"
"application/vnd.sun.xml.calc:sxc:StarCalc;"
"application/vnd.sun.xml.calc.template:stc:StarCalc Template;"
"application/vnd.sun.xml.draw:sxd:StarDraw;"
"application/vnd.sun.xml.draw.template:std:StarDraw Template;"
"application/vnd.sun.xml.impress:sxi:StarImpress;"
"application/vnd.sun.xml.impress.template:sti:StarImpress Template;"
"application/vnd.sun.xml.math:sxm:StarMath;"
"application/vnd.sun.xml.writer:sxw:StarWriter;"
"application/vnd.sun.xml.writer.global:sxg:StarWriter Global;"
"application/vnd.sun.xml.writer.template:stw:StarWriter Template;"
"application/msword:doc:Microsoft Word Document;"
"application/vnd.ms-excel:xls:Microsoft Excel Document;"
"application/vnd.ms-powerpoint:ppt:Microsoft Powerpoint Document;"
"application/x-vnd.oasis.openoffice.text:oot:Open Office Text;"
"application/x-vnd.oasis.openoffice-template:ott:Open Office Text Template;"
"application/x-vnd.oasis.openoffice.text-master:oom:Open Office Master Document;"
"application/x-vnd.oasis.openoffice.text-web:oth:HTML Document Template;"
"application/x-vnd.oasis.openoffice.spreadsheet:oos:Open Office Spreadsheet;"
"application/x-vnd.oasis.openoffice.spreadsheet-template:ots:Open Office Spreadsheet Template;"
"application/x-vnd.oasis.openoffice.drawing:ood:Open Office Drawing;"
"application/x-vnd.oasis.openoffice.drawing-template:otd:Open Office Drawing Template;"
"application/x-vnd.oasis.openoffice.presentation:oop:Open Office Presentation;"
"application/x-vnd.oasis.openoffice.presentation-template:otp:Open Office Presentation Template;"
"application/x-vnd.oasis.openoffice.formula:oof:Open Office Formula";

extern "C"
{
char*
NPP_GetMIMEDescription(void)
{
    debug_fprintf(NSP_LOG_APPEND, "print by Netscape Plugin,  NPP_GetMIMEDescription:%s.\n", pMimeTypes);
    return(pMimeTypes);
}

#ifdef UNIX
NPError
NPP_GetValue(void * instance, NPPVariable variable, void *value)
{
{'fraction_non_alphanumeric': 0.1547397026695796, 'fraction_numerical': 0.013267970373528, 'mean_word_length': 3.8521199586349533, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1878287', 'n_tokens_mistral': 6678, 'n_tokens_neox': 6328, 'n_words': 1581}