forked from Hubbitus/shell.scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace_in_file.php
executable file
·185 lines (158 loc) · 5.57 KB
/
replace_in_file.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env php
<?
/**
* HuRegRep. Utility to make regular replacemnts in files.
*
* @version 1.2.1
* @package HuRegRep
* @author Pahan-Hubbitus (Pavel Alexeev) <Pahan [at] Hubbitus [ dot. ] info>
* @copyright Copyright (c) 2010, Pahan-Hubbitus (Pavel Alexeev)
* @license GPLv2+
*
* @changelog
* * 2009-02-26 16:34 ver 1.0 to 1.0.1
* - Check fo --to (-t) parameter replaced from REQUIRED_VAR to more strict REQUIRED_NOT_NULL. So, empty values is allowed!
*
* * 2010-09-28 22:46 ver 1.0.1 to 1.1
* - Add -c (--comment) option support.
*
* * 2010-10-11 19:47 ver 1.1 to 1.2
* - Replace sha-bang from path to php to call via env.
* - define VERSION and use it in usage().
*
* * 2010-10-17 13:19 ver 1.2 to 1.2.1
* - Add ini_sets 'pcre.backtrack_limit' and 'memory_limit' to allow proceed big files.
**/
$VERSION = '0.2';
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . dirname(__FILE__));
# http://www.php.net/manual/en/pcre.configuration.php
ini_set('pcre.backtrack_limit', 1000000000);
ini_set('memory_limit', '512M');
include_once('autoload.php');
include_once('macroses/REQUIRED_VAR.php');
include_once('macroses/REQUIRED_NOT_NULL.php');
include_once('macroses/EMPTY_VAR.php');
/**
* Prints usage description.
*
* @return nothing
**/
function usage(){
echo <<<HEREDOC
HuRegRep version {$GLOBALS['VERSION']}
Usage:
{$GLOBALS['argv'][0]} -w '/what/' -t 'text to' [file1 file2...]
If files not present, STDIN used.
Options:
-a
--after
Insert argument to AFTER this one. Short form, equals to combination: "--what '/pattern/' --to '$0 add text'"
-w
--what
What replace. PCRE regular expression. See -p to change to plain text.
-t
--to
Text to replace. Backreference $0, $1, $2... allowed (while --plain not used).
So, as no --plain used, and you may use backreference like $1, $2 etc, you shoud escape other appearenc of sign "$" in text --to.
-p
--plain
Use What replace as plain text instead of regular expression.
-l
--line-after
Use cooperated with --after. If present - patern --after match to line, and inserting --to after end of line, not in.
-i
--in-place
Opt. Replace input file by result. Be careful if results are wrong you may waste all data!!!
-e
--escape
Opt. Enable interpretation of backslash escapes (\\n, \\r, \\t etc...).
-c
--comment
Opt. Just for comments instruction because shell does not accept it inside one comment. Just ignored.
Author: Pavel Alexeev aka Pahan-Hubbitus.
On all suggestions, questions, feature requests, BUG-reports - welcome on http://ru.bir.ru/ ( http://ru.bir.ru/viewtopic.php?f=25&t=652 )
HEREDOC;
}#f usage
/** Start options setup and parsing **/
if (1 == $argc) exit(usage());
$opts = new HuGetopt( # Array of recognized options
array(
array('a', 'after', ':'),
array('l', 'line-after'),
array('w', 'what', ':'),
array('t', 'to', ':'),
array('i', 'in-place'),
array('p', 'plain'),
array('e', 'escape'),
array('c', 'comment', ':'),
)
);
$opts->readPHPArgv()->parseArgs();
$nonopts = $opts->getNonOpts(1);
try{
REQUIRED_VAR(EMPTY_VAR($opts->get('what')->Val->{0}, $opts->get('after')->Val->{0}), '--what or --after');
REQUIRED_NOT_NULL($opts->get('to')->Val->{0}, '--to');
}
catch(VariableRequiredException $vre){
exit('Missed mandatory parameter: '.$vre->varName()."\n");
}
if (!isset($nonopts->{0})) $nonopts->{0} = 'php://stdin';
if($opts->get('i')->Val->_last_){
if ($opts->getNonOpts()->length() > 2){//0 - self name
echo 'Warning! Mix --in-place option with multiple in-files. Use first - "'.$opts->getNonOpts()->{1}.'" to output.'."\n";
echo 'Wait 3 second to chance of hit Ctrl+C'."\n";
sleep(3);
}
}
else $outFileName = 'php://stdout';
if ($opts->get('after')->Val->{0}){
if ($opts->get('what')->Val->{0}) throw new VariableRangeException('You can not use mix --what and --after option at once!');
$what = $opts->get('after')->Val;
$prefixTo = '$0';
}
else{
if ($opts->get('after')->Val->{0}) throw new VariableRangeException('You can not use mix --what and --after option at once!');
$what = $opts->get('what')->Val;
$prefixTo = '';
}
#For string-eval esape characters like \n, \t, \r and other use ->walk
if ($opts->get('escape')->Val->_last_){
if ($opts->get('plain')->Val->{0}){//If Palin - add slaches before "$" too.
$to = $opts->get('to')->Val->walk(create_function('&$v', 'eval(\'$v = "'.$prefixTo.'\' . addcslashes($v, \'"$\') . \'";\');'));
}
else{
$to = $opts->get('to')->Val->walk(create_function('&$v', 'eval(\'$v = "'.$prefixTo.'\' . addcslashes($v, \'"\') . \'";\');'));
}
}
elseif($prefixTo){
$to = $opts->get('to')->Val->walk(create_function('&$v', '$v = \''.$prefixTo.'\' . $v;'));
}
else{//SpeedUp only. No needed loop if $prefixTo empty!
$to = $opts->get('to')->Val;
}
if ($opts->get('plain')->Val->{0}) $what->walk(create_function('&$v', '$v = \'/\'.RegExp_pcre::quote($v, \'/\').\'/\';'));
if ($opts->get('line-after')->Val->_last_){
$newWhat = array();
foreach ($what->getArray() as $k => $v){
$re = new RegExp_pcre($v);
//[\r\n] instead & because $ not included in pattern in any case!
$newWhat[$k] = $re->getRegExpDelimiterStart().$re->getRegExpBody().'.*[\r\n]'.$re->getRegExpDelimiterEnd().$re->getRegExpModifiers();
}
$what = new HuArray($newWhat);
}
/**
* Main loop of processing
**/
foreach ($nonopts->getArray() as $file){
if ($opts->get('i')->Val->_last_) $outFileName = $file;
$fileCont = new file_inmem($file);
$fileCont->loadContent();
$re = new RegExp_pcre(
$what->getArray(),
$fileCont->getBLOB(),
$to->getArray()
);
$outFile = new file_inmem;
$outFile->setPath($outFileName)->setContentFromString($re->replace())->writeContent();
}
?>