base
This commit is contained in:
@@ -0,0 +1,904 @@
|
||||
<?
|
||||
require_once ("mysql.inc");
|
||||
// global varialbe
|
||||
$g_debug = 0;
|
||||
|
||||
// Debug Print
|
||||
function debugPrint( ) {
|
||||
global $g_debug;
|
||||
|
||||
if(!$g_debug)
|
||||
return;
|
||||
|
||||
echo "DEBUG : ";
|
||||
for( $i= 0 ; $i < func_num_args() ; ++$i ) {
|
||||
if ( is_array(func_get_arg ( $i )) )
|
||||
print_r(func_get_arg ( $i ));
|
||||
else
|
||||
echo func_get_arg ( $i );
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
//version print
|
||||
function versionPrint() {
|
||||
$product_Version = "3.0.1";
|
||||
$svn_revision = "520";
|
||||
$date = "20110418172000";
|
||||
|
||||
echo "ismcli : $product_Version.$svn_revision-$date\n";
|
||||
}
|
||||
|
||||
// usage print
|
||||
function usqgePrint($prg) {
|
||||
echo "Usage: ".$prg." [OPTION] [--] [args...]\n";
|
||||
echo " ".$prg." -i <user_id> -m <job_type> [--] [args...]\n";
|
||||
echo " ".$prg." -i <user_id> -m add_rc --rc_name=<rc_name>\n";
|
||||
echo " ".$prg." -i <user_id> -m add_vrc --rc_name=<rc_name> --vrc_name=<vrc_name> ".
|
||||
"--svc_ext_type=<vrc_ext_type>\n";
|
||||
echo " ".$prg." -i <user_id> -m add_fhs --rc_name=<rc_name> --vrc_name=<vrc_name> ".
|
||||
"--svc_ext_type=<fhs_ext_type> --fhs_name=<fhs_name> --host_name=<host_name> --private_ip=<private_ip> ".
|
||||
"--publics_ips=<1st_public_ip:2nd_public_ip> \n";
|
||||
echo " ".$prg." -i <user_id> -m add_fhs --rc_name=<rc_name> --vrc_name=<vrc_name> ".
|
||||
"--svc_ext_type=<fhs_ext_type> --fhs_list=<file> \n";
|
||||
echo " ".$prg." -i <user_id> -m del_rc --rc_name=<rc_name> \n";
|
||||
echo " ".$prg." -i <user_id> -m del_vrc --rc_name=<rc_name> --vrc_name=<vrc_name>\n";
|
||||
echo " ".$prg." -i <user_id> -m del_fhs --rc_name=<rc_name> --fhs_name=<fhs_name>\n";
|
||||
echo " -v Version number\n";
|
||||
echo " -i Specifies the user.\n";
|
||||
echo " -m Run job_type.\n";
|
||||
echo " add_rc add rc\n";
|
||||
echo " add_vrc add vrc\n";
|
||||
echo " add_fhs add fhs\n";
|
||||
echo " del_rc delete rc\n";
|
||||
echo " del_vrc delete vrc\n";
|
||||
echo " del_fhs delete fhs\n";
|
||||
echo " args... \n";
|
||||
echo " --rc_name rc name\n";
|
||||
echo " --vrc_name vrc name\n";
|
||||
echo " --fhs_name fhs name\n";
|
||||
echo " --host_name host name\n";
|
||||
echo " --svc_ext_type service extend type of fhs or vrc \n";
|
||||
echo " --private_ip private ip of fhs\n";
|
||||
echo " --publics_ips public ip of fhs\n";
|
||||
echo " --fhs_list read list of fhs from file \n";
|
||||
echo " file fromat : \n";
|
||||
echo " <fhs_name>:<host_name>:<private_ip>:<publics_1st>:<publics_2nd><CR><LF>\n";
|
||||
}
|
||||
|
||||
// parsing argument list
|
||||
function parseArgs($argv) {
|
||||
$options = $argv;
|
||||
$shortopts = "i:m:D::f:v::";
|
||||
$longopts = array(
|
||||
"rc_name:",
|
||||
"vrc_name:",
|
||||
"fhs_name:",
|
||||
"host_name:",
|
||||
"svc_ext_type:",
|
||||
"private_ip:",
|
||||
"publics_ips:",
|
||||
"fhs_list:",
|
||||
);
|
||||
|
||||
//$options = getopt($shortopts);
|
||||
$options = _getopt($shortopts, $longopts);
|
||||
|
||||
if(empty($options)) {
|
||||
usqgePrint($argv[0]);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// version print
|
||||
if(!empty($options['v'])) {
|
||||
versionPrint();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check deboug flag
|
||||
if(!empty($options['D'])) {
|
||||
global $g_debug;
|
||||
$g_debug = 1;
|
||||
}
|
||||
|
||||
// public ip
|
||||
if(!empty($options['publics_ips']))
|
||||
{
|
||||
$pubips = explode(":", $options['publics_ips']);
|
||||
$options['publics_1st'] = $pubips[0];
|
||||
$options['publics_2nd'] = "";
|
||||
|
||||
if(!empty($pubips[1]))
|
||||
$options['publics_2nd'] = $pubips[1];
|
||||
}
|
||||
|
||||
// set file read path
|
||||
if(!empty($options['fhs_list']))
|
||||
{
|
||||
$options["read_path"] = $options['fhs_list'];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
// check options
|
||||
function checkopts($options) {
|
||||
// check id
|
||||
if( chkvalempty($options, 'i') )
|
||||
return 1;
|
||||
switch($options['i']) // strtolower()
|
||||
{
|
||||
case 'svc1':
|
||||
break;
|
||||
case 'ktics':
|
||||
break;
|
||||
case 'op':
|
||||
break;
|
||||
default:
|
||||
echo "ERROR : invalid id : '".$options['i']."'\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// check -f ,-m
|
||||
if( !empty($options['f']) && !empty($options['m']) )
|
||||
{
|
||||
echo "ERROR : Options '-f' can't be used simultaneously with Options '-m'.\n";
|
||||
return 1;
|
||||
}
|
||||
else if(empty($options['f'])) // -m
|
||||
{
|
||||
//check mode : ADD_RC, ADD_VRC, ADD_FHS, DEL_RC, DEL_VCR, DEL_FHS
|
||||
// check mode
|
||||
if( chkvalempty($options, 'm') )
|
||||
return 1;
|
||||
|
||||
switch($options['m']) // strtolower()
|
||||
{
|
||||
case 'add_rc':
|
||||
// check rc_name;
|
||||
if(chkvalempty($options,'rc_name'))
|
||||
return 1;
|
||||
break;
|
||||
case 'add_vrc':
|
||||
// check rc_name;
|
||||
if(chkvalempty($options,'rc_name'))
|
||||
return 1;
|
||||
// check vrc_name;
|
||||
if(chkvalempty($options,'vrc_name'))
|
||||
return 1;
|
||||
// check __svc_ext_type
|
||||
if(chkvalempty($options,'svc_ext_type'))
|
||||
return 1;
|
||||
break;
|
||||
case 'add_fhs':
|
||||
// check rc_name;
|
||||
if(chkvalempty($options,'rc_name'))
|
||||
return 1;
|
||||
// check vrc_name;
|
||||
if(chkvalempty($options,'vrc_name'))
|
||||
return 1;
|
||||
if( empty($options["fhs_list"]) )
|
||||
{
|
||||
// check fhs_name;
|
||||
if(chkvalempty($options,'fhs_name'))
|
||||
return 1;
|
||||
// check host_name;
|
||||
if(chkvalempty($options,'host_name'))
|
||||
return 1;
|
||||
// check __svc_ext_type
|
||||
if(chkvalempty($options,'svc_ext_type'))
|
||||
return 1;
|
||||
// {Private IP}
|
||||
if(chkvalempty($options,'private_ip'))
|
||||
return 1;
|
||||
// {1st Public IP}
|
||||
// {2nd Public IP}
|
||||
if(chkvalempty($options,'publics_ips'))
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// check __svc_ext_type
|
||||
if(chkvalempty($options,'svc_ext_type'))
|
||||
return 1;
|
||||
// check value fhs_list
|
||||
if(chkvalempty($options,'fhs_list'))
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
case 'del_rc':
|
||||
// check rc_name;
|
||||
if(chkvalempty($options,'rc_name'))
|
||||
return 1;
|
||||
break;
|
||||
case 'del_vrc':
|
||||
// check rc_name;
|
||||
if(chkvalempty($options,'rc_name'))
|
||||
return 1;
|
||||
// check vrc_name;
|
||||
if(chkvalempty($options,'vrc_name'))
|
||||
return 1;
|
||||
break;
|
||||
case 'del_fhs':
|
||||
// check rc_name;
|
||||
if(chkvalempty($options,'rc_name'))
|
||||
return 1;
|
||||
|
||||
if( empty($options["fhs_list"]) )
|
||||
{
|
||||
// check fhs_name;
|
||||
if(chkvalempty($options,'fhs_name'))
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// check value fhs_list
|
||||
if(chkvalempty($options,'fhs_list'))
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
echo "ERROR : Unkown mode : '".$options['m']."'\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if(empty($options['m'])) // -f
|
||||
{
|
||||
// check value
|
||||
if( chkvalempty($options, 'f') )
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// check vlaue empty
|
||||
function chkvalempty(&$o, $name) {
|
||||
if( empty($o[$name])|| $o[$name] == " " ) {
|
||||
if(strlen($name) > 1)
|
||||
echo "ERROR : empty value '--".$name."'\n";
|
||||
else
|
||||
echo "ERROR : empty value '-".$name."'\n";
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// convert to ISM command
|
||||
function convert2ISMcommand($options) {
|
||||
$r = array();
|
||||
$command = array();
|
||||
$code = array();
|
||||
|
||||
switch($options['m']) // strtolower()
|
||||
{
|
||||
case 'add_rc':
|
||||
$code['type'] = 301;
|
||||
$code['type_name'] = "_ADD_RC";
|
||||
$command['file_path'] = "/hostservices/physical/".
|
||||
$options['rc_name']."/".
|
||||
$options['rc_name'].".cfg";
|
||||
$command['h_host_name'] = $options['rc_name'];
|
||||
$command['h_alias'] = "";
|
||||
$command['use'] = "virtual-ics-host";
|
||||
$command['check_command'] = "check-rc!\$HOSTNAME$!\$HOSTSTATEID:".
|
||||
"hg_".$options['rc_name'].":,$";
|
||||
$command['hostgroups'] = "hg_physical";
|
||||
$command['address'] = "";
|
||||
$command['notes'] = "physical";
|
||||
$command['__related_to'] = "";
|
||||
$command['__type'] = "RC";
|
||||
$command['__svc_type'] = "C000";
|
||||
$command['__svc_ext_type'] = "";
|
||||
$command['__svc_cont_seq'] = "";
|
||||
$command['__private_address1'] = "";
|
||||
$command['__public_address1'] = "";
|
||||
$command['__public_address2'] = "";
|
||||
$command['hostgroup_name1'] = "hg_".$options['rc_name'];
|
||||
$command['alias1'] = $options['rc_name']." hostgroup";
|
||||
$command['hostgroup_name2'] = "rhg_".$options['rc_name'];
|
||||
$command['alias2'] = $options['rc_name']." real hostgroup";
|
||||
$command['s_host_name'] = $options['rc_name'];
|
||||
$command['service_description'] = $options['rc_name'];
|
||||
$command['s_use'] = "rc-service";
|
||||
$command['servicegroups'] = "";
|
||||
$command['s_notes'] = "physical";
|
||||
$command['servicegroup_name'] = "";
|
||||
$command['s_alias'] = "";
|
||||
break;
|
||||
case 'add_vrc':
|
||||
$code['type'] = 304;
|
||||
$code['type_name'] = "_ADD_VRC";
|
||||
$vrc_name = $options['vrc_name'].".".$options['rc_name'];
|
||||
$command['file_path'] = "/hostservices/physical/".
|
||||
$options['rc_name']."/".
|
||||
$vrc_name."/".
|
||||
$vrc_name.".cfg";
|
||||
$command['h_host_name'] = $vrc_name;
|
||||
$command['h_alias'] = "";
|
||||
$command['use'] = "cs-vrc-host";
|
||||
$command['check_command'] = "check-vrc!\$HOSTNAME$!\$SERVICESTATEID:".
|
||||
"sg_".$vrc_name.":,$";
|
||||
$command['hostgroups'] = "hg_".$options['rc_name'];
|
||||
$command['address'] = "";
|
||||
$command['notes'] = $options['rc_name'];
|
||||
$command['__related_to'] = "vhg_".$vrc_name;
|
||||
$command['__type'] = "VRC";
|
||||
$command['__svc_type'] = "C001";
|
||||
$command['__svc_ext_type'] = $options['svc_ext_type'];
|
||||
$command['__svc_cont_seq'] = "";
|
||||
$command['__private_address1'] = "";
|
||||
$command['__public_address1'] = "";
|
||||
$command['__public_address2'] = "";
|
||||
$command['hostgroup_name1'] = "hg_".$vrc_name;
|
||||
$command['alias1'] = $vrc_name." hostgroup";
|
||||
$command['hostgroup_name2'] = "vhg_".$vrc_name;
|
||||
$command['alias2'] = $vrc_name.
|
||||
" virtual hostgroup";
|
||||
$command['s_host_name'] = $vrc_name;
|
||||
$command['service_description'] = $vrc_name;
|
||||
$command['s_use'] = "cs-vrc-service";
|
||||
$command['servicegroups'] = "";
|
||||
$command['s_notes'] = $options['rc_name'];
|
||||
$command['servicegroup_name'] = "sg_".$vrc_name;
|
||||
$command['s_alias'] = $vrc_name." service group";
|
||||
|
||||
break;
|
||||
case 'add_fhs':
|
||||
$code['type'] = 307;
|
||||
$code['type_name'] = "_ADD_FHS";
|
||||
$vrc_name = $options['vrc_name'].".".$options['rc_name'];
|
||||
$fhs_name = $options['fhs_name'].".".$options['rc_name'];
|
||||
$command['file_path'] = "/hostservices/physical/".
|
||||
$options['rc_name']."/".
|
||||
$vrc_name."/".
|
||||
$fhs_name.".cfg";
|
||||
$command['h_host_name'] = $fhs_name;
|
||||
$command['h_alias'] = $options['host_name'];
|
||||
$command['use'] = "cs-fhs-host";
|
||||
$command['check_command'] = "";
|
||||
$command['hostgroups'] = "hg_".$vrc_name.
|
||||
",rhg_".$options['rc_name'];
|
||||
$command['address'] = $options['publics_1st'];
|
||||
$command['notes'] = $vrc_name;
|
||||
$command['__related_to'] = "vhg_".$fhs_name;
|
||||
$command['__type'] = "FHS";
|
||||
$command['__svc_type'] = "C001";
|
||||
$command['__svc_ext_type'] = $options['svc_ext_type'];
|
||||
$command['__svc_cont_seq'] = "";
|
||||
$command['__private_address1'] = $options['private_ip'];
|
||||
$command['__public_address1'] = $options['publics_1st'];
|
||||
$command['__public_address2'] = $options['publics_2nd'];
|
||||
$command['hostgroup_name1'] = "vhg_".$fhs_name;
|
||||
$command['alias1'] = $fhs_name.
|
||||
" virtual hostgroup";
|
||||
$command['hostgroup_name2'] = "";
|
||||
$command['alias2'] = "";
|
||||
$command['s_host_name'] = $fhs_name;
|
||||
$command['service_description'] = $fhs_name;
|
||||
$command['s_use'] = "cs-".$options['svc_ext_type']."-fhs-service";
|
||||
$command['servicegroups'] = "sg_".$vrc_name;
|
||||
$command['s_notes'] = $vrc_name;
|
||||
$command['servicegroup_name'] = "";
|
||||
$command['s_alias'] = "";
|
||||
break;
|
||||
case 'del_rc':
|
||||
$code['type'] = 302;
|
||||
$code['type_name'] = "_DEL_RC";
|
||||
$command['file_path'] = $options['rc_name'].".cfg";
|
||||
$command['h_host_name'] = $options['rc_name'];
|
||||
break;
|
||||
case 'del_vrc':
|
||||
$code['type'] = 305;
|
||||
$code['type_name'] = "_DEL_VRC";
|
||||
$vrc_name = $options['vrc_name'].".".$options['rc_name'];
|
||||
$command['file_path'] = $vrc_name.".cfg";
|
||||
$command['h_host_name'] = $vrc_name;
|
||||
break;
|
||||
case 'del_fhs':
|
||||
$code['type'] = 308;
|
||||
$code['type_name'] = "_DEL_FHS";
|
||||
$fhs_name = $options['fhs_name'].".".$options['rc_name'];
|
||||
$command['file_path'] = $fhs_name.".cfg";
|
||||
$command['h_host_name'] = $fhs_name;
|
||||
break;
|
||||
}
|
||||
$code['user'] = $options['i'];
|
||||
|
||||
$r['code'] = $code;
|
||||
$r['command'] = $command;
|
||||
return $r;
|
||||
}
|
||||
|
||||
// read file
|
||||
function filereaddata($path) {
|
||||
$data = array();
|
||||
|
||||
// read
|
||||
$lines = @file($path);
|
||||
if(empty($lines)) {
|
||||
echo "ERROR : file read error. - ".$path."\n";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// remove comment(//,#)
|
||||
foreach ($lines as $line_num => $line) {
|
||||
$line = rtrim($line);
|
||||
$debug_str = $line_num." - ".$line;
|
||||
debugPrint("Read Data :", $debug_str);
|
||||
$p = preg_split("/[#]|[\/\/]/", $line);
|
||||
if(!empty($p[0]))
|
||||
$data[] = $p[0];
|
||||
}
|
||||
|
||||
if(empty($data)) {
|
||||
echo "ERROR : empty data. - ".$path."\n";
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
// parsing file read data
|
||||
function parsefiledata($parstype, $dataes) {
|
||||
$r = array();
|
||||
$c = 0;
|
||||
foreach ($dataes as $line_num => $line) {
|
||||
debugPrint("parsing....", $line);
|
||||
$line = $line;
|
||||
$parse = explode(":", $line);
|
||||
if(empty($parse))
|
||||
continue;
|
||||
|
||||
switch($parstype) {
|
||||
case 'add_fhs':
|
||||
$r[$c]['fhs_name'] = $parse[0];
|
||||
$r[$c]['host_name'] = $parse[1];
|
||||
// check
|
||||
if(empty($parse[2]) || empty($parse[3])) {
|
||||
unset($r[$c]);
|
||||
echo "ERROR : parseing data. - ".$line."\n";
|
||||
continue;
|
||||
}
|
||||
$r[$c]['private_ip'] = $parse[2];
|
||||
$r[$c]['publics_1st'] = $parse[3];
|
||||
if(!empty($parse[4])) {
|
||||
$r[$c]['publics_2nd'] = $parse[4];
|
||||
}
|
||||
else
|
||||
$r[$c]['publics_2nd'] ="";
|
||||
break;
|
||||
case 'del_fhs':
|
||||
$r[$c]['fhs_name'] = $parse[0];
|
||||
break;
|
||||
}
|
||||
++$c;
|
||||
}
|
||||
|
||||
if(empty($r)) {
|
||||
echo "ERROR : empty parseing data.\n";
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
// set Option
|
||||
function setOption(&$option, $data)
|
||||
{
|
||||
switch($option['m']) {
|
||||
case 'add_fhs':
|
||||
$option['fhs_name'] = $data['fhs_name'];
|
||||
$option['host_name'] = $data['host_name'];
|
||||
$option['private_ip'] = $data['private_ip'];
|
||||
$option['publics_1st'] = $data['publics_1st'];
|
||||
$option['publics_2nd'] = $data['publics_2nd'];
|
||||
break;
|
||||
case 'del_fhs':
|
||||
$option['fhs_name'] = $data['fhs_name'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// insert ISM
|
||||
function insertData($data) {
|
||||
$r = 0;
|
||||
$dbConnMySql = new dbconnect('222.122.152.140', 'nagios', 'nagiosakstp',
|
||||
'nagios', __FILE__, __LINE__);
|
||||
|
||||
// chage array to string
|
||||
$command = implode(";", $data['command'] );
|
||||
|
||||
$sql = "INSERT INTO ism_commands ".
|
||||
"( USERID, REGTIME, COMMAND_TYPE,".
|
||||
"COMMAND_NAME, COMMAND_ARGS, STATUS, ERRNO ) ".
|
||||
"VALUES ( '".$data['code']['user']."', NOW(), ".
|
||||
$data['code']['type'].", '".$data['code']['type_name']."', '".
|
||||
$command."', 0, 0)";
|
||||
debugPrint("SQL : ", $sql);
|
||||
$res = $dbConnMySql->query($sql,__FILE__, __LINE__ );
|
||||
if( !$res->result )
|
||||
{
|
||||
echo "ERROR : Fail to insert data.\n";
|
||||
$r = 1;
|
||||
}
|
||||
|
||||
$dbConnMySql->close();
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
/* Gets options from the command line argument list */
|
||||
function _getopt ( ) {
|
||||
|
||||
/* _getopt(): Ver. 1.3 2009/05/30
|
||||
My page: http://www.ntu.beautifulworldco.com/weblog/?p=526
|
||||
|
||||
Usage: _getopt ( [$flag,] $short_option [, $long_option] );
|
||||
|
||||
Note that another function split_para() is required, which can be found in the same
|
||||
page.
|
||||
|
||||
_getopt() fully simulates getopt() which is described at
|
||||
http://us.php.net/manual/en/function.getopt.php , including long options for PHP
|
||||
version under 5.3.0. (Prior to 5.3.0, long options was only available on few systems)
|
||||
|
||||
Besides legacy usage of getopt(), I also added a new option to manipulate your own
|
||||
argument lists instead of those from command lines. This new option can be a string
|
||||
or an array such as
|
||||
|
||||
$flag = "-f value_f -ab --required 9 --optional=PK --option -v test -k";
|
||||
or
|
||||
$flag = array ( "-f", "value_f", "-ab", "--required", "9", "--optional=PK", "--option" );
|
||||
|
||||
So there are four ways to work with _getopt(),
|
||||
|
||||
1. _getopt ( $short_option );
|
||||
|
||||
it's a legacy usage, same as getopt ( $short_option ).
|
||||
|
||||
2. _getopt ( $short_option, $long_option );
|
||||
|
||||
it's a legacy usage, same as getopt ( $short_option, $long_option ).
|
||||
|
||||
3. _getopt ( $flag, $short_option );
|
||||
|
||||
use your own argument lists instead of command line arguments.
|
||||
|
||||
4. _getopt ( $flag, $short_option, $long_option );
|
||||
|
||||
use your own argument lists instead of command line arguments.
|
||||
|
||||
*/
|
||||
|
||||
if ( func_num_args() == 1 ) {
|
||||
$flag = $flag_array = $GLOBALS['argv'];
|
||||
$short_option = func_get_arg ( 0 );
|
||||
$long_option = array ();
|
||||
} else if ( func_num_args() == 2 ) {
|
||||
if ( is_array ( func_get_arg ( 1 ) ) ) {
|
||||
$flag = $GLOBALS['argv'];
|
||||
$short_option = func_get_arg ( 0 );
|
||||
$long_option = func_get_arg ( 1 );
|
||||
} else {
|
||||
$flag = func_get_arg ( 0 );
|
||||
$short_option = func_get_arg ( 1 );
|
||||
$long_option = array ();
|
||||
}
|
||||
} else if ( func_num_args() == 3 ) {
|
||||
$flag = func_get_arg ( 0 );
|
||||
$short_option = func_get_arg ( 1 );
|
||||
$long_option = func_get_arg ( 2 );
|
||||
} else {
|
||||
exit ( "wrong options\n" );
|
||||
}
|
||||
|
||||
$short_option = trim ( $short_option );
|
||||
|
||||
$short_no_value = array();
|
||||
$short_required_value = array();
|
||||
$short_optional_value = array();
|
||||
$long_no_value = array();
|
||||
$long_required_value = array();
|
||||
$long_optional_value = array();
|
||||
$options = array();
|
||||
|
||||
for ( $i = 0; $i < strlen ( $short_option ); ) {
|
||||
if ( $short_option[$i] != ":" ) {
|
||||
if ( $i == strlen ( $short_option ) - 1 ) {
|
||||
$short_no_value[] = $short_option[$i];
|
||||
break;
|
||||
} else if ( $short_option[$i+1] != ":" ) {
|
||||
$short_no_value[] = $short_option[$i];
|
||||
$i++;
|
||||
continue;
|
||||
} else if ( $short_option[$i+1] == ":"
|
||||
&&( empty($short_option[$i+2]) || $short_option[$i+2] != ":" )) {
|
||||
$short_required_value[] = $short_option[$i];
|
||||
$i += 2;
|
||||
continue;
|
||||
} else if ( $short_option[$i+1] == ":" && $short_option[$i+2] == ":" ) {
|
||||
$short_optional_value[] = $short_option[$i];
|
||||
$i += 3;
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $long_option as $a ) {
|
||||
if ( substr( $a, -2 ) == "::" ) {
|
||||
$long_optional_value[] = substr( $a, 0, -2);
|
||||
continue;
|
||||
} else if ( substr( $a, -1 ) == ":" ) {
|
||||
$long_required_value[] = substr( $a, 0, -1 );
|
||||
continue;
|
||||
} else {
|
||||
$long_no_value[] = $a;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_array ( $flag ) )
|
||||
$flag_array = $flag;
|
||||
else {
|
||||
$flag = "- $flag";
|
||||
$flag_array = split_para( $flag );
|
||||
}
|
||||
|
||||
for ( $i = 0; $i < count( $flag_array ); ) {
|
||||
|
||||
if ( $i >= count ( $flag_array ) )
|
||||
break;
|
||||
|
||||
if ( ! $flag_array[$i] || $flag_array[$i] == "-" ) {
|
||||
$i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $flag_array[$i]{0} != "-" ) {
|
||||
$i++;
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
if ( substr( $flag_array[$i], 0, 2 ) == "--" ) {
|
||||
|
||||
if (strpos($flag_array[$i], '=') != false) {
|
||||
list($key, $value) = explode('=', substr($flag_array[$i], 2), 2);
|
||||
if ( in_array ( $key, $long_required_value ) || in_array ( $key, $long_optional_value ) )
|
||||
$options[$key][] = $value;
|
||||
$i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strpos($flag_array[$i], '=') == false) {
|
||||
$key = substr( $flag_array[$i], 2 );
|
||||
if ( in_array( substr( $flag_array[$i], 2 ), $long_required_value ) ) {
|
||||
if(empty($flag_array[$i+1]))
|
||||
$options[$key][] = " ";
|
||||
else
|
||||
$options[$key][] = $flag_array[$i+1];
|
||||
$i += 2;
|
||||
continue;
|
||||
} else if ( in_array( substr( $flag_array[$i], 2 ), $long_optional_value ) ) {
|
||||
if ( $flag_array[$i+1] != "" && $flag_array[$i+1]{0} != "-" ) {
|
||||
$options[$key][] = $flag_array[$i+1];
|
||||
$i += 2;
|
||||
} else {
|
||||
$options[$key][] = FALSE;
|
||||
$i ++;
|
||||
}
|
||||
continue;
|
||||
} else if ( in_array( substr( $flag_array[$i], 2 ), $long_no_value ) ) {
|
||||
$options[$key][] = FALSE;
|
||||
$i++;
|
||||
continue;
|
||||
} else {
|
||||
$i++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
} else if ( $flag_array[$i]{0} == "-" && $flag_array[$i]{1} != "-" ) {
|
||||
|
||||
for ( $j=1; $j < strlen($flag_array[$i]); $j++ ) {
|
||||
if ( in_array( $flag_array[$i]{$j}, $short_required_value ) || in_array( $flag_array[$i]{$j}, $short_optional_value )) {
|
||||
if ( $j == strlen($flag_array[$i]) - 1 ) {
|
||||
if ( in_array( $flag_array[$i]{$j}, $short_required_value ) ) {
|
||||
if(empty($flag_array[$i+1]))
|
||||
$options[$flag_array[$i]{$j}][] = " ";
|
||||
else
|
||||
$options[$flag_array[$i]{$j}][] = $flag_array[$i+1];
|
||||
$i += 2;
|
||||
} else if ( in_array( $flag_array[$i]{$j}, $short_optional_value )
|
||||
/*&& ( empty($flag_array[$i+1]) || ($flag_array[$i+1] != "" && $flag_array[$i+1]{0} != "-"))*/ ) {
|
||||
//if(empty($flag_array[$i+1]))
|
||||
// $options[$flag_array[$i]{$j}][] = $flag_array[$i];
|
||||
//else
|
||||
//$options[$flag_array[$i]{$j}][] = $flag_array[$i+1];
|
||||
$options[$flag_array[$i]{$j}][] = TRUE;
|
||||
$i += 1;
|
||||
} else {
|
||||
$options[$flag_array[$i]{$j}][] = FALSE;
|
||||
$i ++;
|
||||
}
|
||||
$plus_i = 0;
|
||||
break;
|
||||
} else {
|
||||
$options[$flag_array[$i]{$j}][] = substr ( $flag_array[$i], $j + 1 );
|
||||
$i ++;
|
||||
$plus_i = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
} else if ( in_array ( $flag_array[$i]{$j}, $short_no_value ) ) {
|
||||
|
||||
$options[$flag_array[$i]{$j}][] = FALSE;
|
||||
$plus_i = 1;
|
||||
continue;
|
||||
|
||||
} else {
|
||||
$plus_i = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$i += $plus_i;
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
$i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ( $options as $key => $value ) {
|
||||
if ( count ( $value ) == 1 ) {
|
||||
$options[ $key ] = $value[0];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $options;
|
||||
|
||||
}
|
||||
|
||||
function split_para ( $pattern ) {
|
||||
|
||||
/* split_para() version 1.0 2008/08/19
|
||||
My page: http://www.ntu.beautifulworldco.com/weblog/?p=526
|
||||
|
||||
This function is to parse parameters and split them into smaller pieces.
|
||||
preg_split() does similar thing but in our function, besides "space", we
|
||||
also take the three symbols " (double quote), '(single quote),
|
||||
and \ (backslash) into consideration because things in a pair of " or '
|
||||
should be grouped together.
|
||||
|
||||
As an example, this parameter list
|
||||
|
||||
-f "test 2" -ab --required "t\"est 1" --optional="te'st 3" --option -v 'test 4'
|
||||
|
||||
will be splited into
|
||||
|
||||
-f
|
||||
t"est 2
|
||||
-ab
|
||||
--required
|
||||
test 1
|
||||
--optional=te'st 3
|
||||
--option
|
||||
-v
|
||||
test 4
|
||||
|
||||
see the code below,
|
||||
|
||||
$pattern = "-f \"test 2\" -ab --required \"t\\\"est 1\" --optional=\"te'st 3\" --option -v 'test 4'";
|
||||
|
||||
$result = split_para( $pattern );
|
||||
|
||||
echo "ORIGINAL PATTERN: $pattern\n\n";
|
||||
|
||||
var_dump( $result );
|
||||
|
||||
*/
|
||||
|
||||
$begin=0;
|
||||
$backslash = 0;
|
||||
$quote = "";
|
||||
$quote_mark = array();
|
||||
$result = array();
|
||||
|
||||
$pattern = trim ( $pattern );
|
||||
|
||||
for ( $end = 0; $end < strlen ( $pattern ) ; ) {
|
||||
|
||||
if ( ! in_array ( $pattern{$end}, array ( " ", "\"", "'", "\\" ) ) ) {
|
||||
$backslash = 0;
|
||||
$end ++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $pattern{$end} == "\\" ) {
|
||||
$backslash++;
|
||||
$end ++;
|
||||
continue;
|
||||
} else if ( $pattern{$end} == "\"" ) {
|
||||
if ( $backslash % 2 == 1 || $quote == "'" ) {
|
||||
$backslash = 0;
|
||||
$end ++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $quote == "" ) {
|
||||
$quote_mark[] = $end - $begin;
|
||||
$quote = "\"";
|
||||
} else if ( $quote == "\"" ) {
|
||||
$quote_mark[] = $end - $begin;
|
||||
$quote = "";
|
||||
}
|
||||
|
||||
$backslash = 0;
|
||||
$end ++;
|
||||
continue;
|
||||
} else if ( $pattern{$end} == "'" ) {
|
||||
if ( $backslash % 2 == 1 || $quote == "\"" ) {
|
||||
$backslash = 0;
|
||||
$end ++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $quote == "" ) {
|
||||
$quote_mark[] = $end - $begin;
|
||||
$quote = "'";
|
||||
} else if ( $quote == "'" ) {
|
||||
$quote_mark[] = $end - $begin;
|
||||
$quote = "";
|
||||
}
|
||||
|
||||
$backslash = 0;
|
||||
$end ++;
|
||||
continue;
|
||||
} else if ( $pattern{$end} == " " ) {
|
||||
if ( $quote != "" ) {
|
||||
$backslash = 0;
|
||||
$end ++;
|
||||
continue;
|
||||
} else {
|
||||
$backslash = 0;
|
||||
$cand = substr( $pattern, $begin, $end-$begin );
|
||||
for ( $j = 0; $j < strlen ( $cand ); $j ++ ) {
|
||||
if ( in_array ( $j, $quote_mark ) )
|
||||
continue;
|
||||
|
||||
$cand1 .= $cand{$j};
|
||||
}
|
||||
if ( $cand1 ) {
|
||||
eval( "\$cand1 = \"$cand1\";" );
|
||||
$result[] = $cand1;
|
||||
}
|
||||
$quote_mark = array();
|
||||
$cand1 = "";
|
||||
$end ++;
|
||||
$begin = $end;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$cand = substr( $pattern, $begin, $end-$begin );
|
||||
for ( $j = 0; $j < strlen ( $cand ); $j ++ ) {
|
||||
if ( in_array ( $j, $quote_mark ) )
|
||||
continue;
|
||||
|
||||
$cand1 .= $cand{$j};
|
||||
}
|
||||
|
||||
eval( "\$cand1 = \"$cand1\";" );
|
||||
|
||||
if ( $cand1 )
|
||||
$result[] = $cand1;
|
||||
|
||||
return $result;
|
||||
}
|
||||
/* Gets options from the command line argument list */
|
||||
?>
|
||||
Reference in New Issue
Block a user