/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jf.baksmali; /** * Main class for baksmali. It recognizes enough options to be able to dispatch * to the right "actual" main. */ public class main { public static final String VERSION = "0.91"; private static String USAGE_MESSAGE = "usage:\n" + " java -jar baksmali.jar --disassemble <.dex file> \n" + " disassembles the given dex file into a set of .smali files\n" + " in the given folder\n" + " java -jar baksmali.jar --dump <.dex file> \n" + " dumps the given dex file to a single annotated dump file\n" + " that follows the order and structure of the dex file.\n" + " java -jar baksmali.jar --version\n" + " Print the version of this tool (" + VERSION + ").\n" + " java -jar baksmali.jar --help\n" + " Print this message."; /** * This class is uninstantiable. */ private main() { // This space intentionally left blank. } /** * Run! */ public static void main(String[] args) { boolean gotCmd = false; boolean showUsage = false; try { for (int i = 0; i < args.length; i++) { String arg = args[i]; if (arg.equals("--") || !arg.startsWith("--")) { gotCmd = false; showUsage = true; break; } gotCmd = true; if (arg.equals("--disassemble")) { baksmali.main(without(args, i)); break; } else if (arg.equals("--dump")) { dump.main(without(args, i)); break; } else if (arg.equals("--version")) { version(); break; } else if (arg.equals("--help")) { showUsage = true; break; } else { gotCmd = false; } } } catch (UsageException ex) { showUsage = true; } catch (RuntimeException ex) { System.err.println("\nUNEXPECTED TOP-LEVEL EXCEPTION:"); ex.printStackTrace(); System.exit(2); } catch (Throwable ex) { System.err.println("\nUNEXPECTED TOP-LEVEL ERROR:"); ex.printStackTrace(); System.exit(3); } if (!gotCmd) { System.err.println("error: no command specified"); showUsage = true; } if (showUsage) { usage(); System.exit(1); } } /** * Prints the version message. */ private static void version() { System.err.println("baksmali version " + VERSION); System.exit(0); } /** * Prints the usage message. */ private static void usage() { System.err.println(USAGE_MESSAGE); } /** * Returns a copy of the given args array, but without the indicated * element. * * @param orig non-null; original array * @param n which element to omit * @return non-null; new array */ private static String[] without(String[] orig, int n) { int len = orig.length - 1; String[] newa = new String[len]; System.arraycopy(orig, 0, newa, 0, n); System.arraycopy(orig, n + 1, newa, n, len - n); return newa; } }