From: Ian Jackson Date: Sat, 28 Dec 2019 00:27:13 +0000 (+0000) Subject: Error handling: Replace or update many calls to `die' X-Git-Tag: subdirmk/0.3~56 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?p=subdirmk.git;a=commitdiff_plain;h=4110b41e20eb2c12fe68cdcb994ee47e7e25ec18;ds=sidebyside Error handling: Replace or update many calls to `die' To conform to new approach. The `ending wrong kind of nest' error will be dealt with in a moment. Signed-off-by: Ian Jackson --- diff --git a/generate b/generate index 84572ed..2a65d99 100755 --- a/generate +++ b/generate @@ -86,7 +86,7 @@ sub close_any_output_file() { } sub oraw { - die unless defined $writing_output; + die 'internal error' unless defined $writing_output; print O @_ or die "error writing $writing_output.tmp: $!\n"; } @@ -115,7 +115,8 @@ sub od { # maybe $-doubled sub start_output_file ($) { close_any_output_file(); ($writing_output) = @_; - die "$writing_output ?" if $output_files{$writing_output}++; + die "internal error ($writing_output?)" + if $output_files{$writing_output}++; my $tmp = "$writing_output.tmp"; open O, ">", $tmp or die "create $tmp: $!\n"; oraw "# autogenerated - do not edit\n"; @@ -194,7 +195,7 @@ sub process_input_mk ($$$$) { my $input = new IO::File $f, '<'; if (!$input) { - die "open $f: $!\n" unless $!==ENOENT && $enoent_ok; + err "open $f: $!" unless $!==ENOENT && $enoent_ok; return; } $input_files{$f}++; @@ -221,6 +222,7 @@ sub process_input_mk ($$$$) { my $pop_nest = sub { my ($nk) = @_; die unless $nest[0][0] eq $nk; + # ^ xxx need better message $ddbl = (shift @nest)[1]; }; @@ -236,14 +238,15 @@ sub process_input_mk ($$$$) { } elsif (s#^\s*$esc\:(?=(-?)include|macro)##) { $buffering_output=''; } elsif (m#^\s*$esc\:([a-z][-0-9a-z_]*)#) { - die "unknown directive $1"; + err "unknown directive &:$1"; } elsif (s{^\s*${esc}TARGETS(?:_([0-9a-zA-Z_]+))?(?=\W)}{}) { my $t = $1 // 'all'; od target_varname($var_prefix, $t); $targets->{$t} //= [ ]; } for (;;) { - die if $ddbl && defined $buffering_output; + err 'cannot $-double &-processed RHS of directive' + if $ddbl && defined $buffering_output; unless ($nest[0][0] eq 'Eval' ? s{^(.*?)($esc|[{}])}{} : s{^(.*?)($esc)}{}) { od $_; last; } @@ -271,14 +274,14 @@ sub process_input_mk ($$$$) { elsif (s{^([~^])\.}{}) { od $srcdirmap{$1} } elsif (s{^\$\-}{}) { $ddbl=undef; } elsif (s{^\$\+}{}) { $ddbl=1; } - elsif (s{^\$\(}{}) { die unless $ddbl; oud "\$("; } - elsif (s{^\$(\d+)}{}) { die unless $ddbl; oud "\$($1)"; } + elsif (s{^\$\(}{}) { ddbl_only($&); oud "\$("; } + elsif (s{^\$(\d+)}{}) { ddbl_only($&); oud "\$($1)"; } elsif (s{^\$\{}{}) { - die if $ddbl; + err 'macro invocation cannot be re-$-doubled' if $ddbl; od '${eval ${call '; $push_nest->('Eval',1); } elsif (s{^([~^]?)(?=[ \t])}{}) { - my $prefix = $pfxmap{$1} // die; + my $prefix = $pfxmap{$1} // die "internal error ($1?)"; my $after=''; if (m{([ \t])$esc}) { ($_,$after) = ($`, $1.$'); } s{(?<=[ \t])(?=\S)(?!\\\s*$)}{$prefix}g; @@ -290,7 +293,7 @@ sub process_input_mk ($$$$) { od $_; $_ = ''; } else { - die "bad escape $$esclitr$_ "; + err "bad &-escape \`$$esclitr$_'"; } } if (defined $buffering_output) { @@ -304,7 +307,7 @@ sub process_input_mk ($$$$) { od "define $1\n"; $push_nest->('Macro', 1); } else { - die "internal error buffering directive $_ "; + die "internal error ($_?)"; } } } @@ -350,7 +353,7 @@ sub process_subtree ($$) { foreach my $child (@{ $node->[1] }) { my @childpath = (@$path, $child->[0]); my $child_subdir = join '/', @childpath; - mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!"; + mkdir $child_subdir or $!==EEXIST or die "mkdir $child_subdir: $!\n"; push @{ $targets{$_} }, $child_subdir foreach process_subtree($child, \@childpath); } @@ -363,10 +366,11 @@ sub process_subtree ($$) { } else { my $sdmk = "${dir_prefix}Subdir.sd.mk"; if (stat $sdmk) { - die "$sdmk unexpectedly exists (${dir_prefix} not mentioned)"; + die + "$0: $sdmk unexpectedly exists (${dir_prefix} not mentioned on subdirmk/generate command line, maybe directory is missing from SUBDIRMK_SUBDIRS)"; } elsif ($!==ENOENT) { } else { - die "stat $sdmk: $!"; + die "stat $sdmk: $!\n"; } } @@ -400,7 +404,8 @@ sub process_final ($) { process_input_mk(\%ntargets, "${srcdir}/Final.sd.mk", \$esclit, 1); delete $ntargets{$_} foreach @$otargets; my @ntargets = sort keys %ntargets; - die "late new targets @ntargets" if @ntargets; + die "$0: Final.sd.mk may not introduce new top-level targets". + " (@ntargets)\n" if @ntargets; } sub process_tree() {