1 /*
  2  * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.asmtools.jdec;
 24 
 25 import org.openjdk.asmtools.common.Tool;
 26 import org.openjdk.asmtools.jdis.uEscWriter;
 27 import org.openjdk.asmtools.util.I18NResourceBundle;
 28 import org.openjdk.asmtools.util.ProductInfo;
 29 
 30 import java.io.DataInputStream;
 31 import java.io.PrintStream;
 32 import java.io.PrintWriter;
 33 import java.util.ArrayList;
 34 
 35 /**
 36  * Main program of the Java DECoder :: class to jcod
 37  */
 38 public class Main extends Tool {
 39 
 40     int     printFlags = 0;
 41 
 42     public static final I18NResourceBundle i18n
 43             = I18NResourceBundle.getBundleForClass(Main.class);
 44 
 45     public Main(PrintWriter out, PrintWriter err, String programName) {
 46         super(out, err, programName);
 47         printCannotReadMsg = (fname) ->
 48                 error( i18n.getString("jdec.error.cannot_read", fname));
 49     }
 50 
 51     public Main(PrintStream out, String program) {
 52         this(new PrintWriter(out), new PrintWriter(System.err), program);
 53     }
 54 
 55     @Override
 56     public void usage() {
 57         println(i18n.getString("jdec.usage"));
 58         println(i18n.getString("jdec.opt.g"));
 59         println(i18n.getString("jdec.opt.version"));
 60     }
 61 
 62     /**
 63      * Run the decoder
 64      */
 65     public synchronized boolean decode(String argv[]) {
 66         long tm = System.currentTimeMillis();
 67         ArrayList<String> vargs = new ArrayList<>();
 68         ArrayList<String> vj = new ArrayList<>();
 69         boolean nowrite = false;
 70         int addOptions = 0;
 71 
 72         // Parse arguments
 73         int i = 0;
 74         for (String arg : argv) {
 75             //
 76             if (arg.equals("-g")) {
 77                 printFlags = printFlags | 1;
 78                 vargs.add(arg);
 79             } else if (arg.equals("-v")) {
 80                 DebugFlag = () -> true;
 81                 vargs.add(arg);
 82                 out.println("arg[" + i + "]=" + argv[i] + "/verbose");
 83             } else if (arg.equals("-version")) {
 84                 out.println(ProductInfo.FULL_VERSION);
 85             } else if (arg.startsWith("-")) {
 86                 error(i18n.getString("jdec.error.invalid_flag", arg));
 87                 usage();
 88                 return false;
 89             } else {
 90                 vargs.add(arg);
 91                 vj.add(arg);
 92             }
 93             i += 1;
 94         }
 95 
 96         if (vj.isEmpty()) {
 97             usage();
 98             return false;
 99         }
100 
101         String[] names = new String[0];
102         names = vj.toArray(names);
103         for (String inpname : names) {
104             try {
105                 DataInputStream dataInputStream = getDataInputStream(inpname);
106                 if( dataInputStream == null )
107                     return false;
108                 ClassData cc = new ClassData(dataInputStream, printFlags, out);
109                 cc.DebugFlag = DebugFlag.getAsBoolean();
110                 cc.decodeClass(inpname);
111                 out.flush();
112                 continue;
113             } catch (Error ee) {
114                 if (DebugFlag.getAsBoolean())
115                     ee.printStackTrace();
116                 error(i18n.getString("jdec.error.fatal_error"));
117             } catch (Exception ee) {
118                 if (DebugFlag.getAsBoolean())
119                     ee.printStackTrace();
120                 error(i18n.getString("jdec.error.fatal_exception"));
121             }
122             return false;
123         }
124         return true;
125     }
126 
127     /**
128      * Main program
129      */
130     public static void main(String argv[]) {
131         Main decoder = new Main(new PrintWriter(new uEscWriter(System.out)), new PrintWriter(System.err), "jdec");
132         System.exit(decoder.decode(argv) ? 0 : 1);
133     }
134 }