menus.subr revision 252980
1155408Srwatsonif [ ! "$_TIMEZONE_MENUS_SUBR" ]; then _TIMEZONE_MENUS_SUBR=1 2155408Srwatson# 3184488Srwatson# Copyright (c) 2011-2012 Devin Teske 4155408Srwatson# All rights reserved. 5155408Srwatson# 6155408Srwatson# Redistribution and use in source and binary forms, with or without 7155408Srwatson# modification, are permitted provided that the following conditions 8155408Srwatson# are met: 9155408Srwatson# 1. Redistributions of source code must retain the above copyright 10155408Srwatson# notice, this list of conditions and the following disclaimer. 11155408Srwatson# 2. Redistributions in binary form must reproduce the above copyright 12155408Srwatson# notice, this list of conditions and the following disclaimer in the 13155408Srwatson# documentation and/or other materials provided with the distribution. 14155408Srwatson# 15155408Srwatson# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16155408Srwatson# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17155408Srwatson# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18155408Srwatson# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19155408Srwatson# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20155408Srwatson# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21155408Srwatson# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22155408Srwatson# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23155408Srwatson# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24155408Srwatson# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25155408Srwatson# SUCH DAMAGE. 26155408Srwatson# 27155408Srwatson# $FreeBSD: head/usr.sbin/bsdconfig/timezone/share/menus.subr 252980 2013-07-07 18:21:30Z dteske $ 28155408Srwatson# 29155408Srwatson############################################################ INCLUDES 30178186Srwatson 31178186SrwatsonBSDCFG_SHARE="/usr/share/bsdconfig" 32178186Srwatson. $BSDCFG_SHARE/common.subr || exit 1 33155408Srwatsonf_dprintf "%s: loading includes..." timezone/menus.subr 34155408Srwatsonf_include $BSDCFG_SHARE/dialog.subr 35155408Srwatson 36155408Srwatson############################################################ GLOBALS 37155408Srwatson 38155408Srwatson# 39155408Srwatson# Export special included variables required by awk(1) for `ENVIRON' visibility 40155408Srwatson# 41155408Srwatsonexport DIALOG_MENU_TAGS 42155408Srwatson 43155408Srwatson############################################################ FUNCTIONS 44155408Srwatson 45184488Srwatson# f_make_menus 46155408Srwatson# 47155408Srwatson# Creates the tag/item ordered-pair list environment variables for the 48155408Srwatson# continent and country menus. 49155408Srwatson# 50184508Srwatson# Required variables [from continents.subr]: 51155408Srwatson# 52155408Srwatson# CONTINENTS 53155408Srwatson# Space-separated list of continents. 54155408Srwatson# continent_*_title 55156880Srwatson# Desired menu text for the continent represented by *. 56155408Srwatson# 57155408Srwatson# Required variables [created by f_read_iso3166_table from iso3166.subr]: 58155408Srwatson# 59155408Srwatson# COUNTRIES 60155408Srwatson# Space-separated list of 2-character country codes. 61155408Srwatson# country_*_name :: when country_*_nzones < 0 62159269Srwatson# Desired menu text for the country-zone represented by *, the 2- 63159269Srwatson# character country code. 64155408Srwatson# 65155408Srwatson# Required variables [created by f_read_zones from zones.subr]: 66155408Srwatson# 67155408Srwatson# country_*_nzones 68155408Srwatson# Number of zones for the country represented by *, the 2- 69155408Srwatson# character country code. Should be -1 if the country has only 70155408Srwatson# one single zone, otherwise 1 or greater to indicate how many 71155408Srwatson# zones the country has. 72174894Swkoszek# country_*_cont :: when country_*_nzones < 0 73159269Srwatson# Principal continent (or ocean) in which the country-zone 74155408Srwatson# represented by *, the 2-character country code, resides. 75155408Srwatson# country_*_cont_N :: when country_*_nzones > 0 76155408Srwatson# Principal continent (or ocean) in which zone-N of the country 77155408Srwatson# represented by * resides, the 2-character country code. 78156883Srwatson# country_*_descr_N :: when country_*_nzones > 0 79156880Srwatson# Desired submenu text for zone-N of the country represented by 80155408Srwatson# *, the 2-character country code. 81155408Srwatson# 82155408Srwatson# Variables created by this function: 83155408Srwatson# 84155408Srwatson# continent_menu_list 85155408Srwatson# Menu-list of continents. 86155408Srwatson# continent_*_nitems 87155408Srwatson# Number of items associated with the continent represented by *, 88155408Srwatson# the continent identifier. 89155408Srwatson# continent_*_tlc_N 90155408Srwatson# 2-character country code of the Nth item in the continent menu 91155408Srwatson# for the continent represented by *, the continent identifier. 92159269Srwatson# continent_*_menu_list 93159269Srwatson# Menu-list of countries/zones for each continent represented by 94159269Srwatson# *, the continent identifier. 95159269Srwatson# country_*_menu_list 96159269Srwatson# For countries that have multiple zones, this is the submenu- 97159269Srwatson# list of zones for said country represented by *, the 2- 98159269Srwatson# character country code. 99159269Srwatson# 100159269Srwatson# This function is a two-parter. Below is the awk(1) portion of the function, 101159269Srwatson# afterward is the sh(1) function which utilizes the below awk script. 102159269Srwatson# 103159269Srwatsonf_make_menus_awk=' 104159269Srwatsonfunction add_zone_n_to_country_menu(tlc, n) 105159269Srwatson{ 106159269Srwatson zone_title = ENVIRON["country_" tlc "_descr_" n] 107159269Srwatson gsub(/'\''/, "'\''\\'\'''\''", zone_title) 108159269Srwatson country_menu_list[tlc] = country_menu_list[tlc] \ 109155408Srwatson ( length(country_menu_list[tlc]) > 0 ? "\n" : "" ) \ 110155408Srwatson n " '\''" zone_title "'\''" 111155408Srwatson} 112155408SrwatsonBEGIN { 113155408Srwatson # 114155408Srwatson # First, count up all the countries in each continent/ocean. 115155408Srwatson # Be careful to count those countries which have multiple zones 116155408Srwatson # only once for each. NB: some countries are in multiple 117155408Srwatson # continents/oceans. 118155408Srwatson # 119155408Srwatson i = split(ENVIRON["COUNTRIES"], countries, /[[:space:]]+/) 120155408Srwatson for (cp = 1; cp <= i; cp++) 121184488Srwatson { 122184488Srwatson tlc = countries[cp] 123184488Srwatson title = ENVIRON["country_" tlc "_name"] 124184508Srwatson gsub(/'\''/, "'\''\\'\'''\''", title) 125184488Srwatson nzones = ENVIRON["country_" tlc "_nzones"] 126184488Srwatson if (!nzones) 127184508Srwatson { 128184508Srwatson # Country has no zones 129184508Srwatson continue 130184508Srwatson } 131184508Srwatson else if (nzones < 0) 132184508Srwatson { 133184508Srwatson # Country has only one zone 134184508Srwatson cont = ENVIRON["country_" tlc "_cont"] 135184488Srwatson nitems = ++continent_nitems[cont] 136184488Srwatson continent_tlc[cont,nitems] = tlc 137184488Srwatson continent_title[cont,nitems] = title 138184488Srwatson } 139184488Srwatson else 140184536Srwatson { 141184536Srwatson # Country has one or more zones 142184536Srwatson for (n = 1; n <= nzones; n++) 143184536Srwatson { 144184536Srwatson add_zone_n_to_country_menu(tlc, n) 145184536Srwatson cont = ENVIRON["country_" tlc "_cont_" n] 146184536Srwatson for (x = 1; x < n; x++) 147155408Srwatson { 148155408Srwatson contx = ENVIRON["country_"tlc"_cont_"x] 149184536Srwatson if (cont == contx) break 150184536Srwatson } 151155408Srwatson if (x == n) 152155408Srwatson { 153155408Srwatson nitems = ++continent_nitems[cont] 154155408Srwatson continent_tlc[cont,nitems] = tlc 155155408Srwatson continent_title[cont,nitems] = title 156159269Srwatson } 157159269Srwatson } 158159269Srwatson } 159159269Srwatson } 160159269Srwatson} 161159269SrwatsonEND { 162159269Srwatson tags = ENVIRON["DIALOG_MENU_TAGS"] 163159269Srwatson cont_menu_list = "" 164159269Srwatson tagn = 0 165159269Srwatson 166159269Srwatson # 167184508Srwatson # Assemble the menu items in the menu list for each continent/ocean. 168184508Srwatson # 169184508Srwatson i = split(ENVIRON["CONTINENTS"], array, /[[:space:]]+/) 170159269Srwatson for (item = 1; item <= i; item++) 171155408Srwatson { 172155408Srwatson cont = array[item] 173159269Srwatson if (!cont) continue 174159269Srwatson 175159269Srwatson if (++tagn >= length(tags)) break 176155408Srwatson tag = substr(tags, tagn, 1) 177155408Srwatson cont_menu_list = cont_menu_list \ 178155408Srwatson ( length(cont_menu_list) > 0 ? "\n" : "" ) \ 179184508Srwatson "'\''" tag "'\'' '\''" \ 180184508Srwatson ENVIRON["continent_" cont "_title"] "'\''" 181184508Srwatson 182184508Srwatson nitems = continent_nitems[cont] 183184508Srwatson printf "continent_%s_nitems=%d\n", cont, nitems 184184508Srwatson 185184508Srwatson menu_list = "" 186184488Srwatson for (n = 1; n <= nitems; n++) 187184508Srwatson { 188184508Srwatson printf "continent_%s_tlc_%d=%s\n", 189184508Srwatson cont, n, continent_tlc[cont,n] 190184508Srwatson 191184508Srwatson title = continent_title[cont,n] 192184508Srwatson menu_list = menu_list \ 193155408Srwatson ( length(menu_list) > 0 ? "\n" : "" ) \ 194184488Srwatson n " '\''" title "'\''" 195184488Srwatson } 196184488Srwatson 197184488Srwatson gsub(/"/, "\\\"", menu_list) 198155408Srwatson printf "continent_%s_menu_list=\"%s\"\n", cont, menu_list 199155408Srwatson } 200184488Srwatson 201155408Srwatson gsub(/"/, "\\\"", continent_menu_list) 202184488Srwatson printf "continent_menu_list=\"%s\"\n", cont_menu_list 203184488Srwatson print "export continent_menu_list" 204184488Srwatson 205184488Srwatson # 206184488Srwatson # Dump the submenus of countries with multiple zones 207184488Srwatson # 208184488Srwatson for (tlc in country_menu_list) 209184488Srwatson { 210155408Srwatson menu_list = country_menu_list[tlc] 211155408Srwatson gsub(/"/, "\\\"", menu_list) 212155408Srwatson printf "country_%s_menu_list=\"%s\"\n", tlc, menu_list 213155408Srwatson } 214155408Srwatson} 215155408Srwatson' 216155408Srwatsonf_make_menus() 217155408Srwatson{ 218155408Srwatson eval $( :| awk "$f_make_menus_awk" ) 219155408Srwatson} 220155408Srwatson 221155408Srwatson############################################################ MAIN 222155408Srwatson 223155408Srwatsonf_dprintf "%s: Successfully loaded." timezone/menus.subr 224155408Srwatson 225155408Srwatsonfi # ! $_TIMEZONE_MENUS_SUBR 226161582Srwatson