/******************************************************************************* Copyright (c) 1997-2004, Perforce Software, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTR IBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ /******************************************************************************* * Name : php_clientapi.cc * * Author : Tony Smith <tony@perforce.com> or <tony@smee.org> * * Description : Php bindings for the Perforce API. Main interface to the * Perforce API. * ******************************************************************************/ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" BEGIN_EXTERN_C() #include "ext/standard/info.h" END_EXTERN_C() #ifdef ZEND_ENGINE_2 #include "zend_object_handlers.h" #endif #include <clientapi.h> #include <spec.h> #include <diff.h> #include <strtable.h> #include "p4result.h" #include "php_clientuser.h" #include "php_clientapi.h" /******************************************************************************* * Local functions ******************************************************************************/ static zval * MkBool( int b ) { zval *z; MAKE_STD_ZVAL( z ); if( b ) { ZVAL_TRUE( z ); } else { ZVAL_FALSE( z ); } return z; } /******************************************************************************* * Our Php classes. ******************************************************************************/ #define M_TAGGED 0x01 #define M_PARSE_FORMS 0x02 #define IS_TAGGED(x) (x & M_TAGGED ) #define IS_PARSE_FORMS(x) (x & M_PARSE_FORMS ) PhpClientApi::PhpClientApi() { initCount = 0; debug = 0; server2 = 0; mode = 0; } PhpClientApi::~PhpClientApi() { if ( initCount ) { Error e; client.Final( &e ); // Ignore errors } } void PhpClientApi::SetProg( const char *p ) { #if P4APIVER_ID >= 513026 // ClientApi::SetProg() was introduced in 2004.2 client.SetProg( p ); #endif } void PhpClientApi::SetDebug( int d ) { debug = d; ui.SetDebug( d ? d - 1 : 0 ); } // // connect to the Perforce server. // int PhpClientApi::Connect() { if ( debug ) fprintf( stderr, "[P4] Connecting to Perforce\n" ); if ( initCount ) { php_error(E_NOTICE, "P4.connect - Perforce client already connected!" ); return 1; } Error e; specDict.Clear(); // Don't cache specs across connections client.Init( &e ); if ( e.Test() ) { ReportError( &e ); return 0; } initCount++; return 1; } // // Disconnect session // int PhpClientApi::Disconnect() { if ( debug ) fprintf( stderr, "[P4] Disconnect\n" ); if ( ! initCount ) { php_error( E_NOTICE, "P4#disconnect - not connected" ); return 1; } Error e; client.Final( &e ); initCount--; return !e.Test(); } // // Test whether or not the connection has been dropped // int PhpClientApi::Dropped() { return client.Dropped(); } void PhpClientApi::Tagged() { client.SetProtocol( "tag", "" ); mode |= M_TAGGED; } void PhpClientApi::ParseForms() { client.SetProtocol( "tag", "" ); client.SetProtocol( "specstring", "" ); mode |= M_PARSE_FORMS + M_TAGGED ; } // // Run returns the results of the command as an array. Errors are reported // through the Errors() interface. // zval * PhpClientApi::Run( const char *cmd, int argc, char * const *argv ) { // Save the entire command string for our error messages. Makes it // easy to see where a script has gone wrong. StrBuf cmdString; cmdString << "\"p4 " << cmd; for( int i = 0; i < argc; i++ ) cmdString << " " << argv[ i ]; cmdString << "\""; // Clear out any results from the previous command ui.Reset(); if ( debug ) fprintf( stderr, "[P4] Executing %s\n", cmdString.Text() ); if ( !initCount ) { ui.AddError( "P4.run: not connected." ); return GetOutput(); } RunCmd( cmd, &ui, argc, argv ); if( ui.LastSpecDef().Length() ) specDict.SetVar( cmd, ui.LastSpecDef() ); return GetOutput(); } // // RunCmd is a private function to work around an obscure protocol // bug in 2000.[12] servers. Running a "p4 -Ztag client -o" messes up the // protocol so if they're running this command then we disconnect and // reconnect to refresh it. For efficiency, we only do this if the // server2 protocol is either 9 or 10 as other versions aren't affected. // void PhpClientApi::RunCmd( const char *cmd, ClientUser *ui, int argc, char * const *argv ) { client.SetArgv( argc, argv ); client.Run( cmd, ui ); // Have to request server2 protocol *after* a command has been run. I // don't know why, but that's the way it is. if ( ! server2 ) { StrPtr *pv = client.GetProtocol( "server2" ); if ( pv ) server2 = pv->Atoi(); } if ( IS_TAGGED(mode) && StrRef( cmd ) == "client" && server2 >= 9 && server2 <= 10 ) { if ( argc && ( StrRef( argv[ 0 ] ) == "-o" ) ) { if ( debug ) printf( "Resetting client to avoid 2000.[12] protocol bug\n" ); Error e; client.Final( &e ); client.Init( &e ); // Pass any errors down to the UI, so they'll get picked up. if ( e.Test() ) ui->HandleError( &e ); } } } // // Parses a string supplied by the user into a hash. To do this we need // the specstring from the server. We try to cache those as we see them, // but the user may not have executed any commands to allow us to cache // them so we may have to fetch the spec first. // zval * PhpClientApi::ParseSpec( const char * type, const char *form ) { StrPtr *specDef = FetchSpecDef( type ); if ( !specDef ) { StrBuf m; m = "No spec definition for "; m.Append( type ); m.Append( " objects." ); ui.AddError( m.Text() ); return MkBool( 0 ); } // Got a specdef so now we can attempt to parse it. SpecDataTable specData; Spec s( specDef->Text(), "" ); Error e; s.ParseNoValid( form, &specData, &e ); if ( e.Test() ) { ui.HandleError( &e ); return MkBool( 0 ); } // Now we've parsed it, convert it into a hash. We do that via // direct access to the method in PhpClientUser - this is ugly, but // expedient. return ui.DictToHash( specData.Dict(), specDef ); } // // Supply the input for the next command // void PhpClientApi::SetInput( zval *input ) { if ( debug ) fprintf( stderr, "[P4] Received input for next command\n" ); ui.SetInput( input ); } // // Fetch a spec definition from the cache - faulting it if it's not there. // StrPtr * PhpClientApi::FetchSpecDef( const char *type ) { StrPtr *sd = specDict.GetVar( type ); if( sd ) return sd; // Fault. Now we have to do something nasty. If we're in parse_forms mode, // we can run a "p4 XXXX -o" and discard the result - the specdef should // now be in the cache. If we're not in parse_forms mode, we're hosed. // There's just no other way to get the specdef. if( ! IS_PARSE_FORMS(mode) ) { ReportError( "P4.parse_spec: Use parse_forms mode to parse specs." ); return NULL; } char * const argv[] = { "-o" }; Run( type, 1, argv ); sd = specDict.GetVar( type ); if( sd ) return sd; // OK, now we're hosed. ReportError( "P4.parse_spec: Error getting spec definition from server!"); return NULL; } // // Report a Perforce error using php_error() // void PhpClientApi::ReportError( Error *e ) { StrBuf b; e->Fmt( &b ); php_error( E_NOTICE, "Perforce Error: %s\n", b.Text() ); } // // Report an error using php_error() // void PhpClientApi::ReportError( const char *msg ) { php_error( E_NOTICE, "Error: %s\n", msg ); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 4627 | Tony Smith |
Rework Jon Parise's PHP interface to work like P4Perl and P4Ruby, mostly. It's pretty close, but lacking method autoloading so the only way to run commands is through the Run() method. This should be considered early beta quality at the moment, Jon will be pulling the bits he likes back into his part of the depot after he's reviewed it. |
||
//guest/tony_smith/perforce/API/Ruby/main/p4clientapi.cpp | |||||
#5 | 4589 | Tony Smith |
Update P4Ruby to support the new SetProg() method in the 2004.2 API. Whilst the new 'P4#prog=' method is always available, it's only functional if P4Ruby is built with a 2004.2 or later API. The build system got a bit of tidying up to support this change and the API version is now detected automatically if possible. I've also removed the --apilibdir and --apiincludedir flags as they complicate matters and I never use them and I don't believe anyone else does either. There are also some minor doc formatting tweaks to go along with the added documentation for prog=. |
||
#4 | 4261 | Tony Smith |
Add support for parsing arbitrary specs from strings in Ruby space. Useful with spec depots. You might obtain the spec by running a "p4 print -q" against a file in a spec depot, but want to parse it into a Ruby hash. i.e. p4 = P4.new p4.parse_forms # Required! p4.connect buf = p4.run_print( "-q", "//specs/client/myclient" ) spec = p4.parse_client( buf ) # Or equivalently spec = p4.parse_spec( "client", buf ) |
||
#3 | 4157 | Tony Smith |
Copyright notice update. No functional change |
||
#2 | 1869 | Tony Smith |
Build in workaround for 2000.[12] protocol bug with "p4 client -o" in tagged mode so script writers don't have to think about it |
||
#1 | 1750 | Tony Smith | Build environment tweaks. | ||
//guest/tony_smith/perforce/API/Ruby/main/p4clientapi.cc | |||||
#10 | 1426 | Tony Smith |
Cleaned up the debug output a little. Introduced some debug levels so you can decide (roughly) what output you want to see. Level 1 shows command execution, connect and disconnect. Level 2 includes Level 1 and also shows the RPC callbacks as they happen. Level 3 includes 1 and 2 and also shows when Ruby garbage collection takes place. Converted all the simple methods of the form P4#meth( arg ) to aliases for P4#meth=. Added P4#debug= to complete the scheme. The P4#meth( arg ) forms are now deprecated. i.e. you should use: p4.user = "tony" and not: p4.user( "tony" ) It's just more Ruby-like. |
||
#9 | 1391 | Tony Smith |
Bug fix. Garbage collection can apparently run at any time (i.e. when you're in C space and not just when you're in Ruby space) and it was occasionally running in between adjacent "delete" and "new" statements when the result set was being reset. This change removes this race condition by making the result member of ClientUserRuby a permanently instantiated variable and extending the P4Result class so that it can reset itself in a way that GC respects. Now the only dynamically allocated C++ object is the top level P4ClientApi object. No functional change. |
||
#8 | 1165 | Tony Smith |
Minor reshuffle. Added the ability to disable exceptions completely if you don't like them or to have them raised only for errors (and not for warnings). Removed P4#warnings interface and replaced it with P4#exception_level. Some minor doc tweaks to go with the above change |
||
#7 | 1164 | Tony Smith |
Reworked exception handling (hopefully for the last time) in P4/Ruby. Now exceptions are raised on completion of Perforce commands if any errors or warnings were received as part of executing the command. This change also adds documentation, and indexes the Ruby interface off my main page. Bad form to combine so many changes in one changelist, but it's getting late and I want to get them submitted! |
||
#6 | 1083 | Tony Smith |
Sweeping change to exception handling and garbage collection. Exceptions are no longer raised for errors encoutered during execution of Perforce commands as that was causing processing to abort at the first error when several success messages may have been close behind. Now exceptions are raised for events which are fatal to the execution of commands - such as failure to connect to the Perforce server for example. For other errors, the user must call "p4.errors? " to determine whether or not errors occured and "p4.errors" to get an array of error messages. You can of course then raise exceptions yourself if you want to: begin client = p4.fetch_client if p4.errors? raise P4Exception, "p4 client -o failed" end rescue P4Exception => m puts( m ) p4.errors.each { |e| puts( e ) } end version.h got renamed because it conflicts with ruby's own version.h file. We may need to look in there at some point for ruby's version so I'm getting it out of the way now. Added gc_hack.h to make sure that GC works properly on all platforms now so Ruby shouldn't nuke any objects we're holding now. |
||
#5 | 1081 | Tony Smith |
Debugging and identification support. Adds two new methods: P4#identify() P4#debug( int ) |
||
#4 | 1029 | Tony Smith |
Fix. Was crashing if you attempted to run a command before connecting. Now correctly raises an exception. |
||
#3 | 1028 | Tony Smith |
Earlier garbage collection fix doesn't work on NT. Reverting it for now until I figure out how to do it properly. |
||
#2 | 1026 | Tony Smith |
Garbage collection update. Removed stale debug statement and added what I think is correct support for garbage collection. Now any Ruby vars supplied as input to a "p4 xxx -i" command will not be swept while we're using them. gc_mark() - Ruby's API function for the mark and sweep garbage collector has a duff prototype in Ruby 1.6.4 and 1.6.5 so I've had to add that here to keep GCC statisfied. Hopefully it can be removed later on. |
||
#1 | 1015 | Tony Smith |
First cut of Perforce bindings for the Ruby scripting language. Similar functionality to the Perl API stuff, but "rubyfied". Supports error reporting via exceptions, and presents tagged output and parsed forms as hash structures, with nested arrays where required. Still early days so the docs are thin on the ground. See the example.pl for a brief guide. Built with Ruby 1.6.4 on Linux. May still be some memory management issues as the Ruby Garbage Collection API has changed a little since the docs I've got and I've just dodged garbage collection for now. Not indexing this just yet. |