2013-12-29 04:53:12 +00:00
|
|
|
/*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you 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
|
2011-08-05 02:17:44 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-08-05 02:17:44 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* 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.
|
2013-12-29 04:53:12 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "config.h"
|
2011-08-05 02:17:44 +00:00
|
|
|
|
2017-02-27 22:34:46 +00:00
|
|
|
#include "terminal/char_mappings.h"
|
2017-04-23 18:38:49 +00:00
|
|
|
#include "terminal/palette.h"
|
2017-02-27 22:34:46 +00:00
|
|
|
#include "terminal/terminal.h"
|
|
|
|
#include "terminal/terminal_handlers.h"
|
|
|
|
#include "terminal/types.h"
|
2014-01-01 22:44:28 +00:00
|
|
|
|
2014-06-11 00:40:58 +00:00
|
|
|
#include <guacamole/client.h>
|
2016-01-13 01:07:32 +00:00
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <guacamole/socket.h>
|
2014-06-11 00:40:58 +00:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <stdlib.h>
|
2014-06-02 19:53:40 +00:00
|
|
|
#include <wchar.h>
|
2011-08-05 02:17:44 +00:00
|
|
|
|
2013-05-26 03:24:44 +00:00
|
|
|
/**
|
|
|
|
* Response string sent when identification is requested.
|
|
|
|
*/
|
|
|
|
#define GUAC_TERMINAL_VT102_ID "\x1B[?6c"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Arbitrary response to ENQ control character.
|
|
|
|
*/
|
|
|
|
#define GUAC_TERMINAL_ANSWERBACK "GUACAMOLE"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Response which indicates the terminal is alive.
|
|
|
|
*/
|
|
|
|
#define GUAC_TERMINAL_OK "\x1B[0n"
|
|
|
|
|
2017-01-29 05:24:06 +00:00
|
|
|
/**
|
|
|
|
* Advances the cursor to the next row, scrolling if the cursor would otherwise
|
|
|
|
* leave the scrolling region. If the cursor is already outside the scrolling
|
|
|
|
* region, the cursor is prevented from leaving the terminal bounds.
|
|
|
|
*
|
|
|
|
* @param term
|
|
|
|
* The guac_terminal whose cursor should be advanced to the next row.
|
|
|
|
*/
|
|
|
|
static void guac_terminal_linefeed(guac_terminal* term) {
|
|
|
|
|
|
|
|
/* Scroll up if necessary */
|
|
|
|
if (term->cursor_row == term->scroll_end)
|
|
|
|
guac_terminal_scroll_up(term, term->scroll_start,
|
|
|
|
term->scroll_end, 1);
|
|
|
|
|
|
|
|
/* Otherwise, just advance to next row if space remains */
|
|
|
|
else if (term->cursor_row < term->term_height - 1)
|
|
|
|
term->cursor_row++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves the cursor backward to the previous row, scrolling if the cursor would
|
|
|
|
* otherwise leave the scrolling region. If the cursor is already outside the
|
|
|
|
* scrolling region, the cursor is prevented from leaving the terminal bounds.
|
|
|
|
*
|
|
|
|
* @param term
|
|
|
|
* The guac_terminal whose cursor should be moved backward by one row.
|
|
|
|
*/
|
|
|
|
static void guac_terminal_reverse_linefeed(guac_terminal* term) {
|
|
|
|
|
|
|
|
/* Scroll down if necessary */
|
|
|
|
if (term->cursor_row == term->scroll_start)
|
|
|
|
guac_terminal_scroll_down(term, term->scroll_start,
|
|
|
|
term->scroll_end, 1);
|
|
|
|
|
|
|
|
/* Otherwise, move back one row if space remains */
|
|
|
|
else if (term->cursor_row > 0)
|
|
|
|
term->cursor_row--;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-29 06:48:45 +00:00
|
|
|
/**
|
|
|
|
* Sets the position of the cursor without exceeding terminal bounds. Values
|
|
|
|
* which are out of bounds will be shifted to the nearest legal boundary.
|
|
|
|
*
|
|
|
|
* @param term
|
|
|
|
* The guac_terminal whose cursor position is being set.
|
|
|
|
*
|
|
|
|
* @param row
|
|
|
|
* The desired new row position.
|
|
|
|
*
|
|
|
|
* @param col
|
|
|
|
* The desired new column position.
|
|
|
|
*/
|
|
|
|
static void guac_terminal_move_cursor(guac_terminal* term, int row, int col) {
|
|
|
|
|
|
|
|
/* Ensure cursor row is within terminal bounds */
|
|
|
|
if (row >= term->term_height)
|
|
|
|
row = term->term_height - 1;
|
|
|
|
else if (row < 0)
|
|
|
|
row = 0;
|
|
|
|
|
|
|
|
/* Ensure cursor column is within terminal bounds */
|
|
|
|
if (col >= term->term_width)
|
|
|
|
col = term->term_width - 1;
|
|
|
|
else if (col < 0)
|
|
|
|
col = 0;
|
|
|
|
|
|
|
|
/* Update cursor position */
|
|
|
|
term->cursor_row = row;
|
|
|
|
term->cursor_col = col;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:45:19 +00:00
|
|
|
int guac_terminal_echo(guac_terminal* term, unsigned char c) {
|
2011-08-05 02:17:44 +00:00
|
|
|
|
2014-06-02 19:53:40 +00:00
|
|
|
int width;
|
|
|
|
|
2013-04-15 22:24:21 +00:00
|
|
|
static int bytes_remaining = 0;
|
|
|
|
static int codepoint = 0;
|
|
|
|
|
2013-05-25 04:18:47 +00:00
|
|
|
const int* char_mapping = term->char_mapping[term->active_char_set];
|
|
|
|
|
2016-01-14 01:38:13 +00:00
|
|
|
/* Echo to pipe stream if open and not starting an ESC sequence */
|
|
|
|
if (term->pipe_stream != NULL && c != 0x1B) {
|
|
|
|
guac_terminal_pipe_stream_write(term, c);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-25 04:18:47 +00:00
|
|
|
/* If using non-Unicode mapping, just map straight bytes */
|
|
|
|
if (char_mapping != NULL) {
|
|
|
|
codepoint = c;
|
|
|
|
bytes_remaining = 0;
|
|
|
|
}
|
|
|
|
|
2013-04-15 22:24:21 +00:00
|
|
|
/* 1-byte UTF-8 codepoint */
|
2013-05-25 04:18:47 +00:00
|
|
|
else if ((c & 0x80) == 0x00) { /* 0xxxxxxx */
|
2013-04-15 22:24:21 +00:00
|
|
|
codepoint = c & 0x7F;
|
|
|
|
bytes_remaining = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 2-byte UTF-8 codepoint */
|
|
|
|
else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */
|
|
|
|
codepoint = c & 0x1F;
|
|
|
|
bytes_remaining = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 3-byte UTF-8 codepoint */
|
|
|
|
else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */
|
|
|
|
codepoint = c & 0x0F;
|
|
|
|
bytes_remaining = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 4-byte UTF-8 codepoint */
|
|
|
|
else if ((c & 0xF8) == 0xF0) { /* 11110xxx */
|
|
|
|
codepoint = c & 0x07;
|
|
|
|
bytes_remaining = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Continuation of UTF-8 codepoint */
|
|
|
|
else if ((c & 0xC0) == 0x80) { /* 10xxxxxx */
|
|
|
|
codepoint = (codepoint << 6) | (c & 0x3F);
|
|
|
|
bytes_remaining--;
|
|
|
|
}
|
|
|
|
|
2013-05-03 09:50:05 +00:00
|
|
|
/* Unrecognized prefix */
|
2013-04-15 22:24:21 +00:00
|
|
|
else {
|
2013-05-03 09:50:05 +00:00
|
|
|
codepoint = '?';
|
|
|
|
bytes_remaining = 0;
|
2013-04-15 22:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we need more bytes, wait for more bytes */
|
|
|
|
if (bytes_remaining != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (codepoint) {
|
2011-08-05 02:17:44 +00:00
|
|
|
|
2013-05-25 05:54:56 +00:00
|
|
|
/* Enquiry */
|
|
|
|
case 0x05:
|
2013-05-26 05:45:26 +00:00
|
|
|
guac_terminal_send_string(term, GUAC_TERMINAL_ANSWERBACK);
|
2013-05-25 05:54:56 +00:00
|
|
|
break;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
/* Bell */
|
|
|
|
case 0x07:
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Backspace */
|
|
|
|
case 0x08:
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term, term->cursor_row,
|
|
|
|
term->cursor_col - 1);
|
2011-08-05 02:17:44 +00:00
|
|
|
break;
|
|
|
|
|
2013-05-26 08:49:47 +00:00
|
|
|
/* Tab */
|
|
|
|
case 0x09:
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term, term->cursor_row,
|
|
|
|
guac_terminal_next_tab(term, term->cursor_col));
|
2013-05-26 08:49:47 +00:00
|
|
|
break;
|
|
|
|
|
2013-05-21 07:55:46 +00:00
|
|
|
/* Line feed / VT / FF */
|
2011-08-05 02:17:44 +00:00
|
|
|
case '\n':
|
2013-05-21 07:57:44 +00:00
|
|
|
case 0x0B: /* VT */
|
|
|
|
case 0x0C: /* FF */
|
2011-08-05 21:46:35 +00:00
|
|
|
|
2017-01-29 05:24:06 +00:00
|
|
|
/* Advance to next row */
|
|
|
|
guac_terminal_linefeed(term);
|
2013-05-24 20:33:32 +00:00
|
|
|
|
|
|
|
/* If automatic carriage return, fall through to CR handler */
|
|
|
|
if (!term->automatic_carriage_return)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Carriage return */
|
|
|
|
case '\r':
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term, term->cursor_row, 0);
|
2011-08-05 02:17:44 +00:00
|
|
|
break;
|
|
|
|
|
2013-05-25 04:28:14 +00:00
|
|
|
/* SO (activates character set G1) */
|
|
|
|
case 0x0E:
|
|
|
|
term->active_char_set = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* SI (activates character set G0) */
|
|
|
|
case 0x0F:
|
|
|
|
term->active_char_set = 0;
|
|
|
|
break;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
/* ESC */
|
|
|
|
case 0x1B:
|
2012-12-09 08:35:37 +00:00
|
|
|
term->char_handler = guac_terminal_escape;
|
2011-08-05 02:17:44 +00:00
|
|
|
break;
|
|
|
|
|
2013-05-21 07:57:44 +00:00
|
|
|
/* CSI */
|
|
|
|
case 0x9B:
|
|
|
|
term->char_handler = guac_terminal_csi;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* DEL (ignored) */
|
|
|
|
case 0x7F:
|
|
|
|
break;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
/* Displayable chars */
|
|
|
|
default:
|
2011-08-05 21:30:10 +00:00
|
|
|
|
2013-05-25 05:54:56 +00:00
|
|
|
/* Don't bother handling control chars if unknown */
|
|
|
|
if (codepoint < 0x20)
|
|
|
|
break;
|
|
|
|
|
2013-05-25 04:18:47 +00:00
|
|
|
/* Translate mappable codepoints to whatever codepoint is mapped */
|
|
|
|
if (codepoint >= 0x20 && codepoint <= 0xFF && char_mapping != NULL)
|
|
|
|
codepoint = char_mapping[codepoint - 0x20];
|
|
|
|
|
2011-08-05 21:46:35 +00:00
|
|
|
/* Wrap if necessary */
|
|
|
|
if (term->cursor_col >= term->term_width) {
|
|
|
|
term->cursor_col = 0;
|
2017-01-29 05:24:06 +00:00
|
|
|
guac_terminal_linefeed(term);
|
2011-08-05 21:30:10 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 07:01:47 +00:00
|
|
|
/* If insert mode, shift other characters right by 1 */
|
|
|
|
if (term->insert_mode)
|
|
|
|
guac_terminal_copy_columns(term, term->cursor_row,
|
|
|
|
term->cursor_col, term->term_width-2, 1);
|
|
|
|
|
2013-03-24 23:56:17 +00:00
|
|
|
/* Write character */
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal_set(term,
|
2011-08-05 02:17:44 +00:00
|
|
|
term->cursor_row,
|
|
|
|
term->cursor_col,
|
2013-05-02 21:56:20 +00:00
|
|
|
codepoint);
|
2011-08-05 02:17:44 +00:00
|
|
|
|
2014-06-02 19:53:40 +00:00
|
|
|
width = wcwidth(codepoint);
|
|
|
|
if (width < 0)
|
|
|
|
width = 1;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
/* Advance cursor */
|
2014-06-02 19:53:40 +00:00
|
|
|
term->cursor_col += width;
|
2011-08-05 21:46:35 +00:00
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:45:19 +00:00
|
|
|
int guac_terminal_escape(guac_terminal* term, unsigned char c) {
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case '(':
|
2013-05-24 06:12:01 +00:00
|
|
|
term->char_handler = guac_terminal_g0_charset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
term->char_handler = guac_terminal_g1_charset;
|
|
|
|
break;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
case ']':
|
2012-12-09 08:35:37 +00:00
|
|
|
term->char_handler = guac_terminal_osc;
|
2011-08-05 02:17:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '[':
|
2012-12-09 08:35:37 +00:00
|
|
|
term->char_handler = guac_terminal_csi;
|
2011-08-05 02:17:44 +00:00
|
|
|
break;
|
|
|
|
|
2013-05-22 06:38:35 +00:00
|
|
|
case '#':
|
|
|
|
term->char_handler = guac_terminal_ctrl_func;
|
|
|
|
break;
|
|
|
|
|
2013-05-21 07:29:19 +00:00
|
|
|
/* Save Cursor (DECSC) */
|
|
|
|
case '7':
|
2013-05-21 07:19:53 +00:00
|
|
|
term->saved_cursor_row = term->cursor_row;
|
|
|
|
term->saved_cursor_col = term->cursor_col;
|
2013-05-21 07:29:19 +00:00
|
|
|
|
|
|
|
term->char_handler = guac_terminal_echo;
|
2013-05-21 07:19:53 +00:00
|
|
|
break;
|
|
|
|
|
2013-05-21 07:29:19 +00:00
|
|
|
/* Restore Cursor (DECRC) */
|
|
|
|
case '8':
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term,
|
|
|
|
term->saved_cursor_row,
|
|
|
|
term->saved_cursor_col);
|
2013-05-21 07:19:53 +00:00
|
|
|
|
2013-05-21 07:29:19 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Index (IND) */
|
|
|
|
case 'D':
|
2017-01-29 05:24:06 +00:00
|
|
|
guac_terminal_linefeed(term);
|
2013-05-21 07:29:19 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Next Line (NEL) */
|
|
|
|
case 'E':
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term, term->cursor_row, 0);
|
2017-01-29 05:24:06 +00:00
|
|
|
guac_terminal_linefeed(term);
|
2013-05-21 07:29:19 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
2013-05-21 07:19:53 +00:00
|
|
|
break;
|
|
|
|
|
2013-05-26 08:49:47 +00:00
|
|
|
/* Set Tab (HTS) */
|
|
|
|
case 'H':
|
|
|
|
guac_terminal_set_tab(term, term->cursor_col);
|
2014-06-11 22:04:39 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
2013-05-26 08:49:47 +00:00
|
|
|
break;
|
|
|
|
|
2013-05-21 07:29:19 +00:00
|
|
|
/* Reverse Linefeed */
|
|
|
|
case 'M':
|
2017-01-29 05:24:06 +00:00
|
|
|
guac_terminal_reverse_linefeed(term);
|
2013-05-03 09:50:05 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
|
|
|
break;
|
|
|
|
|
2013-05-24 23:29:43 +00:00
|
|
|
/* DEC Identify */
|
|
|
|
case 'Z':
|
2013-05-26 05:45:26 +00:00
|
|
|
guac_terminal_send_string(term, GUAC_TERMINAL_VT102_ID);
|
2013-05-24 23:29:43 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Reset */
|
|
|
|
case 'c':
|
|
|
|
guac_terminal_reset(term);
|
|
|
|
break;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
default:
|
2017-04-23 19:58:32 +00:00
|
|
|
guac_client_log(term->client, GUAC_LOG_DEBUG,
|
|
|
|
"Unhandled ESC sequence: %c", c);
|
2012-12-09 08:35:37 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-25 04:28:14 +00:00
|
|
|
/**
|
|
|
|
* Given a character mapping specifier (such as B, 0, U, or K),
|
|
|
|
* returns the corresponding character mapping.
|
|
|
|
*/
|
|
|
|
static const int* __guac_terminal_get_char_mapping(char c) {
|
2013-05-24 06:12:01 +00:00
|
|
|
|
2013-05-25 04:28:14 +00:00
|
|
|
/* Translate character specifier to actual mapping */
|
|
|
|
switch (c) {
|
|
|
|
case 'B': return NULL;
|
|
|
|
case '0': return vt100_map;
|
|
|
|
case 'U': return null_map;
|
|
|
|
case 'K': return user_map;
|
|
|
|
}
|
2013-05-24 06:12:01 +00:00
|
|
|
|
2013-05-25 04:28:14 +00:00
|
|
|
/* Default to Unicode */
|
|
|
|
return NULL;
|
2013-05-24 06:12:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:45:19 +00:00
|
|
|
int guac_terminal_g0_charset(guac_terminal* term, unsigned char c) {
|
2013-05-24 06:12:01 +00:00
|
|
|
|
2013-05-25 04:28:14 +00:00
|
|
|
term->char_mapping[0] = __guac_terminal_get_char_mapping(c);
|
2012-12-09 08:35:37 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
2011-08-05 02:17:44 +00:00
|
|
|
return 0;
|
2013-05-24 06:12:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:45:19 +00:00
|
|
|
int guac_terminal_g1_charset(guac_terminal* term, unsigned char c) {
|
2013-05-24 06:12:01 +00:00
|
|
|
|
2013-05-25 04:28:14 +00:00
|
|
|
term->char_mapping[1] = __guac_terminal_get_char_mapping(c);
|
2013-05-24 06:12:01 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks up the flag specified by the given number and mode. Used by the Set/Reset Mode
|
|
|
|
* functions of the terminal.
|
|
|
|
*/
|
|
|
|
static bool* __guac_terminal_get_flag(guac_terminal* term, int num, char private_mode) {
|
|
|
|
|
|
|
|
if (private_mode == '?') {
|
|
|
|
switch (num) {
|
|
|
|
case 1: return &(term->application_cursor_keys); /* DECCKM */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 20:33:32 +00:00
|
|
|
else if (private_mode == 0) {
|
|
|
|
switch (num) {
|
2013-05-26 05:45:26 +00:00
|
|
|
case 4: return &(term->insert_mode); /* DECIM */
|
2013-05-24 20:33:32 +00:00
|
|
|
case 20: return &(term->automatic_carriage_return); /* LF/NL */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 06:12:01 +00:00
|
|
|
/* Unknown flag */
|
|
|
|
return NULL;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 19:53:47 +00:00
|
|
|
/**
|
|
|
|
* Parses an xterm SGR sequence specifying the RGB values of a color.
|
|
|
|
*
|
|
|
|
* @param argc
|
|
|
|
* The number of arguments within the argv array.
|
|
|
|
*
|
|
|
|
* @param argv
|
|
|
|
* The SGR arguments to parse, with the first relevant argument the
|
|
|
|
* red component of the RGB color.
|
|
|
|
*
|
|
|
|
* @param color
|
|
|
|
* The guac_terminal_color structure which should receive the parsed
|
|
|
|
* color values.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The number of arguments parsed, or zero if argv does not contain
|
|
|
|
* enough elements to represent an RGB color.
|
|
|
|
*/
|
|
|
|
static int guac_terminal_parse_xterm256_rgb(int argc, const int* argv,
|
|
|
|
guac_terminal_color* color) {
|
|
|
|
|
|
|
|
/* RGB color palette entries require three arguments */
|
|
|
|
if (argc < 3)
|
|
|
|
return 0;
|
|
|
|
|
2017-04-23 20:36:46 +00:00
|
|
|
/* Read RGB components from arguments */
|
|
|
|
int red = argv[0];
|
|
|
|
int green = argv[1];
|
|
|
|
int blue = argv[2];
|
|
|
|
|
|
|
|
/* Ignore if components are out of range */
|
|
|
|
if ( red < 0 || red > 255
|
|
|
|
|| green < 0 || green > 255
|
|
|
|
|| blue < 0 || blue > 255)
|
|
|
|
return 3;
|
|
|
|
|
2017-04-23 19:53:47 +00:00
|
|
|
/* Store RGB components */
|
2017-04-23 20:36:46 +00:00
|
|
|
color->red = (uint8_t) red;
|
|
|
|
color->green = (uint8_t) green;
|
|
|
|
color->blue = (uint8_t) blue;
|
2017-04-23 19:53:47 +00:00
|
|
|
|
|
|
|
/* Color is not from the palette */
|
|
|
|
color->palette_index = -1;
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses an xterm SGR sequence specifying the index of a color within the
|
|
|
|
* 256-color palette.
|
|
|
|
*
|
|
|
|
* @param argc
|
|
|
|
* The number of arguments within the argv array.
|
|
|
|
*
|
|
|
|
* @param argv
|
|
|
|
* The SGR arguments to parse, with the first relevant argument being
|
|
|
|
* the index of the color.
|
|
|
|
*
|
|
|
|
* @param color
|
|
|
|
* The guac_terminal_color structure which should receive the parsed
|
|
|
|
* color values.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The number of arguments parsed, or zero if the palette index is
|
2017-04-23 20:36:46 +00:00
|
|
|
* absent.
|
2017-04-23 19:53:47 +00:00
|
|
|
*/
|
|
|
|
static int guac_terminal_parse_xterm256_index(int argc, const int* argv,
|
|
|
|
guac_terminal_color* color) {
|
|
|
|
|
|
|
|
/* 256-color palette entries require only one argument */
|
|
|
|
if (argc < 1)
|
|
|
|
return 0;
|
|
|
|
|
2017-04-23 20:36:46 +00:00
|
|
|
/* Ignore if palette index is out of bounds */
|
2017-04-23 19:53:47 +00:00
|
|
|
int index = argv[0];
|
|
|
|
if (index < 0 || index > 255)
|
2017-04-23 20:36:46 +00:00
|
|
|
return 1;
|
2017-04-23 19:53:47 +00:00
|
|
|
|
|
|
|
/* Copy palette entry */
|
|
|
|
*color = guac_terminal_palette[index];
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses an xterm SGR sequence specifying the index of a color within the
|
|
|
|
* 256-color palette, or specfying the RGB values of a color. The number of
|
|
|
|
* arguments required by these sequences varies. If a 256-color sequence is
|
|
|
|
* recognized, the number of arguments parsed is returned.
|
|
|
|
*
|
|
|
|
* @param argc
|
|
|
|
* The number of arguments within the argv array.
|
|
|
|
*
|
|
|
|
* @param argv
|
|
|
|
* The SGR arguments to parse, with the first relevant argument being
|
|
|
|
* the first element of the array. In the case of an xterm 256-color
|
|
|
|
* SGR sequence, the first element here will be either 2, for an
|
|
|
|
* RGB color, or 5, for a 256-color palette index. All other values
|
|
|
|
* are invalid and will not be parsed.
|
|
|
|
*
|
|
|
|
* @param color
|
|
|
|
* The guac_terminal_color structure which should receive the parsed
|
|
|
|
* color values.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The number of arguments parsed, or zero if argv does not point to
|
|
|
|
* the first element of an xterm 256-color SGR sequence.
|
|
|
|
*/
|
|
|
|
static int guac_terminal_parse_xterm256(int argc, const int* argv,
|
|
|
|
guac_terminal_color* color) {
|
|
|
|
|
|
|
|
/* All 256-color codes must have at least a type */
|
|
|
|
if (argc < 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (argv[0]) {
|
|
|
|
|
|
|
|
/* RGB */
|
|
|
|
case 2:
|
|
|
|
return guac_terminal_parse_xterm256_rgb(
|
|
|
|
argc - 1, &argv[1], color) + 1;
|
|
|
|
|
|
|
|
/* Palette index */
|
|
|
|
case 5:
|
|
|
|
return guac_terminal_parse_xterm256_index(
|
|
|
|
argc - 1, &argv[1], color) + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invalid type */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:45:19 +00:00
|
|
|
int guac_terminal_csi(guac_terminal* term, unsigned char c) {
|
2011-08-05 02:17:44 +00:00
|
|
|
|
2011-08-05 02:36:04 +00:00
|
|
|
/* CSI function arguments */
|
|
|
|
static int argc = 0;
|
2011-08-05 21:39:11 +00:00
|
|
|
static int argv[16] = {0};
|
2011-08-05 02:36:04 +00:00
|
|
|
|
2013-05-22 18:38:39 +00:00
|
|
|
/* Sequence prefix, if any */
|
|
|
|
static char private_mode_character = 0;
|
2013-05-22 18:08:38 +00:00
|
|
|
|
2011-08-05 02:36:04 +00:00
|
|
|
/* Argument building counter and buffer */
|
|
|
|
static int argv_length = 0;
|
|
|
|
static char argv_buffer[256];
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
/* Digits get concatenated into argv */
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
|
|
|
|
/* Concatenate digit if there is space in buffer */
|
2011-08-05 02:36:04 +00:00
|
|
|
if (argv_length < sizeof(argv_buffer)-1)
|
|
|
|
argv_buffer[argv_length++] = c;
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-22 18:38:39 +00:00
|
|
|
/* Specific non-digits stop the parameter, and possibly the sequence */
|
|
|
|
else if ((c >= 0x40 && c <= 0x7E) || c == ';') {
|
2011-08-05 02:17:44 +00:00
|
|
|
|
2011-08-06 00:09:44 +00:00
|
|
|
int i, row, col, amount;
|
2013-05-24 06:12:01 +00:00
|
|
|
bool* flag;
|
2011-08-05 19:41:21 +00:00
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
/* At most 16 parameters */
|
2011-08-05 02:36:04 +00:00
|
|
|
if (argc < 16) {
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
/* Finish parameter */
|
2011-08-05 02:36:04 +00:00
|
|
|
argv_buffer[argv_length] = 0;
|
|
|
|
argv[argc++] = atoi(argv_buffer);
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
/* Prepare for next parameter */
|
2011-08-05 02:36:04 +00:00
|
|
|
argv_length = 0;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle CSI functions */
|
|
|
|
switch (c) {
|
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
/* @: Insert characters (scroll right) */
|
|
|
|
case '@':
|
|
|
|
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
|
|
|
|
/* Scroll right by amount */
|
|
|
|
if (term->cursor_col + amount < term->term_width)
|
|
|
|
guac_terminal_copy_columns(term, term->cursor_row,
|
|
|
|
term->cursor_col, term->term_width - amount - 1,
|
|
|
|
amount);
|
|
|
|
|
|
|
|
/* Clear left */
|
|
|
|
guac_terminal_clear_columns(term, term->cursor_row,
|
|
|
|
term->cursor_col, term->cursor_col + amount - 1);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2011-08-06 00:09:44 +00:00
|
|
|
/* A: Move up */
|
|
|
|
case 'A':
|
|
|
|
|
|
|
|
/* Get move amount */
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
|
|
|
|
/* Move cursor */
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term,
|
|
|
|
term->cursor_row - amount,
|
|
|
|
term->cursor_col);
|
2011-08-06 00:09:44 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* B: Move down */
|
2013-05-22 06:27:34 +00:00
|
|
|
case 'e':
|
2011-08-06 00:09:44 +00:00
|
|
|
case 'B':
|
|
|
|
|
|
|
|
/* Get move amount */
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
|
|
|
|
/* Move cursor */
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term,
|
|
|
|
term->cursor_row + amount,
|
|
|
|
term->cursor_col);
|
2011-08-06 00:09:44 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* C: Move right */
|
2013-05-22 06:57:55 +00:00
|
|
|
case 'a':
|
2011-08-06 00:09:44 +00:00
|
|
|
case 'C':
|
|
|
|
|
|
|
|
/* Get move amount */
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
|
|
|
|
/* Move cursor */
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term,
|
|
|
|
term->cursor_row,
|
|
|
|
term->cursor_col + amount);
|
2011-08-06 00:09:44 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
/* D: Move left */
|
|
|
|
case 'D':
|
2011-08-05 19:41:21 +00:00
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
/* Get move amount */
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
2011-08-05 19:41:21 +00:00
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
/* Move cursor */
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term,
|
|
|
|
term->cursor_row,
|
|
|
|
term->cursor_col - amount);
|
2011-08-05 19:41:21 +00:00
|
|
|
|
2011-08-06 05:59:42 +00:00
|
|
|
break;
|
|
|
|
|
2013-05-22 06:20:13 +00:00
|
|
|
/* E: Move cursor down given number rows, column 1 */
|
|
|
|
case 'E':
|
|
|
|
|
|
|
|
/* Get move amount */
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
|
2017-01-29 06:48:45 +00:00
|
|
|
/* Move cursor down, reset to column 1 */
|
|
|
|
guac_terminal_move_cursor(term,
|
|
|
|
term->cursor_row + amount,
|
|
|
|
0);
|
2013-05-22 06:20:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* F: Move cursor up given number rows, column 1 */
|
|
|
|
case 'F':
|
|
|
|
|
|
|
|
/* Get move amount */
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
|
2017-01-29 06:48:45 +00:00
|
|
|
/* Move cursor up , reset to column 1 */
|
|
|
|
guac_terminal_move_cursor(term,
|
|
|
|
term->cursor_row - amount,
|
|
|
|
0);
|
2013-05-22 06:20:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-05-21 07:47:55 +00:00
|
|
|
/* G: Move cursor, current row */
|
2013-05-24 22:00:54 +00:00
|
|
|
case '`':
|
2013-05-21 07:47:55 +00:00
|
|
|
case 'G':
|
|
|
|
col = argv[0]; if (col != 0) col--;
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term, term->cursor_row, col);
|
2013-05-21 07:47:55 +00:00
|
|
|
break;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
/* H: Move cursor */
|
2013-05-22 06:27:34 +00:00
|
|
|
case 'f':
|
2011-08-05 02:17:44 +00:00
|
|
|
case 'H':
|
2011-08-05 21:39:11 +00:00
|
|
|
|
|
|
|
row = argv[0]; if (row != 0) row--;
|
|
|
|
col = argv[1]; if (col != 0) col--;
|
|
|
|
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term, row, col);
|
2011-08-05 02:17:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* J: Erase display */
|
|
|
|
case 'J':
|
2011-08-05 07:20:09 +00:00
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
/* Erase from cursor to end of display */
|
2011-08-05 07:20:09 +00:00
|
|
|
if (argv[0] == 0)
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal_clear_range(term,
|
2011-08-05 07:20:09 +00:00
|
|
|
term->cursor_row, term->cursor_col,
|
2013-03-29 09:51:31 +00:00
|
|
|
term->term_height-1, term->term_width-1);
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
/* Erase from start to cursor */
|
2011-08-05 07:20:09 +00:00
|
|
|
else if (argv[0] == 1)
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal_clear_range(term,
|
2011-08-05 07:20:09 +00:00
|
|
|
0, 0,
|
2013-03-29 09:51:31 +00:00
|
|
|
term->cursor_row, term->cursor_col);
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
/* Entire screen */
|
2013-05-24 20:44:51 +00:00
|
|
|
else if (argv[0] == 2 || argv[0] == 3)
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_clear_range(term,
|
|
|
|
0, 0, term->term_height - 1, term->term_width - 1);
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* K: Erase line */
|
|
|
|
case 'K':
|
|
|
|
|
|
|
|
/* Erase from cursor to end of line */
|
2011-08-05 07:20:09 +00:00
|
|
|
if (argv[0] == 0)
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_clear_columns(term, term->cursor_row,
|
|
|
|
term->cursor_col, term->term_width - 1);
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
/* Erase from start to cursor */
|
2011-08-05 07:20:09 +00:00
|
|
|
else if (argv[0] == 1)
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_clear_columns(term, term->cursor_row,
|
|
|
|
0, term->cursor_col);
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
/* Erase line */
|
2011-08-05 07:20:09 +00:00
|
|
|
else if (argv[0] == 2)
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_clear_columns(term, term->cursor_row,
|
|
|
|
0, term->term_width - 1);
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2011-08-10 01:32:54 +00:00
|
|
|
/* L: Insert blank lines (scroll down) */
|
|
|
|
case 'L':
|
|
|
|
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal_scroll_down(term,
|
2011-08-10 01:32:54 +00:00
|
|
|
term->cursor_row, term->scroll_end, amount);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2011-08-10 16:31:12 +00:00
|
|
|
/* M: Delete lines (scroll up) */
|
2011-08-10 07:16:50 +00:00
|
|
|
case 'M':
|
|
|
|
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal_scroll_up(term,
|
2011-08-10 07:16:50 +00:00
|
|
|
term->cursor_row, term->scroll_end, amount);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2011-08-10 16:31:12 +00:00
|
|
|
/* P: Delete characters (scroll left) */
|
|
|
|
case 'P':
|
|
|
|
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
|
|
|
|
/* Scroll left by amount */
|
|
|
|
if (term->cursor_col + amount < term->term_width)
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_copy_columns(term, term->cursor_row,
|
|
|
|
term->cursor_col + amount, term->term_width - 1,
|
|
|
|
-amount);
|
2011-08-10 16:31:12 +00:00
|
|
|
|
|
|
|
/* Clear right */
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_clear_columns(term, term->cursor_row,
|
|
|
|
term->term_width - amount, term->term_width - 1);
|
2011-08-10 16:31:12 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-04-08 09:14:58 +00:00
|
|
|
/* X: Erase characters (no scroll) */
|
|
|
|
case 'X':
|
|
|
|
|
|
|
|
amount = argv[0];
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
|
|
|
|
/* Clear characters */
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_clear_columns(term, term->cursor_row,
|
|
|
|
term->cursor_col, term->cursor_col + amount - 1);
|
2013-04-08 09:14:58 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-05-24 22:00:54 +00:00
|
|
|
/* ]: Linux Private CSI */
|
|
|
|
case ']':
|
|
|
|
/* Explicitly ignored */
|
|
|
|
break;
|
|
|
|
|
2013-05-22 05:37:53 +00:00
|
|
|
/* c: Identify */
|
|
|
|
case 'c':
|
2013-05-26 01:20:35 +00:00
|
|
|
if (argv[0] == 0 && private_mode_character == 0)
|
2013-05-26 05:45:26 +00:00
|
|
|
guac_terminal_send_string(term, GUAC_TERMINAL_VT102_ID);
|
2013-05-22 05:37:53 +00:00
|
|
|
break;
|
2011-08-10 18:03:38 +00:00
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
/* d: Move cursor, current col */
|
|
|
|
case 'd':
|
|
|
|
row = argv[0]; if (row != 0) row--;
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term, row, term->cursor_col);
|
2013-05-21 07:35:18 +00:00
|
|
|
break;
|
2011-08-10 18:03:38 +00:00
|
|
|
|
2013-05-26 08:49:47 +00:00
|
|
|
/* g: Clear tab */
|
|
|
|
case 'g':
|
|
|
|
|
|
|
|
/* Clear tab at current location */
|
|
|
|
if (argv[0] == 0)
|
|
|
|
guac_terminal_unset_tab(term, term->cursor_col);
|
|
|
|
|
|
|
|
/* Clear all tabs */
|
|
|
|
else if (argv[0] == 3)
|
|
|
|
guac_terminal_clear_tabs(term);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-05-22 18:08:38 +00:00
|
|
|
/* h: Set Mode */
|
|
|
|
case 'h':
|
2013-05-24 06:12:01 +00:00
|
|
|
|
|
|
|
/* Look up flag and set */
|
|
|
|
flag = __guac_terminal_get_flag(term, argv[0], private_mode_character);
|
|
|
|
if (flag != NULL)
|
|
|
|
*flag = true;
|
2013-05-22 18:08:38 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* l: Reset Mode */
|
|
|
|
case 'l':
|
2013-05-24 06:12:01 +00:00
|
|
|
|
|
|
|
/* Look up flag and clear */
|
|
|
|
flag = __guac_terminal_get_flag(term, argv[0], private_mode_character);
|
|
|
|
if (flag != NULL)
|
|
|
|
*flag = false;
|
2013-05-22 18:08:38 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
/* m: Set graphics rendition */
|
|
|
|
case 'm':
|
2011-08-10 18:03:38 +00:00
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
|
|
|
|
int value = argv[i];
|
|
|
|
|
|
|
|
/* Reset attributes */
|
|
|
|
if (value == 0)
|
|
|
|
term->current_attributes = term->default_char.attributes;
|
|
|
|
|
|
|
|
/* Bold */
|
|
|
|
else if (value == 1)
|
|
|
|
term->current_attributes.bold = true;
|
|
|
|
|
|
|
|
/* Underscore on */
|
|
|
|
else if (value == 4)
|
|
|
|
term->current_attributes.underscore = true;
|
|
|
|
|
2013-05-24 22:00:54 +00:00
|
|
|
/* Reverse video */
|
|
|
|
else if (value == 7)
|
|
|
|
term->current_attributes.reverse = true;
|
|
|
|
|
|
|
|
/* Normal intensity (not bold) */
|
|
|
|
else if (value == 21 || value == 22)
|
|
|
|
term->current_attributes.bold = false;
|
|
|
|
|
|
|
|
/* Reset underscore */
|
|
|
|
else if (value == 24)
|
|
|
|
term->current_attributes.underscore = false;
|
|
|
|
|
|
|
|
/* Reset reverse video */
|
|
|
|
else if (value == 27)
|
|
|
|
term->current_attributes.reverse = false;
|
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
/* Foreground */
|
|
|
|
else if (value >= 30 && value <= 37)
|
2017-04-23 18:02:25 +00:00
|
|
|
term->current_attributes.foreground =
|
|
|
|
guac_terminal_palette[value - 30];
|
2013-05-21 07:35:18 +00:00
|
|
|
|
2017-04-23 19:53:47 +00:00
|
|
|
/* Underscore on, default foreground OR 256-color
|
|
|
|
* foreground */
|
2013-05-21 07:35:18 +00:00
|
|
|
else if (value == 38) {
|
2017-04-23 19:53:47 +00:00
|
|
|
|
|
|
|
/* Attempt to set foreground with 256-color entry */
|
|
|
|
int xterm256_length =
|
|
|
|
guac_terminal_parse_xterm256(argc - 1, &argv[i + 1],
|
|
|
|
&term->current_attributes.foreground);
|
|
|
|
|
|
|
|
/* If valid 256-color entry, foreground has been set */
|
|
|
|
if (xterm256_length > 0)
|
|
|
|
i += xterm256_length;
|
|
|
|
|
|
|
|
/* Otherwise interpret as underscore and default
|
|
|
|
* foreground */
|
|
|
|
else {
|
|
|
|
term->current_attributes.underscore = true;
|
|
|
|
term->current_attributes.foreground =
|
|
|
|
term->default_char.attributes.foreground;
|
|
|
|
}
|
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Underscore off, default foreground */
|
|
|
|
else if (value == 39) {
|
|
|
|
term->current_attributes.underscore = false;
|
|
|
|
term->current_attributes.foreground =
|
|
|
|
term->default_char.attributes.foreground;
|
|
|
|
}
|
|
|
|
|
2013-05-24 22:00:54 +00:00
|
|
|
/* Background */
|
|
|
|
else if (value >= 40 && value <= 47)
|
2017-04-23 18:02:25 +00:00
|
|
|
term->current_attributes.background =
|
|
|
|
guac_terminal_palette[value - 40];
|
2013-05-24 22:00:54 +00:00
|
|
|
|
2017-04-23 19:53:47 +00:00
|
|
|
/* 256-color background */
|
|
|
|
else if (value == 48)
|
|
|
|
i += guac_terminal_parse_xterm256(argc - 1, &argv[i + 1],
|
|
|
|
&term->current_attributes.background);
|
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
/* Reset background */
|
|
|
|
else if (value == 49)
|
|
|
|
term->current_attributes.background =
|
|
|
|
term->default_char.attributes.background;
|
|
|
|
|
2017-04-24 00:22:47 +00:00
|
|
|
/* Intense foreground */
|
|
|
|
else if (value >= 90 && value <= 97)
|
|
|
|
term->current_attributes.foreground =
|
|
|
|
guac_terminal_palette[value - 90
|
|
|
|
+ GUAC_TERMINAL_FIRST_INTENSE];
|
|
|
|
|
|
|
|
/* Intense background */
|
|
|
|
else if (value >= 100 && value <= 107)
|
|
|
|
term->current_attributes.background =
|
|
|
|
guac_terminal_palette[value - 100
|
|
|
|
+ GUAC_TERMINAL_FIRST_INTENSE];
|
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-05-26 03:24:44 +00:00
|
|
|
/* n: Status report */
|
|
|
|
case 'n':
|
|
|
|
|
|
|
|
/* Device status report */
|
|
|
|
if (argv[0] == 5 && private_mode_character == 0)
|
2013-05-26 05:45:26 +00:00
|
|
|
guac_terminal_send_string(term, GUAC_TERMINAL_OK);
|
2013-05-26 03:24:44 +00:00
|
|
|
|
|
|
|
/* Cursor position report */
|
|
|
|
else if (argv[0] == 6 && private_mode_character == 0)
|
2013-05-26 05:45:26 +00:00
|
|
|
guac_terminal_sendf(term, "\x1B[%i;%iR", term->cursor_row+1, term->cursor_col+1);
|
2013-05-26 03:24:44 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-05-24 22:00:54 +00:00
|
|
|
/* q: Set keyboard LEDs */
|
|
|
|
case 'q':
|
|
|
|
/* Explicitly ignored */
|
|
|
|
break;
|
|
|
|
|
2013-05-21 07:35:18 +00:00
|
|
|
/* r: Set scrolling region */
|
|
|
|
case 'r':
|
2013-05-22 05:37:53 +00:00
|
|
|
|
|
|
|
/* If parameters given, set region */
|
|
|
|
if (argc == 2) {
|
|
|
|
term->scroll_start = argv[0]-1;
|
|
|
|
term->scroll_end = argv[1]-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, reset scrolling region */
|
|
|
|
else {
|
|
|
|
term->scroll_start = 0;
|
|
|
|
term->scroll_end = term->term_height - 1;
|
|
|
|
}
|
|
|
|
|
2011-08-10 18:03:38 +00:00
|
|
|
break;
|
|
|
|
|
2013-05-24 22:00:54 +00:00
|
|
|
/* Save Cursor */
|
|
|
|
case 's':
|
|
|
|
term->saved_cursor_row = term->cursor_row;
|
|
|
|
term->saved_cursor_col = term->cursor_col;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Restore Cursor */
|
|
|
|
case 'u':
|
2017-01-29 06:48:45 +00:00
|
|
|
guac_terminal_move_cursor(term,
|
|
|
|
term->saved_cursor_row,
|
|
|
|
term->saved_cursor_col);
|
2013-05-24 22:00:54 +00:00
|
|
|
break;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
/* Warn of unhandled codes */
|
|
|
|
default:
|
2013-04-08 09:14:58 +00:00
|
|
|
if (c != ';') {
|
|
|
|
|
2017-04-23 19:58:32 +00:00
|
|
|
guac_client_log(term->client, GUAC_LOG_DEBUG,
|
2013-04-08 09:14:58 +00:00
|
|
|
"Unhandled CSI sequence: %c", c);
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++)
|
2017-04-23 19:58:32 +00:00
|
|
|
guac_client_log(term->client, GUAC_LOG_DEBUG,
|
2013-04-08 09:14:58 +00:00
|
|
|
" -> argv[%i] = %i", i, argv[i]);
|
|
|
|
|
|
|
|
}
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If not a semicolon, end of CSI sequence */
|
2011-08-05 02:36:04 +00:00
|
|
|
if (c != ';') {
|
2012-12-09 08:35:37 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
2011-08-05 02:36:04 +00:00
|
|
|
|
2011-08-05 21:39:11 +00:00
|
|
|
/* Reset parameters */
|
|
|
|
for (i=0; i<argc; i++)
|
|
|
|
argv[i] = 0;
|
|
|
|
|
2013-05-22 18:38:39 +00:00
|
|
|
/* Reset private mode character */
|
|
|
|
private_mode_character = 0;
|
2013-05-22 18:08:38 +00:00
|
|
|
|
2011-08-05 02:36:04 +00:00
|
|
|
/* Reset argument counters */
|
|
|
|
argc = 0;
|
|
|
|
argv_length = 0;
|
|
|
|
}
|
2011-08-05 02:17:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-22 18:38:39 +00:00
|
|
|
/* Set private mode character if given and unset */
|
|
|
|
else if (c >= 0x3A && c <= 0x3F && private_mode_character == 0)
|
|
|
|
private_mode_character = c;
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-05 23:36:49 +00:00
|
|
|
int guac_terminal_set_directory(guac_terminal* term, unsigned char c) {
|
2013-10-21 02:09:15 +00:00
|
|
|
|
|
|
|
static char filename[2048];
|
|
|
|
static int length = 0;
|
|
|
|
|
|
|
|
/* Stop on ECMA-48 ST (String Terminator */
|
|
|
|
if (c == 0x9C || c == 0x5C || c == 0x07) {
|
|
|
|
filename[length++] = '\0';
|
|
|
|
term->char_handler = guac_terminal_echo;
|
2014-05-07 00:14:40 +00:00
|
|
|
if (term->upload_path_handler)
|
|
|
|
term->upload_path_handler(term->client, filename);
|
2014-11-29 01:20:02 +00:00
|
|
|
else
|
|
|
|
guac_client_log(term->client, GUAC_LOG_DEBUG,
|
2016-03-01 05:41:56 +00:00
|
|
|
"Cannot set upload path. File transfer is not enabled.");
|
2013-10-21 02:09:15 +00:00
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, store character */
|
|
|
|
else if (length < sizeof(filename)-1)
|
|
|
|
filename[length++] = c;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-05 23:36:49 +00:00
|
|
|
int guac_terminal_download(guac_terminal* term, unsigned char c) {
|
2013-10-21 02:09:15 +00:00
|
|
|
|
|
|
|
static char filename[2048];
|
|
|
|
static int length = 0;
|
|
|
|
|
|
|
|
/* Stop on ECMA-48 ST (String Terminator */
|
|
|
|
if (c == 0x9C || c == 0x5C || c == 0x07) {
|
|
|
|
filename[length++] = '\0';
|
|
|
|
term->char_handler = guac_terminal_echo;
|
2014-05-07 00:14:40 +00:00
|
|
|
if (term->file_download_handler)
|
|
|
|
term->file_download_handler(term->client, filename);
|
2014-11-29 01:20:02 +00:00
|
|
|
else
|
|
|
|
guac_client_log(term->client, GUAC_LOG_DEBUG,
|
2016-03-01 05:41:56 +00:00
|
|
|
"Cannot send file. File transfer is not enabled.");
|
2013-10-21 02:09:15 +00:00
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, store character */
|
|
|
|
else if (length < sizeof(filename)-1)
|
|
|
|
filename[length++] = c;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-13 00:28:25 +00:00
|
|
|
int guac_terminal_open_pipe_stream(guac_terminal* term, unsigned char c) {
|
|
|
|
|
|
|
|
static char stream_name[2048];
|
|
|
|
static int length = 0;
|
|
|
|
|
|
|
|
/* Open pipe on ECMA-48 ST (String Terminator) */
|
|
|
|
if (c == 0x9C || c == 0x5C || c == 0x07) {
|
2016-01-13 01:07:32 +00:00
|
|
|
|
|
|
|
/* End stream name string */
|
2016-01-13 00:28:25 +00:00
|
|
|
stream_name[length++] = '\0';
|
|
|
|
length = 0;
|
2016-01-13 01:07:32 +00:00
|
|
|
|
|
|
|
/* Open new pipe stream */
|
2016-01-14 01:38:13 +00:00
|
|
|
guac_terminal_pipe_stream_open(term, stream_name);
|
|
|
|
|
|
|
|
/* Return to echo mode */
|
|
|
|
term->char_handler = guac_terminal_echo;
|
2016-01-13 01:07:32 +00:00
|
|
|
|
2016-01-13 00:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, store character within stream name */
|
|
|
|
else if (length < sizeof(stream_name)-1)
|
|
|
|
stream_name[length++] = c;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int guac_terminal_close_pipe_stream(guac_terminal* term, unsigned char c) {
|
|
|
|
|
|
|
|
/* Handle closure on ECMA-48 ST (String Terminator) */
|
|
|
|
if (c == 0x9C || c == 0x5C || c == 0x07) {
|
2016-01-13 01:07:32 +00:00
|
|
|
|
|
|
|
/* Close any existing pipe */
|
2016-01-14 01:38:13 +00:00
|
|
|
guac_terminal_pipe_stream_close(term);
|
2016-01-13 01:07:32 +00:00
|
|
|
|
2016-01-14 01:38:13 +00:00
|
|
|
/* Return to echo mode */
|
|
|
|
term->char_handler = guac_terminal_echo;
|
2016-01-13 01:07:32 +00:00
|
|
|
|
2016-01-13 00:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore all other characters */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-23 22:13:37 +00:00
|
|
|
int guac_terminal_window_title(guac_terminal* term, unsigned char c) {
|
|
|
|
|
|
|
|
static int position = 0;
|
|
|
|
static char title[4096];
|
|
|
|
|
|
|
|
guac_socket* socket = term->client->socket;
|
|
|
|
|
|
|
|
/* Stop on ECMA-48 ST (String Terminator */
|
|
|
|
if (c == 0x9C || c == 0x5C || c == 0x07) {
|
|
|
|
|
|
|
|
/* Terminate and reset stored title */
|
|
|
|
title[position] = '\0';
|
|
|
|
position = 0;
|
|
|
|
|
|
|
|
/* Send title as connection name */
|
|
|
|
guac_protocol_send_name(socket, title);
|
|
|
|
guac_socket_flush(socket);
|
|
|
|
|
|
|
|
term->char_handler = guac_terminal_echo;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store all other characters within title, space permitting */
|
|
|
|
else if (position < sizeof(title) - 1)
|
|
|
|
title[position++] = (char) c;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-23 20:59:03 +00:00
|
|
|
int guac_terminal_xterm_palette(guac_terminal* term, unsigned char c) {
|
|
|
|
|
|
|
|
/* NOTE: Currently unimplemented. Attempts to set the 256-color palette
|
|
|
|
* are ignored. */
|
|
|
|
|
|
|
|
/* Stop on ECMA-48 ST (String Terminator */
|
|
|
|
if (c == 0x9C || c == 0x5C || c == 0x07)
|
|
|
|
term->char_handler = guac_terminal_echo;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:45:19 +00:00
|
|
|
int guac_terminal_osc(guac_terminal* term, unsigned char c) {
|
2013-10-21 02:09:15 +00:00
|
|
|
|
|
|
|
static int operation = 0;
|
|
|
|
|
|
|
|
/* If digit, append to operation */
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
operation = operation * 10 + c - '0';
|
|
|
|
|
|
|
|
/* If end of parameter, check value */
|
|
|
|
else if (c == ';') {
|
|
|
|
|
|
|
|
/* Download OSC */
|
|
|
|
if (operation == 482200)
|
2014-05-05 23:36:49 +00:00
|
|
|
term->char_handler = guac_terminal_download;
|
2013-10-21 02:09:15 +00:00
|
|
|
|
|
|
|
/* Set upload directory OSC */
|
|
|
|
else if (operation == 482201)
|
2014-05-05 23:36:49 +00:00
|
|
|
term->char_handler = guac_terminal_set_directory;
|
2013-10-21 02:09:15 +00:00
|
|
|
|
2016-01-13 00:28:25 +00:00
|
|
|
/* Open and redirect output to pipe stream OSC */
|
|
|
|
else if (operation == 482202)
|
|
|
|
term->char_handler = guac_terminal_open_pipe_stream;
|
|
|
|
|
|
|
|
/* Close pipe stream OSC */
|
|
|
|
else if (operation == 482203)
|
|
|
|
term->char_handler = guac_terminal_close_pipe_stream;
|
|
|
|
|
2017-04-23 22:13:37 +00:00
|
|
|
/* Set window title OSC */
|
|
|
|
else if (operation == 0 || operation == 2)
|
|
|
|
term->char_handler = guac_terminal_window_title;
|
|
|
|
|
2017-04-23 20:59:03 +00:00
|
|
|
/* xterm 256-color palette redefinition */
|
|
|
|
else if (operation == 4)
|
|
|
|
term->char_handler = guac_terminal_xterm_palette;
|
|
|
|
|
2013-10-21 02:09:15 +00:00
|
|
|
/* Reset parameter for next OSC */
|
|
|
|
operation = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop on ECMA-48 ST (String Terminator */
|
|
|
|
else if (c == 0x9C || c == 0x5C || c == 0x07)
|
|
|
|
term->char_handler = guac_terminal_echo;
|
|
|
|
|
2014-06-11 22:04:39 +00:00
|
|
|
/* Stop on unrecognized character */
|
|
|
|
else {
|
2017-04-23 19:58:32 +00:00
|
|
|
guac_client_log(term->client, GUAC_LOG_DEBUG,
|
|
|
|
"Unexpected character in OSC: 0x%X", c);
|
2014-06-11 22:04:39 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
|
|
|
}
|
|
|
|
|
2011-08-05 02:17:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:45:19 +00:00
|
|
|
int guac_terminal_ctrl_func(guac_terminal* term, unsigned char c) {
|
2013-05-22 06:38:35 +00:00
|
|
|
|
|
|
|
int row;
|
|
|
|
|
|
|
|
/* Build character with current attributes */
|
|
|
|
guac_terminal_char guac_char;
|
|
|
|
guac_char.value = 'E';
|
|
|
|
guac_char.attributes = term->current_attributes;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
/* Alignment test (fill screen with E's) */
|
|
|
|
case '8':
|
|
|
|
|
|
|
|
for (row=0; row<term->term_height; row++)
|
|
|
|
guac_terminal_set_columns(term, row, 0, term->term_width-1, &guac_char);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
term->char_handler = guac_terminal_echo;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|