Unix 终端的基础知识
许多 Unix 系统使用终端。但是在今天的许多情况下,终端也许是一个运行终端程序的 PC 机。从历史上来说,不同的生产商提供了大量的硬件终端。Linux 操作系统包含一个环境变量 TERM,用来表示我们正在使用的终端的类型,如下所示:
ben@ben-m4000t:~$ w 16:35:13 up 6 days, 7:36, 2 users, load average: 0.62, 0.34, 0.25 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ben tty7 :0 19Jan11 6days 24:58 0.86s gnome-session ben pts/0 :0.0 15:04 0.00s 0.27s 0.01s w ben@ben-m4000t:~$ echo $TERM xterm ben@ben-m4000t:~$
硬件终端的多样性使得在一个名为 terminfo 的包中提供一个终端的解决方案。terminfo 的功能是通过属性来描述的,这些属性存储在已编译的 terminfo 文件集合中,并且通常可以在以下目录中找到:
ben@ben-m4000t:~$ ll /etc/terminfo 总计 4 -rw-r--r-- 1 root root 158 2009-10-12 21:12 README ben@ben-m4000t:~$ cat /etc/terminfo/README This directory is for system-local terminfo descriptions. By default, ncurses will search this directory first, then /lib/terminfo, then /usr/share/terminfo. ben@ben-m4000t:~$ ll /usr/share/terminfo 总计 0 ben@ben-m4000t:~$ tree /lib/terminfo /lib/terminfo |-- a | `-- ansi |-- c | |-- cons25 | `-- cygwin |-- d | `-- dumb |-- E | |-- Eterm | `-- Eterm-color -> Eterm |-- h | `-- hurd |-- l | `-- linux |-- m | |-- mach | |-- mach-bold | `-- mach-color |-- p | `-- pcansi |-- r | |-- rxvt | |-- rxvt-basic | |-- rxvt-m -> rxvt-basic | `-- rxvt-unicode |-- s | |-- screen | |-- screen-256color | |-- screen-256color-bce | |-- screen-bce | |-- screen-s | |-- screen-w | `-- sun |-- v | |-- vt100 | |-- vt102 | |-- vt220 | `-- vt52 |-- w | |-- wsvt25 | `-- wsvt25m `-- x |-- xterm |-- xterm-256color |-- xterm-color |-- xterm-debian |-- xterm-mono |-- xterm-r5 |-- xterm-r6 |-- xterm-vt220 `-- xterm-xfree86 13 directories, 38 files ben@ben-m4000t:~$
看到上面的 vt100,有一个非常熟悉的感觉,仿佛回到过去使用 Unix 系统的年代,而现在这种古老的黑白的使用 CRT 的终端硬件再也找不到了。
上面的 xterm 就是前面 TERM 环境变量的值,代表 X Window 的终端模拟器。现在主流的 Linux 操作系统桌面版的 TERM 环境变量的值就是 xterm。而 Linux 操作系统服务器版的 TERM 环境变量的值多数是 linux。而 ansi 代表着一种标准的终端。至于 dumb,代表着一种“哑”终端,只能提供最低限度的功能,如下所示:
ben@ben-m4000t:~$ infocmp dumb # Reconstructed via infocmp from file: /lib/terminfo/d/dumb dumb|80-column dumb tty, am, cols#80, bel=^G, cr=^M, cud1=^J, ind=^J, ben@ben-m4000t:~$ hd /lib/terminfo/d/dumb 00000000 1a 01 18 00 02 00 01 00 82 00 08 00 64 75 6d 62 |............dumb| 00000010 7c 38 30 2d 63 6f 6c 75 6d 6e 20 64 75 6d 62 20 ||80-column dumb | 00000020 74 74 79 00 00 01 50 00 ff ff 00 00 02 00 ff ff |tty...P.........| 00000030 ff ff ff ff ff ff ff ff ff ff ff ff ff ff 04 00 |................| 00000040 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................| * 00000120 ff ff ff ff ff ff ff ff ff ff 06 00 07 00 0d 00 |................| 00000130 0a 00 0a 00 |....| 00000134 ben@ben-m4000t:~$
KnownTerminals 类 (internal static class)
下面就是 mcs/class/corlib/System/KnownTerminals.cs:
001: // 002: // System.KnownTerminals 003: // 004: // Authors: 005: // Gonzalo Paniagua Javier (gonzalo@ximian.com) 006: // 007: // (C) 2005 Novell, Inc (http://www.novell.com) 008: // 009: 010: // 011: // Permission is hereby granted, free of charge, to any person obtaining 012: // a copy of this software and associated documentation files (the 013: // "Software"), to deal in the Software without restriction, including 014: // without limitation the rights to use, copy, modify, merge, publish, 015: // distribute, sublicense, and/or sell copies of the Software, and to 016: // permit persons to whom the Software is furnished to do so, subject to 017: // the following conditions: 018: // 019: // The above copyright notice and this permission notice shall be 020: // included in all copies or substantial portions of the Software. 021: // 022: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 023: // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 024: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 025: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 026: // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 027: // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 028: // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 029: // 030: // Arrays in this file are the contents of terminfo files dumped using the following program: 031: // 032: // using System; 033: // using System.IO; 034: // using System.Text; 035: // 036: // public class DumpIntoArray { 037: // static int Main (string [] args) 038: // { 039: // if (args.Length != 2) { 040: // Console.Error.WriteLine ("Need array name and file name."); 041: // return 1; 042: // } 043: // 044: // Console.WriteLine ("\t\tpublic static byte [] {0} = {{", args [0]); 045: // using (Stream st = File.OpenRead (args [1])) 046: // DumpStream (st); 047: // 048: // Console.WriteLine ("\t\t};"); 049: // return 0; 050: // } 051: // 052: // static void DumpStream (Stream st) 053: // { 054: // byte [] buffer = new byte [4096]; 055: // StringBuilder line = new StringBuilder (); 056: // int nread; 057: // 058: // while ((nread = st.Read (buffer, 0, buffer.Length)) > 0) { 059: // for (int i = 0; i < nread; i++) { 060: // if (line.Length == 0) 061: // line.Append ("\t\t\t"); 062: // 063: // line.AppendFormat ("{0},", buffer [i]); 064: // if (line.Length > 100) { 065: // Console.WriteLine ("{0}", line); 066: // line.Length = 0; 067: // } 068: // } 069: // } 070: // } 071: // } 072: // 073: 074: namespace System 075: { 076: static class KnownTerminals 077: { 078: public static byte [] linux { 079: get { return new byte[] { 080: 26,1,20,0,29,0,16,0,125,1,41,3,108,105,110,117,120,124,108,105,110,117,120,32,99,111,110,115,111, 081: 108,101,0,0,1,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,255,255,8,0,255,255,255,255, 082: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,8,0,64,0,18,0,255,255,0,0, 083: 2,0,4,0,21,0,26,0,33,0,37,0,41,0,255,255,52,0,69,0,71,0,75,0,87,0,255,255,89,0,101,0,255,255,105, 084: 0,109,0,121,0,125,0,255,255,255,255,129,0,135,0,140,0,255,255,255,255,145,0,150,0,155,0,255,255,160, 085: 0,165,0,170,0,175,0,184,0,190,0,255,255,255,255,198,0,203,0,209,0,215,0,255,255,255,255,255,255,255, 086: 255,255,255,255,255,233,0,237,0,255,255,241,0,255,255,255,255,255,255,243,0,255,255,248,0,255,255, 087: 255,255,255,255,255,255,252,0,1,1,7,1,12,1,17,1,22,1,27,1,33,1,39,1,45,1,51,1,56,1,255,255,61,1,255, 088: 255,65,1,70,1,75,1,255,255,255,255,255,255,79,1,255,255,255,255,255,255,255,255,255,255,255,255,255, 089: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,83,1,255,255,86,1,95,1,255,255, 090: 104,1,255,255,113,1,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 091: 255,255,122,1,255,255,255,255,255,255,128,1,131,1,142,1,145,1,147,1,150,1,247,1,255,255,250,1,255, 092: 255,255,255,255,255,255,255,255,255,255,255,252,1,255,255,255,255,255,255,255,255,0,2,255,255,65, 093: 2,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 094: 255,255,255,255,255,255,69,2,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 095: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,74,2,255,255, 096: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 097: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 098: 255,255,255,255,255,255,255,255,255,255,76,2,82,2,88,2,94,2,100,2,106,2,112,2,118,2,124,2,130,2,255, 099: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 100: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 101: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 102: 255,255,255,255,255,255,255,255,255,255,136,2,255,255,255,255,255,255,255,255,255,255,255,255,255, 103: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 104: 255,255,255,255,255,255,255,255,141,2,152,2,157,2,163,2,167,2,176,2,180,2,255,255,255,255,255,255, 105: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 106: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 107: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 108: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 109: 255,255,255,255,5,3,255,255,255,255,255,255,9,3,19,3,255,255,255,255,255,255,255,255,255,255,255, 110: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 111: 29,3,35,3,7,0,13,0,27,91,37,105,37,112,49,37,100,59,37,112,50,37,100,114,0,27,91,51,103,0,27,91,72, 112: 27,91,74,0,27,91,75,0,27,91,74,0,27,91,37,105,37,112,49,37,100,71,0,27,91,37,105,37,112,49,37,100, 113: 59,37,112,50,37,100,72,0,10,0,27,91,72,0,27,91,63,50,53,108,27,91,63,49,99,0,8,0,27,91,63,50,53,104, 114: 27,91,63,48,99,0,27,91,67,0,27,91,65,0,27,91,63,50,53,104,27,91,63,56,99,0,27,91,80,0,27,91,77,0, 115: 27,91,49,49,109,0,27,91,53,109,0,27,91,49,109,0,27,91,50,109,0,27,91,52,104,0,27,91,56,109,0,27,91, 116: 55,109,0,27,91,55,109,0,27,91,52,109,0,27,91,37,112,49,37,100,88,0,27,91,49,48,109,0,27,91,48,59, 117: 49,48,109,0,27,91,52,108,0,27,91,50,55,109,0,27,91,50,52,109,0,27,91,63,53,104,27,91,63,53,108,36, 118: 60,50,48,48,47,62,0,27,91,64,0,27,91,76,0,127,0,27,91,51,126,0,27,91,66,0,27,91,91,65,0,27,91,50, 119: 49,126,0,27,91,91,66,0,27,91,91,67,0,27,91,91,68,0,27,91,91,69,0,27,91,49,55,126,0,27,91,49,56,126, 120: 0,27,91,49,57,126,0,27,91,50,48,126,0,27,91,49,126,0,27,91,50,126,0,27,91,68,0,27,91,54,126,0,27, 121: 91,53,126,0,27,91,67,0,27,91,65,0,13,10,0,27,91,37,112,49,37,100,80,0,27,91,37,112,49,37,100,77,0, 122: 27,91,37,112,49,37,100,64,0,27,91,37,112,49,37,100,76,0,27,99,27,93,82,0,27,56,0,27,91,37,105,37, 123: 112,49,37,100,100,0,27,55,0,10,0,27,77,0,27,91,48,59,49,48,37,63,37,112,49,37,116,59,55,37,59,37, 124: 63,37,112,50,37,116,59,52,37,59,37,63,37,112,51,37,116,59,55,37,59,37,63,37,112,52,37,116,59,53,37, 125: 59,37,63,37,112,53,37,116,59,50,37,59,37,63,37,112,54,37,116,59,49,37,59,37,63,37,112,55,37,116,59, 126: 56,37,59,37,63,37,112,57,37,116,59,49,49,37,59,109,0,27,72,0,9,0,27,91,71,0,43,16,44,17,45,24,46, 127: 25,48,219,96,4,97,177,102,248,103,241,104,176,105,206,106,217,107,191,108,218,109,192,110,197,111, 128: 126,112,196,113,196,114,196,115,95,116,195,117,180,118,193,119,194,120,179,121,243,122,242,123,227, 129: 124,216,125,156,126,254,0,27,91,90,0,27,91,52,126,0,26,0,27,91,50,51,126,0,27,91,50,52,126,0,27,91, 130: 50,53,126,0,27,91,50,54,126,0,27,91,50,56,126,0,27,91,50,57,126,0,27,91,51,49,126,0,27,91,51,50,126, 131: 0,27,91,51,51,126,0,27,91,51,52,126,0,27,91,49,75,0,27,91,37,105,37,100,59,37,100,82,0,27,91,54,110, 132: 0,27,91,63,54,99,0,27,91,99,0,27,91,51,57,59,52,57,109,0,27,93,82,0,27,93,80,37,112,49,37,120,37, 133: 112,50,37,123,50,53,54,125,37,42,37,123,49,48,48,48,125,37,47,37,48,50,120,37,112,51,37,123,50,53, 134: 54,125,37,42,37,123,49,48,48,48,125,37,47,37,48,50,120,37,112,52,37,123,50,53,54,125,37,42,37,123, 135: 49,48,48,48,125,37,47,37,48,50,120,0,27,91,77,0,27,91,51,37,112,49,37,100,109,0,27,91,52,37,112,49, 136: 37,100,109,0,27,91,49,49,109,0,27,91,49,48,109,0 137: }; // linux 138: } 139: } 140: 141: public static byte [] xterm { 142: get { return new byte[] { 143: 26,1,28,0,29,0,15,0,157,1,150,4,120,116,101,114,109,124,88,49,49,32,116,101,114,109,105,110,97,108, 144: 32,101,109,117,108,97,116,111,114,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,80, 145: 0,8,0,24,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,8,0,64, 146: 0,0,0,4,0,6,0,8,0,25,0,30,0,38,0,42,0,46,0,255,255,57,0,74,0,76,0,80,0,87,0,255,255,89,0,96,0,255, 147: 255,100,0,255,255,104,0,108,0,255,255,255,255,112,0,114,0,119,0,124,0,255,255,255,255,133,0,138,0, 148: 255,255,143,0,148,0,153,0,158,0,167,0,169,0,174,0,255,255,183,0,188,0,194,0,200,0,255,255,255,255, 149: 255,255,218,0,255,255,255,255,255,255,236,0,255,255,240,0,255,255,255,255,255,255,242,0,255,255,247, 150: 0,255,255,255,255,255,255,255,255,251,0,255,0,5,1,9,1,13,1,17,1,23,1,29,1,35,1,41,1,47,1,51,1,255, 151: 255,56,1,255,255,60,1,65,1,70,1,255,255,255,255,255,255,74,1,78,1,86,1,255,255,255,255,255,255,255, 152: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,94,1, 153: 103,1,112,1,121,1,255,255,130,1,139,1,148,1,255,255,157,1,255,255,255,255,255,255,166,1,170,1,175, 154: 1,255,255,180,1,183,1,255,255,255,255,201,1,204,1,215,1,218,1,220,1,223,1,45,2,255,255,48,2,255,255, 155: 255,255,255,255,255,255,255,255,255,255,50,2,255,255,255,255,255,255,255,255,54,2,255,255,107,2,255, 156: 255,255,255,111,2,117,2,255,255,255,255,123,2,255,255,255,255,255,255,255,255,255,255,255,255,255, 157: 255,255,255,130,2,134,2,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 158: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 159: 255,255,255,255,255,255,138,2,255,255,255,255,145,2,255,255,255,255,255,255,255,255,150,2,155,2,162, 160: 2,255,255,255,255,167,2,255,255,174,2,255,255,255,255,255,255,181,2,255,255,255,255,255,255,255,255, 161: 255,255,186,2,192,2,198,2,203,2,208,2,213,2,218,2,226,2,234,2,242,2,250,2,2,3,10,3,18,3,26,3,31,3, 162: 36,3,41,3,46,3,54,3,62,3,70,3,78,3,86,3,94,3,102,3,110,3,115,3,120,3,125,3,130,3,138,3,146,3,154, 163: 3,162,3,170,3,178,3,186,3,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 164: 255,255,255,255,255,255,255,255,255,255,255,255,194,3,255,255,255,255,255,255,255,255,255,255,255, 165: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 166: 255,255,255,255,255,255,255,255,255,255,199,3,210,3,215,3,223,3,227,3,255,255,255,255,255,255,255, 167: 255,236,3,50,4,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 168: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 169: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 170: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 171: 255,255,255,255,255,255,120,4,255,255,255,255,255,255,124,4,134,4,255,255,255,255,255,255,255,255, 172: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 173: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 174: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 175: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,144,4,147,4,27,91,90,0,7,0,13, 176: 0,27,91,37,105,37,112,49,37,100,59,37,112,50,37,100,114,0,27,91,51,103,0,27,91,72,27,91,50,74,0,27, 177: 91,75,0,27,91,74,0,27,91,37,105,37,112,49,37,100,71,0,27,91,37,105,37,112,49,37,100,59,37,112,50, 178: 37,100,72,0,10,0,27,91,72,0,27,91,63,50,53,108,0,8,0,27,91,63,50,53,104,0,27,91,67,0,27,91,65,0,27, 179: 91,80,0,27,91,77,0,14,0,27,91,53,109,0,27,91,49,109,0,27,91,63,49,48,52,57,104,0,27,91,52,104,0,27, 180: 91,56,109,0,27,91,55,109,0,27,91,55,109,0,27,91,52,109,0,27,91,37,112,49,37,100,88,0,15,0,27,91,109, 181: 15,0,27,91,63,49,48,52,57,108,0,27,91,52,108,0,27,91,50,55,109,0,27,91,50,52,109,0,27,91,63,53,104, 182: 36,60,49,48,48,47,62,27,91,63,53,108,0,27,91,33,112,27,91,63,51,59,52,108,27,91,52,108,27,62,0,27, 183: 91,76,0,127,0,27,91,51,126,0,27,79,66,0,27,79,80,0,27,91,50,49,126,0,27,79,81,0,27,79,82,0,27,79, 184: 83,0,27,91,49,53,126,0,27,91,49,55,126,0,27,91,49,56,126,0,27,91,49,57,126,0,27,91,50,48,126,0,27, 185: 79,72,0,27,91,50,126,0,27,79,68,0,27,91,54,126,0,27,91,53,126,0,27,79,67,0,27,79,65,0,27,91,63,49, 186: 108,27,62,0,27,91,63,49,104,27,61,0,27,91,37,112,49,37,100,80,0,27,91,37,112,49,37,100,77,0,27,91, 187: 37,112,49,37,100,66,0,27,91,37,112,49,37,100,64,0,27,91,37,112,49,37,100,76,0,27,91,37,112,49,37, 188: 100,68,0,27,91,37,112,49,37,100,67,0,27,91,37,112,49,37,100,65,0,27,91,105,0,27,91,52,105,0,27,91, 189: 53,105,0,27,99,0,27,91,33,112,27,91,63,51,59,52,108,27,91,52,108,27,62,0,27,56,0,27,91,37,105,37, 190: 112,49,37,100,100,0,27,55,0,10,0,27,77,0,27,91,48,37,63,37,112,54,37,116,59,49,37,59,37,63,37,112, 191: 50,37,116,59,52,37,59,37,63,37,112,49,37,112,51,37,124,37,116,59,55,37,59,37,63,37,112,52,37,116, 192: 59,53,37,59,37,63,37,112,55,37,116,59,56,37,59,109,37,63,37,112,57,37,116,14,37,101,15,37,59,0,27, 193: 72,0,9,0,27,79,69,0,96,96,97,97,102,102,103,103,105,105,106,106,107,107,108,108,109,109,110,110,111, 194: 111,112,112,113,113,114,114,115,115,116,116,117,117,118,118,119,119,120,120,121,121,122,122,123,123, 195: 124,124,125,125,126,126,0,27,91,90,0,27,91,63,55,104,0,27,91,63,55,108,0,27,40,66,27,41,48,0,27,79, 196: 70,0,27,79,77,0,27,91,51,59,53,126,0,27,79,53,70,0,27,79,53,72,0,27,91,50,59,53,126,0,27,79,53,68, 197: 0,27,91,54,59,53,126,0,27,91,53,59,53,126,0,27,79,53,67,0,27,91,50,51,126,0,27,91,50,52,126,0,27, 198: 79,50,80,0,27,79,50,81,0,27,79,50,82,0,27,79,50,83,0,27,91,49,53,59,50,126,0,27,91,49,55,59,50,126, 199: 0,27,91,49,56,59,50,126,0,27,91,49,57,59,50,126,0,27,91,50,48,59,50,126,0,27,91,50,49,59,50,126,0, 200: 27,91,50,51,59,50,126,0,27,91,50,52,59,50,126,0,27,79,53,80,0,27,79,53,81,0,27,79,53,82,0,27,79,53, 201: 83,0,27,91,49,53,59,53,126,0,27,91,49,55,59,53,126,0,27,91,49,56,59,53,126,0,27,91,49,57,59,53,126, 202: 0,27,91,50,48,59,53,126,0,27,91,50,49,59,53,126,0,27,91,50,51,59,53,126,0,27,91,50,52,59,53,126,0, 203: 27,79,54,80,0,27,79,54,81,0,27,79,54,82,0,27,79,54,83,0,27,91,49,53,59,54,126,0,27,91,49,55,59,54, 204: 126,0,27,91,49,56,59,54,126,0,27,91,49,57,59,54,126,0,27,91,50,48,59,54,126,0,27,91,50,49,59,54,126, 205: 0,27,91,50,51,59,54,126,0,27,91,50,52,59,54,126,0,27,91,49,75,0,27,91,37,105,37,100,59,37,100,82, 206: 0,27,91,54,110,0,27,91,63,49,59,50,99,0,27,91,99,0,27,91,51,57,59,52,57,109,0,27,91,51,37,63,37,112, 207: 49,37,123,49,125,37,61,37,116,52,37,101,37,112,49,37,123,51,125,37,61,37,116,54,37,101,37,112,49, 208: 37,123,52,125,37,61,37,116,49,37,101,37,112,49,37,123,54,125,37,61,37,116,51,37,101,37,112,49,37, 209: 100,37,59,109,0,27,91,52,37,63,37,112,49,37,123,49,125,37,61,37,116,52,37,101,37,112,49,37,123,51, 210: 125,37,61,37,116,54,37,101,37,112,49,37,123,52,125,37,61,37,116,49,37,101,37,112,49,37,123,54,125, 211: 37,61,37,116,51,37,101,37,112,49,37,100,37,59,109,0,27,91,77,0,27,91,51,37,112,49,37,100,109,0,27, 212: 91,52,37,112,49,37,100,109,0,27,108,0,27,109,0 213: }; // xterm 214: } 215: } 216: 217: public static byte [] ansi { 218: get { return new byte [] { 219: 26,1,40,0,23,0,16,0,125,1,68,2,97,110,115,105,124,97,110,115,105,47,112,99,45,116,101,114,109,32, 220: 99,111,109,112,97,116,105,98,108,101,32,119,105,116,104,32,99,111,108,111,114,0,0,1,0,0,0,0,0,0,0, 221: 0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,80,0,8,0,24,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 222: 255,255,255,255,255,255,8,0,64,0,3,0,0,0,4,0,6,0,255,255,8,0,13,0,20,0,24,0,28,0,255,255,39,0,56, 223: 0,60,0,255,255,64,0,255,255,255,255,68,0,255,255,72,0,255,255,76,0,80,0,255,255,255,255,84,0,90,0, 224: 95,0,255,255,255,255,255,255,255,255,100,0,255,255,105,0,110,0,115,0,120,0,129,0,135,0,255,255,255, 225: 255,255,255,143,0,147,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,151,0,255, 226: 255,155,0,255,255,255,255,255,255,255,255,255,255,157,0,255,255,255,255,255,255,255,255,255,255,255, 227: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,161,0,165,0,255,255,169,0,255, 228: 255,255,255,255,255,173,0,255,255,255,255,255,255,177,0,255,255,255,255,255,255,255,255,255,255,255, 229: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,181,0,255,255,186,0,195, 230: 0,204,0,213,0,222,0,231,0,240,0,249,0,2,1,11,1,255,255,255,255,255,255,255,255,20,1,25,1,30,1,255, 231: 255,255,255,255,255,255,255,255,255,50,1,255,255,61,1,255,255,63,1,149,1,255,255,152,1,255,255,255, 232: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,156,1,255,255,219,1,255, 233: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 234: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 235: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 236: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 237: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 238: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 239: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 240: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 241: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 242: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,223,1,255,255,255,255,255,255,255,255,255, 243: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 244: 255,255,255,255,255,255,255,255,255,255,255,255,228,1,239,1,244,1,7,2,11,2,255,255,255,255,255,255, 245: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 246: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 247: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 248: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 249: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,20,2,30,2,255,255,255,255,255,255, 250: 40,2,44,2,48,2,52,2,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 251: 255,255,56,2,62,2,27,91,90,0,7,0,13,0,27,91,50,103,0,27,91,72,27,91,74,0,27,91,75,0,27,91,74,0,27, 252: 91,37,105,37,112,49,37,100,71,0,27,91,37,105,37,112,49,37,100,59,37,112,50,37,100,72,0,27,91,66,0, 253: 27,91,72,0,27,91,68,0,27,91,67,0,27,91,65,0,27,91,80,0,27,91,77,0,27,91,49,49,109,0,27,91,53,109, 254: 0,27,91,49,109,0,27,91,56,109,0,27,91,55,109,0,27,91,55,109,0,27,91,52,109,0,27,91,37,112,49,37,100, 255: 88,0,27,91,49,48,109,0,27,91,48,59,49,48,109,0,27,91,109,0,27,91,109,0,27,91,76,0,8,0,27,91,66,0, 256: 27,91,72,0,27,91,76,0,27,91,68,0,27,91,67,0,27,91,65,0,13,27,91,83,0,27,91,37,112,49,37,100,80,0, 257: 27,91,37,112,49,37,100,77,0,27,91,37,112,49,37,100,66,0,27,91,37,112,49,37,100,64,0,27,91,37,112, 258: 49,37,100,83,0,27,91,37,112,49,37,100,76,0,27,91,37,112,49,37,100,68,0,27,91,37,112,49,37,100,67, 259: 0,27,91,37,112,49,37,100,84,0,27,91,37,112,49,37,100,65,0,27,91,52,105,0,27,91,53,105,0,37,112,49, 260: 37,99,27,91,37,112,50,37,123,49,125,37,45,37,100,98,0,27,91,37,105,37,112,49,37,100,100,0,10,0,27, 261: 91,48,59,49,48,37,63,37,112,49,37,116,59,55,37,59,37,63,37,112,50,37,116,59,52,37,59,37,63,37,112, 262: 51,37,116,59,55,37,59,37,63,37,112,52,37,116,59,53,37,59,37,63,37,112,54,37,116,59,49,37,59,37,63, 263: 37,112,55,37,116,59,56,37,59,37,63,37,112,57,37,116,59,49,49,37,59,109,0,27,72,0,27,91,73,0,43,16, 264: 44,17,45,24,46,25,48,219,96,4,97,177,102,248,103,241,104,176,106,217,107,191,108,218,109,192,110, 265: 197,111,126,112,196,113,196,114,196,115,95,116,195,117,180,118,193,119,194,120,179,121,243,122,242, 266: 123,227,124,216,125,156,126,254,0,27,91,90,0,27,91,49,75,0,27,91,37,105,37,100,59,37,100,82,0,27, 267: 91,54,110,0,27,91,63,37,91,59,48,49,50,51,52,53,54,55,56,57,93,99,0,27,91,99,0,27,91,51,57,59,52, 268: 57,109,0,27,91,51,37,112,49,37,100,109,0,27,91,52,37,112,49,37,100,109,0,27,40,66,0,27,41,66,0,27, 269: 42,66,0,27,43,66,0,27,91,49,49,109,0,27,91,49,48,109,0 270: }; // ansi 271: } 272: } 273: } 274: }
上述源程序定义了 KnownTerminals 类,这是一个 internal 的静态类,只有以下三个成员:
- linux: public static byte [],公共静态只读属性。(78 - 139 行)
- xterm: public static byte [],公共静态只读属性。(141 - 215 行)
- ansi: public static byte [],公共静态只读属性。(217 - 271 行)
上述三个公共静态只读属性表示了 linux、xterm 和 ansi 三种终端类型。其数据是由上述源程序中第 32 行到第 71 行的程序生成的,而不是手工录入的。
在 Console.dll 项目中,KnownTerminals 类仅在 TermInfoDriver.cs 中被作为下面要讲的 TermInfoReader 类的构造函数的参数使用。
TermInfoBooleans 枚举 (internal enum)
下面就是 mcs/class/corlib/System/TermInfoBooleans.cs:
01: // 02: // System.TermInfoBooleans 03: // 04: // Authors: 05: // Gonzalo Paniagua Javier (gonzalo@ximian.com) 06: // 07: // (C) 2005 Novell, Inc (http://www.novell.com) 08: // 09: 10: // 11: // Permission is hereby granted, free of charge, to any person obtaining 12: // a copy of this software and associated documentation files (the 13: // "Software"), to deal in the Software without restriction, including 14: // without limitation the rights to use, copy, modify, merge, publish, 15: // distribute, sublicense, and/or sell copies of the Software, and to 16: // permit persons to whom the Software is furnished to do so, subject to 17: // the following conditions: 18: // 19: // The above copyright notice and this permission notice shall be 20: // included in all copies or substantial portions of the Software. 21: // 22: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23: // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26: // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27: // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28: // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29: // 30: 31: // These values are taken from 'man 5 terminfo' and /usr/include/term.h. 32: // They are the indexes for the boolean capabilities in a terminfo file. 33: namespace System { 34: enum TermInfoBooleans { 35: AutoLeftMargin, // 0 36: AutoRightMargin, 37: NoEscCtlc, 38: CeolStandoutGlitch, 39: EatNewlineGlitch, 40: EraseOverstrike, 41: GenericType, 42: HardCopy, 43: HasMetaKey, 44: HasStatusLine, 45: InsertNullGlitch, 46: MemoryAbove, 47: MemoryBelow, 48: MoveInsertMode, 49: MoveStandoutMode, 50: OverStrike, 51: StatusLineEscOk, 52: DestTabsMagicSmso, 53: TildeGlitch, 54: TransparentUnderline, 55: XonXoff, 56: NeedsXonXoff, 57: PrtrSilent, 58: HardCursor, 59: NonRevRmcup, 60: NoPadChar, 61: NonDestScrollRegion, 62: CanChange, 63: BackColorErase, 64: HueLightnessSaturation, 65: ColAddrGlitch, 66: CrCancelsMicroMode, 67: HasPrintWheel, 68: RowAddrGlitch, 69: SemiAutoRightMargin, 70: CpiChangesRes, 71: LpiChangesRes, // 36 72: Last 73: } 74: }
上述源程序定义了 TermInfoBooleans 枚举,表示终端的布尔功能,指示终端是否支持特定的特性。
在 Console.dll 项目中,TermInfoBooleans 枚举在 TermInfoReader.cs 中被使用。
TermInfoNumbers 枚举 (internal enum)
下面就是 mcs/class/corlib/System/TermInfoNumbers.cs:
01: // 02: // System.TermInfoNumbers 03: // 04: // Authors: 05: // Gonzalo Paniagua Javier (gonzalo@ximian.com) 06: // 07: // (C) 2005 Novell, Inc (http://www.novell.com) 08: // 09: 10: // 11: // Permission is hereby granted, free of charge, to any person obtaining 12: // a copy of this software and associated documentation files (the 13: // "Software"), to deal in the Software without restriction, including 14: // without limitation the rights to use, copy, modify, merge, publish, 15: // distribute, sublicense, and/or sell copies of the Software, and to 16: // permit persons to whom the Software is furnished to do so, subject to 17: // the following conditions: 18: // 19: // The above copyright notice and this permission notice shall be 20: // included in all copies or substantial portions of the Software. 21: // 22: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23: // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26: // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27: // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28: // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29: // 30: #if !NET_2_1 31: 32: // These values are taken from 'man 5 terminfo' and /usr/include/term.h. 33: // They are the indexes for the numeric capabilities in a terminfo file. 34: namespace System { 35: enum TermInfoNumbers { 36: Columns, // 0 37: InitTabs, 38: Lines, 39: LinesOfMemory, 40: MagicCookieGlitch, 41: PaddingBaudRate, 42: VirtualTerminal, 43: WidthStatusLine, 44: NumLabels, 45: LabelHeight, 46: LabelWidth, 47: MaxAttributes, 48: MaximumWindows, 49: MaxColors, 50: MaxPairs, 51: NoColorVideo, 52: BufferCapacity, 53: DotVertSpacing, 54: DotHorzSpacing, 55: MaxMicroAddress, 56: MaxMicroJump, 57: MicroColSize, 58: MicroLineSize, 59: NumberOfPins, 60: OutputResChar, 61: OutputResLine, 62: OutputResHorzInch, 63: OutputResVertInch, 64: PrintRate, 65: WideCharSize, 66: Buttons, 67: BitImageEntwining, 68: BitImageType, // 32 69: Last 70: } 71: } 72: #endif
上述源程序定义了 TermInfoNumbers 枚举,表示终端的数字功能,定义了尺寸,例如屏幕的行数与列数。
在 Console.dll 项目中,TermInfoNumbers 枚举在 TermInfoReader.cs 和 TermInfoDriver.cs 中被使用。
TermInfoStrings 枚举 (internal enum)
下面就是 mcs/class/corlib/System/TermInfoStrings.cs:
001: // 002: // System.TermInfoStrings 003: // 004: // Authors: 005: // Gonzalo Paniagua Javier (gonzalo@ximian.com) 006: // 007: // (C) 2005 Novell, Inc (http://www.novell.com) 008: // 009: 010: // 011: // Permission is hereby granted, free of charge, to any person obtaining 012: // a copy of this software and associated documentation files (the 013: // "Software"), to deal in the Software without restriction, including 014: // without limitation the rights to use, copy, modify, merge, publish, 015: // distribute, sublicense, and/or sell copies of the Software, and to 016: // permit persons to whom the Software is furnished to do so, subject to 017: // the following conditions: 018: // 019: // The above copyright notice and this permission notice shall be 020: // included in all copies or substantial portions of the Software. 021: // 022: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 023: // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 024: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 025: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 026: // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 027: // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 028: // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 029: // 030: #if !NET_2_1 031: 032: // These values are taken from 'man 5 terminfo' and /usr/include/term.h. 033: // They are the indexes for the string capabilities in a terminfo file. 034: namespace System { 035: enum TermInfoStrings { 036: BackTab, // 0 037: Bell, 038: CarriageReturn, 039: ChangeScrollRegion, 040: ClearAllTabs, 041: ClearScreen, 042: ClrEol, 043: ClrEos, 044: ColumnAddress, 045: CommandCharacter, 046: CursorAddress, 047: CursorDown, 048: CursorHome, 049: CursorInvisible, 050: CursorLeft, 051: CursorMemAddress, 052: CursorNormal, 053: CursorRight, 054: CursorToLl, 055: CursorUp, 056: CursorVisible, 057: DeleteCharacter, 058: DeleteLine, 059: DisStatusLine, 060: DownHalfLine, 061: EnterAltCharsetMode, 062: EnterBlinkMode, 063: EnterBoldMode, 064: EnterCaMode, 065: EnterDeleteMode, 066: EnterDimMode, 067: EnterInsertMode, 068: EnterSecureMode, 069: EnterProtectedMode, 070: EnterReverseMode, 071: EnterStandoutMode, 072: EnterUnderlineMode, 073: EraseChars, 074: ExitAltCharsetMode, 075: ExitAttributeMode, 076: ExitCaMode, 077: ExitDeleteMode, 078: ExitInsertMode, 079: ExitStandoutMode, 080: ExitUnderlineMode, 081: FlashScreen, 082: FormFeed, 083: FromStatusLine, 084: Init1string, 085: Init2string, 086: Init3string, 087: InitFile, 088: InsertCharacter, 089: InsertLine, 090: InsertPadding, 091: KeyBackspace, 092: KeyCatab, 093: KeyClear, 094: KeyCtab, 095: KeyDc, 096: KeyDl, 097: KeyDown, 098: KeyEic, 099: KeyEol, 100: KeyEos, 101: KeyF0, 102: KeyF1, 103: KeyF10, 104: KeyF2, 105: KeyF3, 106: KeyF4, 107: KeyF5, 108: KeyF6, 109: KeyF7, 110: KeyF8, 111: KeyF9, 112: KeyHome, 113: KeyIc, 114: KeyIl, 115: KeyLeft, 116: KeyLl, 117: KeyNpage, 118: KeyPpage, 119: KeyRight, 120: KeySf, 121: KeySr, 122: KeyStab, 123: KeyUp, 124: KeypadLocal, 125: KeypadXmit, 126: LabF0, 127: LabF1, 128: LabF10, 129: LabF2, 130: LabF3, 131: LabF4, 132: LabF5, 133: LabF6, 134: LabF7, 135: LabF8, 136: LabF9, 137: MetaOff, 138: MetaOn, 139: Newline, 140: PadChar, 141: ParmDch, 142: ParmDeleteLine, 143: ParmDownCursor, 144: ParmIch, 145: ParmIndex, 146: ParmInsertLine, 147: ParmLeftCursor, 148: ParmRightCursor, 149: ParmRindex, 150: ParmUpCursor, 151: PkeyKey, 152: PkeyLocal, 153: PkeyXmit, 154: PrintScreen, 155: PrtrOff, 156: PrtrOn, 157: RepeatChar, 158: Reset1string, 159: Reset2string, 160: Reset3string, 161: ResetFile, 162: RestoreCursor, 163: RowAddress, 164: SaveCursor, 165: ScrollForward, 166: ScrollReverse, 167: SetAttributes, 168: SetTab, 169: SetWindow, 170: Tab, 171: ToStatusLine, 172: UnderlineChar, 173: UpHalfLine, 174: InitProg, 175: KeyA1, 176: KeyA3, 177: KeyB2, 178: KeyC1, 179: KeyC3, 180: PrtrNon, 181: CharPadding, 182: AcsChars, 183: PlabNorm, 184: KeyBtab, 185: EnterXonMode, 186: ExitXonMode, 187: EnterAmMode, 188: ExitAmMode, 189: XonCharacter, 190: XoffCharacter, 191: EnaAcs, 192: LabelOn, 193: LabelOff, 194: KeyBeg, 195: KeyCancel, 196: KeyClose, 197: KeyCommand, 198: KeyCopy, 199: KeyCreate, 200: KeyEnd, 201: KeyEnter, 202: KeyExit, 203: KeyFind, 204: KeyHelp, 205: KeyMark, 206: KeyMessage, 207: KeyMove, 208: KeyNext, 209: KeyOpen, 210: KeyOptions, 211: KeyPrevious, 212: KeyPrint, 213: KeyRedo, 214: KeyReference, 215: KeyRefresh, 216: KeyReplace, 217: KeyRestart, 218: KeyResume, 219: KeySave, 220: KeySuspend, 221: KeyUndo, 222: KeySbeg, 223: KeyScancel, 224: KeyScommand, 225: KeyScopy, 226: KeyScreate, 227: KeySdc, 228: KeySdl, 229: KeySelect, 230: KeySend, 231: KeySeol, 232: KeySexit, 233: KeySfind, 234: KeyShelp, 235: KeyShome, 236: KeySic, 237: KeySleft, 238: KeySmessage, 239: KeySmove, 240: KeySnext, 241: KeySoptions, 242: KeySprevious, 243: KeySprint, 244: KeySredo, 245: KeySreplace, 246: KeySright, 247: KeySrsume, 248: KeySsave, 249: KeySsuspend, 250: KeySundo, 251: ReqForInput, 252: KeyF11, 253: KeyF12, 254: KeyF13, 255: KeyF14, 256: KeyF15, 257: KeyF16, 258: KeyF17, 259: KeyF18, 260: KeyF19, 261: KeyF20, 262: KeyF21, 263: KeyF22, 264: KeyF23, 265: KeyF24, 266: KeyF25, 267: KeyF26, 268: KeyF27, 269: KeyF28, 270: KeyF29, 271: KeyF30, 272: KeyF31, 273: KeyF32, 274: KeyF33, 275: KeyF34, 276: KeyF35, 277: KeyF36, 278: KeyF37, 279: KeyF38, 280: KeyF39, 281: KeyF40, 282: KeyF41, 283: KeyF42, 284: KeyF43, 285: KeyF44, 286: KeyF45, 287: KeyF46, 288: KeyF47, 289: KeyF48, 290: KeyF49, 291: KeyF50, 292: KeyF51, 293: KeyF52, 294: KeyF53, 295: KeyF54, 296: KeyF55, 297: KeyF56, 298: KeyF57, 299: KeyF58, 300: KeyF59, 301: KeyF60, 302: KeyF61, 303: KeyF62, 304: KeyF63, 305: ClrBol, 306: ClearMargins, 307: SetLeftMargin, 308: SetRightMargin, 309: LabelFormat, 310: SetClock, 311: DisplayClock, 312: RemoveClock, 313: CreateWindow, 314: GotoWindow, 315: Hangup, 316: DialPhone, 317: QuickDial, 318: Tone, 319: Pulse, 320: FlashHook, 321: FixedPause, 322: WaitTone, 323: User0, 324: User1, 325: User2, 326: User3, 327: User4, 328: User5, 329: User6, 330: User7, 331: User8, 332: User9, 333: OrigPair, 334: OrigColors, 335: InitializeColor, 336: InitializePair, 337: SetColorPair, 338: SetForeground, 339: SetBackground, 340: ChangeCharPitch, 341: ChangeLinePitch, 342: ChangeResHorz, 343: ChangeResVert, 344: DefineChar, 345: EnterDoublewideMode, 346: EnterDraftQuality, 347: EnterItalicsMode, 348: EnterLeftwardMode, 349: EnterMicroMode, 350: EnterNearLetterQuality, 351: EnterNormalQuality, 352: EnterShadowMode, 353: EnterSubscriptMode, 354: EnterSuperscriptMode, 355: EnterUpwardMode, 356: ExitDoublewideMode, 357: ExitItalicsMode, 358: ExitLeftwardMode, 359: ExitMicroMode, 360: ExitShadowMode, 361: ExitSubscriptMode, 362: ExitSuperscriptMode, 363: ExitUpwardMode, 364: MicroColumnAddress, 365: MicroDown, 366: MicroLeft, 367: MicroRight, 368: MicroRowAddress, 369: MicroUp, 370: OrderOfPins, 371: ParmDownMicro, 372: ParmLeftMicro, 373: ParmRightMicro, 374: ParmUpMicro, 375: SelectCharSet, 376: SetBottomMargin, 377: SetBottomMarginParm, 378: SetLeftMarginParm, 379: SetRightMarginParm, 380: SetTopMargin, 381: SetTopMarginParm, 382: StartBitImage, 383: StartCharSetDef, 384: StopBitImage, 385: StopCharSetDef, 386: SubscriptCharacters, 387: SuperscriptCharacters, 388: TheseCauseCr, 389: ZeroMotion, 390: CharSetNames, 391: KeyMouse, 392: MouseInfo, 393: ReqMousePos, 394: GetMouse, 395: SetAForeground, 396: SetABackground, 397: PkeyPlab, 398: DeviceType, 399: CodeSetInit, 400: Set0DesSeq, 401: Set1DesSeq, 402: Set2DesSeq, 403: Set3DesSeq, 404: SetLrMargin, 405: SetTbMargin, 406: BitImageRepeat, 407: BitImageNewline, 408: BitImageCarriageReturn, 409: ColorNames, 410: DefineBitImageRegion, 411: EndBitImageRegion, 412: SetColorBand, 413: SetPageLength, 414: DisplayPcChar, 415: EnterPcCharsetMode, 416: ExitPcCharsetMode, 417: EnterScancodeMode, 418: ExitScancodeMode, 419: PcTermOptions, 420: ScancodeEscape, 421: AltScancodeEsc, 422: EnterHorizontalHlMode, 423: EnterLeftHlMode, 424: EnterLowHlMode, 425: EnterRightHlMode, 426: EnterTopHlMode, 427: EnterVerticalHlMode, 428: SetAAttributes, 429: SetPglenInch, // 393 430: Last 431: } 432: } 433: #endif
上述源程序定义了 TermInfoStrings 枚举,表示终端的字符串功能,用于以下两种不同的用途:
- 定义访问终端所需要的输出字符串。
- 定义当用户按下特定的按键时会接收的输入字符串,通常为功能键或是数字键盘上的特殊键。
在 Console.dll 项目中,TermInfoStrings 枚举在 TermInfoReader.cs 和 TermInfoDriver.cs 中被使用。
TermInfoReaders 类 (internal class)
下面就是 mcs/class/corlib/System/TermInfoReader.cs:
001: // 002: // System.TermInfoReader 003: // 004: // Authors: 005: // Gonzalo Paniagua Javier (gonzalo@ximian.com) 006: // 007: // (C) 2005 Novell, Inc (http://www.novell.com) 008: // 009: 010: // 011: // Permission is hereby granted, free of charge, to any person obtaining 012: // a copy of this software and associated documentation files (the 013: // "Software"), to deal in the Software without restriction, including 014: // without limitation the rights to use, copy, modify, merge, publish, 015: // distribute, sublicense, and/or sell copies of the Software, and to 016: // permit persons to whom the Software is furnished to do so, subject to 017: // the following conditions: 018: // 019: // The above copyright notice and this permission notice shall be 020: // included in all copies or substantial portions of the Software. 021: // 022: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 023: // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 024: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 025: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 026: // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 027: // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 028: // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 029: // 030: #if !NET_2_1 031: using System.IO; 032: using System.Text; 033: namespace System { 034: // This class reads data from a byte array or file containing the terminfo capabilities 035: // information for any given terminal. The maximum allowed size is 4096 bytes. 036: // 037: // Terminfo database files are divided in the following sections: 038: // 039: // Header 040: // Terminal names 041: // Boolean capabilities 042: // Numeric capabilities 043: // Offset of string capabilities 044: // String capabilities 045: // 046: // The header is as follows: 047: // 048: // Magic number (0x1 and 0x1A) 049: // Terminal names size 050: // Boolean section size 051: // Numeric section size 052: // String offsets section size 053: // Strings size 054: // 055: // After the header, there's a NUL terminated string containing terminal names separated 056: // by the character '|'. The last one is the terminal description. 057: // 058: // The boolean capabilities section has bytes that are set to 1 if the capability is supported 059: // and 0 otherwise. If the index of a capability is greater than the section size, 0 is assumed. 060: // 061: // The numeric capabilities section holds 2-byte integers in little endian format. No negative 062: // values are allowed and the absence of a capability is marked as two 0xFF. 063: // 064: // The string offsets section contains 2-byte integer offsets into the string capabilies section. 065: // If the capability is not supported, the index will be two 0xFF bytes. 066: // 067: // The string capabilities section contains NUL terminated strings starting at the offsets found 068: // in the previous section. 069: // 070: // Terminal capabilities indexes are found in /usr/include/term.h file and described in 071: // 'man 5 terminfo'. 072: // 073: 074: class TermInfoReader { 075: //short nameSize; 076: short boolSize; 077: short numSize; 078: short strOffsets; 079: //short strSize; 080: 081: //string [] names; // Last one is the description 082: byte [] buffer; 083: int booleansOffset; 084: //string term; 085: 086: public TermInfoReader (string term, string filename) 087: { 088: using (FileStream st = File.OpenRead (filename)) { 089: long length = st.Length; 090: if (length > 4096) 091: throw new Exception ("File must be smaller than 4K"); 092: 093: buffer = new byte [(int) length]; 094: if (st.Read (buffer, 0, buffer.Length) != buffer.Length) 095: throw new Exception ("Short read"); 096: 097: ReadHeader (buffer, ref booleansOffset); 098: ReadNames (buffer, ref booleansOffset); 099: } 100: 101: } 102: 103: public TermInfoReader (string term, byte [] buffer) 104: { 105: if (buffer == null) 106: throw new ArgumentNullException ("buffer"); 107: 108: this.buffer = buffer; 109: ReadHeader (buffer, ref booleansOffset); 110: ReadNames (buffer, ref booleansOffset); 111: } 112: 113: // public string Term { 114: // get { return term; } 115: // } 116: 117: void ReadHeader (byte [] buffer, ref int position) 118: { 119: short magic = GetInt16 (buffer, position); 120: position += 2; 121: if (magic != 282) 122: throw new Exception (String.Format ("Magic number is wrong: {0}", magic)); 123: 124: /*nameSize =*/ GetInt16 (buffer, position); 125: position += 2; 126: boolSize = GetInt16 (buffer, position); 127: position += 2; 128: numSize = GetInt16 (buffer, position); 129: position += 2; 130: strOffsets = GetInt16 (buffer, position); 131: position += 2; 132: /*strSize =*/ GetInt16 (buffer, position); 133: position += 2; 134: } 135: 136: void ReadNames (byte [] buffer, ref int position) 137: { 138: string prev = GetString (buffer, position); 139: position += prev.Length + 1; 140: //names = prev.Split ('|'); 141: } 142: 143: public bool Get (TermInfoBooleans boolean) 144: { 145: int x = (int) boolean; 146: if (x < 0 || boolean >= TermInfoBooleans.Last || x >= boolSize) 147: return false; 148: 149: int offset = booleansOffset; 150: offset += (int) boolean; 151: return (buffer [offset] != 0); 152: } 153: 154: public int Get (TermInfoNumbers number) 155: { 156: int x = (int) number; 157: if (x < 0 || number >= TermInfoNumbers.Last || x > numSize) 158: return -1; 159: 160: int offset = booleansOffset + boolSize; 161: if ((offset % 2) == 1) 162: offset++; 163: 164: offset += ((int) number) * 2; 165: return GetInt16 (buffer, offset); 166: } 167: 168: public string Get (TermInfoStrings tstr) 169: { 170: int x = (int) tstr; 171: if (x < 0 || tstr >= TermInfoStrings.Last || x > strOffsets) 172: return null; 173: 174: int offset = booleansOffset + boolSize; 175: if ((offset % 2) == 1) 176: offset++; 177: 178: offset += numSize * 2; 179: int off2 = GetInt16 (buffer, offset + (int) tstr * 2); 180: if (off2 == -1) 181: return null; 182: 183: return GetString (buffer, offset + strOffsets * 2 + off2); 184: } 185: 186: public byte [] GetStringBytes (TermInfoStrings tstr) 187: { 188: int x = (int) tstr; 189: if (x < 0 || tstr >= TermInfoStrings.Last || x > strOffsets) 190: return null; 191: 192: int offset = booleansOffset + boolSize; 193: if ((offset % 2) == 1) 194: offset++; 195: 196: offset += numSize * 2; 197: int off2 = GetInt16 (buffer, offset + (int) tstr * 2); 198: if (off2 == -1) 199: return null; 200: 201: return GetStringBytes (buffer, offset + strOffsets * 2 + off2); 202: } 203: 204: short GetInt16 (byte [] buffer, int offset) 205: { 206: int uno = (int) buffer [offset]; 207: int dos = (int) buffer [offset + 1]; 208: if (uno == 255 && dos == 255) 209: return -1; 210: 211: return (short) (uno + dos * 256); 212: } 213: 214: string GetString (byte [] buffer, int offset) 215: { 216: int length = 0; 217: int off = offset; 218: while (buffer [off++] != 0) 219: length++; 220: 221: return Encoding.ASCII.GetString (buffer, offset, length); 222: } 223: 224: byte [] GetStringBytes (byte [] buffer, int offset) 225: { 226: int length = 0; 227: int off = offset; 228: while (buffer [off++] != 0) 229: length++; 230: 231: byte [] result = new byte [length]; 232: Buffer.BlockCopyInternal (buffer, offset, result, 0, length); 233: return result; 234: } 235: 236: internal static string Escape (string s) 237: { 238: StringBuilder sb = new StringBuilder (); 239: for (int i = 0; i < s.Length; i++) { 240: char current = s [i]; 241: if (Char.IsControl (current)) { 242: sb.AppendFormat ("\\x{0:X2}", (int) current); 243: } else { 244: sb.Append (current); 245: } 246: } 247: 248: return sb.ToString (); 249: } 250: } 251: } 252: #endif
上述源程序定义了 TermInfoReader 类。这是一个 internal 类,只能在本程序集内使用。
第 34 行到第 72 行的注释详细解释了 TermInfoReader 类的功能以及 Terminfo 的数据结构,值得我们认真学习。
TermInfoReader 类有以下五个私有的实例字段:
- boolSize: short。(第 76 行)
- numSize: short。(第 77 行)
- strOffsets: short。(第 78 行)
- buffer: byte[]。(第 82 行)
- booleansOffset: int。(第 83 行)
第 86 行到第 101 行的公共构造函数用于从文件中读取 Terminfo 的情况。
第 103 行到第 111 行的公共构造函数用于从字节数组中读取 Terminfo 的情况。这个字节数组就是前面讲过的 KnownTerminals 类的三个公共静态只读属性之一。
上面的两个公共构造函数的第一个参数 term 应该表示终端的名称,但是在程序中并没有被使用。可能以前是有被使用的,这可以从被注释掉的第 84 行以及第 113 行到第 115 行看出来,以前要作为公共只读实例属性返回的。
第 117 行到第 134 行的私有实例方法 ReadHeader 用于读取 Terminfo 的头。
第 136 行到第 141 行的私有实例方法 ReadNames 用于读取终端的名字。
第 143 行到第 184 行的三个重载的公共实例方法 Get 分别用于获得前面所说的终端的布尔(TermInfoBooleans)、数字(TermInfoNumbers)和字符串(TermInfoStrings)功能。
第 186 行到第 202 行的公共实例方法方法 GetStringBytes 用于获得终端的以字节数组表示的字符串(TermInfoStrings)功能。
第 204 行到第 234 行的三个私有实例方法是内部使用的辅助方法。
第 236 行到第 249 行的 internal 静态方法 Escape 用于对字符串中的特殊字符进行转义处理。
在 Console.dll 项目中,TermInfoReader 在 TermInfoDerver.cs 中被使用。
(未完待续)