001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 002// The Molly framework is freely distributable under the terms of an 003// MIT-style license. For details, see the molly pages web site at: 004// http://www.mollypages.org/. Use, modify, have fun ! 005 006package fc.util; 007 008import java.util.*; 009import java.io.*; 010import fc.io.*; 011import java.util.regex.*; 012 013/** 014Misc. utils 015*/ 016public final class MiscUtil 017{ 018public static final String java_keywords[] = { 019 "abstract", "assert", "boolean", "break", "byte", "case", "catch", 020 "char", "class", "const", "continue", "default", "do", "double", 021 "else", "extends", "false", "final", "finally", "float", "for", 022 "goto", "if", "implements", "import", "instanceof", "int", 023 "interface", "long", "native", "new", "null", "package", "private", 024 "protected", "public", "return", "short", "static", "strictfp", 025 "super", "switch", "synchronized", "this", "throw", "throws", 026 "transient", "true", "try", "void", "volatile", "while" 027 }; 028 029/** 030Returns true if the specified word is a Java Language Keyword. Keywords (and hence this comparison) are case sensitive, upper case versions of these are ok in Java. 031*/ 032public static boolean isJavaKeyword(String word) { 033 return (Arrays.binarySearch(java_keywords, word) >= 0); 034 } 035 036 037/** 038Returns the number of columns in the terminal - if this program is invoked from a Terminal. 039<p> 040Returns -1 if not a terminal or terminal size cannot be found 041*/ 042public static int getTerminalColumnCount() 043 { 044 int cols = -1; 045 try { 046 /* 047 ; columns 145; //linux 048 ; 145 columns; //os x 049 */ 050 051 ProcessBuilder pb = new ProcessBuilder("stty", "-a"); 052 pb.redirectErrorStream(true); 053 pb.redirectInput(ProcessBuilder.Redirect.INHERIT); 054 Process p = pb.start(); 055 InputStream is = p.getInputStream(); 056 String s = IOUtil.inputStreamToString(is); 057 //System.out.println("got:----------------\n" + s); 058 //System.out.println("----------------\n"); 059 060 Pattern pat = Pattern.compile("\\s+(\\d+)\\s+columns\\s*|\\s*columns\\s+(\\d+)"); 061 Matcher m = pat.matcher(s); 062 if (m.find()) 063 { 064 // System.out.println("group 1: " + m.group(1)); //first group 065 // System.out.println("group 2: " + m.group(2)); //second group 066 if (m.group(1) != null) { 067 cols = Integer.parseInt(m.group(1)); 068 } 069 else if (m.group(2) != null) { 070 cols = Integer.parseInt(m.group(2)); 071 } 072 } 073 //System.out.println(col); 074 } 075 catch (Exception e) { 076 } 077 078 return cols; 079 } 080 081 082public static void main (String args[]) 083 { 084 Args myargs = new Args(args); 085 System.out.println(isJavaKeyword(myargs.getRequired("word"))); 086 System.out.println("Terminal columns: " + getTerminalColumnCount()); 087 } 088 089} 090