#!/usr/bin/env perl

my @files = `ls *.c`;

foreach $file (@files)
{
   chomp($file);

   # save a backup
   `if [ ! -e '$file.bak' ]; then cp $file $file.bak; fi;`;

   if(open(IN,  $file . '.bak')) {
   if(open(OUT, ">$file")) {
      while(<IN>) {
         # find lines with unclosed strings
         while(/^[^"]*("[^"]+")*"[^"]+$/) {
            s/^(.+)\\$/\1/;   # escaped newlines are not standard
            s/^(.+)/\1"/;     # append this lines with a quote
            print OUT $_;
            $_ = '"' . <IN>;  # prepend the next line with a quote
         }
         print OUT $_;
      }
   }
   }
}
