From owner-svn-src-head@FreeBSD.ORG Sun Jun 2 23:25:28 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 51B39D1D; Sun, 2 Jun 2013 23:25:28 +0000 (UTC) (envelope-from dteske@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id 425B51CD9; Sun, 2 Jun 2013 23:25:28 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r52NPStG096593; Sun, 2 Jun 2013 23:25:28 GMT (envelope-from dteske@svn.freebsd.org) Received: (from dteske@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r52NPSs1096592; Sun, 2 Jun 2013 23:25:28 GMT (envelope-from dteske@svn.freebsd.org) Message-Id: <201306022325.r52NPSs1096592@svn.freebsd.org> From: Devin Teske Date: Sun, 2 Jun 2013 23:25:28 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r251278 - head/usr.sbin/bsdconfig/share X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 02 Jun 2013 23:25:28 -0000 Author: dteske Date: Sun Jun 2 23:25:27 2013 New Revision: 251278 URL: http://svnweb.freebsd.org/changeset/base/251278 Log: Like r250701, introduce another handy function for truncating variables to a specific byte-length. Works like vsnprintf(3). Modified: head/usr.sbin/bsdconfig/share/strings.subr Modified: head/usr.sbin/bsdconfig/share/strings.subr ============================================================================== --- head/usr.sbin/bsdconfig/share/strings.subr Sun Jun 2 23:23:29 2013 (r251277) +++ head/usr.sbin/bsdconfig/share/strings.subr Sun Jun 2 23:25:27 2013 (r251278) @@ -71,6 +71,41 @@ f_snprintf() }'\' \) } +# f_vsnprintf $var_to_set $size $format $format_args +# +# Similar to vsnprintf(3), write at most $size number of bytes into $var_to_set +# using printf(1) syntax (`$format $format_args'). The value of $var_to_set is +# NULL unless at-least one byte is stored from the output. +# +# Example 1: +# +# limit=7 format="%s" +# format_args="'abc 123'" # 3-spaces between abc and 123 +# f_vsnprintf foo $limit "$format" "$format_args" # foo=[abc 1] +# +# Example 2: +# +# limit=12 format="%s %s" +# format_args=" 'doghouse' 'foxhound' " +# # even more spaces added to illustrate escape-method +# f_vsnprintf foo $limit "$format" "$format_args" # foo=[doghouse fox] +# +# Example 3: +# +# limit=13 format="%s %s" +# f_shell_escape arg1 'aaa"aaa' # arg1=[aaa"aaa] (no change) +# f_shell_escape arg2 "aaa'aaa" # arg2=[aaa'\''aaa] (escaped s-quote) +# format_args="'$arg1' '$arg2'" # use single-quotes to surround args +# f_vsnprintf foo $limit "$format" "$format_args" # foo=[aaa"aaa aaa'a] +# +# In all of the above examples, the call to f_vsnprintf() does not change. Only +# the contents of $limit, $format, and $format_args changes in each example. +# +f_vsnprintf() +{ + eval f_snprintf \"\$1\" \"\$2\" \"\$3\" $4 +} + # f_longest_line_length # # Simple wrapper to an awk(1) script to print the length of the longest line of