common.subr (242107) | common.subr (243112) |
---|---|
1if [ ! "$_COMMON_SUBR" ]; then _COMMON_SUBR=1 2# 3# Copyright (c) 2012 Ron McDowell 4# Copyright (c) 2012 Devin Teske 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions --- 11 unchanged lines hidden (view full) --- 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# | 1if [ ! "$_COMMON_SUBR" ]; then _COMMON_SUBR=1 2# 3# Copyright (c) 2012 Ron McDowell 4# Copyright (c) 2012 Devin Teske 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions --- 11 unchanged lines hidden (view full) --- 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# |
28# $FreeBSD: head/usr.sbin/bsdconfig/share/common.subr 242107 2012-10-26 00:31:25Z dteske $ | 28# $FreeBSD: head/usr.sbin/bsdconfig/share/common.subr 243112 2012-11-16 00:59:11Z dteske $ |
29# 30############################################################ GLOBALS 31 32# 33# Program name 34# 35pgm="${0##*/}" 36 --- 229 unchanged lines hidden (view full) --- 266 shift 2 267 done 268 269 f_err "%s\n" "$usage" 270 271 exit $FAILURE 272} 273 | 29# 30############################################################ GLOBALS 31 32# 33# Program name 34# 35pgm="${0##*/}" 36 --- 229 unchanged lines hidden (view full) --- 266 shift 2 267 done 268 269 f_err "%s\n" "$usage" 270 271 exit $FAILURE 272} 273 |
274# f_index_menu_selection $file $pgm | 274# f_index_file $keyword |
275# | 275# |
276# Process $file looking for $menu_selection values that correspond to $pgm. | 276# Process all INDEX files known to bsdconfig and return the path to first file 277# containing a menu_selection line with a keyword portion matching $keyword. 278# 279# If $LANG or $LC_ALL (in order of preference, respectively) is set, 280# "INDEX.encoding" files will be searched first. 281# 282# If no file is found, error status is returned along with the NULL string. 283# 284# This function is a two-parter. Below is the awk(1) portion of the function, 285# afterward is the sh(1) function which utilizes the below awk script. 286# 287f_index_file_awk=' 288# Variables that should be defined on the invocation line: 289# -v keyword="keyword" 290BEGIN { found = 0 } 291( $0 ~ "^menu_selection=\"" keyword "\\|" ) { 292 print FILENAME 293 found++ 294 exit 295} 296END { exit ! found } 297' 298f_index_file() 299{ 300 local keyword="$1" 301 local lang="${LANG:-$LC_ALL}" 302 303 f_dprintf "lang=[$lang]" 304 305 if [ "$lang" ]; then 306 awk -v keyword="$keyword" "$f_index_file_awk" \ 307 $BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX.$lang && 308 return 309 # No match, fall-thru to non-i18n sources 310 fi 311 awk -v keyword="$keyword" "$f_index_file_awk" \ 312 $BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX 313} 314 315# f_index_menusel_keyword $indexfile $pgm 316# 317# Process $indexfile and return only the keyword portion of the menu_selection 318# line with a command portion matching $pgm. 319# |
277# This function is for internationalization (i18n) mapping of the on-disk 278# scriptname ($pgm) into the localized language (given language-specific | 320# This function is for internationalization (i18n) mapping of the on-disk 321# scriptname ($pgm) into the localized language (given language-specific |
279# $file). If $LANG or $LC_ALL (in orderder of preference, respectively) is set, 280# ".encoding" will automatically be appended as a suffix to the provided $file 281# pathname. | 322# $indexfile). If $LANG or $LC_ALL (in orderder of preference, respectively) is 323# set, ".encoding" will automatically be appended as a suffix to the provided 324# $indexfile pathname. |
282# | 325# |
283# If, within $file, multiple $menu_selection values map to $pgm, only the first 284# one will be returned. If no mapping can be made, the NULL string is returned. | 326# If, within $indexfile, multiple $menu_selection values map to $pgm, only the 327# first one will be returned. If no mapping can be made, the NULL string is 328# returned. |
285# | 329# |
286# If $file does not exist, error status is returned along with the NULL string. | 330# If $indexfile does not exist, error status is returned with NULL. |
287# 288# This function is a two-parter. Below is the awk(1) portion of the function, 289# afterward is the sh(1) function which utilizes the below awk script. 290# | 331# 332# This function is a two-parter. Below is the awk(1) portion of the function, 333# afterward is the sh(1) function which utilizes the below awk script. 334# |
291f_index_menusel_awk=' | 335f_index_menusel_keyword_awk=' |
292# Variables that should be defined on the invocation line: 293# -v pgm="program_name" 294# | 336# Variables that should be defined on the invocation line: 337# -v pgm="program_name" 338# |
295( $0 ~ "^menu_selection=.*\\|" pgm "\"" ) { 296 sub(/\|.*/, "") 297 sub(/^menu_selection="/, "") 298 print 299 exit | 339BEGIN { 340 prefix = "menu_selection=\"" 341 plen = length(prefix) 342 found = 0 |
300} | 343} |
344{ 345 if (!match($0, "^" prefix ".*\\|.*\"")) next 346 347 keyword = command = substr($0, plen + 1, RLENGTH - plen - 1) 348 sub(/^.*\|/, "", command) 349 sub(/\|.*$/, "", keyword) 350 351 if ( command == pgm ) 352 { 353 print keyword 354 found++ 355 exit 356 } 357} 358END { exit ! found } |
|
301' | 359' |
302f_index_menu_selection() | 360f_index_menusel_keyword() |
303{ | 361{ |
304 local file="$1" pgm="$2" | 362 local indexfile="$1" pgm="$2" |
305 local lang="${LANG:-$LC_ALL}" 306 307 f_dprintf "lang=[$lang]" 308 | 363 local lang="${LANG:-$LC_ALL}" 364 365 f_dprintf "lang=[$lang]" 366 |
309 if [ -f "$file.$lang" ]; then 310 awk -v pgm="$pgm" "$f_index_menusel_awk" "$file.$lang" || 311 exit $FAILURE 312 elif [ -f "$file" ]; then 313 awk -v pgm="$pgm" "$f_index_menusel_awk" "$file" || 314 exit $FAILURE | 367 if [ -f "$indexfile.$lang" ]; then 368 awk -v pgm="$pgm" \ 369 "$f_index_menusel_keyword_awk" \ 370 "$indexfile.$lang" 371 elif [ -f "$indexfile" ]; then 372 awk -v pgm="$pgm" \ 373 "$f_index_menusel_keyword_awk" \ 374 "$indexfile" 375 fi 376} 377 378# f_index_menusel_command $indexfile $keyword 379# 380# Process $indexfile and return only the command portion of the menu_selection 381# line with a keyword portion matching $keyword. 382# 383# This function is for mapping [possibly international] keywords into the 384# command to be executed. If $LANG or $LC_ALL (order of preference) is set, 385# ".encoding" will automatically be appended as a suffix to the provided 386# $indexfile pathname. 387# 388# If, within $indexfile, multiple $menu_selection values map to $keyword, only 389# the first one will be returned. If no mapping can be made, the NULL string is 390# returned. 391# 392# If $indexfile doesn't exist, error status is returned with NULL. 393# 394# This function is a two-parter. Below is the awk(1) portion of the function, 395# afterward is the sh(1) function which utilizes the below awk script. 396# 397f_index_menusel_command_awk=' 398# Variables that should be defined on the invocation line: 399# -v key="keyword" 400# 401BEGIN { 402 prefix = "menu_selection=\"" 403 plen = length(prefix) 404 found = 0 405} 406{ 407 if (!match($0, "^" prefix ".*\\|.*\"")) next 408 409 keyword = command = substr($0, plen + 1, RLENGTH - plen - 1) 410 sub(/^.*\|/, "", command) 411 sub(/\|.*$/, "", keyword) 412 413 if ( keyword == key ) 414 { 415 print command 416 found++ 417 exit 418 } 419} 420END { exit ! found } 421' 422f_index_menusel_command() 423{ 424 local indexfile="$1" keyword="$2" command 425 local lang="${LANG:-$LC_ALL}" 426 427 f_dprintf "lang=[$lang]" 428 429 if [ -f "$indexfile.$lang" ]; then 430 command=$( awk -v key="$keyword" \ 431 "$f_index_menusel_command_awk" \ 432 "$indexfile.$lang" ) || return $FAILURE 433 elif [ -f "$indexfile" ]; then 434 command=$( awk -v key="$keyword" \ 435 "$f_index_menusel_command_awk" \ 436 "$indexfile" ) || return $FAILURE |
315 else 316 return $FAILURE 317 fi | 437 else 438 return $FAILURE 439 fi |
440 441 # 442 # If the command pathname is not fully qualified fix-up/force to be 443 # relative to the $indexfile directory. 444 # 445 case "$command" in 446 /*) : already fully qualified ;; 447 *) 448 local indexdir="${indexfile%/*}" 449 [ "$indexdir" != "$indexfile" ] || indexdir="." 450 command="$indexdir/$command" 451 esac 452 453 echo "$command" |
|
318} 319 320############################################################ MAIN 321 322# 323# Trap signals so we can recover gracefully 324# 325trap 'f_interrupt' SIGINT 326trap 'f_die' SIGTERM SIGPIPE SIGXCPU SIGXFSZ \ 327 SIGFPE SIGTRAP SIGABRT SIGSEGV 328trap '' SIGALRM SIGPROF SIGUSR1 SIGUSR2 SIGHUP SIGVTALRM 329 330fi # ! $_COMMON_SUBR | 454} 455 456############################################################ MAIN 457 458# 459# Trap signals so we can recover gracefully 460# 461trap 'f_interrupt' SIGINT 462trap 'f_die' SIGTERM SIGPIPE SIGXCPU SIGXFSZ \ 463 SIGFPE SIGTRAP SIGABRT SIGSEGV 464trap '' SIGALRM SIGPROF SIGUSR1 SIGUSR2 SIGHUP SIGVTALRM 465 466fi # ! $_COMMON_SUBR |