aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/terminal/terminaldispatcher.cc
blob: 7515ca703e64b5460d87c8647ec1c1a79c2495c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
    Mosh: the mobile shell
    Copyright 2012 Keith Winstein

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations including
    the two.

    You must obey the GNU General Public License in all respects for all
    of the code used other than OpenSSL. If you modify file(s) with this
    exception, you may extend this exception to your version of the
    file(s), but you are not obligated to do so. If you do not wish to do
    so, delete this exception statement from your version. If you delete
    this exception statement from all source files in the program, then
    also delete it here.
*/

#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include "src/terminal/parseraction.h"
#include "src/terminal/terminalframebuffer.h"
#include "terminaldispatcher.h"

using namespace Terminal;

static const size_t MAXIMUM_CLIPBOARD_SIZE = 16 * 1024;

Dispatcher::Dispatcher()
  : params(), parsed_params(), parsed( false ), dispatch_chars(), OSC_string(), terminal_to_host()
{}

void Dispatcher::newparamchar( const Parser::Param* act )
{
  assert( act->char_present );
  assert( ( act->ch == ';' ) || ( ( act->ch >= '0' ) && ( act->ch <= '9' ) ) );
  if ( params.length() < 100 ) {
    /* enough for 16 five-char params plus 15 semicolons */
    params.push_back( act->ch );
  }
  parsed = false;
}

void Dispatcher::collect( const Parser::Collect* act )
{
  assert( act->char_present );
  if ( ( dispatch_chars.length() < 8 ) /* never should need more than 2 */
       && ( act->ch <= 255 ) ) {       /* ignore non-8-bit */
    dispatch_chars.push_back( act->ch );
  }
}

void Dispatcher::clear( const Parser::Clear* act __attribute( ( unused ) ) )
{
  params.clear();
  dispatch_chars.clear();
  parsed = false;
}

void Dispatcher::parse_params( void )
{
  if ( parsed ) {
    return;
  }

  parsed_params.clear();
  const char* str = params.c_str();
  const char* segment_begin = str;

  while ( 1 ) {
    const char* segment_end = strchr( segment_begin, ';' );
    if ( segment_end == NULL ) {
      break;
    }

    errno = 0;
    char* endptr;
    long val = strtol( segment_begin, &endptr, 10 );
    if ( endptr == segment_begin ) {
      val = -1;
    }

    if ( val > PARAM_MAX || errno == ERANGE ) {
      val = -1;
      errno = 0;
    }

    if ( errno == 0 || segment_begin == endptr ) {
      parsed_params.push_back( val );
    }

    segment_begin = segment_end + 1;
  }

  /* get last param */
  errno = 0;
  char* endptr;
  long val = strtol( segment_begin, &endptr, 10 );
  if ( endptr == segment_begin ) {
    val = -1;
  }

  if ( val > PARAM_MAX || errno == ERANGE ) {
    val = -1;
    errno = 0;
  }

  if ( errno == 0 || segment_begin == endptr ) {
    parsed_params.push_back( val );
  }

  parsed = true;
}

int Dispatcher::getparam( size_t N, int defaultval )
{
  int ret = defaultval;
  if ( !parsed ) {
    parse_params();
  }

  if ( parsed_params.size() > N ) {
    ret = parsed_params[N];
  }

  if ( ret < 1 )
    ret = defaultval;

  return ret;
}

int Dispatcher::param_count( void )
{
  if ( !parsed ) {
    parse_params();
  }

  return parsed_params.size();
}

std::string Dispatcher::str( void )
{
  char assum[64];
  snprintf( assum, 64, "[dispatch=\"%s\" params=\"%s\"]", dispatch_chars.c_str(), params.c_str() );
  return std::string( assum );
}

/* construct on first use to avoid static initialization order crash */
DispatchRegistry& Terminal::get_global_dispatch_registry( void )
{
  static DispatchRegistry global_dispatch_registry;
  return global_dispatch_registry;
}

static void register_function( Function_Type type, const std::string& dispatch_chars, Function f )
{
  switch ( type ) {
    case ESCAPE:
      get_global_dispatch_registry().escape.insert( dispatch_map_t::value_type( dispatch_chars, f ) );
      break;
    case CSI:
      get_global_dispatch_registry().CSI.insert( dispatch_map_t::value_type( dispatch_chars, f ) );
      break;
    case CONTROL:
      get_global_dispatch_registry().control.insert( dispatch_map_t::value_type( dispatch_chars, f ) );
      break;
  }
}

Function::Function( Function_Type type,
                    const std::string& dispatch_chars,
                    void ( *s_function )( Framebuffer*, Dispatcher* ),
                    bool s_clears_wrap_state )
  : function( s_function ), clears_wrap_state( s_clears_wrap_state )
{
  register_function( type, dispatch_chars, *this );
}

void Dispatcher::dispatch( Function_Type type, const Parser::Action* act, Framebuffer* fb )
{
  /* add final char to dispatch key */
  if ( ( type == ESCAPE ) || ( type == CSI ) ) {
    assert( act->char_present );
    Parser::Collect act2;
    act2.char_present = true;
    act2.ch = act->ch;
    collect( &act2 );
  }

  dispatch_map_t* map = NULL;
  switch ( type ) {
    case ESCAPE:
      map = &get_global_dispatch_registry().escape;
      break;
    case CSI:
      map = &get_global_dispatch_registry().CSI;
      break;
    case CONTROL:
      map = &get_global_dispatch_registry().control;
      break;
  }

  std::string key = dispatch_chars;
  if ( type == CONTROL ) {
    assert( act->ch <= 255 );
    char ctrlstr[2] = { (char)act->ch, 0 };
    key = std::string( ctrlstr, 1 );
  }

  dispatch_map_t::const_iterator i = map->find( key );
  if ( i == map->end() ) {
    /* unknown function */
    fb->ds.next_print_will_wrap = false;
    return;
  }
  if ( i->second.clears_wrap_state ) {
    fb->ds.next_print_will_wrap = false;
  }
  i->second.function( fb, this );
}

void Dispatcher::OSC_put( const Parser::OSC_Put* act )
{
  assert( act->char_present );
  if ( OSC_string.size() < MAXIMUM_CLIPBOARD_SIZE ) {
    OSC_string.push_back( act->ch );
  }
}

void Dispatcher::OSC_start( const Parser::OSC_Start* act __attribute( ( unused ) ) )
{
  OSC_string.clear();
}

bool Dispatcher::operator==( const Dispatcher& x ) const
{
  return ( params == x.params ) && ( parsed_params == x.parsed_params ) && ( parsed == x.parsed )
         && ( dispatch_chars == x.dispatch_chars ) && ( OSC_string == x.OSC_string )
         && ( terminal_to_host == x.terminal_to_host );
}