File: clCodeLiteRemoteProcess.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (714 lines) | stat: -rw-r--r-- 22,520 bytes parent folder | download | duplicates (2)
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#include "clCodeLiteRemoteProcess.hpp"

#include "JSON.h"
#include "StringUtils.h"
#include "asyncprocess.h"
#include "cJSON.h"
#include "clSFTPManager.hpp"
#include "cl_command_event.h"
#include "environmentconfig.h"
#include "file_logger.h"
#include "fileutils.h"
#include "globals.h"
#include "processreaderthread.h"

#include <functional>
#include <unordered_map>
#include <wx/event.h>
#include <wx/tokenzr.h>

wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_RESTARTED, clCommandEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_LIST_FILES, clCommandEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_LIST_FILES_DONE, clCommandEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_FIND_RESULTS, clFindInFilesEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_FIND_RESULTS_DONE, clFindInFilesEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_EXEC_OUTPUT, clProcessEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_EXEC_DONE, clProcessEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_LOCATE_DONE, clCommandEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_LOCATE, clCommandEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_FINDPATH, clCommandEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_FINDPATH_DONE, clCommandEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_LIST_LSPS, clCommandEvent);
wxDEFINE_EVENT(wxEVT_CODELITE_REMOTE_LIST_LSPS_DONE, clCommandEvent);
namespace
{
const char msg_terminator[] = ">>codelite-remote-msg-end<<\n";
size_t msg_terminator_len = sizeof(msg_terminator) - 1;
} // namespace

namespace
{
class CodeLiteRemoteProcess : public IProcess
{
    clCodeLiteRemoteProcess* m_process = nullptr;
    std::function<void(const wxString&)> m_callback = nullptr;
    wxString m_output;

private:
    bool DoWrite(const wxString& buff)
    {
        if(!m_process) {
            return false;
        }
        m_process->Write(buff);
        return true;
    }

public:
    CodeLiteRemoteProcess(wxEvtHandler* handler, clCodeLiteRemoteProcess* process)
        : IProcess(handler)
        , m_process(process)
    {
    }
    ~CodeLiteRemoteProcess() { m_process = nullptr; }

    // are we using callback?
    bool IsUsingCallback() const { return m_callback != nullptr; }

    // Use callback instead of events
    void SetCallback(std::function<void(const wxString&)> cb) { m_callback = std::move(cb); }

    // Stop notifying the parent window about input/output from the process
    // this is useful when we wish to terminate the process onExit but we don't want
    // to know about its termination
    void Detach() override {}

    // Read from process stdout - return immediately if no data is available
    bool Read(wxString& buff, wxString& buffErr) override { return false; }

    // Write to the process stdin
    // This version add LF to the buffer
    bool Write(const wxString& buff) override { return DoWrite(buff); }

    // ANSI version
    // This version add LF to the buffer
    bool Write(const std::string& buff) override { return DoWrite(buff); }

    // Write to the process stdin
    bool WriteRaw(const wxString& buff) override { return DoWrite(buff); }

    // ANSI version
    bool WriteRaw(const std::string& buff) override { return DoWrite(buff); }

    /**
     * @brief this method is mostly needed on MSW where writing a password
     * is done directly on the console buffer rather than its stdin
     */
    bool WriteToConsole(const wxString& buff) override { return DoWrite(buff + "\n"); }

    // Return true if the process is still alive
    bool IsAlive() override { return m_process && m_process->IsRunning(); }

    // Clean the process resources and kill the process if it is
    // still alive
    void Cleanup() override {}

    // Terminate the process. It is recommended to use this method
    // so it will invoke the 'Cleaup' procedure and the process
    // termination event will be sent out
    void Terminate() override {}

    /**
     * @brief send signal to the process
     */
    void Signal(wxSignal sig) override { wxUnusedVar(sig); }

    void PostOutputEvent(const wxString& output)
    {
        if(m_callback) {
            // when callback is used, we aggregate the output
            // and post it in the terminated event
            m_output << output;
        } else {
            clProcessEvent e(wxEVT_ASYNC_PROCESS_OUTPUT);
            e.SetOutput(output);
            e.SetProcess(this);
            m_parent->AddPendingEvent(e);
        }
    }

    void PostTerminateEvent()
    {
        if(m_callback) {
            m_callback(m_output);
        } else {
            clProcessEvent e(wxEVT_ASYNC_PROCESS_TERMINATED);
            e.SetProcess(this);
            m_parent->AddPendingEvent(e);
        }
    }
};
} // namespace

