mirror of
https://github.com/myvesta/vesta
synced 2025-07-06 04:51:54 -07:00
myvesta php replacement for gnu 'grep' (but without regular expression)
This commit is contained in:
parent
2f37124545
commit
888db2bc72
4 changed files with 140 additions and 6 deletions
|
@ -2,7 +2,7 @@
|
|||
# info: calling myVesta PHP functions
|
||||
# options: FUNCTION
|
||||
#
|
||||
# The function is calling myVesta PHP functions.
|
||||
# The function is calling myVesta or standard PHP functions directly from bash
|
||||
|
||||
#----------------------------------------------------------#
|
||||
# Action #
|
||||
|
|
|
@ -27,13 +27,36 @@ if (!function_exists($func)) {
|
|||
if (!function_exists($func)) myvesta_throw_error(2, 'Function does not exists');
|
||||
}
|
||||
|
||||
$insert_stdin_at_position=false;
|
||||
if ($func=="myvesta_grep") $insert_stdin_at_position=1;
|
||||
|
||||
$params=array();
|
||||
|
||||
if ($myvesta_stdin!='') $params[]=$myvesta_stdin;
|
||||
$added=0;
|
||||
$stdin_content='';
|
||||
$myvesta_stdin_return_not_found=false;
|
||||
if ($myvesta_stdin!='' && $insert_stdin_at_position===false) {$params[]=$myvesta_stdin; $added++;}
|
||||
|
||||
for ($i=2; $i<$counter; $i++) {
|
||||
$argv[$i]=myvesta_fix_backslashes($argv[$i]);
|
||||
if ($insert_stdin_at_position!==false && $myvesta_stdin=='') if ($insert_stdin_at_position==$added) {$stdin_content=$argv[$i]; $added++; continue;}
|
||||
$params[]=$argv[$i];
|
||||
$added++;
|
||||
}
|
||||
if ($insert_stdin_at_position!=false) {
|
||||
if ($myvesta_stdin=='') {
|
||||
$file_or_stdin=$stdin_content;
|
||||
if (!file_exists($file_or_stdin)) {
|
||||
$myvesta_stdin_return_not_found=true;
|
||||
$myvesta_stdin='';
|
||||
} else {
|
||||
$myvesta_stdin=file_get_contents($file_or_stdin);
|
||||
}
|
||||
}
|
||||
if (isset($params[$insert_stdin_at_position])) array_splice($params, $insert_stdin_at_position, 0, array($myvesta_stdin));
|
||||
else $params[$insert_stdin_at_position]=$myvesta_stdin;
|
||||
}
|
||||
//print_r($params); exit;
|
||||
|
||||
$r=call_user_func_array($func, $params);
|
||||
if (is_bool($r)) {
|
||||
|
|
|
@ -14,9 +14,88 @@ function myvesta_echo($str) {
|
|||
echo $str;
|
||||
}
|
||||
|
||||
function myvesta_exit($code) {
|
||||
function myvesta_exit($code, $echo='') {
|
||||
global $SHLVL, $myvesta_echo_done, $myvesta_last_echo;
|
||||
// myvesta_echo ("==================== ".$argv[0].": ".$code." ====================\n");
|
||||
if ($echo!='') myvesta_echo($echo);
|
||||
if ($SHLVL<3 && $myvesta_echo_done==true) {
|
||||
$last_char=substr($myvesta_last_echo, -1, 1);
|
||||
if ($last_char!="\n") echo "\n";
|
||||
}
|
||||
exit($code);
|
||||
}
|
||||
|
||||
$myvesta_current_user=exec('whoami', $myvesta_output, $myvesta_return_var);
|
||||
if ($myvesta_current_user != 'root') {myvesta_echo ("ERROR: You must be root to execute this script"); myvesta_exit(1);}
|
||||
|
||||
function myvesta_throw_error($code, $message) {
|
||||
global $myvesta_exit_on_error;
|
||||
if ($message!=='') myvesta_echo ("ERROR: ".$message);
|
||||
if ($myvesta_exit_on_error) myvesta_exit($code);
|
||||
return $code;
|
||||
}
|
||||
|
||||
function myvesta_fix_backslashes($s) {
|
||||
$s=str_replace("\\n", "\n", $s);
|
||||
$s=str_replace("\\r", "\r", $s);
|
||||
$s=str_replace("\\t", "\t", $s);
|
||||
return $s;
|
||||
}
|
||||
|
||||
function myvesta_check_args ($requried_arguments, $arguments) {
|
||||
global $argv;
|
||||
$argument_counter=count($argv);
|
||||
$argument_counter--;
|
||||
$argv[0]=str_replace('/usr/local/vesta/bin/', '', $argv[0]);
|
||||
// myvesta_echo ( "-------------------- ".$argv[0]." --------------------\n");
|
||||
if ($argument_counter<$requried_arguments) {
|
||||
$arguments=str_replace(" ", "' '", $arguments);
|
||||
$arguments="'".$arguments."'";
|
||||
return myvesta_throw_error(MYVESTA_ERROR_MISSING_ARGUMENTS, "Usage: $command $arguments");
|
||||
}
|
||||
$argument_arr=explode(" ", $arguments);
|
||||
$i=1;
|
||||
foreach ($argument_arr as $argument) {
|
||||
$GLOBALS[$argument]=myvesta_fix_backslashes($argv[$i]);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
function myvesta_fix_args() {
|
||||
global $argv;
|
||||
$i=0;
|
||||
foreach ($argv as $argument) {
|
||||
if ($i==0) {$i++; continue;}
|
||||
$argv[$i]=myvesta_fix_backslashes($argv[$i]);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
function myvesta_test_func () {
|
||||
$args=func_get_args();
|
||||
myvesta_echo ("You said: ");
|
||||
myvesta_echo (trim(print_r ($args, true)));
|
||||
}
|
||||
<?php
|
||||
|
||||
$myvesta_exit_on_error=true;
|
||||
define('MYVESTA_ERROR_PERMISSION_DENIED', 1);
|
||||
define('MYVESTA_ERROR_MISSING_ARGUMENTS', 2);
|
||||
define('MYVESTA_ERROR_FILE_DOES_NOT_EXISTS', 3);
|
||||
define('MYVESTA_ERROR_STRING_NOT_FOUND', 4);
|
||||
define('MYVESTA_ERROR_GENERAL', 5);
|
||||
|
||||
function myvesta_echo($str) {
|
||||
global $myvesta_echo_done, $myvesta_last_echo;
|
||||
$myvesta_echo_done=true;
|
||||
$myvesta_last_echo=$str;
|
||||
echo $str;
|
||||
}
|
||||
|
||||
function myvesta_exit($code, $echo='') {
|
||||
global $SHLVL, $myvesta_echo_done, $myvesta_last_echo;
|
||||
// myvesta_echo ("==================== ".$argv[0].": ".$code." ====================\n");
|
||||
if ($echo!='') myvesta_echo($echo);
|
||||
if ($SHLVL<3 && $myvesta_echo_done==true) {
|
||||
$last_char=substr($myvesta_last_echo, -1, 1);
|
||||
if ($last_char!="\n") echo "\n";
|
||||
|
@ -71,9 +150,7 @@ function myvesta_fix_args() {
|
|||
}
|
||||
|
||||
function myvesta_test_func () {
|
||||
global $myvesta_stdin;
|
||||
$args=func_get_args();
|
||||
if ($myvesta_stdin!='') array_splice( $args, 0, 0, array($myvesta_stdin) );
|
||||
myvesta_echo ("You said: ");
|
||||
myvesta_echo (trim(print_r ($args, true)));
|
||||
}
|
||||
|
|
|
@ -72,6 +72,40 @@ function myvesta_strip_in_file_between_including_borders($file, $left, $right) {
|
|||
return true;
|
||||
}
|
||||
|
||||
// --- mixed functions ---
|
||||
|
||||
function myvesta_grep($find, $file_or_stdin, $count=0, $quiet=0) {
|
||||
global $myvesta_stdin, $myvesta_stdin_return_not_found;
|
||||
if ($count=='-c') {$count=1; $quiet=0;}
|
||||
if ($count=='-q') {$count=0; $quiet=1;}
|
||||
//echo "find = " . $find."\n"; echo "file_or_stdin = " . $file_or_stdin."\n"; echo "count = " . $count."\n"; echo "quiet = " . $quiet."\n"; exit;
|
||||
if ($myvesta_stdin_return_not_found==true) {
|
||||
if ($count==1) return myvesta_throw_error (MYVESTA_ERROR_STRING_NOT_FOUND, "0");
|
||||
return myvesta_throw_error (MYVESTA_ERROR_STRING_NOT_FOUND, "");
|
||||
}
|
||||
|
||||
$arr=explode("\n", $file_or_stdin);
|
||||
|
||||
$buf='';
|
||||
$hits=0;
|
||||
foreach ($arr as $line) {
|
||||
if (strpos($line, $find)!==false) {
|
||||
$hits++;
|
||||
if ($quiet==false && $count==false) $buf.=$line."\n";
|
||||
}
|
||||
}
|
||||
if ($count==1) {
|
||||
if ($hits==0) return myvesta_exit (MYVESTA_ERROR_STRING_NOT_FOUND, "0");
|
||||
return $hits;
|
||||
}
|
||||
if ($quiet==1) {
|
||||
if ($hits==0) return myvesta_exit (MYVESTA_ERROR_STRING_NOT_FOUND, "");
|
||||
return true;
|
||||
}
|
||||
if ($hits==0) return myvesta_exit (MYVESTA_ERROR_STRING_NOT_FOUND, "");
|
||||
return $buf;
|
||||
}
|
||||
|
||||
// --- string functions ---
|
||||
|
||||
function myvesta_str_get_between (&$text, $left_substring, $right_substring, $start=0, $return_left_substring=0, $return_right_substring=0, $left_substring_necessary=1, $right_substring_necessary=1) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue