summaryrefslogtreecommitdiff
path: root/install-win32/ifdef.pl
blob: d240ebbb59d0a5ece3ec336f1fcdc7888b452f4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/perl

# Simple ifdef/else/endif processor.

die "usage: ifdef [-C<command-prefix>] [-Dname ...] [control-file ...] " if (@ARGV[0] =~ /^(-h|--help)$/);

%Parms = ();

$pre = "!";
while ($arg=shift(@ARGV)) {
    if ($arg =~ /^-/) {
	if ($arg =~ /^-D(\w+)$/) {
	    $Parms{$1} = 1;
	} elsif ($arg =~ /-C(.*)$/) {
	  $pre = $1;
	} else {
	    die "unrecognized option: $arg";
	}
    } else {
	open(CONTROL, "< $arg") or die "cannot open $arg";
	while (<CONTROL>) {
	    if (/^!define\s+(\w+)/) {
                $Parms{$1} = 1;
            }
        }
    }
}

sub ifdef {
  my ($var, $enabled) = @_;
  my $def = 0;
  $def = 1 if (defined $Parms{$var}) || ($var eq "true");
  $def = 0 if $var eq "false";
  while (<STDIN>) {
    if (/^\s*\Q$pre\Eifdef\s+(\w+)\s*$/) {
      return 1 if ifdef ($1, $def & $enabled);
    } elsif (/^\s*\Q$pre\Eelseif\s+(\w+)\s*$/) {
      $def = $def ^ 1;
      return ifdef ($1, $def & $enabled);
    } elsif (/^\s*\Q$pre\Eelse\s*$/) {
      $def = $def ^ 1;
    } elsif (/^\s*\Q$pre\Eendif\s*$/) {
      return 0;
    } elsif (/^\s*\Q$pre\E/) {
      die "unrecognized command: $_";
    } else {
      print if $def && $enabled;
    }
  }
  return 1;
}

ifdef("true", 1);