clCodeLiteRemoteProcess::clCodeLiteRemoteProcess()
{
    Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &clCodeLiteRemoteProcess::OnProcessTerminated, this);
    Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &clCodeLiteRemoteProcess::OnProcessOutput, this);
}

clCodeLiteRemoteProcess::~clCodeLiteRemoteProcess()
{
    Unbind(wxEVT_ASYNC_PROCESS_TERMINATED, &clCodeLiteRemoteProcess::OnProcessTerminated, this);
    Unbind(wxEVT_ASYNC_PROCESS_OUTPUT, &clCodeLiteRemoteProcess::OnProcessOutput, this);
    wxDELETE(m_process);
}

void clCodeLiteRemoteProcess::StartInteractive(const wxString& account, const wxString& scriptPath,
                                               const wxString& contextString)
{
    auto ssh_account = SSHAccountInfo::LoadAccount(account);
    if(ssh_account.GetAccountName().empty()) {
        clWARNING() << "Failed to load ssh account:" << account << endl;
        return;
    }
    StartInteractive(ssh_account, scriptPath, contextString);
}

void clCodeLiteRemoteProcess::StartIfNotRunning()
{
    if(IsRunning()) {
        return;
    }

    // wrap the command in ssh
    wxFileName ssh_exe;
    EnvSetter setter;
    if(!FileUtils::FindExe("ssh", ssh_exe)) {
        clERROR() << "Could not locate ssh executable in your PATH!" << endl;
        return;
    }

    std::vector<wxString> command = { ssh_exe.GetFullPath(), "-o", "ServerAliveInterval=10", "-o",
                                      "StrictHostKeyChecking=no" };
    command.push_back(m_account.GetUsername() + "@" + m_account.GetHost());
    command.push_back("-p");
    command.push_back(wxString() << m_account.GetPort());

    // start the process in interactive mode
    command.push_back("python3 " + m_scriptPath + " --context " + GetContext());

    clDEBUG() << "Starting codelite-remote:" << command << endl;
    // start the process
    m_process = ::CreateAsyncProcess(this, command, IProcessCreateDefault | IProcessRawOutput);
}

void clCodeLiteRemoteProcess::StartInteractive(const SSHAccountInfo& account, const wxString& scriptPath,
                                               const wxString& contextString)
{
    if(m_process) {
        return;
    }

#if USE_SFTP
    // upload codelite-remote script and start it once its uploaded
    wxString localCodeLiteRemoteScript = clStandardPaths::Get().GetBinFolder() + "/codelite-remote";
    clDEBUG() << "Uploading codelite-remote file:" << endl;
    clDEBUG() << localCodeLiteRemoteScript << "->" << scriptPath << endl;
    if(!clSFTPManager::Get().AwaitSaveFile(localCodeLiteRemoteScript, scriptPath, account.GetAccountName())) {
        clERROR() << "Failed to upload file:" << scriptPath << "." << clSFTPManager::Get().GetLastError() << endl;
        return;
    }
#else
    clERROR() << "CodeLite is built with NO SFTP support" << endl;
#endif

    m_going_down = false;
    m_context = contextString;
    m_account = account;
    m_scriptPath = scriptPath;

    StartIfNotRunning();
}

void clCodeLiteRemoteProcess::OnProcessOutput(clProcessEvent& e)
{
    m_outputRead << e.GetOutput();
    ProcessOutput();
}

void clCodeLiteRemoteProcess::OnProcessTerminated(clProcessEvent& e)
{
    wxUnusedVar(e);
    Cleanup();
    if(!m_going_down) {
        // restart the process
        StartIfNotRunning();

        clCommandEvent terminate_event(wxEVT_CODELITE_REMOTE_RESTARTED);
        terminate_event.SetEventObject(this);
        AddPendingEvent(terminate_event);
    }
    m_going_down = false;
}

void clCodeLiteRemoteProcess::Stop()
{
    m_going_down = true;
    if(m_process) {
        m_process->Write(wxString("exit\n"));
    }
    wxDELETE(m_process);
    Cleanup();
}

void clCodeLiteRemoteProcess::Cleanup()
{
    while(!m_completionCallbacks.empty()) {
        m_completionCallbacks.pop_back();
    }
    wxDELETE(m_process);
}

bool clCodeLiteRemoteProcess::GetNextBuffer(wxString& raw_input_buffer, wxString& buffer, bool& is_completed)
{
    size_t separator_len = msg_terminator_len;
    size_t where = raw_input_buffer.find(msg_terminator);
    if(where == wxString::npos) {
        // locate the \n from the end
        is_completed = false;
        where = raw_input_buffer.rfind("\n");
        separator_len = 1;
    } else {
        is_completed = true;
    }

    if(where != wxString::npos) {
        size_t length_to_take = where;
        if(separator_len == 1) {
            length_to_take += 1;
        }
        buffer = raw_input_buffer.Mid(0, length_to_take);
        raw_input_buffer.erase(0, where + separator_len);
    }
    return where != wxString::npos;
}

