imcplugin demo: Extend to support stat() call
[nativeclient.git] / imcplugin / hello.html
blobc6de9a113c90262ad994cd2aff9016c6e0f4c55c
2 <object id="plugin" type="application/x-nacl-imc">Plugin not working</object>
4 <script type="text/javascript" src="filelist.js"></script>
6 <script type="text/javascript">
7 var prefix_dir = "../install-stubout";
8 var args = ["--library-path", "/lib", "/usr/local/bin/python", "demo.py"];
9 var plugin;
10 var proc;
11 var input_field;
12 var output;
14 var S_IFDIR = 0x4000;
16 function receive(message) {
17 var header = message.substring(0, 4);
18 if(header == "Open") {
19 open(normalize_path(message.substring(4)));
21 else if(header == "Stat") {
22 stat(normalize_path(message.substring(4)));
24 else if(header == "Prnt") {
25 print(message.substring(4), "normalOutput");
26 refocus();
28 else {
29 dump("unrecognised message: " + message + "\n");
30 proc.send("Reply to unrecognised message", []);
34 function normalize_path(pathname) {
35 while(pathname[pathname.length-1] == "/") {
36 pathname = pathname.substring(0, pathname.length-1);
38 // Python tries to open "./nacl.so" as well as "nacl.so"
39 while(pathname.substring(0, 2) == "./") {
40 pathname = pathname.substring(2)
42 return pathname;
45 /* imcplugin currently does not respond if we ask for a file that doesn't
46 exist. As a temporary workaround, we whitelist allowed files. */
48 function open(filename) {
49 dump("open: " + filename + "\n");
50 if(!files.hasOwnProperty(filename)) {
51 proc.send("", []);
53 else {
54 plugin.get_file(files[filename], function(file) {
55 proc.send("", [file]);
56 });
60 function stat(filename) {
61 dump("stat: " + filename + "\n");
62 if(dirs.hasOwnProperty(filename)) {
63 var fields = [0,0,S_IFDIR,0,0,0,0,0,0,0,0,0,0,0,0,0];
64 proc.send("Rsta" + encode_stat(fields), []);
66 else if(files.hasOwnProperty(filename)) {
67 var fields = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
68 proc.send("Rsta" + encode_stat(fields), []);
70 else {
71 proc.send("Fail", []);
75 function encode_stat(fields) {
76 out = "";
77 for(var i = 0; i < fields.length; i++) {
78 var val = fields[i];
79 out += String.fromCharCode(val & 0xff);
80 out += String.fromCharCode((val >> 8) & 0xff);
81 out += String.fromCharCode((val >> 16) & 0xff);
82 out += String.fromCharCode((val >> 24) & 0xff);
84 return out;
87 function onload() {
88 try {
89 plugin = document.getElementById("plugin");
90 plugin.get_file(prefix_dir + "/lib/ld-linux.so.2", function(file) {
91 proc = plugin.launch(file, args, receive);
92 });
94 catch(e) {
95 dump(e + "\n");
98 input_field = document.getElementById("input");
99 output = document.getElementById("output");
100 refocus();
102 window.onload = onload;
104 /* This REPL is based loosely on the Javascript shell at
105 http://www.squarefree.com/shell/
106 The JavaScript shell was created by Jesse Ruderman.
107 The JavaScript Shell is GPL/LGPL/MPL tri-licensed. */
109 function input_keydown(event) {
110 if(event.keyCode == 13 && !event.shiftKey) {
111 var data = input_field.value;
112 if(data != "") {
113 proc.send("Eval" + data, []);
114 print(data, "input");
116 setTimeout(function() { input_field.value = ""; }, 0);
118 refocus();
121 function refocus() {
122 input_field.blur();
123 input_field.focus();
126 function print(text, style) {
127 var newdiv = document.createElement("div");
128 newdiv.appendChild(document.createTextNode(text));
129 newdiv.className = style;
130 output.appendChild(newdiv);
132 </script>
134 <style type="text/css">
135 body { background: white; color: black; }
137 #output { white-space: pre; white-space: -moz-pre-wrap; } /* Preserve line breaks, but wrap too if browser supports it */
138 h3 { margin-top: 0; margin-bottom: 0em; }
139 h3 + div { margin: 0; }
141 form { margin: 0; padding: 0; }
142 #input { width: 100%; border: none; padding: 0; overflow: auto; }
144 .input { color: blue; background: white; font: inherit; font-weight: bold; margin-top: .5em; }
145 .normalOutput { color: black; background: white; }
146 </style>
148 <div id="output"></div>
149 <div><textarea id="input" class="input" wrap="off" onkeydown="input_keydown(event)" rows="1"></textarea></div>