main.java revision 2b7d3ebb66c5107ecfa2b31edfea5016a55112f7
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.jf.baksmali;
18
19/**
20 * Main class for baksmali. It recognizes enough options to be able to dispatch
21 * to the right "actual" main.
22 */
23public class main {
24
25    public static final String VERSION = "0.91";
26
27
28    private static String USAGE_MESSAGE =
29        "usage:\n" +
30        "  java -jar baksmali.jar --disassemble <.dex file> <output folder>\n" +
31        "    disassembles the given dex file into a set of .smali files\n" +
32        "    in the given folder\n" +
33        "  java -jar baksmali.jar --dump <.dex file> <dump file>\n" +
34        "    dumps the given dex file to a single annotated dump file\n" +
35        "    that follows the order and structure of the dex file.\n" +
36        "  java -jar baksmali.jar --version\n" +
37        "    Print the version of this tool (" + VERSION +
38        ").\n" +
39        "  java -jar baksmali.jar --help\n" +
40        "    Print this message.";
41
42    /**
43     * This class is uninstantiable.
44     */
45    private main() {
46        // This space intentionally left blank.
47    }
48
49    /**
50     * Run!
51     */
52    public static void main(String[] args) {
53        boolean gotCmd = false;
54        boolean showUsage = false;
55
56        try {
57            for (int i = 0; i < args.length; i++) {
58                String arg = args[i];
59                if (arg.equals("--") || !arg.startsWith("--")) {
60                    gotCmd = false;
61                    showUsage = true;
62                    break;
63                }
64
65                gotCmd = true;
66                if (arg.equals("--disassemble")) {
67                    baksmali.main(without(args, i));
68                    break;
69                } else if (arg.equals("--dump")) {
70                    dump.main(without(args, i));
71                    break;
72                } else if (arg.equals("--version")) {
73                    version();
74                    break;
75                } else if (arg.equals("--help")) {
76                    showUsage = true;
77                    break;
78                } else {
79                    gotCmd = false;
80                }
81            }
82        } catch (UsageException ex) {
83            showUsage = true;
84        } catch (RuntimeException ex) {
85            System.err.println("\nUNEXPECTED TOP-LEVEL EXCEPTION:");
86            ex.printStackTrace();
87            System.exit(2);
88        } catch (Throwable ex) {
89            System.err.println("\nUNEXPECTED TOP-LEVEL ERROR:");
90            ex.printStackTrace();
91            System.exit(3);
92        }
93
94        if (!gotCmd) {
95            System.err.println("error: no command specified");
96            showUsage = true;
97        }
98
99        if (showUsage) {
100            usage();
101            System.exit(1);
102        }
103    }
104
105    /**
106     * Prints the version message.
107     */
108    private static void version() {
109        System.err.println("baksmali version " + VERSION);
110        System.exit(0);
111    }
112
113    /**
114     * Prints the usage message.
115     */
116    private static void usage() {
117        System.err.println(USAGE_MESSAGE);
118    }
119
120    /**
121     * Returns a copy of the given args array, but without the indicated
122     * element.
123     *
124     * @param orig non-null; original array
125     * @param n which element to omit
126     * @return non-null; new array
127     */
128    private static String[] without(String[] orig, int n) {
129        int len = orig.length - 1;
130        String[] newa = new String[len];
131        System.arraycopy(orig, 0, newa, 0, n);
132        System.arraycopy(orig, n + 1, newa, n, len - n);
133        return newa;
134    }
135}