]> gitweb.factorcode.org Git - factor.git/blob - extra/webapps/fjsc/www/termlib/parser_sample.html
core: Add the shuffler words but without primitives.
[factor.git] / extra / webapps / fjsc / www / termlib / parser_sample.html
1 <HTML>
2 <HEAD>
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>
6
7 <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
8 <!--
9
10 /*
11   test sample for termlib.js and termlib_parser.js
12
13   (c) Norbert Landsteiner 2005
14   mass:werk - media environments
15   <http://www.masswerk.at>
16
17 */
18
19 var term;
20
21 var helpPage=[
22         '%CS%+r Terminal Help %-r%n',
23         '  This is just a sample to demonstrate command line parsing.',
24         ' ',
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',
33         ' ',
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>".',
36         ' '
37 ];
38
39 function termOpen() {
40         if (!term) {
41                 term=new Terminal(
42                         {
43                                 x: 220,
44                                 y: 70,
45                                 termDiv: 'termDiv',
46                                 ps: '[guest]$',
47                                 initHandler: termInitHandler,
48                                 handler: commandHandler
49                         }
50                 );
51                 if (term) term.open();
52         }
53         else if (term.closed) {
54                 term.open();
55         }
56         else {
57                 term.focus();
58         }
59 }
60
61 function termInitHandler() {
62         // output a start up screen
63         this.write(
64                 [
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),
75                         '%n'
76                 ]
77         );
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. +++');
81         this.maxLines -= 2;
82         // and leave with prompt
83         this.prompt();
84 }
85
86 function commandHandler() {
87         this.newLine();
88         // check for raw mode first (should not be parsed)
89         if (this.rawMode) {
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+']>';
95                         }
96                         else {
97                                 this.type('Sorry.');
98                         }
99                         this.env.username = '';
100                         this.env.getPassword = false;
101                 }
102                 // leave in normal mode
103                 this.rawMode = false;
104                 this.prompt();
105                 return;
106         }
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
113         parseLine(this);
114         if (this.argv.length == 0) {
115                 // no commmand line input
116         }
117         else if (this.argQL[0]) {
118             // first argument quoted -> error
119                 this.write("Syntax error: first argument quoted.");
120         }
121         else {
122                 var cmd = this.argv[this.argc++];
123                 /*
124                   process commands now
125                   1st argument: this.argv[this.argc]
126                 */
127                 if (cmd == 'help') {
128                         this.write(helpPage);
129                 }
130                 else if (cmd == 'clear') {
131                         // get options
132                         var opts = parserGetopt(this, 'aA');
133                         if (opts.a) {
134                                 // discard status line on opt "a" or "A"
135                                 this.maxLines = this.conf.rows;
136                         }
137                         this.clear();
138                 }
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>');
145                 }
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
155                                         var a = [];
156                                         for (var i=0; i<opts.n.value; i++) a[a.length] = s;
157                                         this.type(a.join(' '));
158                                 }
159                         }
160                         else this.type('usage: repeat -n<value> <string>');
161                 }
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>');
166                         }
167                         else {
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)
173                                 this.rawMode = true;
174                                 this.lock = false;
175                                 return;
176                         }
177                 }
178                 else if (cmd == 'exit') {
179                         this.close();
180                         return;
181                 }
182                 else {
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] + '"'
191                                         ) + '%n';
192                         }
193                         this.write(s, 1);
194                         return;
195                 }
196         }
197         this.prompt();
198 }
199
200
201 //-->
202 </SCRIPT>
203
204 <STYLE TYPE="text/css">
205 body,p,a,td {
206         font-family: courier,fixed,swiss,sans-serif;
207         font-size: 12px;
208         color: #cccccc;
209 }
210 .lh15 {
211         line-height: 15px;
212 }
213 .term {
214         font-family: courier,fixed,swiss,sans-serif;
215         font-size: 12px;
216         color: #33d011;
217         background: none;
218 }
219 .termReverse {
220         color: #111111;
221         background: #33d011;
222 }
223 a,a:link,a:visited {
224         text-decoration: none;
225         color: #77dd11;
226 }
227 a:hover {
228         text-decoration: underline;
229         color: #77dd11;
230 }
231 a:active {
232         text-decoration: underline;
233         color: #dddddd;
234 }
235
236 a.termopen,a.termopen:link,a.termopen:visited {
237         text-decoration: none;
238         color: #77dd11;
239         background: none;
240 }
241 a.termopen:hover {
242         text-decoration: none;
243         color: #222222;
244         background: #77dd11;
245 }
246 a.termopen:active {
247         text-decoration: none;
248         color: #222222;
249         background: #dddddd;
250 }
251
252 </STYLE>
253 </HEAD>
254
255
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">
258
259 <TABLE BORDER="0" CELLSPACING="20" CELLPADDING="0" ALIGN="center">
260 <TR>
261         <TD NOWRAP><A HREF="index.html">termlib.js home</A></TD>
262         <TD>|</TD>
263         <TD NOWRAP><A HREF="multiterm_test.html">multiple terminal test</A></TD>
264         <TD>|</TD>
265         <TD NOWRAP>sample parser</TD>
266         <TD>|</TD>
267         <TD NOWRAP><A HREF="faq.html">faq</A></TD>
268         <TD>|</TD>
269         <TD NOWRAP><A HREF="readme.txt" TITLE="readme.txt (text/plain)">documentation</A></TD>
270 </TR>
271 </TABLE>
272
273 <TABLE BORDER="0" CELLSPACING="20" CELLPADDING="0">
274         <TR><TD NOWRAP>
275                 Sample Parser Test<BR>&nbsp;
276         </TD></TR>
277         <TR><TD NOWRAP>
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">&gt; open terminal &nbsp;</A>
279         </TD></TR>
280         <TR><TD NOWRAP>
281                 &nbsp;
282         </TD></TR>
283         <TR><TD NOWRAP CLASS="lh15">
284                 &nbsp;<BR>
285                 (c) mass:werk,<BR>N. Landsteiner 2003-2005<BR>
286                 <A HREF="http://www.masswerk.at/" TARGET="_blank">http://www.masswerk.at</A>
287         </TD></TR>
288 </TABLE>
289
290 <DIV ID="termDiv" STYLE="position:absolute;"></DIV>
291
292 </BODY>
293 </HTML>