\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#49: I/O Subsystem: Add API Tcl\_OutputBuffered(chan)}
\date{July 25, 2001}
\author{Rolf Schroedter}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#49}&\textbf{I/O Subsystem: Add API Tcl\_OutputBuffered(chan)}\\\hline
Author&

Rolf Schroedter \tipmail{rolf.schroedter}{dlr.de}
\\
Created&Wednesday, \(\text{25}^{\text{th}}\) July 2001\\
Type&Project\\
Tcl Version&\(8.4\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.4 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This document proposes the new public function \textit{Tcl\_OutputBuffered()}, analogous to the existing public function \textit{Tcl\_InputBuffered()}.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Tcl has a \textit{Tcl\_InputBuffered()} function but no analog function for the output buffer. A \textit{Tcl\_OutputBuffered()} function would be useful for non-blocking channel drivers which need to know the number of bytes pending in Tcl's output queue.

The implementation of \cite{tip35} allows one to query the number of bytes in the channels input and output queues with a \textit{[fconfigure -queue]} option. This is a useful feature especially for serial ports because the input/output may be really slow or even stall.

On the driver level only the number of bytes in the system queue can be queried. For a non-blocking channel there may also be some pending output in Tcl buffers. Obviously there is not much sense to know only the byte counter at driver level without knowing \textit{Tcl\_OutputBuffered()}.

\section{Related Ideas}

It could also be useful to add general \textit{[fconfigure -inputbuffer -outputbuffer]} options for all channels returning the values from \textit{Tcl\_InputBuffered(chan)} and \textit{Tcl\_OutputBuffered(chan)} respectively.

At this opportunity the code of \textit{Tcl\_Seek()} and \textit{Tcl\_Tell()} may be shortened, because it repeats the code of \textit{Tcl\_InputBuffered()} and \textit{Tcl\_OutputBuffered()}.

\section{Implementation}

This function would be added to \textit{generic/tclIO.c} and be stubs enabled. This new API should not have any impact on existing applications.

The implementation is analog to what is done in \textit{Tcl\_Tell()}:
\begingroup\small\begin{verbatim}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_OutputBuffered --
 *
 *	Returns the number of bytes of output currently buffered in the
 *	common internal buffer of a channel.
 *
 * Results:
 *	The number of output bytes buffered, or zero if the channel is not
 *	open for writing.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_OutputBuffered(chan)
    Tcl_Channel chan;			/* The channel to query. */
{
    ChannelState *statePtr = ((Channel *) chan)->state;
					/* State of real channel structure. */
    ChannelBuffer *bufPtr;
    int bytesBuffered;

    for (bytesBuffered = 0, bufPtr = statePtr->outQueueHead;
	bufPtr != (ChannelBuffer *) NULL;
	bufPtr = bufPtr->nextPtr) {
	bytesBuffered += (bufPtr->nextAdded - bufPtr->nextRemoved);
    }
    if ((statePtr->curOutPtr != (ChannelBuffer *) NULL) &&
	    (statePtr->curOutPtr->nextAdded > statePtr->curOutPtr->nextRemoved)) {
        statePtr->flags |= BUFFER_READY;
        bytesBuffered +=
            (statePtr->curOutPtr->nextAdded - statePtr->curOutPtr->nextRemoved);
    }
    return bytesBuffered;
}

\end{verbatim}
\endgroup

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#35]{tip35}
Rolf Schroedter,
\emph{Enhanced Support for Serial Communications},
on-line at \url{http://purl.org/tcl/tip/35.html}
\end{thebibliography}
\end{document}
