fixinit.pl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Getopt::Long;
  4. my $file_list;
  5. my $dry_run = 0;
  6. GetOptions("list-file|l=s" => \$file_list,
  7. "dry-run|d" => \$dry_run)
  8. or die("Error in command line arguments\n");
  9. my @files = @ARGV;
  10. if (defined($file_list)) {
  11. die("Unable to open file: " . $file_list) if (!open(FLIST, $file_list));
  12. while (<FLIST>) {
  13. chomp;
  14. push(@files, $_);
  15. }
  16. close(FLIST);
  17. }
  18. my $matched = 0;
  19. foreach my $ifile (@files) {
  20. my $ofile = $ifile . "._tmp_";
  21. my $result = process_file($ifile, $ofile);
  22. if ($result > 0) {
  23. print "File match: " . $ifile . "\n";
  24. $matched++;
  25. rename($ofile, $ifile) if (!$dry_run);
  26. } else {
  27. print "File mismatch: " . $ifile . "\n";
  28. }
  29. }
  30. print "Matched " . $matched . " file(s) out of " . scalar(@files) . "\n";
  31. exit(0);
  32. sub process_file {
  33. my ($ifile, $ofile) = @_;
  34. if (!open(ISF, $ifile)) {
  35. print STDERR "Unable to open file: " . $ifile . "\n";
  36. return -1;
  37. }
  38. my @lines = <ISF>;
  39. close(ISF);
  40. my $num_lines = scalar(@lines);
  41. while ($num_lines > 0) {
  42. last if ($lines[$num_lines - 1] !~ /^\s*[\r\n]*$/);
  43. $num_lines--;
  44. }
  45. my $matched = 0;
  46. my $indev = -1;
  47. my @labels = (
  48. '.dc',
  49. '.name',
  50. '.reset',
  51. '.init',
  52. '.shutdown',
  53. '.attach',
  54. '.walk',
  55. '.stat',
  56. '.open',
  57. '.create',
  58. '.close',
  59. '.read',
  60. '.bread',
  61. '.write',
  62. '.bwrite',
  63. '.remove',
  64. '.wstat',
  65. '.power',
  66. '.config',
  67. '.zread',
  68. '.zwrite',
  69. );
  70. for (my $i = 0; $i < $num_lines; $i++) {
  71. my $ln = $lines[$i];
  72. next if ($ln =~ /^\s*[\r\n]*$/);
  73. if ($ln =~ /^\s*Dev\s+.*tab.*\{/) {
  74. $indev = 0;
  75. } elsif ($indev >= 0) {
  76. if ($ln =~ /^\s*\};/ || $ln =~/^\s*\./) {
  77. $indev = -1;
  78. } elsif ($ln =~ /^\s*([^\s].*)$/){
  79. $lines[$i] = "\t" . $labels[$indev] . " = " . $1 . "\n";
  80. $indev++;
  81. $matched++;
  82. }
  83. }
  84. }
  85. if (!$dry_run) {
  86. if ($matched) {
  87. die("Unable to create file: " . $ofile) if (!open(OSF, ">" . $ofile));
  88. for (my $i = 0; $i < $num_lines; $i++) {
  89. print OSF $lines[$i];
  90. }
  91. close(OSF);
  92. }
  93. }
  94. return $matched;
  95. }