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", "var",
027  "record", "sealed", "permits", "yield", "when"
028  };
029
030
031/** 
032Returns 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. 
033*/
034public static boolean isJavaKeyword(String word) {
035  /* 
036  arghhh - The array must be sorted in ascending order before calling this method
037  can loose sort order if adding stuff at end and forgetting about this, so always sort this badboy
038  */
039  Arrays.sort(java_keywords);
040  return Arrays.binarySearch(java_keywords, word) >= 0;
041  }
042  
043
044/**
045Returns the number of columns in the terminal - if this program is invoked from a Terminal.
046<p>
047Returns -1 if not a terminal or terminal size cannot be found
048*/
049public static int getTerminalColumnCount()
050  {
051  int cols = -1;
052  try {
053    /*
054    ; columns 145;  //linux
055    ; 145 columns;  //os x
056    */
057  
058    ProcessBuilder pb = new ProcessBuilder("stty", "-a");
059    pb.redirectErrorStream(true);
060    pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
061    Process p = pb.start();
062    InputStream is = p.getInputStream();
063    String s = IOUtil.inputStreamToString(is);
064    //System.out.println("got:----------------\n" + s);
065    //System.out.println("----------------\n");
066  
067    Pattern pat = Pattern.compile("\\s+(\\d+)\\s+columns\\s*|\\s*columns\\s+(\\d+)");
068    Matcher m = pat.matcher(s);
069    if (m.find()) 
070      {
071  //    System.out.println("group 1: " + m.group(1));   //first group 
072  //    System.out.println("group 2: " + m.group(2)); //second group
073      if (m.group(1) != null) {
074        cols = Integer.parseInt(m.group(1));
075        }
076      else if (m.group(2) != null) {
077        cols = Integer.parseInt(m.group(2));
078        }
079      }
080    //System.out.println(col);
081    }
082  catch (Exception e) { 
083    }
084  
085  return cols;
086  }
087  
088
089public static void main (String args[])
090  {
091  Args myargs = new Args(args);
092  String word = myargs.get("word");
093  if (word != null) {
094    System.out.println("isJavaKeyword(\"" + word + "\"): " + isJavaKeyword(word));
095    }
096  System.out.println("Terminal columns: " + getTerminalColumnCount());
097  }
098  
099}
100