1 if [ ! "$_TIMEZONE_ISO3166_SUBR" ]; then _TIMEZONE_ISO3166_SUBR=1
3 # Copyright (c) 2011-2012 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." timezone/iso3166.subr
35 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="090.timezone"
36 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
38 ############################################################ CONFIGURATION
43 _PATH_ISO3166="/usr/share/misc/iso3166"
46 # Export required i18n messages for awk(1) ENVIRON visibility
48 export msg_country_code_multiply_defined
49 export msg_invalid_code
50 export msg_invalid_format
52 ############################################################ FUNCTIONS
54 # f_read_iso3166_table
56 # Read the ISO 3166 country code database in _PATH_ISO3166:
57 # /usr/share/misc/iso3166 on FreeBSD
58 # /usr/share/zoneinfo/iso3166.tab on Linux, Mac OS X, and Cygwin
60 # The format of this file on FreeBSD is:
61 # two three number name
63 # The format of this file on Linux, Mac OS X, and Cygwin is:
66 # With each of the following elements (described below) being separated by a
67 # single tab character:
69 # two ISO 3166 2-character country code
70 # three ISO 3166 3-character country code (if provided)
71 # number ISO 3166 numeric country code (if provided)
72 # name Human-readable country name (may contain spaces)
74 # Variables created by this function:
77 # A space-separated list of 2-character country codes.
79 # The country `name' (as described above).
81 # where CODE is the 2-character country code.
83 # This function is a two-parter. Below is the awk(1) portion of the function,
84 # afterward is the sh(1) function which utilizes the below awk script.
86 f_read_iso3166_table_awk='
87 # Variables that should be defined on the invocation line:
88 # -v progname="progname"
94 function die(fmt, argc, argv)
96 printf "f_die 1 \"%%s: %s\" \"%s\"", fmt, progname
97 for (n = 1; n <= argc; n++)
98 printf " \"%s\"", argv[n]
103 function add_country(tlc, name)
105 if (country_name[tlc])
111 die(ENVIRON["msg_country_code_multiply_defined"], 4, argv)
114 country_name[tlc] = name
116 function print_country_name(tlc)
118 name = country_name[tlc]
119 gsub(/"/, "\\\"", name)
120 printf "country_%s_name=\"%s\"\n", tlc, name
121 printf "export country_%s_name\n", tlc
130 # Split the current record (on TAB) into an array
131 split($0, line, /\t/)
133 # Get the ISO3166-1 (Alpha 1) 2-letter country code
137 # Validate the two-character country code
139 if (length(tlc) != 2)
143 die(ENVIRON["msg_invalid_format"], 2, argv)
145 if (!match(tlc, /^[A-Z][A-Z]$/))
150 die(ENVIRON["msg_invalid_code"], 3, argv)
154 # Calculate the substr start-position of the name
158 if (FILENAME ~ /\.tab$/)
163 # Validate field-length of 2nd/3rd columns while we are here
165 if (n > 1 && length(line[n]) != 3)
169 die(ENVIRON["msg_invalid_format"], 2, argv)
172 name_start += length(line[n]) + 1
176 name = substr($0, name_start + 1)
178 add_country(tlc, name)
182 for (tlc in country_name)
184 list = list (length(list) > 0 ? " " : "") tlc
185 print_country_name(tlc)
187 printf "COUNTRIES=\"%s\"\n", list
188 print "export COUNTRIES"
191 f_read_iso3166_table()
193 eval $( awk -v progname="$pgm" \
194 "$f_read_iso3166_table_awk" \
198 ############################################################ MAIN
200 f_dprintf "%s: Successfully loaded." timezone/iso3166.subr
202 fi # ! $_TIMEZONE_ISO3166_SUBR