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

tstack / lnav / 19115445439-2645

05 Nov 2025 08:31PM UTC coverage: 68.952% (+0.05%) from 68.905%
19115445439-2645

push

github

tstack
[tests] change /root use

50626 of 73422 relevant lines covered (68.95%)

433904.85 hits per line

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

82.08
/src/command_executor.cc
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
#include <vector>
31

32
#include "command_executor.hh"
33

34
#include "base/ansi_scrubber.hh"
35
#include "base/ansi_vars.hh"
36
#include "base/fs_util.hh"
37
#include "base/humanize.hh"
38
#include "base/injector.hh"
39
#include "base/itertools.hh"
40
#include "base/paths.hh"
41
#include "base/string_util.hh"
42
#include "bound_tags.hh"
43
#include "breadcrumb_curses.hh"
44
#include "config.h"
45
#include "curl_looper.hh"
46
#include "db_sub_source.hh"
47
#include "help_text_formatter.hh"
48
#include "lnav.hh"
49
#include "lnav.indexing.hh"
50
#include "lnav.prompt.hh"
51
#include "lnav_config.hh"
52
#include "lnav_util.hh"
53
#include "log_format_loader.hh"
54
#include "prql-modules.h"
55
#include "readline_highlighters.hh"
56
#include "service_tags.hh"
57
#include "shlex.hh"
58
#include "sql_help.hh"
59
#include "sql_util.hh"
60
#include "vtab_module.hh"
61

62
#ifdef HAVE_RUST_DEPS
63
#    include "lnav_rs_ext.cxx.hh"
64
#endif
65

66
using namespace std::literals::chrono_literals;
67
using namespace lnav::roles::literals;
68

69
exec_context INIT_EXEC_CONTEXT;
70

71
static sig_atomic_t sql_counter = 0;
72

73
int
74
sql_progress(const log_cursor& lc)
11,744✔
75
{
76
    if (lnav_data.ld_window == nullptr) {
11,744✔
77
        return 0;
11,733✔
78
    }
79

80
    if (!lnav_data.ld_looping) {
11✔
81
        return 1;
×
82
    }
83

84
    if (ui_periodic_timer::singleton().time_to_update(sql_counter)) {
11✔
85
        auto* breadcrumb_view = injector::get<breadcrumb_curses*>();
5✔
86
        breadcrumb_view->set_enabled(false);
5✔
87
        lnav_data.ld_status[LNS_TOP].set_enabled(false);
5✔
88
        lnav_data.ld_view_stack.top() |
5✔
89
            [](auto* tc) { tc->set_enabled(false); };
3✔
90
        ssize_t total = lnav_data.ld_log_source.text_line_count();
5✔
91
        off_t off = lc.lc_curr_line;
5✔
92

93
        if (off >= 0 && off <= total) {
5✔
94
            lnav_data.ld_bottom_source.update_loading(off, total);
5✔
95
            lnav_data.ld_status[LNS_BOTTOM].set_needs_update();
5✔
96
        }
97
        lnav::prompt::get().p_editor.clear_alt_value();
5✔
98
        auto refresh_res
99
            = lnav_data.ld_status_refresher(lnav::func::op_type::blocking);
5✔
100
        if (refresh_res == lnav::progress_result_t::interrupt) {
5✔
101
            return 1;
2✔
102
        }
103
    }
104

105
    return 0;
9✔
106
}
107

108
void
109
sql_progress_finished()
1,669✔
110
{
111
    if (sql_counter == 0) {
1,669✔
112
        return;
1,664✔
113
    }
114

115
    sql_counter = 0;
5✔
116

117
    if (lnav_data.ld_window == nullptr) {
5✔
118
        return;
×
119
    }
120

121
    auto* breadcrumb_view = injector::get<breadcrumb_curses*>();
5✔
122
    breadcrumb_view->set_enabled(true);
5✔
123
    lnav_data.ld_status[LNS_TOP].set_enabled(true);
5✔
124
    lnav_data.ld_view_stack.top() | [](auto* tc) { tc->set_enabled(true); };
8✔
125
    lnav_data.ld_bottom_source.update_loading(0, 0);
5✔
126
    lnav_data.ld_status[LNS_BOTTOM].set_needs_update();
5✔
127
    lnav_data.ld_status_refresher(lnav::func::op_type::blocking);
5✔
128
    lnav_data.ld_views[LNV_DB].redo_search();
5✔
129
}
130

131
static Result<std::string, lnav::console::user_message> execute_from_file(
132
    exec_context& ec,
133
    const std::string& src,
134
    int line_number,
135
    const std::string& cmdline);
136

137
Result<std::string, lnav::console::user_message>
138
execute_command(exec_context& ec, const std::string& cmdline)
717✔
139
{
140
    static auto op = lnav_operation{__FUNCTION__};
717✔
141

142
    auto op_guard = lnav_opid_guard::internal(op);
717✔
143
    std::vector<std::string> args;
717✔
144

145
    log_info("Executing command: %s", cmdline.c_str());
717✔
146

147
    split_ws(cmdline, args);
717✔
148

149
    if (!args.empty()) {
717✔
150
        readline_context::command_map_t::iterator iter;
717✔
151

152
        if ((iter = lnav_commands.find(args[0])) == lnav_commands.end()) {
717✔
153
            return ec.make_error("unknown command - {}", args[0]);
×
154
        }
155

156
        ec.ec_current_help = &iter->second->c_help;
717✔
157
        try {
158
            auto retval = iter->second->c_func(ec, cmdline, args);
717✔
159
            if (retval.isErr()) {
717✔
160
                auto um = retval.unwrapErr();
44✔
161

162
                ec.add_error_context(um);
44✔
163
                ec.ec_current_help = nullptr;
44✔
164
                return Err(um);
44✔
165
            }
44✔
166
            ec.ec_current_help = nullptr;
673✔
167

168
            return retval;
673✔
169
        } catch (const std::exception& e) {
717✔
170
            auto um = lnav::console::user_message::error(
×
171
                          attr_line_t("unexpected error while executing: ")
×
172
                              .append(lnav::roles::quoted_code(cmdline)))
×
173
                          .with_reason(e.what());
×
174
            return Err(um);
×
175
        }
×
176
    }
177

178
    return ec.make_error("no command to execute");
×
179
}
717✔
180

181
static Result<std::map<std::string, scoped_value_t>,
182
              lnav::console::user_message>
183
bind_sql_parameters(exec_context& ec, sqlite3_stmt* stmt)
1,662✔
184
{
185
    std::map<std::string, scoped_value_t> retval;
1,662✔
186
    auto param_count = sqlite3_bind_parameter_count(stmt);
1,662✔
187
    for (int lpc = 0; lpc < param_count; lpc++) {
1,836✔
188
        std::map<std::string, std::string>::iterator ov_iter;
175✔
189
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
175✔
190
        if (name == nullptr) {
175✔
191
            auto um
192
                = lnav::console::user_message::error("invalid SQL statement")
2✔
193
                      .with_reason(
2✔
194
                          "using a question-mark (?) for bound variables "
195
                          "is not supported, only named bound parameters "
196
                          "are supported")
197
                      .with_help(
2✔
198
                          "named parameters start with a dollar-sign "
199
                          "($) or colon (:) followed by the variable name")
200
                      .move();
1✔
201
            ec.add_error_context(um);
1✔
202

203
            return Err(um);
1✔
204
        }
1✔
205

206
        if (name[0] == '$') {
174✔
207
            const auto& lvars = ec.ec_local_vars.top();
145✔
208
            const auto& gvars = ec.ec_global_vars;
145✔
209
            std::map<std::string, scoped_value_t>::const_iterator local_var,
145✔
210
                global_var;
145✔
211
            const char* env_value;
212

213
            if (lnav_data.ld_window) {
145✔
214
                char buf[32];
215
                unsigned int lines, cols;
216

217
                ncplane_dim_yx(lnav_data.ld_window, &lines, &cols);
12✔
218
                if (strcmp(name, "$LINES") == 0) {
12✔
219
                    snprintf(buf, sizeof(buf), "%d", lines);
×
220
                    sqlite3_bind_text(stmt, lpc + 1, buf, -1, SQLITE_TRANSIENT);
×
221
                } else if (strcmp(name, "$COLS") == 0) {
12✔
222
                    snprintf(buf, sizeof(buf), "%d", cols);
×
223
                    sqlite3_bind_text(stmt, lpc + 1, buf, -1, SQLITE_TRANSIENT);
×
224
                }
225
            }
226

227
            if ((local_var = lvars.find(&name[1])) != lvars.end()) {
435✔
228
                mapbox::util::apply_visitor(
126✔
229
                    sqlitepp::bind_visitor(stmt, lpc + 1), local_var->second);
126✔
230
                retval[name] = local_var->second;
378✔
231
            } else if ((global_var = gvars.find(&name[1])) != gvars.end()) {
57✔
232
                mapbox::util::apply_visitor(
6✔
233
                    sqlitepp::bind_visitor(stmt, lpc + 1), global_var->second);
6✔
234
                retval[name] = global_var->second;
18✔
235
            } else if ((env_value = getenv(&name[1])) != nullptr) {
13✔
236
                sqlite3_bind_text(stmt, lpc + 1, env_value, -1, SQLITE_STATIC);
3✔
237
                retval[name] = string_fragment::from_c_str(env_value);
9✔
238
            }
239
        } else if (name[0] == ':' && ec.ec_line_values != nullptr) {
29✔
240
            for (auto& lv : ec.ec_line_values->lvv_values) {
159✔
241
                if (lv.lv_meta.lvm_name != &name[1]) {
130✔
242
                    continue;
101✔
243
                }
244
                switch (lv.lv_meta.lvm_kind) {
29✔
245
                    case value_kind_t::VALUE_BOOLEAN:
×
246
                        sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
247
                        retval[name] = fmt::to_string(lv.lv_value.i);
×
248
                        break;
×
249
                    case value_kind_t::VALUE_FLOAT:
×
250
                        sqlite3_bind_double(stmt, lpc + 1, lv.lv_value.d);
×
251
                        retval[name] = fmt::to_string(lv.lv_value.d);
×
252
                        break;
×
253
                    case value_kind_t::VALUE_INTEGER:
×
254
                        sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
255
                        retval[name] = fmt::to_string(lv.lv_value.i);
×
256
                        break;
×
257
                    case value_kind_t::VALUE_NULL:
×
258
                        sqlite3_bind_null(stmt, lpc + 1);
×
259
                        retval[name] = string_fragment::from_c_str(
×
260
                            db_label_source::NULL_STR);
×
261
                        break;
×
262
                    default:
29✔
263
                        sqlite3_bind_text(stmt,
29✔
264
                                          lpc + 1,
265
                                          lv.text_value(),
266
                                          lv.text_length(),
29✔
267
                                          SQLITE_TRANSIENT);
268
                        retval[name] = lv.to_string();
87✔
269
                        break;
29✔
270
                }
271
            }
272
        } else {
29✔
273
            sqlite3_bind_null(stmt, lpc + 1);
×
274
            log_warning("Could not bind variable: %s", name);
×
275
            retval[name]
×
276
                = string_fragment::from_c_str(db_label_source::NULL_STR);
×
277
        }
278
    }
279

280
    return Ok(retval);
1,661✔
281
}
1,662✔
282

283
static void
284
execute_search(const std::string& search_cmd)
7✔
285
{
286
    textview_curses* tc = get_textview_for_mode(lnav_data.ld_mode);
7✔
287
    auto search_term = string_fragment(search_cmd)
7✔
288
                           .find_right_boundary(0, string_fragment::tag1{'\n'})
7✔
289
                           .to_string();
7✔
290
    tc->execute_search(search_term);
7✔
291
}
7✔
292

293
Result<std::string, lnav::console::user_message>
294
execute_sql(exec_context& ec, const std::string& sql, std::string& alt_msg)
1,677✔
295
{
296
    static auto op = lnav_operation{__FUNCTION__};
1,677✔
297

298
    auto op_guard = lnav_opid_guard::internal(op);
1,677✔
299
    auto_mem<sqlite3_stmt> stmt(sqlite3_finalize);
1,677✔
300
    timeval start_tv, end_tv;
301
    std::string stmt_str = trim(sql);
1,677✔
302
    std::string retval;
1,677✔
303
    int retcode = SQLITE_OK;
1,677✔
304

305
    if (lnav::sql::is_prql(stmt_str)) {
1,677✔
306
        log_info("compiling PRQL: %s", stmt_str.c_str());
42✔
307

308
#if HAVE_RUST_DEPS
309
        extern rust::Vec<lnav_rs_ext::SourceTreeElement> sqlite_extension_prql;
310
        auto opts = lnav_rs_ext::Options{true, "sql.sqlite", true};
42✔
311

312
        auto tree = sqlite_extension_prql;
42✔
313
        for (const auto& mod : lnav_prql_modules) {
126✔
314
            log_debug("lnav_rs_ext adding mod %s", mod.get_name());
84✔
315
            tree.emplace_back(lnav_rs_ext::SourceTreeElement{
168✔
316
                mod.get_name().data(),
84✔
317
                mod.to_string_fragment_producer()->to_string(),
168✔
318
            });
319
        }
320
        tree.emplace_back(lnav_rs_ext::SourceTreeElement{"", stmt_str});
42✔
321
        log_debug("BEGIN compiling tree");
42✔
322
        auto cr = lnav_rs_ext::compile_tree(tree, opts);
42✔
323
        log_debug("END compiling tree");
42✔
324

325
        for (const auto& msg : cr.messages) {
42✔
326
            if (msg.kind != lnav_rs_ext::MessageKind::Error) {
1✔
327
                continue;
×
328
            }
329

330
            auto stmt_al = attr_line_t(stmt_str);
1✔
331
            readline_sql_highlighter(stmt_al, lnav::sql::dialect::prql, 0);
1✔
332
            auto um
333
                = lnav::console::user_message::error(
×
334
                      attr_line_t("unable to compile PRQL: ").append(stmt_al))
2✔
335
                      .with_reason(
1✔
336
                          attr_line_t::from_ansi_str((std::string) msg.reason));
2✔
337
            if (!msg.display.empty()) {
1✔
338
                um.with_note(
1✔
339
                    attr_line_t::from_ansi_str((std::string) msg.display));
2✔
340
            }
341
            for (const auto& hint : msg.hints) {
1✔
342
                um.with_help(attr_line_t::from_ansi_str((std::string) hint));
×
343
                break;
×
344
            }
345
            return Err(um);
1✔
346
        }
1✔
347
        log_debug("done!");
41✔
348
        stmt_str = (std::string) cr.output;
41✔
349
#else
350
        auto um = lnav::console::user_message::error(
351
            attr_line_t("PRQL is not supported in this build"));
352
        return Err(um);
353
#endif
354
    }
44✔
355

356
    log_info("Executing SQL: %s", stmt_str.c_str());
1,676✔
357

358
    auto old_mode = lnav_data.ld_mode;
1,676✔
359
    lnav_data.ld_mode = ln_mode_t::BUSY;
1,676✔
360
    auto mode_fin = finally([old_mode]() { lnav_data.ld_mode = old_mode; });
1,676✔
361
    lnav_data.ld_bottom_source.grep_error("");
1,676✔
362
    lnav_data.ld_status[LNS_BOTTOM].set_needs_update();
1,676✔
363

364
    if (startswith(stmt_str, ".")) {
1,676✔
365
        std::vector<std::string> args;
7✔
366
        split_ws(stmt_str, args);
7✔
367

368
        const auto* sql_cmd_map
369
            = injector::get<readline_context::command_map_t*,
370
                            sql_cmd_map_tag>();
7✔
371
        auto cmd_iter = sql_cmd_map->find(args[0]);
7✔
372

373
        if (cmd_iter != sql_cmd_map->end()) {
7✔
374
            ec.ec_current_help = &cmd_iter->second->c_help;
7✔
375
            auto retval = cmd_iter->second->c_func(ec, stmt_str, args);
7✔
376
            ec.ec_current_help = nullptr;
7✔
377

378
            return retval;
7✔
379
        }
7✔
380
    }
7✔
381

382
    ec.ec_accumulator->clear();
1,669✔
383

384
    require(!ec.ec_source.empty());
1,669✔
385
    const auto& source = ec.ec_source.back();
1,669✔
386
    sql_progress_guard progress_guard(sql_progress,
387
                                      sql_progress_finished,
388
                                      source.s_location,
389
                                      source.s_content);
1,669✔
390
    gettimeofday(&start_tv, nullptr);
1,669✔
391

392
    const auto* curr_stmt = stmt_str.c_str();
1,669✔
393
    auto last_is_readonly = false;
1,669✔
394
    while (curr_stmt != nullptr) {
3,273✔
395
        const char* tail = nullptr;
3,273✔
396
        while (isspace(*curr_stmt)) {
3,273✔
397
            curr_stmt += 1;
×
398
        }
399
        retcode = sqlite3_prepare_v2(
3,273✔
400
            lnav_data.ld_db.in(), curr_stmt, -1, stmt.out(), &tail);
401
        if (retcode != SQLITE_OK) {
3,273✔
402
            const char* errmsg = sqlite3_errmsg(lnav_data.ld_db);
7✔
403

404
            alt_msg = "";
7✔
405

406
            auto um = lnav::console::user_message::error(
14✔
407
                          "failed to compile SQL statement")
408
                          .with_reason(errmsg)
14✔
409
                          .with_snippets(ec.ec_source)
14✔
410
                          .move();
7✔
411

412
            auto annotated_sql = annotate_sql_with_error(
413
                lnav_data.ld_db.in(), curr_stmt, tail);
7✔
414
            auto loc = um.um_snippets.back().s_location;
7✔
415
            if (curr_stmt == stmt_str.c_str()) {
7✔
416
                um.um_snippets.pop_back();
7✔
417
            } else {
418
                auto tail_iter = stmt_str.begin();
×
419

420
                std::advance(tail_iter, (curr_stmt - stmt_str.c_str()));
×
421
                loc.sl_line_number
×
422
                    += std::count(stmt_str.begin(), tail_iter, '\n');
×
423
            }
424

425
            um.with_snippet(lnav::console::snippet::from(loc, annotated_sql));
7✔
426

427
            return Err(um);
7✔
428
        }
7✔
429
        if (stmt == nullptr) {
3,266✔
430
            retcode = SQLITE_DONE;
1,604✔
431
            break;
1,604✔
432
        }
433
#ifdef HAVE_SQLITE3_STMT_READONLY
434
        last_is_readonly = sqlite3_stmt_readonly(stmt.in());
1,662✔
435
        if (ec.is_read_only() && !last_is_readonly) {
1,662✔
436
            return ec.make_error(
437
                "modifying statements are not allowed in this context: {}",
438
                sql);
×
439
        }
440
#endif
441
        bool done = false;
1,662✔
442

443
        auto bound_values = TRY(bind_sql_parameters(ec, stmt.in()));
1,662✔
444
        if (last_is_readonly) {
1,661✔
445
            ec.ec_sql_callback(ec, stmt.in());
1,530✔
446
        }
447
        while (!done) {
5,975✔
448
            retcode = sqlite3_step(stmt.in());
4,371✔
449

450
            if (!ec.ec_label_source_stack.empty()) {
4,371✔
451
                ec.ec_label_source_stack.back()->dls_step_rc = retcode;
4,371✔
452
            }
453

454
            switch (retcode) {
4,371✔
455
                case SQLITE_OK:
1,604✔
456
                case SQLITE_DONE: {
457
                    auto changes = sqlite3_changes(lnav_data.ld_db.in());
1,604✔
458

459
                    log_info("sqlite3_changes() -> %d", changes);
1,604✔
460
                    done = true;
1,604✔
461
                    break;
1,604✔
462
                }
463

464
                case SQLITE_ROW:
2,710✔
465
                    ec.ec_sql_callback(ec, stmt.in());
2,710✔
466
                    break;
2,710✔
467

468
                default: {
57✔
469
                    attr_line_t bound_note;
57✔
470

471
                    if (!bound_values.empty()) {
57✔
472
                        bound_note.append(
3✔
473
                            "the bound parameters are set as follows:\n");
474
                        for (const auto& bval : bound_values) {
11✔
475
                            auto val_as_str = fmt::to_string(bval.second);
8✔
476
                            auto sql_type = bval.second.match(
8✔
477
                                [](const std::string&) { return SQLITE_TEXT; },
5✔
478
                                [](const string_fragment&) {
×
479
                                    return SQLITE_TEXT;
×
480
                                },
481
                                [](bool) { return SQLITE_INTEGER; },
×
482
                                [](int64_t) { return SQLITE_INTEGER; },
1✔
483
                                [](null_value_t) { return SQLITE_NULL; },
1✔
484
                                [](double) { return SQLITE_FLOAT; });
1✔
485
                            auto scrubbed_val = scrub_ws(val_as_str.c_str());
8✔
486
                            truncate_to(scrubbed_val, 40);
8✔
487
                            bound_note.append("  ")
8✔
488
                                .append(lnav::roles::variable(bval.first))
16✔
489
                                .append(":")
8✔
490
                                .append(sqlite3_type_to_string(sql_type))
8✔
491
                                .append(" = ")
8✔
492
                                .append_quoted(scrubbed_val)
16✔
493
                                .append("\n");
8✔
494
                        }
8✔
495
                    }
496

497
                    log_error("sqlite3_step error code: %d", retcode);
57✔
498
                    auto um = sqlite3_error_to_user_message(lnav_data.ld_db)
114✔
499
                                  .with_note(bound_note)
57✔
500
                                  .move();
57✔
501

502
                    if (!ec.get_provenance<exec_context::keyboard_input>()) {
57✔
503
                        um.with_context_snippets(ec.ec_source)
110✔
504
                            .remove_internal_snippets();
55✔
505
                    }
506

507
                    return Err(um);
57✔
508
                }
57✔
509
            }
510
        }
511

512
        curr_stmt = tail;
1,604✔
513
    }
1,661✔
514

515
    if (last_is_readonly && !ec.ec_label_source_stack.empty()) {
1,604✔
516
        auto& dls = *ec.ec_label_source_stack.back();
1,503✔
517
        dls.dls_query_end = std::chrono::steady_clock::now();
1,503✔
518

519
        size_t memory_usage = 0, total_size = 0, cached_chunks = 0;
1,503✔
520
        for (auto cc = dls.dls_cell_container.cc_first.get(); cc != nullptr;
3,007✔
521
             cc = cc->cc_next.get())
1,504✔
522
        {
523
            total_size += cc->cc_capacity;
1,504✔
524
            if (cc->cc_data) {
1,504✔
525
                cached_chunks += 1;
1,503✔
526
                memory_usage += cc->cc_capacity;
1,503✔
527
            } else {
528
                memory_usage += cc->cc_compressed_size;
1✔
529
            }
530
        }
531
        log_debug(
1,503✔
532
            "cell memory footprint: rows=%zu total=%zu; actual=%zu; "
533
            "cached-chunks=%zu",
534
            dls.dls_row_cursors.size(),
535
            total_size,
536
            memory_usage,
537
            cached_chunks);
538
    }
539
    gettimeofday(&end_tv, nullptr);
1,604✔
540
    lnav_data.ld_mode = old_mode;
1,604✔
541
    if (retcode == SQLITE_DONE) {
1,604✔
542
        if (lnav_data.ld_log_source.is_line_meta_changed()) {
1,604✔
543
            lnav_data.ld_log_source.text_filters_changed();
13✔
544
            lnav_data.ld_views[LNV_LOG].reload_data();
13✔
545
        }
546
        lnav_data.ld_filter_view.reload_data();
1,604✔
547
        lnav_data.ld_files_view.reload_data();
1,604✔
548

549
        lnav_data.ld_active_files.fc_files
550
            | lnav::itertools::for_each(&logfile::dump_stats);
1,604✔
551
        if (ec.ec_sql_callback != sql_callback) {
1,604✔
552
            retval = ec.ec_accumulator->get_string();
48✔
553
        } else {
554
            auto& dls = *(ec.ec_label_source_stack.back());
1,556✔
555
            if (!dls.dls_row_cursors.empty()) {
1,556✔
556
                lnav_data.ld_views[LNV_DB].reload_data();
1,442✔
557
                lnav_data.ld_views[LNV_DB].set_selection(0_vl);
1,442✔
558
                lnav_data.ld_views[LNV_DB].set_left(0);
1,442✔
559
                if (lnav_data.ld_flags & LNF_HEADLESS) {
1,442✔
560
                    retval = "";
956✔
561
                    alt_msg = "";
956✔
562
                } else if (dls.dls_row_cursors.size() == 1) {
486✔
563
                    retval = dls.get_row_as_string(0_vl);
438✔
564
                } else {
565
                    int row_count = dls.dls_row_cursors.size();
48✔
566
                    char row_count_buf[128];
567
                    timeval diff_tv;
568

569
                    timersub(&end_tv, &start_tv, &diff_tv);
48✔
570
                    snprintf(row_count_buf,
48✔
571
                             sizeof(row_count_buf),
572
                             ANSI_BOLD("%'d") " row%s matched in " ANSI_BOLD(
573
                                 "%ld.%03ld") " seconds",
574
                             row_count,
575
                             row_count == 1 ? "" : "s",
576
                             diff_tv.tv_sec,
577
                             std::max((long) diff_tv.tv_usec / 1000, 1L));
48✔
578
                    retval = row_count_buf;
48✔
579
                    if (dls.has_log_time_column()) {
48✔
580
                        alt_msg
581
                            = HELP_MSG_1(Q,
2✔
582
                                         "to switch back to the previous view "
583
                                         "at the matching 'log_time' value");
584
                    } else {
585
                        alt_msg = "";
46✔
586
                    }
587
                }
588
            }
589
#ifdef HAVE_SQLITE3_STMT_READONLY
590
            else if (last_is_readonly)
114✔
591
            {
592
                retval = "info: No rows matched";
25✔
593
                alt_msg = "";
25✔
594
            }
595
#endif
596
        }
597
    }
598

599
    return Ok(retval);
1,604✔
600
}
1,677✔
601