void clCodeLiteRemoteProcess::ProcessOutput()
{
    bool is_completed = false;
    wxString buffer;

    while(GetNextBuffer(m_outputRead, buffer, is_completed)) {
        if(m_completionCallbacks.empty()) {
            clDEBUG() << "Read: [" << buffer << "]. But there are no completion callback" << endl;
            continue;
        }

        auto p = m_completionCallbacks.front();
        if(p.handler) {
            auto handler = static_cast<CodeLiteRemoteProcess*>(p.handler);
            handler->PostOutputEvent(buffer);
            if(is_completed) {
                handler->PostTerminateEvent();

                // when using callback the handler is handled internally
                if(handler->IsUsingCallback()) {
                    delete handler;
                }
            }
        } else if(p.func) {
            (this->*p.func)(buffer, is_completed);
        }

        if(is_completed) {
            m_completionCallbacks.pop_front();
            ResetStates();
        }
    }
}

void clCodeLiteRemoteProcess::ListLSPs()
{
    if(!m_process) {
        return;
    }

    // build the command and send it
    JSON root(cJSON_Object);
    auto item = root.toElement();
    item.addProperty("command", "list_lsps");
    m_process->Write(item.format(false) + "\n");

    // push a callback
    m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnListLSPsOutput, nullptr });
}

void clCodeLiteRemoteProcess::ListFiles(const wxString& root_dir, const wxString& extensions)
{
    if(!m_process) {
        return;
    }

    wxString exts = extensions;
    exts.Replace("*", "");

    // build the command and send it
    JSON root(cJSON_Object);
    auto item = root.toElement();
    item.addProperty("command", "ls");
    item.addProperty("root_dir", root_dir);
    item.addProperty("file_extensions", ::wxStringTokenize(exts, ",; |", wxTOKEN_STRTOK));
    LOG_IF_TRACE { clDEBUG1() << "ListFiles: sending command:" << item.format(false) << endl; }
    m_process->Write(item.format(false) + "\n");

    // push a callback
    m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnListFilesOutput, nullptr });
}

void clCodeLiteRemoteProcess::Search(const wxString& root_dir, const wxString& extensions, const wxString& find_what,
                                     bool whole_word, bool icase)
{
    if(!m_process) {
        return;
    }

    wxString exts = extensions;
    exts.Replace("*", "");

    // build the command and send it
    JSON root(cJSON_Object);
    auto item = root.toElement();
    item.addProperty("command", "find");
    item.addProperty("root_dir", root_dir);
    item.addProperty("find_what", find_what);
    item.addProperty("file_extensions", ::wxStringTokenize(exts, ",; |", wxTOKEN_STRTOK));
    item.addProperty("icase", icase);
    item.addProperty("whole_word", whole_word);

    wxString command = item.format(false);
    m_process->Write(command + "\n");
    LOG_IF_TRACE { clDEBUG1() << command << endl; }

    // push a callback
    m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnFindOutput, nullptr });
}

void clCodeLiteRemoteProcess::Locate(const wxString& path, const wxString& name, const wxString& ext,
                                     const std::vector<wxString>& versions)
{
    if(!m_process) {
        return;
    }

    // build the command and send it
    JSON root(cJSON_Object);
    auto item = root.toElement();
    item.addProperty("command", "locate");
    item.addProperty("path", path);
    item.addProperty("name", name);
    item.addProperty("ext", ext);

    // convert std::vector to wxArrayString
    wxArrayString v;
    v.reserve(versions.size());

    for(const auto& s : versions) {
        v.Add(s);
    }

    item.addProperty("versions", v);

    wxString command = item.format(false);
    m_process->Write(command + "\n");
    LOG_IF_TRACE { clDEBUG1() << command << endl; }

    // push a callback
    m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnLocateOutput, nullptr });
}

void clCodeLiteRemoteProcess::FindPath(const wxString& path)
{
    if(!m_process) {
        return;
    }

    // build the command and send it
    JSON root(cJSON_Object);
    auto item = root.toElement();
    item.addProperty("command", "find_path");
    item.addProperty("path", path);

    wxString command = item.format(false);
    m_process->Write(command + "\n");
    LOG_IF_TRACE { clDEBUG1() << command << endl; }

    // push a callback
    m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnFindPathOutput, nullptr });
}

