<commit_before>/*
anycmd.cpp - Total Commander lister plugin

Copyright (C) 2012-2013 by <NAME>

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#define _CRT_SECURE_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include <windows.h>
#include <stdlib.h>
#include <shellapi.h>
#include <malloc.h>
#include <richedit.h>
#include <commdlg.h>
#include <math.h> 
#include <algorithm> 
#include <string>

#include "anycmd.h"

HINSTANCE hinst;
HWND      listWin = 0;

std::string g_text;
std::string g_text_lo;
char        inifilename[MAX_PATH]="anycmd.ini";  // Unused in this plugin,
                                                 // may be used to save data
char detect_string[MAX_PATH];
char command_string[MAX_PATH];
char cmd[MAX_PATH];
int  streams = 3;

//---------------------------------------------------------------------------
static char*
strlcpy( char* p, char* p2, int maxlen )
{
    strncpy( p, p2, maxlen );
    p[maxlen] = 0;
    return p;
}


//---------------------------------------------------------------------------
static void
searchAndReplace( std::string& value, std::string const& search,
                  std::string const& replace ) 
{ 
    std::string::size_type  next; 

    for ( next = value.find( search );        // Try and find the first match 
        next != std::string::npos;          // next is npos if nothing was found 
        next = value.find( search, next )   // search for the next match starting after 
        // the last match that was found. 
        ) { 
            // Inside the loop. So we found a match. 
            if ( next == 0 || value[next - 1] != '\r' ) {
                value.replace( next, search.length(), replace );   // Do the replacement. 
            }
            next += replace.length();                          // Move to just after the replace 
            // This is the point were we start 
            // the next search from.  
    }
}


//---------------------------------------------------------------------------
BOOL APIENTRY
DllMain( HANDLE hModule, 
         DWORD  ul_reason_for_call, 
         LPVOID lpReserved )
{
    switch ( ul_reason_for_call )
    {
    case DLL_PROCESS_ATTACH:
        hinst = (HINSTANCE)hModule;
        break;
    case DLL_PROCESS_DETACH:
        break;
    case DLL_THREAD_ATTACH:
        break;
    case DLL_THREAD_DETACH:
        break;
    }

    return TRUE;
}


//---------------------------------------------------------------------------
void APIENTRY
ListGetDetectString( char* detectString, int maxlen )
{
    strlcpy( detectString, detect_string, maxlen );
}


//---------------------------------------------------------------------------
void APIENTRY
ListSetDefaultParams( ListDefaultParamStruct* dps )
{
    dps->PluginInterfaceVersionHi  = ANYELF_VERSION_HI;
    dps->PluginInterfaceVersionLow = ANYELF_VERSION_LOW;
    strlcpy( inifilename, dps->DefaultIniName, MAX_PATH-1 );

    GetPrivateProfileString( "AnyCmd",
                             "DetectString",
                             "EXT=TXT",
                             detect_string,
                             sizeof( detect_string ),
                             dps->DefaultIniName );
    GetPrivateProfileString( "AnyCmd",
                             "Command",
                             "sort.exe %s",
                             command_string,
                             sizeof( command_string ),
                             dps->DefaultIniName );
    streams = GetPrivateProfileInt( "AnyCmd",
                                    "Stream",
                                    ANYCMD_CATCH_STD_OUT | ANYCMD_CATCH_STD_ERR,
                                    dps->DefaultIniName );
    if ( ( streams & ( ANYCMD_CATCH_STD_OUT | ANYCMD_CATCH_STD_ERR ) ) == 0 ) {
        streams = ANYCMD_CATCH_STD_OUT | ANYCMD_CATCH_STD_ERR;
    }
    sprintf_s( cmd, "%d", streams );
    WritePrivateProfileString( "AnyCmd",
                               "DetectString",
                               detect_string,
                               dps->DefaultIniName );
    WritePrivateProfileString( "AnyCmd",
                               "Command",
                               command_string,
                               dps->DefaultIniName );
    WritePrivateProfileString( "AnyCmd",
                               "Stream",
                               cmd,
                               dps->DefaultIniName );
}


//---------------------------------------------------------------------------
HWND APIENTRY
    ListLoad( HWND parentWin, char* fileToLoad, int showFlags )
{
    RECT r;

    GetClientRect( parentWin, &r );
    // Create window invisbile, only show when data fully loaded!
    listWin = CreateWindow( "EDIT", "", WS_CHILD | ES_MULTILINE | ES_WANTRETURN |
                            ES_READONLY | WS_HSCROLL | WS_VSCROLL |
                            ES_AUTOVSCROLL | ES_NOHIDESEL,
                            r.left, r.top, r.right-r.left, r.bottom-r.top,
                            parentWin, NULL, hinst, NULL);

    if ( listWin != 0 ) {
        SendMessage( listWin, EM_SETMARGINS, EC_LEFTMARGIN, 8 );
        SendMessage( listWin, EM_SETEVENTMASK, 0, ENM_UPDATE ); //ENM_SCROLL doesn't work for thumb movements!

        ShowWindow( listWin, SW_SHOW );

        int ret = ListLoadNext( parentWin, listWin, fileToLoad, showFlags );
        if ( ret == LISTPLUGIN_ERROR ) {
            DestroyWindow( listWin );
            listWin = 0;
        }
    }

    return listWin;
}


//---------------------------------------------------------------------------
int APIENTRY
ListLoadNext( HWND parentWin, HWND listWin, char* fileToLoad, int showFlags)
{
    sprintf_s( cmd, command_string, fileToLoad );

    g_text = receive_text( cmd, streams );
    if ( g_text.empty() ) {
        return LISTPLUGIN_ERROR;
    }

    searchAndReplace( g_text, "\n", "\r\n" );
    g_text_lo.resize( g_text.length() );
    std::transform( g_text.begin(), g_text.end(), g_text_lo.begin(), ::tolower );

    HFONT font;
    if ( showFlags & lcp_ansi ) {
        font = (HFONT)GetStockObject( ANSI_FIXED_FONT );
    }
    else {
        font = (HFONT)GetStockObject( SYSTEM_FIXED_FONT );
    }
    SendMessage( listWin, WM_SETFONT, (WPARAM)font, MAKELPARAM( true, 0 ) );

    SendMessage( listWin, WM_SETTEXT, 0, (LPARAM)g_text.c_str() ); 

    PostMessage( parentWin, WM_COMMAND, MAKELONG( 0, itm_percent ), (LPARAM)listWin );

    return LISTPLUGIN_OK;
}<commit_msg>Find and substitute environment variables in Command string<commit_after>/*
anycmd.cpp - Total Commander lister plugin

Copyright (C) 2012-2013 by <NAME>

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#define _CRT_SECURE_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include <windows.h>
#include <stdlib.h>
#include <shellapi.h>
#include <malloc.h>
#include <richedit.h>
#include <commdlg.h>
#include <math.h> 
#include <algorithm> 
#include <string>

#include "anycmd.h"

HINSTANCE hinst;
HWND      listWin = 0;

std::string g_text;
std::string g_text_lo;
char        inifilename[MAX_PATH]="anycmd.ini";  // Unused in this plugin,
                                                 // may be used to save data
char detect_string[MAX_PATH];
char command_string[MAX_PATH];
char cmd[MAX_PATH];
int  streams = 3;

//---------------------------------------------------------------------------
static char*
strlcpy( char* p, char* p2, int maxlen )
{
    strncpy( p, p2, maxlen );
    p[maxlen] = 0;
    return p;
}


//---------------------------------------------------------------------------
static void
searchAndReplace( std::string& value, std::string const& search,
                  std::string const& replace ) 
{ 
    std::string::size_type  next; 

    for ( next = value.find( search );      // Try and find the first match 
        next != std::string::npos;          // next is npos if nothing was found 
        next = value.find( search, next )   // search for the next match starting after 
        // the last match that was found. 
        ) { 
            // Inside the loop. So we found a match. 
            if ( next == 0 || value[next - 1] != '\r' ) {
                value.replace( next, search.length(), replace );   // Do the replacement. 
            }
            next += replace.length();                          // Move to just after the replace 
            // This is the point were we start 
            // the next search from.  
    }
}


//---------------------------------------------------------------------------
void
find_and_substitute_env_vars( char* str, size_t size )
{
    std::string work( str );

    bool found = true;
    std::basic_string <char>::size_type start_from = 0;
    while ( found ) {
        found = false;
        // Find first occurrence of '%' sign
        std::basic_string <char>::size_type first = work.find( '%', start_from );
        if ( first != std::string::npos ) {
            // Find second occurrence of '%' sign
            std::basic_string <char>::size_type second = work.find( '%', first + 1 );
            if ( second != std::string::npos ) {
                found      = true;
                start_from = first + 1;

                std::string var = work.substr( first + 1, second - first - 1 );

                // Try to substitute the substring between two '%' signs
                char buffer[MAX_PATH];
                if ( GetEnvironmentVariable( var.c_str(), buffer, MAX_PATH ) != 0 ) {
                    buffer[MAX_PATH - 1] = 0;
                    work.replace( first, second - first + 1, buffer );
                }
            }
        }
    }
    std::strncpy( str, work.c_str(), size );
    str[size - 1] = 0;
}


//---------------------------------------------------------------------------
BOOL APIENTRY
DllMain( HANDLE hModule, 
         DWORD  ul_reason_for_call, 
         LPVOID lpReserved )
{
    switch ( ul_reason_for_call )
    {
    case DLL_PROCESS_ATTACH:
        hinst = (HINSTANCE)hModule;
        break;
    case DLL_PROCESS_DETACH:
        break;
    case DLL_THREAD_ATTACH:
        break;
    case DLL_THREAD_DETACH:
        break;
    }

    return TRUE;
}


//---------------------------------------------------------------------------
void APIENTRY
ListGetDetectString( char* detectString, int maxlen )
{
    strlcpy( detectString, detect_string, maxlen );
}


//---------------------------------------------------------------------------
void APIENTRY
ListSetDefaultParams( ListDefaultParamStruct* dps )
{
    dps->PluginInterfaceVersionHi  = ANYELF_VERSION_HI;
    dps->PluginInterfaceVersionLow = ANYELF_VERSION_LOW;
    strlcpy( inifilename, dps->DefaultIniName, MAX_PATH-1 );

    GetPrivateProfileString( "AnyCmd",
                             "DetectString",
                             "EXT=TXT",
                             detect_string,
                             sizeof( detect_string ),
                             dps->DefaultIniName );
    GetPrivateProfileString( "AnyCmd",
                             "Command",
                             "sort.exe %s",
                             command_string,
                             sizeof( command_string ),
                             dps->DefaultIniName );
    streams = GetPrivateProfileInt( "AnyCmd",
                                    "Stream",
                                    ANYCMD_CATCH_STD_OUT | ANYCMD_CATCH_STD_ERR,
                                    dps->DefaultIniName );
    if ( ( streams & ( ANYCMD_CATCH_STD_OUT | ANYCMD_CATCH_STD_ERR ) ) == 0 ) {
        streams = ANYCMD_CATCH_STD_OUT | ANYCMD_CATCH_STD_ERR;
    }

    sprintf_s( cmd, "%d", streams );
    WritePrivateProfileString( "AnyCmd",
                               "DetectString",
                               detect_string,
                               dps->DefaultIniName );
    WritePrivateProfileString( "AnyCmd",
                               "Command",
                               command_string,
                               dps->DefaultIniName );
    WritePrivateProfileString( "AnyCmd",
                               "Stream",
                               cmd,
                               dps->DefaultIniName );
    
    // Substitute environment variables within the command
    find_and_substitute_env_vars( command_string, sizeof( command_string ) );
}


//---------------------------------------------------------------------------
HWND APIENTRY
    ListLoad( HWND parentWin, char* fileToLoad, int showFlags )
{
    RECT r;

    GetClientRect( parentWin, &r );
    // Create window invisbile, only show when data fully loaded!
    listWin = CreateWindow( "EDIT", "", WS_CHILD | ES_MULTILINE | ES_WANTRETURN |
                            ES_READONLY | WS_HSCROLL | WS_VSCROLL |
                            ES_AUTOVSCROLL | ES_NOHIDESEL,
                            r.left, r.top, r.right-r.left, r.bottom-r.top,
                            parentWin, NULL, hinst, NULL);

    if ( listWin != 0 ) {
        SendMessage( listWin, EM_SETMARGINS, EC_LEFTMARGIN, 8 );
        SendMessage( listWin, EM_SETEVENTMASK, 0, ENM_UPDATE ); //ENM_SCROLL doesn't work for thumb movements!

        ShowWindow( listWin, SW_SHOW );

        int ret = ListLoadNext( parentWin, listWin, fileToLoad, showFlags );
        if ( ret == LISTPLUGIN_ERROR ) {
            DestroyWindow( listWin );
            listWin = 0;
        }
    }

    return listWin;
}


//---------------------------------------------------------------------------
int APIENTRY
ListLoadNext( HWND parentWin, HWND listWin, char* fileToLoad, int showFlags)
{
    std::string cmd( command_string );

    searchAndReplace( cmd, "%s", fileToLoad );

    g_text = receive_text( cmd.c_str(), streams );
    if ( g_text.empty() ) {
        return LISTPLUGIN_ERROR;
    }

    searchAndReplace( g_text, "\n", "\r\n" );
    g_text_lo.resize( g_text.length() );
    std::transform( g_text.begin(), g_text.end(), g_text_lo.begin(), ::tolower );

    HFONT font;
    if ( showFlags & lcp_ansi ) {
        font = (HFONT)GetStockObject( ANSI_FIXED_FONT );
    }
    else {
        font = (HFONT)GetStockObject( SYSTEM_FIXED_FONT );
    }
    SendMessage( listWin, WM_SETFONT, (WPARAM)font, MAKELPARAM( true, 0 ) );

    SendMessage( listWin, WM_SETTEXT, 0, (LPARAM)g_text.c_str() ); 

    PostMessage( parentWin, WM_COMMAND, MAKELONG( 0, itm_percent ), (LPARAM)listWin );

    return LISTPLUGIN_OK;
}
{'fraction_non_alphanumeric': 0.15524144625090996, 'fraction_numerical': 0.003821887891288522, 'mean_word_length': 1.6494696239151398, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '281193', 'n_tokens_mistral': 4707, 'n_tokens_neox': 4453, 'n_words': 1292}