602
Result<void, lnav::console::user_message>
603
multiline_executor::handle_command(const std::string& cmdline)
234✔
604
{
605
    this->me_last_result = TRY(execute_from_file(this->me_exec_context,
234✔
606
                                                 this->p_source,
607
                                                 this->p_starting_line_number,
608
                                                 cmdline));
609

610
    return Ok();
228✔
611
}
612

613
static Result<std::string, lnav::console::user_message>
614
execute_file_contents(exec_context& ec, const std::filesystem::path& path)
38✔
615
{
616
    static const std::filesystem::path stdin_path("-");
38✔
617
    static const std::filesystem::path dev_stdin_path("/dev/stdin");
38✔
618

619
    FILE* file;
620

621
    if (path == stdin_path || path == dev_stdin_path) {
38✔
622
        if (isatty(STDIN_FILENO)) {
3✔
623
            return ec.make_error("stdin has already been consumed");
×
624
        }
625
        file = stdin;
3✔
626
    } else if ((file = fopen(path.c_str(), "re")) == nullptr) {
35✔
627
        return ec.make_error("unable to open file");
×
628
    }
629

630
    std::string out_name;
38✔
631

632
    auto_mem<char> line;
38✔
633
    size_t line_max_size;
634
    ssize_t line_size;
635
    multiline_executor me(ec, path.string());
38✔
636

637
    ec.ec_local_vars.top()["0"] = path.string();
114✔
638
    ec.ec_path_stack.emplace_back(path.parent_path());
38✔
639
    exec_context::output_guard og(ec);
76✔
640
    while ((line_size = getline(line.out(), &line_max_size, file)) != -1) {
872✔
641
        TRY(me.push_back(string_fragment::from_bytes(line.in(), line_size)));
840✔
642
    }
643

644
    TRY(me.final());
32✔
645
    auto retval = std::move(me.me_last_result);
32✔
646

647
    if (file == stdin) {
32✔
648
        if (isatty(STDOUT_FILENO)) {
3✔
649
            log_perror(dup2(STDOUT_FILENO, STDIN_FILENO));
×
650
        }
651
    } else {
652
        fclose(file);
29✔
653
    }
654
    ec.ec_path_stack.pop_back();
32✔
655

656
    return Ok(retval);
32✔
657
}
38✔
658

