1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0+
3#
4# the corresponding .litmus.out file, and does not judge the result.
5#
6# . scripts/parseargs.sh
7#
8# Include into other Linux kernel tools/memory-model scripts.
9#
10# Copyright IBM Corporation, 2018
11#
12# Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
13
14T=/tmp/parseargs.sh.$$
15mkdir $T
16
17# Initialize one parameter: initparam name default
18initparam () {
19	echo if test -z '"$'$1'"' > $T/s
20	echo then >> $T/s
21	echo	$1='"'$2'"' >> $T/s
22	echo	export $1 >> $T/s
23	echo fi >> $T/s
24	echo $1_DEF='$'$1  >> $T/s
25	. $T/s
26}
27
28initparam LKMM_DESTDIR "."
29initparam LKMM_HERD_OPTIONS "-conf linux-kernel.cfg"
30initparam LKMM_JOBS `getconf _NPROCESSORS_ONLN`
31initparam LKMM_PROCS "3"
32initparam LKMM_TIMEOUT "1m"
33
34scriptname=$0
35
36usagehelp () {
37	echo "Usage $scriptname [ arguments ]"
38	echo "      --destdir path (place for .litmus.out, default by .litmus)"
39	echo "      --herdopts -conf linux-kernel.cfg ..."
40	echo "      --jobs N (number of jobs, default one per CPU)"
41	echo "      --procs N (litmus tests with at most this many processes)"
42	echo "      --timeout N (herd7 timeout (e.g., 10s, 1m, 2hr, 1d, '')"
43	echo "Defaults: --destdir '$LKMM_DESTDIR_DEF' --herdopts '$LKMM_HERD_OPTIONS_DEF' --jobs '$LKMM_JOBS_DEF' --procs '$LKMM_PROCS_DEF' --timeout '$LKMM_TIMEOUT_DEF'"
44	exit 1
45}
46
47usage () {
48	usagehelp 1>&2
49}
50
51# checkarg --argname argtype $# arg mustmatch cannotmatch
52checkarg () {
53	if test $3 -le 1
54	then
55		echo $1 needs argument $2 matching \"$5\"
56		usage
57	fi
58	if echo "$4" | grep -q -e "$5"
59	then
60		:
61	else
62		echo $1 $2 \"$4\" must match \"$5\"
63		usage
64	fi
65	if echo "$4" | grep -q -e "$6"
66	then
67		echo $1 $2 \"$4\" must not match \"$6\"
68		usage
69	fi
70}
71
72while test $# -gt 0
73do
74	case "$1" in
75	--destdir)
76		checkarg --destdir "(path to directory)" "$#" "$2" '.\+' '^--'
77		LKMM_DESTDIR="$2"
78		mkdir $LKMM_DESTDIR > /dev/null 2>&1
79		if ! test -e "$LKMM_DESTDIR"
80		then
81			echo "Cannot create directory --destdir '$LKMM_DESTDIR'"
82			usage
83		fi
84		if test -d "$LKMM_DESTDIR" -a -w "$LKMM_DESTDIR" -a -x "$LKMM_DESTDIR"
85		then
86			:
87		else
88			echo "Directory --destdir '$LKMM_DESTDIR' insufficient permissions to create files"
89			usage
90		fi
91		shift
92		;;
93	--herdopts|--herdopt)
94		checkarg --destdir "(herd7 options)" "$#" "$2" '.*' '^--'
95		LKMM_HERD_OPTIONS="$2"
96		shift
97		;;
98	-j[1-9]*)
99		njobs="`echo $1 | sed -e 's/^-j//'`"
100		trailchars="`echo $njobs | sed -e 's/[0-9]\+\(.*\)$/\1/'`"
101		if test -n "$trailchars"
102		then
103			echo $1 trailing characters "'$trailchars'"
104			usagehelp
105		fi
106		LKMM_JOBS="`echo $njobs | sed -e 's/^\([0-9]\+\).*$/\1/'`"
107		;;
108	--jobs|--job|-j)
109		checkarg --jobs "(number)" "$#" "$2" '^[1-9][0-9]\+$' '^--'
110		LKMM_JOBS="$2"
111		shift
112		;;
113	--procs|--proc)
114		checkarg --procs "(number)" "$#" "$2" '^[0-9]\+$' '^--'
115		LKMM_PROCS="$2"
116		shift
117		;;
118	--timeout)
119		checkarg --timeout "(timeout spec)" "$#" "$2" '^\([0-9]\+[smhd]\?\|\)$' '^--'
120		LKMM_TIMEOUT="$2"
121		shift
122		;;
123	*)
124		echo Unknown argument $1
125		usage
126		;;
127	esac
128	shift
129done
130if test -z "$LKMM_TIMEOUT"
131then
132	LKMM_TIMEOUT_CMD=""; export LKMM_TIMEOUT_CMD
133else
134	LKMM_TIMEOUT_CMD="timeout $LKMM_TIMEOUT"; export LKMM_TIMEOUT_CMD
135fi
136rm -rf $T
137