-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc
executable file
·214 lines (189 loc) · 3.83 KB
/
c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/sh
#Save default Field separator
SAVEIFS=$IFS
restoreIFS(){
IFS=$SAVEIFS
}
#Trap in case of exit
trap restoreIFS INT
#Build Shortcuts
cFile(){
#Quickly compile and run C code
cFile=$(echo "$1"|rev |cut -d"/" -f1|rev|cut -d. -f1)
error=$(make "$cFile")
#If no error then run executable
if [ $? -eq 0 ]
then
#Create arguements string
args=" "
for i in $@
do
if [ $i != $1 ]
then
args="$args $i"
fi
done
#Run the executable
./$cFile $args
fi
#If executable was made delete it
if [ -f $cFile ]
then
rm $cFile
fi
}
cppFile(){
#Quickly compile and run C++ code
cFile=$(echo "$1"|rev |cut -d"/" -f1|rev|cut -d. -f1)
# cFile=$(echo "$cFile"|cut -d. -f1)
make -s "$cFile"
#If no error run executable
if [ $? -eq 0 ]
then
#Create arguements string
args=" "
for i in $@
do
if [ $i != $1 ]
then
args="$args $i"
fi
done
#Run the executable
./$cFile $args
fi
#If executable was made delete it
if [ -f $cFile ]
then
rm $cFile
fi
}
javaFile(){
#Quickly run and compile java code
#Compile the .java file
error=$(javac $1)
#If executable was generated run it
if [ $? -eq 0 ];then
cFile=$(echo $1|rev |cut -d"/" -f1|rev|cut -d. -f1)
args=" "
#Create arguements string
for i in $@
do
if [ $i != $1 ]
then
args="$args $i"
fi
done
#Run the executable
java $cFile $args
fi
#Remove the .class file is generated
if [ -f "$cFile.class" ]
then
rm "$cFile.class"
fi
}
rustFile(){
error=$(rustc $1)
if [ $? -eq 0 ];then
compiled=$(echo "$1"|rev|cut -d"/" -f1|rev|cut -d. -f1)
args=" "
for i in $@;do
if [ "$i" != "$1" ];then
args="$args $i"
fi
done
./$compiled $args
fi
if [ -f "$compiled" ];then
rm "$compiled"
fi
}
goFile(){
refactoredCode=$(goimports "$1")
if [ -z "$refactoredCode" ];then
echo "problem with goimports"
return
else
echo "$refactoredCode" > "tempFile"
fi
mv "tempFile" $1
for i in $@
do
if [ $i != $1 ]
then
args="$args $i"
fi
done
go run $1 $2
}
latexFile(){
curr=$(pwd)
for i in $@;do
parent=$(echo "$i"|rev|cut --complement -d/ -f1|rev)"/"
filename=$(echo "$i"|rev|cut -d/ -f1|rev)
cd $parent
latexmk -pdf $filename
echo "$i"
lc
cd $curr
done
}
shellScript(){
ext=$(echo "$1"|rev|cut -d. -f1|rev)
line=$(head -1 $1|grep "#!")
if [ "$ext" = "sh" ] || [ -n "$line" ] ;then
if [ -x $1 ];then
$isPath$1
else
chmod +x $1 && $isPath$1 $2
fi
else
echo "Filetype not supported"
fi
}
# Main Build Logic
if [ -z "$1" ]; then
# display usage if no parameters given
echo "Usage: c <path/file_name of type c|c++|java|go|python|rust|lua|tex|any executable>"
echo "Example: c <path/file_name_1.c>"
exit
else
# set Field separator
IFS=$(printf "\n\b")
# Get commanline args
Args=" "
for i in $@;do
if [ "$i" != "$1" ];then
echo "$i"
Args="$Args $i"
fi
done
#Check if absolute path is provided
case "$1" in
/*) isPath=""
;;
*)isPath="./"
esac
if [ -f "$1" ] ; then
case "${1%,}" in
*.c) cFile $isPath"$1" "$Args" ;;
*.cpp) cppFile $isPath"$1" "$Args" ;;
*.java) javaFile $isPath"$1" "$Args" ;;
*.go) goFile $isPath"$1" "$Args" ;;
*.rs) rustFile $isPath"$1" "$Args";;
*.py) python $isPath"$1" "$Args";;
*.lua) lua $isPath"$1" "$Args";;
*.js) node $isPath"$1" "$Args";;
*.tex) for file in $@;do
latexFile $isPath$file
done;;
*) shellScript $isPath$1 $Args;; #if [ -x $1 ]; then
#;;
esac
else
echo "'$1' - file does not exist"
fi
fi
# restore $IFS
IFS=$SAVEIFS