File: grt_shell.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (544 lines) | stat: -rw-r--r-- 12,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/* 
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * 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; version 2 of the
 * License.
 * 
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef _WIN32
#include <stdarg.h>
#endif

#include "grt_manager.h"
#include "grt_shell.h"
#include "base/file_functions.h"


using namespace grt;
using namespace bec;


#define SNIPPETS_FILENAME "shell_snippets.txt"
#define HISTORY_FILENAME "shell_history.txt"
#define BOOKMARKS_FILENAME "shell_bookmarks.txt"


ShellBE::ShellBE(GRTManager *grtm, const GRTDispatcher::Ref dispatcher)
  : _grtm(grtm), _dispatcher(dispatcher)
{
  _grt= grtm->get_grt();

  _shell= 0;

  _save_history_size= 0;
  _skip_history = 0;

  _history_ptr= _history.begin();
}


ShellBE::~ShellBE()
{
}


void ShellBE::set_save_directory(const std::string &path)
{
  _savedata_dir= path;
}


grt::ValueRef ShellBE::get_shell_variable(const std::string &varname)
{
  return _shell->get_global_var(varname);
}


void ShellBE::set_saves_history(int size)
{
  _save_history_size= size;
  if (size <= 0)
  {
    _history.clear();
    _history_ptr= _history.end();
  }
  else
  {
    while ((int)_history.size() > size)
      _history.pop_back();
    _history_ptr= _history.end();
  }
}


void ShellBE::run_script_file(const std::string &path)
{
  grt::ModuleLoader *loader= _grt->get_module_loader_for_file(path);
  if (!loader)
    throw std::runtime_error("Unsupported script file "+path);

  if (!loader->run_script_file(path))
    throw std::runtime_error("An error occurred while executing the script "+path);
}



bool ShellBE::run_script(const std::string &script, const std::string &language)
{
  grt::ModuleLoader *loader= _grt->get_module_loader(language);
  if (loader)
    return loader->run_script(script);
  throw std::runtime_error("Language "+language+" is not supported or enabled");
}


void ShellBE::process_line_async(const std::string &line)
{
  GRTShellTask::Ref task= GRTShellTask::create_task("User shell command", _dispatcher, line);

  task->signal_message().connect(boost::bind(&ShellBE::handle_msg, this, _1));
  task->set_handle_messages_from_thread();

  task->signal_finished().connect(boost::bind(&ShellBE::shell_finished_cb, this, _1, _2, line));
  
  _dispatcher->execute_now(task);
}


void ShellBE::shell_finished_cb(ShellCommand result, const std::string &prompt, const std::string &line)
{
  if (result == ShellCommandExit)
  {
    _grtm->terminate();
    _current_statement.clear();
  }
  else if (result == ShellCommandUnknown)
  { 
    if (_current_statement.empty())
      _current_statement= line;
    else
      _current_statement+= "\n"+line;
  }
  else if (result == ShellCommandStatement)
  {
    if (_current_statement.empty())
      _current_statement= line;
    else
      _current_statement+= "\n"+line;

    if (_save_history_size > 0 && _current_statement != "\n" && _current_statement != "")
      save_history_line(_current_statement);

    _current_statement.clear();    
  }
  else
  {
    if (_current_statement.empty())
      _current_statement= line;
    else
      _current_statement+= "\n"+line;

    if (_save_history_size > 0 && _current_statement != "\n" && _current_statement != "")
      save_history_line(_current_statement);

    _current_statement.clear();
  }

  if (_ready_slot)
    _ready_slot(prompt);
}



bool ShellBE::setup(const std::string &lang)
{
  if (!_grt->init_shell(lang))
    return false;

  _shell= _grt->get_shell();
  _grt->get_shell()->set_disable_quit(true);

  _shell->print_welcome();

  start();

  return true;
}


void ShellBE::start()
{
  _skip_history = 1;
  // trigger a command that will in turn make the GUI receive a ready
  // callback with the prompt that should be shown
  process_line_async("print(\"Ready.\\n\")");
}


void ShellBE::save_history_line(const std::string &line)
{
  if (line.empty()) return;
  if (_skip_history > 0)
  {
    --_skip_history;
    return;
  }
  
  //remove empty cmd if it is on top of the stack
  if (!_history.empty() && (*_history.begin()).empty())
    _history.pop_front();

  if (line[line.size()-1] == '\n')
    _history.push_front(line.substr(0, line.size()-1));
  else
    _history.push_front(line);
  if ((int)_history.size() > _save_history_size)
    _history.pop_back();
  _history_ptr= _history.begin();
}


void ShellBE::clear_history()
{
  _history.clear();
  _history_ptr= _history.end();
}



bool ShellBE::previous_history_line(const std::string &current_line,
                                    std::string &line)
{
  if (_history_ptr == _history.end())
    return false;

  bool saved_current = !(_history_ptr == _history.begin());
  
  // save the current line to the history
  if (!current_line.empty() && !saved_current)
  {
    saved_current= true;
    save_history_line(current_line);
  }
  std::list<std::string>::const_iterator tmp= _history_ptr;
  ++tmp;
  if (tmp == _history.end())
    return false;

  if (saved_current)
    line= *++_history_ptr;
  else
    line= *_history_ptr++;

  return true;
}


bool ShellBE::next_history_line(std::string &line)
{
  if (_history_ptr != _history.begin())
  {
    --_history_ptr;
    line= *_history_ptr;
    if (_history_ptr == _history.begin())
    {
      _history.pop_front();
      _history_ptr= _history.begin();
    }
    return true;
  }
  return false;
}

void ShellBE::reset_history_position()
{
  _history_ptr= _history.begin();
}


void ShellBE::set_ready_handler(const boost::function<void (const std::string&)> &slot)
{
  _ready_slot= slot;
}


void ShellBE::set_output_handler(const boost::function<void (const std::string&)> &slot)
{
  _output_slot= slot;
  if (_output_slot)
    flush_shell_output(); // Write out any pending text we might have.
}



void ShellBE::handle_msg(const Message &msg)
{
  // the default msg handler outputs everything to shell
  switch (msg.type)
  {
  case grt::ErrorMsg:
    write_line("ERROR: "+msg.text+"\n");
    break;
  case grt::WarningMsg:
    write_line("WARNING: "+msg.text+"\n");
    break;
  case grt::InfoMsg:
    write_line("INFO: "+msg.text+"\n");
    break;
  case grt::ProgressMsg:
    write_line("Progress: "+msg.text+"\n");
    break;
  case grt::OutputMsg:
    write(msg.text);
    break;
  default:
    write_line("Message: "+msg.text+"\n");
    break;
  }
}


void ShellBE::writef(const char *fmt, ...)
{
  va_list ap;
  char *tmp;
  std::string line;

  va_start(ap, fmt);
  tmp= g_strdup_vprintf(fmt, ap);
  line= tmp;
  g_free(tmp);
  va_end(ap);

  // Cache the text if there is no output slot set yet (usually at app start).
  // Flush this queue when we have an output slot and are running in the main thread currently.
  if (_grtm->is_threaded())
  {
    {
      base::MutexLock lock(_text_queue_mutex);
      _text_queue.push_back(line);
    }

    // if we're in the main thread, flush the message queue
    if (_grtm->in_main_thread() && _output_slot)
      flush_shell_output();
  }
  else 
  {
    // If not threaded print directly (given we have an output slot).
    // The text queue is flushed when an output slot is set.
    if(_output_slot)
      _output_slot(line);
    else
    {
      base::MutexLock lock(_text_queue_mutex);
      _text_queue.push_back(line);
    }
  }
}


void ShellBE::write_line(const std::string &line)
{
  writef("%s\n", line.c_str());
}


void ShellBE::write(const std::string &text)
{
  writef("%s", text.c_str());
}


void ShellBE::flush_shell_output()
{
  if (!_output_slot)
    return;

  std::string line;

  while(true)
  {
    {
      base::MutexLock lock(_text_queue_mutex);
      if (_text_queue.empty())
        break;
      line = _text_queue.front();
      _text_queue.pop_front();
    }
    _output_slot(line);
  }
}


std::vector<std::string> ShellBE::complete_line(const std::string &line, std::string &nprefix)
{
  return _shell->complete_line(line, nprefix);
}


std::string ShellBE::get_snippet_data()
{
  std::string path= make_path(_savedata_dir, SNIPPETS_FILENAME);
  gchar *contents;
  gsize length;
  
  if (g_file_get_contents(path.c_str(), &contents, &length, NULL))
  {
    std::string text= std::string(contents, contents+length);
    g_free(contents);
    return text;
  }
  return "";
}


void ShellBE::set_snippet_data(const std::string &data)
{
  std::string path= make_path(_savedata_dir, SNIPPETS_FILENAME);

  // Make sure path exists, if not create it with privileges 755
  g_mkdir_with_parents(_savedata_dir.c_str(), 0755);

  if (!g_file_set_contents(path.c_str(), data.c_str(), (gssize)data.size(), NULL))
  {
    throw std::runtime_error("Could not save file "+path);
  }
}


std::vector<std::string> ShellBE::get_grt_tree_bookmarks()
{
  return _grt_tree_bookmarks;
}


void ShellBE::add_grt_tree_bookmark(const std::string &path)
{
  if (std::find(_grt_tree_bookmarks.begin(), _grt_tree_bookmarks.end(), path) == _grt_tree_bookmarks.end())
    _grt_tree_bookmarks.push_back(path);
}


void ShellBE::delete_grt_tree_bookmark(const std::string &path)
{
  std::vector<std::string>::iterator iter= std::find(_grt_tree_bookmarks.begin(),
                                                     _grt_tree_bookmarks.end(),
                                                     path);
  if (iter != _grt_tree_bookmarks.end())
    _grt_tree_bookmarks.erase(iter);
}


void ShellBE::store_state()
{
  // Make sure path exists, if not create it with privileges 744
  g_mkdir_with_parents(_savedata_dir.c_str(), 0700);

  {
    std::string path= make_path(_savedata_dir, HISTORY_FILENAME);
    
    FILE *f= base_fopen(path.c_str(), "w+");
    if (!f)
      throw std::runtime_error("Could not save file "+path);
    
    for (std::list<std::string>::const_iterator i= _history.begin();
         i != _history.end(); ++i)
    {
      char **lines= g_strsplit(i->c_str(), "\n", 0);
      for (int j= 0; lines[j]; j++)
        fprintf(f, " %s\n", lines[j]);
      g_strfreev(lines);
      fprintf(f, "\n");
    }
    fclose(f);
  }
  
  {
    std::string path= make_path(_savedata_dir, BOOKMARKS_FILENAME);
    
    FILE *f= base_fopen(path.c_str(), "w+");
    if (!f)
      throw std::runtime_error("Could not save file "+path);
    
    for (std::vector<std::string>::const_iterator i= _grt_tree_bookmarks.begin();
         i != _grt_tree_bookmarks.end(); ++i)
    {
      fprintf(f, "%s\n", i->c_str());
    }
    fclose(f);
  }
}


void ShellBE::restore_state()
{
  {
    char line[1024];
    std::string path= make_path(_savedata_dir, HISTORY_FILENAME);
    std::string command;
    FILE *f= base_fopen(path.c_str(), "r");
    if (f)
    {
      _history.clear();
      
      while (!feof(f) && fgets(line, sizeof(line), f))
      {
        if (*line==' ')
          command+= line+1;
        else
        {
          while (!command.empty() && (command[command.size()-1] == ' ' || command[command.size()-1] == '\n'))
            command= command.substr(0, command.size()-1);

          if (!command.empty())
            _history.push_back(command);
          command= "";
        }
      }
      fclose(f);
    }
    _history_ptr= _history.begin();
  }
  
  {
    char line[1024];
    std::string path= make_path(_savedata_dir, BOOKMARKS_FILENAME);
    FILE *f= base_fopen(path.c_str(), "r");
    if (f)
    {
      bool cleared= false;
      
      while (!feof(f) && fgets(line, sizeof(line), f))
      {
        char *p= strchr(line, '\n');
        if (p) *p= 0;
        if (strlen(line) > 0 && line[0] == '/')
        {
          if (!cleared)
            _grt_tree_bookmarks.clear();
          cleared= true;
          _grt_tree_bookmarks.push_back(g_strstrip(line));
        }
      }
      fclose(f);
    }
    else
    {
      _grt_tree_bookmarks.push_back("/");
    }
  }
}