Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

595
LINES

< > BotCompany Repo | #599 // x9.java

Java source code

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.ArrayList;
9  
import java.util.Arrays;
10  
import java.util.List;
11  
import java.util.ListIterator;
12  
import java.util.regex.Matcher;
13  
import java.util.regex.Pattern;
14  
15  
/**
16  
 Changes to v8:
17  
 -runProgramQuick (run inside current VM if io base dir is current dir)
18  
  [provides instant System.out]
19  
 -improvements to output
20  
 -auto translators (!123 syntax)
21  
22  
 Still Linux only. (Where are the Windows porters?)
23  
 */
24  
25  
public class x9 {
26  
  static boolean verbose = false, translate = false, list = false;
27  
28  
  static List<String> mainTranslators = new ArrayList<String>();
29  
30  
  public static void main(String[] args) throws Exception {
31  
    File ioBaseDir = new File("."), inputDir = null, outputDir = null;
32  
    String src = ".";
33  
    for (int i = 0; i < args.length; i++) {
34  
      String arg = args[i];
35  
      if (arg.equals("-v") || arg.equals("-verbose"))
36  
        verbose = true;
37  
      else if (arg.equals("-finderror"))
38  
        verbose = true;
39  
      else if (arg.equals("translate"))
40  
        translate = true;
41  
      else if (arg.equals("list"))
42  
        list = true;
43  
      else if (arg.startsWith("input="))
44  
        inputDir = new File(arg.substring(6));
45  
      else if (arg.startsWith("output="))
46  
        outputDir = new File(arg.substring(7));
47  
      else if (arg.equals("with"))
48  
        mainTranslators.add(args[++i]);
49  
      else
50  
        src = arg;
51  
    }
52  
53  
    if (inputDir != null) {
54  
      ioBaseDir = TempDirMaker_make();
55  
      System.out.println("Taking input from: " + inputDir.getAbsolutePath());
56  
      System.out.println("Output is in: " + new File(ioBaseDir, "output").getAbsolutePath());
57  
      copyInput(inputDir, new File(ioBaseDir, "input"));
58  
    }
59  
60  
    javax4(src, ioBaseDir, translate, list);
61  
62  
    if (outputDir != null) {
63  
      copyInput(new File(ioBaseDir, "output"), outputDir);
64  
      System.out.println("Output copied to: " + outputDir.getAbsolutePath());
65  
    }
66  
  }
67  
68  
  public static void javax4(String src, File ioDir, boolean translate, boolean list) throws Exception {
69  
    File srcDir;
70  
    if (isSnippetID(src))
71  
      srcDir = loadSnippetAsMainJava(src);
72  
    else
73  
      srcDir = new File(src);
74  
    File X = programToInput(srcDir);
75  
76  
    X = applyTranslators(X, mainTranslators);
77  
    X = defaultTranslate(X);
78  
79  
    if (translate)
80  
      System.out.println("Program translated to: " + X.getAbsolutePath());
81  
    else if (list)
82  
      System.out.println(loadTextFile(new File(X, "main.java").getPath(), null));
83  
    else
84  
      javax2(X, ioDir, false);
85  
  }
86  
87  
  private static File defaultTranslate(File x) throws Exception {
88  
    x = luaPrintToJavaPrint(x);
89  
    x = autoTranslate(x);
90  
    return x;
91  
  }
92  
93  
  private static File autoTranslate(File x) throws Exception {
94  
    String main = loadTextFile(new File(x, "main.java").getPath(), null);
95  
    List<String> lines = toLines(main);
96  
    List<String> translators = findTranslators(lines);
97  
    if (translators.isEmpty())
98  
      return x;
99  
100  
    main = fromLines(lines);
101  
    File newDir = TempDirMaker_make();
102  
    saveTextFile(new File(newDir, "main.java").getPath(), main);
103  
    return applyTranslators(newDir, translators);
104  
  }
105  
106  
  private static List<String> findTranslators(List<String> lines) {
107  
    List<String> translators = new ArrayList<String>();
108  
    Pattern pattern = Pattern.compile("^!([0-9# \t]+)");
109  
    for (ListIterator<String> iterator = lines.listIterator(); iterator.hasNext(); ) {
110  
      String line = iterator.next();
111  
      line = line.trim();
112  
      Matcher matcher = pattern.matcher(line);
113  
      if (matcher.find()) {
114  
        translators.addAll(Arrays.asList(matcher.group(1).split("[ \t]+")));
115  
        iterator.remove();
116  
      }
117  
    }
118  
    return translators;
119  
  }
120  
121  
  public static List<String> toLines(String s) {
122  
    List<String> lines = new ArrayList<String>();
123  
    int start = 0;
124  
    while (true) {
125  
      int i = toLines_nextLineBreak(s, start);
126  
      if (i < 0) {
127  
        if (s.length() > start) lines.add(s.substring(start));
128  
        break;
129  
      }
130  
131  
      lines.add(s.substring(start, i));
132  
      if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
133  
        i += 2;
134  
      else
135  
        ++i;
136  
137  
      start = i;
138  
    }
139  
    return lines;
140  
  }
141  
142  
  private static int toLines_nextLineBreak(String s, int start) {
143  
    for (int i = start; i < s.length(); i++) {
144  
      char c = s.charAt(i);
145  
      if (c == '\r' || c == '\n')
146  
        return i;
147  
    }
148  
    return -1;
149  
  }
150  
151  
  public static String fromLines(List<String> lines) {
152  
    StringBuilder buf = new StringBuilder();
153  
    for (String line : lines) {
154  
      buf.append(line).append('\n');
155  
    }
156  
    return buf.toString();
157  
  }
158  
159  
  private static File applyTranslators(File x, List<String> translators) throws Exception {
160  
    for (String translator : translators)
161  
      x = applyTranslator(x, translator);
162  
    return x;
163  
  }
164  
165  
  private static File applyTranslator(File x, String translator) throws Exception {
166  
    if (verbose)
167  
      System.out.println("Using translator " + translator + " on sources in " + x.getPath());
168  
    File newDir = runJavaX2_src_from_snippet(translator, null, x, !verbose);
169  
    if (!new File(newDir, "main.java").exists())
170  
      throw new Exception("Translator " + translator + " did not generate main.java");
171  
    if (verbose)
172  
      System.out.println("Translated with " + translator + " from " + x.getPath() + " to " + newDir.getPath());
173  
    x = newDir;
174  
    return x;
175  
  }
176  
177  
  private static File luaPrintToJavaPrint(File x) throws IOException {
178  
    File newDir = TempDirMaker_make();
179  
    String code = loadTextFile(new File(x, "main.java").getPath(), null);
180  
    code = luaPrintToJavaPrint(code);
181  
    if (verbose)
182  
      System.out.println(code);
183  
    saveTextFile(new File(newDir, "main.java").getPath(), code);
184  
    return newDir;
185  
  }
186  
187  
  public static String luaPrintToJavaPrint(String code) {
188  
    return ("\n" + code).replaceAll(
189  
      "(\n\\s*)print (\".*\")",
190  
      "$1System.out.println($2);").substring(1);
191  
  }
192  
193  
  public static File loadSnippetAsMainJava(String snippetID) throws IOException {
194  
    File srcDir = TempDirMaker_make();
195  
    saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippet(snippetID, false));
196  
    return srcDir;
197  
  }
