main.java revision 36836121d7ecf72050d3ef065b7ab5fa86548319
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
19import org.jf.baksmali.UsageException;
20
21/**
22 * Main class for baksmali. It recognizes enough options to be able to dispatch
23 * to the right "actual" main.
24 */
25public class main {
26
27    public static final String VERSION = "0.9";
28
29
30    private static String USAGE_MESSAGE =
31        "usage:\n" +
32        "  java -jar baksmali.jar --disassemble <.dex file> <output folder>\n" +
33        "    disassembles the given dex file into a set of .smali files\n" +
34        "    in the given folder\n" +
35        "  java -jar baksmali.jar --version\n" +
36        "    Print the version of this tool (" + VERSION +
37        ").\n" +
38        "  java -jar baksmali.jar --help\n" +
39        "    Print this message.";
40
41    /**
42     * This class is uninstantiable.
43     */
44    private main() {
45        // This space intentionally left blank.
46    }
47
48    /**
49     * Run!
50     */
51    public static void main(String[] args) {
52        boolean gotCmd = false;
53        boolean showUsage = false;
54
55        try {
56            for (int i = 0; i < args.length; i++) {
57                String arg = args[i];
58                if (arg.equals("--") || !arg.startsWith("--")) {
59                    gotCmd = false;
60                    showUsage = true;
61                    break;
62                }
63
64                gotCmd = true;
65                if (arg.equals("--disassemble")) {
66                    baksmali.main(without(args, i));
67                    break;
68                } else if (arg.equals("--version")) {
69                    version();
70                    break;
71                } else if (arg.equals("--help")) {
72                    showUsage = true;
73                    break;
74                } else {
75                    gotCmd = false;
76                }
77            }
78        } catch (UsageException ex) {
79            showUsage = true;
80        } catch (RuntimeException ex) {
81            System.err.println("\nUNEXPECTED TOP-LEVEL EXCEPTION:");
82            ex.printStackTrace();
83            System.exit(2);
84        } catch (Throwable ex) {
85            System.err.println("\nUNEXPECTED TOP-LEVEL ERROR:");
86            ex.printStackTrace();
87            System.exit(3);
88        }
89
90        if (!gotCmd) {
91            System.err.println("error: no command specified");
92            showUsage = true;
93        }
94
95        if (showUsage) {
96            usage();
97            System.exit(1);
98        }
99    }
100
101    /**
102     * Prints the version message.
103     */
104    private static void version() {
105        System.err.println("baksmali version " + VERSION);
106        System.exit(0);
107    }
108
109    /**
110     * Prints the usage message.
111     */
112    private static void usage() {
113        System.err.println(USAGE_MESSAGE);
114    }
115
116    /**
117     * Returns a copy of the given args array, but without the indicated
118     * element.
119     *
120     * @param orig non-null; original array
121     * @param n which element to omit
122     * @return non-null; new array
123     */
124    private static String[] without(String[] orig, int n) {
125        int len = orig.length - 1;
126        String[] newa = new String[len];
127        System.arraycopy(orig, 0, newa, 0, n);
128        System.arraycopy(orig, n + 1, newa, n, len - n);
129        return newa;
130    }
131}