Reworked test files for better error reporting
[nasm/perl-rewrite.git] / version.pl
blobd6e260a41347130499705169978f7446a2256e45
1 #!/usr/bin/env perl
3 =head1 NAME
5 version.pl - Parse the NASM version file and produce appropriate macros
7 =head1 SYNOPSIS
9 version.pl $format < $filename
11 echo 2.06rc10 | version.pl $format
13 version.pl $format $filename
15 Where $format is one of:
17 h mac sed make nsis id xid perl yaml json
19 =head1 DESCRIPTION
21 The NASM version number is assumed to consist of:
23 E<lt>majorE<gt>.E<lt>minorE<gt>[.E<lt>subminorE<gt>][
24 plE<lt>patchlevelE<gt> |
25 rcE<lt>numberE<gt>
26 ]]E<lt>tailE<gt>
28 ... where E<lt>tailE<gt> is not necessarily numeric, but if it is of the form
29 -E<lt>digitsE<gt> it is assumed to be a snapshot release.
31 =head1 Output Formats
33 =head2 id
35 print "$id\n"
37 =head2 xid
39 printf "0x%08x\n",$id
41 =cut
44 use warnings;
45 use strict;
48 # forward definition of subroutines
49 sub Load;
50 sub help;
51 sub h;
52 sub mac;
53 sub sed;
54 sub make;
55 sub nsis;
56 sub yaml;
57 sub json;
58 sub perl;
62 # jump table to subroutines / variables
63 my %jump = (
64 id => 'id',
65 xid => 'xid',
66 hex_id => 'xid',
68 h => \&h,
69 mac => \&mac,
70 sed => \&sed,
71 make => \&make,
72 nsis => \&nsis,
74 perl => \&perl,
75 dump => \&perl,
76 yaml => \&yaml,
77 yml => \&yaml,
78 json => \&json,
79 js => \&json,
81 help => \&help,
82 usage => sub{
83 require Pod::Usage;
85 Pod::Usage::pod2usage(
86 "run perldoc $0 or pod2text $0 for more information"
93 use Scalar::Util 'reftype';
95 my($cmd, $filename) = @ARGV;
97 if(
98 not $cmd or $cmd =~ m{
100 -h |
101 (?:--)?help |
102 /[?]
106 # in this case $filename is actually output format
107 # we want to know more about
108 $jump{help}->($filename);
109 last;
111 }elsif($cmd eq 'usage'){
112 $jump{usage}->();
115 my $jump = $jump{$cmd};
116 unless( $jump ){
117 $jump{usage}->(cmd=>$cmd);
120 my $version = Load($filename);
122 if( ref $jump ){
123 my $reftype = reftype $jump;
125 if($reftype eq 'CODE'){
126 my $ret = $jump->($version);
127 print "$ret\n" if defined $ret;
129 }else{
130 # an un-used reference type
131 die;
133 }else{
134 print $version->{$jump}, "\n";
138 sub Load{
139 my($filename) = @_;
140 $filename ||= '-';
141 my %version;
144 # only really required for this first match
145 # could probably rewrite the match for earlier Perls
146 require 5.010;
147 my $line;
149 if($filename and $filename ne '-'){
150 open my $file, '<', $filename or die;
152 $line = <$file>;
153 close $file;
154 }else{
155 $line = <STDIN>;
157 chomp $line;
158 die unless length $line;
159 $version{_line} = $line;
161 $line =~ m{ ^
162 (?<major>\d+)[.](?<minor>\d+)
163 (?:[.](?<subminor>\d+))?
165 pl(?<patchlevel>\d+) |
166 rc(?<rc>\d+)
169 [-](?<snapshot>\d+) |
170 (?<tail>.+)
175 for my $key(qw'major minor subminor patchlevel rc'){
176 my $value = $+{$key} || 0;
178 # removes any leading zeros by forcing to a number
179 $version{$key} = $value + 0;
181 for my $key(qw'snapshot tail'){
182 if(exists $+{$key}){
183 $version{$key} = $+{$key};
191 # modify %version if this is a release candidate
192 if($version{rc}){
193 $version{patchlevel} = $version{rc} + 90;
195 if($version{subminor}){
196 $version{subminor}--;
197 }else{
198 $version{subminor} = 99;
200 if($version{minor}){
201 $version{minor}--;
202 }else{
203 $version{minor} = 99;
204 $version{major}--;
211 # add 'id' and 'xid' to %version
212 $version{id} =
213 ($version{major} << 24) +
214 ($version{minor} << 16) +
215 ($version{subminor} << 8) +
216 $version{patchlevel};
217 $version{xid} = sprintf('0x%08x',$version{id});
221 # add 'mangled' to %version
223 my $mangled = sprintf("%d.%02d",$version{major},$version{minor});
225 $version{subminor} or
226 $version{patchlevel} or
227 $version{snapshot}
229 $mangled .= sprintf(".%02d",$version{subminor});
232 $version{patchlevel} or
233 $version{snapshot}
235 $mangled .= sprintf(".%01d",$version{patchlevel})
239 if($version{snapshot}){
240 $mangled .= '.'.$version{snapshot}
241 }elsif( $version{tail}){
242 my $tail = $version{tail};
243 $tail =~ s/-/./g;
244 $mangled .= $tail;
247 $version{mangled} = $mangled;
250 return %version if wantarray;
251 return \%version;
255 =head2 perl - returns a dump of internally used data
258 'rc' => 10,
259 'subminor' => 99,
260 'minor' => 5,
261 'mangled' => '2.05.99.100',
262 'patchlevel' => 100,
263 'id' => 33907556,
264 'xid' => '0x02056364',
265 'major' => 2
268 =cut
270 sub perl{
271 my($version)=@_;
272 no warnings qw'once';
273 require Data::Dumper;
274 local $Data::Dumper::Terse = 1;
275 local $Data::Dumper::Indent = 1;
277 my %ret = %$version;
278 for( keys %ret ){
279 # remove any "hidden" keys
280 delete $ret{$_} if /^[_.]/;
282 return Data::Dumper::Dumper(\%ret);
285 =head2 yaml - returns the same thing as perl, but in YAML format
288 id: 33907556
289 major: 2
290 mangled: 2.05.99.100
291 minor: 5
292 patchlevel: 100
293 rc: 10
294 subminor: 99
295 xid: 0x02056364
297 =cut
299 sub yaml{
300 my($version)=@_;
301 require YAML::XS;
302 YAML::XS->import;
304 my %ret = %$version;
305 for( keys %ret ){
306 # remove any "hidden" keys
307 delete $ret{$_} if /^[_.]/;
309 return Dump(\%ret);
312 =head2 json - returns the same thing as perl, but in JSON format
315 "rc" : 10,
316 "subminor" : 99,
317 "minor" : 5,
318 "mangled" : "2.05.99.100",
319 "patchlevel" : 100,
320 "id" : 33907556,
321 "xid" : "0x02056364",
322 "major" : 2
325 =cut
327 sub json{
328 my($version)=@_;
329 require JSON;
330 #JSON->import;
332 my $json = new JSON;
334 my %ret = %$version;
335 for( keys %ret ){
336 # remove any "hidden" keys
337 delete $ret{$_} if /^[_.]/;
339 return $json->pretty->encode(\%ret);
343 =head2 h
345 #ifndef NASM_VERSION_H
346 #define NASM_VERSION_H
347 #define NASM_MAJOR_VER $major
348 #define NASM_MINOR_VER $minor
349 #define NASM_SUBMINOR_VER {$subminor || 0}
350 #define NASM_PATCHLEVEL_VER {$patchlevel || 0}
351 #define NASM_SNAPSHOT $snapshot -- if snapshot
352 #define NASM_VERSION_ID $hex_id
353 #define NASM_VER "$ver"
354 #endif /* NASM_VERSION_H */
356 =cut
359 #NASM_MAJOR_VER
360 #NASM_MINOR_VER
361 #NASM_SUBMINOR_VER -- this is zero if no subminor
362 #NASM_PATCHLEVEL_VER -- this is zero is no patchlevel
363 #NASM_SNAPSHOT -- if snapshot
364 #NASM_VERSION_ID -- version number encoded
365 #NASM_VER -- whole version number as a string
369 sub h{
370 my($version) = @_;
371 printf <<END, @$version{'major','minor','subminor','patchlevel'};
372 #ifndef NASM_VERSION_H
373 #define NASM_VERSION_H
374 #define NASM_MAJOR_VER %d
375 #define NASM_MINOR_VER %d
376 #define NASM_SUBMINOR_VER %d
377 #define NASM_PATCHLEVEL_VER %d
380 if ($version->{snapshot}) {
381 printf "#define NASM_SNAPSHOT %d\n", $version->{snapshot};
384 printf <<END, @$version{'xid','_line'};
385 #define NASM_VERSION_ID %s
386 #define NASM_VER "%s"
387 #endif /* NASM_VERSION_H */
389 return;
394 =head2 mac
396 __NASM_MAJOR__ $major
397 __NASM_MINOR__ $minor
398 __NASM_SUBMINOR__ $subminor
399 __NASM_PATCHLEVEL__ $patchlevel
400 __NASM_SNAPSHOT__ $snapshot -- if snapshot
401 __NASM_VERSION_ID__ $hex_id
402 __NASM_VER__ $ver
404 =cut
406 sub mac{
407 my($version) = @_;
408 printf <<'END', @$version{'major','minor','subminor','patchlevel'};
409 %%define __NASM_MAJOR__ %d
410 %%define __NASM_MINOR__ %d
411 %%define __NASM_SUBMINOR__ %d
412 %%define __NASM_PATCHLEVEL__ %d
415 if ($version->{snapshot}) {
416 printf "%%define __NASM_SNAPSHOT__ %d\n", $version->{snapshot};
419 printf <<'END', @$version{'id','_line'};
420 %%define __NASM_VERSION_ID__ 0%08Xh
421 %%define __NASM_VER__ "%s"
423 return;
428 =head2 sed
430 s/@@NASM_MAJOR@@/$major/g
431 s/@@NASM_MINOR@@/$minor/g
432 s/@@NASM_SUBMINOR@@/$sub_minor/g
433 s/@@NASM_PATCHLEVEL@@/$patchlevel/g
434 s/@@NASM_SNAPSHOT@@/$snapshot/g
435 s/@@NASM_VERSION_ID@@/$id/g
436 s/@@NASM_VERSION_XID@@/$hex_id/g
437 s/@@NASM_VER@@/$ver/g
438 s/@@NASM_MANGLED_VER@@/$mangled/g
440 =cut
442 sub sed{
443 my($version) = @_;
444 my @rep = @$version{qw{
445 major
446 minor
447 subminor
448 patchlevel
449 snapshot
452 _line
453 mangled
455 no warnings 'uninitialized';
456 sprintf <<'END', @rep;
457 s/@@NASM_MAJOR@@/%d/g
458 s/@@NASM_MINOR@@/%d/g
459 s/@@NASM_SUBMINOR@@/%d/g
460 s/@@NASM_PATCHLEVEL@@/%d/g
461 s/@@NASM_SNAPSHOT@@/%d/g
462 s/@@NASM_VERSION_ID@@/%d/g
463 s/@@NASM_VERSION_XID@@/%s/g
464 s/@@NASM_VER@@/%s/g
465 s/@@NASM_MANGLED_VER@@/%s/g
471 =head2 make
473 NASM_VER=$ver
474 NASM_MAJOR_VER=$major
475 NASM_MINOR_VER=$minor
476 NASM_SUBMINOR_VER=$subminor
477 NASM_PATCHLEVEL_VER=$patchlevel
479 =cut
481 sub make{
482 my($version) = @_;
483 return sprintf <<END, @$version{'_line','major','minor','subminor','patchlevel'};
484 NASM_VER=%s
485 NASM_MAJOR_VER=%d
486 NASM_MINOR_VER=%d
487 NASM_SUBMINOR_VER=%d
488 NASM_PATCHLEVEL_VER=%d
493 =head2 nsis
495 !define VERSION "$version"
496 !define MAJOR_VER $major
497 !define MINOR_VER $minor
498 !define SUBMINOR_VER $subminor
499 !define PATCHLEVEL_VER $patchlevel
501 =cut
503 sub nsis{
504 my($version) = @_;
505 return sprintf <<'END', @$version{'_line','major','minor','subminor','patchlevel'};
506 !define VERSION "%s"
507 !define MAJOR_VER %d
508 !define MINOR_VER %d
509 !define SUBMINOR_VER %d
510 !define PATCHLEVEL_VER %d
515 sub help{
516 my($cmd) = @_;
518 my %help = (
519 sed => 'strings for sed command',
520 mac => 'strings for nasm macros',
521 h => 'strings for headers',
522 make => 'strings for makefiles',
523 perl => 'dump of program data',
524 nsis => 'what is nsis?',
525 json => 'dump of program data in json format',
526 yaml => 'dump of program data in yaml format'
529 if( $cmd and $help{$cmd} ){
530 print $help{$cmd},"\n";
531 }else{
532 print "$0 [help]? [ ".join(' | ',keys %help)." ]\n";
534 return;