198  
199  
  public static File loadSnippetAsMainJavaVerified(String snippetID, String hash) throws IOException {
200  
    File srcDir = TempDirMaker_make();
201  
    saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippetVerified(snippetID, hash));
202  
    return srcDir;
203  
  }
204  
205  
  /** returns output dir */
206  
  private static File runJavaX2_src_from_snippet(String snippetID, String hash, File input, boolean silent) throws Exception {
207  
    File srcDir = hash == null ? loadSnippetAsMainJava(snippetID)
208  
      : loadSnippetAsMainJavaVerified(snippetID, hash);
209  
    srcDir = defaultTranslate(srcDir);
210  
    return runJavaX(srcDir, input, silent);
211  
  }
212  
213  
  /** returns output dir */
214  
  private static File runJavaX(File originalSrcDir, File originalInput, boolean silent) throws Exception {
215  
    File ioBaseDir = TempDirMaker_make();
216  
    File srcDir = new File(ioBaseDir, "src");
217  
    File inputDir = new File(ioBaseDir, "input");
218  
    File outputDir = new File(ioBaseDir, "output");
219  
    copyInput(originalSrcDir, srcDir);
220  
    copyInput(originalInput, inputDir);
221  
    javax2(srcDir, ioBaseDir, silent);
222  
    return outputDir;
223  
  }
