001: /*
002: * VwaitCmd.java --
003: *
004: * This file implements the Tcl "vwait" command.
005: *
006: * Copyright (c) 1997 Sun Microsystems, Inc.
007: *
008: * See the file "license.terms" for information on usage and
009: * redistribution of this file, and for a DISCLAIMER OF ALL
010: * WARRANTIES.
011: *
012: * RCS: @(#) $Id: VwaitCmd.java,v 1.3 2005/10/07 06:50:09 mdejong Exp $
013: */
014:
015: package tcl.lang;
016:
017: /*
018: * This class implements the built-in "vwait" command in Tcl.
019: */
020:
021: class VwaitCmd implements Command {
022:
023: /*
024: *----------------------------------------------------------------------
025: *
026: * cmdProc --
027: *
028: * This procedure is invoked as part of the Command interface to
029: * process the "vwait" Tcl command. See the user documentation
030: * for details on what it does.
031: *
032: * Results:
033: * None.
034: *
035: * Side effects:
036: * See the user documentation.
037: *
038: *----------------------------------------------------------------------
039: */
040:
041: public void cmdProc(Interp interp, // Current interpreter.
042: TclObject argv[]) // Argument list.
043: throws TclException // A standard Tcl exception.
044: {
045: if (argv.length != 2) {
046: throw new TclNumArgsException(interp, 1, argv, "name");
047: }
048:
049: VwaitTrace trace = new VwaitTrace();
050: Var.traceVar(interp, argv[1].toString(), null, TCL.GLOBAL_ONLY
051: | TCL.TRACE_WRITES | TCL.TRACE_UNSETS, trace);
052:
053: int foundEvent = 1;
054: while (!trace.done && (foundEvent != 0)) {
055: foundEvent = interp.getNotifier()
056: .doOneEvent(TCL.ALL_EVENTS);
057: }
058:
059: Var.untraceVar(interp, argv[1].toString(), null,
060: TCL.GLOBAL_ONLY | TCL.TRACE_WRITES | TCL.TRACE_UNSETS,
061: trace);
062:
063: // Clear out the interpreter's result, since it may have been set
064: // by event handlers.
065:
066: interp.resetResult();
067:
068: if (foundEvent == 0) {
069: throw new TclException(interp, "can't wait for variable \""
070: + argv[1] + "\": would wait forever");
071: }
072: }
073:
074: } // end VwaitCmd
075:
076: /*
077: * This class handle variable traces for the "vwait" command.
078: */
079:
080: class VwaitTrace implements VarTrace {
081:
082: /*
083: * TraceCmd.cmdProc continuously watches this variable across calls to
084: * doOneEvent(). It returns immediately when done is set to true.
085: */
086:
087: boolean done = false;
088:
089: /*
090: *----------------------------------------------------------------------
091: *
092: * traceProc --
093: *
094: * This function gets called when the variable that "vwait" is
095: * currently watching is written to.
096: *
097: * Results:
098: * None.
099: *
100: * Side effects:
101: * The done variable is set to true, so that "vwait" will break
102: * the waiting loop.
103: *
104: *----------------------------------------------------------------------
105: */
106:
107: public void traceProc(Interp interp, // The current interpreter.
108: String part1, // A Tcl variable or array name.
109: String part2, // Array element name or NULL.
110: int flags) // Mode flags: Should only be TCL.TRACE_WRITES.
111: {
112: done = true;
113: }
114:
115: } // end VwaitTrace
|