1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-only
3
4# bootconfig utility functions
5
6XBC_TMPFILE=
7XBC_BASEDIR=`dirname $0`
8BOOTCONFIG=${BOOTCONFIG:=$XBC_BASEDIR/../bootconfig}
9if [ ! -x "$BOOTCONFIG" ]; then
10	BOOTCONFIG=`which bootconfig`
11	if [ -z "$BOOTCONFIG" ]; then
12		echo "Erorr: bootconfig command is not found" 1>&2
13		exit 1
14	fi
15fi
16
17xbc_cleanup() {
18	if [ "$XBC_TMPFILE" ]; then
19		rm -f "$XBC_TMPFILE"
20	fi
21}
22
23xbc_init() { # bootconfig-file
24	xbc_cleanup
25	XBC_TMPFILE=`mktemp bconf-XXXX`
26	trap xbc_cleanup EXIT TERM
27
28	$BOOTCONFIG -l $1 > $XBC_TMPFILE || exit 1
29}
30
31nr_args() { # args
32	echo $#
33}
34
35xbc_get_val() { # key [maxnum]
36	if [ "$2" ]; then
37		MAXOPT="-L $2"
38	fi
39	grep "^$1 =" $XBC_TMPFILE | cut -d= -f2- | \
40		sed -e 's/", /" /g' -e "s/',/' /g" | \
41		xargs $MAXOPT -n 1 echo
42}
43
44xbc_has_key() { # key
45	grep -q "^$1 =" $XBC_TMPFILE
46}
47
48xbc_has_branch() { # prefix-key
49	grep -q "^$1" $XBC_TMPFILE
50}
51
52xbc_subkeys() { # prefix-key depth [subkey-pattern]
53	__keys=`echo $1 | sed "s/\./ /g"`
54	__s=`nr_args $__keys`
55	grep "^$1$3" $XBC_TMPFILE | cut -d= -f1| cut -d. -f$((__s + 1))-$((__s + $2)) | uniq
56}
57