<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><!-- generator="wordpress/2.3.2" --><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">

<channel>
	<title>isuraj.com</title>
	<link>http://isuraj.com</link>
	<description>Keep reading, don't expect anything out of it.</description>
	<pubDate>Mon, 04 Feb 2008 02:33:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.2</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/isuraj" type="application/rss+xml" /><item>
		<title>The terminal condition of windows console (Part #1)</title>
		<link>http://isuraj.com/2008/02/04/the-terminal-condition-of-windows-console-part-1/</link>
		<comments>http://isuraj.com/2008/02/04/the-terminal-condition-of-windows-console-part-1/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 02:03:19 +0000</pubDate>
		<dc:creator>Suraj</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://isuraj.com/2008/02/04/the-terminal-condition-of-windows-console-part-1/</guid>
		<description><![CDATA[
For last few days, I have been looking into the source code of Console 2 to understand what makes it tick. I went through the MSDN documentation and obligatory The old new thing to get an idea about how console operates in windows. Spurred on by the comments, I even read the Terminal HOWTO to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://xkcd.com/270/" title="terminal condition"><img src="http://isuraj.com/wp-content/uploads/2008/01/merlin.png" style="width: 140px; height: 171px; margin-right: 10px" alt="terminal condition" align="left" /></a></p>
<p>For last few days, I have been looking into the source code of <a href="http://sourceforge.net/projects/console/">Console 2</a> to understand what makes it tick. I went through the <a href="http://msdn2.microsoft.com/en-us/library/ms682010%28VS.85%29.aspx">MSDN documentation</a> and obligatory <a href="http://blogs.msdn.com/oldnewthing/archive/2007/12/31/6909007.aspx"><em>The old new thing</em></a> to get an idea about how console operates in windows. Spurred on by the comments, I even read the <a href="http://tldp.org/HOWTO/Text-Terminal-HOWTO.html">Terminal HOWTO</a> to get a wider picture. Eventually I understood how Console2 works, its ingenuity and sadly its limitations. So here is a blog post for consolidating whatever info I have gathered. Hopefully you will  find a use for it or even teach me a thing or two. This part just doles out historical background you may want to skip it.</p>
<h2>The console, terminal and shell</h2>
<p>Any operating system (or a runtime library) usually provides a program with <a href="http://en.wikipedia.org/wiki/Standard_streams">stdin, stdout and stderr</a> files. Anything written to <tt>stdout</tt> is displayed to user and any input provided by user can be read from <tt>stdin</tt>. In the beginning, there were line printers for user output. So <tt>stdout</tt> can be considered as just a file stream. Then came mainframes &amp; user terminals which were connected to them via serial bus. The terminals were no different than the printers. Communication was one way only. So anything sent to the terminal was considered final. Jump a little ahead in future and you could see so called smart terminals. These terminals had some kind of controller which allowed them to do fancy stuff. A good example is <a href="http://en.wikipedia.org/wiki/VT100">VT100</a> which was even able to display colors! But the interface to these terminals was still the serial bus. So escape codes were invented. By placing these escape codes in the data stream sent to the terminal, one could use the advanced facilities provided by these terminals; even display a simple <a href="http://en.wikipedia.org/wiki/Curses_%28programming_library%29">GUI</a>! However, the program running on the mainframe had to maintain a buffer in main memory to keep track of the display screen.</p>
<p>The OS used on these mainframes was either custom design or UNIX. So the terminals in UNIX are simple beasts. The OS writes to a device (usually /dev/tty) and the data stream was relayed over serial link to the terminal. Even after personal workstations became a norm; there was no reason to change this interface all you have to do is to emulate a terminal in software. To date all UNIX derivatives use this interface. Your terminal program (xterm or derivative) just listens on /dev/pty and emulates a long dead VT terminal. But this post is about Windows so lets move on.</p>
<h2>In the beginning, there was DOS</h2>
<p>As always many things in windows find their roots in the DOS. Now video adapter was always a part of PC for which the DOS was designed. So the video memory was always accessible. Therefore DOS programs never bothered with escape codes. Even though <a href="http://en.wikipedia.org/wiki/ANSI_escape_code">the support</a> was there, it was always easy &amp; rewarding to directly update the video RAM. So all programs on DOS (except your hello world variety) just took control of video RAM for rendering a <a href="http://en.wikipedia.org/wiki/Conio">GUI</a>.</p>
<h2>Welcome to Windows. Please carry your DOS baggage with you.</h2>
<p>By reading <a href="http://blogs.msdn.com/oldnewthing/">the old new thing</a> I have concluded that, Windows has just one design principle: <em>Backward Compatibility</em>. With windows, the programs started communicating with OS using messages &amp; system dlls. By comparison, DOS programs just cleared DOS from RAM and handled hardware themselves. Up to Windows 98, DOS was real <img src='http://isuraj.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> i.e. the command.com was your command interpreter which ran in <a href="http://en.wikipedia.org/wiki/Virtual_8086_mode">Virtual</a> mode or for playing DOS games, you could boot into <a href="http://en.wikipedia.org/wiki/Real_mode">real</a> mode DOS with no windows baggage attached and your game could then run using <a href="http://en.wikipedia.org/wiki/Unreal_mode">unreal</a> mode <img src='http://isuraj.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> These were the simple old days. The obvious problem with this mode of emulating DOS is a DOS program could very well bring windows to its knees.</p>
<h2>The rebel NT</h2>
<p>The Windows NT line however was a completely different beast. As most of the people are using 2000, XP or Vista which <a href="http://en.wikipedia.org/wiki/Windows_nt">inherit the traits of NT</a>, we are going into the meat of the subject here. Windows NT was <a href="http://en.wikipedia.org/wiki/Architecture_of_Windows_NT">designed</a> to be a container OS. An OS which could host multiple user environments which would share the kernel and device drivers. The only sore thumb was the DOS environment, which placed no restrictions on programs and had no native means of running a <a href="http://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows">message loop</a>. Windows programs communicate using messages for user interface, OS events and pretty much everything else. So a program must wait for a message from windows to do its work.<br />
However, the DOS programs needed some alternate arrangement. They were using <a href="http://spike.scu.edu.au/~barry/interrupts.html">API designed around interrupts</a> to perform user IO. So somebody must translate windows messages into the DOS jargon and simulate the DOS video RAM to display contents in a window. Meet <a href="http://en.wikipedia.org/wiki/Client/Server_Runtime_Subsystem">csrss.exe</a>, the program responsible for handling DOS programs in windows NT.</p>
<h2>What you (or maybe its just me) didn&#8217;t knew about the console</h2>
<p>Every windows program can have a console window! By default windows creates a console window only for programs having <tt>main</tt> function as entry point. The programs with <tt>WinMain</tt> function as entry point do not get a default console window. However, such a program can get its own console by calling <a href="http://msdn2.microsoft.com/en-us/library/ms681944(VS.85).aspx">AllocConsole</a>. Now you could build a better console by either replacing csrss.exe or kernel32.dll. While you are at it you may also want to use your time machine to alter course of history <img src='http://isuraj.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Clearly there is a third way, one used by Console2 which does not involve modifying kernel though it uses scary techniques. We will discuss it in the next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://isuraj.com/2008/02/04/the-terminal-condition-of-windows-console-part-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Shri</title>
		<link>http://isuraj.com/2008/02/01/shri/</link>
		<comments>http://isuraj.com/2008/02/01/shri/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 22:46:48 +0000</pubDate>
		<dc:creator>Suraj</dc:creator>
		
		<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://isuraj.com/2008/02/01/shri/</guid>
		<description><![CDATA[Hope I can keep this blog up &#38; running 
]]></description>
			<content:encoded><![CDATA[<p>Hope I can keep this blog up &amp; running <img src='http://isuraj.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://isuraj.com/2008/02/01/shri/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
