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