001: /*
002: * Env.java --
003: *
004: * This class is used to create and manage the environment array
005: * used by the Tcl interpreter.
006: *
007: * Copyright (c) 1997 Sun Microsystems, Inc.
008: *
009: * See the file "license.terms" for information on usage and redistribution
010: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
011: *
012: * RCS: @(#) $Id: Env.java,v 1.2 1999/08/07 05:46:26 mo Exp $
013: */
014:
015: package tcl.lang;
016:
017: import java.util.*;
018: import java.io.*;
019:
020: /**
021: * This class manages the environment array for Tcl interpreters.
022: */
023:
024: class Env {
025:
026: /*
027: *----------------------------------------------------------------------
028: *
029: * initialize --
030: *
031: * This method is called to initialize an interpreter with it's
032: * initial values for the env array.
033: *
034: * Results:
035: * None.
036: *
037: * Side effects:
038: * The env array in the interpreter is created and populated.
039: *
040: *----------------------------------------------------------------------
041: */
042:
043: static void initialize(Interp interp) {
044: // For a few standrad environment vairables that Tcl users
045: // often assume aways exist (even if they shouldn't), we will
046: // try to create those expected variables with the common unix
047: // names.
048:
049: try {
050: interp.setVar("env", "CLASSPATH", Util
051: .tryGetSystemProperty("java.class.path", ""),
052: TCL.GLOBAL_ONLY);
053: } catch (TclException e) {
054: // Ignore errors.
055: }
056:
057: try {
058: interp.setVar("env", "HOME", Util.tryGetSystemProperty(
059: "user.home", ""), TCL.GLOBAL_ONLY);
060: } catch (TclException e) {
061: // Ignore errors.
062: }
063:
064: try {
065: interp.setVar("env", "USER", Util.tryGetSystemProperty(
066: "user.name", ""), TCL.GLOBAL_ONLY);
067: } catch (TclException e) {
068: // Ignore errors.
069: }
070:
071: // Now we will populate the rest of the env array with the
072: // properties recieved from the System classes. This makes for
073: // a nice shortcut for getting to these useful values.
074:
075: try {
076:
077: Properties props = System.getProperties();
078: Enumeration list = props.propertyNames();
079: while (list.hasMoreElements()) {
080: String key = (String) list.nextElement();
081: try {
082: interp.setVar("env", key, props.getProperty(key),
083: TCL.GLOBAL_ONLY);
084: } catch (TclException e1) {
085: // Ignore errors.
086: }
087: }
088: } catch (SecurityException e2) {
089: // We are inside a browser and we can't access the list of
090: // property names. That's fine. Life goes on ....
091: } catch (Exception e3) {
092: // We are inside a browser and we can't access the list of
093: // property names. That's fine. Life goes on ....
094:
095: System.out
096: .println("Exception while initializing env array");
097: System.out.println(e3);
098: System.out.println("");
099: }
100: }
101:
102: } // end Env
|