changes from H.G. Muller; version 4.3.13
[xboard.git] / winboard / wplugin.c
index 6213680..c6507d3 100644 (file)
-#include "wplugin.h"
-
-static char * makePluginExeName( const char * name )
-{
-    char buf[ MAX_PATH ];
-
-    strcpy( buf, "" );
-
-    strcat( buf, "plugins\\" );
-    strcat( buf, name );
-    strcat( buf, ".exe" );
-
-    return strdup( buf );
-}
-
-WbPlugin * wbpCreate( const char * name )
-{
-    char buf[MAX_PATH];
-    int result = 0;
-
-    // Create the plugin
-    WbPlugin * plugin = (WbPlugin *) malloc( sizeof(WbPlugin) );
-
-    memset( plugin, 0, sizeof(WbPlugin) );
-
-    plugin->name_ = strdup( name );
-    plugin->exe_name_ = makePluginExeName( name );
-    plugin->hPipe_ = INVALID_HANDLE_VALUE;
-    plugin->hProcess_ = INVALID_HANDLE_VALUE;
-
-    // Create the named pipe for plugin communication
-    if( result == 0 ) {
-        strcpy( buf, "\\\\.\\pipe\\" );
-        strcat( buf, name );
-
-        plugin->hPipe_ = CreateNamedPipe( buf,
-            PIPE_ACCESS_DUPLEX,
-            0, // Byte mode
-            2, // Max instances
-            4*1024,
-            4*1024,
-            1000, // Default timeout
-            NULL );
-
-        if( plugin->hPipe_ == INVALID_HANDLE_VALUE ) {
-            DWORD err = GetLastError();
-            result = -1;
-        }
-    }
-
-    // Create the plugin process
-    if( result == 0 ) {
-        STARTUPINFO si;
-        PROCESS_INFORMATION pi;
-
-        ZeroMemory( &si, sizeof(si) );
-        ZeroMemory( &pi, sizeof(pi) );
-
-        si.cb = sizeof(si);
-
-        strcpy( buf, "\"" );
-        strcat( buf, plugin->exe_name_ );
-        strcat( buf, "\"" );
-
-        strcpy( buf, "\"C:\\Program Files\\Borland\\Delphi5\\Projects\\History\\History.exe\"" );
-
-        if( CreateProcess( NULL,
-            buf,
-            NULL,
-            NULL,
-            FALSE, // Inherit handles
-            0, // Creation flags
-            NULL, // Environment
-            NULL, // Current directory
-            &si,
-            &pi ) )
-        {
-            CloseHandle( pi.hThread );
-            plugin->hProcess_ = pi.hProcess;
-        }
-        else {
-            result = -2;
-        }
-    }
-
-    // Destroy the plugin instance if something went wrong
-    if( result != 0 ) {
-        wbpDelete( plugin );
-        plugin = 0;
-    }
-
-    return plugin;
-}
-
-void wbpDelete( WbPlugin * plugin )
-{
-    if( plugin != 0 ) {
-        if( plugin->hPipe_ != INVALID_HANDLE_VALUE ) {
-            CloseHandle( plugin->hPipe_ );
-        }
-
-        if( plugin->hProcess_ != INVALID_HANDLE_VALUE ) {
-            CloseHandle( plugin->hProcess_ );
-        }
-
-        free( plugin->name_ );
-
-        free( plugin->exe_name_ );
-
-        plugin->name_ = 0;
-        plugin->exe_name_ = 0;
-        plugin->hPipe_ = INVALID_HANDLE_VALUE;
-        plugin->hProcess_ = INVALID_HANDLE_VALUE;
-
-        free( plugin );
-    }
-}
-
-int wbpSendMessage( WbPlugin * plugin, const char * msg, size_t msg_len )
-{
-    int result = -1;
-
-    if( plugin != 0 && plugin->hPipe_ != INVALID_HANDLE_VALUE ) {
-        DWORD zf = 0;
-        BOOL ok = TRUE;
-
-        while( ok && (msg_len > 0) ) {
-            DWORD cb = 0;
-
-            ok = WriteFile( plugin->hPipe_,
-                msg,
-                msg_len,
-                &cb,
-                NULL );
-
-            if( ok ) {
-                if( cb > msg_len ) break; // Should *never* happen!
-
-                msg_len -= cb;
-                msg += cb;
-            }
-
-            if( cb == 0 ) {
-                zf++;
-                if( zf >= 3 ) ok = FALSE;
-            }
-            else {
-                zf = 0;
-            }
-        }
-
-        if( ok ) {
-            result = 0;
-        }
-    }
-
-    return result;
-}
-
-int wbpListInit( WbPluginList * list )
-{
-    list->item_count_ = 0;
-
-    return 0;
-}
-
-int wbpListAdd( WbPluginList * list, WbPlugin * plugin )
-{
-    int result = -1;
-
-    if( plugin != 0 ) {
-        if( list->item_count_ < MaxWbPlugins ) {
-            list->item_[ list->item_count_ ] = plugin;
-            list->item_count_++;
-
-            result = 0;
-        }
-    }
-
-    return result;
-}
-
-WbPlugin * wbpListGet( WbPluginList * list, int index )
-{
-    WbPlugin * result = 0;
-
-    if( index >= 0 && index < list->item_count_ ) {
-        result = list->item_[ index ];
-    }
-
-    return result;
-}
-
-int wbpListGetCount( WbPluginList * list )
-{
-    return list->item_count_;
-}
-
-int wbpListDeleteAll( WbPluginList * list )
-{
-    int i;
-
-    for( i=0; i<list->item_count_; i++ ) {
-        wbpDelete( list->item_[i] );
-    }
-
-    return wbpListInit( list );
-}
-
-int wbpListBroadcastMessage( WbPluginList * list, const char * msg, size_t msg_len )
-{
-    int result = 0;
-    int i;
-
-    for( i=0; i<list->item_count_; i++ ) {
-        if( wbpSendMessage( list->item_[i], msg, msg_len ) == 0 ) {
-            result++;
-        }
-        else {
-            // Error sending message to plugin...
-        }
-    }
-
-    return result;
-}
+#include "wplugin.h"\r
+\r
+static char * makePluginExeName( const char * name )\r
+{\r
+    char buf[ MAX_PATH ];\r
+\r
+    strcpy( buf, "" );\r
+\r
+    strcat( buf, "plugins\\" );\r
+    strcat( buf, name );\r
+    strcat( buf, ".exe" );\r
+\r
+    return strdup( buf );\r
+}\r
+\r
+WbPlugin * wbpCreate( const char * name )\r
+{\r
+    char buf[MAX_PATH];\r
+    int result = 0;\r
+\r
+    // Create the plugin\r
+    WbPlugin * plugin = (WbPlugin *) malloc( sizeof(WbPlugin) );\r
+\r
+    memset( plugin, 0, sizeof(WbPlugin) );\r
+\r
+    plugin->name_ = strdup( name );\r
+    plugin->exe_name_ = makePluginExeName( name );\r
+    plugin->hPipe_ = INVALID_HANDLE_VALUE;\r
+    plugin->hProcess_ = INVALID_HANDLE_VALUE;\r
+\r
+    // Create the named pipe for plugin communication\r
+    if( result == 0 ) {\r
+        strcpy( buf, "\\\\.\\pipe\\" );\r
+        strcat( buf, name );\r
+\r
+        plugin->hPipe_ = CreateNamedPipe( buf,\r
+            PIPE_ACCESS_DUPLEX,\r
+            0, // Byte mode\r
+            2, // Max instances\r
+            4*1024,\r
+            4*1024,\r
+            1000, // Default timeout\r
+            NULL );\r
+\r
+        if( plugin->hPipe_ == INVALID_HANDLE_VALUE ) {\r
+            DWORD err = GetLastError();\r
+            result = -1;\r
+        }\r
+    }\r
+\r
+    // Create the plugin process\r
+    if( result == 0 ) {\r
+        STARTUPINFO si;\r
+        PROCESS_INFORMATION pi;\r
+\r
+        ZeroMemory( &si, sizeof(si) );\r
+        ZeroMemory( &pi, sizeof(pi) );\r
+\r
+        si.cb = sizeof(si);\r
+\r
+        strcpy( buf, "\"" );\r
+        strcat( buf, plugin->exe_name_ );\r
+        strcat( buf, "\"" );\r
+\r
+        strcpy( buf, "\"C:\\Program Files\\Borland\\Delphi5\\Projects\\History\\History.exe\"" );\r
+\r
+        if( CreateProcess( NULL,\r
+            buf,\r
+            NULL,\r
+            NULL,\r
+            FALSE, // Inherit handles\r
+            0, // Creation flags\r
+            NULL, // Environment\r
+            NULL, // Current directory\r
+            &si,\r
+            &pi ) )\r
+        {\r
+            CloseHandle( pi.hThread );\r
+            plugin->hProcess_ = pi.hProcess;\r
+        }\r
+        else {\r
+            result = -2;\r
+        }\r
+    }\r
+\r
+    // Destroy the plugin instance if something went wrong\r
+    if( result != 0 ) {\r
+        wbpDelete( plugin );\r
+        plugin = 0;\r
+    }\r
+\r
+    return plugin;\r
+}\r
+\r
+void wbpDelete( WbPlugin * plugin )\r
+{\r
+    if( plugin != 0 ) {\r
+        if( plugin->hPipe_ != INVALID_HANDLE_VALUE ) {\r
+            CloseHandle( plugin->hPipe_ );\r
+        }\r
+\r
+        if( plugin->hProcess_ != INVALID_HANDLE_VALUE ) {\r
+            CloseHandle( plugin->hProcess_ );\r
+        }\r
+\r
+        free( plugin->name_ );\r
+\r
+        free( plugin->exe_name_ );\r
+\r
+        plugin->name_ = 0;\r
+        plugin->exe_name_ = 0;\r
+        plugin->hPipe_ = INVALID_HANDLE_VALUE;\r
+        plugin->hProcess_ = INVALID_HANDLE_VALUE;\r
+\r
+        free( plugin );\r
+    }\r
+}\r
+\r
+int wbpSendMessage( WbPlugin * plugin, const char * msg, size_t msg_len )\r
+{\r
+    int result = -1;\r
+\r
+    if( plugin != 0 && plugin->hPipe_ != INVALID_HANDLE_VALUE ) {\r
+        DWORD zf = 0;\r
+        BOOL ok = TRUE;\r
+\r
+        while( ok && (msg_len > 0) ) {\r
+            DWORD cb = 0;\r
+\r
+            ok = WriteFile( plugin->hPipe_,\r
+                msg,\r
+                msg_len,\r
+                &cb,\r
+                NULL );\r
+\r
+            if( ok ) {\r
+                if( cb > msg_len ) break; // Should *never* happen!\r
+\r
+                msg_len -= cb;\r
+                msg += cb;\r
+            }\r
+\r
+            if( cb == 0 ) {\r
+                zf++;\r
+                if( zf >= 3 ) ok = FALSE;\r
+            }\r
+            else {\r
+                zf = 0;\r
+            }\r
+        }\r
+\r
+        if( ok ) {\r
+            result = 0;\r
+        }\r
+    }\r
+\r
+    return result;\r
+}\r
+\r
+int wbpListInit( WbPluginList * list )\r
+{\r
+    list->item_count_ = 0;\r
+\r
+    return 0;\r
+}\r
+\r
+int wbpListAdd( WbPluginList * list, WbPlugin * plugin )\r
+{\r
+    int result = -1;\r
+\r
+    if( plugin != 0 ) {\r
+        if( list->item_count_ < MaxWbPlugins ) {\r
+            list->item_[ list->item_count_ ] = plugin;\r
+            list->item_count_++;\r
+\r
+            result = 0;\r
+        }\r
+    }\r
+\r
+    return result;\r
+}\r
+\r
+WbPlugin * wbpListGet( WbPluginList * list, int index )\r
+{\r
+    WbPlugin * result = 0;\r
+\r
+    if( index >= 0 && index < list->item_count_ ) {\r
+        result = list->item_[ index ];\r
+    }\r
+\r
+    return result;\r
+}\r
+\r
+int wbpListGetCount( WbPluginList * list )\r
+{\r
+    return list->item_count_;\r
+}\r
+\r
+int wbpListDeleteAll( WbPluginList * list )\r
+{\r
+    int i;\r
+\r
+    for( i=0; i<list->item_count_; i++ ) {\r
+        wbpDelete( list->item_[i] );\r
+    }\r
+\r
+    return wbpListInit( list );\r
+}\r
+\r
+int wbpListBroadcastMessage( WbPluginList * list, const char * msg, size_t msg_len )\r
+{\r
+    int result = 0;\r
+    int i;\r
+\r
+    for( i=0; i<list->item_count_; i++ ) {\r
+        if( wbpSendMessage( list->item_[i], msg, msg_len ) == 0 ) {\r
+            result++;\r
+        }\r
+        else {\r
+            // Error sending message to plugin...\r
+        }\r
+    }\r
+\r
+    return result;\r
+}\r