659
Result<std::string, lnav::console::user_message>
660
execute_file(exec_context& ec, const std::string& path_and_args)
41✔
661
{
662
    static const intern_string_t SRC = intern_string::lookup("cmdline");
95✔
663
    static auto op = lnav_operation{__FUNCTION__};
41✔
664

665
    auto op_guard = lnav_opid_guard::internal(op);
41✔
666

667
    std::string retval, msg;
41✔
668
    shlex lexer(path_and_args);
41✔
669

670
    log_info("Executing file: %s", path_and_args.c_str());
41✔
671

672
    auto split_args_res = lexer.split(scoped_resolver{&ec.ec_local_vars.top()});
41✔
673
    if (split_args_res.isErr()) {
41✔
674
        auto split_err = split_args_res.unwrapErr();
×
675
        auto um = lnav::console::user_message::error(
×
676
                      "unable to parse script command-line")
677
                      .with_reason(split_err.se_error.te_msg)
×
678
                      .with_snippet(lnav::console::snippet::from(
×
679
                          SRC, lexer.to_attr_line(split_err.se_error)))
×
680
                      .move();
×
681

682
        return Err(um);
×
683
    }
684
    auto split_args = split_args_res.unwrap();
41✔
685
    if (split_args.empty()) {
41✔
686
        return ec.make_error("no script specified");
×
687
    }
688

689
    ec.ec_local_vars.emplace();
41✔
690

691
    auto script_name = split_args[0].se_value;
41✔
692
    auto& vars = ec.ec_local_vars.top();
41✔
693
    std::string star, open_error = "file not found";
82✔
694

695
    add_ansi_vars(vars);
41✔
696

697
    vars["#"] = fmt::to_string(split_args.size() - 1);
123✔
698
    for (size_t lpc = 0; lpc < split_args.size(); lpc++) {
124✔
699
        vars[fmt::to_string(lpc)] = split_args[lpc].se_value;
83✔
700
    }
701
    for (size_t lpc = 1; lpc < split_args.size(); lpc++) {
83✔
702
        if (lpc > 1) {
42✔
703
            star.append(" ");
21✔
704
        }
705
        star.append(split_args[lpc].se_value);
42✔
706
    }
707
    vars["__all__"] = star;
41✔
708

709
    std::vector<script_metadata> paths_to_exec;
41✔
710

711
    auto scripts = find_format_scripts(lnav_data.ld_config_paths);
41✔
712
    auto iter = scripts.as_scripts.find(script_name);
41✔
713
    if (iter != scripts.as_scripts.end()) {
41✔
714
        paths_to_exec = iter->second;
28✔
715
    }
716
    if (lnav::filesystem::is_url(script_name)) {
41✔
717
        auto_mem<CURLU> cu(curl_url_cleanup);
×
718
        cu = curl_url();
×
719
        auto set_rc = curl_url_set(cu, CURLUPART_URL, script_name.c_str(), 0);
×
720
        if (set_rc == CURLUE_OK) {
×
721
            auto_mem<char> scheme_part(curl_free);
×
722
            auto get_rc
723
                = curl_url_get(cu, CURLUPART_SCHEME, scheme_part.out(), 0);
×
724
            if (get_rc == CURLUE_OK
×
725
                && string_fragment::from_c_str(scheme_part.in()) == "file")
×
726
            {
727
                auto_mem<char> path_part;
×
728
                auto get_rc
729
                    = curl_url_get(cu, CURLUPART_PATH, path_part.out(), 0);
×
730
                if (get_rc == CURLUE_OK) {
×
731
                    auto rp_res = lnav::filesystem::realpath(path_part.in());
×
732
                    if (rp_res.isOk()) {
×
733
                        struct script_metadata meta;
×
734

735
                        meta.sm_path = rp_res.unwrap();
×
736
                        extract_metadata_from_file(meta);
×
737
                        paths_to_exec.push_back(meta);
×
738
                    }
739
                }
740
            }
741
        }
742
    }
743
    if (script_name == "-" || script_name == "/dev/stdin") {
41✔
744
        paths_to_exec.push_back({script_name, "", "", ""});
3✔
745
    } else if (access(script_name.c_str(), R_OK) == 0) {
38✔
746
        struct script_metadata meta;
7✔
747
        auto rp_res = lnav::filesystem::realpath(script_name);
7✔
748

749
        if (rp_res.isErr()) {
7✔
750
            log_error("unable to get realpath() of %s -- %s",
×
751
                      script_name.c_str(),
752
                      rp_res.unwrapErr().c_str());
753
            meta.sm_path = script_name;
×
754
        } else {
755
            meta.sm_path = rp_res.unwrap();
7✔
756
        }
757
        extract_metadata_from_file(meta);
7✔
758
        paths_to_exec.push_back(meta);
7✔
759
    } else if (errno != ENOENT) {
38✔
760
        open_error = lnav::from_errno().message();
×
761
    } else {
762
        auto script_path = std::filesystem::path(script_name);
31✔
763

764
        if (!script_path.is_absolute()) {
31✔
765
            script_path = ec.ec_path_stack.back() / script_path;
31✔
766
        }
767

768
        if (std::filesystem::is_regular_file(script_path)) {
31✔
769
            script_metadata meta;
×
770

771
            meta.sm_path = script_path;
×
772
            extract_metadata_from_file(meta);
×
773
            paths_to_exec.push_back(meta);
×
774
        } else if (errno != ENOENT) {
31✔
775
            open_error = lnav::from_errno().message();
×
776
        }
777
    }
31✔
778

779
    if (!paths_to_exec.empty()) {
41✔
780
        for (auto& path_iter : paths_to_exec) {
70✔
781
            if (!ec.ec_output_stack.empty()) {
38✔
782
                ec.ec_output_stack.back().od_format
38✔
783
                    = path_iter.sm_output_format;
38✔
784
                log_info("%s: setting out format for '%s' to %d",
38✔
785
                         script_name.c_str(),
786
                         ec.ec_output_stack.back().od_name.c_str(),
787
                         ec.ec_output_stack.back().od_format);
788
            }
789
            retval = TRY(execute_file_contents(ec, path_iter.sm_path));
38✔
790
        }
791
    }
792
    ec.ec_local_vars.pop();
35✔
793

794
    if (paths_to_exec.empty()) {
35✔
795
        return ec.make_error(
796
            "unknown script -- {} -- {}", script_name, open_error);
3✔
797
    }
798

799
    return Ok(retval);
32✔
800
}
56✔
801

802
Result<std::string, lnav::console::user_message>
803
execute_from_file(exec_context& ec,
234✔
804
                  const std::string& src,
805
                  int line_number,
806
                  const std::string& cmdline)
807
{
808
    std::string retval, alt_msg;
234✔
809
    auto _sg
810
        = ec.enter_source(intern_string::lookup(src), line_number, cmdline);
234✔
811

812
    lnav_data.ld_view_stack.top() | [&ec](auto* tc) {
234✔
813
        ec.ec_top_line = tc->get_selection().value_or(0_vl);
230✔
814
    };
230✔
815
    switch (cmdline[0]) {
234✔
816
        case ':':
103✔
817
            retval = TRY(execute_command(ec, cmdline.substr(1)));
103✔
818
            break;
103✔
819
        case '/':
1✔
820
            execute_search(cmdline.substr(1));
1✔
821
            break;
1✔
822
        case ';': {
128✔
823
            if (!ec.ec_msg_callback_stack.empty()) {
128✔
824
                auto src_slash = src.rfind('/');
124✔
825
                auto cmd_al
826
                    = attr_line_t(cmdline.substr(0, cmdline.find('\n')));
124✔
827
                readline_lnav_highlighter(cmd_al, std::nullopt);
124✔
828
                const auto um = lnav::console::user_message::info(
124✔
829
                                    attr_line_t("Executing command at ")
124✔
830
                                        .append(lnav::roles::file(
124✔
831
                                            src_slash != std::string::npos
832
                                                ? src.substr(src_slash + 1)
248✔
833
                                                : src))
834
                                        .append(":")
124✔
835
                                        .append(lnav::roles::number(
248✔
836
                                            fmt::to_string(line_number)))
248✔
837
                                        .append(" \u2014 ")
124✔
838
                                        .append(cmd_al))
124✔
839
                                    .move();
124✔
840
                ec.ec_msg_callback_stack.back()(um);
124✔
841
            }
124✔
842

843
            setup_logline_table(ec);
128✔
844
            retval = TRY(execute_sql(ec, cmdline.substr(1), alt_msg));
128✔
845
            break;
122✔
846
        }
847
        case '|':
2✔
848
            retval = TRY(execute_file(ec, cmdline.substr(1)));
2✔
849
            break;
2✔
850
        default:
×
851
            retval = TRY(execute_command(ec, cmdline));
×
852
            break;
×
853
    }
854

855
    log_info(
228✔
856
        "%s:%d:execute result -- %s", src.c_str(), line_number, retval.c_str());
857

858
    return Ok(retval);
228✔
859
}
234✔
860

