1#!/bin/sh -efu
2#
3# Copyright (c) 2017 Dmitry V. Levin <ldv@altlinux.org>
4# Copyright (c) 2017-2018 The strace developers.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. The name of the author may not be used to endorse or promote products
16#    derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29usage()
30{
31	cat >&2 <<EOF
32Usage: $0 [<input> [<output>]]
33
34Generate test script(s) from <input> table.
35If a <output> is specified, generate the script for this test only,
36otherwise generate scripts for all tests specified in <input> table.
37EOF
38	exit 1
39}
40
41input="${0%/*}/gen_tests.in"
42[ $# -eq 0 ] || { input="$1"; shift; }
43output=
44[ $# -eq 0 ] || { output="$1"; shift; }
45[ $# -eq 0 ] || usage
46
47if [ -n "$output" ]; then
48	match="${output##*/}"
49	match="${match%.gen.test}"
50	[ -n "$match" ] || usage
51else
52	match=
53	dir="$(dirname "$input")"
54fi
55
56names=
57
58while read -r name arg0 args; do {
59	[ -n "${name###*}" ] || continue
60	if [ -z "$match" ]; then
61		names="$names $name"
62		output="$dir/$name.gen.test"
63	else
64		[ "$match" = "$name" ] || continue
65	fi
66
67	hdr="\
68#!/bin/sh -efu
69# Generated by $0 from $input ($name $arg0 $args); do not edit."
70
71	case "$arg0" in
72		+*)
73		cat <<-EOF
74		$hdr
75		set -- $args
76		. "\${srcdir=.}/${arg0#+}"
77		EOF
78		;;
79
80		''|-*)
81		cat <<-EOF
82		$hdr
83		. "\${srcdir=.}/init.sh"
84		run_strace_match_diff $arg0 $args
85		EOF
86		;;
87
88		*)
89		cat <<-EOF
90		$hdr
91		. "\${srcdir=.}/init.sh"
92		$arg0 $args
93		EOF
94		;;
95	esac > "$output"
96
97	chmod a+x "$output"
98} < /dev/null; done < "$input"
99
100if [ -n "$names" ]; then
101	{
102		printf '# Generated by %s from %s; do not edit.\n' "$0" "$input"
103		printf 'GEN_TESTS ='
104		printf ' %s.gen.test' $names
105		echo
106		target='$(srcdir)/%s.gen.test'
107		dep1='$(abs_srcdir)/gen_tests.sh'
108		dep2='$(srcdir)/gen_tests.in'
109		recipe='$(AM_V_GEN) $^ $@'
110		printf "\\n$target: $dep1 $dep2\\n\\t$recipe\\n" $names
111	} > "$dir/gen_tests.am"
112fi
113