1 | import java.io.*; |
2 | import java.lang.reflect.Method; |
3 | import java.net.URL; |
4 | import java.net.URLClassLoader; |
5 | import java.net.URLConnection; |
6 | import java.security.MessageDigest; |
7 | import java.security.NoSuchAlgorithmException; |
8 | import java.util.*; |
9 | import java.util.regex.Matcher; |
10 | import java.util.regex.Pattern; |
11 | |
12 | /** |
13 | JavaX runner version 14. |
14 | |
15 | Changes to v13: |
16 | -transmit command-line arguments to JavaX program (in "args" variable) |
17 | Example: x14 639 2000380 (run #639 with input #2000380) |
18 | [-disabled: transmit arguments from invocation line to translators] |
19 | -make computer id for anonymous stats |
20 | -"-noid" option for disabling computer id |
21 | |
22 | */ |
23 | |
24 | public class x14 { |
25 | static final String version = "JavaX 14"; |
26 | |
27 | static boolean verbose = false, translate = false, list = false, virtualizeTranslators = true; |
28 | static String translateTo = null; |
29 | static boolean preferCached = false, safeOnly = false, noID = false; |
30 | static List<String[]> mainTranslators = new ArrayList<String[]>(); |
31 | private static Map<Long, String> memSnippetCache = new HashMap<Long, String>(); |
32 | private static int processesStarted; |
33 | |
34 | public static void main(String[] args) throws Exception { |
35 | File ioBaseDir = new File("."), inputDir = null, outputDir = null; |
36 | String src = null; |
37 | List<String> programArgs = new ArrayList<String>(); |
38 | |
39 | for (int i = 0; i < args.length; i++) { |
40 | String arg = args[i]; |
41 | if (arg.equals("-v") || arg.equals("-verbose")) |
42 | verbose = true; |
43 | else if (arg.equals("-finderror")) |
44 | verbose = true; |
45 | else if (arg.equals("-offline") || arg.equalsIgnoreCase("-prefercached")) |
46 | preferCached = true; |
47 | else if (arg.equals("-novirt")) |
48 | virtualizeTranslators = false; |
49 | else if (arg.equals("-safeonly")) |
50 | safeOnly = true; |
51 | else if (arg.equals("-noid")) |
52 | noID = true; |
53 | else if (arg.equals("translate")) |
54 | translate = true; |
55 | else if (arg.equals("list")) |
56 | list = true; |
57 | else if (arg.equals("run")) { |
58 | // it's the default command anyway |
59 | } else if (arg.startsWith("input=")) |
60 | inputDir = new File(arg.substring(6)); |
61 | else if (arg.startsWith("output=")) |
62 | outputDir = new File(arg.substring(7)); |
63 | else if (arg.equals("with")) |
64 | mainTranslators.add(new String[] {args[++i], null}); |
65 | else if (translate && arg.equals("to")) |
66 | translateTo = args[++i]; |
67 | else if (src == null) { |
68 | //System.out.println("src=" + arg); |
69 | src = arg; |
70 | } else |
71 | programArgs.add(arg); |
72 | } |
73 | |
74 | if (src == null) src = "."; |
75 | |
76 | if (virtualizeTranslators && !preferCached) |
77 | initDiskSnippetCache(TempDirMaker_make()); |
78 | |
79 | if (inputDir != null) { |
80 | ioBaseDir = TempDirMaker_make(); |
81 | System.out.println("Taking input from: " + inputDir.getAbsolutePath()); |
82 | System.out.println("Output is in: " + new File(ioBaseDir, "output").getAbsolutePath()); |
83 | copyInput(inputDir, new File(ioBaseDir, "input")); |
84 | } |
85 | |
86 | javax4(src, ioBaseDir, translate, list, programArgs.toArray(new String[programArgs.size()])); |
87 | |
88 | if (outputDir != null) { |
89 | copyInput(new File(ioBaseDir, "output"), outputDir); |
90 | System.out.println("Output copied to: " + outputDir.getAbsolutePath()); |
91 | } |
92 | |
93 | if (verbose) |
94 | System.out.println("Processes started: " + processesStarted); |
95 | } |
96 | |
97 | public static void javax4(String src, File ioDir, boolean translate, boolean list, |
98 | String[] args) throws Exception { |
99 | File srcDir; |
100 | if (isSnippetID(src)) |
101 | srcDir = loadSnippetAsMainJava(src); |
102 | else { |
103 | srcDir = new File(src); |
104 | if (!new File(srcDir, "main.java").exists()) { |
105 | System.out.println("This is " + version + ".\n" + |
106 | "No main.java found, exiting"); |
107 | return; |
108 | } |
109 | } |
110 | |
111 | // translate |
112 | |
113 | List<File> libraries = new ArrayList<File>(); |
114 | File X = topLevelTranslate(srcDir, libraries); |
115 | |
116 | // list or run |
117 | |
118 | if (translate) { |
119 | File to = X; |
120 | if (translateTo != null) |
121 | if (new File(translateTo).isDirectory()) |
122 | to = new File(translateTo, "main.java"); |
123 | else |
124 | to = new File(translateTo); |
125 | if (to != X) |
126 | copy(new File(X, "main.java"), to); |
127 | System.out.println("Program translated to: " + to.getAbsolutePath()); |
128 | } else if (list) |
129 | System.out.println(loadTextFile(new File(X, "main.java").getPath(), null)); |
130 | else |
131 | javax2(X, ioDir, false, false, libraries, args); |
132 | } |
133 | |
134 | static File topLevelTranslate(File srcDir, List<File> libraries_out) throws Exception { |
135 | File X = srcDir; |
136 | X = applyTranslators(X, mainTranslators, libraries_out); |
137 | X = defaultTranslate(X, libraries_out); |
138 | return X; |
139 | } |
140 | |
141 | private static File defaultTranslate(File x, List<File> libraries_out) throws Exception { |
142 | x = luaPrintToJavaPrint(x); |
143 | x = repeatAutoTranslate(x, libraries_out); |
144 | return x; |
145 | } |
146 | |
147 | private static File repeatAutoTranslate(File x, List<File> libraries_out) throws Exception { |
148 | while (true) { |
149 | File y = autoTranslate(x, libraries_out); |
150 | if (y == x) |
151 | return x; |
152 | x = y; |
153 | } |
154 | } |
155 | |
156 | private static File autoTranslate(File x, List<File> libraries_out) throws Exception { |
157 | String main = loadTextFile(new File(x, "main.java").getPath(), null); |
158 | List<String> lines = toLines(main); |
159 | List<String[]> translators = findTranslators(lines); |
160 | if (translators.isEmpty()) |
161 | return x; |
162 | |
163 | main = fromLines(lines); |
164 | File newDir = TempDirMaker_make(); |
165 | saveTextFile(new File(newDir, "main.java").getPath(), main); |
166 | return applyTranslators(newDir, translators, libraries_out); |
167 | } |
168 | |
169 | private static List<String[]> findTranslators(List<String> lines) { |
170 | List<String[]> translators = new ArrayList<String[]>(); |
171 | Pattern pattern = Pattern.compile("^!([0-9# \t]+)"); |
172 | Pattern pArgs = Pattern.compile("^\\s*\\((.*)\\)"); |
173 | for (ListIterator<String> iterator = lines.listIterator(); iterator.hasNext(); ) { |
174 | String line = iterator.next(); |
175 | line = line.trim(); |
176 | Matcher matcher = pattern.matcher(line); |
177 | if (matcher.find()) { |
178 | String[] t = matcher.group(1).split("[ \t]+"); |
179 | String rest = line.substring(matcher.end()); |
180 | String arg = null; |
181 | if (t.length == 1) { |
182 | Matcher mArgs = pArgs.matcher(rest); |
183 | if (mArgs.find()) |
184 | arg = mArgs.group(1); |
185 | } |
186 | for (String transi : t) |
187 | translators.add(new String[]{transi, arg}); |
188 | iterator.remove(); |
189 | } |
190 | } |
191 | return translators; |
192 | } |
193 | |
194 | public static List<String> toLines(String s) { |
195 | List<String> lines = new ArrayList<String>(); |
196 | int start = 0; |
197 | while (true) { |
198 | int i = toLines_nextLineBreak(s, start); |
199 | if (i < 0) { |
200 | if (s.length() > start) lines.add(s.substring(start)); |
201 | break; |
202 | } |
203 | |
204 | lines.add(s.substring(start, i)); |
205 | if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') |
206 | i += 2; |
207 | else |
208 | ++i; |
209 | |
210 | start = i; |
211 | } |
212 | return lines; |
213 | } |
214 | |
215 | private static int toLines_nextLineBreak(String s, int start) { |
216 | for (int i = start; i < s.length(); i++) { |
217 | char c = s.charAt(i); |
218 | if (c == '\r' || c == '\n') |
219 | return i; |
220 | } |
221 | return -1; |
222 | } |
223 | |
224 | public static String fromLines(List<String> lines) { |
225 | StringBuilder buf = new StringBuilder(); |
226 | for (String line : lines) { |
227 | buf.append(line).append('\n'); |
228 | } |
229 | return buf.toString(); |
230 | } |
231 | |
232 | private static File applyTranslators(File x, List<String[]> translators, List<File> libraries_out) throws Exception { |
233 | for (String[] translator : translators) |
234 | x = applyTranslator(x, translator[0], translator[1], libraries_out); |
235 | return x; |
236 | } |
237 | |
238 | // also takes a library |
239 | private static File applyTranslator(File x, String translator, String arg, List<File> libraries_out) throws Exception { |
240 | if (verbose) |
241 | System.out.println("Using translator " + translator + " on sources in " + x.getPath()); |
242 | |
243 | File newDir = runTranslatorOnInput(translator, null, arg, x, !verbose, libraries_out); |
244 | |
245 | if (!new File(newDir, "main.java").exists()) { |
246 | throw new Exception("Translator " + translator + " did not generate main.java"); |
247 | // TODO: show translator output |
248 | } |
249 | if (verbose) |
250 | System.out.println("Translated with " + translator + " from " + x.getPath() + " to " + newDir.getPath()); |
251 | x = newDir; |
252 | return x; |
253 | } |
254 | |
255 | private static File luaPrintToJavaPrint(File x) throws IOException { |
256 | File newDir = TempDirMaker_make(); |
257 | String code = loadTextFile(new File(x, "main.java").getPath(), null); |
258 | code = luaPrintToJavaPrint(code); |
259 | if (verbose) |
260 | System.out.println(code); |
261 | saveTextFile(new File(newDir, "main.java").getPath(), code); |
262 | return newDir; |
263 | } |
264 | |
265 | public static String luaPrintToJavaPrint(String code) { |
266 | return ("\n" + code).replaceAll( |
267 | "(\n\\s*)print (\".*\")", |
268 | "$1System.out.println($2);").substring(1); |
269 | } |
270 | |
271 | public static File loadSnippetAsMainJava(String snippetID) throws IOException { |
272 | checkProgramSafety(snippetID); |
273 | File srcDir = TempDirMaker_make(); |
274 | saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippet(snippetID)); |
275 | return srcDir; |
276 | } |
277 | |
278 | public static File loadSnippetAsMainJavaVerified(String snippetID, String hash) throws IOException { |
279 | checkProgramSafety(snippetID); |
280 | File srcDir = TempDirMaker_make(); |
281 | saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippetVerified(snippetID, hash)); |
282 | return srcDir; |
283 | } |
284 | |
285 | /** returns output dir */ |
286 | private static File runTranslatorOnInput(String snippetID, String hash, String arg, File input, |
287 | boolean silent, |
288 | List<File> libraries_out) throws Exception { |
289 | File libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID)); |
290 | if (libraryFile != null) { |
291 | loadLibrary(snippetID, libraries_out, libraryFile); |
292 | return input; |
293 | } |
294 | |
295 | File srcDir = hash == null ? loadSnippetAsMainJava(snippetID) |
296 | : loadSnippetAsMainJavaVerified(snippetID, hash); |
297 | |
298 | long mainJavaSize = new File(srcDir, "main.java").length(); |
299 | |
300 | if (mainJavaSize == 0) { // no text in snippet? assume it's a library |
301 | loadLibrary(snippetID, libraries_out, libraryFile); |
302 | return input; |
303 | } |
304 | |
305 | List<File> libraries = new ArrayList<File>(); |
306 | srcDir = defaultTranslate(srcDir, libraries); |
307 | boolean runInProcess = false; |
308 | File ioBaseDir = TempDirMaker_make(); |
309 | |
310 | if (virtualizeTranslators) { |
311 | if (verbose) System.out.println("Virtualizing translator"); |
312 | |
313 | //srcDir = applyTranslator(srcDir, "#2000351"); // I/O-virtualize the translator |
314 | // that doesn't work because it recurses infinitely... |
315 | |
316 | // So we do it right here: |
317 | String s = loadTextFile(new File(srcDir, "main.java").getPath(), null); |
318 | s = s.replaceAll("new\\s+File\\(", "virtual.newFile("); |
319 | s = s.replaceAll("new\\s+FileInputStream\\(", "virtual.newFileInputStream("); |
320 | s = s.replaceAll("new\\s+FileOutputStream\\(", "virtual.newFileOutputStream("); |
321 | s += "\n\n" + loadSnippet("#2000355"); // load class virtual |
322 | |
323 | // change baseDir |
324 | s = s.replace("virtual_baseDir = \"\";", |
325 | "virtual_baseDir = " + javaQuote(ioBaseDir.getAbsolutePath()) + ";"); |
326 | |
327 | // forward snippet cache |
328 | s = s.replace("static File DiskSnippetCache_dir;", |
329 | "static File DiskSnippetCache_dir = new File(" + javaQuote(DiskSnippetCache_dir.getAbsolutePath()) + ");"); |
330 | s = s.replace("static boolean preferCached = false;", "static boolean preferCached = true;"); |
331 | |
332 | if (verbose) { |
333 | System.out.println("==BEGIN VIRTUALIZED TRANSLATOR=="); |
334 | System.out.println(s); |
335 | System.out.println("==END VIRTUALIZED TRANSLATOR=="); |
336 | } |
337 | saveTextFile(new File(srcDir, "main.java").getPath(), s); |
338 | |
339 | // TODO: silence translator also |
340 | runInProcess = true; |
341 | } |
342 | |
343 | return runJavaX(ioBaseDir, srcDir, input, silent, runInProcess, libraries, |
344 | arg != null ? new String[] {arg} : new String[0]); |
345 | } |
346 | |
347 | static void checkProgramSafety(String snippetID) throws IOException { |
348 | if (!safeOnly) return; |
349 | URL url = new URL("http://tinybrain.de:8080/tb-int/is-javax-safe.php?id=" + parseSnippetID(snippetID)); |
350 | String text = loadPage(url); |
351 | if (!text.startsWith("{\"safe\":\"1\"}")) |
352 | throw new RuntimeException("Translator not safe: #" + parseSnippetID(snippetID)); |
353 | } |
354 | |
355 | private static void loadLibrary(String snippetID, List<File> libraries_out, File libraryFile) throws IOException { |
356 | if (verbose) |
357 | System.out.println("Assuming " + snippetID + " is a library."); |
358 | |
359 | if (libraryFile == null) { |
360 | byte[] data; |
361 | try { |
362 | URL url = new URL("http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + parseSnippetID(snippetID) |
363 | + "&contentType=application/binary"); |
364 | System.err.println("Loading library: " + url); |
365 | data = loadBinaryPage(url.openConnection(), url); |
366 | if (verbose) |
367 | System.err.println("Bytes loaded: " + data.length); |
368 | } catch (FileNotFoundException e) { |
369 | throw new IOException("Binary snippet #" + snippetID + " not found or not public"); |
370 | } |
371 | DiskSnippetCache_putLibrary(parseSnippetID(snippetID), data); |
372 | libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID)); |
373 | } |
374 | |
375 | if (!libraries_out.contains(libraryFile)) |
376 | libraries_out.add(libraryFile); |
377 | } |
378 | |
379 | /** returns output dir */ |
380 | private static File runJavaX(File ioBaseDir, File originalSrcDir, File originalInput, |
381 | boolean silent, boolean runInProcess, |
382 | List<File> libraries, String[] args) throws Exception { |
383 | File srcDir = new File(ioBaseDir, "src"); |
384 | File inputDir = new File(ioBaseDir, "input"); |
385 | File outputDir = new File(ioBaseDir, "output"); |
386 | copyInput(originalSrcDir, srcDir); |
387 | copyInput(originalInput, inputDir); |
388 | javax2(srcDir, ioBaseDir, silent, runInProcess, libraries, args); |
389 | return outputDir; |
390 | } |
391 | |
392 | private static void copyInput(File src, File dst) throws IOException { |
393 | copyDirectory(src, dst); |
394 | } |
395 | |
396 | public static boolean hasFile(File inputDir, String name) { |
397 | return new File(inputDir, name).exists(); |
398 | } |
399 | |
400 | public static void copyDirectory(File src, File dst) throws IOException { |
401 | if (verbose) System.out.println("Copying " + src.getAbsolutePath() + " to " + dst.getAbsolutePath()); |
402 | dst.mkdirs(); |
403 | File[] files = src.listFiles(); |
404 | if (files == null) return; |
405 | for (File file : files) { |
406 | File dst1 = new File(dst, file.getName()); |
407 | if (file.isDirectory()) |
408 | copyDirectory(file, dst1); |
409 | else { |
410 | if (verbose) System.out.println("Copying " + file.getAbsolutePath() + " to " + dst1.getAbsolutePath()); |
411 | copy(file, dst1); |
412 | } |
413 | } |
414 | } |
415 | |
416 | /** Quickly copy a file without a progress bar or any other fancy GUI... :) */ |
417 | public static void copy(File src, File dest) throws IOException { |
418 | FileInputStream inputStream = new FileInputStream(src); |
419 | FileOutputStream outputStream = new FileOutputStream(dest); |
420 | try { |
421 | copy(inputStream, outputStream); |
422 | inputStream.close(); |
423 | } finally { |
424 | outputStream.close(); |
425 | } |
426 | } |
427 | |
428 | public static void copy(InputStream in, OutputStream out) throws IOException { |
429 | byte[] buf = new byte[65536]; |
430 | while (true) { |
431 | int n = in.read(buf); |
432 | if (n <= 0) return; |
433 | out.write(buf, 0, n); |
434 | } |
435 | } |
436 | |
437 | /** writes safely (to temp file, then rename) */ |
438 | public static void saveTextFile(String fileName, String contents) throws IOException { |
439 | File file = new File(fileName); |
440 | File parentFile = file.getParentFile(); |
441 | if (parentFile != null) |
442 | parentFile.mkdirs(); |
443 | String tempFileName = fileName + "_temp"; |
444 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
445 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles); |
446 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
447 | printWriter.print(contents); |
448 | printWriter.close(); |
449 | if (file.exists() && !file.delete()) |
450 | throw new IOException("Can't delete " + fileName); |
451 | |
452 | if (!new File(tempFileName).renameTo(file)) |
453 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
454 | } |
455 | |
456 | /** writes safely (to temp file, then rename) */ |
457 | public static void saveBinaryFile(String fileName, byte[] contents) throws IOException { |
458 | File file = new File(fileName); |
459 | File parentFile = file.getParentFile(); |
460 | if (parentFile != null) |
461 | parentFile.mkdirs(); |
462 | String tempFileName = fileName + "_temp"; |
463 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
464 | fileOutputStream.write(contents); |
465 | fileOutputStream.close(); |
466 | if (file.exists() && !file.delete()) |
467 | throw new IOException("Can't delete " + fileName); |
468 | |
469 | if (!new File(tempFileName).renameTo(file)) |
470 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
471 | } |
472 | |
473 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
474 | if (!new File(fileName).exists()) |
475 | return defaultContents; |
476 | |
477 | FileInputStream fileInputStream = new FileInputStream(fileName); |
478 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles); |
479 | return loadTextFile(inputStreamReader); |
480 | } |
481 | |
482 | public static String loadTextFile(Reader reader) throws IOException { |
483 | StringBuilder builder = new StringBuilder(); |
484 | try { |
485 | BufferedReader bufferedReader = new BufferedReader(reader); |
486 | String line; |
487 | while ((line = bufferedReader.readLine()) != null) |
488 | builder.append(line).append('\n'); |
489 | } finally { |
490 | reader.close(); |
491 | } |
492 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
493 | } |
494 | |
495 | static File DiskSnippetCache_dir; |
496 | |
497 | public static void initDiskSnippetCache(File dir) { |
498 | DiskSnippetCache_dir = dir; |
499 | dir.mkdirs(); |
500 | } |
501 | |
502 | // Data files are immutable, use centralized cache |
503 | public static synchronized File DiskSnippetCache_getLibrary(long snippetID) throws IOException { |
504 | File file = new File(getGlobalCache(), "data_" + snippetID + ".jar"); |
505 | if (verbose) |
506 | System.out.println("Checking data cache: " + file.getPath()); |
507 | return file.exists() ? file : null; |
508 | } |
509 | |
510 | public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { |
511 | return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); |
512 | } |
513 | |
514 | private static File DiskSnippetCache_getFile(long snippetID) { |
515 | return new File(DiskSnippetCache_dir, "" + snippetID); |
516 | } |
517 | |
518 | public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { |
519 | saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); |
520 | } |
521 | |
522 | public static synchronized void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException { |
523 | saveBinaryFile(new File(getGlobalCache(), "data_" + snippetID).getPath() + ".jar", data); |
524 | } |
525 | |
526 | public static File DiskSnippetCache_getDir() { |
527 | return DiskSnippetCache_dir; |
528 | } |
529 | |
530 | public static void initSnippetCache() { |
531 | if (DiskSnippetCache_dir == null) |
532 | initDiskSnippetCache(getGlobalCache()); |
533 | } |
534 | |
535 | private static File getGlobalCache() { |
536 | File file = new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"); |
537 | file.mkdirs(); |
538 | return file; |
539 | } |
540 | |
541 | public static String loadSnippetVerified(String snippetID, String hash) throws IOException { |
542 | String text = loadSnippet(snippetID); |
543 | String realHash = getHash(text.getBytes("UTF-8")); |
544 | if (!realHash.equals(hash)) { |
545 | String msg; |
546 | if (hash.isEmpty()) |
547 | msg = "Here's your hash for " + snippetID + ", please put in your program: " + realHash; |
548 | else |
549 | msg = "Hash mismatch for " + snippetID + ": " + realHash + " (new) vs " + hash + " - has tinybrain.de been hacked??"; |
550 | throw new RuntimeException(msg); |
551 | } |
552 | return text; |
553 | } |
554 | |
555 | public static String getHash(byte[] data) { |
556 | return bytesToHex(getFullFingerprint(data)); |
557 | } |
558 | |
559 | public static byte[] getFullFingerprint(byte[] data) { |
560 | try { |
561 | return MessageDigest.getInstance("MD5").digest(data); |
562 | } catch (NoSuchAlgorithmException e) { |
563 | throw new RuntimeException(e); |
564 | } |
565 | } |
566 | |
567 | public static String bytesToHex(byte[] bytes) { |
568 | return bytesToHex(bytes, 0, bytes.length); |
569 | } |
570 | |
571 | public static String bytesToHex(byte[] bytes, int ofs, int len) { |
572 | StringBuilder stringBuilder = new StringBuilder(len*2); |
573 | for (int i = 0; i < len; i++) { |
574 | String s = "0" + Integer.toHexString(bytes[ofs+i]); |
575 | stringBuilder.append(s.substring(s.length()-2, s.length())); |
576 | } |
577 | return stringBuilder.toString(); |
578 | } |
579 | |
580 | public static String loadSnippet(String snippetID) throws IOException { |
581 | return loadSnippet(parseSnippetID(snippetID)); |
582 | } |
583 | |
584 | public static long parseSnippetID(String snippetID) { |
585 | return Long.parseLong(shortenSnippetID(snippetID)); |
586 | } |
587 | |
588 | private static String shortenSnippetID(String snippetID) { |
589 | if (snippetID.startsWith("#")) |
590 | snippetID = snippetID.substring(1); |
591 | String httpBlaBla = "http://tinybrain.de/"; |
592 | if (snippetID.startsWith(httpBlaBla)) |
593 | snippetID = snippetID.substring(httpBlaBla.length()); |
594 | return snippetID; |
595 | } |
596 | |
597 | public static boolean isSnippetID(String snippetID) { |
598 | snippetID = shortenSnippetID(snippetID); |
599 | return isInteger(snippetID) && Long.parseLong(snippetID) != 0; |
600 | } |
601 | |
602 | public static boolean isInteger(String s) { |
603 | return Pattern.matches("\\-?\\d+", s); |
604 | } |
605 | |
606 | public static String loadSnippet(long snippetID) throws IOException { |
607 | String text = memSnippetCache.get(snippetID); |
608 | if (text != null) |
609 | return text; |
610 | |
611 | if (preferCached) { |
612 | initSnippetCache(); |
613 | text = DiskSnippetCache_get(snippetID); |
614 | if (text != null) |
615 | return text; |
616 | } |
617 | |
618 | try { |
619 | URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID); |
620 | text = loadPage(url); |
621 | } catch (FileNotFoundException e) { |
622 | throw new IOException("Snippet #" + snippetID + " not found or not public"); |
623 | } |
624 | |
625 | memSnippetCache.put(snippetID, text); |
626 | |
627 | try { |
628 | initSnippetCache(); |
629 | DiskSnippetCache_put(snippetID, text); |
630 | } catch (IOException e) { |
631 | System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); |
632 | } |
633 | |
634 | return text; |
635 | } |
636 | |
637 | private static String loadPage(URL url) throws IOException { |
638 | System.err.println("Loading: " + url.toExternalForm()); |
639 | URLConnection con = url.openConnection(); |
640 | return loadPage(con, url); |
641 | } |
642 | |
643 | public static String loadPage(URLConnection con, URL url) throws IOException { |
644 | setHeaders(con); |
645 | String contentType = con.getContentType(); |
646 | if (contentType == null) |
647 | throw new IOException("Page could not be read: " + url); |
648 | //Log.info("Content-Type: " + contentType); |
649 | String charset = guessCharset(contentType); |
650 | Reader r = new InputStreamReader(con.getInputStream(), charset); |
651 | StringBuilder buf = new StringBuilder(); |
652 | while (true) { |
653 | int ch = r.read(); |
654 | if (ch < 0) |
655 | break; |
656 | //Log.info("Chars read: " + buf.length()); |
657 | buf.append((char) ch); |
658 | } |
659 | return buf.toString(); |
660 | } |
661 | |
662 | public static byte[] loadBinaryPage(URLConnection con, URL url) throws IOException { |
663 | setHeaders(con); |
664 | ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
665 | InputStream inputStream = con.getInputStream(); |
666 | while (true) { |
667 | int ch = inputStream.read(); |
668 | if (ch < 0) |
669 | break; |
670 | buf.write(ch); |
671 | } |
672 | inputStream.close(); |
673 | return buf.toByteArray(); |
674 | } |
675 | |
676 | private static void setHeaders(URLConnection con) throws IOException { |
677 | String computerID = getComputerID(); |
678 | if (computerID != null) |
679 | con.setRequestProperty("X-ComputerID", computerID); |
680 | } |
681 | |
682 | public static String guessCharset(String contentType) { |
683 | Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*"); |
684 | Matcher m = p.matcher(contentType); |
685 | /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ |
686 | return m.matches() ? m.group(1) : "ISO-8859-1"; |
687 | } |
688 | |
689 | /** runs a transpiled set of sources */ |
690 | public static void javax2(File srcDir, File ioBaseDir, boolean silent, boolean runInProcess, |
691 | List<File> libraries, String[] args) throws Exception { |
692 | File classesDir = TempDirMaker_make(); |
693 | String javacOutput = compileJava(srcDir, libraries, classesDir); |
694 | |
695 | // run |
696 | |
697 | if (verbose) System.out.println("Running program (" + srcDir.getAbsolutePath() |
698 | + ") on io dir " + ioBaseDir.getAbsolutePath() + (runInProcess ? "[in-process]" : "") + "\n"); |
699 | runProgram(javacOutput, classesDir, ioBaseDir, silent, runInProcess, libraries, args); |
700 | } |
701 | |
702 | static String compileJava(File srcDir, List<File> libraries, File classesDir) throws IOException { |
703 | // collect sources |
704 | |
705 | List<File> sources = new ArrayList<File>(); |
706 | if (verbose) System.out.println("Scanning for sources in " + srcDir.getPath()); |
707 | scanForSources(srcDir, sources, true); |
708 | if (sources.isEmpty()) |
709 | throw new IOException("No sources found"); |
710 | |
711 | // compile |
712 | |
713 | File optionsFile = File.createTempFile("javax", ""); |
714 | if (verbose) System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath()); |
715 | String options = "-d " + bashQuote(classesDir.getPath()); |
716 | writeOptions(sources, libraries, optionsFile, options); |
717 | classesDir.mkdirs(); |
718 | return invokeJavac(optionsFile); |
719 | } |
720 | |
721 | private static void runProgram(String javacOutput, File classesDir, File ioBaseDir, |
722 | boolean silent, boolean runInProcess, |
723 | List<File> libraries, String[] args) throws Exception { |
724 | // print javac output if compile failed and it hasn't been printed yet |
725 | boolean didNotCompile = !didCompile(classesDir); |
726 | if (verbose || didNotCompile) |
727 | System.out.println(javacOutput); |
728 | if (didNotCompile) |
729 | return; |
730 | |
731 | if (runInProcess |
732 | || (ioBaseDir.getAbsolutePath().equals(new File(".").getAbsolutePath()) && !silent)) { |
733 | runProgramQuick(classesDir, libraries, args); |
734 | return; |
735 | } |
736 | |
737 | boolean echoOK = false; |
738 | // TODO: add libraries to class path |
739 | String bashCmd = "(cd " + bashQuote(ioBaseDir.getAbsolutePath()) + " && (java -cp " |
740 | + bashQuote(classesDir.getAbsolutePath()) + " main" + (echoOK ? "; echo ok" : "") + "))"; |
741 | if (verbose) System.out.println(bashCmd); |
742 | String output = backtick(bashCmd); |
743 | if (verbose || !silent) |
744 | System.out.println(output); |
745 | } |
746 | |
747 | static boolean didCompile(File classesDir) { |
748 | return hasFile(classesDir, "main.class"); |
749 | } |
750 | |
751 | private static void runProgramQuick(File classesDir, List<File> libraries, |
752 | String[] args) throws Exception { |
753 | // collect urls |
754 | URL[] urls = new URL[libraries.size()+1]; |
755 | urls[0] = classesDir.toURI().toURL(); |
756 | for (int i = 0; i < libraries.size(); i++) |
757 | urls[i+1] = libraries.get(i).toURI().toURL(); |
758 | |
759 | // make class loader |
760 | URLClassLoader classLoader = new URLClassLoader(urls); |
761 | |
762 | // load JavaX main class |
763 | Class<?> mainClass = classLoader.loadClass("main"); |
764 | |
765 | // run main method |
766 | Method main = mainClass.getMethod("main", String[].class); |
767 | main.invoke(null, (Object) args); |
768 | } |
769 | |
770 | private static String invokeJavac(File optionsFile) throws IOException { |
771 | String output; |
772 | try { |
773 | output = invokeEcj(optionsFile); |
774 | } catch (NoClassDefFoundError e) { |
775 | if (verbose) { |
776 | System.err.println("ecj not found - using javac"); |
777 | e.printStackTrace(); |
778 | } |
779 | output = backtick("javac " + bashQuote("@" + optionsFile.getPath())); |
780 | } |
781 | if (verbose) System.out.println(output); |
782 | return output; |
783 | } |
784 | |
785 | // throws ClassNotFoundError if ecj is not in classpath |
786 | static String invokeEcj(File optionsFile) { |
787 | StringWriter writer = new StringWriter(); |
788 | PrintWriter printWriter = new PrintWriter(writer); |
789 | org.eclipse.jdt.core.compiler.CompilationProgress progress = null; |
790 | |
791 | // add more eclipse options in the line below |
792 | |
793 | org.eclipse.jdt.core.compiler.batch.BatchCompiler.compile( |
794 | new String[] { "@" + optionsFile.getPath(), |
795 | "-source", "1.7", |
796 | "-warn:-constructorName", |
797 | "-warn:-unusedImport" |
798 | }, |
799 | printWriter, |
800 | printWriter, |
801 | progress); |
802 | return writer.toString(); |
803 | } |
804 | |
805 | private static void writeOptions(List<File> sources, List<File> libraries, |
806 | File optionsFile, String moreOptions) throws IOException { |
807 | FileWriter writer = new FileWriter(optionsFile); |
808 | for (File source : sources) |
809 | writer.write(bashQuote(source.getPath()) + " "); |
810 | if (!libraries.isEmpty()) { |
811 | List<String> cp = new ArrayList<String>(); |
812 | for (File lib : libraries) |
813 | cp.add(lib.getAbsolutePath()); |
814 | writer.write("-cp " + bashQuote(join(File.pathSeparator, cp)) + " "); |
815 | } |
816 | writer.write(moreOptions); |
817 | writer.close(); |
818 | } |
819 | |
820 | static void scanForSources(File source, List<File> sources, boolean topLevel) { |
821 | if (source.isFile() && source.getName().endsWith(".java")) |
822 | sources.add(source); |
823 | else if (source.isDirectory() && !isSkippedDirectoryName(source.getName(), topLevel)) { |
824 | File[] files = source.listFiles(); |
825 | for (File file : files) |
826 | scanForSources(file, sources, false); |
827 | } |
828 | } |
829 | |
830 | private static boolean isSkippedDirectoryName(String name, boolean topLevel) { |
831 | 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.) |
832 | return name.equalsIgnoreCase("input") || name.equalsIgnoreCase("output"); |
833 | } |
834 | |
835 | public static String backtick(String cmd) throws IOException { |
836 | ++processesStarted; |
837 | File outFile = File.createTempFile("_backtick", ""); |
838 | File scriptFile = File.createTempFile("_backtick", isWindows() ? ".bat" : ""); |
839 | |
840 | String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1"; |
841 | //Log.info("[Backtick] " + command); |
842 | try { |
843 | saveTextFile(scriptFile.getPath(), command); |
844 | String[] command2; |
845 | if (isWindows()) |
846 | command2 = new String[] { scriptFile.getPath() }; |
847 | else |
848 | command2 = new String[] { "/bin/bash", scriptFile.getPath() }; |
849 | Process process = Runtime.getRuntime().exec(command2); |
850 | try { |
851 | process.waitFor(); |
852 | } catch (InterruptedException e) { |
853 | throw new RuntimeException(e); |
854 | } |
855 | process.exitValue(); |
856 | return loadTextFile(outFile.getPath(), ""); |
857 | } finally { |
858 | scriptFile.delete(); |
859 | } |
860 | } |
861 | |
862 | /** possibly improvable */ |
863 | public static String javaQuote(String text) { |
864 | return bashQuote(text); |
865 | } |
866 | |
867 | /** possibly improvable */ |
868 | public static String bashQuote(String text) { |
869 | if (text == null) return null; |
870 | return "\"" + text |
871 | .replace("\\", "\\\\") |
872 | .replace("\"", "\\\"") |
873 | .replace("\n", "\\n") |
874 | .replace("\r", "\\r") + "\""; |
875 | } |
876 | |
877 | public final static String charsetForTextFiles = "UTF8"; |
878 | |
879 | static long TempDirMaker_lastValue; |
880 | |
881 | public static File TempDirMaker_make() { |
882 | File dir = new File(System.getProperty("user.home"), ".javax/" + TempDirMaker_newValue()); |
883 | dir.mkdirs(); |
884 | return dir; |
885 | } |
886 | |
887 | private static long TempDirMaker_newValue() { |
888 | long value; |
889 | do |
890 | value = System.currentTimeMillis(); |
891 | while (value == TempDirMaker_lastValue); |
892 | TempDirMaker_lastValue = value; |
893 | return value; |
894 | } |
895 | |
896 | public static String join(String glue, Iterable<String> strings) { |
897 | StringBuilder buf = new StringBuilder(); |
898 | Iterator<String> i = strings.iterator(); |
899 | if (i.hasNext()) { |
900 | buf.append(i.next()); |
901 | while (i.hasNext()) |
902 | buf.append(glue).append(i.next()); |
903 | } |
904 | return buf.toString(); |
905 | } |
906 | |
907 | public static boolean isWindows() { |
908 | return System.getProperty("os.name").contains("Windows"); |
909 | } |
910 | |
911 | public static String makeRandomID(int length) { |
912 | Random random = new Random(); |
913 | char[] id = new char[length]; |
914 | for (int i = 0; i< id.length; i++) |
915 | id[i] = (char) ((int) 'a' + random.nextInt(26)); |
916 | return new String(id); |
917 | } |
918 | |
919 | static String computerID; |
920 | public static String getComputerID() throws IOException { |
921 | if (noID) return null; |
922 | if (computerID == null) { |
923 | File file = new File(System.getProperty("user.home"), ".tinybrain/computer-id"); |
924 | computerID = loadTextFile(file.getPath(), null); |
925 | if (computerID == null) { |
926 | computerID = makeRandomID(12); |
927 | saveTextFile(file.getPath(), computerID); |
928 | } |
929 | if (verbose) |
930 | System.out.println("Local computer ID: " + computerID); |
931 | } |
932 | return computerID; |
933 | } |
934 | } |
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
ID | Author/Program | Comment | Date | |
---|---|---|---|---|
451 | #1000610 | Edit suggestion: !636 !629 main { static Object androidContext; static String programID; public static void main(String[] args) throws Exception { import java.io.*; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import java.net.URLConnection; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /** JavaX runner version 14. Changes to v13: -transmit command-line arguments to JavaX program (in "args" variable) Example: x14 639 2000380 (run #639 with input #2000380) [-disabled: transmit arguments from invocation line to translators] -make computer id for anonymous stats -"-noid" option for disabling computer id */ public class x14 { static final String version = "JavaX 14"; static boolean verbose = false, translate = false, list = false, virtualizeTranslators = true; static String translateTo = null; static boolean preferCached = false, safeOnly = false, noID = false; static List<String[]> mainTranslators = new ArrayList<String[]>(); private static Map<Long, String> memSnippetCache = new HashMap<Long, String>(); private static int processesStarted; public static void main(String[] args) throws Exception { File ioBaseDir = new File("."), inputDir = null, outputDir = null; String src = null; List<String> programArgs = new ArrayList<String>(); for (int i = 0; i < args.length; i++) { String arg = args[i]; if (arg.equals("-v") || arg.equals("-verbose")) verbose = true; else if (arg.equals("-finderror")) verbose = true; else if (arg.equals("-offline") || arg.equalsIgnoreCase("-prefercached")) preferCached = true; else if (arg.equals("-novirt")) virtualizeTranslators = false; else if (arg.equals("-safeonly")) safeOnly = true; else if (arg.equals("-noid")) noID = true; else if (arg.equals("translate")) translate = true; else if (arg.equals("list")) list = true; else if (arg.equals("run")) { // it's the default command anyway } else if (arg.startsWith("input=")) inputDir = new File(arg.substring(6)); else if (arg.startsWith("output=")) outputDir = new File(arg.substring(7)); else if (arg.equals("with")) mainTranslators.add(new String[] {args[++i], null}); else if (translate && arg.equals("to")) translateTo = args[++i]; else if (src == null) { //System.out.println("src=" + arg); src = arg; } else programArgs.add(arg); } if (src == null) src = "."; if (virtualizeTranslators && !preferCached) initDiskSnippetCache(TempDirMaker_make()); if (inputDir != null) { ioBaseDir = TempDirMaker_make(); System.out.println("Taking input from: " + inputDir.getAbsolutePath()); System.out.println("Output is in: " + new File(ioBaseDir, "output").getAbsolutePath()); copyInput(inputDir, new File(ioBaseDir, "input")); } javax4(src, ioBaseDir, translate, list, programArgs.toArray(new String[programArgs.size()])); if (outputDir != null) { copyInput(new File(ioBaseDir, "output"), outputDir); System.out.println("Output copied to: " + outputDir.getAbsolutePath()); } if (verbose) System.out.println("Processes started: " + processesStarted); } public static void javax4(String src, File ioDir, boolean translate, boolean list, String[] args) throws Exception { File srcDir; if (isSnippetID(src)) srcDir = loadSnippetAsMainJava(src); else { srcDir = new File(src); if (!new File(srcDir, "main.java").exists()) { System.out.println("This is " + version + ".\n" + "No main.java found, exiting"); return; } } // translate List<File> libraries = new ArrayList<File>(); File X = topLevelTranslate(srcDir, libraries); // list or run if (translate) { File to = X; if (translateTo != null) if (new File(translateTo).isDirectory()) to = new File(translateTo, "main.java"); else to = new File(translateTo); if (to != X) copy(new File(X, "main.java"), to); System.out.println("Program translated to: " + to.getAbsolutePath()); } else if (list) System.out.println(loadTextFile(new File(X, "main.java").getPath(), null)); else javax2(X, ioDir, false, false, libraries, args); } static File topLevelTranslate(File srcDir, List<File> libraries_out) throws Exception { File X = srcDir; X = applyTranslators(X, mainTranslators, libraries_out); X = defaultTranslate(X, libraries_out); return X; } private static File defaultTranslate(File x, List<File> libraries_out) throws Exception { x = luaPrintToJavaPrint(x); x = repeatAutoTranslate(x, libraries_out); return x; } private static File repeatAutoTranslate(File x, List<File> libraries_out) throws Exception { while (true) { File y = autoTranslate(x, libraries_out); if (y == x) return x; x = y; } } private static File autoTranslate(File x, List<File> libraries_out) throws Exception { String main = loadTextFile(new File(x, "main.java").getPath(), null); List<String> lines = toLines(main); List<String[]> translators = findTranslators(lines); if (translators.isEmpty()) return x; main = fromLines(lines); File newDir = TempDirMaker_make(); saveTextFile(new File(newDir, "main.java").getPath(), main); return applyTranslators(newDir, translators, libraries_out); } private static List<String[]> findTranslators(List<String> lines) { List<String[]> translators = new ArrayList<String[]>(); Pattern pattern = Pattern.compile("^!([0-9# \t]+)"); Pattern pArgs = Pattern.compile("^\\s*\\((.*)\\)"); for (ListIterator<String> iterator = lines.listIterator(); iterator.hasNext(); ) { String line = iterator.next(); line = line.trim(); Matcher matcher = pattern.matcher(line); if (matcher.find()) { String[] t = matcher.group(1).split("[ \t]+"); String rest = line.substring(matcher.end()); String arg = null; if (t.length == 1) { Matcher mArgs = pArgs.matcher(rest); if (mArgs.find()) arg = mArgs.group(1); } for (String transi : t) translators.add(new String[]{transi, arg}); iterator.remove(); } } return translators; } public static List<String> toLines(String s) { List<String> lines = new ArrayList<String>(); int start = 0; while (true) { int i = toLines_nextLineBreak(s, start); if (i < 0) { if (s.length() > start) lines.add(s.substring(start)); break; } lines.add(s.substring(start, i)); if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') i += 2; else ++i; start = i; } return lines; } private static int toLines_nextLineBreak(String s, int start) { for (int i = start; i < s.length(); i++) { char c = s.charAt(i); if (c == '\r' || c == '\n') return i; } return -1; } public static String fromLines(List<String> lines) { StringBuilder buf = new StringBuilder(); for (String line : lines) { buf.append(line).append('\n'); } return buf.toString(); } private static File applyTranslators(File x, List<String[]> translators, List<File> libraries_out) throws Exception { for (String[] translator : translators) x = applyTranslator(x, translator[0], translator[1], libraries_out); return x; } // also takes a library private static File applyTranslator(File x, String translator, String arg, List<File> libraries_out) throws Exception { if (verbose) System.out.println("Using translator " + translator + " on sources in " + x.getPath()); File newDir = runTranslatorOnInput(translator, null, arg, x, !verbose, libraries_out); if (!new File(newDir, "main.java").exists()) { throw new Exception("Translator " + translator + " did not generate main.java"); // TODO: show translator output } if (verbose) System.out.println("Translated with " + translator + " from " + x.getPath() + " to " + newDir.getPath()); x = newDir; return x; } private static File luaPrintToJavaPrint(File x) throws IOException { File newDir = TempDirMaker_make(); String code = loadTextFile(new File(x, "main.java").getPath(), null); code = luaPrintToJavaPrint(code); if (verbose) System.out.println(code); saveTextFile(new File(newDir, "main.java").getPath(), code); return newDir; } public static String luaPrintToJavaPrint(String code) { return ("\n" + code).replaceAll( "(\n\\s*)print (\".*\")", "$1System.out.println($2);").substring(1); } public static File loadSnippetAsMainJava(String snippetID) throws IOException { checkProgramSafety(snippetID); File srcDir = TempDirMaker_make(); saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippet(snippetID)); return srcDir; } public static File loadSnippetAsMainJavaVerified(String snippetID, String hash) throws IOException { checkProgramSafety(snippetID); File srcDir = TempDirMaker_make(); saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippetVerified(snippetID, hash)); return srcDir; } /** returns output dir */ private static File runTranslatorOnInput(String snippetID, String hash, String arg, File input, boolean silent, List<File> libraries_out) throws Exception { File libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID)); if (libraryFile != null) { loadLibrary(snippetID, libraries_out, libraryFile); return input; } File srcDir = hash == null ? loadSnippetAsMainJava(snippetID) : loadSnippetAsMainJavaVerified(snippetID, hash); long mainJavaSize = new File(srcDir, "main.java").length(); if (mainJavaSize == 0) { // no text in snippet? assume it's a library loadLibrary(snippetID, libraries_out, libraryFile); return input; } List<File> libraries = new ArrayList<File>(); srcDir = defaultTranslate(srcDir, libraries); boolean runInProcess = false; File ioBaseDir = TempDirMaker_make(); if (virtualizeTranslators) { if (verbose) System.out.println("Virtualizing translator"); //srcDir = applyTranslator(srcDir, "#2000351"); // I/O-virtualize the translator // that doesn't work because it recurses infinitely... // So we do it right here: String s = loadTextFile(new File(srcDir, "main.java").getPath(), null); s = s.replaceAll("new\\s+File\\(", "virtual.newFile("); s = s.replaceAll("new\\s+FileInputStream\\(", "virtual.newFileInputStream("); s = s.replaceAll("new\\s+FileOutputStream\\(", "virtual.newFileOutputStream("); s += "\n\n" + loadSnippet("#2000355"); // load class virtual // change baseDir s = s.replace("virtual_baseDir = \"\";", "virtual_baseDir = " + javaQuote(ioBaseDir.getAbsolutePath()) + ";"); // forward snippet cache s = s.replace("static File DiskSnippetCache_dir;", "static File DiskSnippetCache_dir = new File(" + javaQuote(DiskSnippetCache_dir.getAbsolutePath()) + ");"); s = s.replace("static boolean preferCached = false;", "static boolean preferCached = true;"); if (verbose) { System.out.println("==BEGIN VIRTUALIZED TRANSLATOR=="); System.out.println(s); System.out.println("==END VIRTUALIZED TRANSLATOR=="); } saveTextFile(new File(srcDir, "main.java").getPath(), s); // TODO: silence translator also runInProcess = true; } return runJavaX(ioBaseDir, srcDir, input, silent, runInProcess, libraries, arg != null ? new String[] {arg} : new String[0]); } static void checkProgramSafety(String snippetID) throws IOException { if (!safeOnly) return; URL url = new URL("http://tinybrain.de:8080/tb-int/is-javax-safe.php?id=" + parseSnippetID(snippetID)); String text = loadPage(url); if (!text.startsWith("{\"safe\":\"1\"}")) throw new RuntimeException("Translator not safe: #" + parseSnippetID(snippetID)); } private static void loadLibrary(String snippetID, List<File> libraries_out, File libraryFile) throws IOException { if (verbose) System.out.println("Assuming " + snippetID + " is a library."); if (libraryFile == null) { byte[] data; try { URL url = new URL("http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + parseSnippetID(snippetID) + "&contentType=application/binary"); System.err.println("Loading library: " + url); data = loadBinaryPage(url.openConnection(), url); if (verbose) System.err.println("Bytes loaded: " + data.length); } catch (FileNotFoundException e) { throw new IOException("Binary snippet #" + snippetID + " not found or not public"); } DiskSnippetCache_putLibrary(parseSnippetID(snippetID), data); libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID)); } if (!libraries_out.contains(libraryFile)) libraries_out.add(libraryFile); } /** returns output dir */ private static File runJavaX(File ioBaseDir, File originalSrcDir, File originalInput, boolean silent, boolean runInProcess, List<File> libraries, String[] args) throws Exception { File srcDir = new File(ioBaseDir, "src"); File inputDir = new File(ioBaseDir, "input"); File outputDir = new File(ioBaseDir, "output"); copyInput(originalSrcDir, srcDir); copyInput(originalInput, inputDir); javax2(srcDir, ioBaseDir, silent, runInProcess, libraries, args); return outputDir; } private static void copyInput(File src, File dst) throws IOException { copyDirectory(src, dst); } public static boolean hasFile(File inputDir, String name) { return new File(inputDir, name).exists(); } public static void copyDirectory(File src, File dst) throws IOException { if (verbose) System.out.println("Copying " + src.getAbsolutePath() + " to " + dst.getAbsolutePath()); dst.mkdirs(); File[] files = src.listFiles(); if (files == null) return; for (File file : files) { File dst1 = new File(dst, file.getName()); if (file.isDirectory()) copyDirectory(file, dst1); else { if (verbose) System.out.println("Copying " + file.getAbsolutePath() + " to " + dst1.getAbsolutePath()); copy(file, dst1); } } } /** Quickly copy a file without a progress bar or any other fancy GUI... :) */ public static void copy(File src, File dest) throws IOException { FileInputStream inputStream = new FileInputStream(src); FileOutputStream outputStream = new FileOutputStream(dest); try { copy(inputStream, outputStream); inputStream.close(); } finally { outputStream.close(); } } public static void copy(InputStream in, OutputStream out) throws IOException { byte[] buf = new byte[65536]; while (true) { int n = in.read(buf); if (n <= 0) return; out.write(buf, 0, n); } } /** writes safely (to temp file, then rename) */ public static void saveTextFile(String fileName, String contents) throws IOException { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); } /** writes safely (to temp file, then rename) */ public static void saveBinaryFile(String fileName, byte[] contents) throws IOException { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); fileOutputStream.write(contents); fileOutputStream.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); } public static String loadTextFile(String fileName, String defaultContents) throws IOException { if (!new File(fileName).exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(fileName); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles); return loadTextFile(inputStreamReader); } public static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { BufferedReader bufferedReader = new BufferedReader(reader); String line; while ((line = bufferedReader.readLine()) != null) builder.append(line).append('\n'); } finally { reader.close(); } return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); } static File DiskSnippetCache_dir; public static void initDiskSnippetCache(File dir) { DiskSnippetCache_dir = dir; dir.mkdirs(); } // Data files are immutable, use centralized cache public static synchronized File DiskSnippetCache_getLibrary(long snippetID) throws IOException { File file = new File(getGlobalCache(), "data_" + snippetID + ".jar"); if (verbose) System.out.println("Checking data cache: " + file.getPath()); return file.exists() ? file : null; } public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); } private static File DiskSnippetCache_getFile(long snippetID) { return new File(DiskSnippetCache_dir, "" + snippetID); } public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); } public static synchronized void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException { saveBinaryFile(new File(getGlobalCache(), "data_" + snippetID).getPath() + ".jar", data); } public static File DiskSnippetCache_getDir() { return DiskSnippetCache_dir; } public static void initSnippetCache() { if (DiskSnippetCache_dir == null) initDiskSnippetCache(getGlobalCache()); } private static File getGlobalCache() { File file = new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"); file.mkdirs(); return file; } public static String loadSnippetVerified(String snippetID, String hash) throws IOException { String text = loadSnippet(snippetID); String realHash = getHash(text.getBytes("UTF-8")); if (!realHash.equals(hash)) { String msg; if (hash.isEmpty()) msg = "Here's your hash for " + snippetID + ", please put in your program: " + realHash; else msg = "Hash mismatch for " + snippetID + ": " + realHash + " (new) vs " + hash + " - has tinybrain.de been hacked??"; throw new RuntimeException(msg); } return text; } public static String getHash(byte[] data) { return bytesToHex(getFullFingerprint(data)); } public static byte[] getFullFingerprint(byte[] data) { try { return MessageDigest.getInstance("MD5").digest(data); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } public static String bytesToHex(byte[] bytes) { return bytesToHex(bytes, 0, bytes.length); } public static String bytesToHex(byte[] bytes, int ofs, int len) { StringBuilder stringBuilder = new StringBuilder(len*2); for (int i = 0; i < len; i++) { String s = "0" + Integer.toHexString(bytes[ofs+i]); stringBuilder.append(s.substring(s.length()-2, s.length())); } return stringBuilder.toString(); } public static String loadSnippet(String snippetID) throws IOException { return loadSnippet(parseSnippetID(snippetID)); } public static long parseSnippetID(String snippetID) { return Long.parseLong(shortenSnippetID(snippetID)); } private static String shortenSnippetID(String snippetID) { if (snippetID.startsWith("#")) snippetID = snippetID.substring(1); String httpBlaBla = "http://tinybrain.de/"; if (snippetID.startsWith(httpBlaBla)) snippetID = snippetID.substring(httpBlaBla.length()); return snippetID; } public static boolean isSnippetID(String snippetID) { snippetID = shortenSnippetID(snippetID); return isInteger(snippetID) && Long.parseLong(snippetID) != 0; } public static boolean isInteger(String s) { return Pattern.matches("\\-?\\d+", s); } public static String loadSnippet(long snippetID) throws IOException { String text = memSnippetCache.get(snippetID); if (text != null) return text; if (preferCached) { initSnippetCache(); text = DiskSnippetCache_get(snippetID); if (text != null) return text; } try { URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID); text = loadPage(url); } catch (FileNotFoundException e) { throw new IOException("Snippet #" + snippetID + " not found or not public"); } memSnippetCache.put(snippetID, text); try { initSnippetCache(); DiskSnippetCache_put(snippetID, text); } catch (IOException e) { System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); } return text; } private static String loadPage(URL url) throws IOException { System.err.println("Loading: " + url.toExternalForm()); URLConnection con = url.openConnection(); return loadPage(con, url); } public static String loadPage(URLConnection con, URL url) throws IOException { setHeaders(con); String contentType = con.getContentType(); if (contentType == null) throw new IOException("Page could not be read: " + url); //Log.info("Content-Type: " + contentType); String charset = guessCharset(contentType); Reader r = new InputStreamReader(con.getInputStream(), charset); StringBuilder buf = new StringBuilder(); while (true) { int ch = r.read(); if (ch < 0) break; //Log.info("Chars read: " + buf.length()); buf.append((char) ch); } return buf.toString(); } public static byte[] loadBinaryPage(URLConnection con, URL url) throws IOException { setHeaders(con); ByteArrayOutputStream buf = new ByteArrayOutputStream(); InputStream inputStream = con.getInputStream(); while (true) { int ch = inputStream.read(); if (ch < 0) break; buf.write(ch); } inputStream.close(); return buf.toByteArray(); } private static void setHeaders(URLConnection con) throws IOException { String computerID = getComputerID(); if (computerID != null) con.setRequestProperty("X-ComputerID", computerID); } public static String guessCharset(String contentType) { Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*"); Matcher m = p.matcher(contentType); /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ return m.matches() ? m.group(1) : "ISO-8859-1"; } /** runs a transpiled set of sources */ public static void javax2(File srcDir, File ioBaseDir, boolean silent, boolean runInProcess, List<File> libraries, String[] args) throws Exception { File classesDir = TempDirMaker_make(); String javacOutput = compileJava(srcDir, libraries, classesDir); // run if (verbose) System.out.println("Running program (" + srcDir.getAbsolutePath() + ") on io dir " + ioBaseDir.getAbsolutePath() + (runInProcess ? "[in-process]" : "") + "\n"); runProgram(javacOutput, classesDir, ioBaseDir, silent, runInProcess, libraries, args); } static String compileJava(File srcDir, List<File> libraries, File classesDir) throws IOException { // collect sources List<File> sources = new ArrayList<File>(); if (verbose) System.out.println("Scanning for sources in " + srcDir.getPath()); scanForSources(srcDir, sources, true); if (sources.isEmpty()) throw new IOException("No sources found"); // compile File optionsFile = File.createTempFile("javax", ""); if (verbose) System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath()); String options = "-d " + bashQuote(classesDir.getPath()); writeOptions(sources, libraries, optionsFile, options); classesDir.mkdirs(); return invokeJavac(optionsFile); } private static void runProgram(String javacOutput, File classesDir, File ioBaseDir, boolean silent, boolean runInProcess, List<File> libraries, String[] args) throws Exception { // print javac output if compile failed and it hasn't been printed yet boolean didNotCompile = !didCompile(classesDir); if (verbose || didNotCompile) System.out.println(javacOutput); if (didNotCompile) return; if (runInProcess || (ioBaseDir.getAbsolutePath().equals(new File(".").getAbsolutePath()) && !silent)) { runProgramQuick(classesDir, libraries, args); return; } boolean echoOK = false; // TODO: add libraries to class path String bashCmd = "(cd " + bashQuote(ioBaseDir.getAbsolutePath()) + " && (java -cp " + bashQuote(classesDir.getAbsolutePath()) + " main" + (echoOK ? "; echo ok" : "") + "))"; if (verbose) System.out.println(bashCmd); String output = backtick(bashCmd); if (verbose || !silent) System.out.println(output); } static boolean didCompile(File classesDir) { return hasFile(classesDir, "main.class"); } private static void runProgramQuick(File classesDir, List<File> libraries, String[] args) throws Exception { // collect urls URL[] urls = new URL[libraries.size()+1]; urls[0] = classesDir.toURI().toURL(); for (int i = 0; i < libraries.size(); i++) urls[i+1] = libraries.get(i).toURI().toURL(); // make class loader URLClassLoader classLoader = new URLClassLoader(urls); // load JavaX main class Class<?> mainClass = classLoader.loadClass("main"); // run main method Method main = mainClass.getMethod("main", String[].class); main.invoke(null, (Object) args); } private static String invokeJavac(File optionsFile) throws IOException { String output; try { output = invokeEcj(optionsFile); } catch (NoClassDefFoundError e) { if (verbose) { System.err.println("ecj not found - using javac"); e.printStackTrace(); } output = backtick("javac " + bashQuote("@" + optionsFile.getPath())); } if (verbose) System.out.println(output); return output; } // throws ClassNotFoundError if ecj is not in classpath static String invokeEcj(File optionsFile) { StringWriter writer = new StringWriter(); PrintWriter printWriter = new PrintWriter(writer); org.eclipse.jdt.core.compiler.CompilationProgress progress = null; // add more eclipse options in the line below org.eclipse.jdt.core.compiler.batch.BatchCompiler.compile( new String[] { "@" + optionsFile.getPath(), "-source", "1.7", "-warn:-constructorName", "-warn:-unusedImport" }, printWriter, printWriter, progress); return writer.toString(); } private static void writeOptions(List<File> sources, List<File> libraries, File optionsFile, String moreOptions) throws IOException { FileWriter writer = new FileWriter(optionsFile); for (File source : sources) writer.write(bashQuote(source.getPath()) + " "); if (!libraries.isEmpty()) { List<String> cp = new ArrayList<String>(); for (File lib : libraries) cp.add(lib.getAbsolutePath()); writer.write("-cp " + bashQuote(join(File.pathSeparator, cp)) + " "); } writer.write(moreOptions); writer.close(); } static void scanForSources(File source, List<File> sources, boolean topLevel) { if (source.isFile() && source.getName().endsWith(".java")) sources.add(source); else if (source.isDirectory() && !isSkippedDirectoryName(source.getName(), topLevel)) { File[] files = source.listFiles(); for (File file : files) scanForSources(file, sources, false); } } private static boolean isSkippedDirectoryName(String name, boolean topLevel) { 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.) return name.equalsIgnoreCase("input") || name.equalsIgnoreCase("output"); } public static String backtick(String cmd) throws IOException { ++processesStarted; File outFile = File.createTempFile("_backtick", ""); File scriptFile = File.createTempFile("_backtick", isWindows() ? ".bat" : ""); String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1"; //Log.info("[Backtick] " + command); try { saveTextFile(scriptFile.getPath(), command); String[] command2; if (isWindows()) command2 = new String[] { scriptFile.getPath() }; else command2 = new String[] { "/bin/bash", scriptFile.getPath() }; Process process = Runtime.getRuntime().exec(command2); try { process.waitFor(); } catch (InterruptedException e) { throw new RuntimeException(e); } process.exitValue(); return loadTextFile(outFile.getPath(), ""); } finally { scriptFile.delete(); } } /** possibly improvable */ public static String javaQuote(String text) { return bashQuote(text); } /** possibly improvable */ public static String bashQuote(String text) { if (text == null) return null; return "\"" + text .replace("\\", "\\\\") .replace("\"", "\\\"") .replace("\n", "\\n") .replace("\r", "\\r") + "\""; } public final static String charsetForTextFiles = "UTF8"; static long TempDirMaker_lastValue; public static File TempDirMaker_make() { File dir = new File(System.getProperty("user.home"), ".javax/" + TempDirMaker_newValue()); dir.mkdirs(); return dir; } private static long TempDirMaker_newValue() { long value; do value = System.currentTimeMillis(); while (value == TempDirMaker_lastValue); TempDirMaker_lastValue = value; return value; } public static String join(String glue, Iterable<String> strings) { StringBuilder buf = new StringBuilder(); Iterator<String> i = strings.iterator(); if (i.hasNext()) { buf.append(i.next()); while (i.hasNext()) buf.append(glue).append(i.next()); } return buf.toString(); } public static boolean isWindows() { return System.getProperty("os.name").contains("Windows"); } public static String makeRandomID(int length) { Random random = new Random(); char[] id = new char[length]; for (int i = 0; i< id.length; i++) id[i] = (char) ((int) 'a' + random.nextInt(26)); return new String(id); } static String computerID; public static String getComputerID() throws IOException { if (noID) return null; if (computerID == null) { File file = new File(System.getProperty("user.home"), ".tinybrain/computer-id"); computerID = loadTextFile(file.getPath(), null); if (computerID == null) { computerID = makeRandomID(12); saveTextFile(file.getPath(), computerID); } if (verbose) System.out.println("Local computer ID: " + computerID); } return computerID; } } }} | 2015-08-18 15:42:33 | delete |
449 | #1000604 (pitcher) | 2015-08-18 00:07:22 |
Snippet ID: | #645 |
Snippet name: | x14.java |
Eternal ID of this version: | #645/1 |
Text MD5: | 5c3cfdd80f15dedc05d4e55dc20627f8 |
Author: | stefan |
Category: | javax |
Type: | Java source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-06-26 20:39:27 |
Source code size: | 33861 bytes / 934 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 1231 / 183 |
Referenced in: | [show references] |