1 /* 2 * Copyright 2016, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package org.jf.baksmali; 33 34 import com.beust.jcommander.JCommander; 35 import com.beust.jcommander.Parameter; 36 import com.google.common.collect.Lists; 37 import org.jf.baksmali.HelpCommand.HlepCommand; 38 import org.jf.util.jcommander.Command; 39 import org.jf.util.jcommander.ExtendedCommands; 40 import org.jf.util.jcommander.ExtendedParameters; 41 42 import java.io.IOException; 43 import java.io.InputStream; 44 import java.util.List; 45 import java.util.Properties; 46 47 @ExtendedParameters( 48 includeParametersInUsage = true, 49 commandName = "baksmali", 50 postfixDescription = "See baksmali help <command> for more information about a specific command") 51 public class Main extends Command { 52 public static final String VERSION = loadVersion(); 53 54 @Parameter(names = {"--help", "-h", "-?"}, help = true, 55 description = "Show usage information") 56 private boolean help; 57 58 @Parameter(names = {"--version", "-v"}, help = true, 59 description = "Print the version of baksmali and then exit") 60 public boolean version; 61 62 private JCommander jc; 63 Main()64 public Main() { 65 super(Lists.<JCommander>newArrayList()); 66 } 67 run()68 @Override public void run() { 69 } 70 getJCommander()71 @Override protected JCommander getJCommander() { 72 return jc; 73 } 74 main(String[] args)75 public static void main(String[] args) { 76 Main main = new Main(); 77 78 JCommander jc = new JCommander(main); 79 main.jc = jc; 80 jc.setProgramName("baksmali"); 81 List<JCommander> commandHierarchy = main.getCommandHierarchy(); 82 83 ExtendedCommands.addExtendedCommand(jc, new DisassembleCommand(commandHierarchy)); 84 ExtendedCommands.addExtendedCommand(jc, new DeodexCommand(commandHierarchy)); 85 ExtendedCommands.addExtendedCommand(jc, new DumpCommand(commandHierarchy)); 86 ExtendedCommands.addExtendedCommand(jc, new HelpCommand(commandHierarchy)); 87 ExtendedCommands.addExtendedCommand(jc, new HlepCommand(commandHierarchy)); 88 ExtendedCommands.addExtendedCommand(jc, new ListCommand(commandHierarchy)); 89 90 jc.parse(args); 91 92 if (main.version) { 93 version(); 94 } 95 96 if (jc.getParsedCommand() == null || main.help) { 97 main.usage(); 98 return; 99 } 100 101 Command command = (Command)jc.getCommands().get(jc.getParsedCommand()).getObjects().get(0); 102 command.run(); 103 } 104 version()105 protected static void version() { 106 System.out.println("baksmali " + VERSION + " (http://smali.org)"); 107 System.out.println("Copyright (C) 2010 Ben Gruver (JesusFreke@JesusFreke.com)"); 108 System.out.println("BSD license (http://www.opensource.org/licenses/bsd-license.php)"); 109 System.exit(0); 110 } 111 loadVersion()112 private static String loadVersion() { 113 InputStream propertiesStream = Baksmali.class.getClassLoader().getResourceAsStream("baksmali.properties"); 114 String version = "[unknown version]"; 115 if (propertiesStream != null) { 116 Properties properties = new Properties(); 117 try { 118 properties.load(propertiesStream); 119 version = properties.getProperty("application.version"); 120 } catch (IOException ex) { 121 // ignore 122 } 123 } 124 return version; 125 } 126 } 127