X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?p=subdirmk.git;a=blobdiff_plain;f=generate;h=84572edc2ef842e0ceffacd884aa306851518831;hp=0f9dd2d75ba289af83b4072812c13d92c2185b2f;hb=143bb5d7dfa60aa0549d934a2e9356961d97895a;hpb=172acdebcae01e79278788880ee148f16395dc47 diff --git a/generate b/generate index 0f9dd2d..84572ed 100755 --- a/generate +++ b/generate @@ -19,6 +19,22 @@ print "$0 @ARGV\n" or die $!; our $srcdir='.'; +# error handling methods: +# +# Error in input file, while $err_file and $. set, eg in most of +# process_input_mk: +# err "message"; +# +# Other input or usage errors: +# die "$0: $file:$lno: problem\n"; +# die "$0: some problem not locatable in that way\n"; +# +# System call error (not ENOENT) accessing input/output files: +# die "description of problem eg maybe erbing noun: $!\n"; +# +# Bug detedcted in `generate': +# die "internal error (some information)?"; # or similar + while (@ARGV && $ARGV[0] =~ m/^-/) { $_ = shift @ARGV; last if $_ eq '--'; @@ -69,17 +85,31 @@ sub close_any_output_file() { $writing_output = undef; } -sub o { +sub oraw { + die unless defined $writing_output; + print O @_ or die "error writing $writing_output.tmp: $!\n"; +} + +sub oud { # undoubled if (defined $buffering_output) { $buffering_output .= $_ foreach @_; return; } - die unless defined $writing_output; - print O @_ or die "error writing $writing_output.tmp: $!\n"; + oraw @_; } +our $ddbl; + sub od { # maybe $-doubled - o @_; + if (!$ddbl) { + oud @_; + return; + } + foreach (@_) { + my $e = $_; + $e =~ s{\$}{\$\$}g; + oud $e; + } } sub start_output_file ($) { @@ -88,7 +118,7 @@ sub start_output_file ($) { die "$writing_output ?" if $output_files{$writing_output}++; my $tmp = "$writing_output.tmp"; open O, ">", $tmp or die "create $tmp: $!\n"; - o "# autogenerated - do not edit\n"; + oraw "# autogenerated - do not edit\n"; } sub install_output_files () { @@ -106,7 +136,7 @@ sub write_makefile ($$) { my $suppress_templates= '$(if $(filter-out clean real-clean, $(subdirmk_targets)),,'. ' MAKEFILE_TEMPLATES=)'; - o < "\$(top_srcdir)${dir_suffix}", '~' => "\$(top_srcdir)", @@ -165,12 +210,30 @@ sub process_input_mk ($$$$) { ); $pfxmap{$_} = $srcdirmap{$_}.'/' foreach keys %srcdirmap; + local $ddbl; + my @nest = (['']); + + my $push_nest = sub { + my ($nk, $nndbl) = @_; + unshift @nest, [ $nk, $ddbl ]; + $ddbl = $nndbl; + }; + my $pop_nest = sub { + my ($nk) = @_; + die unless $nest[0][0] eq $nk; + $ddbl = (shift @nest)[1]; + }; + while (<$input>) { if (s#^\s*$esc\:changequote\s+(\S+)\s+$##) { $$esclitr = $1; $set_esc->(); next; - } elsif (s#^\s*$esc\:(?=(-?)include)##) { + } elsif (s#^\s*$esc\:endm\s+$##) { + $pop_nest->('Macro'); + od "endef\n"; + next; + } elsif (s#^\s*$esc\:(?=(-?)include|macro)##) { $buffering_output=''; } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) { die "unknown directive $1"; @@ -180,10 +243,22 @@ sub process_input_mk ($$$$) { $targets->{$t} //= [ ]; } for (;;) { - unless (s{^(.*?)$esc}{}) { od $_; last; } + die if $ddbl && defined $buffering_output; + unless ($nest[0][0] eq 'Eval' + ? s{^(.*?)($esc|[{}])}{} + : s{^(.*?)($esc)}{}) { od $_; last; } od $1; + if ($2 eq '{') { + $ddbl++; + next; + } elsif ($2 eq '}') { + next if --$ddbl; + $pop_nest->('Eval'); + od '}}'; + next; + } if (s{^\\$esc}{}) { od "$$esclitr" } - elsif (s{^\\\$}{}) { od '$' } + elsif (s{^\\\$}{}) { oud '$' } elsif (s{^\\\s+$}{}) { } elsif (s{^$esc}{}) { od "$$esclitr$$esclitr" } elsif (m{^(?=$caps_re)}) { od $var_prefix } @@ -194,7 +269,15 @@ sub process_input_mk ($$$$) { elsif (s{^([~^]?)/}{}) { od $pfxmap{$1} } elsif (s{^\.}{}) { od $dir_name } elsif (s{^([~^])\.}{}) { od $srcdirmap{$1} } - elsif (s{^([~^]?)(?=[ \t])}{}) { + elsif (s{^\$\-}{}) { $ddbl=undef; } + elsif (s{^\$\+}{}) { $ddbl=1; } + elsif (s{^\$\(}{}) { die unless $ddbl; oud "\$("; } + elsif (s{^\$(\d+)}{}) { die unless $ddbl; oud "\$($1)"; } + elsif (s{^\$\{}{}) { + die if $ddbl; + od '${eval ${call '; + $push_nest->('Eval',1); + } elsif (s{^([~^]?)(?=[ \t])}{}) { my $prefix = $pfxmap{$1} // die; my $after=''; if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); } @@ -217,11 +300,15 @@ sub process_input_mk ($$$$) { my $subf = "$srcdir/$2"; process_input_mk($targets, $subf, $esclitr, $1); od "\n"; + } elsif (m#^macro\s+(\S+)\s+$#) { + od "define $1\n"; + $push_nest->('Macro', 1); } else { die "internal error buffering directive $_ "; } } } + die "unclosed $nest[0][0]" if $nest[0][0]; $input->error and die "read $f: $!\n"; close $input or die "close $f: $!\n"; } @@ -283,7 +370,7 @@ sub process_subtree ($$) { } } - o "\n"; + oraw "\n"; my @targets = sort keys %targets; foreach my $target (@targets) { @@ -321,17 +408,17 @@ sub process_tree() { process_final(\@targets); start_output_file("main.mk.tmp"); foreach my $v (qw(top_srcdir abs_top_srcdir)) { - o "$v=\@$v@\n"; + oraw "$v=\@$v@\n"; } - o "SUBDIRMK_MAKEFILES :=\n"; - o "MAKEFILE_TEMPLATES :=\n"; + oraw "SUBDIRMK_MAKEFILES :=\n"; + oraw "MAKEFILE_TEMPLATES :=\n"; foreach my $mf (@output_makefiles) { - o "SUBDIRMK_MAKEFILES += $mf\n"; + oraw "SUBDIRMK_MAKEFILES += $mf\n"; } foreach my $input (sort keys %input_files) { - o "MAKEFILE_TEMPLATES += $input\n"; + oraw "MAKEFILE_TEMPLATES += $input\n"; } - o "include \$(SUBDIRMK_MAKEFILES)\n"; + oraw "include \$(SUBDIRMK_MAKEFILES)\n"; } build_tree();