summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsdconfig/timezone/share/menus.subr
blob: 3b6db6b9941be2f6ddc23c8e771e7cd58681f035 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
if [ ! "$_TIMEZONE_MENUS_SUBR" ]; then _TIMEZONE_MENUS_SUBR=1
#
# Copyright (c) 2011-2012 Devin Teske
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
############################################################ INCLUDES

BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
f_dprintf "%s: loading includes..." timezone/menus.subr
f_include $BSDCFG_SHARE/dialog.subr

############################################################ GLOBALS

#
# Export special included variables required by awk(1) for `ENVIRON' visibility
#
export DIALOG_MENU_TAGS

############################################################ FUNCTIONS

# f_make_menus
#
# Creates the tag/item ordered-pair list environment variables for the
# continent and country menus.
#
# Required variables [from continents.subr]:
#
# 	CONTINENTS
# 		Space-separated list of continents.
# 	continent_*_title
# 		Desired menu text for the continent represented by *.
#
# Required variables [created by f_read_iso3166_table from iso3166.subr]:
#
# 	COUNTRIES
# 		Space-separated list of 2-character country codes.
# 	country_*_name :: when country_*_nzones < 0
# 		Desired menu text for the country-zone represented by *, the 2-
# 		character country code.
#
# Required variables [created by f_read_zones from zones.subr]:
#
# 	country_*_nzones
# 		Number of zones for the country represented by *, the 2-
# 		character country code. Should be -1 if the country has only
# 		one single zone, otherwise 1 or greater to indicate how many
# 		zones the country has.
# 	country_*_cont :: when country_*_nzones < 0
# 		Principal continent (or ocean) in which the country-zone
# 		represented by *, the 2-character country code, resides.
# 	country_*_cont_N :: when country_*_nzones > 0
# 		Principal continent (or ocean) in which zone-N of the country
# 		represented by * resides, the 2-character country code.
# 	country_*_descr_N :: when country_*_nzones > 0
# 		Desired submenu text for zone-N of the country represented by
# 		*, the 2-character country code.
#
# Variables created by this function:
#
# 	continent_menu_list
# 		Menu-list of continents.
# 	continent_*_nitems
# 		Number of items associated with the continent represented by *,
# 		the continent identifier.
# 	continent_*_tlc_N
# 		2-character country code of the Nth item in the continent menu
# 		for the continent represented by *, the continent identifier.
# 	continent_*_menu_list
# 		Menu-list of countries/zones for each continent represented by
# 		*, the continent identifier.
# 	country_*_menu_list
# 		For countries that have multiple zones, this is the submenu-
# 		list of zones for said country represented by *, the 2-
# 		character country code.
#
# This function is a two-parter. Below is the awk(1) portion of the function,
# afterward is the sh(1) function which utilizes the below awk script.
#
f_make_menus_awk='
function add_zone_n_to_country_menu(tlc, n)
{
	zone_title = ENVIRON["country_" tlc "_descr_" n]
	gsub(/'\''/, "'\''\\'\'''\''", zone_title)
	country_menu_list[tlc] = country_menu_list[tlc] \
		( length(country_menu_list[tlc]) > 0 ? "\n" : "" ) \
		n " '\''" zone_title "'\''"
}
BEGIN {
	#
	# First, count up all the countries in each continent/ocean.
	# Be careful to count those countries which have multiple zones
	# only once for each.  NB: some countries are in multiple
	# continents/oceans.
	#
	i = split(ENVIRON["COUNTRIES"], countries, /[[:space:]]+/)
	for (cp = 1; cp <= i; cp++)
	{
		tlc = countries[cp]
		title = ENVIRON["country_" tlc "_name"]
		gsub(/'\''/, "'\''\\'\'''\''", title)
		nzones = ENVIRON["country_" tlc "_nzones"]
		if (!nzones)
		{
			# Country has no zones
			continue
		}
		else if (nzones < 0)
		{
			# Country has only one zone
			cont = ENVIRON["country_" tlc "_cont"]
			nitems = ++continent_nitems[cont]
			continent_tlc[cont,nitems] = tlc
			continent_title[cont,nitems] = title
		}
		else
		{
			# Country has one or more zones
			for (n = 1; n <= nzones; n++)
			{
				add_zone_n_to_country_menu(tlc, n)
				cont = ENVIRON["country_" tlc "_cont_" n]
				for (x = 1; x < n; x++)
				{
					contx = ENVIRON["country_"tlc"_cont_"x]
					if (cont == contx) break
				}
				if (x == n)
				{
					nitems = ++continent_nitems[cont]
					continent_tlc[cont,nitems] = tlc
					continent_title[cont,nitems] = title
				}
			}
		}
	}
}
END {
	tags = ENVIRON["DIALOG_MENU_TAGS"]
	cont_menu_list = ""
	tagn = 0

	#
	# Assemble the menu items in the menu list for each continent/ocean.
	#
	i = split(ENVIRON["CONTINENTS"], array, /[[:space:]]+/)
	for (item = 1; item <= i; item++)
	{
		cont = array[item]
		if (!cont) continue

		if (++tagn >= length(tags)) break
		tag = substr(tags, tagn, 1)
		cont_menu_list = cont_menu_list \
			( length(cont_menu_list) > 0 ? "\n" : "" ) \
			"'\''" tag "'\'' '\''" \
			ENVIRON["continent_" cont "_title"] "'\''"

		nitems = continent_nitems[cont]
		printf "continent_%s_nitems=%d\n", cont, nitems

		menu_list = ""
		for (n = 1; n <= nitems; n++)
		{
			printf "continent_%s_tlc_%d=%s\n",
			       cont, n, continent_tlc[cont,n]

			title = continent_title[cont,n]
			menu_list = menu_list \
				( length(menu_list) > 0 ? "\n" : "" ) \
				n " '\''" title "'\''"
		}

		gsub(/"/, "\\\"", menu_list)
		printf "continent_%s_menu_list=\"%s\"\n", cont, menu_list
	}

	gsub(/"/, "\\\"", continent_menu_list)
	printf "continent_menu_list=\"%s\"\n", cont_menu_list
	print "export continent_menu_list"

	#
	# Dump the submenus of countries with multiple zones
	#
	for (tlc in country_menu_list)
	{
		menu_list = country_menu_list[tlc]
		gsub(/"/, "\\\"", menu_list)
		printf "country_%s_menu_list=\"%s\"\n", tlc, menu_list
	}
}
'
f_make_menus()
{
	eval $( :| awk "$f_make_menus_awk" )
}

############################################################ MAIN

f_dprintf "%s: Successfully loaded." timezone/menus.subr

fi # ! $_TIMEZONE_MENUS_SUBR
OpenPOWER on IntegriCloud