Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 2 Jun 2013 23:25:28 +0000 (UTC)
From:      Devin Teske <dteske@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r251278 - head/usr.sbin/bsdconfig/share
Message-ID:  <201306022325.r52NPSs1096592@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201306022325.r52NPSs1096592>