On Wed, Nov 23, 2022 at 10:15:20AM +0100, Martijn van Duren wrote:
> Here's an attempt to remove sed from netstart.
I don't see the point in this.
> Since we use sed in a
> simple string replacement without any fancy regex stuff I think we can
> relatively easy use something based on shell built-ins.
>
> Risk of the current code is that if someone places search inside
> replacement we get an infinite loop, but since both search and
> replacement are under our control I don't think it's worth making the
> code larger than needed.
>
> Only lightly tested
>
> thoughts?
>
> martijn@
>
> Index: netstart
> ===================================================================
> RCS file: /cvs/src/etc/netstart,v
> retrieving revision 1.229
> diff -u -p -r1.229 netstart
> --- netstart 5 Nov 2022 12:06:05 -0000 1.229
> +++ netstart 23 Nov 2022 09:12:28 -0000
> @@ -35,6 +35,18 @@ stripcom() {
> done <$_file
> }
>
> +# Usage: gsub search replace line
> +gsub() {
> + local _s="$1" _r="$2" _l
> + shift 2
> + _l="$@"
> +
> + while [[ "$_l" == *$_s* ]]; do
> + _l="${_l%%$_s*}$_r${_l#*$_s}"
> + done
> + print -n -- "$_l"
> +}
> +
> # Parse and "unpack" a hostname.if(5) line given as positional parameters.
> # Fill the _cmds array with the resulting interface configuration commands.
> parse_hn_line() {
> @@ -82,7 +94,7 @@ parse_hn_line() {
> dhcp) _cmds[${#_cmds[*]}]="ifconfig $_if inet autoconf"
> V4_AUTOCONF=true
> ;;
> - '!'*) _cmd=$(print -- "${_c[@]}" | sed 's/\$if/'$_if'/g')
> + '!'*) _cmd=$(gsub '$if' $_if "${_c[*]}")
> _cmds[${#_cmds[*]}]="${_cmd#!}"
> ;;
> *) _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
>