-
Notifications
You must be signed in to change notification settings - Fork 0
/
contator
132 lines (129 loc) · 3.73 KB
/
contator
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
129
130
131
132
#!/bin/bash
function c_execute {
dbg "Running $TMP_SCRIPT"
SCRIPT_OUTPUT=""
chmod +x "$TMP_SCRIPT"
must $? "Failed to set +x on $TMP_SCRIPT"
if [ "$IS_C" = true ]; then
dbg "It is C"
mv "$TMP_SCRIPT" "$TMP_SCRIPT".c
must $? "Couldn't rename $TMP_SCRIPT for C"
TMP_SCRIPT="$TMP_SCRIPT".c
fi
if declare -p CONTATE_CONFIG > /dev/null 2>&1 ; then
dbg "Sourcing"
. $TMP_SCRIPT
else
SCRIPT_OUTPUT="$($TMP_SCRIPT)"
fi
must $? "Script output failed"
TOTAL_LINES=$(echo "$SCRIPT_OUTPUT"| wc -l)
i=0
while IFS='' read -r scriptoutput2; do
dbg "Line Number:$i / $TOTAL_LINES"
i=$(( i + 1 ))
if [ $i -gt 1 ]; then
if [ "${OUTPUT}" = "/dev/stdout" ]; then
echo -n "${CoP}${INDENTATION}${scriptoutput2}"
else
echo -n "${CoP}${INDENTATION}${scriptoutput2}" >> "$OUTPUT"
fi
else
if [ "${OUTPUT}" = "/dev/stdout" ]; then
echo -n "${CoP}${scriptoutput2}"
else
echo -n "${CoP}${scriptoutput2}" >> "$OUTPUT"
fi
fi
if [ $i -lt $TOTAL_LINES ]; then
echo "" >> "$OUTPUT"
else
dbg "Was last line"
fi
done <<< "$SCRIPT_OUTPUT"
unset IS_C
}
[ -z "${CONTATE_START_REGEX}" ] && CONTATE_START_REGEX='^(.*)#!(/.+)$'
[ -z "${CONTATE_END_REGEX}" ] && CONTATE_END_REGEX='^([[:space:]]*)!#(.*)$'
[ -z "${CONTATE_INLINE}" ] && CONTATE_INLINE='^.*#!\$\((.+)\)!#.*$'
# 1- Target file - realpath is fine, are we being passed that? it's fine to set it
# 2- Output file- this needs to be a realpath + any directories that have been or need to be made
contate_file(){
dbg "TARGET=$1"
dbg "OUTPUT=$2"
TARGET="$1"
OUTPUT="$2"
IS_C=false
unset TMP_SCRIPT
while IFS='' read -r line || [[ -n "$line" ]]; do # if the EOF comes with no NL, second term helps
# IN SCRIPT
if [ -n "${TMP_SCRIPT}" ]; then
# END BLOCK..
if [[ "$line" =~ $CONTATE_END_REGEX ]]; then
dbg "END SCRIP on '$line' with rematch '${BASH_REMATCH[2]}'"
c_execute # TODO what do with output?
if [ "${OUTPUT}" = "/dev/stdout" ]; then
echo "${CoE}${BASH_REMATCH[2]}"
else
echo "${CoE}${BASH_REMATCH[2]}" >> ${OUTPUT}
fi
rm "$TMP_SCRIPT"
unset TMP_SCRIPT
else
dbg "Process line '$line'"
echo "${line#$INDENTATION}" >> "$TMP_SCRIPT"
fi
# Start Contate Block
elif [[ "$line" =~ $CONTATE_START_REGEX ]] && [[ "${BASH_REMATCH[1]}" != *"#" ]]; then
dbg "START SCR on '$line' with rematch: '${BASH_REMATCH[1]}'"
if grep -qe "tcc -run" <<< "$line"; then
IS_C=true
fi
# set filetype
TMP_SCRIPT="$(mktemp)"
echo "${line#${BASH_REMATCH[1]}}" >> "$TMP_SCRIPT"
if [ "${OUTPUT}" = "/dev/stdout" ]; then
echo -n "${CoS}${BASH_REMATCH[1]}"
else
echo -n "${CoS}${BASH_REMATCH[1]}" >>"$OUTPUT"
fi
[[ "$line" =~ ^([[:space:]]*) ]]
INDENTATION="${BASH_REMATCH[1]}"
dbg "Captured indentation: '$INDENTATION'"
# Direct copy
elif [[ "$line" =~ $CONTATE_INLINE ]]; then
dbg "Whole inline line: ${BASH_REMATCH[0]}"
dbg "Inline replacement: '${BASH_REMATCH[1]}'"
INLINE_OUTPUT="$(eval "${BASH_REMATCH[1]}")"
must $? "${BASH_REMATCH[1]} failed to be eval'ed"
dbg "INLINE OUTPUT: $INLINE_OUTPUT"
INLINE_OUTPUT="${CoI}${line/\#\!$\(${BASH_REMATCH[1]})\!#/$INLINE_OUTPUT}"
if [ -n "$INLINE_OUTPUT" ]; then
if [ "${OUTPUT}" = "/dev/stdout" ]; then
echo "${INLINE_OUTPUT}"
else
echo "${INLINE_OUTPUT}" >> "$OUTPUT"
fi
fi
else
if [ "${OUTPUT}" = "/dev/stdout" ]; then
echo "${CoC}$line"
else
echo "${CoC}$line" >> "$OUTPUT"
fi
fi
done < "$TARGET"
if [ -z "$TMP_SCRIPT" ]; then
dbg "WROTE SCRIPT"
else
dbg "Reached EOF w/in script"
c_execute # TODO what do with output?
if [ "${OUTPUT}" = "/dev/stdout" ]; then
echo "${CoE}${BASH_REMATCH[2]}"
else
echo "${CoE}${BASH_REMATCH[2]}" >> ${OUTPUT}
fi
rm "$TMP_SCRIPT"
unset TMP_SCRIPT
fi
}