]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/bsdconfig/timezone/share/countries.subr
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.sbin / bsdconfig / timezone / share / countries.subr
1 if [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1
2 #
3 # Copyright (c) 2011-2015 Devin Teske
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
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.
14 #
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
25 # SUCH DAMAGE.
26 #
27 # $FreeBSD$
28 #
29 ############################################################ FUNCTIONS
30
31 # f_country $code $property [$var_to_set]
32 #
33 # Returns a single property of a given country. Available properties are:
34 #
35 #       name         Name of the country as read from _PATH_ISO3166.
36 #       nzones       Number of zones within the country (-1 if country has
37 #                    only a single zone).
38 #       filename     The filename portion of the TZ field (after the `/') as
39 #                    read from _PATH_ZONETAB.
40 #       cont         The principal continent in which the country lies (appears
41 #                    before the `/' in the TZ field of _PATH_ZONETAB).
42 #       filename_N   Like filename, but for the Nth zone when the country has
43 #                    multiple zones (nzones > 0).
44 #       cont_N       Like cont, but for the Nth zone when the country has
45 #                    multiple zones (nzones > 0).
46 #       descr_N      Like name, but for the Nth zone when the country has
47 #                    multiple zones (nzones > 0)
48 #
49 # If $var_to_set is missing or NULL, the value of $var_to_get is printed to
50 # standard output for capturing in a sub-shell (which is less-recommended
51 # because of performance degredation; for example, when called in a loop).
52 #
53 f_country()
54 {
55         f_getvar "country_${1}_$2" $3
56 }
57
58 # f_sort_countries
59 #
60 # Sorts alphabetically the 2-character country codes listed in $COUNTRIES based
61 # on the name of each country.
62 #
63 # This function is a two-parter. Below is the awk(1) portion of the function,
64 # afterward is the sh(1) function which utilizes the below awk script.
65 #
66 f_sort_countries_awk='
67 function _asorti(src, dest)
68 {
69         k = nitems = 0
70         for (i in src) dest[++nitems] = i
71         for (i = 1; i <= nitems; k = i++) {
72                 idx = dest[i]
73                 while ((k > 0) && (dest[k] > idx)) {
74                         dest[k+1] = dest[k]; k--
75                 }
76                 dest[k+1] = idx
77         }
78         return nitems
79 }
80 BEGIN {
81         split(ENVIRON["COUNTRIES"], array, /[[:space:]]+/)
82         for (item in array)
83         {
84                 tlc = array[item]
85                 name = ENVIRON["country_" tlc "_name"]
86                 countries[name] = tlc
87         }
88         n = _asorti(countries, sorted_countries)
89         for (i = 1; i <= n; i++)
90                 print countries[sorted_countries[i]]
91         exit
92 }
93 '
94 f_sort_countries()
95 {
96         export COUNTRIES # for awk(1) ENVIRON[] visibility
97         COUNTRIES=$( awk "$f_sort_countries_awk" )
98         export COUNTRIES # Pedantic
99 }
100
101 ############################################################ MAIN
102
103 f_dprintf "%s: Successfully loaded." timezone/countries.subr
104
105 fi # ! $_TIMEZONE_COUNTRIES_SUBR