Add files from parent branch HEAD:
[pkgsrc.git] / mk / check / check-subr.sh
1 # $NetBSD: check-subr.sh,v 1.4 2006/11/11 23:08:00 rillig Exp $
2 #
3 # This file contains shell functions that are used by the various shell
4 # programs that check things in pkgsrc. All these programs must be
5 # called with the following environment variables set:
6 #
7 # SKIP_FILTER
8 #       A shell expression of the form
9 #
10 #               */pattern.*) continue;; *.txt) continue;;
11 #
12 #       that can be passed to eval(1) in order to skip the files
13 #       that are chosen by the respective *_SKIP variable in
14 #       make(1).
15 #
16
17 # Implementation notes:
18 #
19 # 1. The SKIP_FILTER variable should only be used in the following
20 # pattern, usually inside a "for" or "while" loop.
21 #
22 #       skip=no
23 #       eval "case \"\$fname\" in $SKIP_FILTER *.orig) skip=yes;; esac"
24 #       [ $skip = no ] || continue
25 #
26 # 2. The programs using this file are run with the tools wrapper
27 # directory in the PATH, so they call the utilities by their base names.
28 # They may also assume to be interpreted by a POSIX-conforming shell, in
29 # particular _not_ by the Solaris /bin/sh.
30 #
31
32 # All programs that check something are very strict.
33 set -eu
34
35 cs_exitcode=0
36
37 # usage: cs_setprogname "progname"
38 #
39 # This function sets the variable that will later be used in diagnostic
40 # messages to identify the program that generated the message.
41 cs_setprogname() {
42         cs_progname="${1##*/}"
43 }
44
45 # Each diagnostic message can be preceded by a heading to better identify
46 # messages that belong together. The heading will only be printed if it
47 # differs from the last one.
48 cs_last_heading=""
49
50 # usage: cs_error_heading "new heading"
51 cs_error_heading() {
52         if [ x"$1" != x"$cs_last_heading" ]; then
53                 cs_last_heading="$1"
54                 cs_error_msg "=> $1"
55         fi
56 }
57
58 # usage: cs_warning_heading "new heading"
59 cs_warning_heading() {
60         if [ x"$1" != x"$cs_last_heading" ]; then
61                 cs_last_heading="$1"
62                 cs_warning_msg "=> $1"
63         fi
64 }
65
66 # usage: cs_error_msg "error message"
67 cs_error_msg() {
68         echo "ERROR: [$cs_progname] $*" 1>&2
69         cs_exitcode=1
70 }
71
72 # usage: cs_warning_msg "warning message"
73 cs_warning_msg() {
74         echo "WARNING: [$cs_progname] $*" 1>&2
75 }
76
77 cs_hline=\
78 "==========================================================================="
79
80 # usage: cs_explain <<EOF
81 cs_explain() {
82         { echo ""
83           echo "Explanation:"
84           echo "$cs_hline"
85           cat
86           echo "$cs_hline"
87           echo ""
88         } 1>&2
89 }
90
91 # usage: cs_exit
92 #
93 # At the end of the program, cs_exit should be called to return the
94 # appropriate exit status to the calling process. It is non-zero when
95 # any error messages have been printed and zero otherwise.
96 cs_exit() {
97         exit "$cs_exitcode"
98 }