224  
225  
  private static void copyInput(File src, File dst) throws IOException {
226  
    copyDirectory(src, dst);
227  
  }
228  
229  
  private static File programToInput(File srcDir) {
230  
    return srcDir;
231  
  }
232  
233  
  public static boolean hasFile(File inputDir, String name) {
234  
    return new File(inputDir, name).exists();
235  
  }
236  
237  
  public static void copyDirectory(File src, File dst) throws IOException {
238  
    if (verbose) System.out.println("Copying " + src.getAbsolutePath() + " to " + dst.getAbsolutePath());
239  
    dst.mkdirs();
240  
    File[] files = src.listFiles();
241  
    if (files == null) return;
242  
    for (File file : files) {
243  
      File dst1 = new File(dst, file.getName());
244  
      if (file.isDirectory())
245  
        copyDirectory(file, dst1);
246  
      else {
247  
        if (verbose) System.out.println("Copying " + file.getAbsolutePath() + " to " + dst1.getAbsolutePath());
248  
        copy(file, dst1);
249  
      }
250  
    }
251  
  }
252  
253  
  /** Quickly copy a file without a progress bar or any other fancy GUI... :) */
254  
  public static void copy(File src, File dest) throws IOException {
255  
    FileInputStream inputStream = new FileInputStream(src);
256  
    FileOutputStream outputStream = new FileOutputStream(dest);
257  
    try {
258  
      copy(inputStream, outputStream);
259  
      inputStream.close();
260  
    } finally {
261  
      outputStream.close();
262  
    }
263  
  }
264  
265  
  public static void copy(InputStream in, OutputStream out) throws IOException {
266  
    byte[] buf = new byte[65536];
267  
    while (true) {
268  
      int n = in.read(buf);
269  
      if (n <= 0) return;
270  
      out.write(buf, 0, n);
271  
    }
272  
  }
273  
274  
  /** writes safely (to temp file, then rename) */
275  
  public static void saveTextFile(String fileName, String contents) throws IOException {
276  
    File file = new File(fileName);
277  
    File parentFile = file.getParentFile();
278  
    if (parentFile != null)
279  
      parentFile.mkdirs();
280  
    String tempFileName = fileName + "_temp";
281  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
282  
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles);
283  
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
284  
    printWriter.print(contents);
285  
    printWriter.close();
286  
    if (file.exists() && !file.delete())
287  
      throw new IOException("Can't delete " + fileName);
288  
289  
    if (!new File(tempFileName).renameTo(file))
290  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
291  
  }
292  
293  
  public static String loadTextFile(String fileName, String defaultContents) throws IOException {
294  
    if (!new File(fileName).exists())
295  
      return defaultContents;
296  
297  
    FileInputStream fileInputStream = new FileInputStream(fileName);
298  
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles);
299  
    return loadTextFile(inputStreamReader);
300  
  }
