+++ /dev/null
-/*\r
- * wplugin.c\r
- *\r
- * Copyright 2009 Free Software Foundation, Inc.\r
- * ------------------------------------------------------------------------\r
- *\r
- * GNU XBoard is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or (at\r
- * your option) any later version.\r
- *\r
- * GNU XBoard is distributed in the hope that it will be useful, but\r
- * WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
- * General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see http://www.gnu.org/licenses/. *\r
- *\r
- *------------------------------------------------------------------------\r
- ** See the file ChangeLog for a revision history. */\r
-\r
-#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