Subversion Repositories DevTools

Rev

Rev 5710 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 dpurdie 1
#! perl
2
########################################################################
6177 dpurdie 3
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
227 dpurdie 4
#
5
# Module name   : jats.sh
6
# Module type   : Makefile system
7
# Compiler(s)   : n/a
8
# Environment(s): jats
9
#
10
# Description   : Transform a file
11
#                 This is a JATS command line utility that can be used to:
12
#                   1) Copy a file
13
#                   2) Perform DOS <-> Unix converion
14
#                   3) Perform tag substitution
15
#
16
# Usage:    jats_transform_file
17
#               -infile=xxxxx
18
#               -outfile=yyyyyyy
19
#               -dos2unix, -unix2dos
20
#               -tag=tag,value
21
#
22
#               The utilty may be used by user scripts
23
#               The utilty may be used within GenerateFiles
24
#
25
#......................................................................#
26
 
255 dpurdie 27
require 5.006_001;
227 dpurdie 28
use strict;
29
use warnings;
30
 
31
use JatsError;
32
use FileUtils;
33
 
34
use File::Copy;
35
use Getopt::Long;
36
use Pod::Usage;                             # required for help support
37
 
38
 
39
################################################################################
40
#   Option variables
41
#
42
 
43
my $VERSION = "1.0.0";                      # Update this
44
my $opt_verbose = 0;
45
my $opt_datafile = "";
46
my $opt_ofile;
47
my $opt_infile;
48
my $opt_help = 0;
49
my $opt_manual;
50
my $opt_2dos;
51
my $opt_2unix;
52
my @opt_tags;
53
my $opt_tsep = ',';
54
 
55
#   Globals
56
#
57
my @tag_order;
58
my %tags;                                      # Tags data
59
 
60
my $result = GetOptions (
61
                "help+"     => \$opt_help,          # flag, multiple use allowed
62
                "manual"    => \$opt_manual,        # flag
63
                "verbose+"  => \$opt_verbose,       # flag
64
                "outfile=s" => \$opt_ofile,         # string
65
                "infile=s"  => \$opt_infile,        # string
66
                "unix"      => \$opt_2unix,         # flag
67
                "dos"       => \$opt_2dos,          # flag
68
                "tag=s"     => \@opt_tags,          # flag
69
                "tsep=s"    => \$opt_tsep,          # flag
70
                );
71
 
72
#
73
#   Process help and manual options
74
#
75
pod2usage(-verbose => 0, -message => "Version: $VERSION")  if ($opt_help == 1  || ! $result);
76
pod2usage(-verbose => 1)  if ($opt_help == 2 );
77
pod2usage(-verbose => 2)  if ($opt_manual || ($opt_help > 2));
78
 
79
#
80
#   Configure the error reporting process now that we have the user options
81
#
82
ErrorConfig( 'name'    =>'TRANS',
83
             'verbose' => $opt_verbose );
84
 
85
#
86
#   Allow two command line argumenst to be infile and outfile
87
#
88
unless ( $opt_infile || $opt_ofile )
89
{
90
    $opt_infile = shift @ARGV;
91
    $opt_ofile =  shift @ARGV;
92
}
93
 
