/*******************************************************************************
Copyright (c) 1997-2007, 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 "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;
}
}
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, *data;
// If both specdef and data are set, then we need to parse the form
// and return the results. If not, then we just convert it as is.
spec = values->GetVar( "specdef" );
data = values->GetVar( "data" );
if( P4PERL_DEBUG_FLOW )
PerlIO_stdoutf( "[PerlClientUser::OutputStat]: Received tagged output\n"
);
//
// Save the spec definition for later
//
if( spec )
specMgr->AddSpecDef( cmd.Text(), spec->Text() );
if ( spec && data )
{
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.
Error e;
SpecDataTable specData;
Spec s( spec->Text(), "", &e );
if( !e.Test() )s.ParseNoValid( data->Text(), &specData, &e );
if ( e.Test() )
{
HandleError( &e );
return;
}
results.AddOutput( specMgr->StrDictToHash( specData.Dict() ) );
}
else
{
results.AddOutput( specMgr->StrDictToHash( values ) );
}
}
/*
* 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 );
}
/*
* 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 );
}