void clCodeLiteRemoteProcess::ResetStates()
{
    m_fif_matches_count = 0;
    m_fif_files_scanned = 0;
}

bool clCodeLiteRemoteProcess::DoExec(const wxString& cmd, const wxString& working_directory, const clEnvList_t& env,
                                     IProcess* handler)
{
    if(!m_process) {
        return false;
    }

    // build the command and send it
    JSON root(cJSON_Object);
    auto item = root.toElement();
    item.addProperty("command", "exec");
    item.addProperty("wd", working_directory);
    item.addProperty("cmd", cmd);

    auto envarr = item.AddArray("env");
    for(const auto& p : env) {
        auto entry = envarr.AddObject(wxEmptyString);
        entry.addProperty("name", p.first);
        entry.addProperty("value", p.second);
    }

    wxString command = item.format(false);
    m_process->Write(command + "\n");
    LOG_IF_TRACE { clDEBUG1() << command << endl; }

    // push a callback
    m_completionCallbacks.push_back({ &clCodeLiteRemoteProcess::OnExecOutput, handler });
    return true;
}

void clCodeLiteRemoteProcess::Exec(const wxArrayString& args, const wxString& working_directory, const clEnvList_t& env)
{
    wxString cmdstr = GetCmdString(args);
    if(cmdstr.empty()) {
        return;
    }
    DoExec(cmdstr, working_directory, env);
}

void clCodeLiteRemoteProcess::Exec(const wxString& cmd, const wxString& working_directory, const clEnvList_t& env)
{
    DoExec(cmd, working_directory, env);
}

void clCodeLiteRemoteProcess::Write(const wxString& str)
{
    if(IsRunning()) {
        return;
    }
    if(!str.EndsWith("\n")) {
        m_process->Write(str + "\n");
    } else {
        m_process->Write(str);
    }
}

IProcess* clCodeLiteRemoteProcess::CreateAsyncProcess(wxEvtHandler* handler, const wxString& cmd,
                                                      const wxString& working_directory, const clEnvList_t& env)
{
    CodeLiteRemoteProcess* p = new CodeLiteRemoteProcess(handler, this);
    if(DoExec(cmd, working_directory, env, p)) {
        return p;
    }
    wxDELETE(p);
    return nullptr;
}

void clCodeLiteRemoteProcess::CreateAsyncProcessCB(const wxString& cmd, std::function<void(const wxString&)> callback,
                                                   const wxString& working_directory, const clEnvList_t& env)
{
    CodeLiteRemoteProcess* p = new CodeLiteRemoteProcess(nullptr, this);
    p->SetCallback(std::move(callback));
    if(DoExec(cmd, working_directory, env, p)) {
        return;
    }
    wxDELETE(p);
}

// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
// completion handlers
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------

void clCodeLiteRemoteProcess::OnListLSPsOutput(const wxString& output, bool is_completed)
{
    clCommandEvent event(wxEVT_CODELITE_REMOTE_LIST_LSPS);

    // parse the output
    event.SetString(output);
    AddPendingEvent(event);

    if(is_completed) {
        clCommandEvent event_done(wxEVT_CODELITE_REMOTE_LIST_LSPS_DONE);
        AddPendingEvent(event_done);
    }
}

void clCodeLiteRemoteProcess::OnListFilesOutput(const wxString& output, bool is_completed)
{
    clCommandEvent event(wxEVT_CODELITE_REMOTE_LIST_FILES);

    LOG_IF_TRACE { clDEBUG1() << output << endl; }

    // parse the output (line based)
    wxArrayString files = ::wxStringTokenize(output, "\r\n", wxTOKEN_STRTOK);
    event.GetStrings().swap(files);
    AddPendingEvent(event);

    if(is_completed) {
        clCommandEvent event_done(wxEVT_CODELITE_REMOTE_LIST_FILES_DONE);
        AddPendingEvent(event_done);
    }
}

void clCodeLiteRemoteProcess::OnFindPathOutput(const wxString& output, bool is_completed)
{
    clCommandEvent event(wxEVT_CODELITE_REMOTE_FINDPATH);

    // parse the output
    LOG_IF_TRACE { clDEBUG1() << "FindPath output: [" << output << "]" << endl; }
    wxString fullpath = output;
    fullpath.Trim().Trim(false);
    event.SetString(fullpath);
    AddPendingEvent(event);

    if(is_completed) {
        clCommandEvent event_done(wxEVT_CODELITE_REMOTE_FINDPATH_DONE);
        AddPendingEvent(event_done);
    }
}

