-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembly_format.sh
executable file
·53 lines (44 loc) · 1.26 KB
/
assembly_format.sh
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
#!/bin/bash
if ! [[ "$1" ]]; then
printf "assembly_format.sh <input file name>, and pipe to an output if necessary\n"
exit 1
fi
max_code_len=0
# todo put the codes into functions.
# and make it open file only one time. (probably)
while IFS='' read -r line || [[ -n "$line" ]]; do
# calculate the max width
# ignore all the lines that starts with ;
if [[ "$line" =~ ^\; ]] || ! [[ "$line" == *";"* ]]; then
continue
fi
code=$( echo $line | cut -d";" -f1 | sed "s/[[:space:]]*$//")
comment=$( echo $line | cut -d";" -f2 | sed "s/^\s+//")
code_len=${#code}
comment_len=${#comment}
if (( $max_code_len < $code_len )); then
max_code_len=$code_len
fi
done < "$1"
while IFS='' read -r line || [[ -n "$line" ]]; do
# ignore all the lines that starts with ;
if [[ "$line" =~ ^\; ]] || ! [[ "$line" == *";"* ]]; then
printf "$line\n"
continue
fi
code=$( echo $line | cut -d";" -f1 | sed "s/[[:space:]]*$//")
comment=$( echo $line | cut -d";" -f2 | sed "s/^\s+//")
code_len=${#code}
comment_len=${#comment}
printf "$code"
# echo the max_width
typeset -i i=0
req_len=$(( $max_code_len - $code_len + 4))
while (( $i < $req_len )); do
printf " "
(( ++i ))
done
printf ";"
printf "$comment"
printf "\n"
done < "$1"