');
- } else {
- // more lines, cursor in the middle
- var line_index;
- var current;
- pos = position;
- for (i=0; i current_len) {
- pos -= current_len;
- } else {
- break;
- }
- }
- current = array[i];
- line_index = i;
- // cursor on first character in line
- if (pos === current.length) {
- pos = 0;
- current = array[++line_index];
- }
- draw_cursor_line(current, pos);
- lines_before(array.slice(0, line_index));
- lines_after(array.slice(line_index+1));
- }
- }
- }
- }
- } else {
- if (string === '') {
- before.html('');
- cursor.html(' ');
- after.html('');
- } else {
- draw_cursor_line(string, position);
- }
- }
- };
- })(self);
- var last_command;
- // -----------------------------------------------------------------------
- // :: Draw prompt that can be a function or a string
- // -----------------------------------------------------------------------
- var draw_prompt = (function() {
- var prompt_node = self.find('.prompt');
- function set(prompt) {
- prompt_len = skipFormattingCount(prompt);
- prompt_node.html($.terminal.format($.terminal.encode(prompt)));
- }
- return function() {
- switch (typeof prompt) {
- case 'string':
- set(prompt);
- break;
- case 'function':
- prompt(set);
- break;
- }
- };
- })();
- // -----------------------------------------------------------------------
- // :: Paste content to terminal using hidden textarea
- // -----------------------------------------------------------------------
- function paste() {
- clip.focus();
- //wait until Browser insert text to textarea
- self.oneTime(1, function() {
- self.insert(clip.val());
- clip.blur().val('');
- });
- }
- var first_up_history = true;
- //var prevent_keypress = false;
- // -----------------------------------------------------------------------
- // :: Keydown Event Handler
- // -----------------------------------------------------------------------
- function keydown_event(e) {
- var result, pos, len;
- if (typeof options.keydown == 'function') {
- result = options.keydown(e);
- if (result !== undefined) {
- //prevent_keypress = true;
- return result;
- }
- }
- if (enabled) {
- if (e.which !== 38 &&
- !(e.which === 80 && e.ctrlKey)) {
- first_up_history = true;
- }
- // arrows / Home / End / ENTER
- if (reverse_search && (e.which === 35 || e.which === 36 ||
- e.which === 37 || e.which === 38 ||
- e.which === 39 || e.which === 40 ||
- e.which === 13 || e.which === 27)) {
- clear_reverse_state();
- draw_prompt();
- if (e.which === 27) { // ESC
- command = '';
- }
- redraw();
- // finish reverse search and execute normal event handler
- keydown_event.call(this, e);
- } else if (e.altKey) {
- // Chrome on Windows sets ctrlKey and altKey for alt
- // need to check for alt first
- //if (e.which === 18) { // press ALT
- if (e.which === 68) { //ALT+D
- self.set(command.slice(0, position) +
- command.slice(position).replace(/[^ ]+ |[^ ]+$/, ''),
- true);
- // chrome jump to address bar
- return false;
- }
- return true;
- } else if (e.keyCode === 13) { //enter
- if (e.shiftKey) {
- self.insert('\n');
- } else {
- if (history && command && !mask &&
- ((typeof options.historyFilter == 'function' &&
- options.historyFilter(command)) ||
- !options.historyFilter)) {
- history.append(command);
- }
- var tmp = command;
- history.reset();
- self.set('');
- if (options.commands) {
- options.commands(tmp);
- }
- if (typeof prompt === 'function') {
- draw_prompt();
- }
- }
- } else if (e.which === 8) { //backspace
- if (reverse_search) {
- reverse_search_string = reverse_search_string.slice(0, -1);
- draw_reverse_prompt();
- } else {
- if (command !== '' && position > 0) {
- command = command.slice(0, position - 1) +
- command.slice(position, command.length);
- --position;
- redraw();
- }
- }
- } else if (e.which === 67 && e.ctrlKey && e.shiftKey) { // CTRL+SHIFT+C
- selected_text = getSelectedText();
- } else if (e.which === 86 && e.ctrlKey && e.shiftKey) {
- if (selected_text !== '') {
- self.insert(selected_text);
- }
- } else if (e.which === 9 && !(e.ctrlKey || e.altKey)) { // TAB
- self.insert('\t');
- } else if (e.which === 46) {
- //DELETE
- if (command !== '' && position < command.length) {
- command = command.slice(0, position) +
- command.slice(position + 1, command.length);
- redraw();
- }
- return true;
- } else if (history && e.which === 38 ||
- (e.which === 80 && e.ctrlKey)) {
- //UP ARROW or CTRL+P
- if (first_up_history) {
- last_command = command;
- self.set(history.current());
- } else {
- self.set(history.previous());
- }
- first_up_history = false;
- } else if (history && e.which === 40 ||
- (e.which === 78 && e.ctrlKey)) {
- //DOWN ARROW or CTRL+N
- self.set(history.end() ? last_command : history.next());
- } else if (e.which === 37 ||
- (e.which === 66 && e.ctrlKey)) {
- //CTRL+LEFT ARROW or CTRL+B
- if (e.ctrlKey && e.which !== 66) {
- len = position - 1;
- pos = 0;
- if (command[len] === ' ') {
- --len;
- }
- for (var i = len; i > 0; --i) {
- if (command[i] === ' ' && command[i+1] !== ' ') {
- pos = i + 1;
- break;
- } else if (command[i] === '\n' && command[i+1] !== '\n') {
- pos = i;
- break;
- }
- }
- self.position(pos);
- } else {
- //LEFT ARROW or CTRL+B
- if (position > 0) {
- --position;
- redraw();
- }
- }
- } else if (e.which === 82 && e.ctrlKey) { // CTRL+R
- if (reverse_search) {
- reverse_history_search(true);
- } else {
- backup_prompt = prompt;
- draw_reverse_prompt();
- last_command = command;
- command = '';
- redraw();
- reverse_search = true;
- }
- } else if (e.which == 71 && e.ctrlKey) { // CTRL+G
- if (reverse_search) {
- prompt = backup_prompt;
- draw_prompt();
- command = last_command;
- redraw();
- reverse_search = false;
- reverse_search_string = '';
- }
- } else if (e.which === 39 ||
- (e.which === 70 && e.ctrlKey)) {
- //RIGHT ARROW OR CTRL+F
- if (e.ctrlKey && e.which !== 70) {
- // jump to beginning or end of the word
- if (command[position] === ' ') {
- ++position;
- }
- var re = /\S[\n\s]{2,}|[\n\s]+\S?/;
- var match = command.slice(position).match(re);
- if (!match || match[0].match(/^\s+$/)) {
- position = command.length;
- } else {
- if (match[0][0] !== ' ') {
- position += match.index + 1;
- } else {
- position += match.index + match[0].length - 1;
- if (match[0][match[0].length-1] !== ' ') {
- --position;
- }
- }
- }
- redraw();
- } else {
- if (position < command.length) {
- ++position;
- redraw();
- }
- }
- } else if (e.which === 123) { //F12 - Allow Firebug
- return true;
- } else if (e.which === 36) { //HOME
- self.position(0);
- } else if (e.which === 35) {
- //END
- self.position(command.length);
- } else if (e.shiftKey && e.which == 45) { // Shift+Insert
- paste();
- return true;
- } else if (e.ctrlKey || e.metaKey) {
- if (e.which === 192) { // CMD+` switch browser window on Mac
- return true;
- }
- if (e.metaKey) {
- if(e.which === 82) { // CMD+r page reload in Chrome Mac
- return true;
- } else if(e.which === 76) {
- return true; // CMD+l jump into Omnibox on Chrome Mac
- }
- }
- if (e.shiftKey) { // CTRL+SHIFT+??
- if (e.which === 84) {
- //CTRL+SHIFT+T open closed tab
- return true;
- }
- //} else if (e.altKey) { //ALT+CTRL+??
- } else {
- if (e.which === 81) { // CTRL+W
- // don't work in Chromium (can't prevent close tab)
- if (command !== '' && position !== 0) {
- var first = command.slice(0, position);
- var last = command.slice(position+1);
- var m = first.match(/([^ ]+ *$)/);
- position = first.length-m[0].length;
- kill_text = first.slice(position);
- command = first.slice(0, position) + last;
- redraw();
- }
- return false;
- } else if (e.which === 72) { // CTRL+H
- if (command !== '' && position > 0) {
- command = command.slice(0, --position);
- if (position < command.length-1) {
- command += command.slice(position);
- }
- redraw();
- }
- return false;
- //NOTE: in opera charCode is undefined
- } else if (e.which === 65) {
- //CTRL+A
- self.position(0);
- } else if (e.which === 69) {
- //CTRL+E
- self.position(command.length);
- } else if (e.which === 88 || e.which === 67 || e.which === 84) {
- //CTRL+X CTRL+C CTRL+W CTRL+T
- return true;
- } else if (e.which === 89) { // CTRL+Y
- if (kill_text !== '') {
- self.insert(kill_text);
- }
- } else if (e.which === 86) {
- //CTRL+V
- paste();
- return true;
- } else if (e.which === 75) {
- //CTRL+K
- if (position === 0) {
- kill_text = command;
- self.set('');
- } else if (position !== command.length) {
- kill_text = command.slice(position);
- self.set(command.slice(0, position));
- }
- } else if (e.which === 85) { // CTRL+U
- if (command !== '' && position !== 0) {
- kill_text = command.slice(0, position);
- self.set(command.slice(position, command.length));
- self.position(0);
- }
- } else if (e.which === 17) { //CTRL+TAB switch tab
- return false;
- }
- }
- } else {
- return true;
- }
- return false;
- } /*else {
- if ((e.altKey && e.which === 68) ||
- (e.ctrlKey &&
- $.inArray(e.which, [65, 66, 68, 69, 80, 78, 70]) > -1) ||
- // 68 === D
- [35, 36, 37, 38, 39, 40].has(e.which)) {
- return false;
- }
- } */
- }
- var history_list = [];
- // -----------------------------------------------------------------------
- // :: Command Line Methods
- // -----------------------------------------------------------------------
- $.extend(self, {
- name: function(string) {
- if (string !== undefined) {
- name = string;
- var enabled = history && history.enabled() || !history;
- history = new History(string, historySize);
- // disable new history if old was disabled
- if (!enabled) {
- history.disable();
- }
- return self;
- } else {
- return name;
- }
- },
- purge: function() {
- history.clear();
- return self;
- },
- history: function() {
- return history;
- },
- set: function(string, stay) {
- if (string !== undefined) {
- command = string;
- if (!stay) {
- position = command.length;
- }
- redraw();
- if (typeof options.onCommandChange === 'function') {
- options.onCommandChange(command);
- }
- }
- return self;
- },
- insert: function(string, stay) {
- if (position === command.length) {
- command += string;
- } else if (position === 0) {
- command = string + command;
- } else {
- command = command.slice(0, position) +
- string + command.slice(position);
- }
- if (!stay) {
- position += string.length;
- }
- redraw();
- if (typeof options.onCommandChange === 'function') {
- options.onCommandChange(command);
- }
- return self;
- },
- get: function() {
- return command;
- },
- commands: function(commands) {
- if (commands) {
- options.commands = commands;
- return self;
- } else {
- return commands;
- }
- },
- destroy: function() {
- $(document.documentElement || window).unbind('.cmd');
- self.stopTime('blink', blink);
- self.find('.cursor').next().remove().end().prev().remove().end().remove();
- self.find('.prompt, .clipboard').remove();
- self.removeClass('cmd').removeData('cmd');
- return self;
- },
- prompt: function(user_prompt) {
- if (user_prompt === undefined) {
- return prompt;
- } else {
- if (typeof user_prompt === 'string' ||
- typeof user_prompt === 'function') {
- prompt = user_prompt;
- } else {
- throw 'prompt must be a function or string';
- }
- draw_prompt();
- // we could check if command is longer then numchars-new prompt
- redraw();
- return self;
- }
- },
- kill_text: function() {
- return kill_text;
- },
- position: function(n) {
- if (typeof n === 'number') {
- position = n < 0 ? 0 : n > command.length ? command.length : n;
- redraw();
- return self;
- } else {
- return position;
- }
- },
- visible: (function() {
- var visible = self.visible;
- return function() {
- visible.apply(self, []);
- redraw();
- draw_prompt();
- };
- })(),
- show: (function() {
- var show = self.show;
- return function() {
- show.apply(self, []);
- redraw();
- draw_prompt();
- };
- })(),
- resize: function(num) {
- if (num) {
- num_chars = num;
- } else {
- change_num_chars();
- }
- redraw();
- return self;
- },
- enable: function() {
- enabled = true;
- animation(true);
- return self;
- },
- isenabled: function() {
- return enabled;
- },
- disable: function() {
- enabled = false;
- animation(false);
- return self;
- },
- mask: function(display) {
- if (typeof display === 'boolean') {
- mask = display;
- redraw();
- return self;
- } else {
- return mask;
- }
- }
- });
- // -----------------------------------------------------------------------
- // :: INIT
- // -----------------------------------------------------------------------
- self.name(options.name || options.prompt || '');
- prompt = options.prompt || '> ';
- draw_prompt();
- if (options.enabled === undefined || options.enabled === true) {
- self.enable();
- }
- // Keystrokes
- var object;
- $(document.documentElement || window).bind('keypress.cmd', function(e) {
- var result;
- if (e.ctrlKey && e.which === 99) { // CTRL+C
- return true;
- }
- if (!reverse_search && typeof options.keypress === 'function') {
- result = options.keypress(e);
- }
- if (result === undefined || result) {
- if (enabled) {
- if ($.inArray(e.which, [38, 13, 0, 8]) > -1 &&
- e.keyCode !== 123 && // for F12 which === 0
- //!(e.which === 40 && e.shiftKey ||
- !(e.which === 38 && e.shiftKey)) {
- return false;
- } else if (!e.ctrlKey && !(e.altKey && e.which === 100) || e.altKey) { // ALT+D
- // TODO: this should be in one statement
- if (reverse_search) {
- reverse_search_string += String.fromCharCode(e.which);
- reverse_history_search();
- draw_reverse_prompt();
- } else {
- self.insert(String.fromCharCode(e.which));
- }
- return false;
- }
- }
- } else {
- return result;
- }
- }).bind('keydown.cmd', keydown_event);
- // characters
- self.data('cmd', self);
- return self;
- }; // cmd plugin
-
- // -------------------------------------------------------------------------
- // :: TOOLS
- // -------------------------------------------------------------------------
- function skipFormattingCount(string) {
- // this will covert html entities to single characters
- return $('
' + $.terminal.strip(string) + '
').text().length;
- }
- // -------------------------------------------------------------------------
- function formattingCount(string) {
- return string.length - skipFormattingCount(string);
- }
- // -------------------------------------------------------------------------
- // taken from https://hacks.mozilla.org/2011/09/detecting-and-generating-css-animations-in-javascript/
- function supportAnimations() {
- var animation = false,
- animationstring = 'animation',
- keyframeprefix = '',
- domPrefixes = 'Webkit Moz O ms Khtml'.split(' '),
- pfx = '',
- elm = document.createElement('div');
- if (elm.style.animationName) { animation = true; }
- if (animation === false) {
- for (var i = 0; i < domPrefixes.length; i++) {
- if (elm.style[ domPrefixes[i] + 'AnimationName' ] !== undefined) {
- pfx = domPrefixes[i];
- animationstring = pfx + 'Animation';
- keyframeprefix = '-' + pfx.toLowerCase() + '-';
- animation = true;
- break;
- }
- }
- }
- return animation;
- }
- // -------------------------------------------------------------------------
- function processCommand(string, fn) {
- var args = string.replace(/^\s+|\s+$/g, '').split(/(\s+)/);
- var rest = string.replace(/^[^\s]+\s*/, '');
- return {
- name: args[0],
- args: fn(rest),
- rest: rest
- };
- }
- // colors from http://www.w3.org/wiki/CSS/Properties/color/keywords
- var color_names = [
- 'black', 'silver', 'gray', 'white', 'maroon', 'red', 'purple',
- 'fuchsia', 'green', 'lime', 'olive', 'yellow', 'navy', 'blue', 'teal',
- 'aqua', 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure',
- 'beige', 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet',
- 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral',
- 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan',
- 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki',
- 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred',
- 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray',
- 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink',
- 'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick',
- 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite',
- 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'grey', 'honeydew',
- 'hotpink', 'indianred', 'indigo', 'ivory', 'khaki', 'lavender',
- 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral',
- 'lightcyan', 'lightgoldenrodyellow', 'lightgray', 'lightgreen',
- 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen',
- 'lightskyblue', 'lightslategray', 'lightslategrey', 'lightsteelblue',
- 'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', 'maroon',
- 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple',
- 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen',
- 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream',
- 'mistyrose', 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive',
- 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod',
- 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip',
- 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'red',
- 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown',
- 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue',
- 'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue', 'tan',
- 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white',
- 'whitesmoke', 'yellow', 'yellowgreen'];
- // -------------------------------------------------------------------------
- var format_split_re = /(\[\[[gbiuso]*;[^;]*;[^\]]*\](?:[^\]]*\\\][^\]]*|[^\]]*|[^\[]*\[[^\]]*)\]?)/i;
- var format_parts_re = /\[\[([gbiuso]*);([^;]*);([^;\]]*);?([^;\]]*);?([^\]]*)\]([^\]]*\\\][^\]]*|[^\]]*|[^\[]*\[[^\]]*)\]?/gi;
- var format_re = /\[\[([gbiuso]*;[^;\]]*;[^;\]]*(?:;|[^\]()]*);?[^\]]*)\]([^\]]*\\\][^\]]*|[^\]]*|[^\[]*\[[^\]]*)\]?/gi;
- var format_full_re = /^\[\[([gbiuso]*;[^;\]]*;[^;\]]*(?:;|[^\]()]*);?[^\]]*)\]([^\]]*\\\][^\]]*|[^\]]*|[^\[]*\[[^\]]*)\]$/gi;
- var color_hex_re = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i;
- //var url_re = /https?:\/\/(?:(?!&[^;]+;)[^\s:"'<>)])+/g;
- var url_re = /\bhttps?:\/\/(?:(?!&[^;]+;)[^\s"'<>)])+\b/g;
- var email_re = /((([^<>('")[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})))/g;
- var command_re = /('[^']*'|"(\\"|[^"])*"|\/(\\\/|[^\/])+\/[gimy]*|(\\ |[^ ])+|[\w-]+)/g;
- var format_begin_re = /(\[\[[gbiuso]*;[^;]*;[^\]]*\])/i;
- var format_last_re = /\[\[[gbiuso]*;[^;]*;[^\]]*\]?$/i;
- $.terminal = {
- // -----------------------------------------------------------------------
- // :: Validate html color (it can be name or hex)
- // -----------------------------------------------------------------------
- valid_color: function(color) {
- return color.match(color_hex_re) || $.inArray(color.toLowerCase(), color_names) !== -1;
- },
- // -----------------------------------------------------------------------
- // :: Escape all special regex characters, so it can be use as regex to
- // :: match exact string that contain those characters
- // -----------------------------------------------------------------------
- escape_regex: function(string) {
- var special = /([\^\$\[\]\(\)\+\*\.\|])/g;
- return string.replace(special, '\\$1');
- },
- // -----------------------------------------------------------------------
- // :: test if string contain formatting
- // -----------------------------------------------------------------------
- have_formatting: function(str) {
- return str.match(format_re);
- },
- is_formatting: function(str) {
- return str.match(format_full_re);
- },
- // -----------------------------------------------------------------------
- // :: return array of formatting and text between them
- // -----------------------------------------------------------------------
- format_split: function(str) {
- return str.split(format_split_re);
- },
- // -----------------------------------------------------------------------
- // :: split text into lines with equal length so each line can be rendered
- // :: separately (text formatting can be longer then a line).
- // -----------------------------------------------------------------------
- split_equal: function(str, length) {
- var formatting = false;
- var in_text = false;
- var braket = 0;
- var prev_format = '';
- var result = [];
- // add format text as 5th paramter to formatting it's used for
- // data attribute in format function
- var array = str.replace(format_re, function(_, format, text) {
- var semicolons = format.match(/;/g).length;
- // missing semicolons
- if (semicolons == 2) {
- semicolons = ';;';
- } else if (semicolons == 3) {
- semicolons = ';';
- } else {
- semicolons = '';
- }
- // return '[[' + format + ']' + text + ']';
- // closing braket will break formatting so we need to escape those using
- // html entity equvalent
- return '[[' + format + semicolons +
- text.replace(/\\\]/g, ']').replace(/\n/g, '\\n') + ']' +
- text + ']';
- }).split(/\n/g);
- for (var i = 0, len = array.length; i < len; ++i) {
- if (array[i] === '') {
- result.push('');
- continue;
- }
- var line = array[i];
- var first_index = 0;
- var count = 0;
- for (var j=0, jlen=line.length; j/g, '>')
- .replace(/ /g, ' ')
- .replace(/\t/g, ' ');
- },
- // -----------------------------------------------------------------------
- // :: Replace terminal formatting with html
- // -----------------------------------------------------------------------
- format: function(str, options) {
- var settings = $.extend({}, {
- linksNoReferrer: false
- }, options || {});
- if (typeof str === 'string') {
- //support for formating foo[[u;;]bar]baz[[b;#fff;]quux]zzz
- var splited = $.terminal.format_split(str);
- if (splited && splited.length > 1) {
- str = $.map(splited, function(text) {
- if (text === '') {
- return text;
- } else if (text.substring(0,1) === '[') {
- // use substring for IE quirks mode - [0] don't work
- return text.replace(format_parts_re, function(s,
- style,
- color,
- background,
- _class,
- data_text,
- text) {
- if (text === '') {
- return ''; //'';
- }
- text = text.replace(/\\]/g, ']');
- var style_str = '';
- if (style.indexOf('b') !== -1) {
- style_str += 'font-weight:bold;';
- }
- var text_decoration = [];
- if (style.indexOf('u') !== -1) {
- text_decoration.push('underline');
- }
- if (style.indexOf('s') !== -1) {
- text_decoration.push('line-through');
- }
- if (style.indexOf('o') !== -1) {
- text_decoration.push('overline');
- }
- if (text_decoration.length) {
- style_str += 'text-decoration:' +
- text_decoration.join(' ') + ';';
- }
- if (style.indexOf('i') !== -1) {
- style_str += 'font-style:italic;';
- }
- if ($.terminal.valid_color(color)) {
- style_str += 'color:' + color + ';';
- if (style.indexOf('g') !== -1) {
- style_str += 'text-shadow:0 0 5px ' + color + ';';
- }
- }
- if ($.terminal.valid_color(background)) {
- style_str += 'background-color:' + background;
- }
- var data;
- if (data_text === '') {
- data = text;
- } else {
- data = data_text.replace(/]/g, ']');
- }
- var result = '' + text + '';
- return result;
- });
- } else {
- return '' + text + '';
- }
- }).join('');
- }
- return $.map(str.split(/(<\/?span[^>]*>)/g), function(string) {
- if (!string.match(/span/)) {
- return string.replace(url_re, function(link) {
- var comma = link.match(/\.$/);
- link = link.replace(/\.$/, '');
- return '' + link + '' +
- (comma ? '.' : '');
- }).replace(email_re, '$1');
- } else {
- return string;
- }
- }).join('').replace(/ <\/span>/g, ' ');
- } else {
- return '';
- }
- },
- // -----------------------------------------------------------------------
- // :: Replace brackets with html entities
- // -----------------------------------------------------------------------
- escape_brackets: function(string) {
- return string.replace(/\[/g, '[').replace(/\]/g, ']');
- },
- // -----------------------------------------------------------------------
- // :: Remove formatting from text
- // -----------------------------------------------------------------------
- strip: function(str) {
- return str.replace(format_parts_re, '$6');
- },
- // -----------------------------------------------------------------------
- // :: Return active terminal
- // -----------------------------------------------------------------------
- active: function() {
- return terminals.front();
- },
- // -----------------------------------------------------------------------
- // :: Replace overtyping (from man) formatting with terminal formatting
- // -----------------------------------------------------------------------
- overtyping: function(string) {
- return string.replace(/((?:_\x08.|.\x08_)+)/g, function(full, g) {
- return '[[u;;]' + full.replace(/_x08|\x08_|_\u0008|\u0008_/g, '') + ']';
- }).replace(/((?:.\x08.)+)/g, function(full, g) {
- return '[[b;#fff;]' + full.replace(/(.)(?:\x08|\u0008)(.)/g,
- function(full, g1, g2) {
- return g2;
- }) + ']';
- });
- },
- // -----------------------------------------------------------------------
- // :: Html colors taken from ANSI formatting in Linux Terminal
- // -----------------------------------------------------------------------
- ansi_colors: {
- normal: {
- black: '#000',
- red: '#A00',
- green: '#008400',
- yellow: '#A50',
- blue: '#00A',
- magenta: '#A0A',
- cyan: '#0AA',
- white: '#AAA'
- },
- faited: {
- black: '#000',
- red: '#640000',
- green: '#006100',
- yellow: '#737300',
- blue: '#000087',
- magenta: '#650065',
- cyan: '#008787',
- white: '#818181'
- },
- bold: {
- black: '#000',
- red: '#F55',
- green: '#44D544',
- yellow: '#FF5',
- blue: '#55F',
- magenta: '#F5F',
- cyan: '#5FF',
- white: '#FFF'
- },
- // XTerm 8-bit pallete
- palette: [
- '#000000', '#AA0000', '#00AA00', '#AA5500', '#0000AA',
- '#AA00AA', '#00AAAA', '#AAAAAA', '#555555', '#FF5555',
- '#55FF55', '#FFFF55', '#5555FF', '#FF55FF', '#55FFFF',
- '#FFFFFF', '#000000', '#00005F', '#000087', '#0000AF',
- '#0000D7', '#0000FF', '#005F00', '#005F5F', '#005F87',
- '#005FAF', '#005FD7', '#005FFF', '#008700', '#00875F',
- '#008787', '#0087AF', '#0087D7', '#00AF00', '#00AF5F',
- '#00AF87', '#00AFAF', '#00AFD7', '#00AFFF', '#00D700',
- '#00D75F', '#00D787', '#00D7AF', '#00D7D7', '#00D7FF',
- '#00FF00', '#00FF5F', '#00FF87', '#00FFAF', '#00FFD7',
- '#00FFFF', '#5F0000', '#5F005F', '#5F0087', '#5F00AF',
- '#5F00D7', '#5F00FF', '#5F5F00', '#5F5F5F', '#5F5F87',
- '#5F5FAF', '#5F5FD7', '#5F5FFF', '#5F8700', '#5F875F',
- '#5F8787', '#5F87AF', '#5F87D7', '#5F87FF', '#5FAF00',
- '#5FAF5F', '#5FAF87', '#5FAFAF', '#5FAFD7', '#5FAFFF',
- '#5FD700', '#5FD75F', '#5FD787', '#5FD7AF', '#5FD7D7',
- '#5FD7FF', '#5FFF00', '#5FFF5F', '#5FFF87', '#5FFFAF',
- '#5FFFD7', '#5FFFFF', '#870000', '#87005F', '#870087',
- '#8700AF', '#8700D7', '#8700FF', '#875F00', '#875F5F',
- '#875F87', '#875FAF', '#875FD7', '#875FFF', '#878700',
- '#87875F', '#878787', '#8787AF', '#8787D7', '#8787FF',
- '#87AF00', '#87AF5F', '#87AF87', '#87AFAF', '#87AFD7',
- '#87AFFF', '#87D700', '#87D75F', '#87D787', '#87D7AF',
- '#87D7D7', '#87D7FF', '#87FF00', '#87FF5F', '#87FF87',
- '#87FFAF', '#87FFD7', '#87FFFF', '#AF0000', '#AF005F',
- '#AF0087', '#AF00AF', '#AF00D7', '#AF00FF', '#AF5F00',
- '#AF5F5F', '#AF5F87', '#AF5FAF', '#AF5FD7', '#AF5FFF',
- '#AF8700', '#AF875F', '#AF8787', '#AF87AF', '#AF87D7',
- '#AF87FF', '#AFAF00', '#AFAF5F', '#AFAF87', '#AFAFAF',
- '#AFAFD7', '#AFAFFF', '#AFD700', '#AFD75F', '#AFD787',
- '#AFD7AF', '#AFD7D7', '#AFD7FF', '#AFFF00', '#AFFF5F',
- '#AFFF87', '#AFFFAF', '#AFFFD7', '#AFFFFF', '#D70000',
- '#D7005F', '#D70087', '#D700AF', '#D700D7', '#D700FF',
- '#D75F00', '#D75F5F', '#D75F87', '#D75FAF', '#D75FD7',
- '#D75FFF', '#D78700', '#D7875F', '#D78787', '#D787AF',
- '#D787D7', '#D787FF', '#D7AF00', '#D7AF5F', '#D7AF87',
- '#D7AFAF', '#D7AFD7', '#D7AFFF', '#D7D700', '#D7D75F',
- '#D7D787', '#D7D7AF', '#D7D7D7', '#D7D7FF', '#D7FF00',
- '#D7FF5F', '#D7FF87', '#D7FFAF', '#D7FFD7', '#D7FFFF',
- '#FF0000', '#FF005F', '#FF0087', '#FF00AF', '#FF00D7',
- '#FF00FF', '#FF5F00', '#FF5F5F', '#FF5F87', '#FF5FAF',
- '#FF5FD7', '#FF5FFF', '#FF8700', '#FF875F', '#FF8787',
- '#FF87AF', '#FF87D7', '#FF87FF', '#FFAF00', '#FFAF5F',
- '#FFAF87', '#FFAFAF', '#FFAFD7', '#FFAFFF', '#FFD700',
- '#FFD75F', '#FFD787', '#FFD7AF', '#FFD7D7', '#FFD7FF',
- '#FFFF00', '#FFFF5F', '#FFFF87', '#FFFFAF', '#FFFFD7',
- '#FFFFFF', '#080808', '#121212', '#1C1C1C', '#262626',
- '#303030', '#3A3A3A', '#444444', '#4E4E4E', '#585858',
- '#626262', '#6C6C6C', '#767676', '#808080', '#8A8A8A',
- '#949494', '#9E9E9E', '#A8A8A8', '#B2B2B2', '#BCBCBC',
- '#C6C6C6', '#D0D0D0', '#DADADA', '#E4E4E4', '#EEEEEE'
- ]
- },
- // -----------------------------------------------------------------------
- // :: Replace ANSI formatting with terminal formatting
- // -----------------------------------------------------------------------
- from_ansi: (function() {
- var color = {
- 30: 'black',
- 31: 'red',
- 32: 'green',
- 33: 'yellow',
- 34: 'blue',
- 35: 'magenta',
- 36: 'cyan',
- 37: 'white',
-
- 39: 'white' // default color
- };
- var background = {
- 40: 'black',
- 41: 'red',
- 42: 'green',
- 43: 'yellow',
- 44: 'blue',
- 45: 'magenta',
- 46: 'cyan',
- 47: 'white',
-
- 49: 'black' // default background
- };
- function format_ansi(code) {
- var controls = code.split(';');
- var num;
- var faited = false;
- var reverse = false;
- var bold = false;
- var styles = [];
- var output_color = '';
- var output_background = '';
- var _8bit_color = false;
- var _8bit_background = false;
- var process_8bit = false;
- var palette = $.terminal.ansi_colors.palette;
- for(var i in controls) {
- num = parseInt(controls[i], 10);
- switch(num) {
- case 1:
- styles.push('b');
- bold = true;
- faited = false;
- break;
- case 4:
- styles.push('u');
- break;
- case 3:
- styles.push('i');
- break;
- case 5:
- process_8bit = true;
- break;
- case 38:
- _8bit_color = true;
- break;
- case 48:
- _8bit_background = true;
- break;
- case 2:
- faited = true;
- bold = false;
- break;
- case 7:
- reverse = true;
- break;
- default:
- if (_8bit_color && process_8bit && palette[num-1]) {
- output_color = palette[num-1];
- } else if (color[num]) {
- output_color = color[num];
- }
- if (_8bit_background && process_8bit && palette[num-1]) {
- output_background = palette[num-1];
- } else if (background[num]) {
- output_background = background[num];
- }
- }
- if (num !== 5) {
- process_8bit = false;
- }
- }
- if (reverse) {
- if (output_color && output_background) {
- var tmp = output_background;
- output_background = output_color;
- output_color = tmp;
- } else {
- output_color = 'black';
- output_background = 'white';
- }
- }
- var colors, backgrounds;
- if (bold) {
- colors = backgrounds = $.terminal.ansi_colors.bold;
- } else if (faited) {
- colors = backgrounds = $.terminal.ansi_colors.faited;
- } else {
- colors = backgrounds = $.terminal.ansi_colors.normal;
- }
- return [styles.join(''),
- _8bit_color ? output_color : colors[output_color],
- _8bit_background ? output_background : backgrounds[output_background]
- ];
- }
- return function(input) {
- var splitted = input.split(/(\x1B\[[0-9;]*[A-Za-z])/g);
- if (splitted.length == 1) {
- return input;
- }
- var output = [];
- //skip closing at the begining
- if (splitted.length > 3 && splitted.slice(0,3).join('') == '[0m') {
- splitted = splitted.slice(3);
- }
- var inside = false, next, prev_color, prev_background, code, match;
- for (var i=0; iFooBarBaz', '\tformatting');
- string = 'http://terminal.jcubic.pl/examples.php https://www.google.com/?q=jquery%20terminal';
- assert($.terminal.format(string) === 'http://terminal.jcubic.pl/examples.phphttps://www.google.com/?q=jquery%20terminal', '\turls');
- string = 'foo@bar.com baz.quux@example.com';
- assert($.terminal.format(string) === 'foo@bar.combaz.quux@example.com', '\temails');
- string = '-_-[[biugs;#fff;#000]Foo]-_-[[i;;;foo]Bar]-_-[[ous;;]Baz]-_-';
- assert($.terminal.strip(string) === '-_-Foo-_-Bar-_-Baz-_-', '$.terminal.strip');
- string = '[[bui;#fff;]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sed dolor nisl, in suscipit justo. Donec a enim et est porttitor semper at vitae augue. Proin at nulla at dui mattis mattis. Nam a volutpat ante. Aliquam consequat dui eu sem convallis ullamcorper. Nulla suscipit, massa vitae suscipit ornare, tellus] est [[b;;#f00]consequat nunc, quis blandit elit odio eu arcu. Nam a urna nec nisl varius sodales. Mauris iaculis tincidunt orci id commodo. Aliquam] non magna quis [[i;;]tortor malesuada aliquam] eget ut lacus. Nam ut vestibulum est. Praesent volutpat tellus in eros dapibus elementum. Nam laoreet risus non nulla mollis ac luctus [[ub;#fff;]felis dapibus. Pellentesque mattis elementum augue non sollicitudin. Nullam lobortis fermentum elit ac mollis. Nam ac varius risus. Cras faucibus euismod nulla, ac auctor diam rutrum sit amet. Nulla vel odio erat], ac mattis enim.';
- term.echo('$.terminal.split_equal');
- var cols = [10, 40, 60, 400];
- for (var i=cols.length; i--;) {
- var lines = $.terminal.split_equal(string, cols[i]);
- var success = true;
- for (var j=0; j cols[i]) {
- success = false;
- break;
- }
- }
- assert(success, '\tsplit ' + cols[i]);
- }
- }
- };
-
- // -----------------------------------------------------------------------
- // Helper plugins
- // -----------------------------------------------------------------------
- $.fn.visible = function() {
- return this.css('visibility', 'visible');
- };
- $.fn.hidden = function() {
- return this.css('visibility', 'hidden');
- };
- // -----------------------------------------------------------------------
- // JSON-RPC CALL
- // -----------------------------------------------------------------------
- var ids = {};
- $.jrpc = function(url, method, params, success, error) {
- ids[url] = ids[url] || 0;
- var request = $.json_stringify({
- 'jsonrpc': '2.0', 'method': method,
- 'params': params, 'id': ++ids[url]});
- return $.ajax({
- url: url,
- data: request,
- success: function(result, status, jqXHR) {
- var content_type = jqXHR.getResponseHeader('Content-Type');
- if (!content_type.match(/application\/json/)) {
- if (console && console.warn) {
- console.warn('Response Content-Type is not application/json');
- } else {
- throw new Error('WARN: Response Content-Type is not application/json');
- }
- }
- var json;
- try {
- json = $.parseJSON(result);
- } catch (e) {
- if (error) {
- error(jqXHR, 'Invalid JSON', e);
- } else {
- throw new Error('Invalid JSON');
- }
- return;
- }
- // don't catch errors in success callback
- success(json, status, jqXHR);
- },
- error: error,
- contentType: 'application/json',
- dataType: 'text',
- async: true,
- cache: false,
- //timeout: 1,
- type: 'POST'});
- };
-
- // -----------------------------------------------------------------------
- function is_scrolled_into_view(elem) {
- var docViewTop = $(window).scrollTop();
- var docViewBottom = docViewTop + $(window).height();
-
- var elemTop = $(elem).offset().top;
- var elemBottom = elemTop + $(elem).height();
-
- return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
- }
- // -----------------------------------------------------------------------
- // :: Create fake terminal to calcualte the dimention of one character
- // :: this will make terminal work if terminal div is not added to the
- // :: DOM at init like with:
- // :: $('').terminal().echo('foo bar').appendTo('body');
- // -----------------------------------------------------------------------
- function char_size() {
- var temp = $('
' +
- '
').appendTo('body');
- var span = temp.find('span');
- var result = {
- width: span.width(),
- height: span.outerHeight()
- };
- temp.remove();
- return result;
- }
- // -----------------------------------------------------------------------
- // :: calculate numbers of characters
- // -----------------------------------------------------------------------
- function get_num_chars(terminal) {
- var width = char_size().width;
- var result = Math.floor(terminal.width() / width);
- if (have_scrollbars(terminal)) {
- var SCROLLBAR_WIDTH = 20;
- // assume that scrollbars are 20px - in my Laptop with
- // Linux/Chrome they are 16px
- var margins = terminal.innerWidth() - terminal.width();
- result -= Math.ceil((SCROLLBAR_WIDTH - margins / 2) / (width-1));
- }
- return result;
- }
- // -----------------------------------------------------------------------
- // :: Calculate number of lines that fit without scroll
- // -----------------------------------------------------------------------
- function get_num_rows(terminal) {
- return Math.floor(terminal.height() / char_size().height);
- }
- // -----------------------------------------------------------------------
- // :: Get Selected Text (this is internal because it return text even if
- // :: it's outside of terminal, is used to paste text to the terminal)
- // -----------------------------------------------------------------------
- function getSelectedText() {
- if (window.getSelection || document.getSelection) {
- var selection = (window.getSelection || document.getSelection)();
- if (selection.text) {
- return selection.text;
- } else {
- return selection.toString();
- }
- } else if (document.selection) {
- return document.selection.createRange().text;
- }
- }
- // -----------------------------------------------------------------------
- // :: check if div have scrollbars (need to have overflow auto or always)
- // -----------------------------------------------------------------------
- function have_scrollbars(div) {
- return div.get(0).scrollHeight > div.innerHeight();
- }
- // -----------------------------------------------------------------------
- // :: TERMINAL PLUGIN CODE
- // -----------------------------------------------------------------------
- var version = '0.8.7';
- var version_set = !version.match(/^\{\{/);
- var copyright = 'Copyright (c) 2011-2013 Jakub Jankiewicz ';
- var version_string = version_set ? ' version ' + version : ' ';
- //regex is for placing version string aligned to the right
- var reg = new RegExp(" {" + version_string.length + "}$");
- // -----------------------------------------------------------------------
- // :: Terminal Signatures
- // -----------------------------------------------------------------------
- var signatures = [
- ['jQuery Terminal', '(c) 2011-2013 jcubic'],
- ['jQuery Terminal Emulator' + (version_set ? ' v. ' + version : ''),
- copyright.replace(/ *<.*>/, '')],
- ['jQuery Terminal Emulator' + (version_set ? version_string : ''),
- copyright.replace(/^Copyright /, '')],
- [' _______ ________ __',
- ' / / _ /_ ____________ _/__ ___/______________ _____ / /',
- ' __ / / // / // / _ / _/ // / / / _ / _/ / / \\/ / _ \\/ /',
- '/ / / // / // / ___/ // // / / / ___/ // / / / / /\\ / // / /__',
- '\\___/____ \\\\__/____/_/ \\__ / /_/____/_//_/ /_/ /_/ \\/\\__\\_\\___/',
- ' \\/ /____/ '.replace(reg, ' ') +
- version_string,
- copyright],
- [' __ _____ ________ __',
- ' / // _ /__ __ _____ ___ __ _/__ ___/__ ___ ______ __ __ __ ___ / /',
- ' __ / // // // // // _ // _// // / / // _ // _// // // \\/ // _ \\/ /',
- '/ / // // // // // ___// / / // / / // ___// / / / / // // /\\ // // / /__',
- '\\___//____ \\\\___//____//_/ _\\_ / /_//____//_/ /_/ /_//_//_/ /_/ \\__\\_\\___/',
- ' \\/ /____/ '.replace(reg, '') +
- version_string,
- copyright]
- ];
- // -----------------------------------------------------------------------
- // :: Default options
- // -----------------------------------------------------------------------
- $.terminal.defaults = {
- prompt: '> ',
- history: true,
- exit: true,
- clear: true,
- enabled: true,
- historySize: 60,
- checkArity: true,
- exceptionHandler: null,
- cancelableAjax: true,
- processArguments: true,
- linksNoReferrer: false,
- login: null,
- outputLimit: -1,
- onAjaxError: null,
- onRPCError: null,
- completion: false,
- historyFilter: null,
- onInit: $.noop,
- onClear: $.noop,
- onBlur: $.noop,
- onFocus: $.noop,
- onTerminalChange: $.noop,
- onExit: $.noop,
- keypress: $.noop,
- keydown: $.noop,
- strings: {
- wrongPasswordTryAgain: "Wrong password try again!",
- wrongPassword: "Wrong password!",
- ajaxAbortError: "Error while aborting ajax call!",
- wrongArity: "Wrong number of arguments. Function '%s' expect %s got %s!",
- commandNotFound: "Command '%s' Not Found!",
- oneRPCWithIgnore: "You can use only one rpc with ignoreSystemDescribe",
- oneInterpreterFunction: "You can't use more then one function (rpc with " +
- "ignoreSystemDescribe is count as one)",
- loginFunctionMissing: "You don't have login function",
- noTokenError: "Access denied (no token)",
- serverResponse: "Server reponse is",
- wrongGreetings: "Wrong value of greetings parameter",
- notWhileLogin: "You can't call that function while in login",
- loginIsNotAFunction: "Authenticate must be a function",
- canExitError: "You can't exit from main interpeter",
- invalidCompletion: "Invalid completion",
- login: "login",
- password: "password"
- }
- };
- // -----------------------------------------------------------------------
- // :: All terminal globals
- // -----------------------------------------------------------------------
- var requests = []; // for canceling on CTRL+D
- var terminals = new Cycle(); // list of terminals global in this scope
- $.fn.terminal = function(init_interpreter, options) {
- // -----------------------------------------------------------------------
- // :: helper function
- // -----------------------------------------------------------------------
- function get_processed_command(command) {
- if (typeof settings.processArguments === 'function') {
- return processCommand(command, settings.processArguments);
- } else if (settings.processArguments) {
- return $.terminal.parseCommand(command);
- } else {
- return $.terminal.splitCommand(command);
- }
- }
- // -----------------------------------------------------------------------
- // :: Display object on terminal
- // -----------------------------------------------------------------------
- function display_object(object) {
- if (typeof object === 'string') {
- self.echo(object);
- } else if (object instanceof Array) {
- self.echo($.map(object, function(object) {
- return $.json_stringify(object);
- }).join(' '));
- } else if (typeof object === 'object') {
- self.echo($.json_stringify(object));
- } else {
- self.echo(object);
- }
- }
- // -----------------------------------------------------------------------
- // :: Helper function
- // -----------------------------------------------------------------------
- function display_json_rpc_error(error) {
- if (typeof settings.onRPCError === 'function') {
- settings.onRPCError.call(self, error);
- } else {
- self.error('[RPC] ' + error.message);
- }
- }
- // -----------------------------------------------------------------------
- // :: Create interpreter function from url string
- // -----------------------------------------------------------------------
- function make_basic_json_rpc_interpreter(url) {
- var service = function(method, params) {
- self.pause();
- $.jrpc(url, method, params, function(json) {
- if (!json.error) {
- if (typeof settings.processRPCResponse === 'function') {
- settings.processRPCResponse.call(self, json.result);
- } else {
- display_object(json.result);
- }
- } else {
- display_json_rpc_error(json.error);
- }
- self.resume();
- }, ajax_error);
- };
- //this is the interpreter function
- return function(command, terminal) {
- if (command === '') {
- return;
- }
- command = get_processed_command(command);
- if (!settings.login || command.name === 'help') {
- // allows to call help without a token
- service(command.name, command.args);
- } else {
- var token = terminal.token();
- if (token) {
- service(command.name, [token].concat(command.args));
- } else {
- //should never happen
- terminal.error('[AUTH] ' +
- strings.noTokenError);
- }
- }
- };
- }
- // -----------------------------------------------------------------------
- // :: Create interpreter function from Object. If the value is object
- // :: it will create nested interpreters
- // -----------------------------------------------------------------------
- function make_object_interpreter(object, arity, fallback) {
- // function that maps commands to object methods
- // it keeps terminal context
- return function(user_command, terminal) {
- if (user_command === '') {
- return;
- }
- //command = split_command_line(command);
- var command = get_processed_command(user_command);
- var val = object[command.name];
- var type = $.type(val);
- if (type === 'function') {
- if (arity && val.length !== command.args.length) {
- self.error("[Arity] " +
- sprintf(strings.wrongArity,
- command.name,
- val.length,
- command.args.length));
- } else {
- return val.apply(self, command.args);
- }
- } else if (type === 'object' || type === 'string') {
- var commands = [];
- if (type === 'object') {
- commands = Object.keys(val);
- val = make_object_interpreter(val, arity);
- }
- terminal.push(val, {
- prompt: command.name + '> ',
- name: command.name,
- completion: type === 'object' ? function(term, string, callback) {
- callback(commands);
- } : undefined
- });
- } else {
- if ($.type(fallback) === 'function') {
- fallback(user_command, self);
- } else if ($.type(settings.onCommandNotFound) === 'function') {
- settings.onCommandNotFound(user_command, self);
- } else {
- terminal.error(sprintf(strings.commandNotFound, command.name));
- }
- }
- };
- }
- // -----------------------------------------------------------------------
- function ajax_error(xhr, status, error) {
- self.resume(); // onAjaxError can use pause/resume call it first
- if (typeof settings.onAjaxError == 'function') {
- settings.onAjaxError.call(self, xhr, status, error);
- } else if (status !== 'abort') {
- self.error('[AJAX] ' + status + ' - ' +
- strings.serverResponse +
- ': \n' + xhr.responseText);
- }
- }
- // -----------------------------------------------------------------------
- function make_json_rpc_object(url, success) {
- $.jrpc(url, 'system.describe', [], function(ret) {
- var commands = [];
- if (ret.procs) {
- var interpreter_object = {};
- $.each(ret.procs, function(_, proc) {
- interpreter_object[proc.name] = function() {
- var args = Array.prototype.slice.call(arguments);
- if (settings.checkArity && proc.params &&
- proc.params.length !== args.length) {
- self.error("[Arity] " +
- sprintf(strings.wrongArity,
- proc.name,
- proc.params.length,
- args.length));
- } else {
- self.pause();
- $.jrpc(url, proc.name, args, function(json) {
- if (json.error) {
- display_json_rpc_error(json.error);
- } else {
- display_object(json.result);
- }
- self.resume();
- }, ajax_error);
- }
- };
- });
- success(interpreter_object);
- } else {
- success(null);
- }
- }, function() {
- success(null);
- });
- }
- // -----------------------------------------------------------------------
- function make_interpreter(user_interpreter, finalize) {
- finalize = finalize || $.noop;
- var type = $.type(user_interpreter);
- var result = {};
- var commands;
- var rpc_count = 0; // only one rpc can be use for array
- var function_interpreter;
- if (type === 'array') {
- var object = {};
- // recur will be called when previous acync call is finished
- (function recur(interpreters, success) {
- if (interpreters.length) {
- var first = interpreters[0];
- var rest = interpreters.slice(1);
- var type = $.type(first);
- if (type === 'string') {
- rpc_count++;
- self.pause();
- if (settings.ignoreSystemDescribe) {
- if (rpc_count === 1) {
- function_interpreter = make_basic_json_rpc_interpreter(first);
- } else {
- self.error(strings.oneRPCWithIgnore);
- }
- recur(rest, success);
- } else {
- make_json_rpc_object(first, function(new_object) {
- // will ignore rpc in array that don't have system.describe
- if (new_object) {
- $.extend(object, new_object);
- }
- self.resume();
- recur(rest, success);
- });
- }
- } else if (type === 'function') {
- if (function_interpreter) {
- self.error(strings.oneInterpreterFunction);
- } else {
- function_interpreter = first;
- }
- } else if (type === 'object') {
- $.extend(object, first);
- recur(rest, success);
- }
- } else {
- success();
- }
- })(user_interpreter, function() {
- commands = Object.keys(object);
- result.interpreter = make_object_interpreter(object, false, function_interpreter);
- result.completion = function(term, string, callback) {
- callback(commands);
- };
- finalize(result);
- });
- } else if (type === 'string') {
- if (settings.ignoreSystemDescribe) {
- finalize({
- interpreter: make_basic_json_rpc_interpreter(user_interpreter),
- completion: settings.completion
- });
- } else {
- self.pause();
- make_json_rpc_object(user_interpreter, function(object) {
- if (object) {
- result.interpreter = make_object_interpreter(object, false);
- result.completion = function(term, string, callback) {
- callback(commands);
- };
- } else {
- // no procs in system.describe
- result.interpreter = make_basic_json_rpc_interpreter(user_interpreter);
- result.completion = settings.completion;
- }
- self.resume();
- finalize(result);
- });
- }
- } else if (type === 'object') {
- commands = Object.keys(user_interpreter);
- result.interpreter = make_object_interpreter(user_interpreter, settings.checkArity);
- result.completion = function(term, string, callback) {
- callback(commands);
- };
- finalize(result);
- } else {
- // allow $(').terminal();
- if (type === 'undefined') {
- user_interpreter = $.noop;
- } else if (type !== 'function') {
- throw type + " is invalid interpreter value";
- }
- finalize({
- interpreter: user_interpreter,
- completion: settings.completion
- });
- }
- }
- // -----------------------------------------------------------------------
- // :: Create JSON-RPC authentication function
- // -----------------------------------------------------------------------
- function make_json_rpc_login(url, login) {
- var method = $.type(login) === 'boolean' ? 'login' : login;
- return function(user, passwd, callback, term) {
- self.pause();
- $.jrpc(url,
- method,
- [user, passwd],
- function(response) {
- self.resume();
- if (!response.error && response.result) {
- callback(response.result);
- } else {
- // null will trigger message that login fail
- callback(null);
- }
- }, ajax_error);
- };
- //default name is login so you can pass true
- }
- // -----------------------------------------------------------------------
- // :: Return exception message as string
- // -----------------------------------------------------------------------
- function exception_message(e) {
- if (typeof e === 'string') {
- return e;
- } else if (typeof e.fileName === 'string') {
- return e.fileName + ': ' + e.message;
- } else {
- return e.message;
- }
- }
- // -----------------------------------------------------------------------
- // :: display Exception on terminal
- // -----------------------------------------------------------------------
- function display_exception(e, label) {
- if (typeof settings.exceptionHandler == 'function') {
- settings.exceptionHandler.call(self, e);
- } else {
- self.exception(e, label);
- }
- }
- // -----------------------------------------------------------------------
- function scroll_to_bottom() {
- var scrollHeight = scroll_object.prop ? scroll_object.prop('scrollHeight') :
- scroll_object.attr('scrollHeight');
- scroll_object.scrollTop(scrollHeight);
- }
- // -----------------------------------------------------------------------
- // :: validating if object is a string or a function, call that function
- // :: and display the exeption if any
- // -----------------------------------------------------------------------
- function validate(label, object) {
- try {
- if (typeof object === 'function') {
- object(function() {
- // don't care
- });
- } else if (typeof object !== 'string') {
- var msg = label + ' must be string or function';
- throw msg;
- }
- } catch (e) {
- display_exception(e, label.toUpperCase());
- return false;
- }
- return true;
- }
- // -----------------------------------------------------------------------
- // :: Draw line - can have line breaks and be longer than the width of
- // :: the terminal, there are 2 options raw and finalize
- // :: raw - will not encode the string and finalize if a function that
- // :: will have div container of the line as first argument
- // :: NOTE: it formats and appends lines to output_buffer. The actual
- // :: append to terminal output happens in the flush function
- // -----------------------------------------------------------------------
- var output_buffer = [];
- var NEW_LINE = 1;
- function draw_line(string, options) {
- // prevent exception in display exception
- try {
- var line_settings = $.extend({
- raw: false,
- finalize: $.noop
- }, options || {});
- string = $.type(string) === "function" ? string() : string;
- string = $.type(string) === "string" ? string : String(string);
- var i, len;
- if (!line_settings.raw) {
- string = $.terminal.encode(string);
- }
- string = $.terminal.overtyping(string);
- string = $.terminal.from_ansi(string);
- output_buffer.push(NEW_LINE);
- if (!line_settings.raw && (string.length > num_chars || string.match(/\n/))) {
- var array = $.terminal.split_equal(string, num_chars);
- for (i = 0, len = array.length; i < len; ++i) {
- if (array[i] === '' || array[i] === '\r') {
- output_buffer.push(' ');
- } else {
- if (line_settings.raw) {
- output_buffer.push(array[i]);
- } else {
- output_buffer.push($.terminal.format(array[i], {
- linksNoReferer: settings.linksNoReferer
- }));
- }
- }
- }
- } else {
- if (!line_settings.raw) {
- string = $.terminal.format(string, {
- linksNoReferer: settings.linksNoReferer
- });
- }
- output_buffer.push(string);
- }
- output_buffer.push(line_settings.finalize);
- } catch (e) {
- output_buffer = [];
- // don't display exception if exception throw in terminal
- alert('[Internal Exception(draw_line)]:' + exception_message(e) + '\n' +
- e.stack);
- }
- }
- // -----------------------------------------------------------------------
- // Redraw all lines
- // -----------------------------------------------------------------------
- function redraw() {
- command_line.resize(num_chars);
- var o = output.empty().detach();
- var lines_to_show;
- if (settings.outputLimit >= 0) {
- // flush will limit lines but if there is lot of
- // lines we don't need to show them and then remove
- // them from terminal
- var limit = settings.outputLimit === 0 ?
- self.rows() :
- settings.outputLimit;
- lines_to_show = lines.slice(lines.length-limit-1);
- } else {
- lines_to_show = lines;
- }
- $.each(lines_to_show, function(i, line) {
- draw_line.apply(null, line); // line is an array
- });
- command_line.before(o);
- self.flush();
- }
- // -----------------------------------------------------------------------
- // :: Display user greetings or terminal signature
- // -----------------------------------------------------------------------
- function show_greetings() {
- if (settings.greetings === undefined) {
- self.echo(self.signature);
- } else if (settings.greetings) {
- var type = typeof settings.greetings;
- if (type === 'string') {
- self.echo(settings.greetings);
- } else if (type === 'function') {
- settings.greetings.call(self, self.echo);
- } else {
- self.error(strings.wrongGreetings);
- }
- }
- }
- // -----------------------------------------------------------------------
- // :: Display prompt and last command
- // -----------------------------------------------------------------------
- function echo_command(command) {
- command = $.terminal.escape_brackets($.terminal.encode(command, true));
- var prompt = command_line.prompt();
- if (command_line.mask()) {
- command = command.replace(/./g, '*');
- }
- if (typeof prompt === 'function') {
- prompt(function(string) {
- self.echo(string + command);
- });
- } else {
- self.echo(prompt + command);
- }
- }
- // -----------------------------------------------------------------------
- // :: Wrapper over interpreter, it implements exit and catches all exeptions
- // :: from user code and displays them on the terminal
- // -----------------------------------------------------------------------
- function commands(command, silent, exec) {
- try {
- if (!ghost()) {
- prev_command = $.terminal.splitCommand(command).name;
- if (exec && typeof settings.historyFilter == 'function' &&
- settings.historyFilter(command) || !settings.historyFilter) {
- command_line.history().append(command);
- }
- }
- var interpreter = interpreters.top();
- if (command === 'exit' && settings.exit) {
- var count = interpreters.size();
- self.token();
- if (count == 1 && self.token() || count > 1) {
- if (!silent) {
- echo_command(command);
- }
- self.pop();
- }
- } else {
- if (!silent) {
- echo_command(command);
- }
- var position = lines.length-1;
- if (command === 'clear' && settings.clear) {
- self.clear();
- } else {
- // Execute command from the interpreter
- var result = interpreter.interpreter(command, self);
- if (result !== undefined) {
- // was lines after echo_command (by interpreter)
- if (position === lines.length-1) {
- lines.pop();
- if (result !== false) {
- self.echo(result);
- }
- } else {
- if (result === false) {
- lines = lines.slice(0, position).
- concat(lines.slice(position+1));
- } else {
- lines = lines.slice(0, position).
- concat([result]).
- concat(lines.slice(position+1));
- }
- }
- self.resize();
- }
- }
- }
- } catch (e) {
- display_exception(e, 'USER');
- self.resume();
- throw e;
- }
- }
- // -----------------------------------------------------------------------
- // :: The logout function removes Storage, disables history and runs
- // :: the login function. This function is called only when options.login
- // :: function is defined. The check for this is in the self.pop method
- // -----------------------------------------------------------------------
- function global_logout() {
- if (typeof settings.onBeforeLogout === 'function') {
- try {
- if (settings.onBeforeLogout(self) === false) {
- return;
- }
- } catch (e) {
- display_exception(e, 'onBeforeLogout');
- throw e;
- }
- }
- logout();
- if (typeof settings.onAfterLogout === 'function') {
- try {
- settings.onAfterLogout(self);
- } catch (e) {
- display_exception(e, 'onAfterlogout');
- throw e;
- }
- }
- self.login(settings.login, true, initialize);
- }
- // -----------------------------------------------------------------------
- function logout() {
- var name = self.prefix_name(true) + '_';
- $.Storage.remove(name + 'token');
- $.Storage.remove(name + 'login');
- }
- // -----------------------------------------------------------------------
- // :: Save the interpreter name for use with purge
- // -----------------------------------------------------------------------
- function maybe_append_name(interpreter_name) {
- var storage_key = self.prefix_name() + '_interpreters';
- var names = $.Storage.get(storage_key);
- if (names) {
- names = $.parseJSON(names);
- } else {
- names = [];
- }
- if ($.inArray(interpreter_name, names) == -1) {
- names.push(interpreter_name);
- $.Storage.set(storage_key, $.json_stringify(names));
- }
- }
- // -----------------------------------------------------------------------
- // :: Function enables history, sets prompt, runs interpreter function
- // -----------------------------------------------------------------------
- function prepare_top_interpreter(silent) {
- var interpreter = interpreters.top();
- var name = self.prefix_name(true);
- if (!ghost()) {
- maybe_append_name(name);
- }
- command_line.name(name);
- if (typeof interpreter.prompt == 'function') {
- command_line.prompt(function(command) {
- interpreter.prompt(command, self);
- });
- } else {
- command_line.prompt(interpreter.prompt);
- }
- command_line.set('');
- if (!silent && typeof interpreter.onStart === 'function') {
- interpreter.onStart(self);
- }
- }
- // ---------------------------------------------------------------------
- function initialize() {
- prepare_top_interpreter();
- show_greetings();
- // was_paused flag is workaround for case when user call exec before
- // login and pause in onInit, 3rd exec will have proper timing (will
- // execute after onInit resume)
- var was_paused = false;
- if (typeof settings.onInit === 'function') {
- onPause = function() { // local in terminal
- was_paused = true;
- };
- try {
- settings.onInit(self);
- } catch (e) {
- display_exception(e, 'OnInit');
- throw e;
- } finally {
- onPause = $.noop;
- if (!was_paused) {
- // resume login if user didn't call pause in onInit
- // if user pause in onInit wait with exec until it resume
- self.resume();
- }
- }
- }
- }
- // ---------------------------------------------------------------------
- // :: function complete the command
- // ---------------------------------------------------------------------
- function complete_helper(command, string, commands) {
- var test = command_line.get().substring(0, command_line.position());
- if (test !== command) {
- // command line changed between TABS - ignore
- return;
- }
- var regex = new RegExp('^' + $.terminal.escape_regex(string));
- var matched = [];
- for (var i=commands.length; i--;) {
- if (regex.test(commands[i])) {
- matched.push(commands[i]);
- }
- }
- if (matched.length === 1) {
- self.insert(matched[0].replace(regex, '') + ' ');
- } else if (matched.length > 1) {
- if (tab_count >= 2) {
- echo_command(command);
- self.echo(matched.join('\t'));
- tab_count = 0;
- } else {
- var found = false;
- var found_index;
- var j;
- loop:
- for (j=string.length; j 1 ||
- settings.login !== undefined) {
- self.pop('');
- } else {
- self.resume();
- self.echo('');
- }
- } else {
- self.set_command('');
- }
- }
- return false;
- } else if (e.which === 76 && e.ctrlKey) { // CTRL+L
- self.clear();
- } else if (completion && e.which === 9) { // TAB
- // TODO: move this to cmd plugin
- // add completion = array | function
- ++tab_count;
- // cursor can be in the middle of the command
- // so we need to get the text before the cursor
- var command = command_line.get().substring(0, command_line.position());
- var strings = command.split(' ');
- var string; // string before cursor that will be completed
- if (strings.length == 1) {
- string = strings[0];
- } else {
- string = strings[strings.length-1];
- for (i=strings.length-1; i>0; i--) {
- // treat escape space as part of the string
- if (strings[i-1][strings[i-1].length-1] == '\\') {
- string = strings[i-1] + ' ' + string;
- } else {
- break;
- }
- }
- }
- switch ($.type(completion)) {
- case 'function':
- completion(self, string, function(commands) {
- complete_helper(command, string, commands);
- });
- break;
- case 'array':
- complete_helper(command, string, completion);
- break;
- default:
- // terminal will not catch this because it's an event
- throw new Error($.terminal.defaults.strings.invalidCompletion);
- }
- return false;
- } else if (e.which === 86 && e.ctrlKey) { // CTRL+V
- self.oneTime(1, function() {
- scroll_to_bottom();
- });
- return;
- } else if (e.which === 9 && e.ctrlKey) { // CTRL+TAB
- if (terminals.length() > 1) {
- self.focus(false);
- return false;
- }
- } else if (e.which === 34) { // PAGE DOWN
- self.scroll(self.height());
- } else if (e.which === 33) { // PAGE UP
- self.scroll(-self.height());
- } else {
- self.attr({scrollTop: self.attr('scrollHeight')});
- }
- } else if (e.which === 68 && e.ctrlKey) { // CTRL+D (if paused)
- if (requests.length) {
- for (i=requests.length; i--;) {
- var r = requests[i];
- if (4 !== r.readyState) {
- try {
- r.abort();
- } catch (error) {
- self.error(strings.ajaxAbortError);
- }
- }
- }
- requests = [];
- // only resume if there are ajax calls
- self.resume();
- }
- return false;
- }
- }
- // -----------------------------------------------------------------------
- var self = this;
- if (this.length > 1) {
- return this.each(function() {
- $.fn.terminal.call($(this),
- init_interpreter,
- $.extend({name: self.selector}, options));
- });
- } else {
- // terminal already exists
- if (self.data('terminal')) {
- return self.data('terminal');
- }
- if (self.length === 0) {
- throw 'Sorry, but terminal said that "' + self.selector +
- '" is not valid selector!';
- }
- //var names = []; // stack if interpeter names
- var scroll_object;
- var prev_command; // used for name on the terminal if not defined
- var loged_in = false;
- var tab_count = 0; // for tab completion
- // array of line objects:
- // - function (called whenever necessary, result is printed)
- // - array (expected form: [line, settings])
- // - anything else (cast to string when painted)
- var lines = [];
- var output; // .terminal-output jquery object
- var terminal_id = terminals.length();
- var num_chars; // numer of chars in line
- var num_rows; // number of lines that fit without scrollbar
- var command_list = []; // for tab completion
- var url;
- var in_login = false; // some Methods should not be called when login
- // TODO: Try to use mutex like counter for pause/resume
- var onPause = $.noop; // used to indicate that user call pause onInit
- var old_width, old_height;
- var dalyed_commands = []; // used when exec commands with pause
- var settings = $.extend({},
- $.terminal.defaults,
- {name: self.selector},
- options || {});
- var strings = $.terminal.defaults.strings;
- var enabled = settings.enabled;
- var paused = false;
- // -----------------------------------------------------------------------
- // TERMINAL METHODS
- // -----------------------------------------------------------------------
- $.extend(self, $.omap({
- // -----------------------------------------------------------------------
- // :: Clear the output
- // -----------------------------------------------------------------------
- clear: function() {
- output.html('');
- command_line.set('');
- lines = [];
- try {
- settings.onClear(self);
- } catch (e) {
- display_exception(e, 'onClear');
- throw e;
- }
- self.attr({ scrollTop: 0});
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Return an object that can be used with import_view to restore the state
- // -----------------------------------------------------------------------
- export_view: function() {
- if (in_login) {
- throw new Exception(strings.notWhileLogin);
- }
- return {
- prompt: self.get_prompt(),
- command: self.get_command(),
- position: command_line.position(),
- lines: lines.slice(0)
- };
- },
- // -----------------------------------------------------------------------
- // :: Restore the state of the previous exported view
- // -----------------------------------------------------------------------
- import_view: function(view) {
- if (in_login) {
- throw new Exception(strings.notWhileLogin);
- }
- self.set_prompt(view.prompt);
- self.set_command(view.command);
- command_line.position(view.position);
- lines = view.lines;
- redraw();
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Execute a command, it will handle commands that do AJAX calls
- // :: and have delays, if the second argument is set to true it will not
- // :: echo executed command
- // -----------------------------------------------------------------------
- exec: function(command, silent) {
- // both commands executed here (resume will call Terminal::exec)
- if (paused) {
- dalyed_commands.push([command, silent]);
- } else {
- commands(command, silent, true);
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Function changes the prompt of the command line to login
- // :: with a password and calls the user login function with
- // :: the callback that expects a token. The login is successful
- // :: if the user calls it with value that is truthy
- // -----------------------------------------------------------------------
- login: function(auth, infinite, success, error) {
- if (in_login) {
- throw new Error(strings.notWhileLogin);
- }
- if (typeof auth !== 'function') {
- throw new Error(strings.loginIsNotAFunction);
- }
- if (self.token(true) && self.login_name(true)) {
- if (typeof success == 'function') {
- success();
- }
- return self;
- }
- var user = null;
- // don't store login data in history
- if (settings.history) {
- command_line.history().disable();
- }
- in_login = true;
- return self.push(function(user) {
- self.set_mask(true).push(function(pass) {
- try {
- auth.call(self, user, pass, function(token, silent) {
- if (token) {
- self.pop().pop();
- if (settings.history) {
- command_line.history().enable();
- }
- var name = self.prefix_name(true) + '_';
- $.Storage.set(name + 'token', token);
- $.Storage.set(name + 'login', user);
- in_login = false;
- if (typeof success == 'function') {
- // will be used internaly since users know
- // when login success (they decide when
- // it happen by calling the callback -
- // this funtion)
- success();
- }
- } else {
- if (infinite) {
- if (!silent) {
- self.error(strings.wrongPasswordTryAgain);
- }
- self.pop().set_mask(false);
- } else {
- in_login = false;
- if (!silent) {
- self.error(strings.wrongPassword);
- }
- self.pop().pop();
- }
- // used only to call pop in push
- if (typeof error == 'function') {
- error();
- }
- }
- });
- } catch(e) {
- display_exception(e, 'USER(authentication)');
- }
- }, {
- prompt: strings.password + ': '
- });
- }, {
- prompt: strings.login + ': '
- });
- },
- // -----------------------------------------------------------------------
- // :: User defined settings and defaults as well
- // -----------------------------------------------------------------------
- settings: settings,
- // -----------------------------------------------------------------------
- // :: Return commands function from top interpreter
- // -----------------------------------------------------------------------
- commands: function() {
- return interpreters.top().interpreter;
- },
- // -----------------------------------------------------------------------
- // :: Low Level method that overwrites interpreter
- // -----------------------------------------------------------------------
- setInterpreter: function(user_interpreter, login) {
- function overwrite_interpreter() {
- self.pause();
- make_interpreter(user_interpreter, function(result) {
- self.resume();
- var top = interpreters.top();
- $.extend(top, result);
- prepare_top_interpreter(true);
- });
- }
- if ($.type(user_interpreter) == 'string' && login) {
- self.login(make_json_rpc_login(user_interpreter, login),
- true,
- overwrite_interpreter);
- } else {
- overwrite_interpreter();
- }
- },
- // -----------------------------------------------------------------------
- // :: Show user greetings or terminal signature
- // -----------------------------------------------------------------------
- greetings: function() {
- show_greetings();
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Return true if terminal is paused false otherwise
- // -----------------------------------------------------------------------
- paused: function() {
- return paused;
- },
- // -----------------------------------------------------------------------
- // :: Pause the terminal, it should be used for ajax calls
- // -----------------------------------------------------------------------
- pause: function() {
- onPause();
- if (!paused && command_line) {
- paused = true;
- self.disable();
- command_line.hidden();
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Resume the previously paused terminal
- // -----------------------------------------------------------------------
- resume: function() {
- if (paused && command_line) {
- paused = false;
- self.enable();
- command_line.visible();
- var original = dalyed_commands;
- dalyed_commands = [];
- while (original.length) {
- self.exec.apply(self, original.shift());
- }
- scroll_to_bottom();
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Return the number of characters that fit into the width of the terminal
- // -----------------------------------------------------------------------
- cols: function() {
- return num_chars;
- },
- // -----------------------------------------------------------------------
- // :: Return the number of lines that fit into the height of the terminal
- // -----------------------------------------------------------------------
- rows: function() {
- return num_rows;
- },
- // -----------------------------------------------------------------------
- // :: Return the History object
- // -----------------------------------------------------------------------
- history: function() {
- return command_line.history();
- },
- // -----------------------------------------------------------------------
- // :: Switch to the next terminal
- // -----------------------------------------------------------------------
- next: function() {
- if (terminals.length() === 1) {
- return self;
- } else {
- var offsetTop = self.offset().top;
- var height = self.height();
- var scrollTop = self.scrollTop();
- if (!is_scrolled_into_view(self)) {
- self.enable();
- $('html,body').animate({scrollTop: offsetTop-50}, 500);
- return self;
- } else {
- terminals.front().disable();
- var next = terminals.rotate().enable();
- // 100 provides buffer in viewport
- var x = next.offset().top - 50;
- $('html,body').animate({scrollTop: x}, 500);
- try {
- settings.onTerminalChange(next);
- } catch (e) {
- display_exception(e, 'onTerminalChange');
- throw e;
- }
- return next;
- }
- }
- },
- // -----------------------------------------------------------------------
- // :: Make the terminal in focus or blur depending on the first argument.
- // :: If there is more then one terminal it will switch to next one,
- // :: if the second argument is set to true the events will be not fired.
- // :: Used on init
- // -----------------------------------------------------------------------
- focus: function(toggle, silent) {
- self.oneTime(1, function() {
- if (terminals.length() === 1) {
- if (toggle === false) {
- try {
- if (!silent && settings.onBlur(self) !== false) {
- self.disable();
- }
- } catch (e) {
- display_exception(e, 'onBlur');
- throw e;
- }
- } else {
- try {
- if (!silent && settings.onFocus(self) !== false) {
- self.enable();
- }
- } catch (e) {
- display_exception(e, 'onFocus');
- throw e;
- }
- }
- } else {
- if (toggle === false) {
- self.next();
- } else {
- var front = terminals.front();
- if (front != self) {
- front.disable();
- if (!silent) {
- try {
- settings.onTerminalChange(self);
- } catch (e) {
- display_exception(e, 'onTerminalChange');
- throw e;
- }
- }
- }
- terminals.set(self);
- self.enable();
- }
- }
- });
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Enable the terminal
- // -----------------------------------------------------------------------
- enable: function() {
- if (num_chars === undefined) {
- //enabling first time
- self.resize();
- }
- if (command_line) {
- command_line.enable();
- enabled = true;
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Disable the terminal
- // -----------------------------------------------------------------------
- disable: function() {
- if (command_line) {
- enabled = false;
- command_line.disable();
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: return true if the terminal is enabled
- // -----------------------------------------------------------------------
- enabled: function() {
- return enabled;
- },
- // -----------------------------------------------------------------------
- // :: Return the terminal signature depending on the size of the terminal
- // -----------------------------------------------------------------------
- signature: function() {
- var cols = self.cols();
- var i = cols < 15 ? null : cols < 35 ? 0 : cols < 55 ? 1 : cols < 64 ? 2 : cols < 75 ? 3 : 4;
- if (i !== null) {
- return signatures[i].join('\n') + '\n';
- } else {
- return '';
- }
- },
- // -----------------------------------------------------------------------
- // :: Return the version number
- // -----------------------------------------------------------------------
- version: function() {
- return version;
- },
- // -----------------------------------------------------------------------
- // :: Return actual command line object (jquery object with cmd methods)
- // -----------------------------------------------------------------------
- cmd: function() {
- return command_line;
- },
- // -----------------------------------------------------------------------
- // :: Return the current command entered by terminal
- // -----------------------------------------------------------------------
- get_command: function() {
- return command_line.get();
- },
- // -----------------------------------------------------------------------
- // :: Change the command line to the new one
- // -----------------------------------------------------------------------
- set_command: function(command) {
- command_line.set(command);
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Insert text into the command line after the cursor
- // -----------------------------------------------------------------------
- insert: function(string) {
- if (typeof string === 'string') {
- command_line.insert(string);
- return self;
- } else {
- throw "insert function argument is not a string";
- }
- },
- // -----------------------------------------------------------------------
- // :: Set the prompt of the command line
- // -----------------------------------------------------------------------
- set_prompt: function(prompt) {
- if (validate('prompt', prompt)) {
- if (typeof prompt == 'function') {
- command_line.prompt(function(command) {
- prompt(command, self);
- });
- } else {
- command_line.prompt(prompt);
- }
- interpreters.top().prompt = prompt;
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Return the prompt used by the terminal
- // -----------------------------------------------------------------------
- get_prompt: function() {
- return interpreters.top().prompt;
- // command_line.prompt(); - can be a wrapper
- //return command_line.prompt();
- },
- // -----------------------------------------------------------------------
- // :: Enable or Disable mask depedning on the passed argument
- // -----------------------------------------------------------------------
- set_mask: function(display) {
- command_line.mask(display);
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Return the ouput of the terminal as text
- // -----------------------------------------------------------------------
- get_output: function(raw) {
- if (raw) {
- return lines;
- } else {
- return $.map(lines, function(item) {
- return typeof item[0] == 'function' ? item[0]() : item[0];
- }).join('\n');
- }
- },
- // -----------------------------------------------------------------------
- // :: Recalculate and redraw everything
- // -----------------------------------------------------------------------
- resize: function(width, height) {
- if (width && height) {
- self.width(width);
- self.height(height);
- }
- width = self.width();
- height = self.height();
- var new_num_chars = get_num_chars(self);
- var new_num_rows = get_num_rows(self);
- // only if number of chars changed
- if (new_num_chars !== num_chars || new_num_rows !== num_rows) {
- num_chars = new_num_chars;
- num_rows = new_num_rows;
- redraw();
- if (typeof settings.onResize === 'function' &&
- (old_height !== height || old_width !== width)) {
- settings.onResize(self);
- }
- if (old_height !== height || old_width !== width) {
- old_height = height;
- old_width = width;
- }
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Flush the output to the terminal
- // -----------------------------------------------------------------------
- flush: function() {
- try {
- var wrapper;
- // print all lines
- $.each(output_buffer, function(i, line) {
- if (line === NEW_LINE) {
- wrapper = $('');
- } else if (typeof line === 'function') {
- wrapper.appendTo(output);
- try {
- line(wrapper);
- } catch (e) {
- display_exception(e, 'USER:echo(finalize)');
- }
- } else {
- $('').html(line).appendTo(wrapper).width('100%');
- }
- });
- if (settings.outputLimit >= 0) {
- var limit = settings.outputLimit === 0 ?
- self.rows() :
- settings.outputLimit;
- var $lines = output.find('div div');
- if ($lines.length > limit) {
- var for_remove = $lines.slice(0, lines.length-limit+1);
- // you can't get parent if you remove the element so
- // we first get the parent
- var parents = for_remove.parent();
- for_remove.remove();
- parents.each(function() {
- var self = $(this);
- if (self.is(':empty')) {
- self.remove();
- }
- });
- }
- }
- scroll_to_bottom();
- output_buffer = [];
- } catch (e) {
- alert('[Flush] ' + exception_message(e) + '\n' +
- e.stack);
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Print data to the terminal output. It can have two options:
- // :: a function that is called with the container div that holds the
- // :: output (as a jquery object) every time the output is printed
- // :: (including resize and scrolling)
- // :: If the line is a function it will be called for every redraw.
- // -----------------------------------------------------------------------
- echo: function(string, options) {
- try {
- string = string || '';
- var settings = $.extend({
- flush: true,
- raw: false,
- finalize: $.noop
- }, options || {});
- output_buffer = [];
- draw_line(string, settings);
- lines.push([string, settings]);
- if (settings.flush) {
- self.flush();
- }
- on_scrollbar_show_resize();
- } catch (e) {
- // if echo throw exception we can't use error to display that
- // exception
- alert('[Terminal.echo] ' + exception_message(e) + '\n' +
- e.stack);
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: echo red text
- // -----------------------------------------------------------------------
- error: function(message, finalize) {
- //quick hack to fix trailing back slash
- return self.echo('[[;#f00;]' + $.terminal.escape_brackets(message).
- replace(/\\$/, '\') + ']', finalize);
- },
- // -----------------------------------------------------------------------
- // :: Display Exception on terminal
- // -----------------------------------------------------------------------
- exception: function(e, label) {
- var message = exception_message(e);
- if (label) {
- message = '[' + label + ']: ' + message;
- }
- if (message) {
- self.error(message, {
- finalize: function(div) {
- div.addClass('exception message');
- }
- });
- }
- if (typeof e.fileName === 'string') {
- //display filename and line which throw exeption
- self.pause();
- $.get(e.fileName, function(file) {
- self.resume();
- var num = e.lineNumber - 1;
- var line = file.split('\n')[num];
- if (line) {
- self.error('[' + e.lineNumber + ']: ' + line);
- }
- });
- }
- if (e.stack) {
- self.error(e.stack, {
- finalize: function(div) {
- div.addClass('exception stack-trace');
- }
- });
- }
- },
- // -----------------------------------------------------------------------
- // :: Scroll Div that holds the terminal
- // -----------------------------------------------------------------------
- scroll: function(amount) {
- var pos;
- amount = Math.round(amount);
- if (scroll_object.prop) { // work with jQuery > 1.6
- if (amount > scroll_object.prop('scrollTop') && amount > 0) {
- scroll_object.prop('scrollTop', 0);
- }
- pos = scroll_object.prop('scrollTop');
- scroll_object.scrollTop(pos + amount);
- } else {
- if (amount > scroll_object.attr('scrollTop') && amount > 0) {
- scroll_object.attr('scrollTop', 0);
- }
- pos = scroll_object.attr('scrollTop');
- scroll_object.scrollTop(pos + amount);
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Exit all interpreters and logout. The function will throw exception
- // :: if there is no login provided
- // -----------------------------------------------------------------------
- logout: settings.login ? function() {
- while (interpreters.size() > 1) {
- self.pop();
- }
- return self.pop();
- } : function() {
- self.error(strings.loginFunctionMissing);
- },
- // -----------------------------------------------------------------------
- // :: Function returns the token returned by callback function in login
- // :: function. It does nothing (return undefined) if there is no login
- // -----------------------------------------------------------------------
- token: settings.login ? function(local) {
- return $.Storage.get(self.prefix_name(local) + '_token');
- } : $.noop,
- // -----------------------------------------------------------------------
- // :: Function return Login name entered by the user
- // -----------------------------------------------------------------------
- login_name: settings.login ? function(local) {
- return $.Storage.get(self.prefix_name(local) + '_login');
- } : $.noop,
- // -----------------------------------------------------------------------
- // :: Function returns the name of current interpreter
- // -----------------------------------------------------------------------
- name: function() {
- return interpreters.top().name;
- },
- // -----------------------------------------------------------------------
- // :: Function return prefix name for login/token
- // -----------------------------------------------------------------------
- prefix_name: function(local) {
- var name = (settings.name ? settings.name + '_' : '') + terminal_id;
- if (local && interpreters.size() > 1) {
- name += '_' + interpreters.map(function(intrp) {
- return intrp.name;
- }).slice(1).join('_');
- }
- return name;
- },
- // -----------------------------------------------------------------------
- // :: Push a new interenter on the Stack
- // -----------------------------------------------------------------------
- push: function(interpreter, options) {
- options = options || {};
- options.name = options.name || prev_command;
- options.prompt = options.prompt || options.name + ' ';
- //names.push(options.name);
- var top = interpreters.top();
- if (top) {
- top.mask = command_line.mask();
- }
- make_interpreter(interpreter, function(result) {
- // result is object with interpreter and completion properties
- interpreters.push($.extend({}, result, options));
- if (options.login) {
- var type = $.type(options.login);
- if (type == 'function') {
- // self.pop on error
- self.login(options.login,
- false,
- prepare_top_interpreter,
- self.pop);
- } else if ($.type(interpreter) == 'string' &&
- type == 'string' || type == 'boolean') {
- self.login(make_json_rpc_login(interpreter, options.login),
- false,
- prepare_top_interpreter,
- self.pop);
- }
- } else {
- prepare_top_interpreter();
- }
- });
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Remove the last interpreter from the Stack
- // -----------------------------------------------------------------------
- pop: function(string) {
- if (string !== undefined) {
- echo_command(string);
- }
- var token = self.token(true);
- if (interpreters.size() == 1) {
- if (settings.login) {
- global_logout();
- if ($.type(settings.onExit) === 'function') {
- try {
- settings.onExit(self);
- } catch (e) {
- display_exception(e, 'onExit');
- throw e;
- }
- }
- } else {
- self.error(strings.canExitError);
- }
- } else {
- if (token) {
- logout();
- }
- var current = interpreters.pop();
- prepare_top_interpreter();
- if ($.type(current.onExit) === 'function') {
- try {
- current.onExit(self);
- } catch (e) {
- display_exception(e, 'onExit');
- throw e;
- }
- }
- // restore mask
- self.set_mask(interpreters.top().mask);
- }
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Return how deep you are in nested interpreters
- // -----------------------------------------------------------------------
- level: function() {
- return interpreters.size();
- },
- // -----------------------------------------------------------------------
- // :: Reinitialize the terminal
- // -----------------------------------------------------------------------
- reset: function() {
- self.clear();
- while(interpreters.size() > 1) {
- interpreters.pop();
- }
- initialize();
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Remove all local storage left by terminal, it will not logout you
- // :: until you refresh the browser
- // -----------------------------------------------------------------------
- purge: function() {
- var prefix = self.prefix_name() + '_';
- var names = $.Storage.get(prefix + 'interpreters');
- $.each($.parseJSON(names), function(_, name) {
- $.Storage.remove(name + '_commands');
- $.Storage.remove(name + '_token');
- $.Storage.remove(name + '_login');
- });
- command_line.purge();
- $.Storage.remove(prefix + 'interpreters');
- return self;
- },
- // -----------------------------------------------------------------------
- // :: Remove all events and DOM nodes left by terminal, it will not purge
- // :: the terminal so you will have the same state when you refresh the
- // :: browser
- // -----------------------------------------------------------------------
- destroy: function() {
- command_line.destroy().remove();
- output.remove();
- $(document).unbind('.terminal');
- $(window).unbind('.terminal');
- self.unbind('click, mousewheel');
- self.removeData('terminal').removeClass('terminal');
- if (settings.width) {
- self.css('width', '');
- }
- if (settings.height) {
- self.css('height', '');
- }
- return self;
- }
- }, function(_, fun) {
- // wrap all functions and display execptions
- return function() {
- try {
- return fun.apply(this, Array.prototype.slice.apply(arguments));
- } catch (e) {
- // exec catch by command (resume call exec)
- if (_ !== 'exec' && _ !== 'resume') {
- display_exception(e, 'TERMINAL');
- }
- throw e;
- }
- };
- }));
-
- // ---------------------------------------------------------------------
- var on_scrollbar_show_resize = (function() {
- var scroll_bars = have_scrollbars(self);
- return function() {
- if (scroll_bars !== have_scrollbars(self)) {
- // if the scollbar appearance changes we will have a different
- // number of chars
- self.resize();
- scroll_bars = have_scrollbars(self);
- }
- };
- })();
-
- // ---------------------------------------------------------------------
- // INIT CODE
- // ---------------------------------------------------------------------
- if (settings.width) {
- self.width(settings.width);
- }
- if (settings.height) {
- self.height(settings.height);
- }
- if (!navigator.userAgent.toLowerCase().match(/(webkit)[ \/]([\w.]+)/) &&
- self[0].tagName.toLowerCase() == 'body') {
- scroll_object = $('html');
- } else {
- scroll_object = self;
- }
- // register ajaxSend for cancel requests on CTRL+D
- $(document).bind('ajaxSend.terminal', function(e, xhr, opt) {
- requests.push(xhr);
- });
- output = $('
').addClass('terminal-output').appendTo(self);
- self.addClass('terminal');
- // keep focus in clipboard textarea in mobile
- if (('ontouchstart' in window) || window.DocumentTouch &&
- document instanceof DocumentTouch) {
- self.click(function() {
- self.find('textarea').focus();
- });
- self.find('textarea').focus();
- }
- /*
- self.bind('touchstart.touchScroll', function() {
-
- });
- self.bind('touchmove.touchScroll', function() {
-
- });
- */
- //$('').hide().focus().appendTo(self);
-
- // before login event
- if (settings.login && typeof settings.onBeforeLogin === 'function') {
- try {
- settings.onBeforeLogin(self);
- } catch (e) {
- display_exception(e, 'onBeforeLogin');
- throw e;
- }
- }
- // create json-rpc authentication function
- if (typeof init_interpreter === 'string' &&
- (typeof settings.login === 'string' || settings.login)) {
- settings.login = make_json_rpc_login(init_interpreter, settings.login);
- }
- terminals.append(self);
- var interpreters;
- var command_line;
- make_interpreter(init_interpreter, function(interpreter) {
- interpreters = new Stack($.extend({
- name: settings.name,
- prompt: settings.prompt,
- greetings: settings.greetings
- }, interpreter));
- // CREATE COMMAND LINE
- command_line = $('').appendTo(self).cmd({
- prompt: settings.prompt,
- history: settings.history,
- historyFilter: settings.historyFilter,
- historySize: settings.historySize,
- width: '100%',
- keydown: key_down,
- keypress: settings.keypress ? function(e) {
- return settings.keypress(e, self);
- } : null,
- onCommandChange: function(command) {
- if ($.type(settings.onCommandChange) === 'function') {
- try {
- settings.onCommandChange(command, self);
- } catch (e) {
- display_exception(e, 'onCommandChange');
- throw e;
- }
- }
- scroll_to_bottom();
- },
- commands: commands
- });
- if (enabled) {
- self.focus(undefined, true);
- } else {
- self.disable();
- }
- $(document).bind('click.terminal', function(e) {
- if (!$(e.target).closest('.terminal').hasClass('terminal') &&
- settings.onBlur(self) !== false) {
- self.disable();
- }
- });
- self.click(function(e) {
- if (!self.enabled()) {
- self.focus();
- }
- }).mousedown(function(e) {
- if (e.which == 2) {
- self.insert(getSelectedText());
- }
- });
- // ------------------------------------------------
- // Run Login
- if (settings.login) {
- self.login(settings.login, true, initialize);
- } else {
- initialize();
- }
- if (self.is(':visible')) {
- num_chars = get_num_chars(self);
- command_line.resize(num_chars);
- num_rows = get_num_rows(self);
- }
- self.oneTime(100, function() {
- $(window).bind('resize.terminal', function() {
- if (self.is(':visible')) {
- var width = self.width();
- var height = self.height();
- // prevent too many calculations in IE
- if (old_height !== height || old_width !== width) {
- self.resize();
- }
- }
- });
- });
- if ($.event.special.mousewheel) {
- var shift = false;
- $(document).bind('keydown.terminal', function(e) {
- if (e.shiftKey) {
- shift = true;
- }
- }).bind('keyup.terminal', function(e) {
- // in Google Chromium/Linux shiftKey is false
- if (e.shiftKey || e.which == 16) {
- shift = false;
- }
- });
- self.mousewheel(function(event, delta) {
- if (!shift) {
- if (delta > 0) {
- self.scroll(-40);
- } else {
- self.scroll(40);
- }
- //event.preventDefault();
- }
- });
- }
- });
- self.data('terminal', self);
- return self;
- }
- }; //terminal plugin
-})(jQuery);
diff --git a/contrib/online_ide/www/js/pnacl_js.js b/contrib/online_ide/www/js/pnacl_js.js
deleted file mode 100644
index 1ebcd0b..0000000
--- a/contrib/online_ide/www/js/pnacl_js.js
+++ /dev/null
@@ -1,72 +0,0 @@
-// This file is part of pnacl_nit ( http://www.pnacl_nitlanguage.org )
-//
-// Copyright 2014 Johan Kayser
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-pnacl_nitModule = null; // Global application object.
-var github_acces_token = '9d9412ce7f8223348287aac676b2835baa8f0cfc'; // Github API token.
-var lib_files = []; // Stores the Nit lib files in JS.
-var lib_files_number = 0;
-var sha_filename_map = {}; // Map that stores files with their 'sha' as key and 'name' as value
-var lib_files_loaded = false;
-var terminal = null; //terminal object.
-
-// Indicate load success.
-function moduleDidLoad() {
- pnacl_nitModule = document.getElementById('pnacl_nit');
- if (pnacl_nitModule != null) {
- // When the module is loaded, we load the lib and init the terminal.
- console.log('pnacl_nitModule loaded.');
- load_nit_lib();
- init_terminal();
- }
-}
-
-// The 'message' event handler. This handler is fired when the NaCl module
-// posts a message to the browser by calling PPB_Messaging.PostMessage()
-// (in C) or pp::Instance.PostMessage() (in C++). This implementation
-// displays the result in the JS console, puts the result in the '#rez' input and make it visible.
-function handleMessage(message_event) {
- if (message_event.data.hasOwnProperty('exit')){
- // If the code exited, shows the error code and reloads Nit lib after the launch of a new thread.
- console.log('Nit code exited with value: ' + message_event.data.exit + '.');
- console.log(message_event.data.exit_thread);
- load_nit_lib();
- }
- else if (!message_event.data.hasOwnProperty('operation'))
- {
- if(message_event.data == "\n") {
- // Do nothing.
- }
- else {
- terminal.echo(message_event.data);
- }
- }
- else if (message_event.data.operation == 'load_response'){
- // We use this to count how many files were loaded for the lib in PNaCl,
- // if the number matches the number of files loaded from Github,
- // we know we have all the files.
- if (message_event.data.files_number == lib_files_number) {
- console.log('Nit library loaded (' + message_event.data.files_number + ' files), ready to go.');
- lib_files_loaded = true;
- }
- }
-}
-
-// Called if the application calls exit(n), abort(), or crashes.
-function handleCrash() {
- // We remove the modules and add a new one.
- $( "#pnacl_nit" ).remove();
- $( "#listener" ).append("");
-}
diff --git a/contrib/online_ide/www/pnacl/pnacl_nit.nmf b/contrib/online_ide/www/pnacl/pnacl_nit.nmf
deleted file mode 100644
index a252fef..0000000
--- a/contrib/online_ide/www/pnacl/pnacl_nit.nmf
+++ /dev/null
@@ -1,10 +0,0 @@
-{
- "program": {
- "portable": {
- "pnacl-translate": {
- "url": "pnacl_nit.pexe"
- }
- }
- }
-}
-
diff --git a/lib/core/text/native.nit b/lib/core/text/native.nit
index 0469a7d..f23b604 100644
--- a/lib/core/text/native.nit
+++ b/lib/core/text/native.nit
@@ -27,10 +27,6 @@ in "C" `{
#define be32toh(val) _byteswap_ulong(val)
#endif
-#ifdef __pnacl__
- #define be16toh(val) (((val) >> 8) | ((val) << 8))
- #define be32toh(val) ((be16toh((val) << 16) | (be16toh((val) >> 16))))
-#endif
#ifndef be32toh
#define be32toh(val) betoh32(val)
#endif
diff --git a/lib/pnacl/examples/converter/Makefile b/lib/pnacl/examples/converter/Makefile
deleted file mode 100644
index 8c9dbd7..0000000
--- a/lib/pnacl/examples/converter/Makefile
+++ /dev/null
@@ -1,8 +0,0 @@
-default:
- ../../../bin/nitc --semi-global converter.nit
-
-HTTPD_PY := python $(NACL_SDK_ROOT)/tools/httpd.py
-serve:
- $(HTTPD_PY) -C . --no-dir-check
-
-.PHONY: serve
diff --git a/lib/pnacl/examples/converter/README.md b/lib/pnacl/examples/converter/README.md
deleted file mode 100644
index bc2a555..0000000
--- a/lib/pnacl/examples/converter/README.md
+++ /dev/null
@@ -1,22 +0,0 @@
-Steps to make the example work :
-
-1. Install the native client SDK (https://developer.chrome.com/native-client/sdk/download).
-
-2. Declare the environment variable NACL_SDK_ROOT as the root of the target platform within the SDK (ex: ~/nacl_sdk/pepper_34/) :
- $ export NACL_SDK_ROOT=/path/to/nacl_sdk/pepper_[your_version]
-
-3. Compile the Nit code with: `nitc --semi-global converter.nit` or `make`.
-
-You must use the '--semi-global' (or `--global`) option. Some features in the standard library are not supported by the NaCL platform, the global compiler do not try to compile them.
-
-4. Start a local server using: `make serve`.
-
-5. Set up the Chrome browser :
- - PNaCl is enabled by default in Chrome version 31 and later.
- - For a better development experience, itâs also recommended to disable the Chrome cache :
- - Open Chromeâs developer tools by clicking the menu icon menu-icon and choosing Tools > Developer tools.
- - Click the gear icon gear-icon in the bottom right corner of the Chrome window.
- - Under the âGeneralâ settings, check the box next to âDisable cache (while DevTools is open)â.
- - Keep the Developer Tools pane open while developing Native Client applications.
-
-6. You can now access the application in Chrome at the address `http://localhost:5103/`.
diff --git a/lib/pnacl/examples/converter/converter.nit b/lib/pnacl/examples/converter/converter.nit
deleted file mode 100644
index cda360e..0000000
--- a/lib/pnacl/examples/converter/converter.nit
+++ /dev/null
@@ -1,128 +0,0 @@
-# This file is part of NIT ( http://www.nitlanguage.org )
-#
-# Copyright 2014 Johan Kayser
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-# Example of a currency converter using PNaCl with Nit
-#
-# First imports the pnacl module
-# Then redefines the 'handle_dictionary' method
-#Â Creates a converter and initializes it
-#Â Finally checks for dictionaries
-
-import pnacl is example
-
-class Converter
- super PnaclApp
-
- #Â We have to redefine the method to do what we want when receiving a dictionary from JS
- redef fun handle_dictionary(dictionary) do
- #Â Getting values from the dictionary
- var from = dictionary["from"]
- var to = dictionary["to"]
- var value = dictionary["value"]
-
- #Â If conversion to the same currency return the same value
- if from isa String and to isa String then
- if from == "EUR" and to == "EUR" then
- if value isa String then
- var rez = value.to_f
- var d = new PepperDictionary
- d["value"] = rez
- post_dictionary d
- end
- end
- end
- #Â If conversion to the same currency return the same value
- if from isa String and to isa String then
- if from == "CAD" and to == "CAD" then
- if value isa String then
- var rez = value.to_f
- var d = new PepperDictionary
- d["value"] = rez
- post_dictionary d
- end
- end
- end
- #Â If conversion to the same currency return the same value
- if from isa String and to isa String then
- if from == "USD" and to == "USD" then
- if value isa String then
- var rez = value.to_f
- var d = new PepperDictionary
- d["value"] = rez
- post_dictionary d
- end
- end
- end
- #Â Converts EUR to CAD
- if from == "EUR" and to == "CAD" then
- if value isa String then
- var rez = (value.to_f * 1.52) # April 30 2014
- var d = new PepperDictionary
- d["value"] = rez
- post_dictionary d
- end
- end
- #Â Converts EUR to USD
- if from == "EUR" and to == "USD" then
- if value isa String then
- var rez = (value.to_f * 1.38640) # April 30 2014
- var d = new PepperDictionary
- d["value"] = rez
- post_dictionary d
- end
- end
- #Â Converts CAD to EUR
- if from == "CAD" and to == "EUR" then
- if value isa String then
- var rez = (value.to_f * 0.65840) # April 30 2014
- var d = new PepperDictionary
- d["value"] = rez
- post_dictionary d
- end
- end
- #Â Converts CAD to USD
- if from == "CAD" and to == "USD" then
- if value isa String then
- var rez = (value.to_f * 0.91240) # April 30 2014
- var d = new PepperDictionary
- d["value"] = rez
- post_dictionary d
- end
- end
- #Â Converts USD to EUR
- if from == "USD" and to == "EUR" then
- if value isa String then
- var rez = (value.to_f * 0.721) # April 30 2014
- var d = new PepperDictionary
- d["value"] = rez
- post_dictionary d
- end
- end
- #Â Converts USD to CAD
- if from == "USD" and to == "CAD" then
- if value isa String then
- var rez = (value.to_f * 1.095) # April 30 2014
- var d = new PepperDictionary
- d["value"] = rez
- post_dictionary d
- end
- end
- end
-end
-
-redef fun app do return once new Converter
-app.initialize # Needed to correctly set up Nit control over the Pepper API
-app.run # Launches an infinite loop in order to check for dictionaries
diff --git a/lib/pnacl/examples/converter/converter/index.html b/lib/pnacl/examples/converter/converter/index.html
deleted file mode 100644
index 4143033..0000000
--- a/lib/pnacl/examples/converter/converter/index.html
+++ /dev/null
@@ -1,89 +0,0 @@
-
-
-
-
- Converter
-
-
-
-
-
Currency converter with PNaCl and Nit
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/lib/pnacl/examples/converter/converter/js/functions.js b/lib/pnacl/examples/converter/converter/js/functions.js
deleted file mode 100644
index cd7f506..0000000
--- a/lib/pnacl/examples/converter/converter/js/functions.js
+++ /dev/null
@@ -1,35 +0,0 @@
-// This file is part of NIT ( http://www.nitlanguage.org )
-//
-// Copyright 2014 Johan Kayser
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-// Checks that the text in the input is numeric and not null
-// and then if two currencies where chosen sends a dictionary to Nit.
-$( "#button" ).click(function() {
- $("#pre-input").removeClass("has-error");
- if (($('#input').val() == "")||($.isNumeric($('#input').val()) == false)) {
- $("#pre-input").addClass("has-error");
- }
- else
- {
- if (($('#from').val() != null) && ($('#to').val() != null)) {
- var dictionary = {
- value: parseFloat($('#input').val()).toFixed(2),
- from: $('#from').val(),
- to: $('#to').val(),
- }
- converterModule.postMessage(dictionary);
- }
- }
-});
diff --git a/lib/pnacl/examples/converter/converter/js/pnacl_js.js b/lib/pnacl/examples/converter/converter/js/pnacl_js.js
deleted file mode 100644
index da29a31..0000000
--- a/lib/pnacl/examples/converter/converter/js/pnacl_js.js
+++ /dev/null
@@ -1,41 +0,0 @@
-// This file is part of NIT ( http://www.nitlanguage.org )
-//
-// Copyright 2014 Johan Kayser
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-converterModule = null; // Global application object.
-
-// Indicate load success.
-function moduleDidLoad() {
- converterModule = document.getElementById('converter');
- // Send a message to the Native Client module.
- if (converterModule != null) {
- console.log('converterModule loaded.');
- }
-}
-
-// The 'message' event handler. This handler is fired when the NaCl module
-// posts a message to the browser by calling PPB_Messaging.PostMessage()
-// (in C) or pp::Instance.PostMessage() (in C++). This implementation
-// displays the result in the JS console, puts the result in the '#rez' input and make it visible.
-function handleMessage(message_event) {
- if (message_event.data.hasOwnProperty('exit')){
- console.log('Nit code exited with value: ' + message_event.data.exit + '.');
- }
- else {
- console.log(message_event.data.value.valueOf());
- $('#rez').val(message_event.data.value.valueOf().toFixed(2));
- $('#rez').css('visibility', 'visible');
- }
-}
diff --git a/lib/pnacl/package.ini b/lib/pnacl/package.ini
deleted file mode 100644
index c3e650b..0000000
--- a/lib/pnacl/package.ini
+++ /dev/null
@@ -1,11 +0,0 @@
-[package]
-name=pnacl
-tags=platform,lib
-maintainer=Johan Kayser
-license=Apache-2.0
-[upstream]
-browse=https://github.com/nitlang/nit/tree/master/lib/pnacl
-git=https://github.com/nitlang/nit.git
-git.directory=lib/pnacl
-homepage=http://nitlanguage.org
-issues=https://github.com/nitlang/nit/issues
diff --git a/lib/pnacl/pnacl.nit b/lib/pnacl/pnacl.nit
deleted file mode 100644
index 71c87e4..0000000
--- a/lib/pnacl/pnacl.nit
+++ /dev/null
@@ -1,763 +0,0 @@
-# This file is part of NIT ( http://www.nitlanguage.org ).
-#
-# Copyright 2014 Johan Kayser
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-# Provides PNaCl support for Nit.
-#
-# To use this module and compile for PNaCl, you must install the
-# NaCl SDK (This file is based on Pepper 33).
-# If NACL_SDK_ROOT is not set in your PATH, you have to work in
-# 'nacl_sdk/pepper_your_pepper_version/getting_started/your_project_folder'.
-module pnacl is platform
-
-import core
-intrude import core::stream
-
-in "C Header" `{
- #include "ppapi/c/pp_errors.h"
- #include "ppapi/c/ppp.h"
- #include "ppapi/c/ppp_instance.h"
- #include "ppapi/c/pp_bool.h"
- #include "ppapi/c/ppb_var.h"
- #include "ppapi/c/ppb_messaging.h"
- #include "ppapi/c/ppp_messaging.h"
- #include "ppapi/c/ppb_var_dictionary.h"
- #include "ppapi/c/ppb_var_array.h"
-`}
-
-`{
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- #define MAX_DICTIONARY_QUEUE_SIZE 200
- #define MAX_MESSAGE_QUEUE_SIZE 10
-
- extern int nit_main(int, char**);
-
- /* A working thread for Nit. */
- static pthread_t g_nit_thread;
-
- /* Mutex that guards the queues. */
- static pthread_mutex_t g_dictionary_queue_mutex;
- static pthread_mutex_t g_message_queue_mutex;
-
- /* Condition variables that are signalled when the queues are not empty. */
- static pthread_cond_t g_dictionary_queue_not_empty_cond;
- static pthread_cond_t g_message_queue_not_empty_cond;
-
- /** Circular queues of dictionaries and messages from JavaScript to be handled.
- *
- * If g_queue_start < g_queue_end:
- * all elements in the range [g_queue_start, g_queue_end) are valid.
- * If g_queue_start > g_queue_end:
- * all elements in the ranges [0, g_queue_end) and
- * [g_queue_start, MAX_QUEUE_SIZE) are valid.
- * If g_queue_start == g_queue_end, and g_queue_size > 0:
- * all elements in the g_queue are valid.
- * If g_queue_start == g_queue_end, and g_queue_size == 0:
- * No elements are valid. */
- static struct PP_Var g_dictionary_queue[MAX_DICTIONARY_QUEUE_SIZE];
- static char* g_message_queue[MAX_MESSAGE_QUEUE_SIZE];
-
- /* The index of the head of the queues. */
- static int g_dictionary_queue_start = 0;
- static int g_message_queue_start = 0;
-
- /* The index of the tail of the queues, non-inclusive. */
- static int g_dictionary_queue_end = 0;
- static int g_message_queue_end = 0;
-
- /* The size of the queues. */
- static int g_dictionary_queue_size = 0;
- static int g_message_queue_size = 0;
-
- /* PNaCl interfaces. */
- const PPB_Messaging* g_varMessagingInterface;
- const PPB_Var* g_varInterface;
- const PPB_VarDictionary* g_varDictionaryInterface;
- const PPB_VarArray* g_varArrayInterface;
-
- PP_Instance g_instance;
- PnaclApp app;
-
- /* A wrapper to launch the Nit main on a new thread. */
- void* WrapperNitMain(void* arg) {
- nit_main(0, NULL);
- return NULL;
- }
-
- /** Return whether the queues are empty.
- *
- * NOTE: this function assumes g_queue_mutex lock is held.
- * @return non-zero if the queue is empty. */
- static int IsDictionaryQueueEmpty() { return g_dictionary_queue_size == 0; }
- static int IsMessageQueueEmpty() { return g_message_queue_size == 0; }
-
- /** Return whether the queues are full.
- *
- * NOTE: this function assumes g_queue_mutex lock is held.
- * @return non-zero if the queue is full. */
- static int IsDictionaryQueueFull() { return g_dictionary_queue_size == MAX_DICTIONARY_QUEUE_SIZE; }
- static int IsMessageQueueFull() { return g_message_queue_size == MAX_MESSAGE_QUEUE_SIZE; }
-
- /* Initialize the queues. */
- void InitializeQueues() {
- pthread_mutex_init(&g_dictionary_queue_mutex, NULL);
- pthread_cond_init(&g_dictionary_queue_not_empty_cond, NULL);
- pthread_mutex_init(&g_message_queue_mutex, NULL);
- pthread_cond_init(&g_message_queue_not_empty_cond, NULL);
- }
-
- /** Enqueue a dictionary (i.e. add to the end)
- *
- * If the queue is full, the dictionary will be dropped.
- *
- * NOTE: this function assumes g_dictionary_queue_mutex is _NOT_ held.
- * @param[in] dictionary, the dictionary to enqueue.
- * @return non-zero if the dictionary was added to the queue. */
- int EnqueueDictionary(struct PP_Var dictionary) {
- pthread_mutex_lock(&g_dictionary_queue_mutex);
-
- /* We shouldn't block the main thread waiting for the queue to not be full,
- * so just drop the dictionary. */
- if (IsDictionaryQueueFull()) {
- pthread_mutex_unlock(&g_dictionary_queue_mutex);
- return 0;
- }
-
- g_dictionary_queue[g_dictionary_queue_end] = dictionary;
- g_dictionary_queue_end = (g_dictionary_queue_end + 1) % MAX_DICTIONARY_QUEUE_SIZE;
- g_dictionary_queue_size++;
-
- pthread_cond_signal(&g_dictionary_queue_not_empty_cond);
-
- pthread_mutex_unlock(&g_dictionary_queue_mutex);
-
- return 1;
- }
-
- /** Enqueue a message (i.e. add to the end)
- *
- * If the queue is full, the message will be dropped.
- *
- * NOTE: this function assumes g_message_queue_mutex is _NOT_ held.
- * @param[in] message The message to enqueue.
- * @return non-zero if the message was added to the queue. */
- int EnqueueMessage(char* message) {
- pthread_mutex_lock(&g_message_queue_mutex);
-
- /* We shouldn't block the main thread waiting for the queue to not be full,
- * so just drop the message. */
- if (IsMessageQueueFull()) {
- pthread_mutex_unlock(&g_message_queue_mutex);
- return 0;
- }
-
- g_message_queue[g_message_queue_end] = message;
- g_message_queue_end = (g_message_queue_end + 1) % MAX_MESSAGE_QUEUE_SIZE;
- g_message_queue_size++;
-
- pthread_cond_signal(&g_message_queue_not_empty_cond);
-
- pthread_mutex_unlock(&g_message_queue_mutex);
-
- return 1;
- }
-
- /** Dequeue a dictionary and return it.
- *
- * This function blocks until a dictionary is available. It should not be called
- * on the main thread.
- *
- * NOTE: this function assumes g_dictionary_queue_mutex is _NOT_ held.
- * @return The dictionary at the head of the queue. */
- struct PP_Var DequeueDictionary() {
- struct PP_Var dictionary = g_varDictionaryInterface->Create();
-
- pthread_mutex_lock(&g_dictionary_queue_mutex);
-
- while (IsDictionaryQueueEmpty()) {
- pthread_cond_wait(&g_dictionary_queue_not_empty_cond, &g_dictionary_queue_mutex);
- }
-
- dictionary = g_dictionary_queue[g_dictionary_queue_start];
- g_dictionary_queue_start = (g_dictionary_queue_start + 1) % MAX_DICTIONARY_QUEUE_SIZE;
- g_dictionary_queue_size--;
-
- pthread_mutex_unlock(&g_dictionary_queue_mutex);
-
- return dictionary;
- }
-
- /** Dequeue a message and return it.
- *
- * This function blocks until a message is available. It should not be called
- * on the main thread.
- *
- * NOTE: this function assumes g_queue_mutex is _NOT_ held.
- * @return The message at the head of the queue. */
- char* DequeueMessage() {
- char* message = NULL;
-
- pthread_mutex_lock(&g_message_queue_mutex);
-
- while (IsMessageQueueEmpty()) {
- pthread_cond_wait(&g_message_queue_not_empty_cond, &g_message_queue_mutex);
- }
-
- message = g_message_queue[g_message_queue_start];
- g_message_queue_start = (g_message_queue_start + 1) % MAX_MESSAGE_QUEUE_SIZE;
- g_message_queue_size--;
-
- pthread_mutex_unlock(&g_message_queue_mutex);
-
- return message;
- }
-
- /* Posts a string message to JS. */
- void PostMessage(char* message) {
- /* Create PP_Var containing the message body. */
- struct PP_Var varString = g_varInterface->VarFromUtf8(message, strlen(message));
-
- /* Post message to the JavaScript layer. */
- g_varMessagingInterface->PostMessage(g_instance, varString);
- }
-
- /* Posts a Dictionary (JS like object) to JS. */
- void PostDictionary(struct PP_Var dictionary) {
- g_varMessagingInterface->PostMessage(g_instance, dictionary);
- }
-
- /* Posts a Variable (aka PepperVar) to JS.
- Should only be used for testing, conventional conversation is made
- with Strings or Dictionaries. */
- void PostVar(struct PP_Var v) {
- g_varMessagingInterface->PostMessage(g_instance, v);
- }
-
- /* char* to PP_Var. */
- static struct PP_Var CStrToVar(const char* str) {
- if (g_varInterface != NULL) {
- return g_varInterface->VarFromUtf8(str, strlen(str));
- }
- return PP_MakeUndefined();
- }
-
- static PP_Bool Instance_DidCreate(PP_Instance instance, uint32_t argc, const char* argn[], const char* argv[]) {
- g_instance = instance;
-
- /* Initialization of the queues and creation of the thread for Nit. */
- InitializeQueues();
- pthread_create(&g_nit_thread, NULL, &WrapperNitMain, NULL);
-
- return PP_TRUE;
- }
-
- static void Instance_DidDestroy(PP_Instance instance) {
- // TODO
- }
-
- static void Instance_DidChangeView(PP_Instance pp_instance, PP_Resource view) {
- // TODO
- }
-
- static void Instance_DidChangeFocus(PP_Instance pp_instance, PP_Bool has_focus) {
- // TODO
- }
-
- static PP_Bool Instance_HandleDocumentLoad(PP_Instance pp_instance, PP_Resource pp_url_loader) {
- // TODO
- return PP_FALSE;
- }
-
- /* Called when JS sends something, is set to accept Strings or Dictionaries,
- returns an error if received object is not a String or Dictionary. */
- void Messaging_HandleMessage(PP_Instance instance, struct PP_Var varMessage) {
- if(varMessage.type == PP_VARTYPE_DICTIONARY) {
- if(!EnqueueDictionary(varMessage)) {
- struct PP_Var errorMessage = CStrToVar("QueueFull : dropped dictionary because the queue was full.");
- g_varMessagingInterface->PostMessage(g_instance, errorMessage);
- }
- }
- else if(varMessage.type == PP_VARTYPE_STRING) {
- uint32_t len;
- char* message = (char*)g_varInterface->VarToUtf8(varMessage, &len);
- if(!EnqueueMessage(message)) {
- struct PP_Var errorMessage = CStrToVar("QueueFull : dropped message because the queue was full.");
- g_varMessagingInterface->PostMessage(g_instance, errorMessage);
- }
- }
- else {
- struct PP_Var errorMessage = CStrToVar("TypeError : only accepts JS objects or Strings");
- g_varMessagingInterface->PostMessage(g_instance, errorMessage);
- }
- }
-
- /* This function is called by Nit when using check_dictionary,
- returns the dictionary at the head of the queue. */
- void* NitHandleDictionary() {
- struct PP_Var dictionary = DequeueDictionary();
- PnaclApp_handle_dictionary(app, &dictionary);
- return 0;
- }
-
- /* This function is called By Nit when waiting for a user input. */
- char* NitHandleMessage() {
- return DequeueMessage();
- }
-
- /* Entry point */
- PP_EXPORT int32_t PPP_InitializeModule(PP_Module module_id, PPB_GetInterface get_browser_interface) {
- /* Initializing global pointers. */
- g_varMessagingInterface = (const PPB_Messaging*) get_browser_interface(PPB_MESSAGING_INTERFACE);
- g_varInterface = (const PPB_Var*) get_browser_interface(PPB_VAR_INTERFACE);
- g_varDictionaryInterface = (const PPB_VarDictionary*) get_browser_interface(PPB_VAR_DICTIONARY_INTERFACE);
- g_varArrayInterface = (const PPB_VarArray*) get_browser_interface(PPB_VAR_ARRAY_INTERFACE);
- return PP_OK;
- }
-
- PP_EXPORT void PPP_ShutdownModule() {
- // TODO
- }
-
- PP_EXPORT const void* PPP_GetInterface(const char* interface_name) {
- if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0)
- {
- static PPP_Instance instance_interface = {
- &Instance_DidCreate,
- &Instance_DidDestroy,
- &Instance_DidChangeView,
- &Instance_DidChangeFocus,
- &Instance_HandleDocumentLoad
- };
- return &instance_interface;
- }
- else if (strcmp(interface_name, PPP_MESSAGING_INTERFACE) == 0) {
- static PPP_Messaging messaging_interface = {
- &Messaging_HandleMessage
- };
- return &messaging_interface;
- }
- return NULL;
- }
-
- /* Hack in order to avoid the problem with file. */
- int poll(struct pollfd* fds, nfds_t nfds, int timeout) { return 0; }
-`}
-
-# Nit class representing a Pepper C API PP_Var typed as a Dictionary.
-extern class PepperDictionary `{ struct PP_Var* `}
-
- new `{
- struct PP_Var* self = malloc( sizeof( struct PP_Var ) );
- *self = g_varDictionaryInterface->Create();
- return self;
- `}
-
- # Get fonction using PepperVars.
- #
- # Returns the value that is associated with 'key'.
- # If 'key' is not a String typed PepperVar, or doesn't exist in the Dictionary, an undefined PepperVar is returned.
- fun native_get(key: PepperVar): PepperVar `{
- struct PP_Var* value = malloc( sizeof ( struct PP_Var ) );
- *value = g_varDictionaryInterface->Get(*self, *key);
- return value;
- `}
-
- # Returns the value associated with 'key'.
- #
- # 'key' must be a String.
- # If 'key' is not a String or doesn't exist in the Dictionary, 'null' is returned.
- fun [](key: nullable Pepperable): nullable Pepperable
- do
- var native_key = key.to_pepper
- var native_value = native_get(native_key)
- return native_value.to_nit
- end
-
- # Set function using PepperVars.
- #
- # Sets the value associated with the specified key.
- # 'key' must be a String typed PepperVar.
- # If 'key' hasn't existed in the Dictionary, it is added and associated with 'value'.
- # Otherwise, the previous value is replaced with 'value'.
- #Â Returns a Boolean indicating whether the operation succeeds.
- fun native_set(key: PepperVar, value: PepperVar): Bool `{
- PP_Bool b;
- b = g_varDictionaryInterface->Set(*self, *key, *value);
- return b;
- `}
-
- # Sets the value associated with the specified key.
- #
- # 'key' must be a String.
- # If 'key' hasn't existed in the Dictionary, it is added and associated with 'value'.
- # Otherwise, the previous value is replaced with 'value'.
- #Â Returns a Boolean indicating whether the operation succeeds.
- fun []=(key: nullable Pepperable, value: nullable Pepperable): Bool
- do
- var native_key = key.to_pepper
- var native_value = value.to_pepper
- return native_set(native_key, native_value)
- end
-
- # Deletes the specified key and its associated value, if the key exists.
- #
- # Takes a String typed PepperVar.
- fun native_delete(key: PepperVar) `{
- g_varDictionaryInterface->Delete(*self, *key);
- `}
-
- # Deletes the specified key and its associated value, if the key exists.
- #
- # Takes a String.
- fun delete(key: String)
- do
- var native_key = key.to_pepper
- native_delete native_key
- end
-
- # Checks whether a key exists.
- #
- # Takes a String typed PepperVar.
- fun native_has_key(key: PepperVar): Bool `{
- PP_Bool b;
- b = g_varDictionaryInterface->HasKey(*self, *key);
- return b;
- `}
-
- # Checks whether a key exists.
- #
- # Takes a String.
- fun has_key(key: String): Bool
- do
- var native_key = key.to_pepper
- return native_has_key(native_key)
- end
-
- # Gets all the keys in a dictionary.
- #
- # Returns a PepperArray which contains all the keys of the Dictionary. The elements are string vars.
- fun get_keys: PepperArray `{
- struct PP_Var* array = malloc( sizeof( struct PP_Var ) );
- *array = g_varDictionaryInterface->GetKeys(*self);
- return array;
- `}
-
- # Use this function to copy a dictionary.
- fun copy: PepperDictionary `{
- struct PP_Var* varDictionary = malloc( sizeof( struct PP_Var ) );
- *varDictionary = g_varDictionaryInterface->Create();
- *varDictionary = *self;
- return varDictionary;
- `}
-end
-
-# Nit class representing a Pepper C API PP_Var typed as an Array.
-extern class PepperArray `{ struct PP_Var* `}
-
- new `{
- struct PP_Var* self = malloc( sizeof( struct PP_Var ) );
- *self = g_varArrayInterface->Create();
- return self;
- `}
-
- #Â Returns the element at the specified position as a PepperVar.
- #
- # If 'index' is larger than or equal to the array length, an undefined PepperVar is returned.
- fun native_get(index: Int): PepperVar `{
- struct PP_Var* value = malloc( sizeof( struct PP_Var ) );
- *value = g_varArrayInterface->Get(*self, index);
- return value;
- `}
-
- # Returns the element at the specified position.
- #
- # If 'index' is larger than or equal to the array length, 'null' is returned.
- fun [](index: Int): nullable Pepperable
- do
- var native_value = native_get(index)
- return native_value.to_nit
- end
-
- # Returns an int containing the length of the PepperArray.
- fun length: Int `{
- int length = g_varArrayInterface->GetLength(*self);
- return length;
- `}
-
- # Takes a PepperVar for the 'value' param.
- #
- # Sets the value of an element in the array at indicated index.
- # If 'index' is larger than or equal to the array length, the length is updated to be 'index' + 1.
- # Any position in the array that hasn't been set before is set to undefined, i.e., PepperVar of C type PP_VARTYPE_UNDEFINED.
- # Returns a Boolean indicating whether the operation succeeds.
- fun native_set(index: Int, value: PepperVar): Bool `{
- PP_Bool b;
- b = g_varArrayInterface->Set(*self, index, *value);
- return b;
- `}
-
- # Sets the value of an element in the array at indicated index.
- #
- # If 'index' is larger than or equal to the array length, the length is updated to be 'index' + 1.
- # Any position in the array that hasn't been set before is set to undefined, i.e., PepperVar of C type PP_VARTYPE_UNDEFINED.
- # Returns a Boolean indicating whether the operation succeeds.
- fun []=(index: Int, value: nullable Pepperable): Bool
- do
- var native_value = value.to_pepper
- return native_set(index, native_value)
- end
-
- # Sets the array length.
- #
- # If 'length' is smaller than its current value, the array is truncated to the new length.
- # Any elements that no longer fit are removed and the references to them will be released.
- # If 'length' is larger than its current value, undefined PepperVars are appended to increase the array to the specified length.
- #Â Returns a Boolean indicating whether the operation succeeds.
- fun length=(length: Int): Bool `{
- PP_Bool b;
- b = g_varArrayInterface->SetLength(*self, length);
- return b;
- `}
-end
-
-# Nit class representing a Pepper C API PP_Var.
-extern class PepperVar `{ struct PP_Var* `}
-
- new `{
- return malloc( sizeof( struct PP_Var ) );
- `}
-
- # Converts PepperVar to standard types.
- #
- # Actually supports bools, ints, floats, strings. To be used with 'isa'.
- fun to_nit: nullable Pepperable
- do
- if isa_null then return null
- if isa_bool then return as_bool
- if isa_int then return as_int
- if isa_float then return as_float
- if isa_string then return as_string
- if is_undefined then return null
-
- return null
- end
-
- private fun isa_null: Bool `{ return self->type == PP_VARTYPE_NULL; `}
- private fun isa_bool: Bool `{ return self->type == PP_VARTYPE_BOOL; `}
- private fun isa_int: Bool `{ return self->type == PP_VARTYPE_INT32; `}
- private fun isa_float: Bool `{ return self->type == PP_VARTYPE_DOUBLE; `}
- private fun isa_string: Bool `{ return self->type == PP_VARTYPE_STRING; `}
- private fun is_undefined: Bool `{ return self->type == PP_VARTYPE_UNDEFINED; `}
-
- private fun as_bool: Bool `{ return self->value.as_bool; `}
- private fun as_int: Int `{ return self->value.as_int; `}
- private fun as_float: Float `{ return self->value.as_double; `}
- private fun as_string: String import CString.to_s_with_length `{
- uint32_t len;
- char* str = (char*)g_varInterface->VarToUtf8(*self, &len);
- return CString_to_s_with_length(str, len);
- `}
-end
-
-# Provides a method to convert in PepperVars.
-interface Pepperable
- fun to_pepper: PepperVar is abstract
-end
-
-redef class Int
- super Pepperable
-
- # Converts a Int into a PepperVar with Int type.
- redef fun to_pepper `{
- struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
- *var = PP_MakeInt32(self);
- return var;
- `}
-end
-
-redef class Float
- super Pepperable
-
- # Converts a Float into a PepperVar with Float type.
- redef fun to_pepper `{
- struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
- *var = PP_MakeDouble(self);
- return var;
- `}
-end
-
-redef class Bool
- super Pepperable
-
- # Converts a Bool into a PepperVar with Bool type.
- redef fun to_pepper `{
- struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
- *var = PP_MakeBool(self);
- return var;
- `}
-end
-
-redef class String
- super Pepperable
-
- # Converts a String into a PepperVar with String type.
- redef fun to_pepper: PepperVar import String.to_cstring, String.length `{
- char *str = String_to_cstring(self);
- struct PP_Var* var = malloc( sizeof( struct PP_Var ) );
- *var = g_varInterface->VarFromUtf8(str, String_length(self));
- return var;
- `}
-end
-
-# A stream for PNaCl, redefines basic input and output methods.
-class PnaclStream
- super PollableReader
- super Writer
- super BufferedReader
-
- init do prepare_buffer(10)
-
- redef var end_reached: Bool = false
-
- redef fun eof do return end_reached
-
- #Â Redefintion of 'write' to send messages to the browser.
- redef fun write(s: Text) do app.post_message s.to_s
-
- redef fun is_writable: Bool do return true
-
- #Â Checks if there is a message in the queue, and if so the message is handled automatically.
- fun check_message: CString `{
- return NitHandleMessage();
- `}
-
- # fill_buffer now checks for a message in the message queue which is filled by user inputs.
- redef fun fill_buffer
- do
- _buffer_pos = 0
- var nns = check_message
- var nslen = nns.cstring_length
- _buffer_length = nslen
- nns.copy_to(buffer, nslen, 0, 0)
- end
-end
-
-#Â For a PNaCl app, Sys uses PnaclStreams.
-redef class Sys
- fun pnacl_stdstr: PnaclStream do return once new PnaclStream
-
- #Â NaCl input.
- redef fun stdin do return pnacl_stdstr
-
- # NaCl output.
- redef fun stdout do return pnacl_stdstr
-
- #Â NaCl output for errors.
- redef fun stderr do return pnacl_stdstr
-end
-
-#Â Class that provides the tools to interact with PNaCl.
-class PnaclApp
-
- #Â Sets everything up to work, need to be called at first.
- fun initialize import PnaclApp.handle_message, PnaclApp.handle_dictionary, CString.to_s_with_length `{
- app = self;
- `}
-
- # Posts a message to JS.
- fun post_message(message: String) import String.to_cstring `{
- char* str = String_to_cstring(message);
- PostMessage(str);
- `}
-
- # Posts a dictionary to JS.
- fun post_dictionary(dictionary: PepperDictionary) `{
- PostDictionary(*dictionary);
- `}
-
- # Posts a PepperVar to JS.
- #
- # Should be used for testing, not recommanded for conventional conversation.
- private fun post_var(v: PepperVar) `{
- PostVar(*v);
- `}
-
- # Is called when a message is received from JS.
- #
- # Is set to be redefined in your application to handle like you want.
- fun handle_message(message: String)
- do
- # To be Implemented by user.
- end
-
- # Is called when a Dictionary is received from JS.
- #
- # Is set to be redefined in your application to handle like you want.
- #Â The dictionary is freed after this method returns.
- fun handle_dictionary(dictionary: PepperDictionary)
- do
- # To be Implemented by user.
- end
-
- #Â Checks if there is a dictionary in the queue, and if so the dictionary is handled automatically.
- fun check_dictionary `{
- NitHandleDictionary();
- `}
-
- #Â Infinite loop on check_dictionary
- fun run
- do
- loop
- check_dictionary
- end
- end
-end
-
-# Creates a new thread for Nit.
-#
-# This function launches the Nit main on a new thread.
-#Â Its purpose is to allow Nit to be still operational after an exit when needed,
-# because reloading the page may not be an option.
-#
-# Should only be used within the 'exit' before stopping the current thread
-# when the Nit execution causes a crash.
-#
-# REQUIRE: g_nit_thread and WrapperNitMain are set.
-fun create_thread `{
- pthread_create(&g_nit_thread, NULL, &WrapperNitMain, NULL);
-`}
-
-# Calls 'pthread_exit on current thread.
-fun exit_thread(exit_value: Int) `{
- pthread_exit((void*) exit_value);
-`}
-
-#Â Redef of exit in order to avoid the module to crash by terminating only the Nit thread.
-redef fun exit(exit_value: Int)
-do
- var dictionary = new PepperDictionary
- dictionary["exit"] = exit_value
- app.post_dictionary dictionary
- exit_thread exit_value
-end
-
-fun app: PnaclApp do return once new PnaclApp
diff --git a/share/man/nitc.md b/share/man/nitc.md
index c241573..243c83a 100644
--- a/share/man/nitc.md
+++ b/share/man/nitc.md
@@ -38,7 +38,7 @@ To combine files into a single program, use the `-m` option.
$ nitc prog1.nit -m other_module.nit
nitc can produces executables for various platforms when specific modules are used.
-Currently, android, pnacl and emscripten are supported.
+Currently, android and emscripten are supported.
See the documentation of these specific modules for details.
diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit
index ac6edce..ea4adde 100644
--- a/src/compiler/abstract_compiler.nit
+++ b/src/compiler/abstract_compiler.nit
@@ -715,10 +715,6 @@ abstract class AbstractCompiler
self.header.add_decl("#ifdef _WIN32")
self.header.add_decl(" #define be32toh(val) _byteswap_ulong(val)")
self.header.add_decl("#endif")
- self.header.add_decl("#ifdef __pnacl__")
- self.header.add_decl(" #define be16toh(val) (((val) >> 8) | ((val) << 8))")
- self.header.add_decl(" #define be32toh(val) ((be16toh((val) << 16) | (be16toh((val) >> 16))))")
- self.header.add_decl("#endif")
self.header.add_decl("#ifdef ANDROID")
self.header.add_decl(" #ifndef be32toh")
self.header.add_decl(" #define be32toh(val) betoh32(val)")
diff --git a/src/compiler/compiler.nit b/src/compiler/compiler.nit
index 4dce71a..befc8b6 100644
--- a/src/compiler/compiler.nit
+++ b/src/compiler/compiler.nit
@@ -22,6 +22,5 @@ import memory_logger
import compiler_serialization
import platform::android
-import platform::pnacl
import platform::emscripten
import platform::ios
diff --git a/src/platform/pnacl.nit b/src/platform/pnacl.nit
deleted file mode 100644
index f994c45..0000000
--- a/src/platform/pnacl.nit
+++ /dev/null
@@ -1,251 +0,0 @@
-# This file is part of NIT ( http://www.nitlanguage.org )
-#
-# Copyright 2014 Johan Kayser
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-# Compile program for the PNaCl platform
-module pnacl
-
-import platform
-import compiler::abstract_compiler
-
-redef class ToolContext
- redef fun platform_from_name(name)
- do
- if name == "pnacl" then return new PnaclPlatform
- return super
- end
-end
-
-class PnaclPlatform
- super Platform
-
- redef fun name do return "pnacl"
-
- redef fun supports_libunwind do return false
-
- redef fun no_main do return true
-
- redef fun toolchain(toolcontext, compiler) do return new PnaclToolchain(toolcontext, compiler)
-end
-
-class PnaclToolchain
- super MakefileToolchain
-
- redef fun write_files(compile_dir, cfiles)
- do
- var app_name = compiler.mainmodule.name
-
- # create compile_dir
- var dir = compile_dir
- if not dir.file_exists then dir.mkdir
-
- # compile normal C files
- super
-
- # Gather extra C files generated elsewhere than in super
- for f in compiler.extern_bodies do
- if f isa ExternCFile then cfiles.add(f.filename.basename)
- end
-
- # Outname
- var outname = toolcontext.opt_output.value
- if outname == null then outname = "{compiler.mainmodule.name}"
-
- var ofiles = new Array[String]
- for cfile in cfiles do ofiles.add(cfile.substring(0, cfile.length-2) + ".o")
-
- ## Generate makefile
- var file = "{dir}/Makefile"
- """
-# This file was generated by Nit, any modification will be lost.
-
-# Get pepper directory for toolchain and includes.
-#
-# If NACL_SDK_ROOT is not set, then assume it can be found five directories up.
-#
-THIS_MAKEFILE := $(abspath $(lastword $(MAKEFILE_LIST)))
-NACL_SDK_ROOT ?= $(abspath $(dir $(THIS_MAKEFILE))../../../..)
-
-# Project Build flags
-WARNINGS := -Wno-long-long -Wno-unused-value -Wno-unused-label -Wno-duplicate-decl-specifier -Wno-switch -Wno-embedded-directive
-CXXFLAGS := -pthread $(WARNINGS)
-
-CXXFLAGS += -g -O0 # Debug
-# CXXFLAGS += -O3 # Release
-
-#
-# Compute tool paths
-#
-GETOS := python $(NACL_SDK_ROOT)/tools/getos.py
-OSHELPERS = python $(NACL_SDK_ROOT)/tools/oshelpers.py
-OSNAME := $(shell $(GETOS))
-
-PNACL_TC_PATH := $(abspath $(NACL_SDK_ROOT)/toolchain/$(OSNAME)_pnacl)
-PNACL_CXX := $(PNACL_TC_PATH)/bin/pnacl-clang
-PNACL_FINALIZE := $(PNACL_TC_PATH)/bin/pnacl-finalize
-CXXFLAGS += -I$(NACL_SDK_ROOT)/include -I$(NACL_SDK_ROOT)/include/pnacl
-LDFLAGS := -L$(NACL_SDK_ROOT)/lib/pnacl/Release -lppapi_cpp -lppapi -lm
-
-#
-# Disable DOS PATH warning when using Cygwin based tools Windows
-#
-CYGWIN ?= nodosfilewarning
-export CYGWIN
-
-# Declare the ALL target first, to make the 'all' target the default build
-all: ../{{{outname}}}/{{{app_name}}}.pexe
-
-.c.o:
- $(PNACL_CXX) -c $< -g -O0 $(CXXFLAGS)
-
-{{{app_name}}}.pexe: {{{ofiles.join(" ")}}}
- $(PNACL_CXX) -o $@ $^ $(LDFLAGS)
-
-../{{{outname}}}/{{{app_name}}}.pexe: {{{app_name}}}.pexe
- $(PNACL_FINALIZE) -o $@ $<
- """.write_to_file(file)
-
- ### generate the minimal index.html
- if not outname.file_exists then outname.mkdir
- file = "{outname}/index.html"
-
- if not file.file_exists then """
-
-
-
-
- {{{app_name}}}
-
-
-
-
PNaCl : Minimal HTML for {{{app_name}}}
-
-
-
-
-
-
-
-
-
Status NO-STATUS
-
-
- """.write_to_file(file)
-
- ### generate pnacl_js.js in a folder named 'js'
- dir = "{outname}/js/"
- if not dir.file_exists then dir.mkdir
- file = "{dir}/pnacl_js.js"
- if not file.file_exists then """
-// This file was generated by Nit, any modification will be lost.
-
-{{{app_name}}}Module = null; // Global application object.
-statusText = 'NO-STATUS';
-
-// Indicate load success.
-function moduleDidLoad() {
- {{{app_name}}}Module = document.getElementById('{{{app_name}}}');
- updateStatus('SUCCESS');
- // Send a message to the Native Client module like that
- //{{{app_name}}}Module.postMessage('Hello World');
-}
-
-// The 'message' event handler. This handler is fired when the NaCl module
-// posts a message to the browser by calling PPB_Messaging.PostMessage()
-// (in C) or pp::Instance.PostMessage() (in C++). This implementation
-// simply displays the content of the message in an alert panel.
-function handleMessage(message_event) {
- console.log(message_event.data);
-}
-
-// If the page loads before the Native Client module loads, then set the
-// status message indicating that the module is still loading. Otherwise,
-// do not change the status message.
-function pageDidLoad() {
- if ({{{app_name}}}Module == null) {
- updateStatus('LOADING...');
- } else {
- // It's possible that the Native Client module onload event fired
- // before the page's onload event. In this case, the status message
- // will reflect 'SUCCESS', but won't be displayed. This call will
- // display the current message.
- updateStatus();
- }
-}
-
-// Set the global status message. If the element with id 'statusField'
-// exists, then set its HTML to the status message as well.
-// opt_message The message test. If this is null or undefined, then
-// attempt to set the element with id 'statusField' to the value of
-// |statusText|.
-function updateStatus(opt_message) {
- if (opt_message)
- statusText = opt_message;
- var statusField = document.getElementById('statusField');
- if (statusField) {
- statusField.innerHTML = statusText;
- }
-}
- """.write_to_file(file)
-
- ### generate the manifest file : app_name.nmf
- # used to point the HTML to the Native Client module
- # and optionally provide additional commands to the PNaCl translator in Chrome
- file = "{outname}/{app_name}.nmf"
- """
-{
- "program": {
- "portable": {
- "pnacl-translate": {
- "url": "{{{app_name}}}.pexe"
- }
- }
- }
-}
- """.write_to_file(file)
- end
-
- redef fun write_makefile(compile_dir, cfiles)
- do
- # Do nothing, already done in `write_files`
- end
-
- redef fun compile_c_code(compile_dir)
- do
- # Generate the pexe
- toolcontext.exec_and_check(["make", "-C", compile_dir, "-j", "4"], "PNaCl project error")
- end
-end
diff --git a/tests/turing.skip b/tests/turing.skip
index e6ce3b1..1400778 100644
--- a/tests/turing.skip
+++ b/tests/turing.skip
@@ -8,7 +8,6 @@ gtk
nitcorn
ffi_objc
mpi
-pnacl
emscipten
nodejs
nitunit