home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / bin / fixmswrd.pl < prev    next >
Encoding:
Perl Script  |  2005-11-16  |  4.5 KB  |  207 lines

  1. #!/usr/bin/perl
  2. # $Id: fixmswrd.pl,v 1.4 2002/02/21 21:53:01 giles Exp $
  3.  
  4. #   (C) 1997 Anthony Shipman
  5. #   This software is provided 'as-is', without any express or implied
  6. #   warranty.  In no event will the authors be held liable for any damages
  7. #   arising from the use of this software.
  8. #   Permission is granted to anyone to use this software for any purpose,
  9. #   including commercial applications, and to alter it and redistribute it
  10. #   freely, subject to the following restrictions:
  11. #   1. The origin of this software must not be misrepresented; you must not
  12. #      claim that you wrote the original software. If you use this software
  13. #      in a product, an acknowledgment in the product documentation would be
  14. #      appreciated but is not required.
  15. #   2. Altered source versions must be plainly marked as such, and must not be
  16. #      misrepresented as being the original software.
  17. #   3. This notice may not be removed or altered from any source distribution.
  18. #   Anthony Shipman    shipmana@acm.org
  19.  
  20. # This program patches the postscript generated by MS Word printer drivers
  21. # so that they work with ghostview 1.5.  The problem is that the document
  22. # structuring conventions are not followed by Word.  The pages are supposed
  23. # to be independent but they depend on a dictionary being opened outside
  24. # of the pages.  The erroneous structure is
  25. #     %%EndSetup
  26. #     NTPSOct95 begin
  27. #     %%Page: 1 1
  28. #     <text>
  29. #     showpage
  30. #     %%Page: 2 2
  31. #     <text>
  32. #     showpage
  33. #     ......
  34. #     %%Trailer
  35. #     ...
  36. #     end
  37. #     %%EOF
  38. # This only works if the all of the structure around the pages is preserved.
  39. # The opening of NTPSOct95 happens outside of any structured section so
  40. # it is never seen by ghostview.  We change the structure to
  41. #     %%EndSetup
  42. #     %%Page: 1 1
  43. #     NTPSOct95 begin
  44. #     <text>
  45. #     showpage
  46. #     end
  47. #     %%Page: 2 2
  48. #     NTPSOct95 begin
  49. #     <text>
  50. #     showpage
  51. #     end
  52. #     ......
  53. #     %%Trailer
  54. #     ...
  55. #     %%EOF
  56. # That is the dictionary opening is repeated inside each page.
  57. # We add a comment to the document to mark that it has been converted.
  58. # This has the form 
  59. #    %LOCALGhostviewPatched
  60. #
  61. # Usage:
  62. #    fixmswrd [-v] [file [output-file]]
  63.  
  64. require 'getopts.pl';
  65.  
  66. #=================================================================
  67.  
  68. $program = "fixmswrd";
  69.  
  70. sub usage {
  71.     die "Usage: $program [-v] [file [output-file]]\n";
  72. }
  73.  
  74. #=================================================================
  75.  
  76. &Getopts("v") || &usage;
  77.  
  78. $verbose = $opt_v;
  79.  
  80.  
  81. $infile = shift(@ARGV);
  82. if ($infile)
  83. {
  84.     open(INFILE, $infile) || die "$program: Cannot read from $infile\n";
  85.     $handle = "INFILE";
  86. }
  87. else
  88. {
  89.     $handle = "STDIN";
  90. }
  91.  
  92.  
  93. $outfile = shift(@ARGV);
  94. if ($outfile)
  95. {
  96.     open(OUTFILE, ">$outfile") || die "$program: Cannot write to $outfile\n";
  97.     select(OUTFILE);
  98. }
  99.  
  100. #  This reads the header comments and detects the presence of the marker.
  101. $have_marker = 0;
  102.  
  103. undef $dict_name;
  104. undef $dict_line;
  105.  
  106. &read_comments;
  107. &put_comments;
  108.  
  109. if ($have_marker)
  110. {
  111.     $verbose && print STDERR "$program: Warning - already converted\n";
  112.  
  113.     while(<$handle>)        # pass the file through unchanged.
  114.     {
  115.     print;
  116.     }
  117. }
  118. else
  119. {
  120.     $seen_trailer = 0;
  121.  
  122.     while(<$handle>)        # massage the file
  123.     {
  124.     if ($dict_line)
  125.     {
  126.         next if (/$dict_line/o);        # drop the old begin line
  127.         $seen_trailer = 1 if (/^%%Trailer/);
  128.         next if ($seen_trailer and /^end/);    # drop the old end line
  129.     }
  130.  
  131.     print;
  132.  
  133.     if (/^%%Page:/)
  134.     {
  135.         print "$dict_name begin\n";    # add at the start of the page
  136.     }
  137.     elsif (/^showpage/)
  138.     {
  139.         print "end\n";            # add at the end of the page
  140.     }
  141.     elsif (/^%%BeginResource: procset (\S+)/)
  142.     {
  143.         $dict_name = $1;
  144.         $dict_line = "^$dict_name begin";
  145.     }
  146.     elsif (/^%%BeginProcSet: (\S+)/)    # for older document versions
  147.     {
  148.         $dict_name = $1;
  149.         $dict_line = "^$dict_name begin";
  150.     }
  151.     elsif (/^%%EndProlog:/)
  152.     {
  153.         unless ($dict_line) 
  154.         {
  155.         $verbose && 
  156.             print STDERR "$program: Warning - unrecognised document structure\n";
  157.         }
  158.     }
  159.     }
  160. }
  161.  
  162. exit 0;
  163.  
  164. #=================================================================
  165.  
  166.  
  167. #  This reads all of the header comments into an array which we can write
  168. #  out again later.  In addition we detect the presence of the marker comment.
  169.  
  170. sub read_comments
  171. {
  172.     @headers = ();
  173.  
  174.     while (<$handle>)
  175.     {                # without chopping
  176.     push(@headers, $_);
  177.     if (/^%LOCALGhostviewPatched/)
  178.     {
  179.         $have_marker = 1;
  180.     }
  181.     last if /^%%EndComments/;
  182.     }
  183. }
  184.  
  185.  
  186.  
  187. sub put_comments 
  188. {
  189.     foreach $h (@headers)
  190.     {
  191.     if (!$have_marker and ($h =~ /^%%EndComments/))
  192.     {
  193.         print "%LOCALGhostviewPatched\n";
  194.     }
  195.     print $h;        # contains the newline
  196.     }
  197. }
  198.