Skip to content

Commit

Permalink
Automatically regenerate locale_table.h
Browse files Browse the repository at this point in the history
I got tired of editing each entry when something new comes along.

Doing this opens up new possibilities of simplification, but this commit
tries to keep the generated header as close to the original as possible.
  • Loading branch information
khwilliamson committed Feb 27, 2024
1 parent 975f9cc commit 4110b0c
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 4 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -5917,6 +5917,7 @@ regen/genpacksizetables.pl Generate the size tables for pack/unpack
regen/HeaderParser.pm Module used to parse header files
regen/keywords.pl Program to write keywords.h
regen/lib_cleanup.pl Generate lib/.gitignore from MANIFEST
regen/locale.pl Program to write locale_table.h
regen/mg_vtable.pl generate mg_vtable.h
regen/miniperlmain.pl generate miniperlmain.c
regen/mk_invlists.pl Generates charclass_invlists.h
Expand Down
1 change: 1 addition & 0 deletions Makefile.SH
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ CHMOD_W = chmod +w
# regcomp.pl: regnodes.h
# warnings.pl: warnings.h lib/warnings.pm
# feature.pl: feature.h lib/feature.pm
# locale.pl: locale_table.h
# The correct versions should be already supplied with the perl kit,
# in case you don't have perl available.
# To force them to be regenerated, run
Expand Down
15 changes: 12 additions & 3 deletions locale_table.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/* locale_entry.h
/* -*- mode: C; buffer-read-only: t -*-
*
* Copyright (C) 2023 by Larry Wall and others
* locale_table.h
*
* Copyright (C) 2023, 2024 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
* This defines a macro for each individual locale category used on the this
* !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
* This file is built by regen/locale.pl from data in regen/locale.pl.
* Any changes made here will be lost!
*/

/* This defines a macro for each individual locale category used on the this
* system. (The congomerate category LC_ALL is not included.) This
* file will be #included as the interior of various parallel arrays and in
* other constructs; each usage will re-#define the macro to generate its
Expand Down Expand Up @@ -203,3 +210,5 @@
# define USE_LOCALE_TOD
# endif
#endif

/* ex: set ro ft=c: */
1 change: 1 addition & 0 deletions regen.pl
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@
scope_types.pl
tidy_embed.pl
warnings.pl
locale.pl
123 changes: 123 additions & 0 deletions regen/locale.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/perl -w
#
# Regenerate (overwriting only if changed):
#
# locale_table.h
#
# Also accepts the standard regen_lib -q and -v args.
#
# This script is normally invoked from regen.pl.

BEGIN {
require './regen/regen_lib.pl';
}

use strict;
use warnings;

sub open_print_header {
my ($file, $quote) = @_;
return open_new($file, '>',
{ by => 'regen/locale.pl',
from => 'data in regen/locale.pl',
file => $file, style => '*',
copyright => [2023..2024],
quote => $quote });
}

my $l = open_print_header('locale_table.h');
print $l <<EOF;
/* This defines a macro for each individual locale category used on the this
* system. (The congomerate category LC_ALL is not included.) This
* file will be #included as the interior of various parallel arrays and in
* other constructs; each usage will re-#define the macro to generate its
* appropriate data.
*
* This guarantees the arrays will be parallel, and populated in the order
* given here. That order is mostly arbitrary. LC_CTYPE is first because when
* we are setting multiple categories, CTYPE often needs to match the other(s),
* and the way the code is constructed, if we set the other category first, we
* might otherwise have to set CTYPE twice.
*
* Each entry takes the token giving the category name, and either the name of
* a function to call that does specialized set up for this category when it is
* changed into, or NULL if no such set up is needed
*/
EOF

# It's not worth generalizing these further.
my %comment = ( COLLATE => <<EOF
/* Perl outsources all its collation efforts to the libc strxfrm(), so
* if it isn't available on the system, default "C" locale collation
* gets used */
EOF
);
my %extra_conditional = (
COLLATE => " || ! defined(HAS_STRXFRM)",
);


while (<DATA>) { # Read in the categories
chomp;
my ($name, $function) = split /\s*\|\s*/, $_;

my $macro_arg = $function ? $function : "NULL";
my $macro_with_func = "PERL_LOCALE_TABLE_ENTRY($name, $macro_arg)";
my $macro_sans_func = "PERL_LOCALE_TABLE_ENTRY($name, NULL)";
my ($common_macro, $macro_if_name, $macro_unless_name);
if ($macro_with_func eq $macro_sans_func) {
$common_macro = $macro_with_func;
$macro_if_name = "";
$macro_unless_name = "";
}
else {
$common_macro = "";
$macro_if_name = $macro_with_func;
$macro_unless_name = $macro_sans_func;
}

print $l "#ifdef LC_${name}\n";
print $l $comment{$name} if defined $comment{$name};
print $l "\n $common_macro\n\n" if $common_macro;

my $extra = "";
$extra = $extra_conditional{$name} if defined $extra_conditional{$name};
print $l "# if defined(NO_LOCALE) || defined(NO_LOCALE_${name})$extra\n";

print $l "\n $macro_unless_name\n\n" if $macro_unless_name;
print $l <<~EOF;
# define HAS_IGNORED_LOCALE_CATEGORIES_
# define LC_${name}_AVAIL_ 0
# else
EOF
print $l "\n $macro_if_name\n\n" if $macro_if_name;
print $l <<~EOF;
# define LC_${name}_AVAIL_ 1
# define USE_LOCALE_${name}
# endif
#endif
EOF
}

close DATA;

read_only_bottom_close_and_rename($l);

# category | update function
__DATA__
CTYPE | S_new_ctype
NUMERIC | S_new_numeric
COLLATE | S_new_collate
TIME
MESSAGES
MONETARY
ADDRESS
IDENTIFICATION
MEASUREMENT
PAPER
TELEPHONE
NAME
SYNTAX
TOD
2 changes: 1 addition & 1 deletion t/porting/regen.t
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if ( $Config{usecrosscompile} ) {
skip_all( "Not all files are available during cross-compilation" );
}

my $tests = 27; # I can't see a clean way to calculate this automatically.
my $tests = 28; # I can't see a clean way to calculate this automatically.

my %skip = ("regen_perly.pl" => [qw(perly.act perly.h perly.tab)],
"regen/keywords.pl" => [qw(keywords.c keywords.h)],
Expand Down

0 comments on commit 4110b0c

Please sign in to comment.