94
#
95
#   Sanity test the user arguemnts
96
#
97
Error ("Too many command line arguments: @ARGV" )
98
    if ( $#ARGV >= 0 );
99
 
100
Error ("No Input File specified")
101
    unless ( $opt_infile );
102
 
103
Error ("Input File is a directory")
104
    if ( -d $opt_infile );
105
 
106
Error ("Input File does not exist")
107
    unless ( -e $opt_infile );
108
 
109
Error ("No Output File specified" )
110
    unless ( $opt_ofile );
111
 
112
Error ("Input and output file are the same" )
113
    if ( $opt_infile eq $opt_ofile );
114
 
115
Error ("Select one of DOS or UNIX line endings" )
116
    if ( $opt_2unix && $opt_2dos );
117
 
118
#
119
#   Determine the output directory
120
#   Ensure that it exists
121
#
122
my $tdir = StripFileExt( $opt_ofile );
123
Verbose ("Target directory: $tdir");
124
Error ("Target directory does not exist: $tdir")
125
    if ( $tdir && ! -d $tdir );
126
 
127
Verbose ("Create DOS line endings" ) if ( $opt_2dos );
128
Verbose ("Create UNIX line endings" ) if ( $opt_2unix );
129
 
130
#
131
#   Process the TAGS and create a structure to be used later
132
#
133
my $sep = quotemeta ($opt_tsep );
134
foreach my $tag ( @opt_tags )
135
{
136
    my ($tname,$tvalue) = split ( $sep, $tag );
137
    Error ("No tag value in: $tag" ) unless ( defined $tvalue );
138
    Error ("Duplicate Tag: $tname" ) if ( exists $tags{$tname} );
139
    Verbose ("Tag: $tname :: $tvalue");
140
    push @tag_order, $tname;
141
    $tags{$tname} = $tvalue;
142
}
143
 
144
#
145
#   Is this a simple file copy
146
#   If so then don't treat the file as a text file
147
#
148
unless ( $opt_2unix || $opt_2dos || @opt_tags )
149
{
150
    simple_file_copy();
151
    exit (0);
152
}
153
 
154
#
155
#   Not a simple copy
156
#   Read each line from the input file
157
#       - Perform translations
158
#       - Perform line end fixup
159
#
160
open (FH, "<", $opt_infile ) || Error ("Cannot open input file: $opt_infile", "Reason: $!");
161
binmode(FH);
162
 
229 dpurdie 163
#
164
#   Create the output file
165
#   Attempt to delete it first, if it exists
166
#
167
unlink ($opt_ofile) if -f ($opt_ofile);
227 dpurdie 168
open (FO, ">", $opt_ofile ) || Error ("Cannot open output file: $opt_ofile", "Reason: $!");
169
binmode(FO);
170
 
171
#
172
#   Setup line ending
173
#
174
my $eol = '';
175
$eol = "\n"   if ( $opt_2unix );
176
$eol = "\r\n" if ( $opt_2dos );
177
 
178
#
179
#   Read each line form the input file
180
#
181
while ( <FH> )
182
{
183
    #
184
    #   Perform tag replacement
185
    #
186
    foreach my $tag ( @tag_order )
187
    {
188
        my $value = $tags{$tag};
189
        if ( s~$tag~$value~g )
190
        {
191
            Verbose ("Replaced: $tag with $value");
192
        }
193
    }
194
 
195
    #
196
    #   Perform Line Ending massaging
197
    #
198
    if ( $opt_2unix || $opt_2dos )
199
    {
200
        s~[\n\r]+$~$eol~;
201
    }
202
 
203
    print FO $_;
204
}
205
close FH;
206
close FO;
207
 
208
exit (0);
209
 
210
 
211
#-------------------------------------------------------------------------------
212
# Function        : simple_file_copy
213
#
214
# Description     : Simple binary file copy
215
#                   Used to copy a file, with a possible name change
216
#
217
# Inputs          : None. Use globals
218
#                         $opt_infile - does exist and is not a directory
219
#
220
# Returns         : Will not return on error
221
#
222
sub simple_file_copy
223
{
224
    Verbose ("Simple Copy: $opt_infile, $opt_ofile" );
225
    unless (File::Copy::copy($opt_infile, $opt_ofile))
226
    {
227
        Error ("File Copy Failed: $!",
228
               "Input file: $opt_infile",
229
               "Output file: $opt_ofile" );
230
    }
231
}
232
 
233
#-------------------------------------------------------------------------------
234
#   Documentation
235
#
236
 
237
=pod
238
 
361 dpurdie 239
=for htmltoc    MAKEUTIL::
240
 
227 dpurdie 241
=head1 NAME
242
 
243
jats_transform_file - Transform a file
244
 
245
=head1 SYNOPSIS
246
 
247
  jats etool jats_transform_file [options]
248
 
249
 Options:
250
    -help               - brief help message
251
    -help -help         - Detailed help message
252
    -man                - Full documentation
253
    -verbose            - Verbose operation
254
    -infile=file        - Input file
255
    -outfile=file       - Output file
256
    -dos                - Output file will have DOS line endingd
257
    -unix               - Output file will have Unix line endings
258
    -tsep=c             - Tag seperator character. Default ','
259
    -tag=name,value     - Perform tag replacement
260
 
261
 
262
=head1 OPTIONS
263
 
264
=over 8
265
 
266
=item B<-help>
267
 
268
Print a brief help message and exits.
269
 
270
=item B<-help -help>
271
 
272
Print a detailed help message with an explanation for each option.
273
 
274
=item B<-man>
275
 
276
Prints the manual page and exits.
277
 
278
=item B<-verbose>
279
 
280
Increases program output. This option may be specified mutiple times
281
 
282
=item B<-infile=xxx>
283
 
284
The name of the input file. There is no default file.
285
 
286
=item B<-outfile=xxx>
287
 
288
The name of the output file. The output file cannot be the same as
289
the input file. This utility does not do in-place transformations
290
 
291
=item B<-dos>
292
 
293
The input file is assumed to be a text file. Line endings will be replaced
294
with DOS line endings.
295
 
296
=item B<-unix>
297
 
298
The input file is assumed to be a text file. Line endings will be replaced
299
with Unix line endings.
300
 
301
=item B<-tsep=c>
302
 
303
The character specified the seperator used in the -tag option to isolate the
304
name and the tag. The default character is ','.
305
 
306
This value applies to all tags fields specified on the command line.
307
 
308
=item B<-tag=name,value>
309
 
310
The input file is assumed to be a text file. Lines will be scanned and text
311
replacement will occur.
312
 
313
Tags of the form 'name' will be replaced with 'value'.
314
 
315
The order of replacement is the same as that specified on the command line.
316
 
317
=back
318
 
319
=head1 DESCRIPTION
320
 
321
This JATS utility is used within the build system to perform a number of file
322
transformation operations.
323
 
324
If no transformation options are invoked then the utility will assume that the
325
file is a binary file and simply copy the file.
326
 
327
If neiterh DOS or UNIX line endings are specified then the line endings will
328
be preserved.
329
 
5476 dpurdie 330
=head1 EXAMPLE
331
 
332
The following makfile.pl fragment will create 'file1.h' based on 'file1.template'. It will replace various fields.
333
 
334
    GenerateFiles ('*', 'jats_transform_file',
335
                        '--AutoGenerate',
336
                        '--NoVarTag',
337
                        '-infile=--Prerequisite(file1.template)',
338
                        '-outfile=--Generated(file1.h)',
339
                        '-unix',
340
                        '-tag=__NAME__,--Var(BuildName)',
341
                        '-tag=__VERSION__,--Var(BuildVersion)',
342
                        '-tag=__VERSIONNUM__,--Var(BuildVersionNum)',
343
                    );
344
 
227 dpurdie 345
=cut
346