3 <TITLE>termlib Sample Parser</TITLE>
\r
4 <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="termlib.js"></SCRIPT>
\r
5 <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="termlib_parser.js"></SCRIPT>
\r
7 <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
\r
11 test sample for termlib.js and termlib_parser.js
\r
13 (c) Norbert Landsteiner 2005
\r
14 mass:werk - media environments
\r
15 <http://www.masswerk.at>
\r
22 '%CS%+r Terminal Help %-r%n',
\r
23 ' This is just a sample to demonstrate command line parsing.',
\r
25 ' Use one of the following commands:',
\r
26 ' clear [-a] .......... clear the terminal',
\r
27 ' option "a" also removes the status line',
\r
28 ' number -n<value> .... return value of option "n" (test for options)',
\r
29 ' repeat -n<value> .... repeats the first argument n times (another test)',
\r
30 ' login <username> .... sample login (test for raw mode)',
\r
31 ' exit ................ close the terminal (same as <ESC>)',
\r
32 ' help ................ show this help page',
\r
34 ' other input will be echoed to the terminal as a list of parsed arguments',
\r
35 ' in the format <argument index> <quoting level> "<parsed value>".',
\r
39 function termOpen() {
\r
47 initHandler: termInitHandler,
\r
48 handler: commandHandler
\r
51 if (term) term.open();
\r
53 else if (term.closed) {
\r
61 function termInitHandler() {
\r
62 // output a start up screen
\r
65 TermGlobals.center('####################################################', 80),
\r
66 TermGlobals.center('# #', 80),
\r
67 TermGlobals.center('# termlib.js - Sample Parser #', 80),
\r
68 TermGlobals.center('# Input is echoed as a list of parsed arguments. #', 80),
\r
69 TermGlobals.center('# #', 80),
\r
70 TermGlobals.center('# Type "help" for commands. #', 80),
\r
71 TermGlobals.center('# #', 80),
\r
72 TermGlobals.center('# (c) N. Landsteiner 2005; www.masswerk.at #', 80),
\r
73 TermGlobals.center('# #', 80),
\r
74 TermGlobals.center('####################################################', 80),
\r
78 // set a double status line
\r
79 this.statusLine('', 8,2); // just a line of strike
\r
80 this.statusLine(' +++ This is just a test sample for command parsing. Type "help" for help. +++');
\r
82 // and leave with prompt
\r
86 function commandHandler() {
\r
88 // check for raw mode first (should not be parsed)
\r
90 if (this.env.getPassword) {
\r
91 // sample password handler (lineBuffer == stored username ?)
\r
92 if (this.lineBuffer == this.env.username) {
\r
93 this.user = this.env.username;
\r
94 this.ps = '['+this.user+']>';
\r
97 this.type('Sorry.');
\r
99 this.env.username = '';
\r
100 this.env.getPassword = false;
\r
102 // leave in normal mode
\r
103 this.rawMode = false;
\r
107 // normal command parsing
\r
108 // just call the termlib_parser with a reference of the calling Terminal instance
\r
109 // parsed arguments will be imported in this.argv,
\r
110 // quoting levels per argument in this.argQL (quoting character or empty)
\r
111 // cursor for arguments is this.argc (used by parserGetopt)
\r
112 // => see 'termlib_parse.js' for configuration and details
\r
114 if (this.argv.length == 0) {
\r
115 // no commmand line input
\r
117 else if (this.argQL[0]) {
\r
118 // first argument quoted -> error
\r
119 this.write("Syntax error: first argument quoted.");
\r
122 var cmd = this.argv[this.argc++];
\r
124 process commands now
\r
125 1st argument: this.argv[this.argc]
\r
127 if (cmd == 'help') {
\r
128 this.write(helpPage);
\r
130 else if (cmd == 'clear') {
\r
132 var opts = parserGetopt(this, 'aA');
\r
134 // discard status line on opt "a" or "A"
\r
135 this.maxLines = this.conf.rows;
\r
139 else if (cmd == 'number') {
\r
140 // test for value options
\r
141 var opts = parserGetopt(this, 'n');
\r
142 if (opts.illegals.length) this.type('illegal option. usage: number -n<value>')
\r
143 else if ((opts.n) && (opts.n.value != -1)) this.type('option value: '+opts.n.value)
\r
144 else this.type('usage: number -n<value>');
\r
146 else if (cmd == 'repeat') {
\r
147 // another test for value options
\r
148 var opts = parserGetopt(this, 'n');
\r
149 if (opts.illegals.length) this.type('illegal option. usage: repeat -n<value> <string>')
\r
150 else if ((opts.n) && (opts.n.value != -1)) {
\r
151 // first normal argument is again this.argv[this.argc]
\r
152 var s = this.argv[this.argc];
\r
153 if (typeof s != 'undefined') {
\r
154 // repeat this string n times
\r
156 for (var i=0; i<opts.n.value; i++) a[a.length] = s;
\r
157 this.type(a.join(' '));
\r
160 else this.type('usage: repeat -n<value> <string>');
\r
162 else if (cmd == 'login') {
\r
163 // sample login (test for raw mode)
\r
164 if ((this.argc == this.argv.length) || (this.argv[this.argc] == '')) {
\r
165 this.type('usage: login <username>');
\r
168 this.env.getPassword = true;
\r
169 this.env.username = this.argv[this.argc];
\r
170 this.write('%+iSample login: repeat username as password.%-i%n');
\r
171 this.type('password: ');
\r
172 // exit in raw mode (blind input)
\r
173 this.rawMode = true;
\r
178 else if (cmd == 'exit') {
\r
183 // for test purpose just output argv as list
\r
184 // assemble a string of style-escaped lines and output it in more-mode
\r
185 s=' INDEX QL ARGUMENT%n';
\r
186 for (var i=0; i<this.argv.length; i++) {
\r
187 s += TermGlobals.stringReplace('%', '%%',
\r
188 TermGlobals.fillLeft(i, 6) +
\r
189 TermGlobals.fillLeft((this.argQL[i])? this.argQL[i]:'-', 4) +
\r
190 ' "' + this.argv[i] + '"'
\r
204 <STYLE TYPE="text/css">
\r
206 font-family: courier,fixed,swiss,sans-serif;
\r
214 font-family: courier,fixed,swiss,sans-serif;
\r
221 background: #33d011;
\r
223 a,a:link,a:visited {
\r
224 text-decoration: none;
\r
228 text-decoration: underline;
\r
232 text-decoration: underline;
\r
236 a.termopen,a.termopen:link,a.termopen:visited {
\r
237 text-decoration: none;
\r
242 text-decoration: none;
\r
244 background: #77dd11;
\r
246 a.termopen:active {
\r
247 text-decoration: none;
\r
249 background: #dddddd;
\r
256 <BODY BGCOLOR="#222222" LINK="#77dd11" TEXT="#cccccc" ALINK="#dddddd" VLINK="#77dd11"
\r
257 TOPMARGIN="0" BOTTOMMARGIN="0" LEFTMARGIN="0" RIGHTMARGIN="0" MARGINHEIGHT="0" MARGINWIDTH="0">
\r
259 <TABLE BORDER="0" CELLSPACING="20" CELLPADDING="0" ALIGN="center">
\r
261 <TD NOWRAP><A HREF="index.html">termlib.js home</A></TD>
\r
263 <TD NOWRAP><A HREF="multiterm_test.html">multiple terminal test</A></TD>
\r
265 <TD NOWRAP>sample parser</TD>
\r
267 <TD NOWRAP><A HREF="faq.html">faq</A></TD>
\r
269 <TD NOWRAP><A HREF="readme.txt" TITLE="readme.txt (text/plain)">documentation</A></TD>
\r
273 <TABLE BORDER="0" CELLSPACING="20" CELLPADDING="0">
\r
275 Sample Parser Test<BR>
\r
278 <A HREF="javascript:termOpen()" onfocus="if(this.blur)this.blur();" onmouseover="window.status='terminal 1'; return true" onmouseout="window.status=''; return true" CLASS="termopen">> open terminal </A>
\r
283 <TR><TD NOWRAP CLASS="lh15">
\r
285 (c) mass:werk,<BR>N. Landsteiner 2003-2005<BR>
\r
286 <A HREF="http://www.masswerk.at/" TARGET="_blank">http://www.masswerk.at</A>
\r
290 <DIV ID="termDiv" STYLE="position:absolute;"></DIV>
\r