1#!/usr/bin/env bash
2
3# This script is used to generate a gconv-modules file that takes into
4# account only the gconv modules installed by Buildroot, and generates
5# a stripped-down gconv-moduels file on its stdout.
6# It takes two arguments:
7#   $1: the directory where to look for gconv modules definitions
8#   $2: a space-separated list of gconv modules that were actually
9#       installed
10
11# Starting with glibc-2.34, modules definitions are located in multiple
12# files:
13#   ${1}/gconv-modules
14#   ${1}/gconv-modules.d/*.conf
15
16# The format of gconv-modules is precisely documented in the
17# file itself. It consists of two different directives:
18#   module  FROMSET  TOSET  FILENAME  COST
19#   alias   ALIAS  REALNAME
20# and that's what this script parses and generates.
21#
22# There are two kinds of 'module' directives:
23#   - the first defines conversion of a charset to/from INTERNAL representation
24#   - the second defines conversion of a charset to/from another charset
25# we handle each with slightly different code, since the second never has
26# associated aliases.
27
28for f in ${1}/gconv-modules ${1}/gconv-modules.d/*.conf; do
29    [ -e "${f}" ] || continue
30    cat "${f}"
31done \
32|awk -v files="${2}" '
33$1 == "alias" {
34    aliases[$3] = aliases[$3] " " $2;
35}
36$1 == "module" && $2 != "INTERNAL" && $3 == "INTERNAL" {
37    file2internals[$4] = file2internals[$4] " " $2;
38    mod2cost[$2] = $5;
39}
40$1 == "module" && $2 != "INTERNAL" && $3 != "INTERNAL" {
41    file2cset[$4] = file2cset[$4] " " $2 ":" $3;
42    mod2cost[$2] = $5;
43}
44
45END {
46    nb_files = split(files, all_files);
47    for(f = 1; f <= nb_files; f++) {
48        file = all_files[f];
49        printf("# Modules and aliases for: %s\n", file);
50        nb_mods = split(file2internals[file], mods);
51        for(i = 1; i <= nb_mods; i++) {
52            nb_aliases = split(aliases[mods[i]], mod_aliases);
53            for(j = 1; j <= nb_aliases; j++) {
54                printf("alias\t%s\t%s\n", mod_aliases[j], mods[i]);
55            }
56            printf("module\t%s\t%s\t%s\t%d\n", mods[i], "INTERNAL", file, mod2cost[mods[i]]);
57            printf("module\t%s\t%s\t%s\t%d\n", "INTERNAL", mods[i], file, mod2cost[mods[i]]);
58            printf("\n" );
59        }
60        printf("%s", nb_mods != 0 ? "\n" : "");
61        nb_csets = split(file2cset[file], csets);
62        for(i = 1; i <= nb_csets; i++) {
63            split(csets[i], cs, ":");
64            printf("module\t%s\t%s\t%s\t%d\n", cs[1], cs[2], file, mod2cost[cs[1]]);
65        }
66        printf("%s", nb_csets != 0 ? "\n\n" : "");
67    }
68}
69'
70