301  
302  
  public static String loadTextFile(Reader reader) throws IOException {
303  
    StringBuilder builder = new StringBuilder();
304  
    try {
305  
      BufferedReader bufferedReader = new BufferedReader(reader);
306  
      String line;
307  
      while ((line = bufferedReader.readLine()) != null)
308  
        builder.append(line).append('\n');
309  
    } finally {
310  
      reader.close();
311  
    }
312  
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
313  
  }
314  
315  
  static File DiskSnippetCache_dir;
316  
317  
  public static void initDiskSnippetCache(File dir) {
318  
    DiskSnippetCache_dir = dir;
319  
    dir.mkdirs();
320  
  }
321  
322  
  public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
323  
    return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
324  
  }
325  
326  
  private static File DiskSnippetCache_getFile(long snippetID) {
327  
    return new File(DiskSnippetCache_dir, "" + snippetID);
328  
  }
329  
330  
  public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
331  
    saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
332  
  }
333  
334  
  public static File DiskSnippetCache_getDir() {
335  
    return DiskSnippetCache_dir;
336  
  }
337  
338  
  public static void initSnippetCache() {
339  
    if (DiskSnippetCache_dir == null)
340  
      initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"));
341  
  }
342  
343  
  public static String loadSnippetVerified(String snippetID, String hash) throws IOException {
344  
    String text = loadSnippet(snippetID, !hash.isEmpty());
345  
    String realHash = getHash(text.getBytes("UTF-8"));
346  
    if (!realHash.equals(hash)) {
347  
      String msg;
348  
      if (hash.isEmpty())
349  
        msg = "Here's your hash for " + snippetID + ", please put in your program: " + realHash;
350  
      else
351  
        msg = "Hash mismatch for " + snippetID + ": " + realHash + " (new) vs " + hash + " - has tinybrain.de been hacked??";
352  
      throw new RuntimeException(msg);
353  
    }
354  
    return text;
355  
  }
356  
357  
  public static String getHash(byte[] data) {
358  
    return bytesToHex(getFullFingerprint(data));
359  
  }
360  
361  
  public static byte[] getFullFingerprint(byte[] data) {
362  
    try {
363  
      return MessageDigest.getInstance("MD5").digest(data);
364  
    } catch (NoSuchAlgorithmException e) {
365  
      throw new RuntimeException(e);
366  
    }
367  
  }
368  
369  
  public static String bytesToHex(byte[] bytes) {
370  
    return bytesToHex(bytes, 0, bytes.length);
371  
  }
372  
373  
  public static String bytesToHex(byte[] bytes, int ofs, int len) {
374  
    StringBuilder stringBuilder = new StringBuilder(len*2);
375  
    for (int i = 0; i < len; i++) {
376  
      String s = "0" + Integer.toHexString(bytes[ofs+i]);
377  
      stringBuilder.append(s.substring(s.length()-2, s.length()));
378  
    }
379  
    return stringBuilder.toString();
380  
  }
381  
382  
  public static String loadSnippet(String snippetID, boolean preferCached) throws IOException {
383  
    return loadSnippet(parseSnippetID(snippetID), preferCached);
384  
  }
385  
386  
  public static long parseSnippetID(String snippetID) {
387  
    return Long.parseLong(shortenSnippetID(snippetID));
388  
  }
389  
390  
  private static String shortenSnippetID(String snippetID) {
391  
    if (snippetID.startsWith("#"))
392  
      snippetID = snippetID.substring(1);
393  
    String httpBlaBla = "http://tinybrain.de/";
394  
    if (snippetID.startsWith(httpBlaBla))
395  
      snippetID = snippetID.substring(httpBlaBla.length());
396  
    return snippetID;
397  
  }
398  
399  
  public static boolean isSnippetID(String snippetID) {
400  
    snippetID = shortenSnippetID(snippetID);
401  
    return isInteger(snippetID) && Long.parseLong(snippetID) != 0;
402  
  }
