001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.ssh.term;
020
021import org.apache.sshd.common.PtyMode;
022import org.apache.sshd.server.Environment;
023
024import java.nio.charset.Charset;
025import java.util.logging.Level;
026import java.util.logging.Logger;
027import java.util.regex.Matcher;
028import java.util.regex.Pattern;
029
030public class SSHContext {
031
032  /** . */
033  static final Pattern LC_TYPE_PATTERN = Pattern.compile("(?:\\p{Alpha}{2}_\\p{Alpha}{2}\\.)?([^@]+)(?:@.+)?");
034
035  /**
036   * Parse the <code>LC_CTYPE</code> format or return null.
037   *
038   * @param value the value to parse
039   * @return the corresponding charset
040   */
041  public static Charset parseEncoding(String value) {
042    Matcher matcher = LC_TYPE_PATTERN.matcher(value);
043    if (matcher.matches()) {
044      String encoding = matcher.group(1);
045      try {
046        return Charset.forName(encoding);
047      }
048      catch (Exception e) {
049        log.log(Level.FINE, "Could not find charset " + encoding + " for LC_TYPE " + value, e);
050        return null;
051      }
052    } else {
053      log.log(Level.FINE, "Could not parse LC_TYPE " + value);
054    }
055    return null;
056  }
057
058  /** . */
059  private static final Logger log = Logger.getLogger(SSHContext.class.getName());
060
061  /** . */
062  public final int verase;
063
064  /** . */
065  private final Environment env;
066
067  /** . */
068  public final Charset encoding;
069
070  public SSHContext(Environment env) {
071    if (env == null) {
072      throw new NullPointerException("No null env");
073    }
074
075    //
076    Integer verase = env.getPtyModes().get(PtyMode.VERASE);
077    String LC_CTYPE = env.getEnv().get("LC_CTYPE");
078    Charset encoding;
079    if (LC_CTYPE != null) {
080      encoding = parseEncoding(LC_CTYPE);
081    } else {
082      encoding = null;
083    }
084
085    //
086    this.env = env;
087    this.verase = verase != null ? verase : -1;
088    this.encoding = encoding;
089  }
090
091  public int getWidth() {
092    String s = env.getEnv().get(Environment.ENV_COLUMNS);
093    int width = -1;
094    if (s != null) {
095      try {
096        width = Integer.parseInt(s);
097      }
098      catch (NumberFormatException e) {
099        log.log(Level.WARNING, "Could not parse ssh term width " + s);
100      }
101    }
102    return width;
103  }
104
105  public int getHeight() {
106    String s = env.getEnv().get(Environment.ENV_LINES);
107    int height = -1;
108    if (s != null) {
109      try {
110        height = Integer.parseInt(s);
111      }
112      catch (NumberFormatException e) {
113        log.log(Level.WARNING, "Could not parse ssh term height " + s);
114      }
115    }
116    return height;
117  }
118
119  public String getProperty(String key)
120  {
121    return env.getEnv().get(key);
122  }
123}