1#! /bin/sh
2# $NetBSD: accept.sh,v 1.18 2025/02/27 06:48:29 rillig Exp $
3#
4# Copyright (c) 2021 The NetBSD Foundation, Inc.
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#
16# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27#
28
29# usage: accept.sh [-u] <pattern>...
30#
31#         Run one or more lint tests, saving their output in the corresponding
32#         .exp files, for incorporating the messages into the .c files as
33#         'expect' comments.
34
35set -eu
36
37atf_get_srcdir() {
38          echo "."
39}
40atf_test_case() {
41          :
42}
43
44: "${archsubdir:=$(make -v ARCHSUBDIR)}"
45. './t_integration.sh'                  # for configure_test_case
46
47update_flags=''
48while getopts 'u' opt; do
49          case $opt in
50          u)        update_flags='-u';;
51          *)        echo "usage: $0 [-u] pattern..." 1>&2
52                    exit 1;;
53          esac
54done
55shift $((OPTIND - 1))
56
57done_tests=''
58for pattern in "$@"; do
59          # shellcheck disable=SC2231
60          for cfile in *$pattern*.c; do
61                    base=${cfile%.*}
62                    exp_tmp_file="$base.exp.tmp"
63                    exp_file="$base.exp"
64                    ln_tmp_file="$base.exp-ln.tmp"
65                    ln_file="$base.exp-ln"
66
67                    configure_test_case "$cfile"
68                    # shellcheck disable=SC2154
69                    if [ "$skip" = yes ]; then
70                              continue
71                    fi
72
73                    if [ ! -f "$ln_file" ]; then
74                              ln_file='/dev/null'
75                    fi
76
77                    # shellcheck disable=SC2154
78                    # shellcheck disable=SC2086
79                    if "$lint1" $flags "$base.c" "$ln_tmp_file" > "$exp_tmp_file"; then
80                              if [ -s "$exp_tmp_file" ]; then
81                                        echo "$base produces output but exits successfully"
82                                        sed 's,^,| ,' "$exp_tmp_file"
83                              fi
84                    elif [ $? -ge 128 ]; then
85                              echo "$base crashed"
86                              continue
87                    fi
88
89                    if [ -f "$exp_file" ] && cmp -s "$exp_tmp_file" "$exp_file"; then
90                              rm "$exp_tmp_file"
91                    else
92                              mv "$exp_tmp_file" "$exp_file"
93                    fi
94
95                    if [ ! -f "$ln_tmp_file" ]; then
96                              : 'No cleanup necessary.'
97                    elif [ "$ln_file" = '/dev/null' ]; then
98                              rm "$ln_tmp_file"
99                    else
100                              if tr -d ' \t' < "$ln_file" | sed '/^$/d' > "$ln_file.trimmed.tmp" &&
101                                  tr -d ' \t' < "$ln_tmp_file" > "$ln_tmp_file.trimmed.tmp" &&
102                                  cmp -s "$ln_file.trimmed.tmp" "$ln_tmp_file.trimmed.tmp"; then
103                                        rm "$ln_tmp_file"
104                              else
105                                        echo "Replacing $ln_file"
106                                        mv "$ln_tmp_file" "$ln_file"
107                              fi
108                              rm -f "$ln_file.trimmed.tmp" "$ln_tmp_file.trimmed.tmp"
109                    fi
110
111                    case "$base" in (msg_*)
112                              if grep 'This message is not used\.' "$cfile" >/dev/null; then
113                                        : 'Skip further checks.'
114                              elif [ ! -s "$exp_file" ]; then
115                                        echo "$base should produce warnings"
116                              elif grep '^TODO: "Add example code' "$cfile" >/dev/null; then
117                                        : 'ok, this test is not yet written'
118                              else
119                                        msgid=${base}
120                                        msgid=${msgid#msg_00}
121                                        msgid=${msgid#msg_0}
122                                        msgid=${msgid#msg_}
123                                        msgid=${msgid%%_*}
124                                        if ! grep "\\[$msgid\\]\$" "$exp_file" >/dev/null; then
125                                                  echo "$base should trigger the message '$msgid'"
126                                        fi
127                              fi
128                    esac
129
130                    done_tests="$done_tests $cfile"
131          done
132done
133
134# shellcheck disable=SC2086
135lua './check-expect.lua' $update_flags $done_tests
136