\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#305: ANSI Escape Sequence Support for Windows's Console Channel Driver}
\date{February 21, 2007}
\author{David Gravereaux}
\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 \#305}&\textbf{ANSI Escape Sequence Support for Windows's Console Channel Driver}\\\hline
Author&

David Gravereaux \tipmail{davygrvy}{pobox.com}
\\
Created&Wednesday, \(\text{21}^{\text{st}}\) February 2007\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Withdrawn\\
Vote&Pending\\
Version&\tipversion$Revision: 1.4 $\\
Post History&\\
Discussions To&news:comp.lang.tcl\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

The console channel driver for windows (win/tclWinConsole.c) could support colors, cursor movement, and scrolling by embedding such commands in the stream to the console in the age-old tradition of ANSI\_X3.64-1979 escapes. By filtering-out such commands in the DriverOutputProc and doing the command actions, greater cross-platform support is enabled to other platforms (such as Linux) that have ANSI support.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

A long time ago, in a land far, far away... DOS programmers wrote character-mode applications for a screen that was 80x25 cells large and had 16 colors and a few special attributes using a strange codepage called \textit{IBM PC} or \textit{CP437}. And life wasn't that bad.

Currently, there appears to only be two ways to write such an application in Tcl:
\begin{itemize}
\item{}
Use the services of TWAPI to call the native Console API intrinsics.
\item{}
Use the ck shell (Curses Tcl Toolkit).
\end{itemize}

This change proposal is similar to the first option, but the commands are placed in the stream in a standardized manner that gains us immediate support with Linux.

This change proposal is different than the second option in that only the output direction of the console driver is modified. No support is proposed for such things as raw keyscan and mouse events.

This change proposal will not effect the opening modes of the console, therefore will not have an adverse change to any current scripts.

\section{Specification}

This TIP proposes two changes visible at the script level. Firstly, the \textbf{fconfigure} command will be able to set the speed of channels directed to the Windows console (often being stdout and stderr) via a \textbf{-baud} option. Secondly, channels directed to the Windows console will interpret ANSI control sequences as ANSI control sequences (subject to the limitations outlined below) and not directives to write characters.

The \textit{blink} and \textit{underline} SGR modes can not be supported due to OS limitations, nor can the color palette be modified to affirm color consistency. \textit{reverse} and \textit{concealed} SGR modes can both be emulated, but \textit{concealed} has a security risk and is not a replacement for turning local echo off.

\section{Reference Implementation}

A patch is currently being worked on. There is an ANSI test extension for wish available [\url{http://www.pobox.com/~davygrvy/tclstuff/winAnsiCon12_pack.zip}] that contains numerous test screens (some with ansimations) and appears to work perfectly.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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