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