myvesta php replacement for gnu 'sed' (but without regular expression)

This commit is contained in:
myvesta 2023-04-15 16:38:53 +02:00 committed by GitHub
parent b01da84b15
commit 3fbb3dad78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 11 deletions

View file

@ -29,11 +29,13 @@ if (!function_exists($func)) {
$insert_stdin_at_position=false;
if ($func=="myvesta_grep") $insert_stdin_at_position=1;
if ($func=="myvesta_sed") $insert_stdin_at_position=2;
$params=array();
$added=0;
$stdin_content='';
$myvesta_stdin_from_file='';
$myvesta_stdin_return_not_found=false;
if ($myvesta_stdin!='' && $insert_stdin_at_position===false) {$params[]=$myvesta_stdin; $added++;}
@ -51,6 +53,7 @@ if ($insert_stdin_at_position!=false) {
$myvesta_stdin='';
} else {
$myvesta_stdin=file_get_contents($file_or_stdin);
$myvesta_stdin_from_file=$file_or_stdin;
}
}
if (isset($params[$insert_stdin_at_position])) array_splice($params, $insert_stdin_at_position, 0, array($myvesta_stdin));

View file

@ -1,6 +1,7 @@
<?php
$myvesta_exit_on_error=true;
$myvesta_quiet_mode=0;
define('MYVESTA_ERROR_PERMISSION_DENIED', 1);
define('MYVESTA_ERROR_MISSING_ARGUMENTS', 2);
define('MYVESTA_ERROR_FILE_DOES_NOT_EXISTS', 3);
@ -8,19 +9,22 @@ define('MYVESTA_ERROR_STRING_NOT_FOUND', 4);
define('MYVESTA_ERROR_GENERAL', 5);
function myvesta_echo($str) {
global $myvesta_echo_done, $myvesta_last_echo;
global $myvesta_echo_done, $myvesta_last_echo, $myvesta_quiet_mode;
if ($myvesta_quiet_mode==1) return;
$myvesta_echo_done=true;
$myvesta_last_echo=$str;
echo $str;
}
function myvesta_exit($code, $echo='') {
global $SHLVL, $myvesta_echo_done, $myvesta_last_echo;
global $SHLVL, $myvesta_echo_done, $myvesta_last_echo, $myvesta_quiet_mode;
// 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";
if ($myvesta_quiet_mode!=1) {
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);
}
@ -29,8 +33,8 @@ $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);
global $myvesta_exit_on_error, $myvesta_quiet_mode;
if ($message!=='' && $myvesta_quiet_mode!=1) myvesta_echo ("ERROR: ".$message);
if ($myvesta_exit_on_error) myvesta_exit($code);
return $code;
}

View file

@ -75,9 +75,10 @@ function myvesta_strip_in_file_between_including_borders($file, $left, $right) {
// --- 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;}
global $myvesta_stdin, $myvesta_stdin_return_not_found, $myvesta_quiet_mode;
if ($count==='-c') {$count=1; $quiet=0;}
if ($count==='-q') {$count=0; $quiet=1;}
$myvesta_quiet_mode=$quiet;
//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");
@ -106,6 +107,25 @@ function myvesta_grep($find, $file_or_stdin, $count=0, $quiet=0) {
return $buf;
}
function myvesta_sed($find, $replace, $file_or_stdin) {
global $myvesta_stdin, $myvesta_stdin_return_not_found, $myvesta_stdin_from_file;
//echo "find = " . $find."\n"; echo "replace = " . $replace."\n"; echo "file_or_stdin = " . $file_or_stdin."\n"; echo "stdin_from_file = " . $myvesta_stdin_from_file."\n"; exit;
if ($myvesta_stdin_return_not_found==true) {
return myvesta_throw_error (MYVESTA_ERROR_STRING_NOT_FOUND, "File not found");
}
if (strpos($file_or_stdin, $find)===false) return myvesta_throw_error (MYVESTA_ERROR_STRING_NOT_FOUND, "String '$find' not found");
$file_or_stdin=str_replace($find, $replace, $file_or_stdin);
if ($myvesta_stdin_from_file!='') {
$r=file_put_contents($myvesta_stdin_from_file, $file_or_stdin);
if ($r===false) return false;
} else {
myvesta_echo ($file_or_stdin);
}
return true;
}
// --- 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) {