-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathw3270.html
427 lines (414 loc) · 14 KB
/
w3270.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<html>
<head>
<meta charset="utf-8">
<style>
html, body {
width: 100%;
height: 100%;
}
body {
margin: 0px;
background-color: black;
}
div.terminal {
white-space: pre;
font-family: monospace;
font-size: 12pt;
color: red;
cursor: default;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
function executeCommand(command, callback) {
var request = JSON.stringify({command: command + '\n'});
$.ajax({
type: 'POST',
url: '/',
dataType: 'json',
data: request,
success: callback
});
}
function moveCursor(row, column) {
var rows = document.querySelectorAll('div.terminal div');
if (this.cursorRow != undefined && this.cursorColumn != undefined) {
var columns = rows[this.cursorRow].querySelectorAll('span');
var cell = columns[this.cursorColumn];
var foreground = cell.style.color;
var background = cell.style.backgroundColor;
cell.style.color = background;
cell.style.backgroundColor = foreground;
if (cell.dataset.nondisplay) {
cell.style.visibility = 'hidden';
}
}
if (row < 0) {
row = rows.length + row % rows.length;
} else if (row >= rows.length) {
row = row % rows.length;
}
var columns = rows[row].querySelectorAll('span');
if (column < 0) {
column = columns.length + column % columns.length;
} else if (column >= columns.length) {
column = column % columns.length;
}
var cell = columns[column];
var foreground = cell.style.color;
var background = cell.style.backgroundColor;
if (!background) {
background = 'black';
}
cell.style.color = background;
cell.style.backgroundColor = foreground;
if (cell.dataset.nondisplay) {
cell.style.visibility = '';
}
this.cursorRow = row;
this.cursorColumn = column;
}
function setCharacter(row, column, character) {
var rows = document.querySelectorAll('div.terminal div');
var columns = rows[row].querySelectorAll('span');
var cell = columns[column];
if (cell.dataset.protected) {
return false;
}
cell.innerText = character;
return true;
}
$(window).keypress(function(event) {
if (event.which === 0) {
return;
}
var character = String.fromCharCode(event.which);
if (!character.match(/[ a-zA-Z0-9`~!@#$%^&*()_+\-=[\]{};':",.\/<>?\\|]/)) {
return;
}
var row = this.cursorRow;
var column = this.cursorColumn;
if (!setCharacter(this.cursorRow, this.cursorColumn, character)) {
return;
}
column++;
moveCursor(row, column);
executeCommand('String("' + character + '")');
});
$(window).keydown(function(event) {
var row = this.cursorRow;
var column = this.cursorColumn;
switch (event.keyCode) {
case 8: // backspace
event.preventDefault();
var rows = document.body.querySelectorAll('div.terminal div');
var columns = rows[row].querySelectorAll('span');
if (columns[column - 1].dataset.fieldStart) {
break;
}
column--;
moveCursor(row, column);
setCharacter(this.cursorRow, this.cursorColumn, ' ');
executeCommand('BackSpace()', function() {
executeCommand('Delete()');
});
break;
case 9: // tab
event.preventDefault();
executeCommand('Tab()', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 13: // enter
executeCommand('Enter()', function() {
executeCommand('Wait(1000, InputField)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
});
break;
case 37: // left
column--;
moveCursor(row, column);
executeCommand('Left()');
break;
case 38: // up
row--;
moveCursor(row, column);
executeCommand('Up()');
break;
case 39: // right
column++;
moveCursor(row, column);
executeCommand('Right()');
break;
case 40: // down
row++;
moveCursor(row, column);
executeCommand('Down()');
break;
case 46: // delete
event.preventDefault();
executeCommand('Delete()', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 112: // f1
event.preventDefault();
executeCommand('PF(1)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 113: // f2
event.preventDefault();
executeCommand('PF(2)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 114: // f3
event.preventDefault();
executeCommand('PF(3)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 115: // f4
event.preventDefault();
executeCommand('PF(4)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 116: // f5
event.preventDefault();
executeCommand('PF(5)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 117: // f6
event.preventDefault();
executeCommand('PF(6)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 118: // f7
event.preventDefault();
executeCommand('PF(7)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 119: // f8
event.preventDefault();
executeCommand('PF(8)', function(response) {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 120: // f9
event.preventDefault();
executeCommand('PF(9)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 121: // f10
event.preventDefault();
executeCommand('PF(10)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 122: // f11
event.preventDefault();
executeCommand('PF(11)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
case 123: // f12
event.preventDefault();
executeCommand('PF(12)', function() {
executeCommand('ReadBuffer()', processBuffer);
});
break;
}
});
function unicode(array) {
var result = '';
var index = 0;
var length = array.length;
while (index < length) {
var byte = array[index++];
switch (byte >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
result += String.fromCharCode(array[index - 1]);
break;
case 12: case 13:
var left = array[index++];
result += String.fromCharCode(((byte & 0x1f) << 6) | (left & 0x3f));
break;
case 14:
var left = array[index++];
var right = array[index++];
result += String.fromCharCode(((byte & 0x0f) << 12) | ((left & 0x3f) << 6) | ((right & 0x3f) << 0));
break;
}
}
return result;
}
function processBuffer(response) {
var status = response.status.split(' ');
var row = parseInt(status[8]);
var column = parseInt(status[9]);
var terminalBlock = document.createElement('div');
terminalBlock.className = 'terminal';
var field = {};
for (var lineIndex = 0; lineIndex < response.data.length; lineIndex++) {
var line = response.data[lineIndex];
var lineBlock = document.createElement('div');
var array = line.split(' ');
for (var dataIndex = 0; dataIndex < array.length; dataIndex++) {
var data = array[dataIndex];
var character = undefined;
var attributes = undefined;
if (data.length == 2) {
var code = parseInt(data, 16);
character = code ? String.fromCharCode(parseInt(data, 16)) : ' ';
} else {
character = ' ';
var match = data.match(/S([FA])\((.*?)\)/);
if (match) {
attributes = {};
if (match[1] == 'F') {
field = {};
} else if (match[1] == 'A') {
character = undefined;
}
attributes.type = match[1];
match[2].split(',').forEach(function(attribute) {
var pair = attribute.split('=');
attributes[parseInt(pair[0], 16)] = parseInt(pair[1], 16);
});
if (0xc0 in attributes) {
field.protected = Boolean(attributes[0xc0] & 0x20);
field.bright = Boolean(attributes[0xc0] & 0x08);
field.nondisplay = Boolean((attributes[0xc0] & 0x0c) == 0x0c);
}
if (0x41 in attributes) {
if (attributes[0x41] == 0xf0) {
character = undefined;
}
field.reverse = attributes[0x41] == 0xf2;
field.underscore = attributes[0x41] == 0xf4;
}
if (0x42 in attributes) {
var color = undefined;
switch (attributes[0x42]) {
case 0xf0: color = 'deepskyblue'; break;
case 0xf1: color = 'dodgerblue '; break;
case 0xf2: color = 'red'; break;
case 0xf3: color = 'white'; break;
case 0xf4: color = 'lime'; break;
case 0xf5: color = 'turquoise'; break;
case 0xf6: color = 'yellow'; break;
case 0xf7: color = 'white'; break;
case 0xf8: color = 'black'; break;
case 0xf9: color = 'white'; break;
case 0xfa: color = 'white'; break;
case 0xfb: color = 'white'; break;
case 0xfc: color = 'white'; break;
case 0xfd: color = 'white'; break;
case 0xfe: color = 'white'; break;
case 0xff: color = 'white'; break;
}
field.color = color;
}
} else {
var code = [];
for (var index = 0; index < data.length; index += 2) {
code.push(parseInt(data.substr(index, 2), 16));
}
character = unicode(code);
}
}
if (character) {
var span = document.createElement('span');
span.innerHTML = character;
if (!field.color) {
if (field.protected === false) {
if (field.bright === false) {
field.color = 'lime';
} else if (field.bright === true) {
field.color = 'red';
}
} else if (field.protected === true) {
if (field.bright === false) {
field.color = 'turquoise';
} else if (field.bright === true) {
field.color = 'white';
}
}
}
if (!field.color) {
field.color = 'red';
}
if (!attributes && field.reverse) {
span.style.color = 'black';
span.style.backgroundColor = field.color;
} else {
span.style.color = field.color;
}
if (field.protected) {
span.dataset.protected = true;
}
if (field.nondisplay) {
span.dataset.nondisplay = true;
span.style.visibility = 'hidden';
}
if (!attributes && field.underscore) {
span.style.boxShadow = 'inset 0 -1px 0 0';
span.style.mozBoxShadow = span.style.boxShadow;
span.style.webkitBoxShadow = span.style.boxShadow;
}
if (attributes && attributes.type == 'F') {
span.dataset.fieldStart = true;
}
lineBlock.appendChild(span);
}
}
terminalBlock.appendChild(lineBlock);
}
terminalBlock.onclick = function(event) {
var cell = event.target;
var column = 0;
var node = cell;
while (node = node.previousSibling) {
column++;
}
var row = 0;
node = cell.parentNode;
while (node = node.previousSibling) {
row++;
}
moveCursor(row, column);
executeCommand('MoveCursor(' + row + ',' + column + ')');
};
if (!document.body.childNodes.length) {
document.body.appendChild(terminalBlock);
} else {
document.body.replaceChild(terminalBlock, document.body.childNodes[0]);
}
cursorRow = undefined;
cursorColumn = undefined;
moveCursor(row, column);
}
$(document).ready(function() {
executeCommand('', function(response) {
if (response.status.split(' ')[3] == 'N') {
var hostname = prompt('Host name');
executeCommand('Connect("' + hostname + '")', function(response) {
executeCommand('ReadBuffer()', processBuffer);
});
} else {
executeCommand('ReadBuffer()', processBuffer);
}
});
});
</script>
</head>
<body>
</body>
</html>