Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 6ecade9997adb6da0eae58a100ec59317f6ec50a
Ticket: 405995ffffffffffffffffffffffffffffffffff
Tcl_Ungets filling buffer wrongly
User & Date: jiangwu on 2004-02-07 03:29:42
Changes

  1. icomment:
    Logged In: YES 
    user_id=966384
    
    There are no way to test this using normal Tcl command.  
    Here is something I put together, which adds a "ungets" 
    subcommand to "testchannel", then added a single test to 
    the "io.test" to verify ungets.  This test fails without the fix 
    and succeeds with the fix:
    
    > cvs diff -u generic/tclTest.c tests/io.test
    Index: generic/tclTest.c
    ==============================================
    =====================
    RCS file: /home/cvs/starvine/cvsroot/tcl/generic/tclTest.c,v
    retrieving revision 1.2
    diff -u -r1.2 tclTest.c
    --- generic/tclTest.c   11 Oct 2003 07:02:48 -0000      1.2
    +++ generic/tclTest.c   6 Feb 2004 20:26:53 -0000
    @@ -291,6 +291,9 @@
     static Tcl_Channel     TestOpenFileChannelProc3 
    _ANSI_ARGS_((
                                Tcl_Interp *interp, CONST char 
    *fileName,
                                CONST char *modeString, int 
    permissions));
    +static int      UngetsChannelProc _ANSI_ARGS_((
    +                Tcl_Interp *interp, Tcl_Channel channel,
    +                CONST char *input, CONST char *addAtEnd));
     static int             TestpanicCmd _ANSI_ARGS_((ClientData 
    dummy,
                                Tcl_Interp *interp, int argc, CONST char 
    **argv));
     static int             TestparserObjCmd _ANSI_ARGS_
    ((ClientData dummy,
    @@ -5158,6 +5161,46 @@
            return (NULL);
         }
     }
    +
    +
    +/*
    + *--------------------------------------------------------
    --------------
    + *
    + * UngetsChannelProc  --
    + *
    + *     Implements the "ungets ..." subcommand in 
    TestChannelCmd.  Calls
    + *  the "Tcl_Ungets" C API with the given arguments.
    + *
    + * Results:
    + *     A integer value, as returned by Tcl_Ungets.
    + *
    + * Side effects:
    + *     None.
    + *
    + *--------------------------------------------------------
    --------------
    + */
    +static int
    +UngetsChannelProc(interp, channel, input, addAtEnd)
    +    Tcl_Interp  *interp;                /* Interpreter for error 
    reporting */
    +    Tcl_Channel channel;                /* Channel to push the 
    data back */
    +    CONST char  *input;                 /* String to push back  
    */
    +    CONST char  *addAtEnd;              /* A integer flag, 1 or 
    0 for whether
    +                                           the string should be pushed 
    back to
    +                                           the end or the beginning */
    +{
    +    int inputLen = strlen(input);
    +    int len, addAtEndFlag;
    +
    +    if (Tcl_GetInt(interp, addAtEnd, &addAtEndFlag) == 
    TCL_ERROR) {
    +        Tcl_AppendResult(interp, "expecting an integer flag for 
    \"addAtEnd\"",
    +                         (char *) NULL);
    +        return TCL_ERROR;
    +    }
    +    len = Tcl_Ungets(channel, input, inputLen, addAtEndFlag);
    +    Tcl_SetObjResult(interp, Tcl_NewIntObj(len));
    +
    +    return TCL_OK;
    +}
     ♀
     /*
      *---------------------------------------------------------
    -------------
    @@ -5580,9 +5623,23 @@
            return Tcl_UnstackChannel(interp, chan);
         }
    
    +    if ((cmdName[0] == 'u') && (strncmp(cmdName, "ungets", 
    len) == 0)) {
    +       /*
    +        * Syntax: ungets channel string
    +        */
    +
    +        if (argc != 5) {
    +            Tcl_AppendResult(interp, "wrong # args: should be 
    \"", argv[0],
    +                             " ungets channel string atEnd\"", (char 
    *) NULL);
    +            return TCL_ERROR;
    +        }
    +
    +        return UngetsChannelProc(interp, chan, argv[3], argv
    [4]);
    +    }
    +
         Tcl_AppendResult(interp, "bad option \"", cmdName, "\": 
    should be ",
                 "cut, clearchannelhandlers, info, isshared, mode, 
    open, "
    -           "readable, splice, writable, transform, unstack",
    +           "readable, splice, writable, transform, unstack, 
    ungets",
                 (char *) NULL);
         return TCL_ERROR;
     }
    Index: tests/io.test
    ==============================================
    =====================
    RCS file: /home/cvs/starvine/cvsroot/tcl/tests/io.test,v
    retrieving revision 1.2
    diff -u -r1.2 io.test
    --- tests/io.test       11 Oct 2003 07:02:50 -0000      1.2
    +++ tests/io.test       6 Feb 2004 20:27:00 -0000
    @@ -7109,8 +7109,19 @@
         list $x $result
     } {1 {gets {} catch {error writing "stdout": invalid 
    argument}}}
    
    +# Test Tcl_Ungets
    
    -
    +test io-61.1 {testing Tcl_Ungets, push data back at 
    beginning} {testchannel} {
    +    set f [open $path(test1) w]
    +    puts $f "123456789"
    +    close $f
    +    set f [open $path(test1) r]
    +    gets $f
    +    testchannel ungets $f "123" 0
    +    set x [gets $f]
    +    close $f
    +    set x
    +} {123}
    
     # cleanup
     foreach file [list fooBar longfile script output test1 pipe 
    my_script foo \
    
    
    
  2. login: "jiangwu"
  3. mimetype: "text/plain"
  4. username: "jiangwu"