861
Result<std::string, lnav::console::user_message>
862
execute_any(exec_context& ec, const std::string& cmdline_with_mode)
37✔
863
{
864
    if (cmdline_with_mode.empty()) {
37✔
865
        auto um = lnav::console::user_message::error("empty command")
×
866
                      .with_help(
×
867
                          "a command should start with ':', ';', '/', '|' and "
868
                          "followed by the operation to perform")
869
                      .move();
×
870
        if (!ec.ec_source.empty()) {
×
871
            um.with_snippet(ec.ec_source.back());
×
872
        }
873
        return Err(um);
×
874
    }
875

876
    std::string retval, alt_msg, cmdline = cmdline_with_mode.substr(1);
37✔
877
    auto _cleanup = finally([&ec] {
37✔
878
        if (ec.is_read_write() &&
40✔
879
            // only rebuild in a script or non-interactive mode so we don't
880
            // block the UI.
881
            lnav_data.ld_flags & LNF_HEADLESS)
3✔
882
        {
883
            rescan_files();
×
884
            wait_for_pipers(std::nullopt);
×
885
            rebuild_indexes_repeatedly();
×
886
        }
887
    });
74✔
888

889
    switch (cmdline_with_mode[0]) {
37✔
890
        case ':':
×
891
            retval = TRY(execute_command(ec, cmdline));
×
892
            break;
×
893
        case '/':
×
894
            execute_search(cmdline);
×
895
            break;
×
896
        case ';':
31✔
897
            setup_logline_table(ec);
31✔
898
            retval = TRY(execute_sql(ec, cmdline, alt_msg));
31✔
899
            break;
31✔
900
        case '|': {
6✔
901
            retval = TRY(execute_file(ec, cmdline));
6✔
902
            break;
4✔
903
        }
904
        default:
×
905
            retval = TRY(execute_command(ec, cmdline));
×
906
            break;
×
907
    }
908

909
    return Ok(retval);
35✔
910
}
37✔
911

912
void
913
execute_init_commands(
605✔
914
    exec_context& ec,
915
    std::vector<std::pair<Result<std::string, lnav::console::user_message>,
916
                          std::string>>& msgs)
917
{
918
    if (lnav_data.ld_cmd_init_done) {
605✔
919
        return;
×
920
    }
921

922
    std::string out_name;
605✔
923
    std::optional<exec_context::output_t> ec_out;
605✔
924
    auto_fd fd_copy;
605✔
925
    int option_index = 1;
605✔
926

927
    {
928
        log_info("Executing initial commands");
605✔
929
        exec_context::output_guard og(ec, out_name, ec_out);
605✔
930

931
        for (auto& cmd : lnav_data.ld_commands) {
1,599✔
932
            static const auto COMMAND_OPTION_SRC
933
                = intern_string::lookup("command-option");
2,058✔
934

935
            std::string alt_msg;
994✔
936

937
            wait_for_children();
994✔
938

939
            lnav_data.ld_view_stack.top() | [&ec](auto* tc) {
994✔
940
                ec.ec_top_line = tc->get_selection().value_or(0_vl);
994✔
941
            };
994✔
942
            log_debug("init cmd: %s", cmd.c_str());
994✔
943
            {
944
                auto top_view = lnav_data.ld_view_stack.top();
994✔
945
                auto _sg
946
                    = ec.enter_source(COMMAND_OPTION_SRC, option_index++, cmd);
994✔
947
                switch (cmd.at(0)) {
994✔
948
                    case ':':
610✔
949
                        msgs.emplace_back(execute_command(ec, cmd.substr(1)),
610✔
950
                                          alt_msg);
951
                        break;
610✔
952
                    case '/':
6✔
953
                        execute_search(cmd.substr(1));
6✔
954
                        break;
6✔
955
                    case ';':
352✔
956
                        setup_logline_table(ec);
352✔
957
                        msgs.emplace_back(
352✔
958
                            execute_sql(ec, cmd.substr(1), alt_msg), alt_msg);
704✔
959
                        break;
352✔
960
                    case '|':
26✔
961
                        msgs.emplace_back(execute_file(ec, cmd.substr(1)),
26✔
962
                                          alt_msg);
963
                        break;
26✔
964
                }
965

966
                rescan_files();
994✔
967
                auto deadline = ui_clock::now();
994✔
968
                if (lnav_data.ld_flags & LNF_HEADLESS) {
994✔
969
                    deadline += 5s;
993✔
970
                } else {
971
                    deadline += 500ms;
1✔
972
                }
973
                wait_for_pipers(deadline);
994✔
974
                if (lnav_data.ld_flags & LNF_HEADLESS
994✔
975
                    || lnav_data.ld_input_dispatcher.id_count == 0)
1✔
976
                {
977
                    rebuild_indexes_repeatedly();
994✔
978
                } else {
979
                    rebuild_indexes(deadline);
×
980
                }
981
                if (top_view == lnav_data.ld_view_stack.top()) {
994✔
982
                    setup_initial_view_stack();
930✔
983
                }
984

985
                lnav::progress_tracker::instance().wait_for_completion();
994✔
986
            }
994✔
987
        }
994✔
988
    }
605✔
989
    lnav_data.ld_commands.clear();
605✔
990
    lnav_data.ld_cmd_init_done = true;
605✔
991
}
605✔
992

993
int
994
sql_callback(exec_context& ec, sqlite3_stmt* stmt)
4,143✔
995
{
996
    auto& dls = *(ec.ec_label_source_stack.back());
4,143✔
997
    const int ncols = sqlite3_column_count(stmt);
4,143✔
998

999
    if (!sqlite3_stmt_busy(stmt)) {
4,143✔
1000
        dls.clear();
1,484✔
1001

1002
        for (int lpc = 0; lpc < ncols; lpc++) {
4,508✔
1003
            const int type = sqlite3_column_type(stmt, lpc);
3,024✔
1004
            std::string colname = sqlite3_column_name(stmt, lpc);
3,024✔
1005

1006
            dls.push_header(colname, type);
3,024✔
1007
        }
3,024✔
1008
        return 0;
1,484✔
1009
    }
1010

1011
    int retval = 0;
2,659✔
1012
    auto set_vars = dls.dls_row_cursors.empty();
2,659✔
1013

1014
    if (dls.dls_row_cursors.empty()) {
2,659✔
1015
        dls.dls_query_start = std::chrono::steady_clock::now();
1,434✔
1016
        for (int lpc = 0; lpc < ncols; lpc++) {
4,275✔
1017
            int type = sqlite3_column_type(stmt, lpc);
2,841✔
1018
            std::string colname = sqlite3_column_name(stmt, lpc);
2,841✔
1019

1020
            bool graphable = (type == SQLITE_INTEGER || type == SQLITE_FLOAT);
2,841✔
1021
            auto& hm = dls.dls_headers[lpc];
2,841✔
1022
            hm.hm_column_type = type;
2,841✔
1023
            if (lnav_data.ld_db_key_names.count(colname) > 0) {
2,841✔
1024
                hm.hm_graphable = false;
565✔
1025
            } else if (graphable) {
2,276✔
1026
                dls.set_col_as_graphable(lpc);
650✔
1027
            }
1028
        }
2,841✔
1029
    }
1030

1031
    dls.dls_row_cursors.emplace_back(dls.dls_cell_container.end_cursor());
2,659✔
1032
    dls.dls_push_column = 0;
2,659✔
1033
    for (int lpc = 0; lpc < ncols; lpc++) {
11,862✔
1034
        const auto value_type = sqlite3_column_type(stmt, lpc);
9,203✔
1035
        db_label_source::column_value_t value;
9,203✔
1036
        auto& hm = dls.dls_headers[lpc];
9,203✔
1037

1038
        switch (value_type) {
9,203✔
1039
            case SQLITE_INTEGER:
2,681✔
1040
                value = (int64_t) sqlite3_column_int64(stmt, lpc);
2,681✔
1041
                hm.hm_align = text_align_t::end;
2,681✔
1042
                break;
2,681✔
1043
            case SQLITE_FLOAT:
231✔
1044
                value = sqlite3_column_double(stmt, lpc);
231✔
1045
                hm.hm_align = text_align_t::end;
231✔
1046
                break;
231✔
1047
            case SQLITE_NULL:
2,191✔
1048
                value = null_value_t{};
2,191✔
1049
                break;
2,191✔
1050
            default: {
4,100✔
1051
                auto frag = string_fragment::from_bytes(
4,100✔
1052
                    sqlite3_column_text(stmt, lpc),
1053
                    sqlite3_column_bytes(stmt, lpc));
4,100✔
1054
                if (!frag.empty()) {
4,100✔
1055
                    if (isdigit(frag[0])) {
4,033✔
1056
                        hm.hm_align = text_align_t::end;
1,394✔
1057
                        if (!hm.hm_graphable.has_value()) {
1,394✔
1058
                            auto split_res = humanize::try_from<double>(frag);
246✔
1059
                            if (split_res.has_value()) {
246✔
1060
                                dls.set_col_as_graphable(lpc);
58✔
1061
                            } else {
1062
                                hm.hm_graphable = false;
188✔
1063
                            }
1064
                        }
1065
                    } else if (!hm.hm_graphable.has_value()) {
2,639✔
1066
                        hm.hm_graphable = false;
1,037✔
1067
                    }
1068
                }
1069
                value = frag;
4,100✔
1070
                break;
4,100✔
1071
            }
1072
        }
1073
        dls.push_column(value);
9,203✔
1074
        if ((hm.hm_column_type == SQLITE_TEXT
9,203✔
1075
             || hm.hm_column_type == SQLITE_NULL)
5,002✔
1076
            && hm.hm_sub_type == 0)
6,324✔
1077
        {
1078
            switch (value_type) {
6,210✔
1079
                case SQLITE_TEXT:
3,988✔
1080
                    auto* raw_value = sqlite3_column_value(stmt, lpc);
3,988✔
1081
                    hm.hm_column_type = SQLITE_TEXT;
3,988✔
1082
                    hm.hm_sub_type = sqlite3_value_subtype(raw_value);
3,988✔
1083
                    break;
3,988✔
1084
            }
1085
        }
1086
        if (set_vars && !ec.ec_local_vars.empty() && !ec.ec_dry_run) {
9,203✔
1087
            if (sql_ident_needs_quote(hm.hm_name.c_str())) {
2,841✔
1088
                continue;
1,151✔
1089
            }
1090
            auto& vars = ec.ec_local_vars.top();
1,690✔
1091

1092
            vars[hm.hm_name] = value.match(
3,380✔
1093
                [](const string_fragment& sf) {
×
1094
                    return scoped_value_t{sf.to_string()};
743✔
1095
                },
1096
                [](int64_t i) { return scoped_value_t{i}; },
480✔
1097
                [](double d) { return scoped_value_t{d}; },
15✔
1098
                [](null_value_t) { return scoped_value_t{null_value_t{}}; });
3,832✔
1099
        }
1100
    }
9,203✔
1101

1102
    return retval;
2,659✔
1103
}
1104

