xref: /freebsd/usr.sbin/bsdconfig/timezone/share/menus.subr (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1ab2043b8SDevin Teskeif [ ! "$_TIMEZONE_MENUS_SUBR" ]; then _TIMEZONE_MENUS_SUBR=1
2ab2043b8SDevin Teske#
3ab2043b8SDevin Teske# Copyright (c) 2011-2012 Devin Teske
4f8ea072aSDevin Teske# All rights reserved.
5ab2043b8SDevin Teske#
6ab2043b8SDevin Teske# Redistribution and use in source and binary forms, with or without
7ab2043b8SDevin Teske# modification, are permitted provided that the following conditions
8ab2043b8SDevin Teske# are met:
9ab2043b8SDevin Teske# 1. Redistributions of source code must retain the above copyright
10ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer.
11ab2043b8SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
12ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer in the
13ab2043b8SDevin Teske#    documentation and/or other materials provided with the distribution.
14ab2043b8SDevin Teske#
15ab2043b8SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
168e37a7c8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17ab2043b8SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ab2043b8SDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ab2043b8SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ab2043b8SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ab2043b8SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ab2043b8SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ab2043b8SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ab2043b8SDevin Teske# SUCH DAMAGE.
26ab2043b8SDevin Teske#
27ab2043b8SDevin Teske#
28ab2043b8SDevin Teske############################################################ INCLUDES
29ab2043b8SDevin Teske
30ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
31ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3256961fd7SDevin Teskef_dprintf "%s: loading includes..." timezone/menus.subr
33ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
34ab2043b8SDevin Teske
35ab2043b8SDevin Teske############################################################ GLOBALS
36ab2043b8SDevin Teske
37ab2043b8SDevin Teske#
38ab2043b8SDevin Teske# Export special included variables required by awk(1) for `ENVIRON' visibility
39ab2043b8SDevin Teske#
40ab2043b8SDevin Teskeexport DIALOG_MENU_TAGS
41ab2043b8SDevin Teske
42ab2043b8SDevin Teske############################################################ FUNCTIONS
43ab2043b8SDevin Teske
44ab2043b8SDevin Teske# f_make_menus
45ab2043b8SDevin Teske#
46ab2043b8SDevin Teske# Creates the tag/item ordered-pair list environment variables for the
47ab2043b8SDevin Teske# continent and country menus.
48ab2043b8SDevin Teske#
49ab2043b8SDevin Teske# Required variables [from continents.subr]:
50ab2043b8SDevin Teske#
51ab2043b8SDevin Teske# 	CONTINENTS
52ab2043b8SDevin Teske# 		Space-separated list of continents.
53ab2043b8SDevin Teske# 	continent_*_title
54ab2043b8SDevin Teske# 		Desired menu text for the continent represented by *.
55ab2043b8SDevin Teske#
56ab2043b8SDevin Teske# Required variables [created by f_read_iso3166_table from iso3166.subr]:
57ab2043b8SDevin Teske#
58ab2043b8SDevin Teske# 	COUNTRIES
59ab2043b8SDevin Teske# 		Space-separated list of 2-character country codes.
60ab2043b8SDevin Teske# 	country_*_name :: when country_*_nzones < 0
61ab2043b8SDevin Teske# 		Desired menu text for the country-zone represented by *, the 2-
62ab2043b8SDevin Teske# 		character country code.
63ab2043b8SDevin Teske#
64ab2043b8SDevin Teske# Required variables [created by f_read_zones from zones.subr]:
65ab2043b8SDevin Teske#
66ab2043b8SDevin Teske# 	country_*_nzones
67ab2043b8SDevin Teske# 		Number of zones for the country represented by *, the 2-
68ab2043b8SDevin Teske# 		character country code. Should be -1 if the country has only
69ab2043b8SDevin Teske# 		one single zone, otherwise 1 or greater to indicate how many
70ab2043b8SDevin Teske# 		zones the country has.
71ab2043b8SDevin Teske# 	country_*_cont :: when country_*_nzones < 0
72ab2043b8SDevin Teske# 		Principal continent (or ocean) in which the country-zone
73ab2043b8SDevin Teske# 		represented by *, the 2-character country code, resides.
74ab2043b8SDevin Teske# 	country_*_cont_N :: when country_*_nzones > 0
75ab2043b8SDevin Teske# 		Principal continent (or ocean) in which zone-N of the country
76ab2043b8SDevin Teske# 		represented by * resides, the 2-character country code.
77ab2043b8SDevin Teske# 	country_*_descr_N :: when country_*_nzones > 0
78ab2043b8SDevin Teske# 		Desired submenu text for zone-N of the country represented by
79ab2043b8SDevin Teske# 		*, the 2-character country code.
80ab2043b8SDevin Teske#
81ab2043b8SDevin Teske# Variables created by this function:
82ab2043b8SDevin Teske#
83ab2043b8SDevin Teske# 	continent_menu_list
84ab2043b8SDevin Teske# 		Menu-list of continents.
85ab2043b8SDevin Teske# 	continent_*_nitems
86ab2043b8SDevin Teske# 		Number of items associated with the continent represented by *,
87ab2043b8SDevin Teske# 		the continent identifier.
88ab2043b8SDevin Teske# 	continent_*_tlc_N
89ab2043b8SDevin Teske# 		2-character country code of the Nth item in the continent menu
90ab2043b8SDevin Teske# 		for the continent represented by *, the continent identifier.
91ab2043b8SDevin Teske# 	continent_*_menu_list
92ab2043b8SDevin Teske# 		Menu-list of countries/zones for each continent represented by
93ab2043b8SDevin Teske# 		*, the continent identifier.
94ab2043b8SDevin Teske# 	country_*_menu_list
95ab2043b8SDevin Teske# 		For countries that have multiple zones, this is the submenu-
96ab2043b8SDevin Teske# 		list of zones for said country represented by *, the 2-
97ab2043b8SDevin Teske# 		character country code.
98ab2043b8SDevin Teske#
99ab2043b8SDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
100ab2043b8SDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
101ab2043b8SDevin Teske#
102ab2043b8SDevin Teskef_make_menus_awk='
103ab2043b8SDevin Teskefunction add_zone_n_to_country_menu(tlc, n)
104ab2043b8SDevin Teske{
105ab2043b8SDevin Teske	zone_title = ENVIRON["country_" tlc "_descr_" n]
106*bc3f5ec9SDevin Teske	gsub(/'\''/, "'\''\\'\'\''", zone_title)
107ab2043b8SDevin Teske	country_menu_list[tlc] = country_menu_list[tlc] \
108ab2043b8SDevin Teske		( length(country_menu_list[tlc]) > 0 ? "\n" : "" ) \
109ab2043b8SDevin Teske		n " '\''" zone_title "'\''"
110ab2043b8SDevin Teske}
111ab2043b8SDevin TeskeBEGIN {
112ab2043b8SDevin Teske	#
113ab2043b8SDevin Teske	# First, count up all the countries in each continent/ocean.
114ab2043b8SDevin Teske	# Be careful to count those countries which have multiple zones
115ab2043b8SDevin Teske	# only once for each.  NB: some countries are in multiple
116ab2043b8SDevin Teske	# continents/oceans.
117ab2043b8SDevin Teske	#
118ab2043b8SDevin Teske	i = split(ENVIRON["COUNTRIES"], countries, /[[:space:]]+/)
119ab2043b8SDevin Teske	for (cp = 1; cp <= i; cp++)
120ab2043b8SDevin Teske	{
121ab2043b8SDevin Teske		tlc = countries[cp]
122ab2043b8SDevin Teske		title = ENVIRON["country_" tlc "_name"]
123*bc3f5ec9SDevin Teske		gsub(/'\''/, "'\''\\'\'\''", title)
124ab2043b8SDevin Teske		nzones = ENVIRON["country_" tlc "_nzones"]
125ab2043b8SDevin Teske		if (!nzones)
126ab2043b8SDevin Teske		{
127ab2043b8SDevin Teske			# Country has no zones
128ab2043b8SDevin Teske			continue
129ab2043b8SDevin Teske		}
130ab2043b8SDevin Teske		else if (nzones < 0)
131ab2043b8SDevin Teske		{
132ab2043b8SDevin Teske			# Country has only one zone
133ab2043b8SDevin Teske			cont = ENVIRON["country_" tlc "_cont"]
134ab2043b8SDevin Teske			nitems = ++continent_nitems[cont]
135ab2043b8SDevin Teske			continent_tlc[cont,nitems] = tlc
136ab2043b8SDevin Teske			continent_title[cont,nitems] = title
137ab2043b8SDevin Teske		}
138ab2043b8SDevin Teske		else
139ab2043b8SDevin Teske		{
140ab2043b8SDevin Teske			# Country has one or more zones
141ab2043b8SDevin Teske			for (n = 1; n <= nzones; n++)
142ab2043b8SDevin Teske			{
143ab2043b8SDevin Teske				add_zone_n_to_country_menu(tlc, n)
144ab2043b8SDevin Teske				cont = ENVIRON["country_" tlc "_cont_" n]
145ab2043b8SDevin Teske				for (x = 1; x < n; x++)
146ab2043b8SDevin Teske				{
147ab2043b8SDevin Teske					contx = ENVIRON["country_"tlc"_cont_"x]
148ab2043b8SDevin Teske					if (cont == contx) break
149ab2043b8SDevin Teske				}
150ab2043b8SDevin Teske				if (x == n)
151ab2043b8SDevin Teske				{
152ab2043b8SDevin Teske					nitems = ++continent_nitems[cont]
153ab2043b8SDevin Teske					continent_tlc[cont,nitems] = tlc
154ab2043b8SDevin Teske					continent_title[cont,nitems] = title
155ab2043b8SDevin Teske				}
156ab2043b8SDevin Teske			}
157ab2043b8SDevin Teske		}
158ab2043b8SDevin Teske	}
159ab2043b8SDevin Teske}
160ab2043b8SDevin TeskeEND {
161ab2043b8SDevin Teske	tags = ENVIRON["DIALOG_MENU_TAGS"]
162ab2043b8SDevin Teske	cont_menu_list = ""
163ab2043b8SDevin Teske	tagn = 0
164ab2043b8SDevin Teske
165ab2043b8SDevin Teske	#
166ab2043b8SDevin Teske	# Assemble the menu items in the menu list for each continent/ocean.
167ab2043b8SDevin Teske	#
168ab2043b8SDevin Teske	i = split(ENVIRON["CONTINENTS"], array, /[[:space:]]+/)
169ab2043b8SDevin Teske	for (item = 1; item <= i; item++)
170ab2043b8SDevin Teske	{
171ab2043b8SDevin Teske		cont = array[item]
172ab2043b8SDevin Teske		if (!cont) continue
173ab2043b8SDevin Teske
174ab2043b8SDevin Teske		if (++tagn >= length(tags)) break
175ab2043b8SDevin Teske		tag = substr(tags, tagn, 1)
176ab2043b8SDevin Teske		cont_menu_list = cont_menu_list \
177ab2043b8SDevin Teske			( length(cont_menu_list) > 0 ? "\n" : "" ) \
178ab2043b8SDevin Teske			"'\''" tag "'\'' '\''" \
179ab2043b8SDevin Teske			ENVIRON["continent_" cont "_title"] "'\''"
180ab2043b8SDevin Teske
181ab2043b8SDevin Teske		nitems = continent_nitems[cont]
182ab2043b8SDevin Teske		printf "continent_%s_nitems=%d\n", cont, nitems
183ab2043b8SDevin Teske
184ab2043b8SDevin Teske		menu_list = ""
185ab2043b8SDevin Teske		for (n = 1; n <= nitems; n++)
186ab2043b8SDevin Teske		{
187ab2043b8SDevin Teske			printf "continent_%s_tlc_%d=%s\n",
188ab2043b8SDevin Teske			       cont, n, continent_tlc[cont,n]
189ab2043b8SDevin Teske
190ab2043b8SDevin Teske			title = continent_title[cont,n]
191ab2043b8SDevin Teske			menu_list = menu_list \
192ab2043b8SDevin Teske				( length(menu_list) > 0 ? "\n" : "" ) \
193ab2043b8SDevin Teske				n " '\''" title "'\''"
194ab2043b8SDevin Teske		}
195ab2043b8SDevin Teske
196ab2043b8SDevin Teske		gsub(/"/, "\\\"", menu_list)
197ab2043b8SDevin Teske		printf "continent_%s_menu_list=\"%s\"\n", cont, menu_list
198ab2043b8SDevin Teske	}
199ab2043b8SDevin Teske
200ab2043b8SDevin Teske	gsub(/"/, "\\\"", continent_menu_list)
201ab2043b8SDevin Teske	printf "continent_menu_list=\"%s\"\n", cont_menu_list
202ab2043b8SDevin Teske	print "export continent_menu_list"
203ab2043b8SDevin Teske
204ab2043b8SDevin Teske	#
205ab2043b8SDevin Teske	# Dump the submenus of countries with multiple zones
206ab2043b8SDevin Teske	#
207ab2043b8SDevin Teske	for (tlc in country_menu_list)
208ab2043b8SDevin Teske	{
209ab2043b8SDevin Teske		menu_list = country_menu_list[tlc]
210ab2043b8SDevin Teske		gsub(/"/, "\\\"", menu_list)
211ab2043b8SDevin Teske		printf "country_%s_menu_list=\"%s\"\n", tlc, menu_list
212ab2043b8SDevin Teske	}
213ab2043b8SDevin Teske}
214ab2043b8SDevin Teske'
215ab2043b8SDevin Teskef_make_menus()
216ab2043b8SDevin Teske{
217ab2043b8SDevin Teske	eval $( :| awk "$f_make_menus_awk" )
218ab2043b8SDevin Teske}
219ab2043b8SDevin Teske
22056961fd7SDevin Teske############################################################ MAIN
22156961fd7SDevin Teske
22256961fd7SDevin Teskef_dprintf "%s: Successfully loaded." timezone/menus.subr
22356961fd7SDevin Teske
224ab2043b8SDevin Teskefi # ! $_TIMEZONE_MENUS_SUBR
225