/* * px * Copyright (c) 2008 Shawn Hladky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Portions from p4u and p4dctl http://public.perforce.com:8080/guest/tony_smith/perforce/p4u/ http://public.perforce.com:8080/guest/tony_smith/perforce/p4dctl/ ------------------------------------------------------------------------------ Copyright (c) 2004-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 CONTRIBUTORS "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. ------------------------------------------------------------------------------ */ #include "pxheaders.h" #include "program.h" #include "pxclientuser.h" ErrorId px_usage = { ErrorOf( ES_CLIENT, 0, E_INFO, EV_USAGE, 0 ), "\n" "Usage:\n" "\n" " px [options] command [command-options] [command-args]\n\n" "\n" " Where options are:\n\n" " -h -? print this message\n" " -v level debug modes\n" " -V print version\n" " -x file read args from named file\n" " -X file read args from named file, process 1 at a time\n" " -F format run command with custom formatted output\n" "\n" " -c client set client name (default $P4CLIENT)\n" " -C charset set character set (default $P4CHARSET)\n" " -d dir set current directory for relative paths\n" " -H host set host name (default $P4HOST)\n" " -L language set message langauge (default $P4LANGUAGE)\n" " -p port set server port (default $P4PORT)\n" " -P password set user's password (default $P4PASSWD)\n" " -u user set user's username (default $P4USER)\n" "\n" " Try 'px help' for information on Perforce commands.\n" "\n" }; /* #define P4APIVER_STRING "2007.3" #define P4APIVER_ID 513795 #define ID_OS "NTX86" #define ID_PATCH "1234" */ #include "version.h" #define ID_REL "1.0" #define ID_NAME "PX" void Program::Version( StrBuf &b ) { b.Clear(); b << "An extended Perforce command line client.\n"; b << "Copyright 2007-2008 Shawn Hladky. All rights reserved.\n"; b << "Portions copyright 1995-2008 Perforce Software. All rights reserved.\n"; b << ID_NAME << "/" << ID_OS << "/" << ID_REL << "/" << ID_PATCH; b << " (Perforce API " << P4APIVER_STRING << "/" << P4APIVER_ID << ")\n"; } Program::Program() { //enviro = &env; } int Program::Run( int argc, char **argv ) { // // Parse the command line // Options opts; Error * e = new Error; StrPtr * s = 0; StrPtr * xfile = 0; bool tagged = false; // batchSize is the number of arguments appended to a command with '-x' int batchSize = 1024; opts.Parse( --argc, ++argv, "?C:c:d:H:hF:L:P:p:u:Vv:x:X:z:Z:", OPT_ANY, px_usage, e); AssertLog.Abort( e ); int i; for( i = 0; s = opts.GetValue( 'v', i ); i++ ) p4debug.SetLevel( s->Text() ); pxClient client; StrPtr* zArg; //from RPC trace, it appears -Z (capital) is processed before init, and -z (lower) is processed after for(int j=0; zArg = opts.GetValue('Z', j); j++) { StrRef tag("tag"); if (zArg->Contains(tag)) tagged = true; client.SetProtocolV(zArg->Text()); } // // Now override the defaults with values from the command line // where appropriate. We don't have to worry about the values in the // environment or in P4CONFIG files since the ClientApi class will // load those for us. // // // Simple options first // if( s = opts[ 'c' ] ) client.SetClient( s ); if( s = opts[ 'd' ] ) client.SetCwd( s ); if( s = opts[ 'H' ] ) client.SetHost( s ); if( s = opts[ 'L' ] ) client.SetLanguage( s ); if( s = opts[ 'P' ] ) client.SetPassword( s ); if( s = opts[ 'p' ] ) client.SetPort( s ); if( s = opts[ 'u' ] ) client.SetUser( s ); xfile = opts[ 'x' ]; if (opts[ 'X' ]) { xfile = opts[ 'X' ]; batchSize = 1; } // // Now the options that would cause us to exit without much further // ado. // if( opts[ 'h' ] || opts[ '?' ] ) { StrBuf t; e->Set( px_usage ); e->Fmt( &t ); printf( "%s", t.Text() ); exit( 0 ); } if( opts[ 'V' ] ) { StrBuf t; Version( t ); printf( "%s", t.Text() ); return 0; } Enviro env; HostEnv host; StrBuf cwd; host.GetCwd(cwd); env.Config(cwd); StrBuf formatString; if (s = opts['F']) { formatString.Set(s); tagged = true; client.SetProtocol("tag", ""); } pxClientUser ui(&formatString, &env); // // Charset needs special handling because ClientApi::SetCharset() // doesn't work properly. We need to use SetTrans() and that means // we need to lookup the value. We also check the environment value // now to make sure they're using a valid charset before we continue. // const char * charset = 0; if( s = opts[ 'C' ] ) { charset = s->Text(); } else { Enviro env; HostEnv hostEnv; StrBuf cwd; hostEnv.GetCwd( cwd ); env.Config( cwd ); charset = env.Get("P4CHARSET"); } if( charset ) { CharSetApi::CharSet cs = CharSetApi::Lookup( charset ); if( cs == (CharSetApi::CharSet) -1 ) { fprintf( stderr, "Bad Charset: %s is not a valid charset\n", charset ); return 1; } client.SetTrans( cs ); } // // If the user's forgotten to type a command, we'll run // 'p4 help' for them. // if( !argc ) { argv = new char*[2]; argv[ 0 ] = "help"; argv[ 1 ] = 0; argc = 1; } // // OK, we're about ready to go. Connect to the server. // // We need this to happen after the connection settings are set up, // but before we try to do anything that requires server communication. // client.Init( e ); AssertLog.Abort( e ); bool caseSensitive = true; if (client.GetProtocol("nocase") != NULL) caseSensitive = false; int serverLevel = 1; if (client.GetProtocol("server2") != NULL) serverLevel = client.GetProtocol("server2")->Atoi(); // find the "z" arguments, and pass them as RPC Variables for(int j=0;;j++) { zArg = opts.GetValue('z', j); if(!zArg) break; StrRef tag("tag"); if (zArg->Contains(tag)) tagged = true; client.SetVarV(zArg->Text()); } client.SetProg(ID_NAME); client.SetVersion(ID_REL); // // Run the command: // If we've got a -x flag we have to read our arguments from // a file. If not, we just go with what we've got. Get the simple // version out of the way first. // StrBuf command(argv[0]); if( ! xfile ) { client.SetArgv( argc - 1, argv + 1 ); client.Run( command.Text(), tagged, caseSensitive, serverLevel, &ui ); // Close connection client.Final( e ); AssertLog.Abort( e ); return 0; } // -x version FileSys *xf = FileSys::Create( FST_TEXT ); xf->Set( *xfile ); xf->Open( FOM_READ, e ); AssertLog.Abort( e ); StrBuf l; int eof = 0; int batchCount = 0; // -x and -X flags both batch commands. // -x batches the commands in groups of 1024 // -X runs one at a time, or batches in groups of 1 while( !e->Test() && !client.Dropped() && !eof ) { eof = !xf->ReadLine( &l, e ); if( !eof ) { // On the first run of the batch, set any flags before appending the -x arguments if( batchCount == 0 ) { client.SetArgv(argc - 1, argv + 1 ); } // add arguments for each line of imput client.SetVar( "", &l ); batchCount++; } // don't allow unlimited arguments, batch them with batchSize if( eof && batchCount || ( batchCount == batchSize ) ) { client.Run( command.Text(), tagged, caseSensitive, serverLevel, &ui ); batchCount = 0; } } xf->Close( e ); AssertLog.Abort( e ); client.Final( e ); AssertLog.Abort( e ); return 0; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 6343 | Sam Stafford |
A few fixes/modifications to ClientApi setup: 1) ClientApi.Init() moved to below global opts processing; this makes the -p option work. 2) API level override removed; this is to protect against server output changes. 3) The version string has been shortened to ID_NAME/ID_REL so it fits in "monitor -e" output. |
||
#1 | 6342 | Sam Stafford | Branching px to make a fix. | ||
//guest/shawn_hladky/px/src/program.cpp | |||||
#3 | 6332 | Shawn Hladky |
px: Updates to build script. Trying to fix -x option (still not right) Fix api download script |
||
#2 | 6311 | Shawn Hladky | px: fix -x flag when flags are added on the command line | ||
#1 | 6308 | Shawn Hladky | px: Initial submit |