1105
int
1106
internal_sql_callback(exec_context& ec, sqlite3_stmt* stmt)
26✔
1107
{
1108
    if (!sqlite3_stmt_busy(stmt)) {
26✔
1109
        return 0;
15✔
1110
    }
1111

1112
    const int ncols = sqlite3_column_count(stmt);
11✔
1113

1114
    auto& vars = ec.ec_local_vars.top();
11✔
1115

1116
    for (int lpc = 0; lpc < ncols; lpc++) {
27✔
1117
        const char* column_name = sqlite3_column_name(stmt, lpc);
16✔
1118

1119
        if (sql_ident_needs_quote(column_name)) {
16✔
1120
            continue;
×
1121
        }
1122

1123
        auto value_type = sqlite3_column_type(stmt, lpc);
16✔
1124
        scoped_value_t value;
16✔
1125
        switch (value_type) {
16✔
1126
            case SQLITE_INTEGER:
2✔
1127
                value = (int64_t) sqlite3_column_int64(stmt, lpc);
2✔
1128
                break;
2✔
1129
            case SQLITE_FLOAT:
×
1130
                value = sqlite3_column_double(stmt, lpc);
×
1131
                break;
×
1132
            case SQLITE_NULL:
1✔
1133
                value = null_value_t{};
1✔
1134
                break;
1✔
1135
            default:
13✔
1136
                value
1137
                    = std::string((const char*) sqlite3_column_text(stmt, lpc),
26✔
1138
                                  sqlite3_column_bytes(stmt, lpc));
26✔
1139
                break;
13✔
1140
        }
1141
        vars[column_name] = value;
16✔
1142
    }
16✔
1143

1144
    return 0;
11✔
1145
}
1146

1147
std::future<std::string>
1148
pipe_callback(exec_context& ec, const std::string& cmdline, auto_fd& fd)
7✔
1149
{
1150
    static auto& prompt = lnav::prompt::get();
7✔
1151
    auto out = ec.get_output();
7✔
1152

1153
    if (out) {
7✔
1154
        FILE* file = *out;
7✔
1155

1156
        return std::async(std::launch::async, [fd = std::move(fd), file]() {
14✔
1157
            char buffer[1024];
1158
            ssize_t rc;
1159

1160
            if (file == stdout) {
7✔
1161
                lnav_data.ld_stdout_used = true;
7✔
1162
            }
1163

1164
            while ((rc = read(fd, buffer, sizeof(buffer))) > 0) {
14✔
1165
                fwrite(buffer, rc, 1, file);
7✔
1166
            }
1167

1168
            return std::string();
14✔
1169
        });
7✔
1170
    }
1171
    std::error_code errc;
×
1172
    std::filesystem::create_directories(lnav::paths::workdir(), errc);
×
1173
    auto open_temp_res = lnav::filesystem::open_temp_file(lnav::paths::workdir()
×
1174
                                                          / "exec.XXXXXX");
×
1175
    if (open_temp_res.isErr()) {
×
1176
        return lnav::futures::make_ready_future(
1177
            fmt::format(FMT_STRING("error: cannot open temp file -- {}"),
×
1178
                        open_temp_res.unwrapErr()));
×
1179
    }
1180

1181
    auto tmp_pair = open_temp_res.unwrap();
×
1182

1183
    auto reader = std::thread(
1184
        [in_fd = std::move(fd), out_fd = std::move(tmp_pair.second)]() {
×
1185
            char buffer[1024];
1186
            ssize_t rc;
1187

1188
            while ((rc = read(in_fd, buffer, sizeof(buffer))) > 0) {
×
1189
                write(out_fd, buffer, rc);
×
1190
            }
1191
        });
×
1192
    reader.detach();
×
1193

1194
    static int exec_count = 0;
1195
    auto desc
1196
        = fmt::format(FMT_STRING("exec-{}-output {}"), exec_count++, cmdline);
×
1197
    lnav_data.ld_active_files.fc_file_names[tmp_pair.first]
×
1198
        .with_filename(desc)
×
1199
        .with_include_in_session(false)
×
1200
        .with_detect_format(false)
×
1201
        .with_init_location(0_vl);
×
1202
    lnav_data.ld_files_to_front.emplace_back(desc);
×
1203
    prompt.p_editor.set_alt_value(HELP_MSG_1(X, "to close the file"));
×
1204

1205
    return lnav::futures::make_ready_future(std::string());
×
1206
}
1207

1208
void
1209
add_global_vars(exec_context& ec)
676✔
1210
{
1211
    for (const auto& iter : lnav_config.lc_global_vars) {
10,822✔
1212
        shlex subber(iter.second);
10,146✔
1213
        std::string str;
10,146✔
1214

1215
        if (!subber.eval(str, scoped_resolver{&ec.ec_global_vars})) {
10,146✔
1216
            log_error("Unable to evaluate global variable value: %s",
×
1217
                      iter.second.c_str());
1218
            continue;
×
1219
        }
1220

1221
        ec.ec_global_vars[iter.first] = str;
10,146✔
1222
    }
10,146✔
1223
}
676✔
1224

