1 if [ ! "$_TIMEZONE_MENUS_SUBR" ]; then _TIMEZONE_MENUS_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/menus.subr
34 f_include $BSDCFG_SHARE/dialog.subr
36 ############################################################ GLOBALS
39 # Export special included variables required by awk(1) for `ENVIRON' visibility
41 export DIALOG_MENU_TAGS
43 ############################################################ FUNCTIONS
47 # Creates the tag/item ordered-pair list environment variables for the
48 # continent and country menus.
50 # Required variables [from continents.subr]:
53 # Space-separated list of continents.
55 # Desired menu text for the continent represented by *.
57 # Required variables [created by f_read_iso3166_table from iso3166.subr]:
60 # Space-separated list of 2-character country codes.
61 # country_*_name :: when country_*_nzones < 0
62 # Desired menu text for the country-zone represented by *, the 2-
63 # character country code.
65 # Required variables [created by f_read_zones from zones.subr]:
68 # Number of zones for the country represented by *, the 2-
69 # character country code. Should be -1 if the country has only
70 # one single zone, otherwise 1 or greater to indicate how many
71 # zones the country has.
72 # country_*_cont :: when country_*_nzones < 0
73 # Principal continent (or ocean) in which the country-zone
74 # represented by *, the 2-character country code, resides.
75 # country_*_cont_N :: when country_*_nzones > 0
76 # Principal continent (or ocean) in which zone-N of the country
77 # represented by * resides, the 2-character country code.
78 # country_*_descr_N :: when country_*_nzones > 0
79 # Desired submenu text for zone-N of the country represented by
80 # *, the 2-character country code.
82 # Variables created by this function:
85 # Menu-list of continents.
87 # Number of items associated with the continent represented by *,
88 # the continent identifier.
90 # 2-character country code of the Nth item in the continent menu
91 # for the continent represented by *, the continent identifier.
92 # continent_*_menu_list
93 # Menu-list of countries/zones for each continent represented by
94 # *, the continent identifier.
96 # For countries that have multiple zones, this is the submenu-
97 # list of zones for said country represented by *, the 2-
98 # character country code.
100 # This function is a two-parter. Below is the awk(1) portion of the function,
101 # afterward is the sh(1) function which utilizes the below awk script.
104 function add_zone_n_to_country_menu(tlc, n)
106 zone_title = ENVIRON["country_" tlc "_descr_" n]
107 gsub(/'\''/, "'\''\\'\'''\''", zone_title)
108 country_menu_list[tlc] = country_menu_list[tlc] \
109 ( length(country_menu_list[tlc]) > 0 ? "\n" : "" ) \
110 n " '\''" zone_title "'\''"
114 # First, count up all the countries in each continent/ocean.
115 # Be careful to count those countries which have multiple zones
116 # only once for each. NB: some countries are in multiple
119 i = split(ENVIRON["COUNTRIES"], countries, /[[:space:]]+/)
120 for (cp = 1; cp <= i; cp++)
123 title = ENVIRON["country_" tlc "_name"]
124 gsub(/'\''/, "'\''\\'\'''\''", title)
125 nzones = ENVIRON["country_" tlc "_nzones"]
128 # Country has no zones
133 # Country has only one zone
134 cont = ENVIRON["country_" tlc "_cont"]
135 nitems = ++continent_nitems[cont]
136 continent_tlc[cont,nitems] = tlc
137 continent_title[cont,nitems] = title
141 # Country has one or more zones
142 for (n = 1; n <= nzones; n++)
144 add_zone_n_to_country_menu(tlc, n)
145 cont = ENVIRON["country_" tlc "_cont_" n]
146 for (x = 1; x < n; x++)
148 contx = ENVIRON["country_"tlc"_cont_"x]
149 if (cont == contx) break
153 nitems = ++continent_nitems[cont]
154 continent_tlc[cont,nitems] = tlc
155 continent_title[cont,nitems] = title
162 tags = ENVIRON["DIALOG_MENU_TAGS"]
167 # Assemble the menu items in the menu list for each continent/ocean.
169 i = split(ENVIRON["CONTINENTS"], array, /[[:space:]]+/)
170 for (item = 1; item <= i; item++)
175 if (++tagn >= length(tags)) break
176 tag = substr(tags, tagn, 1)
177 cont_menu_list = cont_menu_list \
178 ( length(cont_menu_list) > 0 ? "\n" : "" ) \
179 "'\''" tag "'\'' '\''" \
180 ENVIRON["continent_" cont "_title"] "'\''"
182 nitems = continent_nitems[cont]
183 printf "continent_%s_nitems=%d\n", cont, nitems
186 for (n = 1; n <= nitems; n++)
188 printf "continent_%s_tlc_%d=%s\n",
189 cont, n, continent_tlc[cont,n]
191 title = continent_title[cont,n]
192 menu_list = menu_list \
193 ( length(menu_list) > 0 ? "\n" : "" ) \
194 n " '\''" title "'\''"
197 gsub(/"/, "\\\"", menu_list)
198 printf "continent_%s_menu_list=\"%s\"\n", cont, menu_list
201 gsub(/"/, "\\\"", continent_menu_list)
202 printf "continent_menu_list=\"%s\"\n", cont_menu_list
203 print "export continent_menu_list"
206 # Dump the submenus of countries with multiple zones
208 for (tlc in country_menu_list)
210 menu_list = country_menu_list[tlc]
211 gsub(/"/, "\\\"", menu_list)
212 printf "country_%s_menu_list=\"%s\"\n", tlc, menu_list
218 eval $( :| awk "$f_make_menus_awk" )
221 ############################################################ MAIN
223 f_dprintf "%s: Successfully loaded." timezone/menus.subr
225 fi # ! $_TIMEZONE_MENUS_SUBR