From e369ff5f2c0b0af85177277395fe226b38113471 Mon Sep 17 00:00:00 2001 From: Daniel Gustafsson Date: Mon, 31 Oct 2016 15:55:31 +0100 Subject: [PATCH 1/5] Fix filehandle usage This patch modernizes the way the Unicode Perl scripts open files for reading and writing as well as how the IO is done against the filehandles: * Replace all global filehandles and GLOB derefences with local filehandle variables. Using open(FOO, ..); creates a global filehandle in FOO across the script and modules. Also remove typeglobbing of *FOO with passing a reference to the opened filehandle. While using scalar filehandles removes the need to explicitly call close(); since they will automatically be closed when going out of scope, all close(); calls are left in place since they aid readability and can minimize confusion. * Use new-style open() calls with explicit open modes to prevent against opening files for reading with write access etc. Doing open(my $f, $name); for reading the file with filename stored in $name causes the file to be opened with write permissions is $name contains ">foo". --- src/backend/utils/mb/Unicode/UCS_to_EUC_CN.pl | 6 +- .../utils/mb/Unicode/UCS_to_EUC_JIS_2004.pl | 6 +- src/backend/utils/mb/Unicode/UCS_to_GB18030.pl | 6 +- .../utils/mb/Unicode/UCS_to_SHIFT_JIS_2004.pl | 6 +- src/backend/utils/mb/Unicode/UCS_to_UHC.pl | 6 +- src/backend/utils/mb/Unicode/convutils.pm | 93 +++++++++++----------- src/backend/utils/mb/Unicode/make_mapchecker.pl | 2 +- 7 files changed, 63 insertions(+), 62 deletions(-) diff --git a/src/backend/utils/mb/Unicode/UCS_to_EUC_CN.pl b/src/backend/utils/mb/Unicode/UCS_to_EUC_CN.pl index 8c6039f..a290931 100755 --- a/src/backend/utils/mb/Unicode/UCS_to_EUC_CN.pl +++ b/src/backend/utils/mb/Unicode/UCS_to_EUC_CN.pl @@ -21,11 +21,11 @@ $this_script = $0; $in_file = "gb-18030-2000.xml"; -open(FILE, $in_file) || die("cannot open $in_file"); +open(my $fd, '<', $in_file) || die("cannot open $in_file"); my @mapping; -while () +while (<$fd>) { next if (!m/) direction => 'both' } } -close(FILE); +close($fd); print_tables("EUC_CN", \@mapping); print_radix_trees($this_script, "EUC_CN", \@mapping); diff --git a/src/backend/utils/mb/Unicode/UCS_to_EUC_JIS_2004.pl b/src/backend/utils/mb/Unicode/UCS_to_EUC_JIS_2004.pl index 1b4e99f..aff0d35 100755 --- a/src/backend/utils/mb/Unicode/UCS_to_EUC_JIS_2004.pl +++ b/src/backend/utils/mb/Unicode/UCS_to_EUC_JIS_2004.pl @@ -15,11 +15,11 @@ $this_script = $0; $in_file = "euc-jis-2004-std.txt"; -open(FILE, $in_file) || die("cannot open $in_file"); +open(my $fd, '<', $in_file) || die("cannot open $in_file"); my @all; -while ($line = ) +while (my $line = <$fd>) { if ($line =~ /^0x(.*)[ \t]*U\+(.*)\+(.*)[ \t]*#(.*)$/) { @@ -56,7 +56,7 @@ while ($line = ) push @all, { direction => 'both', ucs => $ucs, code => $code, comment => $rest }; } -close(FILE); +close($fd); print_tables("EUC_JIS_2004", \@all, 1); print_radix_trees($this_script, "EUC_JIS_2004", \@all); diff --git a/src/backend/utils/mb/Unicode/UCS_to_GB18030.pl b/src/backend/utils/mb/Unicode/UCS_to_GB18030.pl index aaa8302..c1ade68 100755 --- a/src/backend/utils/mb/Unicode/UCS_to_GB18030.pl +++ b/src/backend/utils/mb/Unicode/UCS_to_GB18030.pl @@ -21,11 +21,11 @@ $this_script = $0; $in_file = "gb-18030-2000.xml"; -open(FILE, $in_file) || die("cannot open $in_file"); +open(my $fd, '<', $in_file) || die("cannot open $in_file"); my @mapping; -while () +while (<$fd>) { next if (!m/) } } } -close(FILE); +close($fd); print_tables("GB18030", \@mapping); print_radix_trees($this_script, "GB18030", \@mapping); diff --git a/src/backend/utils/mb/Unicode/UCS_to_SHIFT_JIS_2004.pl b/src/backend/utils/mb/Unicode/UCS_to_SHIFT_JIS_2004.pl index a9641e4..86ed705 100755 --- a/src/backend/utils/mb/Unicode/UCS_to_SHIFT_JIS_2004.pl +++ b/src/backend/utils/mb/Unicode/UCS_to_SHIFT_JIS_2004.pl @@ -15,11 +15,11 @@ $this_script = $0; $in_file = "sjis-0213-2004-std.txt"; -open(FILE, $in_file) || die("cannot open $in_file"); +open(my $fd, '<', $in_file) || die("cannot open $in_file"); my @mapping; -while ($line = ) +while (my $line = <$fd>) { if ($line =~ /^0x(.*)[ \t]*U\+(.*)\+(.*)[ \t]*#(.*)$/) { @@ -78,7 +78,7 @@ while ($line = ) direction => $direction }; } -close(FILE); +close($fd); print_tables("SHIFT_JIS_2004", \@mapping, 1); print_radix_trees($this_script, "SHIFT_JIS_2004", \@mapping); diff --git a/src/backend/utils/mb/Unicode/UCS_to_UHC.pl b/src/backend/utils/mb/Unicode/UCS_to_UHC.pl index 6f61df4..e49e5c9 100755 --- a/src/backend/utils/mb/Unicode/UCS_to_UHC.pl +++ b/src/backend/utils/mb/Unicode/UCS_to_UHC.pl @@ -21,11 +21,11 @@ $this_script = $0; $in_file = "windows-949-2000.xml"; -open(FILE, $in_file) || die("cannot open $in_file"); +open(my $in, '<', $in_file) || die("cannot open $in_file"); my @mapping; -while () +while (<$in>) { next if (!m/) } } } -close(FILE); +close($in); # One extra character that's not in the source file. push @mapping, { direction => 'both', code => 0xa2e8, ucs => 0x327e, comment => 'CIRCLED HANGUL IEUNG U' }; diff --git a/src/backend/utils/mb/Unicode/convutils.pm b/src/backend/utils/mb/Unicode/convutils.pm index 35ba423..cb0c596 100644 --- a/src/backend/utils/mb/Unicode/convutils.pm +++ b/src/backend/utils/mb/Unicode/convutils.pm @@ -44,7 +44,7 @@ sub read_source my ($fname) = @_; my @r; - open(my $in, $fname) || die("cannot open $fname"); + open(my $in, '<', $fname) || die("cannot open $fname"); while (<$in>) { @@ -161,7 +161,7 @@ sub print_from_utf8_map my $fname = lc("utf8_to_${charset}.map"); print "- Writing UTF8=>${charset} conversion table: $fname\n"; - open(my $out, "> $fname") || die "cannot open output file : $fname\n"; + open(my $out, '>', $fname) || die "cannot open output file : $fname\n"; printf($out "/* src/backend/utils/mb/Unicode/$fname */\n\n". "static const pg_utf_to_local ULmap${charset}[ %d ] = {", scalar(@$table)); @@ -196,7 +196,7 @@ sub print_from_utf8_combined_map my $fname = lc("utf8_to_${charset}_combined.map"); print "- Writing UTF8=>${charset} conversion table: $fname\n"; - open(my $out, "> $fname") || die "cannot open output file : $fname\n"; + open(my $out, '>', $fname) || die "cannot open output file : $fname\n"; printf($out "/* src/backend/utils/mb/Unicode/$fname */\n\n". "static const pg_utf_to_local_combined ULmap${charset}_combined[ %d ] = {", scalar(@$table)); @@ -225,7 +225,7 @@ sub print_to_utf8_map my $fname = lc("${charset}_to_utf8.map"); print "- Writing ${charset}=>UTF8 conversion table: $fname\n"; - open(my $out, "> $fname") || die "cannot open output file : $fname\n"; + open(my $out, '>', $fname) || die "cannot open output file : $fname\n"; printf($out "/* src/backend/utils/mb/Unicode/${fname} */\n\n". "static const pg_local_to_utf LUmap${charset}[ %d ] = {", scalar(@$table)); @@ -261,7 +261,7 @@ sub print_to_utf8_combined_map my $fname = lc("${charset}_to_utf8_combined.map"); print "- Writing ${charset}=>UTF8 conversion table: $fname\n"; - open(my $out, "> $fname") || die "cannot open output file : $fname\n"; + open(my $out, '>', $fname) || die "cannot open output file : $fname\n"; printf($out "/* src/backend/utils/mb/Unicode/${fname} */\n\n". "static const pg_local_to_utf_combined LUmap${charset}_combined[ %d ] = {", scalar(@$table)); @@ -298,7 +298,7 @@ sub load_maptable my($fname) = @_; my %c; - open(my $in, $fname) || die("cannot open $fname"); + open(my $in, '<', $fname) || die("cannot open $fname"); while(<$in>) { @@ -308,6 +308,8 @@ sub load_maptable } } + close($in); + return \%c; } @@ -725,8 +727,8 @@ sub print_chars_table my($st, $ed) = ($table->{attr}{min}, $table->{attr}{max}); my($type) = $table->{attr}{is32bit} ? "uint32" : "uint16"; - printf(OUT "static const %s %s[] =\n{", $type, $tblname); - printf(OUT " /* chars content - index range = [%02x, %02x] */", $st, $ed); + printf { $$hd } "static const %s %s[] =\n{", $type, $tblname; + printf { $$hd } " /* chars content - index range = [%02x, %02x] */", $st, $ed; # values in character table are written in fixedwidth # hexadecimals. calculate the number of columns in a line. 13 is @@ -748,14 +750,14 @@ sub print_chars_table if (!$first0) { $line =~ s/\s+$//; # remove trailing space - print $hd $line, ",\n"; + print { $$hd } $line, ",\n"; $line = ""; } $first0 = 0; # write segment header - printf($hd "\n /*** %4sxx - offset 0x%05x ***/", - $s->{label}, $s->{offset}); + printf { $$hd } "\n /*** %4sxx - offset 0x%05x ***/", + $s->{label}, $s->{offset}; # write segment content my $first1 = 1; @@ -771,7 +773,7 @@ sub print_chars_table if ($xpos >= $colnum || $first1) { $line =~ s/\s+$//; # remove trailing space - print $hd $line, "\n"; + print { $$hd } $line, "\n"; $line = sprintf(" /* %02x */ ", $j); $xpos = 0; } @@ -795,11 +797,10 @@ sub print_chars_table } $xpos++; } - } $line =~ s/\s+$//; - print $hd $line, "\n};\n"; + print { $$hd } $line, "\n};\n"; } ###################################################### @@ -818,9 +819,9 @@ sub print_flat_table my($hd, $table, $tblname, $width) = @_; my($st, $ed) = ($table->{attr}{min}, $table->{attr}{max}); - print $hd "static const $radix_node_type $tblname =\n{"; - printf($hd "\n 0x%x, 0x%x, /* table range */\n", $st, $ed); - print $hd " {"; + print { $$hd } "static const $radix_node_type $tblname =\n{"; + printf { $$hd } "\n 0x%x, 0x%x, /* table range */\n", $st, $ed; + print { $$hd } " {"; my $first = 1; my $line = ""; @@ -837,7 +838,7 @@ sub print_flat_table if ($first || length($line.$newitem) > $width) { $line =~ s/\s+$//; # remove trailing space - print $hd "$line\n"; + print { $$hd } "$line\n"; $line = " "; } else @@ -847,8 +848,8 @@ sub print_flat_table $line .= $newitem; $first = 0; } - print $hd $line; - print $hd "\n }\n};\n"; + print { $$hd } $line; + print { $$hd } "\n }\n};\n"; } ###################################################### @@ -868,16 +869,16 @@ sub print_segmented_table my ($st, $ed) = ($table->{attr}{min}, $table->{attr}{max}); # write the variable definition - print $hd "static const $radix_node_type $tblname =\n{"; - printf($hd "\n 0x%02x, 0x%02x, /*index range */\n {", $st, $ed); + print { $$hd } "static const $radix_node_type $tblname =\n{"; + printf { $$hd } "\n 0x%02x, 0x%02x, /*index range */\n {", $st, $ed; my $first0 = 1; foreach my $k (sort {$a <=> $b} keys $table->{i}) { - print $hd ",\n" if (!$first0); + print { $$hd } ",\n" if (!$first0); $first0 = 0; - printf($hd "\n /*** %sxxxx - offset 0x%05x ****/", - $table->{i}{$k}{label}, $table->{i}{$k}{offset}); + printf { $$hd } "\n /*** %sxxxx - offset 0x%05x ****/", + $table->{i}{$k}{label}, $table->{i}{$k}{offset}; my $segstart = $table->{i}{$k}{lower}; my $segend = $table->{i}{$k}{upper}; @@ -895,7 +896,7 @@ sub print_segmented_table if ($first1 || length($line.$newitem) > $width) { $line =~ s/\s+$//; - print OUT "$line\n"; + print { $$hd } "$line\n"; $line = sprintf(" /* %2s%02x */ ", $table->{i}{$k}{label}, $j); } else @@ -905,9 +906,9 @@ sub print_segmented_table $line .= $newitem; $first1 = 0; } - print $hd $line; + print { $$hd } $line; } - print $hd "\n }\n};\n"; + print { $$hd } "\n }\n};\n"; } ######################################### @@ -954,20 +955,20 @@ sub print_radix_main my $b4i2name = make_table_refname($trie->{b4idx}[1], $name_prefix); my $b4i3name = make_table_refname($trie->{b4idx}[2], $name_prefix); - print $hd "static const $radix_type $tblname =\n{\n"; - print $hd " /* final character table offset and body */\n"; - printf($hd " 0x%x, 0x%x, %s, %s, %s,\n", + print { $$hd } "static const $radix_type $tblname =\n{\n"; + print { $$hd } " /* final character table offset and body */\n"; + printf { $$hd } " 0x%x, 0x%x, %s, %s, %s,\n", $trie->{csegs}{attr}{min}, $trie->{csegs}{attr}{max}, $trie->{csegs}{attr}{has0page} ? 'true' : 'false', - $ctbl16name, $ctbl32name); - - print $hd " /* 2-byte code table */\n"; - print $hd " $b2iname,\n"; - print $hd " /* 3-byte code tables */\n"; - print $hd " {$b3i1name, $b3i2name},\n"; - print $hd " /* 4-byte code table */\n"; - print $hd " {$b4i1name, $b4i2name, $b4i3name},\n"; - print $hd "};\n"; + $ctbl16name, $ctbl32name; + + print { $$hd } " /* 2-byte code table */\n"; + print { $$hd } " $b2iname,\n"; + print { $$hd } " /* 3-byte code tables */\n"; + print { $$hd } " {$b3i1name, $b3i2name},\n"; + print { $$hd } " /* 4-byte code table */\n"; + print { $$hd } " {$b4i1name, $b4i2name, $b4i3name},\n"; + print { $$hd } "};\n"; } ###################################################### @@ -1053,22 +1054,22 @@ sub print_radix_map print "- Writing UTF8=>${csname} conversion radix index: $fname\n"; } - open(OUT, "> $fname") || die("cannot open $fname"); + open(my $out, '>', "$fname") || die("cannot open $fname"); - print OUT "/* This file is generated by $this_script */\n\n"; + print $out "/* This file is generated by $this_script */\n\n"; foreach my $t (@{$trie->{all}}) { my $table_name = $name_prefix.$t->{attr}{name}; - if (&print_radix_table(*OUT, $t, $table_name, $tblwidth)) + if (&print_radix_table(\$out, $t, $table_name, $tblwidth)) { - print OUT "\n"; + print $out "\n"; } } - &print_radix_main(*OUT, $tblname, $trie, $name_prefix); - close(OUT); + &print_radix_main(\$out, $tblname, $trie, $name_prefix); + close($out); } diff --git a/src/backend/utils/mb/Unicode/make_mapchecker.pl b/src/backend/utils/mb/Unicode/make_mapchecker.pl index 0e1cbb6..d2ef1d6 100755 --- a/src/backend/utils/mb/Unicode/make_mapchecker.pl +++ b/src/backend/utils/mb/Unicode/make_mapchecker.pl @@ -22,7 +22,7 @@ foreach my $rmap (@radixmaps) # Generate sanity checker source my $out; -open($out, ">map_checker.h") || +open($out, '>', "map_checker.h") || die "cannot open file to write: map_checker.c"; foreach my $i (sort @radixmaps) { -- 2.6.4 (Apple Git-63)