1225
void
1226
exec_context::set_output(const std::string& name,
606✔
1227
                         FILE* file,
1228
                         int (*closer)(FILE*))
1229
{
1230
    log_info("redirecting command output to: %s", name.c_str());
606✔
1231
    this->ec_output_stack.back().od_output | [](auto out) {
606✔
1232
        if (out.second != nullptr) {
×
1233
            out.second(out.first);
×
1234
        }
1235
    };
1236
    this->ec_output_stack.back()
606✔
1237
        = output_desc{name, std::make_pair(file, closer)};
1,212✔
1238
}
606✔
1239

1240
void
1241
exec_context::clear_output()
73✔
1242
{
1243
    log_info("redirecting command output to screen");
73✔
1244
    this->ec_output_stack.back().od_output | [](auto out) {
73✔
1245
        if (out.second != nullptr) {
35✔
1246
            out.second(out.first);
35✔
1247
        }
1248
    };
35✔
1249
    this->ec_output_stack.back().od_name = "default";
73✔
1250
    this->ec_output_stack.back().od_output = std::nullopt;
73✔
1251
}
73✔
1252

1253
exec_context::exec_context(logline_value_vector* line_values,
3,531✔
1254
                           sql_callback_t sql_callback,
1255
                           pipe_callback_t pipe_callback)
3,531✔
1256
    : ec_line_values(line_values),
3,531✔
1257
      ec_accumulator(std::make_unique<attr_line_t>()),
3,531✔
1258
      ec_sql_callback(sql_callback), ec_pipe_callback(pipe_callback)
7,062✔
1259
{
1260
    this->ec_local_vars.push(std::map<std::string, scoped_value_t>());
3,531✔
1261
    this->ec_path_stack.emplace_back(".");
3,531✔
1262
    this->ec_output_stack.emplace_back("screen", std::nullopt);
3,531✔
1263
}
3,531✔
1264

1265
Result<std::string, lnav::console::user_message>
1266
exec_context::execute(source_location loc, const std::string& cmdline)
2✔
1267
{
1268
    static auto& prompt = lnav::prompt::get();
2✔
1269
    static const auto& dls = lnav_data.ld_db_row_source;
1270

1271
    lnav::textinput::history::op_guard hist_guard;
2✔
1272
    auto sg = this->enter_source(loc, cmdline);
2✔
1273

1274
    auto before_dls_gen = dls.dls_generation;
2✔
1275
    if (this->get_provenance<mouse_input>() && !prompt.p_editor.is_enabled()) {
2✔
1276
        auto& hist = prompt.get_history_for(cmdline[0]);
×
1277
        hist_guard = hist.start_operation(cmdline.substr(1));
×
1278
    }
1279

1280
    auto exec_res = execute_any(*this, cmdline);
2✔
1281
    if (exec_res.isErr()) {
2✔
1282
        hist_guard.og_status = log_level_t::LEVEL_ERROR;
×
1283
        if (!this->ec_msg_callback_stack.empty()) {
×
1284
            this->ec_msg_callback_stack.back()(exec_res.unwrapErr());
×
1285
        }
1286
    } else {
1287
        if (before_dls_gen != dls.dls_generation
4✔
1288
            && dls.dls_row_cursors.size() > 1)
2✔
1289
        {
1290
            ensure_view(LNV_DB);
×
1291
        }
1292
    }
1293

1294
    return exec_res;
4✔
1295
}
2✔
1296

1297
void
1298
exec_context::add_error_context(lnav::console::user_message& um) const
74✔
1299
{
1300
    switch (um.um_level) {
74✔
1301
        case lnav::console::user_message::level::raw:
×
1302
        case lnav::console::user_message::level::info:
1303
        case lnav::console::user_message::level::ok:
1304
            return;
×
1305
        default:
74✔
1306
            break;
74✔
1307
    }
1308

1309
    if (!this->get_provenance<keyboard_input>() && um.um_snippets.empty()) {
74✔
1310
        um.with_snippets(this->ec_source);
34✔
1311
        um.remove_internal_snippets();
34✔
1312
    }
1313

1314
    if (this->ec_current_help != nullptr && um.um_help.empty()) {
74✔
1315
        attr_line_t help;
33✔
1316

1317
        format_help_text_for_term(*this->ec_current_help,
33✔
1318
                                  70,
1319
                                  help,
1320
                                  help_text_content::synopsis_and_summary);
1321
        um.with_help(help);
33✔
1322
    }
33✔
1323
}
1324

1325
lnav::console::user_message
1326
exec_context::make_error_from_str(std::string&& str) const
28✔
1327
{
1328
    auto retval = lnav::console::user_message::error(std::move(str));
28✔
1329
    this->add_error_context(retval);
28✔
1330
    return retval;
28✔
1331
}
×
1332

1333
exec_context::sql_callback_guard
1334
exec_context::push_callback(sql_callback_t cb)
5✔
1335
{
1336
    return sql_callback_guard(*this, cb);
5✔
1337
}
1338

1339
exec_context::source_guard
1340
exec_context::enter_source(source_location loc, const std::string& content)
2,450✔
1341
{
1342
    attr_line_t content_al{content};
2,450✔
1343
    content_al.with_attr_for_all(VC_ROLE.value(role_t::VCR_QUOTED_CODE));
2,450✔
1344
    readline_lnav_highlighter(content_al, -1);
2,450✔
1345
    this->ec_source.emplace_back(lnav::console::snippet::from(loc, content_al));
2,450✔
1346
    return {this};
4,900✔
1347
}
2,450✔
1348

1349
exec_context::output_guard::output_guard(exec_context& context,
676✔
1350
                                         std::string name,
1351
                                         const std::optional<output_t>& file,
1352
                                         text_format_t tf)
676✔
1353
    : sg_context(context), sg_active(!name.empty())
676✔
1354
{
1355
    if (name.empty()) {
676✔
1356
        return;
605✔
1357
    }
1358
    if (file) {
71✔
1359
        log_info("redirecting command output to: %s", name.c_str());
33✔
1360
    } else if (!context.ec_output_stack.empty()) {
38✔
1361
        tf = context.ec_output_stack.back().od_format;
38✔
1362
    }
1363
    context.ec_output_stack.emplace_back(std::move(name), file, tf);
71✔
1364
}
1365

1366
exec_context::output_guard::~output_guard()
676✔
1367
{
1368
    if (this->sg_active) {
676✔
1369
        this->sg_context.clear_output();
71✔
1370
        this->sg_context.ec_output_stack.pop_back();
71✔
1371
    }
1372
}
676✔
1373
exec_context::sql_callback_guard::sql_callback_guard(exec_context& context,
5✔
1374
                                                     sql_callback_t cb)
5✔
1375
    : scg_context(context), scg_old_callback(context.ec_sql_callback)
5✔
1376
{
1377
    context.ec_sql_callback = cb;
5✔
1378
}
5✔
1379
exec_context::sql_callback_guard::sql_callback_guard(sql_callback_guard&& other)
×
1380
    : scg_context(other.scg_context),
×
1381
      scg_old_callback(std::exchange(other.scg_old_callback, nullptr))
×
1382
{
1383
}
1384
exec_context::sql_callback_guard::~sql_callback_guard()
5✔
1385
{
1386
    if (this->scg_old_callback != nullptr) {
5✔
1387
        this->scg_context.ec_sql_callback = this->scg_old_callback;
5✔
1388
    }
1389
}
5✔
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