403  
404  
  public static boolean isInteger(String s) {
405  
    return Pattern.matches("\\-?\\d+", s);
406  
  }
407  
408  
  public static String loadSnippet(long snippetID, boolean preferCached) throws IOException {
409  
    if (preferCached) {
410  
      initSnippetCache();
411  
      String text = DiskSnippetCache_get(snippetID);
412  
      if (text != null)
413  
        return text;
414  
    }
415  
416  
    String text;
417  
    try {
418  
      URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID);
419  
      text = loadPage(url);
420  
    } catch (FileNotFoundException e) {
421  
      throw new IOException("Snippet #" + snippetID + " not found or not public");
422  
    }
423  
424  
    try {
425  
      initSnippetCache();
426  
      DiskSnippetCache_put(snippetID, text);
427  
    } catch (IOException e) {
428  
      System.err.println("Minor warning: Couldn't save snippet to cache ("  + DiskSnippetCache_getDir() + ")");
429  
    }
430  
431  
    return text;
432  
  }
433  
434  
  private static String loadPage(URL url) throws IOException {
435  
    System.out.println("Loading: " + url.toExternalForm());
436  
    URLConnection con = url.openConnection();
437  
    return loadPage(con, url);
438  
  }
439  
440  
  public static String loadPage(URLConnection con, URL url) throws IOException {
441  
    String contentType = con.getContentType();
442  
    if (contentType == null)
443  
      throw new IOException("Page could not be read: " + url);
444  
    //Log.info("Content-Type: " + contentType);
445  
    String charset = guessCharset(contentType);
446  
    Reader r = new InputStreamReader(con.getInputStream(), charset);
447  
    StringBuilder buf = new StringBuilder();
448  
    while (true) {
449  
      int ch = r.read();
450  
      if (ch < 0)
451  
        break;
452  
      //Log.info("Chars read: " + buf.length());
453  
      buf.append((char) ch);
454  
    }
455  
    return buf.toString();
456  
  }
457  
458  
  public static String guessCharset(String contentType) {
459  
    Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
460  
    Matcher m = p.matcher(contentType);
461  
    /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */
462  
    return m.matches() ? m.group(1) : "ISO-8859-1";
463  
  }
464  
465  
  public static void javax2(File srcDir, File ioBaseDir, boolean silent) throws Exception {
466  
    List<File> sources = new ArrayList<File>();
467  
    if (verbose) System.out.println("Scanning for sources in " + srcDir.getPath());
468  
    scanForSources(srcDir, sources, true);
469  
    if (sources.isEmpty()) {
470  
      System.out.println("No sources found");
471  
      return;
472  
    }
473  
    File optionsFile = File.createTempFile("javax", "");
474  
    File classesDir = TempDirMaker_make();
475  
    if (verbose) System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath());
476  
    String options = "-d " + bashQuote(classesDir.getPath());
477  
    writeOptions(sources, optionsFile, options);
478  
    classesDir.mkdirs();
479  
    String javacOutput = invokeJavac(optionsFile);
480  
    if (verbose) System.out.println("Running program (" + srcDir.getAbsolutePath()
481  
      + ") on io dir " + ioBaseDir.getAbsolutePath() + "\n");
482  
    runProgram(javacOutput, classesDir, ioBaseDir, silent);
483  
  }
484  
485  
  private static void runProgram(String javacOutput, File classesDir, File ioBaseDir,
486  
                                 boolean silent) throws Exception {
487  
    // print javac output if compile failed and it hasn't been printed yet
488  
    boolean didNotCompile = !hasFile(classesDir, "main.class");
489  
    if (verbose || didNotCompile)
490  
      System.out.println(javacOutput);
491  
    if (didNotCompile)
492  
      return;
493  
494  
    if (ioBaseDir.getAbsolutePath().equals(new File(".").getAbsolutePath()) && !silent) {
495  
      runProgramQuick(classesDir);
496  
      return;
497  
    }
498  
499  
    boolean echoOK = false;
500  
    String bashCmd = "(cd " + bashQuote(ioBaseDir.getAbsolutePath()) + " && (java -cp "
501  
      + bashQuote(classesDir.getAbsolutePath()) + " main" + (echoOK ? "; echo ok" : "") + "))";
502  
    if (verbose) System.out.println(bashCmd);
503  
    String output = backtick(bashCmd);
504  
    if (verbose || !silent)
505  
      System.out.println(output);
506  
  }
507  
508  
  private static void runProgramQuick(File classesDir) throws Exception {
509  
    URLClassLoader classLoader = new URLClassLoader(new URL[]{classesDir.toURI().toURL()});
510  
    Class<?> mainClass = classLoader.loadClass("main");
511  
    Method main = mainClass.getMethod("main", String[].class);
512  
    main.invoke(null, (Object) new String[0]);
513  
  }
514  
515  
  private static String invokeJavac(File optionsFile) throws IOException {
516  
    String javacOutput = backtick("javac " + bashQuote("@" + optionsFile.getPath()));
517  
    if (verbose) System.out.println(javacOutput);
518  
    return javacOutput;
519  
  }
520  
521  
  private static void writeOptions(List<File> sources, File sourcesFile, String moreOptions) throws IOException {
522  
    FileWriter writer = new FileWriter(sourcesFile);
523  
    for (File source : sources)
524  
      writer.write(bashQuote(source.getPath()) + " ");
525  
    writer.write(moreOptions);
526  
    writer.close();
527  
  }
528  
529  
  private static void scanForSources(File source, List<File> sources, boolean topLevel) {
530  
    if (source.isFile() && source.getName().endsWith(".java"))
531  
      sources.add(source);
532  
    else if (source.isDirectory() && !isSkippedDirectoryName(source.getName(), topLevel)) {
533  
      File[] files = source.listFiles();
534  
      for (File file : files)
535  
        scanForSources(file, sources, false);
536  
    }
537  
  }
538  
539  
  private static boolean isSkippedDirectoryName(String name, boolean topLevel) {
540  
    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.)
541  
    return name.equalsIgnoreCase("input") || name.equalsIgnoreCase("output");
542  
  }
543  
544  
  public static String backtick(String cmd) throws IOException {
545  
    File outFile = File.createTempFile("_backtick", "");
546  
    File scriptFile = File.createTempFile("_backtick", "");
547  
548  
    String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1";
549  
    //Log.info("[Backtick] " + command);
550  
    try {
551  
      saveTextFile(scriptFile.getPath(), command);
552  
      String[] command2 = {"/bin/bash", scriptFile.getPath() };
553  
      Process process = Runtime.getRuntime().exec(command2);
554  
      try {
555  
        process.waitFor();
556  
      } catch (InterruptedException e) {
557  
        throw new RuntimeException(e);
558  
      }
559  
      int value = process.exitValue();
560  
      //Log.info("exit value: " + value);
561  
      return loadTextFile(outFile.getPath(), "");
562  
    } finally {
563  
      scriptFile.delete();
564  
    }
565  
  }
566  
567  
  /** possibly improvable */
568  
  public static String bashQuote(String text) {
569  
    if (text == null) return null;
570  
    return "\"" + text
571  
      .replace("\\", "\\\\")
572  
      .replace("\"", "\\\"")
573  
      .replace("\n", "\\n")
574  
      .replace("\r", "\\r") + "\"";
575  
  }
576  
577  
  public final static String charsetForTextFiles = "UTF8";
578  
579  
  static long TempDirMaker_lastValue;
580  
581  
  public static File TempDirMaker_make() {
582  
    File dir = new File(System.getProperty("user.home"), ".javax/" + TempDirMaker_newValue());
583  
    dir.mkdirs();
584  
    return dir;
585  
  }
586  
587  
  private static long TempDirMaker_newValue() {
588  
    long value;
589  
    do
590  
      value = System.currentTimeMillis();
591  
    while (value == TempDirMaker_lastValue);
592  
    TempDirMaker_lastValue = value;
593  
    return value;
594  
  }
595  
}

download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
737 #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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
Changes to v8:
-runProgramQuick (run inside current VM if io base dir is current dir)
[provides instant System.out]
-improvements to output
-auto translators (!123 syntax)

Still Linux only. (Where are the Windows porters?)
*/

public class x9 {
static boolean verbose = false, translate = false, list = false;

static List<String> mainTranslators = new ArrayList<String>();

public static void main(String[] args) throws Exception {
File ioBaseDir = new File("."), inputDir = null, outputDir = null;
String src = ".";
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("translate"))
translate = true;
else if (arg.equals("list"))
list = true;
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(args[++i]);
else
src = arg;
}

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);

if (outputDir != null) {
copyInput(new File(ioBaseDir, "output"), outputDir);
System.out.println("Output copied to: " + outputDir.getAbsolutePath());
}
}

public static void javax4(String src, File ioDir, boolean translate, boolean list) throws Exception {
File srcDir;
if (isSnippetID(src))
srcDir = loadSnippetAsMainJava(src);
else
srcDir = new File(src);
File X = programToInput(srcDir);

X = applyTranslators(X, mainTranslators);
X = defaultTranslate(X);

if (translate)
System.out.println("Program translated to: " + X.getAbsolutePath());
else if (list)
System.out.println(loadTextFile(new File(X, "main.java").getPath(), null));
else
javax2(X, ioDir, false);
}

private static File defaultTranslate(File x) throws Exception {
x = luaPrintToJavaPrint(x);
x = autoTranslate(x);
return x;
}

private static File autoTranslate(File x) 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);
}

private static List<String> findTranslators(List<String> lines) {
List<String> translators = new ArrayList<String>();
Pattern pattern = Pattern.compile("^!([0-9# \t]+)");
for (ListIterator<String> iterator = lines.listIterator(); iterator.hasNext(); ) {
String line = iterator.next();
line = line.trim();
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
translators.addAll(Arrays.asList(matcher.group(1).split("[ \t]+")));
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) throws Exception {
for (String translator : translators)
x = applyTranslator(x, translator);
return x;
}

private static File applyTranslator(File x, String translator) throws Exception {
if (verbose)
System.out.println("Using translator " + translator + " on sources in " + x.getPath());
File newDir = runJavaX2_src_from_snippet(translator, null, x, !verbose);
if (!new File(newDir, "main.java").exists())
throw new Exception("Translator " + translator + " did not generate main.java");
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 {
File srcDir = TempDirMaker_make();
saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippet(snippetID, false));
return srcDir;
}

public static File loadSnippetAsMainJavaVerified(String snippetID, String hash) throws IOException {
File srcDir = TempDirMaker_make();
saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippetVerified(snippetID, hash));
return srcDir;
}

/** returns output dir */
private static File runJavaX2_src_from_snippet(String snippetID, String hash, File input, boolean silent) throws Exception {
File srcDir = hash == null ? loadSnippetAsMainJava(snippetID)
: loadSnippetAsMainJavaVerified(snippetID, hash);
srcDir = defaultTranslate(srcDir);
return runJavaX(srcDir, input, silent);
}

/** returns output dir */
private static File runJavaX(File originalSrcDir, File originalInput, boolean silent) throws Exception {
File ioBaseDir = TempDirMaker_make();
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);
return outputDir;
}

private static void copyInput(File src, File dst) throws IOException {
copyDirectory(src, dst);
}

private static File programToInput(File srcDir) {
return srcDir;
}

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);
}

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();
}

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 File DiskSnippetCache_getDir() {
return DiskSnippetCache_dir;
}

public static void initSnippetCache() {
if (DiskSnippetCache_dir == null)
initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"));
}

public static String loadSnippetVerified(String snippetID, String hash) throws IOException {
String text = loadSnippet(snippetID, !hash.isEmpty());
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, boolean preferCached) throws IOException {
return loadSnippet(parseSnippetID(snippetID), preferCached);
}

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, boolean preferCached) throws IOException {
if (preferCached) {
initSnippetCache();
String text = DiskSnippetCache_get(snippetID);
if (text != null)
return text;
}

String 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");
}

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.out.println("Loading: " + url.toExternalForm());
URLConnection con = url.openConnection();
return loadPage(con, url);
}

public static String loadPage(URLConnection con, URL url) throws IOException {
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 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";
}

public static void javax2(File srcDir, File ioBaseDir, boolean silent) throws Exception {
List<File> sources = new ArrayList<File>();
if (verbose) System.out.println("Scanning for sources in " + srcDir.getPath());
scanForSources(srcDir, sources, true);
if (sources.isEmpty()) {
System.out.println("No sources found");
return;
}
File optionsFile = File.createTempFile("javax", "");
File classesDir = TempDirMaker_make();
if (verbose) System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath());
String options = "-d " + bashQuote(classesDir.getPath());
writeOptions(sources, optionsFile, options);
classesDir.mkdirs();
String javacOutput = invokeJavac(optionsFile);
if (verbose) System.out.println("Running program (" + srcDir.getAbsolutePath()
+ ") on io dir " + ioBaseDir.getAbsolutePath() + "\n");
runProgram(javacOutput, classesDir, ioBaseDir, silent);
}

private static void runProgram(String javacOutput, File classesDir, File ioBaseDir,
boolean silent) throws Exception {
// print javac output if compile failed and it hasn't been printed yet
boolean didNotCompile = !hasFile(classesDir, "main.class");
if (verbose || didNotCompile)
System.out.println(javacOutput);
if (didNotCompile)
return;

if (ioBaseDir.getAbsolutePath().equals(new File(".").getAbsolutePath()) && !silent) {
runProgramQuick(classesDir);
return;
}

boolean echoOK = false;
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);
}

private static void runProgramQuick(File classesDir) throws Exception {
URLClassLoader classLoader = new URLClassLoader(new URL[]{classesDir.toURI().toURL()});
Class<?> mainClass = classLoader.loadClass("main");
Method main = mainClass.getMethod("main", String[].class);
main.invoke(null, (Object) new String[0]);
}

private static String invokeJavac(File optionsFile) throws IOException {
String javacOutput = backtick("javac " + bashQuote("@" + optionsFile.getPath()));
if (verbose) System.out.println(javacOutput);
return javacOutput;
}

private static void writeOptions(List<File> sources, File sourcesFile, String moreOptions) throws IOException {
FileWriter writer = new FileWriter(sourcesFile);
for (File source : sources)
writer.write(bashQuote(source.getPath()) + " ");
writer.write(moreOptions);
writer.close();
}

private 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 {
File outFile = File.createTempFile("_backtick", "");
File scriptFile = File.createTempFile("_backtick", "");

String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1";
//Log.info("[Backtick] " + command);
try {
saveTextFile(scriptFile.getPath(), command);
String[] command2 = {"/bin/bash", scriptFile.getPath() };
Process process = Runtime.getRuntime().exec(command2);
try {
process.waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
int value = process.exitValue();
//Log.info("exit value: " + value);
return loadTextFile(outFile.getPath(), "");
} finally {
scriptFile.delete();
}
}

/** 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;
}
}

}}
2015-08-19 14:59:51  delete 
735 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #599
Snippet name: x9.java
Eternal ID of this version: #599/1
Text MD5: 2530dcfd18784a0af3cfbc475392b47d
Author: stefan
Category: javax
Type: Java source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-04-30 17:23:04
Source code size: 21342 bytes / 595 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1235 / 194
Referenced in: [show references]