f516cb2002-04-07Johan Sundström #! /usr/bin/env pike
73662c2002-04-21Martin Nilsson // -*- pike -*- $Id: rsif,v 1.8 2002/04/21 16:23:21 nilsson Exp $
5267b71995-08-09Fredrik Hübinette (Hubbe) 
73662c2002-04-21Martin Nilsson string version = ("$Revision: 1.8 $"/" ")[1]; int verbosity = 1; // more output
82c20c2002-04-06Johan Sundström int overwrite = 1; // no backups
73662c2002-04-21Martin Nilsson constant usage = #"rsif [options] <from> <to> <files>
82c20c2002-04-06Johan Sundström  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: -b, --backups Saves the unaltered file in <filename>~ -v, --verbose Writes more junk to stderr. -q, --quiet Writes no output at all. -V, --version Writes the version number of rsif.
73662c2002-04-21Martin Nilsson  -h, --help Shows this help message.\n";
82c20c2002-04-06Johan Sundström  int main(int argc, array(string) argv) { foreach(Getopt.find_all_options(argv, ({ ({ "backup", Getopt.NO_ARG, "-b,--backup"/"," }), ({ "verbose", Getopt.NO_ARG, "-v,--verbose"/"," }), ({ "version", Getopt.NO_ARG, "-V,--version"/"," }), ({ "quiet", Getopt.NO_ARG, "-q,--quiet"/"," }), ({ "help", Getopt.NO_ARG, "-h,--help"/"," }) })), array opt) switch(opt[0]) {
73662c2002-04-21Martin Nilsson  case "backup": overwrite = 0; break; case "verbose": verbosity++; break; case "version": write(version + "\n"); return 0; case "quiet": verbosity = 0; break; case "help": write(usage); return 0;
82c20c2002-04-06Johan Sundström  } argv = Getopt.get_args(argv);
73662c2002-04-21Martin Nilsson  if(4 > (argc = sizeof(argv))) { werror(usage); return 1; }
82c20c2002-04-06Johan Sundström  string from = argv[1], to = argv[2], file; if(argc == 4 && argv[-1] == "-")
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
82c20c2002-04-06Johan Sundström  if(file = Stdio.stdin.read()) { if(has_value(file, argv[1])) { if(verbosity > 1) werror("Processing stdin.\n"); write(replace(file, argv[1], argv[2])); } else write(file); } return 0;
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
89b6882002-04-06Johan Sundström  int failures, mode;
82c20c2002-04-06Johan Sundström  if(verbosity > 1) werror("Replaced strings in these files:\n"); foreach(argv[3..], string filename)
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
82c20c2002-04-06Johan Sundström  if(file = Stdio.read_file(filename))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
82c20c2002-04-06Johan Sundström  if(has_value(file, argv[1]))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
82c20c2002-04-06Johan Sundström  if(verbosity > 0) write("%s\n", filename); file = replace(file, argv[1], argv[2]);
89b6882002-04-06Johan Sundström  mode = file_stat(filename)->mode;
82c20c2002-04-06Johan Sundström  if(overwrite || mv(filename, filename + "~"))
89b6882002-04-06Johan Sundström  failures += !Stdio.write_file(filename, file, mode);
82c20c2002-04-06Johan Sundström  else
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
82c20c2002-04-06Johan Sundström  werror("Failed to create backup file.\n"); failures++;
5267b71995-08-09Fredrik Hübinette (Hubbe)  } } } }
82c20c2002-04-06Johan Sundström  return failures;
5267b71995-08-09Fredrik Hübinette (Hubbe) }