lineends.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/local/bin/perl
  2. #
  3. # Heuristically converts line endings to the current OS's preferred format
  4. #
  5. # All existing line endings must be identical (e.g. lf's only, or even
  6. # the accedental cr.cr.lf sequence.) If some lines end lf, and others as
  7. # cr.lf, the file is presumed binary. If the cr character appears anywhere
  8. # except prefixed to an lf, the file is presumed binary. If there is no
  9. # change in the resulting file size, or the file is binary, the conversion
  10. # is discarded.
  11. #
  12. # Todo: Handle NULL stdin characters gracefully.
  13. #
  14. use IO::File;
  15. use File::Find;
  16. # The ignore list is '-' seperated, with this leading hyphen and
  17. # trailing hyphens in ever concatinated list below.
  18. $ignore = "-";
  19. # Image formats
  20. $ignore .= "gif-jpg-jpeg-png-ico-bmp-";
  21. # Archive formats
  22. $ignore .= "tar-gz-z-zip-jar-war-bz2-tgz-";
  23. # Many document formats
  24. $ignore .= "eps-psd-pdf-ai-";
  25. # Some encodings
  26. $ignore .= "ucs2-ucs4-";
  27. # Some binary objects
  28. $ignore .= "class-so-dll-exe-obj-a-o-lo-slo-sl-dylib-";
  29. # Some build env files
  30. $ignore .= "mcp-xdc-ncb-opt-pdb-ilk-sbr-";
  31. $preservedate = 1;
  32. $forceending = 0;
  33. $givenpaths = 0;
  34. $notnative = 0;
  35. while (defined @ARGV[0]) {
  36. if (@ARGV[0] eq '--touch') {
  37. $preservedate = 0;
  38. }
  39. elsif (@ARGV[0] eq '--nocr') {
  40. $notnative = -1;
  41. }
  42. elsif (@ARGV[0] eq '--cr') {
  43. $notnative = 1;
  44. }
  45. elsif (@ARGV[0] eq '--force') {
  46. $forceending = 1;
  47. }
  48. elsif (@ARGV[0] eq '--FORCE') {
  49. $forceending = 2;
  50. }
  51. elsif (@ARGV[0] =~ m/^-/) {
  52. die "What is " . @ARGV[0] . " supposed to mean?\n\n"
  53. . "Syntax:\t$0 [option()s] [path(s)]\n\n" . <<'OUTCH'
  54. Where: paths specifies the top level directory to convert (default of '.')
  55. options are;
  56. --cr keep/add one ^M
  57. --nocr remove ^M's
  58. --touch the datestamp (default: keeps date/attribs)
  59. --force mismatched corrections (unbalanced ^M's)
  60. --FORCE all files regardless of file name!
  61. OUTCH
  62. }
  63. else {
  64. find(\&totxt, @ARGV[0]);
  65. print "scanned " . @ARGV[0] . "\n";
  66. $givenpaths = 1;
  67. }
  68. shift @ARGV;
  69. }
  70. if (!$givenpaths) {
  71. find(\&totxt, '.');
  72. print "did .\n";
  73. }
  74. sub totxt {
  75. $oname = $_;
  76. $tname = '.#' . $_;
  77. if (!-f) {
  78. return;
  79. }
  80. @exts = split /\./;
  81. if ($forceending < 2) {
  82. while ($#exts && ($ext = pop(@exts))) {
  83. if ($ignore =~ m|-$ext-|i) {
  84. return;
  85. }
  86. }
  87. }
  88. @ostat = stat($oname);
  89. $srcfl = new IO::File $oname, "r" or die;
  90. $dstfl = new IO::File $tname, "w" or die;
  91. binmode $srcfl;
  92. if ($notnative) {
  93. binmode $dstfl;
  94. }
  95. undef $t;
  96. while (<$srcfl>) {
  97. if (s/(\r*)\n$/\n/) {
  98. $n = length $1;
  99. if (!defined $t) {
  100. $t = $n;
  101. }
  102. if (!$forceending && (($n != $t) || m/\r/)) {
  103. print "mismatch in " .$oname. ":" .$n. " expected " .$t. "\n";
  104. undef $t;
  105. last;
  106. }
  107. elsif ($notnative > 0) {
  108. s/\n$/\r\n/;
  109. }
  110. }
  111. print $dstfl $_;
  112. }
  113. if (defined $t && (tell $srcfl == tell $dstfl)) {
  114. undef $t;
  115. }
  116. undef $srcfl;
  117. undef $dstfl;
  118. if (defined $t) {
  119. unlink $oname or die;
  120. rename $tname, $oname or die;
  121. @anames = ($oname);
  122. if ($preservedate) {
  123. utime $ostat[9], $ostat[9], @anames;
  124. }
  125. chmod $ostat[2] & 07777, @anames;
  126. chown $ostat[5], $ostat[6], @anames;
  127. print "Converted file " . $oname . " to text in " . $File::Find::dir . "\n";
  128. }
  129. else {
  130. unlink $tname or die;
  131. }
  132. }