-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinefilter.sh
executable file
·128 lines (102 loc) · 2.23 KB
/
linefilter.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
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
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
function main {
check_arguments "$@"
read_the_file "$@"
# preprocess
parse_define_macros
apply_define_macros
remove_comments_and_empty_lines
# apply scope
}
################################
# input handling
################################
function check_arguments {
# number of arguments
if [ $# -lt 2 ]
then
echo "usage: $0 <input file> <target1> [<target2> ...]"
exit
fi
# input file
if [ ! -r $1 ]
then
echo "$1 is not a file or not readable."
exit
fi
if [ ! -s $1 ]
then
# file is empty, output should be empty too.
exit
fi
}
function read_the_file {
readarray lines < $1
}
################################
# macros
################################
function parse_define_macros {
defines_index=0
for index in ${!lines[*]}
do
if [ -n "${lines[$index]}" ] # line is not empty
then
if [[ "${lines[$index]}" == \#define* ]]
then
defines[defines_index]="${lines[$index]}"
fi
fi
done
}
function apply_macro {
macro_name=$1
shift
tmp=$@
macro_value=${tmp} # without the " it will strip the string
echo "$macro_name"
echo "$macro_value"
# replace WHOLE WORDS in lines.
}
function apply_define_macros {
for index in ${!defines[*]}
do
apply_macro ${lines[$index]:7}
done
}
function remove_comments_and_empty_lines {
for index in ${!lines[*]}
do
if [[ "${lines[$index]}" == \#* ]]
then
unset 'lines[index]'
continue
fi
tmp=${lines[$index]:0:1}
if [ -z $tmp ]
then
unset 'lines[index]'
continue
fi
done
}
################################
# helper stuffs
################################
function print_variables {
echo -e "\n\n\ncontent of lines:"
for index in ${!lines[*]}
do
echo -n "${lines[$index]}"
done
echo "end of content"
echo -e "\n\n\ncontent of defines"
for index in ${!defines[*]}
do
echo "${defines[$index]}"
done
echo "end of content"
}
################################
main "$@"
#print_variables