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:
JesusFreke@JesusFreke.com 2010-03-03 07:11:32 +00:00
parent 7bb8ace93b
commit c616a0dadc
7 changed files with 138 additions and 2 deletions

View File

@ -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>

View File

@ -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, "");

View File

@ -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>

View File

@ -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>

View File

@ -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
View 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>

View 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;
}
}
}