## Common shell functions used by demo scripts programs/*/*.sh. ## How to write a demo script ## ========================== ## ## Include this file near the top of each demo script: ## . "${0%/*}/demo_common.sh" ## ## Start with a "msg" call that explains the purpose of the script. ## Then call the "depends_on" function to ensure that all config ## dependencies are met. ## ## As the last thing in the script, call the cleanup function. ## ## You can use the functions and variables described below. set -e -u # Check if the provided path ($1) can be a valid root for Mbed TLS or TF-PSA-Crypto. # This is based on the fact that "scripts/project_name.txt" exists. is_valid_root () { if ! [ -f "$1/scripts/project_name.txt" ]; then return 1; fi return 0; } ## At the end of the while loop below $root_dir will point to the root directory ## of the Mbed TLS or TF-PSA-Crypto source tree. root_dir="${0%/*}" ## Find a nice path to the root directory, avoiding unnecessary "../". ## ## The code supports demo scripts nested up to 4 levels deep. ## ## The code works no matter where the demo script is relative to the current ## directory, even if it is called with a relative path. n=4 while true; do # If we went up too many folders, then give up and return a failure. if [ $n -eq 0 ]; then echo >&2 "This doesn't seem to be an Mbed TLS source tree." exit 125 fi if is_valid_root "$root_dir"; then break; fi n=$((n - 1)) case $root_dir in .) root_dir="..";; ..|?*/..) root_dir="$root_dir/..";; ?*/*) root_dir="${root_dir%/*}";; /*) root_dir="/";; *) root_dir=".";; esac done # Now that we have a root path we can source the "project_detection.sh" script. . "$root_dir/framework/scripts/project_detection.sh" ## msg LINE... ## msg &2 <