\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#259: Making 'exec' Optionally Binary Safe}
\date{December 12, 2005}
\author{Andreas Leitgeb}
\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 \#259}&\textbf{Making 'exec' Optionally Binary Safe}\\\hline
Author&

Andreas Leitgeb \tipmail{avl}{logic.at}
\\
Created&Monday, \(\text{12}^{\text{th}}\) December 2005\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.4 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

A new option shall be added to the command \textbf{exec}, that allows the user to specify that input redirected from immediate data (using \textbf{\tiplangle{}\tiplangle{}}) and/or the data received from the external command shall not undergo any translation within Tcl.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Motivation}

External programs may expect binary data, or write out binary data, or neither or even both. Whether a program reads/writes binary data or platform-encoded data is generally specific to the particular program and known by the programmer who intends to \textbf{exec} it from a Tcl script.

For example, a hexdump-utility expectably reads binary data and outputs text.

\section{Deficiencies of Current State of exec}

\textbf{Problem 1:} For passing string-data to external programs, \textbf{exec} now behaves arguably incorrect, because it does not pass through \(\backslash\)0-bytes.

\textbf{Problem 2:} For returning the result of external programs, \textbf{exec} applies translations based on system-default encoding, which is OK in most cases, except, of course, for programs that output binary data.

Problem 1 is actually a bug, but for compatibility reasons some internal function cannot be changed, because it is believed to be used by some extensions (although it is not officially exported), thus blocking the fixing of the bug. This TIP goes beyond that, by not having it fixed to some particular consistent behaviour, but to let the script-developer decide on what is the right behaviour for his needs.

Problem 2 prevents the output of binary-outputting programs from being correctly retrieved.

\section{Proposal for a New Option}

The \textbf{exec} command already has an interface for options, and currently supports one option \textbf{-keepnewline} and the end-of-options marker \textbf{-{}-}. This means that adding a new option will not adversely affect any existing scripts.

This TIP proposes a new option, \textbf{-binary} \textit{arg}. \textit{arg} can be either a single boolean value:

\begin{quote}
if a boolean true then both input (if a \textbf{\tiplangle{}\tiplangle{}} redirection is present) and return value are passed verbatim between Tcl and the external program.
\end{quote}

\begin{quote}
if a boolean false (which is the default) then behaviour would be like it is now, except for input being \(\backslash\)0-safe and system-translation taking place as appropriate (e.g. line-endings).
\end{quote}

\textit{arg} can also be one of the keywords \textbf{in}, \textbf{out}, \textbf{both} (which is equivalent to 1) or an empty string (equivalent to 0) for more readable code. The directions are to be seen from external programs perspective.

If \textit{arg} is any true boolean value, \textbf{both} or \textbf{out}, then the option \textbf{-keepnewline} is implied.

If some usage of \textbf{exec} does not use \textbf{\tiplangle{}\tiplangle{}} string-redirection, then the \textbf{in}-bit has no visible effect.

For now, no binary flag is defined for stderr. This might be subject of a future TIP or left out due to lack of need.

\section{Alternatives}

Benjamin Riefenstahl suggested to not make a binary (in the sense of yes/no) decision for each of input and output, but to directly specify the encodings to use (of which ``binary'' would also be a valid one).

This has some subtle disadvantage for usage.

As proposed, there is *one* option with one argument of effectively 4 different values. From each of these values it is evident, on which channels conversion takes place.

To specify arbitrary encodings independently for two channels, there would need to be a list of encodings. For consistency with other commands, the ``stdin''-encoding would have to be first, though it is used rarelier than output-encoding. So most times one would have to pass -encoding \{\{\} binary\} (yuck!) to specify encoding for output only.

Unlike with general channels (as used for open \tipbar{}... or sockets) the data going through exec is always ``limited'': before actually calling exec it exists completely in memory as argument to exec, and afterwards output is returned as one single returnvalue. Each of these chunks can be handled as binary for exec, and explicitly converted through ``encoding convertto'' (for input) or encoding convertfrom (for output).

Encoding per system-default is achieved by not adding any -binary option to exec at all, which covers probably \tiprangle{}95\% of all usages, anyway. (the remaining 5\% being the ones for whom this TIP has been written)

\section{Implementation}

No implementation exists right now, although it is possible that this will change in near future.

An implementation would have to change these functions: Those functions that are not modifyable due to them being used elsewhere, need to be replaced by an extended version, and the old function can be changed to call the new one with appropriate extra arguments, and eventually be phased out.
\begin{description}
\item[Tcl\_ExecObjCmd]
Handle the new option and set new bits for the flags argument of \textit{Tcl\_OpenCommandChannel} or add a new bitset argument.
\item[Tcl\_OpenCommandChannel]
Deal with the new bits, or with new argument and pass them/it on.
\item[TclCreatePipeline]
needs new arguments \textit{flags} (for solving bug \#768678 along the way) and pass new arguments to a new variant of \textit{TclpCreateTempFile}.
\item[TclpCreateTempFile]
both Unix and Win version currently get a dumb C-style char-pointer with no length-information. They need two extra arguments, a length and the appropriate binary-bit. Since \textit{TclpCreateTempFile} is in the stubs-table, we definitely need a new Version of it, e.g. \textit{TclpCreateTempFileEx}, which will take and use these new arguments.
\end{description}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
