aboutsummaryrefslogtreecommitdiff
path: root/contrib/bc/scripts/functions.sh
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/bc/scripts/functions.sh')
-rwxr-xr-xcontrib/bc/scripts/functions.sh175
1 files changed, 174 insertions, 1 deletions
diff --git a/contrib/bc/scripts/functions.sh b/contrib/bc/scripts/functions.sh
index 65ec0a1167fe..8ae765859e51 100755
--- a/contrib/bc/scripts/functions.sh
+++ b/contrib/bc/scripts/functions.sh
@@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: BSD-2-Clause
#
-# Copyright (c) 2018-2021 Gavin D. Howard and contributors.
+# Copyright (c) 2018-2023 Gavin D. Howard and contributors.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
@@ -71,6 +71,88 @@ err_exit() {
exit "$2"
}
+# Function for checking the "d"/"dir" argument of scripts. This function expects
+# a usage() function to exist in the caller.
+# @param 1 The argument to check.
+check_d_arg() {
+
+ if [ "$#" -ne 1 ]; then
+ printf 'Invalid number of args to check_d_arg\n'
+ exit 1
+ fi
+
+ _check_d_arg_arg="$1"
+ shift
+
+ if [ "$_check_d_arg_arg" != "bc" ] && [ "$_check_d_arg_arg" != "dc" ]; then
+ _check_d_arg_msg=$(printf 'Invalid d arg: %s\nMust be either "bc" or "dc".\n\n' \
+ "$_check_d_arg_arg")
+ usage "$_check_d_arg_msg"
+ fi
+}
+
+# Function for checking the boolean arguments of scripts. This function expects
+# a usage() function to exist in the caller.
+# @param 1 The argument to check.
+check_bool_arg() {
+
+ if [ "$#" -ne 1 ]; then
+ printf 'Invalid number of args to check_bool_arg\n'
+ exit 1
+ fi
+
+ _check_bool_arg_arg="$1"
+ shift
+
+ if [ "$_check_bool_arg_arg" != "0" ] && [ "$_check_bool_arg_arg" != "1" ]; then
+ _check_bool_arg_msg=$(printf 'Invalid bool arg: %s\nMust be either "0" or "1".\n\n' \
+ "$_check_bool_arg_arg")
+ usage "$_check_bool_arg_msg"
+ fi
+}
+
+# Function for checking the executable arguments of scripts. This function
+# expects a usage() function to exist in the caller.
+# @param 1 The argument to check.
+check_exec_arg() {
+
+ if [ "$#" -ne 1 ]; then
+ printf 'Invalid number of args to check_exec_arg\n'
+ exit 1
+ fi
+
+ _check_exec_arg_arg="$1"
+ shift
+
+ if [ ! -x "$_check_exec_arg_arg" ]; then
+ if ! command -v "$_check_exec_arg_arg" >/dev/null 2>&1; then
+ _check_exec_arg_msg=$(printf 'Invalid exec arg: %s\nMust be an executable file.\n\n' \
+ "$_check_exec_arg_arg")
+ usage "$_check_exec_arg_msg"
+ fi
+ fi
+}
+
+# Function for checking the file arguments of scripts. This function expects a
+# usage() function to exist in the caller.
+# @param 1 The argument to check.
+check_file_arg() {
+
+ if [ "$#" -ne 1 ]; then
+ printf 'Invalid number of args to check_file_arg\n'
+ exit 1
+ fi
+
+ _check_file_arg_arg="$1"
+ shift
+
+ if [ ! -f "$_check_file_arg_arg" ]; then
+ _check_file_arg_msg=$(printf 'Invalid file arg: %s\nMust be a file.\n\n' \
+ "$_check_file_arg_arg")
+ usage "$_check_file_arg_msg"
+ fi
+}
+
# Check the return code on a test and exit with a fail if it's non-zero.
# @param d The calculator under test.
# @param err The return code.
@@ -326,3 +408,94 @@ gen_nlspath() {
# Return the result.
printf '%s' "$_gen_nlspath_nlspath"
}
+
+ALL=0
+NOSKIP=1
+SKIP=2
+
+# Filters text out of a file according to the build type.
+# @param in File to filter.
+# @param out File to write the filtered output to.
+# @param type Build type.
+filter_text() {
+
+ _filter_text_in="$1"
+ shift
+
+ _filter_text_out="$1"
+ shift
+
+ _filter_text_buildtype="$1"
+ shift
+
+ # Set up some local variables.
+ _filter_text_status="$ALL"
+ _filter_text_last_line=""
+
+ # We need to set IFS, so we store it here for restoration later.
+ _filter_text_ifs="$IFS"
+
+ # Remove the file- that will be generated.
+ rm -rf "$_filter_text_out"
+
+ # Here is the magic. This loop reads the template line-by-line, and based on
+ # _filter_text_status, either prints it to the markdown manual or not.
+ #
+ # Here is how the template is set up: it is a normal markdown file except
+ # that there are sections surrounded tags that look like this:
+ #
+ # {{ <build_type_list> }}
+ # ...
+ # {{ end }}
+ #
+ # Those tags mean that whatever build types are found in the
+ # <build_type_list> get to keep that section. Otherwise, skip.
+ #
+ # Obviously, the tag itself and its end are not printed to the markdown
+ # manual.
+ while IFS= read -r _filter_text_line; do
+
+ # If we have found an end, reset the status.
+ if [ "$_filter_text_line" = "{{ end }}" ]; then
+
+ # Some error checking. This helps when editing the templates.
+ if [ "$_filter_text_status" -eq "$ALL" ]; then
+ err_exit "{{ end }} tag without corresponding start tag" 2
+ fi
+
+ _filter_text_status="$ALL"
+
+ # We have found a tag that allows our build type to use it.
+ elif [ "${_filter_text_line#\{\{* $_filter_text_buildtype *\}\}}" != "$_filter_text_line" ]; then
+
+ # More error checking. We don't want tags nested.
+ if [ "$_filter_text_status" -ne "$ALL" ]; then
+ err_exit "start tag nested in start tag" 3
+ fi
+
+ _filter_text_status="$NOSKIP"
+
+ # We have found a tag that is *not* allowed for our build type.
+ elif [ "${_filter_text_line#\{\{*\}\}}" != "$_filter_text_line" ]; then
+
+ if [ "$_filter_text_status" -ne "$ALL" ]; then
+ err_exit "start tag nested in start tag" 3
+ fi
+
+ _filter_text_status="$SKIP"
+
+ # This is for normal lines. If we are not skipping, print.
+ else
+ if [ "$_filter_text_status" -ne "$SKIP" ]; then
+ if [ "$_filter_text_line" != "$_filter_text_last_line" ]; then
+ printf '%s\n' "$_filter_text_line" >> "$_filter_text_out"
+ fi
+ _filter_text_last_line="$_filter_text_line"
+ fi
+ fi
+
+ done < "$_filter_text_in"
+
+ # Reset IFS.
+ IFS="$_filter_text_ifs"
+}