Version 10 of chan mode

Updated 2007-05-04 10:15:09 by suchenwi

Richard Suchenwirth 2007-05-04 - MJ contributed this nice example of how to extend Tcl with a new subcommand to the chan ensemble (new from 8.5). First, write a self-contained function that implements what you want:

 /*
  *----------------------------------------------------------------------
  *
  * Tcl_ModeObjCmd --
  *
  *----------------------------------------------------------------------
 */

        /* ARGSUSED */
 int
 Tcl_ModeObjCmd(
    ClientData dummy,                /* Not used. */
    Tcl_Interp *interp,                /* Current interpreter. */
    int objc,                        /* Number of arguments. */
    Tcl_Obj *const objv[])        /* Argument objects. */
 {
    Tcl_Channel chan;                /* The channel to puts on. */
    const char *channelId; /* Name of channel for puts. */
    int mode;                        /* Mode in which channel is opened. */

    if (objc != 2) {
        Tcl_WrongNumArgs(interp, 1, objv, "channelId");
        return TCL_ERROR;
    }

    channelId = Tcl_GetString(objv[1]);

    chan = Tcl_GetChannel(interp, channelId, &mode);
    if (chan == (Tcl_Channel) NULL) {
        return TCL_ERROR;
    }

    if ((mode & TCL_READABLE) != 0) {
        Tcl_AppendElement(interp, "r");
    }

    if ((mode & TCL_WRITABLE) != 0) {
        Tcl_AppendElement(interp, "w");
    }

    return TCL_OK;

 }

Then, make it available as a command to Tcl:

            Tcl_CreateObjCommand(interp, "::tcl::chan::mode",
                            Tcl_ModeObjCmd, NULL, NULL);