[oe-commits] Chris Larson : bin/{cp, sed}: Add shell script wrappers for portability

git version control git at git.openembedded.org
Fri Oct 22 03:36:23 UTC 2010


Module: openembedded.git
Branch: org.openembedded.dev
Commit: 788c62604ce3a0f99132fb1267236fa16fa1eac8
URL:    http://gitweb.openembedded.net/?p=openembedded.git&a=commit;h=788c62604ce3a0f99132fb1267236fa16fa1eac8

Author: Chris Larson <chris_larson at mentor.com>
Date:   Wed Oct 20 08:20:35 2010 -0700

bin/{cp,sed}: Add shell script wrappers for portability

Signed-off-by: Chris Larson <chris_larson at mentor.com>

---

 bin/cp  |   62 ++++++++++++++++++++++++++++++++++++++
 bin/sed |  102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 164 insertions(+), 0 deletions(-)

diff --git a/bin/cp b/bin/cp
new file mode 100755
index 0000000..81fdbe8
--- /dev/null
+++ b/bin/cp
@@ -0,0 +1,62 @@
+#!/bin/sh
+#
+# - We do not allow -i due to the non-interactive nature of OE tasks
+# - We do not allow -r, as it has known problems, and is marked
+#   obsolescent in the standard
+# - We allow -a as shorthand for -RpP
+# - Otherwise, we stick to what SuSv3 defines
+
+realbin() {
+     _script=`basename $0`
+     found=
+     for bin in `which -a $_script`; do
+          if ! cmp -s $bin $0; then
+                found=$bin
+                break
+          fi
+     done
+     if [ -n "$found" ]; then
+          echo "$found"
+     else
+          return 1
+     fi
+}
+
+quote(){
+    /usr/bin/sed -e "s,','\\\\'',g; 1s,^,',; \$s,\$,',;" << EOF
+$1
+EOF
+}
+
+save () {
+    case "$1" in
+    # when a string contains a "'" we have to escape it
+    *\'*)
+        saved="$saved `quote "$1"`"
+        ;;
+    # otherwise just quote the variable
+    *)
+        saved="$saved '$1'"
+        ;;
+    esac
+}
+
+saved=""
+while getopts fpaRHLP opt; do
+    case "$opt" in
+        a)
+            opt="RpP"
+            ;;
+        \?)
+            exit 1
+            ;;
+    esac
+    save "-$opt"
+done
+shift $(($OPTIND - 1))
+for arg; do
+    save "$arg"
+done
+
+eval set -- "$saved"
+exec `realbin` "$@"
diff --git a/bin/sed b/bin/sed
new file mode 100755
index 0000000..520c5bd
--- /dev/null
+++ b/bin/sed
@@ -0,0 +1,102 @@
+#!/bin/sh
+#
+# Sed portability notes
+# - SuSv3
+#    - Only -e, -f, and -n are standard options
+# - Linux
+#    - -i argument is of the form -i[ext]
+#    - extended regular expressions argument is -r
+# - Mac OSX
+#    - -i argument is of the form -i [ext]
+#    - extended regular expressions argument is -E
+#
+# Here we support a limited subset of the available sed capabilities,
+# ensuring that all those supported by this script can be utilized
+# regardless of the current platform.
+#
+# We accept -n, -e, -f, and -i with no backup extension.  We support
+# extended regular expressions using the -r argument.  Note that extended
+# regular expressions support may not be retained, depending upon the
+# capabilities of the other platforms we wish to support, and we may need
+# to reimplement -i internally in this script on some platforms.
+
+
+realbin() {
+     _script=`basename $0`
+     found=
+     for bin in `which -a $_script`; do
+          if ! cmp -s $bin $0; then
+                found=$bin
+                break
+          fi
+     done
+     if [ -n "$found" ]; then
+          echo "$found"
+     else
+          return 1
+     fi
+}
+
+quote(){
+    /usr/bin/sed -e "s,','\\\\'',g; 1s,^,',; \$s,\$,',;" << EOF
+$1
+EOF
+}
+
+save () {
+    case "$1" in
+    # when a string contains a "'" we have to escape it
+    *\'*)
+        saved="$saved `quote "$1"`"
+        ;;
+    # otherwise just quote the variable
+    *)
+        saved="$saved '$1'"
+        ;;
+    esac
+}
+
+case `uname -s` in
+    Darwin)
+        inplace_arg="-i ''"
+        extended_re_arg="-E"
+        getopt_os="ir"
+        ;;
+    Linux)
+        inplace_arg="-i"
+        extended_re_arg="-r"
+        getopt_os="ir"
+        ;;
+esac
+
+saved=""
+while getopts ne:f:$getopt_os opt; do
+    case "$opt" in
+        n)
+            save "-$opt"
+            ;;
+        e|f)
+            save "-$opt"
+            save "$OPTARG"
+            ;;
+        i)
+            saved="$saved $inplace_arg"
+            continue
+            ;;
+        r)
+            saved="$saved $extended_re_arg"
+            continue
+            ;;
+        \?)
+            echo >&2 "Unsupported argument: $OPTARG"
+            exit 1
+            ;;
+    esac
+done
+shift $(($OPTIND - 1))
+for arg; do
+    save "$arg"
+done
+
+eval set -- "$saved"
+exec `realbin` "$@"





More information about the Openembedded-commits mailing list