void clCodeLiteRemoteProcess::OnLocateOutput(const wxString& output, bool is_completed)
{
    clCommandEvent event(wxEVT_CODELITE_REMOTE_LOCATE);

    // parse the output
    LOG_IF_TRACE { clDEBUG1() << "Locate output: [" << output << "]" << endl; }
    wxString fullpath = output;
    fullpath.Trim().Trim(false);
    event.SetFileName(fullpath);
    AddPendingEvent(event);

    if(is_completed) {
        clCommandEvent event_done(wxEVT_CODELITE_REMOTE_LOCATE_DONE);
        AddPendingEvent(event_done);
    }
}

void clCodeLiteRemoteProcess::OnFindOutput(const wxString& output, bool is_completed)
{
    wxArrayString files = ::wxStringTokenize(output, "\r\n", wxTOKEN_STRTOK);
    if(files.empty()) {
        return;
    }

    clFindInFilesEvent::Match::vec_t matches;
    matches.reserve(files.size());
    for(const wxString& file : files) {
        JSON j(file);
        if(!j.isOk()) {
            continue;
        }

        auto file_matches = j.toElement();

        if((m_fif_matches_count == 0) && (m_fif_files_scanned == 0) && file_matches.hasNamedObject("files_scanned")) {
            m_fif_files_scanned = file_matches["files_scanned"].toSize_t();
            continue;
        }

        clFindInFilesEvent::Match match;
        match.file = file_matches["file"].toString();
        auto json_locations = file_matches["matches"];
        size_t loc_count = json_locations.arraySize();

        // add the locations for this file
        if(loc_count) {
            match.locations.reserve(loc_count);
            for(size_t j = 0; j < loc_count; j++) {
                clFindInFilesEvent::Location loc;
                auto json_loc = json_locations[j];
                loc.line = json_loc["ln"].toSize_t();
                loc.column_start = json_loc["start"].toSize_t();
                loc.column_end = json_loc["end"].toSize_t();
                loc.pattern = json_loc["pattern"].toString();
                match.locations.push_back(loc);
                m_fif_matches_count++;
            }
            // add the match
            matches.push_back(match);
        }
    }

    if(!matches.empty()) {
        clFindInFilesEvent event(wxEVT_CODELITE_REMOTE_FIND_RESULTS);
        event.SetMatches(matches);
        AddPendingEvent(event);
    }

    if(is_completed) {
        clFindInFilesEvent event_done(wxEVT_CODELITE_REMOTE_FIND_RESULTS_DONE);
        event_done.SetInt(m_fif_files_scanned);
        AddPendingEvent(event_done);
    }
}

void clCodeLiteRemoteProcess::OnExecOutput(const wxString& buffer, bool is_completed)
{
    if(!buffer.empty()) {
        clProcessEvent output_event(wxEVT_CODELITE_REMOTE_EXEC_OUTPUT);
        output_event.SetOutput(buffer);
        AddPendingEvent(output_event);
    }

    if(is_completed) {
        clProcessEvent end_event(wxEVT_CODELITE_REMOTE_EXEC_DONE);
        AddPendingEvent(end_event);
    }
}

bool clCodeLiteRemoteProcess::SyncExec(const wxString& cmd, const wxString& working_directory, const clEnvList_t& env,
                                       wxString* output)
{
    if(!m_completionCallbacks.empty()) {
        clWARNING() << "unable to run SyncExec() for command:" << cmd << "async queue is not empty" << endl;
        return false;
    }
    if(!m_process) {
        clWARNING() << "unable to run SyncExec() for command:" << cmd << "no process" << endl;
        return false;
    }
    // disable the background reader thread
    m_process->SuspendAsyncReads();

    if(!DoExec(cmd, working_directory, env)) {
        return false;
    }

    // DoExec pushes a callback to the queue - pop it
    // as we dont really need it
    m_completionCallbacks.pop_back();

    // read
    wxString buff_out, buff_err;
    m_outputRead.clear();

    wxString complete_output;
    while(m_process->Read(buff_out, buff_err)) {
        m_outputRead << buff_out;
        size_t where = m_outputRead.find(msg_terminator);
        if(where == wxString::npos) {
            continue;
        }

        // strip the terminator and make it our output
        *output = m_outputRead.Mid(0, where);
        LOG_IF_TRACE { clDEBUG1() << "SyncExec(" << cmd << "):" << *output << endl; }

        m_outputRead.clear();
        // resume the async nature of the process
        m_process->ResumeAsyncReads();
        return true;
    }

    // process terminated
    clProcessEvent dummy;
    OnProcessTerminated(dummy);
    return false;
}