1 6 8 9 package jasmin; 10 11 import java.io.*; 12 import jas.jasError; 13 14 18 public class Main { 19 public static void assemble(InputStream in, OutputStream out, boolean number_lines) { 20 ClassFile classFile = new ClassFile(); 21 22 try { 23 InputStream inp = new BufferedInputStream(in); 24 classFile.readJasmin(inp, "Jasmin", number_lines); 25 inp.close(); 26 27 if (classFile.errorCount() > 0) { 29 System.err.println("Jasmin: Found " 30 + classFile.errorCount() + " errors"); 31 return; 32 } 33 34 classFile.write(out); 35 out.flush(); 36 } catch (java.io.FileNotFoundException e) { 37 System.err.println("Jasmin: file not found"); 38 System.exit(-1); 39 } catch (jasError e) { 40 classFile.report_error("JAS Error " + e.getMessage()); 41 e.printStackTrace(); 42 } catch (Exception e) { 43 classFile.report_error("Jasmin: exception - <" + 44 e.getClass().getName() + "> " + e.getMessage() + 45 "."); 46 e.printStackTrace(); 47 } 48 if (classFile.errorCount() > 0) { 49 System.err.println("Jasmin: Found " 50 + classFile.errorCount() + " errors"); 51 } 52 } 53 54 57 public static final String version = "2.2.3"; 58 59 66 public static void assemble(String dest_dir, String fname, 67 boolean number_lines) { 68 File file = new File(fname); 69 File out_file = null; 70 ClassFile classFile = new ClassFile(); 71 72 try { 73 InputStream inp = new BufferedInputStream(new FileInputStream(fname)); 74 classFile.readJasmin(inp, file.getName(), number_lines); 75 inp.close(); 76 77 if (classFile.errorCount() > 0) { 79 System.err.println(fname + ": Found " 80 + classFile.errorCount() + " errors"); 81 return; 82 } 83 84 String class_path[] = (ScannerUtils.splitClassField( 85 classFile.getClassName())); 86 String class_name = class_path[1]; 87 88 if (class_path[0] != null) { 90 String class_dir = ScannerUtils.convertChars( 91 class_path[0], "./", 92 File.separatorChar); 93 if (dest_dir != null) { 94 dest_dir = dest_dir + File.separator + class_dir; 95 } else { 96 dest_dir = class_dir; 97 } 98 } 99 if (dest_dir == null) { 100 out_file = new File(class_name + ".class"); 101 } else { 102 out_file = new File(dest_dir, class_name + ".class"); 103 104 106 File dest = new File(dest_dir); 107 if (!dest.exists()) { 108 dest.mkdirs(); 109 } 110 111 if (!dest.isDirectory()) { 112 throw new IOException("Cannot create directory"); 113 } 114 } 115 116 FileOutputStream outp = new FileOutputStream(out_file); 117 classFile.write(outp); 118 outp.close(); 119 121 } catch (java.io.FileNotFoundException e) { 122 System.err.println(fname + ": file not found"); 123 System.exit(-1); 124 } catch (jasError e) { 125 classFile.report_error("JAS Error " + e.getMessage()); 126 e.printStackTrace(); 127 } catch (Exception e) { 128 classFile.report_error(fname + ": exception - <" + 129 e.getClass().getName() + "> " + e.getMessage() + 130 "."); 131 e.printStackTrace(); 132 } 133 if (classFile.errorCount() > 0) { 134 System.err.println(fname + ": Found " 135 + classFile.errorCount() + " errors"); 136 } 137 } 138 139 public static void main(String args[]) { 140 int i; 141 String dest_dir = null; 142 boolean debug = false; 143 144 String files[] = new String [args.length]; 145 int num_files = 0; 146 147 if (args.length == 0) { 148 System.err.println("usage: jasmin [-d <directory>] [-version] <file> [<file> ...]"); 149 System.exit(-1); 150 } 151 152 for (i = 0; i < args.length; i++) { 153 if (args[i].equals("-d")) { 154 dest_dir = args[i + 1]; 155 i++; 156 } else if (args[i].equals("-g")) { 157 debug = true; 158 } else if (args[i].equals("-version")) { 159 System.out.println("Jasmin version: " + version); 160 System.exit(0); 161 } else { 162 files[num_files++] = args[i]; 163 } 164 } 165 166 for (i = 0; i < num_files; i++) { 167 assemble(dest_dir, files[i], debug); 168 } 169 } 170 }; 171 172 177 178 179 180 181 | Popular Tags |