• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tstack / lnav / 19080893927-2636

04 Nov 2025 07:43PM UTC coverage: 68.931% (+0.03%) from 68.898%
19080893927-2636

push

github

tstack
[tidy] some more cleanup

9 of 29 new or added lines in 5 files covered. (31.03%)

1855 existing lines in 27 files now uncovered.

50572 of 73366 relevant lines covered (68.93%)

428466.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.78
/src/command_executor.hh
1
/**
2
 * Copyright (c) 2015, Timothy Stack
3
 *
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 * * Neither the name of Timothy Stack nor the names of its contributors
15
 * may be used to endorse or promote products derived from this software
16
 * without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29

30
#ifndef LNAV_COMMAND_EXECUTOR_H
31
#define LNAV_COMMAND_EXECUTOR_H
32

33
#include <filesystem>
34
#include <functional>
35
#include <future>
36
#include <optional>
37
#include <stack>
38
#include <string>
39
#include <vector>
40

41
#include <sqlite3.h>
42

43
#include "base/auto_fd.hh"
44
#include "base/lnav.console.hh"
45
#include "base/lnav.resolver.hh"
46
#include "db_sub_source.hh"
47
#include "fmt/format.h"
48
#include "help_text.hh"
49
#include "lnav.script.parser.hh"
50
#include "vis_line.hh"
51

52
struct exec_context;
53
class attr_line_t;
54
class logline_value;
55
struct logline_value_vector;
56

57
using sql_callback_t = int (*)(exec_context&, sqlite3_stmt*);
58
int sql_callback(exec_context& ec, sqlite3_stmt* stmt);
59
int internal_sql_callback(exec_context& ec, sqlite3_stmt* stmt);
60

61
using pipe_callback_t
62
    = std::future<std::string> (*)(exec_context&, const std::string&, auto_fd&);
63

64
using msg_callback_t = std::function<void(const lnav::console::user_message&)>;
65

66
struct exec_context {
67
    enum class perm_t {
68
        READ_WRITE,
69
        READ_ONLY,
70
    };
71

72
    using output_t = std::pair<FILE*, int (*)(FILE*)>;
73

74
    exec_context(logline_value_vector* line_values = nullptr,
75
                 sql_callback_t sql_callback = ::sql_callback,
76
                 pipe_callback_t pipe_callback = nullptr);
77

78
    bool is_read_write() const { return this->ec_perms == perm_t::READ_WRITE; }
37✔
79

80
    bool is_read_only() const { return this->ec_perms == perm_t::READ_ONLY; }
1,662✔
81

82
    exec_context& with_perms(perm_t perms)
50✔
83
    {
84
        this->ec_perms = perms;
50✔
85
        return *this;
50✔
86
    }
87

88
    void add_error_context(lnav::console::user_message& um) const;
89

90
    lnav::console::user_message make_error_from_str(std::string&& str) const;
91

92
    template<typename... Args>
93
    lnav::console::user_message make_error_msg(fmt::string_view format_str,
28✔
94
                                               const Args&... args) const
95
    {
96
        auto str = fmt::vformat(format_str, fmt::make_format_args(args...));
48✔
97

98
        return this->make_error_from_str(std::move(str));
56✔
99
    }
28✔
100

101
    template<typename... Args>
102
    Result<std::string, lnav::console::user_message> make_error(
26✔
103
        fmt::string_view format_str, const Args&... args) const
104
    {
105
        return Err(this->make_error_msg(format_str, args...));
26✔
106
    }
107

108
    std::optional<FILE*> get_output() const
182✔
109
    {
110
        for (auto iter = this->ec_output_stack.rbegin();
182✔
111
             iter != this->ec_output_stack.rend();
228✔
112
             ++iter)
46✔
113
        {
114
            if (iter->od_output && iter->od_output->first) {
226✔
115
                return iter->od_output->first;
180✔
116
            }
117
        }
118

119
        return std::nullopt;
2✔
120
    }
121

122
    text_format_t get_output_format() const
7✔
123
    {
124
        auto retval = text_format_t::TF_UNKNOWN;
7✔
125

126
        if (!this->ec_output_stack.empty()) {
7✔
127
            retval = this->ec_output_stack.back().od_format;
7✔
128
        }
129
        return retval;
7✔
130
    }
131

132
    void set_output_format(text_format_t tf)
100✔
133
    {
134
        if (!this->ec_output_stack.empty()
100✔
135
            && this->ec_output_stack.back().od_format
100✔
136
                == text_format_t::TF_UNKNOWN)
137
        {
138
            this->ec_output_stack.back().od_format = tf;
98✔
139
        }
140
    }
100✔
141

142
    void set_output(const std::string& name, FILE* file, int (*closer)(FILE*));
143

144
    void clear_output();
145

146
    struct mouse_input {};
147
    struct keyboard_input {};
148
    struct user {};
149
    struct file_open {
150
        std::string fo_name;
151
    };
152
    struct external_access {
153
        std::string ea_src;
154
    };
155

156
    using provenance_t = mapbox::util::
157
        variant<user, keyboard_input, mouse_input, file_open, external_access>;
158

159
    struct provenance_guard {
160
        explicit provenance_guard(exec_context* context, provenance_t prov)
8✔
161
            : pg_context(context)
8✔
162
        {
163
            this->pg_context->ec_provenance.push_back(prov);
8✔
164
        }
8✔
165

166
        provenance_guard(const provenance_guard&) = delete;
167
        provenance_guard(provenance_guard&& other)
168
            : pg_context(other.pg_context)
169
        {
170
            other.pg_context = nullptr;
171
        }
172

173
        ~provenance_guard()
8✔
174
        {
175
            if (this->pg_context != nullptr) {
8✔
176
                this->pg_context->ec_provenance.pop_back();
8✔
177
            }
178
        }
8✔
179

UNCOV
180
        exec_context* operator->() { return this->pg_context; }
×
181

182
        exec_context* pg_context;
183
    };
184

185
    provenance_guard with_provenance(provenance_t prov)
3✔
186
    {
187
        return provenance_guard{this, prov};
3✔
188
    }
189

190
    struct source_guard {
191
        source_guard(exec_context* context) : sg_context(context) {}
2,437✔
192

193
        source_guard(const source_guard&) = delete;
194

195
        source_guard(source_guard&& other)
196
            : sg_context(std::exchange(other.sg_context, nullptr))
197
        {
198
        }
199

200
        ~source_guard()
2,437✔
201
        {
202
            if (this->sg_context != nullptr) {
2,437✔
203
                this->sg_context->ec_source.pop_back();
2,437✔
204
            }
205
        }
2,437✔
206

207
        exec_context* sg_context;
208
    };
209

210
    struct output_guard {
211
        explicit output_guard(exec_context& context,
212
                              std::string name = "default",
213
                              const std::optional<output_t>& file
214
                              = std::nullopt,
215
                              text_format_t tf = text_format_t::TF_UNKNOWN);
216

217
        ~output_guard();
218

219
        exec_context& sg_context;
220
        bool sg_active;
221
    };
222

223
    struct sql_callback_guard {
224
        sql_callback_guard(exec_context& context, sql_callback_t cb);
225

226
        sql_callback_guard(const sql_callback_guard&) = delete;
227

228
        sql_callback_guard(sql_callback_guard&& other);
229

230
        ~sql_callback_guard();
231

232
        exec_context& scg_context;
233
        sql_callback_t scg_old_callback;
234
    };
235

236
    sql_callback_guard push_callback(sql_callback_t cb);
237

238
    source_guard enter_source(source_location, const std::string& content);
239

240
    source_guard enter_source(intern_string_t path,
2,435✔
241
                              int line_number,
242
                              const std::string& content)
243
    {
244
        return this->enter_source(source_location{path, line_number}, content);
2,435✔
245
    }
246

247
    struct db_source_guard {
248
        explicit db_source_guard(exec_context* context) : dsg_context(context)
2✔
249
        {
250
        }
2✔
251

252
        db_source_guard(const db_source_guard&) = delete;
253

254
        db_source_guard(db_source_guard&& other) noexcept
255
            : dsg_context(std::exchange(other.dsg_context, nullptr))
256
        {
257
        }
258

259
        ~db_source_guard()
2✔
260
        {
261
            if (this->dsg_context != nullptr) {
2✔
262
                this->dsg_context->ec_label_source_stack.pop_back();
2✔
263
            }
264
        }
2✔
265

266
        exec_context* dsg_context;
267
    };
268

269
    db_source_guard enter_db_source(db_label_source* dls)
2✔
270
    {
271
        this->ec_label_source_stack.push_back(dls);
2✔
272

273
        return db_source_guard{this};
2✔
274
    }
275

276
    struct msg_cb_guard {
277
        explicit msg_cb_guard(std::vector<msg_callback_t>* cb_stack)
597✔
278
            : sg_cb_stack(cb_stack)
597✔
279
        {
280
        }
597✔
281

282
        msg_cb_guard(const msg_cb_guard&) = delete;
283
        msg_cb_guard(msg_cb_guard&& other) noexcept
284
            : sg_cb_stack(other.sg_cb_stack)
285
        {
286
            other.sg_cb_stack = nullptr;
287
        }
288

289
        ~msg_cb_guard()
597✔
290
        {
291
            if (this->sg_cb_stack != nullptr) {
597✔
292
                this->sg_cb_stack->pop_back();
597✔
293
            }
294
        }
597✔
295

296
        std::vector<msg_callback_t>* sg_cb_stack;
297
    };
298

299
    msg_cb_guard add_msg_callback(msg_callback_t cb)
597✔
300
    {
301
        this->ec_msg_callback_stack.emplace_back(std::move(cb));
597✔
302
        return msg_cb_guard{&this->ec_msg_callback_stack};
597✔
303
    }
304

305
    scoped_resolver create_resolver()
237✔
306
    {
307
        return {
308
            &this->ec_local_vars.top(),
237✔
309
            &this->ec_global_vars,
237✔
310
        };
237✔
311
    }
312

313
    Result<std::string, lnav::console::user_message> execute(
314
        source_location loc, const std::string& cmdline);
315

316
    using kv_pair_t = std::pair<std::string, std::string>;
317

UNCOV
318
    Result<std::string, lnav::console::user_message> execute_with_int(
×
319
        source_location loc, const std::string& cmdline)
320
    {
UNCOV
321
        return this->execute(loc, cmdline);
×
322
    }
323

324
    template<typename... Args>
UNCOV
325
    Result<std::string, lnav::console::user_message> execute_with_int(
×
326
        source_location loc,
327
        const std::string& cmdline,
328
        kv_pair_t pair,
329
        Args... args)
330
    {
UNCOV
331
        this->ec_local_vars.top().emplace(pair);
×
332
        return this->execute_with_int(loc, cmdline, args...);
×
333
    }
334

335
    template<typename... Args>
UNCOV
336
    Result<std::string, lnav::console::user_message> execute_with(
×
337
        source_location loc, const std::string& cmdline, Args... args)
338
    {
UNCOV
339
        this->ec_local_vars.push({});
×
340
        auto retval = this->execute_with_int(loc, cmdline, args...);
×
341
        this->ec_local_vars.pop();
×
342

UNCOV
343
        return retval;
×
344
    }
345

346
    template<typename T>
347
    std::optional<T> get_provenance() const
150✔
348
    {
349
        for (const auto& elem : this->ec_provenance) {
150✔
350
            if (elem.is<T>()) {
7✔
351
                return elem.get<T>();
7✔
352
            }
353
        }
354

355
        return std::nullopt;
143✔
356
    }
357

358
    vis_line_t ec_top_line{0_vl};
359
    bool ec_dry_run{false};
360
    perm_t ec_perms{perm_t::READ_WRITE};
361

362
    logline_value_vector* ec_line_values;
363
    std::stack<std::map<std::string, scoped_value_t>> ec_local_vars;
364
    std::vector<provenance_t> ec_provenance;
365
    std::map<std::string, scoped_value_t> ec_global_vars;
366
    std::vector<std::filesystem::path> ec_path_stack;
367
    std::vector<lnav::console::snippet> ec_source;
368
    help_text* ec_current_help{nullptr};
369

370
    struct output_desc {
371
        output_desc(std::string name,
4,181✔
372
                    std::optional<output_t> out,
373
                    text_format_t tf = text_format_t::TF_UNKNOWN)
374
            : od_name(std::move(name)), od_output(std::move(out)), od_format(tf)
4,181✔
375
        {
376
        }
4,181✔
377

378
        std::string od_name;
379
        std::optional<output_t> od_output;
380
        text_format_t od_format{text_format_t::TF_UNKNOWN};
381
    };
382

383
    std::vector<output_desc> ec_output_stack;
384
    std::unique_ptr<attr_line_t> ec_accumulator;
385

386
    sql_callback_t ec_sql_callback;
387
    pipe_callback_t ec_pipe_callback;
388
    std::vector<msg_callback_t> ec_msg_callback_stack;
389
    std::vector<db_label_source*> ec_label_source_stack;
390

391
    struct ui_callbacks {
392
        std::function<void()> uc_pre_stdout_write;
393
        std::function<void()> uc_post_stdout_write;
394
        std::function<void()> uc_redraw;
395
    };
396
    ui_callbacks ec_ui_callbacks;
397
};
398

399
Result<std::string, lnav::console::user_message> execute_command(
400
    exec_context& ec, const std::string& cmdline);
401

402
Result<std::string, lnav::console::user_message> execute_sql(
403
    exec_context& ec, const std::string& sql, std::string& alt_msg);
404

405
class multiline_executor : public lnav::script::parser {
406
public:
407
    exec_context& me_exec_context;
408
    std::string me_last_result;
409

410
    multiline_executor(exec_context& ec, const std::string& src)
60✔
411
        : parser(src), me_exec_context(ec)
60✔
412
    {
413
    }
60✔
414

415
    Result<void, lnav::console::user_message> handle_command(
416
        const std::string& cmd) override;
417
};
418

419
Result<std::string, lnav::console::user_message> execute_file(
420
    exec_context& ec, const std::string& path_and_args);
421
Result<std::string, lnav::console::user_message> execute_any(
422
    exec_context& ec, const std::string& cmdline);
423
void execute_init_commands(
424
    exec_context& ec,
425
    std::vector<std::pair<Result<std::string, lnav::console::user_message>,
426
                          std::string>>& msgs);
427

428
std::future<std::string> pipe_callback(exec_context& ec,
429
                                       const std::string& cmdline,
430
                                       auto_fd& fd);
431

432
int sql_progress(const struct log_cursor& lc);
433
void sql_progress_finished();
434

435
void add_global_vars(exec_context& ec);
436

437
#endif  // LNAV_COMMAND_EXECUTOR_H
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc