996
|
1 #!/usr/bin/env bash
|
|
2 # -*- coding: utf-8 -*-
|
|
3 ###########################################################################
|
|
4 # #
|
|
5 # cfunge - A standard-conforming Befunge93/98/109 interpreter in C. #
|
|
6 # Copyright (C) 2008-2009 Arvid Norlander #
|
|
7 # #
|
|
8 # This program is free software: you can redistribute it and/or modify #
|
|
9 # it under the terms of the GNU General Public License as published by #
|
|
10 # the Free Software Foundation, either version 3 of the License, or #
|
|
11 # (at the proxy's option) any later version. Arvid Norlander is a #
|
|
12 # proxy who can decide which future versions of the GNU General Public #
|
|
13 # License can be used. #
|
|
14 # #
|
|
15 # This program is distributed in the hope that it will be useful, #
|
|
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
|
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
|
18 # GNU General Public License for more details. #
|
|
19 # #
|
|
20 # You should have received a copy of the GNU General Public License #
|
|
21 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
|
22 # #
|
|
23 ###########################################################################
|
|
24
|
|
25 # Generate a fingerprint template.
|
|
26 # This must be run from top source directory.
|
|
27
|
|
28 # Error to fail with for old bash.
|
|
29 fail_old_bash() {
|
|
30 echo "Sorry your bash version is too old!"
|
|
31 echo "You need at least version 3.2.10 of bash"
|
|
32 echo "Please install a newer version:"
|
|
33 echo " * Either use your distro's packages"
|
|
34 echo " * Or see http://www.gnu.org/software/bash/"
|
|
35 exit 2
|
|
36 }
|
|
37
|
|
38 # Check bash version. We need at least 3.2.10
|
|
39 # Lets not use anything like =~ here because
|
|
40 # that may not work on old bash versions.
|
|
41 if [[ "${BASH_VERSINFO[0]}${BASH_VERSINFO[1]}" -lt 32 ]]; then
|
|
42 fail_old_bash
|
|
43 elif [[ "${BASH_VERSINFO[0]}${BASH_VERSINFO[1]}" -eq 32 && "${BASH_VERSINFO[2]}" -lt 10 ]]; then
|
|
44 fail_old_bash
|
|
45 fi
|
|
46
|
|
47 if [[ ! -d src/fingerprints ]]; then
|
|
48 echo "ERROR: Run from top source directory please." >&2
|
|
49 exit 1
|
|
50 fi
|
|
51
|
|
52 set -e
|
|
53
|
|
54 if [[ ! -f tools/fprint_funcs.sh ]]; then
|
|
55 echo "ERROR: Couldn't find tools/fprint_funcs.sh." >&2
|
|
56 exit 1
|
|
57 fi
|
|
58 source tools/fprint_funcs.sh
|
|
59 if [[ $? -ne 0 ]]; then
|
|
60 echo "ERROR: Couldn't load tools/fprint_funcs.sh." >&2
|
|
61 exit 1
|
|
62 fi
|
|
63
|
|
64 # Variables
|
|
65 FPRINT=""
|
|
66 fp_URL=""
|
|
67 fp_SAFE=""
|
|
68 fp_CONDITION=""
|
|
69 fp_OPCODES=""
|
|
70 fp_DESCRIPTION=""
|
|
71
|
|
72 fp_OPCODES=""
|
|
73 fp_OPCODE_NAMES=()
|
|
74 fp_OPCODE_DESC=()
|
|
75
|
|
76 if [[ -z $1 ]]; then
|
|
77 echo "ERROR: Please provide finger print name!" >&2
|
|
78 echo "Usage: $0 FingerprintName opcodes" >&2
|
|
79 exit 1
|
|
80 else
|
|
81 FPRINT="$1"
|
|
82 fi
|
|
83
|
|
84 progress "Sanity checking parameters"
|
|
85 checkfprint "$FPRINT"
|
|
86
|
|
87 if [[ -e src/fingerprints/$FPRINT ]]; then
|
|
88 die "A fingerprint with that name already exists"
|
|
89 fi
|
|
90
|
|
91 progress "Looking for spec file"
|
|
92
|
|
93 if [[ -f "src/fingerprints/${FPRINT}.spec" ]]; then
|
|
94 status "Good, spec file found."
|
|
95 else
|
|
96 die "Sorry, but you need a spec file for the fingerprint." \
|
|
97 "Either you misspelled the parameter to this script, or you misspelled" \
|
|
98 "the spec file. Or you forgot to create a spec file." \
|
|
99 "If you didn't typo the name on the command line to this script then" \
|
|
100 "the spec file should be placed at src/fingerprints/${FPRINT}.spec"
|
|
101 fi
|
|
102
|
|
103 cd "src/fingerprints" || die "Couldn't change directory to src/fingerprints"
|
|
104
|
|
105 progress "Parsing spec file"
|
|
106 parse_spec "${FPRINT}"
|
|
107
|
|
108 addtoh() {
|
|
109 echo "$1" >> "${FPRINT}.h"
|
|
110 }
|
|
111 addtoc() {
|
|
112 echo "$1" >> "${FPRINT}.c"
|
|
113 }
|
|
114
|
|
115
|
|
116
|
|
117 progress "Creating directory"
|
|
118 mkdir "$FPRINT" || die "mkdir failed"
|
|
119 cd "$FPRINT" || die "cd to src/fingerprints/$FPRINT failed"
|
|
120
|
|
121 progress "Creating header file"
|
|
122 cat > "${FPRINT}.h" << EOF
|
|
123 /* -*- mode: C; coding: utf-8; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
|
|
124 *
|
|
125 * cfunge - A standard-conforming Befunge93/98/109 interpreter in C.
|
|
126 * Copyright (C) 2008-2009 Arvid Norlander <anmaster AT tele2 DOT se>
|
|
127 *
|
|
128 * This program is free software: you can redistribute it and/or modify
|
|
129 * it under the terms of the GNU General Public License as published by
|
|
130 * the Free Software Foundation, either version 3 of the License, or
|
|
131 * (at the proxy's option) any later version. Arvid Norlander is a
|
|
132 * proxy who can decide which future versions of the GNU General Public
|
|
133 * License can be used.
|
|
134 *
|
|
135 * This program is distributed in the hope that it will be useful,
|
|
136 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
137 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
138 * GNU General Public License for more details.
|
|
139 *
|
|
140 * You should have received a copy of the GNU General Public License
|
|
141 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
142 */
|
|
143
|
|
144 EOF
|
|
145
|
|
146 addtoh "#ifndef FUNGE_HAD_SRC_FINGERPRINTS_${FPRINT}_H"
|
|
147 addtoh "#define FUNGE_HAD_SRC_FINGERPRINTS_${FPRINT}_H"
|
|
148
|
|
149
|
|
150 cat >> "${FPRINT}.h" << EOF
|
|
151
|
|
152 #include "../../global.h"
|
|
153 #include "../manager.h"
|
|
154
|
|
155 EOF
|
|
156
|
|
157 if [[ "$fp_CONDITION" ]]; then
|
|
158 addtoh "#if $fp_CONDITION"
|
|
159 fi
|
|
160
|
|
161 addtoh "bool finger_${FPRINT}_load(instructionPointer * ip);"
|
|
162
|
|
163 if [[ "$fp_CONDITION" ]]; then
|
|
164 addtoh "#endif /* $fp_CONDITION */"
|
|
165 fi
|
|
166
|
|
167 addtoh ""
|
|
168 addtoh "#endif"
|
|
169
|
|
170 ##############
|
|
171 # Now for .c #
|
|
172 ##############
|
|
173
|
|
174 progress "Creating source file"
|
|
175 cat > "${FPRINT}.c" << EOF
|
|
176 /* -*- mode: C; coding: utf-8; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
|
|
177 *
|
|
178 * cfunge - A standard-conforming Befunge93/98/109 interpreter in C.
|
|
179 * Copyright (C) 2008-2009 Arvid Norlander <anmaster AT tele2 DOT se>
|
|
180 *
|
|
181 * This program is free software: you can redistribute it and/or modify
|
|
182 * it under the terms of the GNU General Public License as published by
|
|
183 * the Free Software Foundation, either version 3 of the License, or
|
|
184 * (at the proxy's option) any later version. Arvid Norlander is a
|
|
185 * proxy who can decide which future versions of the GNU General Public
|
|
186 * License can be used.
|
|
187 *
|
|
188 * This program is distributed in the hope that it will be useful,
|
|
189 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
190 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
191 * GNU General Public License for more details.
|
|
192 *
|
|
193 * You should have received a copy of the GNU General Public License
|
|
194 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
195 */
|
|
196
|
|
197 EOF
|
|
198 addtoc "#include \"${FPRINT}.h\""
|
|
199
|
|
200 if [[ "$fp_CONDITION" ]]; then
|
|
201 addtoc "#if $fp_CONDITION"
|
|
202 fi
|
|
203
|
|
204 cat >> "${FPRINT}.c" << EOF
|
|
205 #include "../../stack.h"
|
|
206
|
|
207 // TODO: Add code to template functions
|
|
208
|
|
209 EOF
|
|
210
|
|
211 for (( i = 0; i < ${#fp_OPCODES}; i++ )); do
|
|
212 ord number "${fp_OPCODES:$i:1}"
|
|
213 addtoc "/// ${fp_OPCODES:$i:1} - ${fp_OPCODE_DESC[$number]}"
|
|
214 addtoc "static void finger_${FPRINT}_${fp_OPCODE_NAMES[$number]}(instructionPointer * ip)"
|
|
215 addtoc '{'
|
|
216 addtoc '}'
|
|
217 addtoc ''
|
|
218 done
|
|
219
|
|
220
|
|
221
|
|
222 addtoc "bool finger_${FPRINT}_load(instructionPointer * ip)"
|
|
223 addtoc '{'
|
|
224 for (( i = 0; i < ${#fp_OPCODES}; i++ )); do
|
|
225 ord number "${fp_OPCODES:$i:1}"
|
|
226 addtoc " manager_add_opcode(${FPRINT}, '${fp_OPCODES:$i:1}', ${fp_OPCODE_NAMES[$number]})"
|
|
227 done
|
|
228
|
|
229 cat >> "${FPRINT}.c" << EOF
|
|
230 return true;
|
|
231 }
|
|
232 EOF
|
|
233
|
|
234 if [[ "$fp_CONDITION" ]]; then
|
|
235 addtoc "#endif /* $fp_CONDITION */"
|
|
236 fi
|
|
237
|
|
238 status "File creation done"
|
|
239 echo
|
|
240 echo "To make cfunge aware of the new fingerprint run tools/gen_fprint_list.sh"
|
|
241 echo "You may need to run cmake or similar to make the build system aware as well."
|
|
242 echo
|
|
243 echo "All done! However make sure the copyright in the files is correct. Oh, and another thing: implement the fingerprint :)"
|