1 | import java.io.*; |
2 | import java.net.URL; |
3 | import java.net.URLConnection; |
4 | import java.security.MessageDigest; |
5 | import java.security.NoSuchAlgorithmException; |
6 | import java.util.ArrayList; |
7 | import java.util.List; |
8 | import java.util.regex.Matcher; |
9 | import java.util.regex.Pattern; |
10 | |
11 | /** |
12 | Changes to v4: |
13 | input=<directory> argument |
14 | */ |
15 | |
16 | public class x5 { |
17 | static boolean verbose = false; |
18 | static boolean v2mode = false; |
19 | |
20 | private static String isCompilable = "http://tinybrain.de/" |
21 | + /*"564"*/"1000251"; // silenced version (but I think we're silencing now anyway) |
22 | private static String isCompilableHash = "8fe3851d7a686b60546a2429a2bde0d9"; |
23 | |
24 | private static String mainAdder = "http://tinybrain.de/1000252"; |
25 | private static String mainAdderHash = "ce1e89785facd87ea64a6d9ad16a7aec"; |
26 | |
27 | public static void main(String[] args) throws IOException { |
28 | java.io.File ioBaseDir = new java.io.File("."), inputDir = null; |
29 | String src = "."; |
30 | for (int i = 0; i < args.length; i++) { |
31 | String arg = args[i]; |
32 | if (arg.equals("-v")) |
33 | verbose = true; |
34 | else if (arg.equals("-v2")) |
35 | v2mode = true; |
36 | else if (arg.equals("-finderror")) |
37 | verbose = v2mode = true; |
38 | else if (arg.startsWith("input=")) |
39 | inputDir = new java.io.File(arg.substring(6)); |
40 | else |
41 | src = arg; |
42 | } |
43 | |
44 | if (inputDir != null) { |
45 | ioBaseDir = TempDirMaker_make(); |
46 | System.out.println("Taking input from: " + inputDir.getAbsolutePath()); |
47 | System.out.println("Output is in: " + new java.io.File(ioBaseDir, "output").getAbsolutePath()); |
48 | copyInput(inputDir, new java.io.File(ioBaseDir, "input")); |
49 | } |
50 | |
51 | javax4(src, ioBaseDir); |
52 | } |
53 | |
54 | public static void javax4(String src, java.io.File ioDir) throws IOException { |
55 | java.io.File srcDir; |
56 | if (isSnippetID(src)) |
57 | srcDir = loadSnippetAsMainJava(src); |
58 | else |
59 | srcDir = new java.io.File(src); |
60 | java.io.File X = programToInput(srcDir); |
61 | |
62 | if (!v2mode) { |
63 | boolean compilable = isCompilable(X); |
64 | if (verbose) System.out.println("Compilable: " + compilable); |
65 | if (!compilable) { |
66 | X = expandProgram(X); |
67 | java.io.File file = new java.io.File(X, "main.java"); |
68 | String program = loadTextFile(file.getPath(), null); |
69 | if (verbose) System.out.println("New program:\n" + program); |
70 | if (program == null) |
71 | throw new RuntimeException("Expansion failed (tried: " + file.getPath() + ")"); |
72 | } |
73 | } |
74 | |
75 | java.io.File Y = luaPrintToJavaPrint(X); |
76 | javax2(Y, ioDir, false); |
77 | } |
78 | |
79 | private static java.io.File luaPrintToJavaPrint(java.io.File x) throws IOException { |
80 | java.io.File newDir = TempDirMaker_make(); |
81 | String code = loadTextFile(new java.io.File(x, "main.java").getPath(), null); |
82 | code = luaPrintToJavaPrint(code); |
83 | if (verbose) |
84 | System.out.println(code); |
85 | saveTextFile(new java.io.File(newDir, "main.java").getPath(), code); |
86 | return newDir; |
87 | } |
88 | |
89 | public static String luaPrintToJavaPrint(String code) { |
90 | return ("\n" + code).replaceAll( |
91 | "(\n\\s*)print (\".*\")", |
92 | "$1System.out.println($2);").substring(1); |
93 | } |
94 | |
95 | public static java.io.File loadSnippetAsMainJava(String snippetID) throws IOException { |
96 | java.io.File srcDir = TempDirMaker_make(); |
97 | saveTextFile(new java.io.File(srcDir, "main.java").getPath(), loadSnippet(snippetID, false)); |
98 | return srcDir; |
99 | } |
100 | |
101 | public static java.io.File loadSnippetAsMainJavaVerified(String snippetID, String hash) throws IOException { |
102 | java.io.File srcDir = TempDirMaker_make(); |
103 | saveTextFile(new java.io.File(srcDir, "main.java").getPath(), loadSnippetVerified(snippetID, hash)); |
104 | return srcDir; |
105 | } |
106 | |
107 | private static java.io.File expandProgram(java.io.File x) throws IOException { |
108 | return runJavaX2_on_snippet(mainAdder, mainAdderHash, x, true); |
109 | } |
110 | |
111 | private static boolean isCompilable(java.io.File x) throws IOException { |
112 | java.io.File output = runJavaX2_on_snippet(isCompilable, isCompilableHash, x, true); |
113 | return hasFile(output, "is-compilable"); |
114 | } |
115 | |
116 | private static File runJavaX2_on_snippet(String snippetID, String hash, File x, boolean silent) throws IOException { |
117 | java.io.File srcDir = loadSnippetAsMainJavaVerified(snippetID, hash); |
118 | return runJavaX(srcDir, x, silent); |
119 | } |
120 | |
121 | private static File runJavaX(File originalSrcDir, File originalInput, boolean silent) throws IOException { |
122 | java.io.File ioBaseDir = TempDirMaker_make(); |
123 | java.io.File srcDir = new java.io.File(ioBaseDir, "src"); |
124 | java.io.File inputDir = new java.io.File(ioBaseDir, "input"); |
125 | File input = inputDir; |
126 | java.io.File outputDir = new java.io.File(ioBaseDir, "output"); |
127 | File output = outputDir; |
128 | copyInput(originalSrcDir, srcDir); |
129 | copyInput(originalInput, input); |
130 | javax2(srcDir, ioBaseDir, silent); |
131 | return output; |
132 | } |
133 | |
134 | private static void copyInput(File src, File dst) throws IOException { |
135 | copyDirectory(src, dst); |
136 | } |
137 | |
138 | private static File programToInput(java.io.File srcDir) { |
139 | return srcDir; |
140 | } |
141 | |
142 | public static boolean hasFile(File inputDir, String name) { |
143 | return new File(inputDir, name).exists(); |
144 | } |
145 | |
146 | public static void copyDirectory(java.io.File src, java.io.File dst) throws IOException { |
147 | if (verbose) System.out.println("Copying " + src.getAbsolutePath() + " to " + dst.getAbsolutePath()); |
148 | dst.mkdirs(); |
149 | java.io.File[] files = src.listFiles(); |
150 | if (files == null) return; |
151 | for (java.io.File file : files) { |
152 | java.io.File dst1 = new java.io.File(dst, file.getName()); |
153 | if (file.isDirectory()) |
154 | copyDirectory(file, dst1); |
155 | else { |
156 | if (verbose) System.out.println("Copying " + file.getAbsolutePath() + " to " + dst1.getAbsolutePath()); |
157 | copy(file, dst1); |
158 | } |
159 | } |
160 | } |
161 | |
162 | /** Quickly copy a file without a progress bar or any other fancy GUI... :) */ |
163 | public static void copy(java.io.File src, java.io.File dest) throws IOException { |
164 | FileInputStream inputStream = new FileInputStream(src); |
165 | FileOutputStream outputStream = new FileOutputStream(dest); |
166 | try { |
167 | copy(inputStream, outputStream); |
168 | inputStream.close(); |
169 | } finally { |
170 | outputStream.close(); |
171 | } |
172 | } |
173 | |
174 | public static void copy(InputStream in, OutputStream out) throws IOException { |
175 | byte[] buf = new byte[65536]; |
176 | while (true) { |
177 | int n = in.read(buf); |
178 | if (n <= 0) return; |
179 | out.write(buf, 0, n); |
180 | } |
181 | } |
182 | |
183 | /** writes safely (to temp file, then rename) */ |
184 | public static void saveTextFile(String fileName, String contents) throws IOException { |
185 | java.io.File file = new java.io.File(fileName); |
186 | java.io.File parentFile = file.getParentFile(); |
187 | if (parentFile != null) |
188 | parentFile.mkdirs(); |
189 | String tempFileName = fileName + "_temp"; |
190 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
191 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles); |
192 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
193 | printWriter.print(contents); |
194 | printWriter.close(); |
195 | if (file.exists() && !file.delete()) |
196 | throw new IOException("Can't delete " + fileName); |
197 | |
198 | if (!new java.io.File(tempFileName).renameTo(file)) |
199 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
200 | } |
201 | |
202 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
203 | if (!new java.io.File(fileName).exists()) |
204 | return defaultContents; |
205 | |
206 | FileInputStream fileInputStream = new FileInputStream(fileName); |
207 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles); |
208 | return loadTextFile(inputStreamReader); |
209 | } |
210 | |
211 | public static String loadTextFile(Reader reader) throws IOException { |
212 | StringBuilder builder = new StringBuilder(); |
213 | try { |
214 | BufferedReader bufferedReader = new BufferedReader(reader); |
215 | String line; |
216 | while ((line = bufferedReader.readLine()) != null) |
217 | builder.append(line).append('\n'); |
218 | } finally { |
219 | reader.close(); |
220 | } |
221 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
222 | } |
223 | |
224 | static java.io.File DiskSnippetCache_dir; |
225 | |
226 | public static void initDiskSnippetCache(java.io.File dir) { |
227 | DiskSnippetCache_dir = dir; |
228 | dir.mkdirs(); |
229 | } |
230 | |
231 | public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { |
232 | return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); |
233 | } |
234 | |
235 | private static java.io.File DiskSnippetCache_getFile(long snippetID) { |
236 | return new java.io.File(DiskSnippetCache_dir, "" + snippetID); |
237 | } |
238 | |
239 | public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { |
240 | saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); |
241 | } |
242 | |
243 | public static java.io.File DiskSnippetCache_getDir() { |
244 | return DiskSnippetCache_dir; |
245 | } |
246 | |
247 | public static void initSnippetCache() { |
248 | if (DiskSnippetCache_dir == null) |
249 | initDiskSnippetCache(new java.io.File(System.getProperty("user.home"), ".tinybrain/snippet-cache")); |
250 | } |
251 | |
252 | public static String loadSnippetVerified(String snippetID, String hash) throws IOException { |
253 | String text = loadSnippet(snippetID, !hash.isEmpty()); |
254 | String realHash = getHash(text.getBytes("UTF-8")); |
255 | if (!realHash.equals(hash)) { |
256 | String msg; |
257 | if (hash.isEmpty()) |
258 | msg = "Here's your hash for " + snippetID + ", please put in your program: " + realHash; |
259 | else |
260 | msg = "Hash mismatch for " + snippetID + ": " + realHash + " (new) vs " + hash + " - has tinybrain.de been hacked??"; |
261 | throw new RuntimeException(msg); |
262 | } |
263 | return text; |
264 | } |
265 | |
266 | public static String getHash(byte[] data) { |
267 | return bytesToHex(getFullFingerprint(data)); |
268 | } |
269 | |
270 | public static byte[] getFullFingerprint(byte[] data) { |
271 | try { |
272 | return MessageDigest.getInstance("MD5").digest(data); |
273 | } catch (NoSuchAlgorithmException e) { |
274 | throw new RuntimeException(e); |
275 | } |
276 | } |
277 | |
278 | public static String bytesToHex(byte[] bytes) { |
279 | return bytesToHex(bytes, 0, bytes.length); |
280 | } |
281 | |
282 | public static String bytesToHex(byte[] bytes, int ofs, int len) { |
283 | StringBuilder stringBuilder = new StringBuilder(len*2); |
284 | for (int i = 0; i < len; i++) { |
285 | String s = "0" + Integer.toHexString(bytes[ofs+i]); |
286 | stringBuilder.append(s.substring(s.length()-2, s.length())); |
287 | } |
288 | return stringBuilder.toString(); |
289 | } |
290 | |
291 | public static String loadSnippet(String snippetID, boolean preferCached) throws IOException { |
292 | return loadSnippet(parseSnippetID(snippetID), preferCached); |
293 | } |
294 | |
295 | public static long parseSnippetID(String snippetID) { |
296 | return Long.parseLong(shortenSnippetID(snippetID)); |
297 | } |
298 | |
299 | private static String shortenSnippetID(String snippetID) { |
300 | if (snippetID.startsWith("#")) |
301 | snippetID = snippetID.substring(1); |
302 | String httpBlaBla = "http://tinybrain.de/"; |
303 | if (snippetID.startsWith(httpBlaBla)) |
304 | snippetID = snippetID.substring(httpBlaBla.length()); |
305 | return snippetID; |
306 | } |
307 | |
308 | public static boolean isSnippetID(String snippetID) { |
309 | snippetID = shortenSnippetID(snippetID); |
310 | return isInteger(snippetID) && Long.parseLong(snippetID) != 0; |
311 | } |
312 | |
313 | public static boolean isInteger(String s) { |
314 | return Pattern.matches("\\-?\\d+", s); |
315 | } |
316 | |
317 | public static String loadSnippet(long snippetID, boolean preferCached) throws IOException { |
318 | if (preferCached) { |
319 | initSnippetCache(); |
320 | String text = DiskSnippetCache_get(snippetID); |
321 | if (text != null) |
322 | return text; |
323 | } |
324 | |
325 | String text; |
326 | try { |
327 | URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID); |
328 | text = loadPage(url); |
329 | } catch (FileNotFoundException e) { |
330 | throw new IOException("Snippet #" + snippetID + " not found or not public"); |
331 | } |
332 | |
333 | try { |
334 | initSnippetCache(); |
335 | DiskSnippetCache_put(snippetID, text); |
336 | } catch (IOException e) { |
337 | System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); |
338 | } |
339 | |
340 | return text; |
341 | } |
342 | |
343 | private static String loadPage(URL url) throws IOException { |
344 | System.out.println("Loading: " + url.toExternalForm()); |
345 | URLConnection con = url.openConnection(); |
346 | return loadPage(con, url); |
347 | } |
348 | |
349 | public static String loadPage(URLConnection con, URL url) throws IOException { |
350 | String contentType = con.getContentType(); |
351 | if (contentType == null) |
352 | throw new IOException("Page could not be read: " + url); |
353 | //Log.info("Content-Type: " + contentType); |
354 | String charset = guessCharset(contentType); |
355 | Reader r = new InputStreamReader(con.getInputStream(), charset); |
356 | StringBuilder buf = new StringBuilder(); |
357 | while (true) { |
358 | int ch = r.read(); |
359 | if (ch < 0) |
360 | break; |
361 | //Log.info("Chars read: " + buf.length()); |
362 | buf.append((char) ch); |
363 | } |
364 | return buf.toString(); |
365 | } |
366 | |
367 | public static String guessCharset(String contentType) { |
368 | Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*"); |
369 | Matcher m = p.matcher(contentType); |
370 | /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ |
371 | return m.matches() ? m.group(1) : "ISO-8859-1"; |
372 | } |
373 | |
374 | public static void javax2(java.io.File srcDir, java.io.File ioBaseDir, boolean silent) throws IOException { |
375 | List<java.io.File> sources = new ArrayList<java.io.File>(); |
376 | if (verbose) System.out.println("Scanning for sources in " + srcDir.getPath()); |
377 | scanForSources(srcDir, sources, true); |
378 | if (sources.isEmpty()) { |
379 | System.out.println("No sources found"); |
380 | return; |
381 | } |
382 | java.io.File optionsFile = java.io.File.createTempFile("javax", ""); |
383 | java.io.File classesDir = TempDirMaker_make(); |
384 | if (verbose) System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath()); |
385 | String options = "-d " + bashQuote(classesDir.getPath()); |
386 | writeOptions(sources, optionsFile, options); |
387 | classesDir.mkdirs(); |
388 | invokeJavac(optionsFile); |
389 | if (verbose) System.out.println("Running program (class main.java)\n"); |
390 | runProgram(classesDir, ioBaseDir, silent); |
391 | } |
392 | |
393 | private static void runProgram(java.io.File classesDir, java.io.File ioBaseDir, boolean silent) throws IOException { |
394 | boolean echoOK = false; |
395 | String bashCmd = "(cd " + bashQuote(ioBaseDir.getAbsolutePath()) + " && (java -cp " |
396 | + bashQuote(classesDir.getAbsolutePath()) + " main" + (echoOK ? "; echo ok" : "") + "))"; |
397 | if (verbose) System.out.println(bashCmd); |
398 | String output = backtick(bashCmd); |
399 | if (!silent) |
400 | System.out.println(output); |
401 | } |
402 | |
403 | private static void invokeJavac(java.io.File optionsFile) throws IOException { |
404 | String javacOutput = backtick("javac " + bashQuote("@" + optionsFile.getPath())); |
405 | if (verbose) System.out.println(javacOutput); |
406 | } |
407 | |
408 | private static void writeOptions(List<java.io.File> sources, java.io.File sourcesFile, String moreOptions) throws IOException { |
409 | FileWriter writer = new FileWriter(sourcesFile); |
410 | for (java.io.File source : sources) |
411 | writer.write(bashQuote(source.getPath()) + " "); |
412 | writer.write(moreOptions); |
413 | writer.close(); |
414 | } |
415 | |
416 | private static void scanForSources(java.io.File source, List<java.io.File> sources, boolean topLevel) { |
417 | if (source.isFile() && source.getName().endsWith(".java")) |
418 | sources.add(source); |
419 | else if (source.isDirectory() && !isSkippedDirectoryName(source.getName(), topLevel)) { |
420 | java.io.File[] files = source.listFiles(); |
421 | for (java.io.File file : files) |
422 | scanForSources(file, sources, false); |
423 | } |
424 | } |
425 | |
426 | private static boolean isSkippedDirectoryName(String name, boolean topLevel) { |
427 | if (topLevel) return false; // input or output ok as highest directory (intentionally specified by user, not just found by a directory scan in which case we probably don't want it. it's more like heuristics actually.) |
428 | return name.equalsIgnoreCase("input") || name.equalsIgnoreCase("output"); |
429 | } |
430 | |
431 | public static String backtick(String cmd) throws IOException { |
432 | java.io.File outFile = java.io.File.createTempFile("_backtick", ""); |
433 | java.io.File scriptFile = java.io.File.createTempFile("_backtick", ""); |
434 | |
435 | String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1"; |
436 | //Log.info("[Backtick] " + command); |
437 | try { |
438 | saveTextFile(scriptFile.getPath(), command); |
439 | String[] command2 = {"/bin/bash", scriptFile.getPath() }; |
440 | Process process = Runtime.getRuntime().exec(command2); |
441 | try { |
442 | process.waitFor(); |
443 | } catch (InterruptedException e) { |
444 | throw new RuntimeException(e); |
445 | } |
446 | int value = process.exitValue(); |
447 | //Log.info("exit value: " + value); |
448 | return loadTextFile(outFile.getPath(), ""); |
449 | } finally { |
450 | scriptFile.delete(); |
451 | } |
452 | } |
453 | |
454 | /** possibly improvable */ |
455 | public static String bashQuote(String text) { |
456 | if (text == null) return null; |
457 | return "\"" + text |
458 | .replace("\\", "\\\\") |
459 | .replace("\"", "\\\"") |
460 | .replace("\n", "\\n") |
461 | .replace("\r", "\\r") + "\""; |
462 | } |
463 | |
464 | public final static String charsetForTextFiles = "UTF8"; |
465 | |
466 | static long TempDirMaker_lastValue; |
467 | |
468 | public static java.io.File TempDirMaker_make() { |
469 | java.io.File dir = new java.io.File(System.getProperty("user.home"), ".javax/" + TempDirMaker_newValue()); |
470 | dir.mkdirs(); |
471 | return dir; |
472 | } |
473 | |
474 | private static long TempDirMaker_newValue() { |
475 | long value; |
476 | do |
477 | value = System.currentTimeMillis(); |
478 | while (value == TempDirMaker_lastValue); |
479 | TempDirMaker_lastValue = value; |
480 | return value; |
481 | } |
482 | } |
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #577 |
Snippet name: | x5.java, updated (full source code of JavaX 5) |
Eternal ID of this version: | #577/1 |
Text MD5: | 78605858f84f7cd25fa3db8b38b39c58 |
Author: | stefan |
Category: | |
Type: | Java source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-04-14 18:27:54 |
Source code size: | 18082 bytes / 482 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 854 / 240 |
Referenced in: | [show references] |