/*******************************************************************************
Copyright (c) 2001-2008, 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 : perlclientuser.cc
*
* Author : Tony Smith <tony@perforce.com> or <tony@smee.org>
*
* Description : Perl bindings for the Perforce API. User interface class
* for getting Perforce results into Perl.
*
******************************************************************************/
#include <clientapi.h>
#include <spec.h>
#include <diff.h>
#include "perlheaders.h"
#include "p4result.h"
#include "p4perldebug.h"
#include "specmgr.h"
#include "p4mergedata.h"
#include "perlclientuser.h"
/*******************************************************************************
* PerlClientUser - the user interface part. Gets responses from the Perforce
* server, and converts the data to Perl format for returning to the caller.
******************************************************************************/
PerlClientUser::PerlClientUser( SpecMgr *s )
{
debug = 0;
input = 0;
specMgr = s;
}
void
PerlClientUser::Reset()
{
results.Reset();
lastSpecDef.Clear();
// Leave input alone.
}
void
PerlClientUser::Finished()
{
// Reset input coz we should be done with it now. Decrement the ref count
// so it can be reclaimed.
if ( P4PERL_DEBUG_FLOW && input )
PerlIO_stdoutf( "[PerlClientUser::Finished] Cleaning up saved input\n");
if( input )
{
sv_2mortal( input );
input = 0;
}
resolver = 0;
}
void
PerlClientUser::HandleError( Error *e )
{
if( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser:HandleError]: Received error\n" );
results.AddError( e );
}
void
PerlClientUser::OutputText( const char *data, int length )
{
if ( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::OutputText]: Received %d bytes\n",
length );
results.AddOutput( newSVpv( data, length ) );
}
void
PerlClientUser::OutputInfo( char level, const char *data )
{
if ( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::OutputInfo]: Received data\n" );
results.AddOutput( data );
}
void
PerlClientUser::OutputBinary( const char *data, int length )
{
if ( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::OutputBinary]: Received %d bytes\n",
length );
//
// Binary is just stored in a string. Since the char * version of
// P4Result::AddOutput() assumes it can strlen() to find the length,
// we'll make the String object here.
//
results.AddOutput( newSVpv( data, length) );
}
void
PerlClientUser::OutputStat( StrDict *values )
{
StrPtr * spec = values->GetVar( "specdef" );
StrPtr * data = values->GetVar( "data" );
StrPtr * sf = values->GetVar( "specFormatted" );
StrDict * dict = values;
SpecDataTable specData;
Error e;
if( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::OutputStat]: Received tagged output\n"
);
//
// Determine whether or not the data we've got contains a spec in one form
// or another. 2000.1 -> 2005.1 servers supplied the form in a data variable
// and we use the spec variable to parse the form. 2005.2 and later servers
// supply the spec ready-parsed but set the 'specFormatted' variable to tell
// the client what's going on. Either way, we need the specdef variable set
// to enable spec parsing.
//
int isspec = spec && ( sf || data );
//
// Save the spec definition for later
//
if( spec )
specMgr->AddSpecDef( cmd.Text(), spec->Text() );
if ( spec && data )
{
// 2000.1 -> 2005.1 server's handle tagged form output by supplying the
// form as text in the 'data' variable. We need to convert it to a
// P4::Spec object using the supplied spec.
if ( P4PERL_DEBUG_FORMS )
PerlIO_stdoutf( "[PerlClientUser::OutputStat]: Parsing form\n" );
// Parse up the form. Use the ParseNoValid() interface to prevent
// errors caused by the use of invalid defaults for select items in
// jobspecs.
#if P4API_VERSION >= 513538
Spec s( spec->Text(), "", &e );
#else
Spec s( spec->Text(), "" );
#endif
if( !e.Test() ) s.ParseNoValid( data->Text(), &specData, &e );
if( e.Test() )
{
HandleError( &e );
return;
}
dict = specData.Dict();
}
//
// If what we've got is a parsed form, then we'll convert it to a P4::Spec
// object. Otherwise it's a plain hash.
//
if( isspec )
{
if( P4PERL_DEBUG_FORMS )
fprintf(stderr ,"[PerlClientUser::OutputStat]: Converting to P4::Spec object\n");
results.AddOutput( specMgr->StrDictToSpec( dict, spec ) );
}
else
{
if( P4PERL_DEBUG_FORMS )
fprintf(stderr ,"[PerlClientUser::OutputStat]: Converting to hash\n");
results.AddOutput( specMgr->StrDictToHash( dict ) );
}
}
/*
* Diff support for Perl API. Since the Diff class only writes its output
* to files, we run the requested diff putting the output into a temporary
* file. Then we read the file in and add its contents line by line to the
* results.
*/
void
PerlClientUser::Diff( FileSys *f1, FileSys *f2, int doPage,
char *diffFlags, Error *e )
{
if ( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::Diff]: Comparing files\n" );
//
// Duck binary files. Much the same as ClientUser::Diff, we just
// put the output into Perl space rather than stdout.
//
if( !f1->IsTextual() || !f2->IsTextual() )
{
if ( f1->Compare( f2, e ) )
results.AddOutput( "(... files differ ...)" );
return;
}
// Time to diff the two text files. Need to ensure that the
// files are in binary mode, so we have to create new FileSys
// objects to do this.
FileSys *f1_bin = FileSys::Create( FST_BINARY );
FileSys *f2_bin = FileSys::Create( FST_BINARY );
FileSys *t = FileSys::CreateGlobalTemp( f1->GetType() );
f1_bin->Set( f1->Name() );
f2_bin->Set( f2->Name() );
{
//
// In its own block to make sure that the diff object is deleted
// before we delete the FileSys objects.
//
#ifndef OS_NEXT
::
#endif
Diff d;
d.SetInput( f1_bin, f2_bin, diffFlags, e );
if ( ! e->Test() ) d.SetOutput( t->Name(), e );
if ( ! e->Test() ) d.DiffWithFlags( diffFlags );
d.CloseOutput( e );
// OK, now we have the diff output, read it in and add it to
// the output.
if ( ! e->Test() ) t->Open( FOM_READ, e );
if ( ! e->Test() )
{
StrBuf b;
while( t->ReadLine( &b, e ) )
results.AddOutput( b.Text() );
}
}
delete t;
delete f1_bin;
delete f2_bin;
if ( e->Test() ) HandleError( e );
}
/*
* Resolve support. This sucker works by calling a method on a stored
* instance of a class - the method is called once for each resolve
* and returns the merge answer: "am", "at", "ay", etc.
*/
int
PerlClientUser::Resolve( ClientMerge *m, Error *e )
{
if ( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::Resolve]: Resolving... \n" );
//
// If no resolver has been set, abort the resolve...
//
if( !resolver )
{
warn( "No P4::Resolver object supplied. Aborting resolve" );
return CMS_QUIT;
}
//
// First detect what the merger thinks the result ought to be
//
StrBuf t;
MergeStatus autoMerge = m->AutoResolve( CMF_FORCE );
// Now convert that to a string;
switch( autoMerge )
{
case CMS_QUIT: t = "q"; break;
case CMS_SKIP: t = "s"; break;
case CMS_MERGED: t = "am"; break;
case CMS_EDIT: t = "e"; break;
case CMS_YOURS: t = "ay"; break;
case CMS_THEIRS: t = "at"; break;
}
SV * mergeData = MkMergeData( m, t );
SV * r;
StrBuf reply;
for( int loop=0 ; loop < 10 ; loop++ )
{
int n;
//
// Now call the resolver, and pass down the mergeData object
// so they can see what's going on.
//
dSP;
ENTER;
SAVETMPS;
PUSHMARK( sp );
XPUSHs( resolver );
XPUSHs( mergeData );
PUTBACK;
reply.Clear();
n = perl_call_method( "Resolve", G_SCALAR );
SPAGAIN;
if( n >= 1 )
reply = POPp;
// Cleanup
PUTBACK;
FREETMPS;
LEAVE;
if( !reply.Length() ) continue;
else if( reply == "ay" ) return CMS_YOURS;
else if( reply == "at" ) return CMS_THEIRS;
else if( reply == "am" ) return CMS_MERGED;
else if( reply == "ae" ) return CMS_EDIT;
else if( reply == "s" ) return CMS_SKIP;
else if( reply == "q" ) return CMS_QUIT;
else warn( "Invalid 'p4 resolve' response." );
}
warn( "Aborting resolve after 10 attempts" );
return CMS_QUIT;
}
/*
* Prompt the user for input
*/
void
PerlClientUser::Prompt( const StrPtr &msg, StrBuf &rsp, int noEcho, Error *e )
{
if ( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::Prompt]: Using supplied input\n" );
InputData( &rsp, e );
}
/*
* convert input from the user into a form digestible to Perforce. This
* involves either (a) converting any supplied hash to a Perforce form, or
* (b) reading whatever we were given as a string.
*/
void
PerlClientUser::InputData( StrBuf *strbuf, Error *e )
{
if ( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::InputData]: Using supplied input\n" );
if( !input )
{
warn( "Perforce asked for input, but none had been supplied!" );
return;
}
//
// Check that what we've got is a reference. It really ought to be
// because of the way SetInput is coded, but just to make sure.
//
if( ! SvROK( input ) )
{
warn( "Bad input data encountered! What did you pass to SetInput()?" );
return;
}
//
// Now de-reference it and try to figure out if we're looking at a PV,
// an HV, or an AV. If it's an array, then it may be an array of PVs or
// an array of HVs, so we shift it by one and use the first element.
//
SV *s = SvRV( input );
if( SvTYPE( s ) == SVt_PVAV )
{
if ( P4PERL_DEBUG_DATA )
PerlIO_stdoutf( "[PerlClientUser::InputData]: Using first element "
"of supplied array.\n" );
s = av_shift( (AV *) s );
if( !s )
{
warn( "Ran out of input for Perforce command!" );
return;
}
//
// If what was in the array is a reference, dereference it now.
//
if( SvROK( s ) ) s = SvRV( s );
}
if( SvTYPE( s ) == SVt_PVHV )
{
if ( P4PERL_DEBUG_DATA )
PerlIO_stdoutf( "[PerlClientUser::InputData]: Input is a hashref."
" Formatting...\n" );
StrPtr * specDef = varList->GetVar( "specdef" );
specMgr->AddSpecDef( cmd.Text(), specDef->Text() );
specMgr->SpecToString( cmd.Text(), (HV *)s, *strbuf, e );
return;
}
// Otherwise, we assume it's a string - a reasonable assumption
if ( P4PERL_DEBUG_DATA )
PerlIO_stdoutf( "[PerlClientUser::InputData]: Input is a string...\n" );
strbuf->Set( SvPV_nolen( s ) );
}
/*
* Accept input from Perl for later use. We just save what we're given here
* because we may not have the specdef available to parse it with at this time.
* To deal with Perl's horrible reference count system, we create a new
* reference here to whatever we're given. That way we'll increment the
* reference count of the object when it's given to us, and we have to
* decrement the refcount when we're done with this object. Ugly, but hey,
* that's Perl!
*/
void
PerlClientUser::SetInput( SV * i )
{
if ( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::SetInput]: Stashing input for "
"later\n" );
SV *t = i;
if( SvROK( i ) )
t = SvRV( i );
input = newRV( t );
}
SV *
PerlClientUser::MkMergeData( ClientMerge *m, StrPtr &h )
{
if ( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::MkMergeData]: Creating MergeData "
"object\n" );
P4MergeData *md = new P4MergeData( this, m, h );
SV *sv = newSViv( PTR2IV( md ) );
//
// Now create a reference to the IV, and bless it into the right class.
// This is a constructor by the back door...
//
HV * stash = gv_stashpv( "P4::MergeData", TRUE );
sv = newRV_noinc( sv );
sv_bless( sv, stash );
return sv;
}