mirror of
https://github.com/revanced/smali.git
synced 2025-05-11 11:54:29 +02:00
Add support for detecting the console width in unix and windows environments, and printing the usage info accordingly
git-svn-id: https://smali.googlecode.com/svn/trunk@666 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
parent
7bb8ace93b
commit
c616a0dadc
@ -90,6 +90,11 @@
|
||||
<artifactId>dexlib</artifactId>
|
||||
<version>${version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jf</groupId>
|
||||
<artifactId>util</artifactId>
|
||||
<version>${version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-cli</groupId>
|
||||
<artifactId>commons-cli</artifactId>
|
||||
|
@ -30,6 +30,7 @@ package org.jf.baksmali;
|
||||
|
||||
import org.apache.commons.cli.*;
|
||||
import org.jf.dexlib.DexFile;
|
||||
import org.jf.util.ConsoleUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
@ -294,7 +295,7 @@ public class main {
|
||||
*/
|
||||
private static void usage(boolean printDebugOptions) {
|
||||
baksmaliHelpFormatter formatter = new baksmaliHelpFormatter();
|
||||
formatter.setWidth(100);
|
||||
formatter.setWidth(ConsoleUtil.getConsoleWidth());
|
||||
|
||||
formatter.printHelp("java -jar baksmali.jar [options] <dex-file>",
|
||||
"disassembles and/or dumps a dex file", basicOptions, "");
|
||||
|
1
pom.xml
1
pom.xml
@ -25,6 +25,7 @@
|
||||
<module>dexlib</module>
|
||||
<module>smali</module>
|
||||
<module>baksmali</module>
|
||||
<module>util</module>
|
||||
<module>maven-smali-plugin</module>
|
||||
<module>smali-integration-tests</module>
|
||||
</modules>
|
||||
|
@ -84,6 +84,11 @@
|
||||
<artifactId>dexlib</artifactId>
|
||||
<version>${version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jf</groupId>
|
||||
<artifactId>util</artifactId>
|
||||
<version>${version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-cli</groupId>
|
||||
<artifactId>commons-cli</artifactId>
|
||||
|
@ -42,6 +42,7 @@ import org.antlr.runtime.TokenRewriteStream;
|
||||
import org.antlr.runtime.Lexer;
|
||||
import org.antlr.runtime.tree.CommonTree;
|
||||
import org.antlr.runtime.tree.CommonTreeNodeStream;
|
||||
import org.jf.util.ConsoleUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Set;
|
||||
@ -288,7 +289,7 @@ public class main {
|
||||
*/
|
||||
private static void usage(boolean printDebugOptions) {
|
||||
smaliHelpFormatter formatter = new smaliHelpFormatter();
|
||||
formatter.setWidth(100);
|
||||
formatter.setWidth(ConsoleUtil.getConsoleWidth());
|
||||
|
||||
formatter.printHelp("java -jar smali.jar [options] [--] [<smali-file>|folder]*",
|
||||
"assembles a set of smali files into a dex file", basicOptions, "");
|
||||
|
13
util/pom.xml
Normal file
13
util/pom.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.jf</groupId>
|
||||
<artifactId>util</artifactId>
|
||||
<version>${aversion}</version>
|
||||
<parent>
|
||||
<groupId>org.jf</groupId>
|
||||
<artifactId>smali-pom</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
</project>
|
110
util/src/main/java/org/jf/util/ConsoleUtil.java
Normal file
110
util/src/main/java/org/jf/util/ConsoleUtil.java
Normal file
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* [The "BSD licence"]
|
||||
* Copyright (c) 2010 Ben Gruver
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.jf.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ConsoleUtil {
|
||||
/**
|
||||
* Attempt to find the width of the console. If it can't get the width, return a default of 80
|
||||
* @return
|
||||
*/
|
||||
public static int getConsoleWidth() {
|
||||
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
|
||||
try {
|
||||
return attemptMode();
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
return attemptStty();
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
}
|
||||
|
||||
return 80;
|
||||
}
|
||||
|
||||
private static int attemptStty() {
|
||||
String output = attemptCommand(new String[]{"sh", "-c", "stty size < /dev/tty"});
|
||||
if (output == null) {
|
||||
return 80;
|
||||
}
|
||||
|
||||
String[] vals = output.split(" ");
|
||||
if (vals.length < 2) {
|
||||
return 80;
|
||||
}
|
||||
return Integer.parseInt(vals[1]);
|
||||
}
|
||||
|
||||
private static int attemptMode() {
|
||||
String output = attemptCommand(new String[]{"mode", "con"});
|
||||
if (output == null) {
|
||||
return 80;
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile("Columns:[ \t]*(\\d+)");
|
||||
Matcher m = pattern.matcher(output);
|
||||
if (!m.find()) {
|
||||
return 80;
|
||||
}
|
||||
|
||||
return Integer.parseInt(m.group(1));
|
||||
}
|
||||
|
||||
private static String attemptCommand(String[] command) {
|
||||
StringBuffer buffer = null;
|
||||
|
||||
try {
|
||||
|
||||
Process p = Runtime.getRuntime().exec(command);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (buffer == null) {
|
||||
buffer = new StringBuffer();
|
||||
}
|
||||
|
||||
buffer.append(line);
|
||||
}
|
||||
|
||||
if (buffer != null) {
|
||||
return buffer.toString();
|
||||
}
|
||||
return null;
|
||||
} catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user