summaryrefslogtreecommitdiffstats
path: root/code_format/uncrustify_file
blob: 0f2204a83719b462fdc081830f7e4027f375eb68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/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