Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
pike
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pikelang
pike
Commits
f2364bb8
Commit
f2364bb8
authored
Dec 5, 2002
by
Martin Nilsson
Browse files
Options
Downloads
Patches
Plain Diff
Added recursive processing.
Rev: lib/modules/Tools.pmod/Standalone.pmod/rsif.pike:1.2
parent
6e91d947
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/modules/Tools.pmod/Standalone.pmod/rsif.pike
+58
-31
58 additions, 31 deletions
lib/modules/Tools.pmod/Standalone.pmod/rsif.pike
with
58 additions
and
31 deletions
lib/modules/Tools.pmod/Standalone.pmod/rsif.pike
+
58
−
31
View file @
f2364bb8
#! /usr/bin/env pike
// -*- pike -*- $Id: rsif.pike,v 1.
1
2002/
09
/05 1
5:26:51 marcus
Exp $
// -*- pike -*- $Id: rsif.pike,v 1.
2
2002/
12
/05 1
9:51:03 nilsson
Exp $
string version = ("$Revision: 1.1 $"/" ")[1];
int verbosity = 1; // more output
int overwrite = 1; // no backups
string version = ("$Revision: 1.2 $"/" ")[1];
int(0..1) verbosity = 1; // more output
int(0..1) overwrite = 1; // no backups
int(0..1) recursive = 0; // not recursive
constant usage = #"rsif [options] <from> <to> <files>
rsif (\"replace string in file\") replaces all occurrences of the string
<from> with the string <to> in listed files. The name of the files that
were changed are written to stdout. Available options:
rsif (\"replace string in file\") replaces all occurrences of the
string <from> with the string <to> in listed files. The name of the
files that were changed are written to stdout. Directories may be
given instead of files, in which case all the files in that directory
will be processed. Available options:
-b, --backups Saves the unaltered file in <filename>~
-r, --recursive Processes directories recusively
-v, --verbose Writes more junk to stderr.
-q, --quiet Writes no output at all.
-V, --version Writes the version number of rsif.
-h, --help Shows this help message.\n";
-h, --help Shows this help message.
";
int main(int argc, array(string) argv)
int(0..) process_path(string path, string from, string to) {
if(Stdio.is_dir(path)) {
if( path[-1] != '/' )
path += "/";
int failures;
foreach(get_dir(path), string fn)
failures += process_path( path+fn, from, to );
return failures;
}
else
return process(path, from, to);
}
int(0..1) process(string path, string from, string to) {
string file = Stdio.read_file(path);
if(file) {
if(has_value(file, from)) {
if(verbosity > 0)
write("%s\n", path);
string file = replace(file, from, to);
int mode = file_stat(path)->mode;
if(overwrite || mv(path, path + "~"))
return !Stdio.write_file(path, file, mode);
else {
werror("Failed to create backup file.\n");
return 1;
}
}
}
}
int(0..) main(int argc, array(string) argv)
{
foreach(Getopt.find_all_options(argv, ({
({ "backup", Getopt.NO_ARG, "-b,--backup"/"," }),
({ "recurse", Getopt.NO_ARG, "-r,--recursive"/"," }),
({ "verbose", Getopt.NO_ARG, "-v,--verbose"/"," }),
({ "version", Getopt.NO_ARG, "-V,--version"/"," }),
({ "quiet", Getopt.NO_ARG, "-q,--quiet"/"," }),
...
...
@@ -30,6 +70,9 @@ int main(int argc, array(string) argv)
case "backup":
overwrite = 0;
break;
case "recurse":
recursive = 1;
break;
case "verbose":
verbosity++;
break;
...
...
@@ -50,9 +93,10 @@ int main(int argc, array(string) argv)
return 1;
}
string from = argv[1], to = argv[2]
, file
;
string from = argv[1], to = argv[2];
if(argc == 4 && argv[-1] == "-")
{
string file;
if(file = Stdio.stdin.read())
{
if(has_value(file, argv[1]))
...
...
@@ -66,28 +110,11 @@ int main(int argc, array(string) argv)
return 0;
}
int failures
, mode
;
int failures;
if(verbosity > 1)
werror("Replaced strings in these files:\n");
foreach(argv[3..], string filename)
{
if(file = Stdio.read_file(filename))
{
if(has_value(file, argv[1]))
{
if(verbosity > 0)
write("%s\n", filename);
file = replace(file, argv[1], argv[2]);
mode = file_stat(filename)->mode;
if(overwrite || mv(filename, filename + "~"))
failures += !Stdio.write_file(filename, file, mode);
else
{
werror("Failed to create backup file.\n");
failures++;
}
}
}
}
foreach(argv[3..], string path)
failures += process_path(path, argv[1], argv[2]);
return failures;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment