#!/bin/bash # # This script tries to format a single file with uncrustify-trinity # using the specified configuration file and until there are no # further changes, up to the maximum number of times specified. # # Parameters: # $1: config file to use # $2: source file to format # $3: maximum number of formatting attempts (optional integer - default is 5) # Use 0 to just validate whether a file adhere to the required code format # # Check uncrustify-trinity location UNCRUSTIFY_BIN="" if [ -x "/opt/trinity/bin/uncrustify-trinity" ]; then UNCRUSTIFY_BIN="/opt/trinity/bin/uncrustify-trinity" elif [ -x "/usr/bin/uncrustify-trinity" ]; then UNCRUSTIFY_BIN="/usr/bin/uncrustify-trinity" elif [ -x "/usr/local/bin/uncrustify-trinity" ]; then UNCRUSTIFY_BIN="/usr/local/bin/uncrustify-trinity" fi if [ "${UNCRUSTIFY_BIN}" = "" ]; then echo "[UF] --- Unable to find uncrustify-trinity executable. Aborting." exit 1 fi # Check config file exists CFG_FILE=$1 if [ ! -e "$1" ]; then echo "[UF] --- Unable to find the specified configuration file [${CFG_FILE}]. Aborting." exit 2 fi # Check source file exists SRC_FILE=$2 if [ ! -e "$2" ]; then echo "[UF] --- Unable to find the specified source file [${SRC_FILE}]. Aborting." exit 3 fi # Setup intermediate file names FMT_SRC_FILE="${SRC_FILE%.*}_uncrusted_src.${SRC_FILE##*.}" FMT_DST_FILE="${SRC_FILE%.*}_uncrusted_dst.${SRC_FILE##*.}" # Validate the number of retries, if specified NUM_ATTEMPTS=5 # default, if not specified if [ "$3" != "" ]; then NUM_ATTEMPTS=$(($3)) if [ ${NUM_ATTEMPTS} -lt 0 ]; then echo "[UF] --- The number of attempts [${NUM_ATTEMPTS}] must be zero or more. Aborting." exit 4 fi fi REAL_NUM_ATTEMPTS=$((${NUM_ATTEMPTS} + 1)) # Format file and check for errors # Each pass formats FMT_SRC_FILE into FMT_DST_FILE. # If there are no errors, a check for changes is done and # if there are no changes since the previous pass, the process stops. # If changes are detected, the process continue with a new pass # until either there are no more changes of the maximum number of # attempts has been reached. # At each new pass, the last FMT_DST_FILE becomes the new FMT_SRC_FILE. echo "[UF] Processing file ${SRC_FILE}" cp "${SRC_FILE}" "${FMT_SRC_FILE}" FINISHED="n" COUNTER=0 while [ "${FINISHED}" != "y" -a ${COUNTER} -lt ${REAL_NUM_ATTEMPTS} ]; do COUNTER=$((${COUNTER} + 1)) echo "[UF] Pass ${COUNTER}" ${UNCRUSTIFY_BIN} -c "${CFG_FILE}" -f "$FMT_SRC_FILE" -o "${FMT_DST_FILE}" RESULT=$? if [ ! ${RESULT} -eq 0 ]; then echo "[UF] --- Processing error reported by uncrustify-trinity [error code ${RESULT}]" rm "${FMT_SRC_FILE}" 2>/dev/null rm "${FMT_DST_FILE}" 2>/dev/null exit 5 fi # Check for changes if cmp -s "${FMT_SRC_FILE}" "${FMT_DST_FILE}"; then # No changes detected FINISHED="y" else # Changes detected, prepare for next pass mv "${FMT_DST_FILE}" "${FMT_SRC_FILE}" fi done # If a stable formatted output is achieved within the maximum number # of attempts allowed, update the source file and exit successfully. # Otherwise remove all the temporary files and exit with error. if [ "${FINISHED}" != "y" ]; then if [ ${NUM_ATTEMPTS} -eq 0 ]; then echo "[UF] --- Code format verification FAILED." else echo "[UF] --- Unable to reach stable formatted code in ${NUM_ATTEMPTS} attempts." fi rm "${FMT_SRC_FILE}" 2>/dev/null rm "${FMT_DST_FILE}" 2>/dev/null exit 6 fi if [ $COUNTER -gt 1 ]; then # Only update the file if there were actual changes (COUNTER > 1) # to avoid updating the modified time unnecessarily mv "${FMT_DST_FILE}" "${SRC_FILE}" else rm "${FMT_DST_FILE}" fi rm "${FMT_SRC_FILE}" 2>/dev/null if [ ${NUM_ATTEMPTS} -eq 0 ]; then echo "[UF] Code format verification OK." fi