summaryrefslogtreecommitdiff
path: root/mk/tools/shquote.sh
blob: 439da01025934ace26fd801e6e9d0285eea41752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /bin/sh
# $NetBSD: shquote.sh,v 1.2 2019/05/22 20:47:05 rillig Exp $

# Quotes all shell meta characters from $1 and writes the result to $shquoted.
shquote()
{
	shquoted=$1
	case $shquoted in
	*\'*)
		# replace each ' with '\''
		shquoted=`$tools_wrapper_sed -e "s,','\\\\\\\\'',g" <<EOF
$shquoted
EOF`
	esac

	case $shquoted in
	(*[!!%+,\-./0-9:=@A-Z_a-z]*|'')

		# Move the single quote after the first equals sign, so that
		# macro definitions look like -DMACRO='"value"' instead of
		# the less common '-DMACRO="value"'.
		case $shquoted in
		(*=*)	lhs=${shquoted%%=*}
			case $lhs in
			(*[!!%+,\-./0-9:=@A-Z_a-z]*|'') ;;
			(*)	shquoted="$lhs='${shquoted#*=}'"
				return
			esac
		esac

		shquoted="'$shquoted'"
	esac
}

shquote_args() {
	shquoted_args=''
	for arg in "$@"; do
		shquote "$arg"
		shquoted_args="$shquoted_args $shquoted"
	done
}