From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1NIJNd-0000nI-7l for garchives@archives.gentoo.org; Wed, 09 Dec 2009 10:02:45 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id C90E7E078E for ; Wed, 9 Dec 2009 10:02:44 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 05907E066B for ; Wed, 9 Dec 2009 08:41:19 +0000 (UTC) Received: from vapier.localnet (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 75A75673DB for ; Wed, 9 Dec 2009 08:41:18 +0000 (UTC) From: Mike Frysinger Organization: wh0rd.org To: gentoo-dev@lists.gentoo.org Subject: [gentoo-dev] new eshopts_{push,pop} functions Date: Wed, 9 Dec 2009 03:41:37 -0500 User-Agent: KMail/1.12.4 (Linux/2.6.31.4; KDE/4.3.4; x86_64; ; ) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart1626510.XHFLYMNUqB"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <200912090341.38018.vapier@gentoo.org> X-Archives-Salt: ed8930b6-d7c8-467a-bd95-227d2d1dafd8 X-Archives-Hash: 8163f3986f766ec59ccf6ee35e33e817 --nextPart1626510.XHFLYMNUqB Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable i got tired of copying & pasting the shell option save/restore code, so i created two helper functions. am i missing anything before i add to eutils.eclass ? i could extend eshopts_pop to take a numeric argument for number of stack entries to pop, but i dont see much value/use. # @FUNCTION: eshopts_push # @USAGE: [options to `set`] # @DESCRIPTION: # Often times code will want to enable a shell option to change code behavi= or. # Since changing shell options can easily break other pieces of code (which # assume the default state), eshopts_push is used to (1) push the current s= hell # options onto a stack and (2) pass the specified arguments to set. # # A common example is to disable shell globbing so that special meaning/care # may be used with variables/arguments to custom functions. That would be: # @CODE # eshopts_push -o noglob # for x in ${foo} ; do # if ...some check... ; then # eshopts_pop # return 0 # fi # done # eshopts_pop # @CODE eshopts_push() { # have to assume __ESHOPTS_SAVE__ isn't screwed with # as a `declare -a` here will reset its value local i=3D${#__ESHOPTS_SAVE__[@]} __ESHOPTS_SAVE__[$i]=3D$- [[ $# -eq 0 ]] && return 0 set "$@" || die "eshopts_push: bad options to set" } # @FUNCTION: eshopts_pop # @USAGE: # @DESCRIPTION: # Restore the shell options to the state saved with the corresponding # eshopts_push call. See that function for more details. eshopts_pop() { [[ $# -ne 0 ]] && die "eshopts_pop takes no arguments" local i=3D$(( ${#__ESHOPTS_SAVE__[@]} - 1 )) [[ ${i} -eq -1 ]] && die "eshopts_{push,pop}: unbalanced pair" local s=3D${__ESHOPTS_SAVE__[$i]} unset __ESHOPTS_SAVE__[$i] set +$- || die "eshopts_pop: sanity: invalid shell settings !?" set -${s} || die "eshopts_pop: sanity: unable to restore saved shell setti= ngs" } and an example of new usage in eutils.eclass: @@ -1343,16 +1363,15 @@ check_license() { =20 # here is where we check for the licenses the user already # accepted ... if we don't find a match, we make the user accept =2D local shopts=3D$- local alic =2D set -o noglob #so that bash doesn't expand "*" + eshopts_push -o noglob # so that bash doesn't expand "*" for alic in ${ACCEPT_LICENSE} ; do if [[ ${alic} =3D=3D ${l} ]]; then =2D set +o noglob; set -${shopts} #reset old shell opts + eshopts_pop return 0 fi done =2D set +o noglob; set -$shopts #reset old shell opts + eshopts_pop =20 local licmsg=3D$(emktemp) cat <<-EOF > ${licmsg} =2Dmike --nextPart1626510.XHFLYMNUqB Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.13 (GNU/Linux) iQIcBAABAgAGBQJLH2LBAAoJEEFjO5/oN/WB1qQP/REdfvw9V6ajtSGoOSn6Nrd2 xjZCIk65oryuuGgfw2xhYdimXU30Anqk2iOdHaeZ9s3EgqcKcaVPq8aAyNGv6VAw 2bM+UeMtBnRCXyNYDM2P2H5oC2THr5Mzva2pQUC88LMcWQXQcSczKxQk1KACNLBM Yimdad5zPOf7K2FRc+j8ugoKgAVQwAzquoAbwdOCuDQNCPt+tBQPdzNqalRhAS5f D4+0gdIANJUD+JmCoQM3OnIyrwDlzWrhsCsVsKTrZg7LUzjG6h9bn0mwQQoEyZWJ 1uulxpM8FpAAyTwVLS82/raskwrOC8d0uY4GkvBNlTxO+koJwPPWzTnRidC4rkW/ 46fF6ZWuSwuvl1As0LjEvWW5GAbCY/DRpfRDFkSrgooGEFaoo7jBUBllGazWyZvd UTz6IuA6l7pQrFNtGNlEuh70hQlUWQ/peRTF4dC8RzVVJDN4uuvHJhzcRUv0GIOu rjxMYJGTS7N457NcY1RJtenZwI9d3fSimXLdAdmv+EHLbB+jASAt9VHUliwtnTyX pnQXyKqioM+5yQUys9GMjv6uxFQRMPRk1NaYzQnLeww687vacc6ZgpjxHlSqHiVR Qx/lM9xsS8UNbn87whm/ahsGOFkojcoiZmXZly3NGy8pXhwI5QtAb9hqO9F6jsZ5 G03cldPfyqYBU9yIP2Ms =w/9h -----END PGP SIGNATURE----- --nextPart1626510.XHFLYMNUqB--