From c616a0dadca9cbffdf4827eeb6c2da064ad9f7b7 Mon Sep 17 00:00:00 2001 From: "JesusFreke@JesusFreke.com" Date: Wed, 3 Mar 2010 07:11:32 +0000 Subject: [PATCH] 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 --- baksmali/pom.xml | 5 + .../src/main/java/org/jf/baksmali/main.java | 3 +- pom.xml | 1 + smali/pom.xml | 5 + smali/src/main/java/org/jf/smali/main.java | 3 +- util/pom.xml | 13 +++ .../main/java/org/jf/util/ConsoleUtil.java | 110 ++++++++++++++++++ 7 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 util/pom.xml create mode 100644 util/src/main/java/org/jf/util/ConsoleUtil.java diff --git a/baksmali/pom.xml b/baksmali/pom.xml index 9deb04e5..f0b65098 100644 --- a/baksmali/pom.xml +++ b/baksmali/pom.xml @@ -90,6 +90,11 @@ dexlib ${version} + + org.jf + util + ${version} + commons-cli commons-cli diff --git a/baksmali/src/main/java/org/jf/baksmali/main.java b/baksmali/src/main/java/org/jf/baksmali/main.java index 21ec2585..35110eb7 100644 --- a/baksmali/src/main/java/org/jf/baksmali/main.java +++ b/baksmali/src/main/java/org/jf/baksmali/main.java @@ -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] ", "disassembles and/or dumps a dex file", basicOptions, ""); diff --git a/pom.xml b/pom.xml index f9330537..df038264 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,7 @@ dexlib smali baksmali + util maven-smali-plugin smali-integration-tests diff --git a/smali/pom.xml b/smali/pom.xml index 82f72053..6f8ff624 100644 --- a/smali/pom.xml +++ b/smali/pom.xml @@ -84,6 +84,11 @@ dexlib ${version} + + org.jf + util + ${version} + commons-cli commons-cli diff --git a/smali/src/main/java/org/jf/smali/main.java b/smali/src/main/java/org/jf/smali/main.java index 26abfee5..260167b3 100644 --- a/smali/src/main/java/org/jf/smali/main.java +++ b/smali/src/main/java/org/jf/smali/main.java @@ -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] [--] [|folder]*", "assembles a set of smali files into a dex file", basicOptions, ""); diff --git a/util/pom.xml b/util/pom.xml new file mode 100644 index 00000000..e8ecb073 --- /dev/null +++ b/util/pom.xml @@ -0,0 +1,13 @@ + + + 4.0.0 + org.jf + util + ${aversion} + + org.jf + smali-pom + 1.0-SNAPSHOT + + \ No newline at end of file diff --git a/util/src/main/java/org/jf/util/ConsoleUtil.java b/util/src/main/java/org/jf/util/ConsoleUtil.java new file mode 100644 index 00000000..4d1e030f --- /dev/null +++ b/util/src/main/java/org/jf/util/ConsoleUtil.java @@ -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; + } + } +}