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

tstack / lnav / 18921488161-2616

29 Oct 2025 08:39PM UTC coverage: 68.075% (-0.8%) from 68.921%
18921488161-2616

push

github

tstack
[files-panel] show file load progress

18 of 23 new or added lines in 6 files covered. (78.26%)

607 existing lines in 26 files now uncovered.

49621 of 72892 relevant lines covered (68.07%)

427861.28 hits per line

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

81.76
/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,741✔
75
{
76
    if (lnav_data.ld_window == nullptr) {
11,741✔
77
        return 0;
11,732✔
78
    }
79

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

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

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

105
    return 0;
8✔
106
}
107

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

115
    sql_counter = 0;
3✔
116

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

121
    auto* breadcrumb_view = injector::get<breadcrumb_curses*>();
3✔
122
    breadcrumb_view->set_enabled(true);
3✔
123
    lnav_data.ld_status[LNS_TOP].set_enabled(true);
3✔
124
    lnav_data.ld_view_stack.top() | [](auto* tc) { tc->set_enabled(true); };
5✔
125
    lnav_data.ld_bottom_source.update_loading(0, 0);
3✔
126
    lnav_data.ld_status[LNS_BOTTOM].set_needs_update();
3✔
127
    lnav_data.ld_status_refresher(lnav::func::op_type::blocking);
3✔
128
    lnav_data.ld_views[LNV_DB].redo_search();
3✔
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)
704✔
139
{
140
    static auto op = lnav_operation{__FUNCTION__};
704✔
141

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

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

147
    split_ws(cmdline, args);
704✔
148

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

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

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

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

168
            return retval;
661✔
169
        } catch (const std::exception& e) {
704✔
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
}
704✔
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,657✔
184
{
185
    std::map<std::string, scoped_value_t> retval;
1,657✔
186
    auto param_count = sqlite3_bind_parameter_count(stmt);
1,657✔
187
    for (int lpc = 0; lpc < param_count; lpc++) {
1,825✔
188
        std::map<std::string, std::string>::iterator ov_iter;
169✔
189
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
169✔
190
        if (name == nullptr) {
169✔
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] == '$') {
168✔
207
            const auto& lvars = ec.ec_local_vars.top();
139✔
208
            const auto& gvars = ec.ec_global_vars;
139✔
209
            std::map<std::string, scoped_value_t>::const_iterator local_var,
139✔
210
                global_var;
139✔
211
            const char* env_value;
212

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

217
                ncplane_dim_yx(lnav_data.ld_window, &lines, &cols);
6✔
218
                if (strcmp(name, "$LINES") == 0) {
6✔
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) {
6✔
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()) {
417✔
228
                mapbox::util::apply_visitor(
124✔
229
                    sqlitepp::bind_visitor(stmt, lpc + 1), local_var->second);
124✔
230
                retval[name] = local_var->second;
372✔
231
            } else if ((global_var = gvars.find(&name[1])) != gvars.end()) {
45✔
232
                mapbox::util::apply_visitor(
3✔
233
                    sqlitepp::bind_visitor(stmt, lpc + 1), global_var->second);
3✔
234
                retval[name] = global_var->second;
9✔
235
            } else if ((env_value = getenv(&name[1])) != nullptr) {
12✔
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,656✔
281
}
1,657✔
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,672✔
295
{
296
    static auto op = lnav_operation{__FUNCTION__};
1,672✔
297

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

305
    if (lnav::sql::is_prql(stmt_str)) {
1,672✔
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,671✔
357

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

364
    if (startswith(stmt_str, ".")) {
1,671✔
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,664✔
383

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

392
    const auto* curr_stmt = stmt_str.c_str();
1,664✔
393
    auto last_is_readonly = false;
1,664✔
394
    while (curr_stmt != nullptr) {
3,264✔
395
        const char* tail = nullptr;
3,264✔
396
        while (isspace(*curr_stmt)) {
3,264✔
397
            curr_stmt += 1;
×
398
        }
399
        retcode = sqlite3_prepare_v2(
3,264✔
400
            lnav_data.ld_db.in(), curr_stmt, -1, stmt.out(), &tail);
401
        if (retcode != SQLITE_OK) {
3,264✔
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,257✔
430
            retcode = SQLITE_DONE;
1,600✔
431
            break;
1,600✔
432
        }
433
#ifdef HAVE_SQLITE3_STMT_READONLY
434
        last_is_readonly = sqlite3_stmt_readonly(stmt.in());
1,657✔
435
        if (ec.is_read_only() && !last_is_readonly) {
1,657✔
436
            return ec.make_error(
437
                "modifying statements are not allowed in this context: {}",
438
                sql);
×
439
        }
440
#endif
441
        bool done = false;
1,657✔
442

443
        auto bound_values = TRY(bind_sql_parameters(ec, stmt.in()));
1,657✔
444
        if (last_is_readonly) {
1,656✔
445
            ec.ec_sql_callback(ec, stmt.in());
1,526✔
446
        }
447
        while (!done) {
5,964✔
448
            retcode = sqlite3_step(stmt.in());
4,364✔
449

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

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

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

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

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

471
                    if (!bound_values.empty()) {
56✔
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);
56✔
498
                    auto um = sqlite3_error_to_user_message(lnav_data.ld_db)
112✔
499
                                  .with_note(bound_note)
56✔
500
                                  .move();
56✔
501

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

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

512
        curr_stmt = tail;
1,600✔
513
    }
1,656✔
514

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

519
        size_t memory_usage = 0, total_size = 0, cached_chunks = 0;
1,500✔
520
        for (auto cc = dls.dls_cell_container.cc_first.get(); cc != nullptr;
3,001✔
521
             cc = cc->cc_next.get())
1,501✔
522
        {
523
            total_size += cc->cc_capacity;
1,501✔
524
            if (cc->cc_data) {
1,501✔
525
                cached_chunks += 1;
1,500✔
526
                memory_usage += cc->cc_capacity;
1,500✔
527
            } else {
528
                memory_usage += cc->cc_compressed_size;
1✔
529
            }
530
        }
531
        log_debug(
1,500✔
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,600✔
540
    lnav_data.ld_mode = old_mode;
1,600✔
541
    if (retcode == SQLITE_DONE) {
1,600✔
542
        if (lnav_data.ld_log_source.is_line_meta_changed()) {
1,600✔
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,600✔
547
        lnav_data.ld_files_view.reload_data();
1,600✔
548

549
        lnav_data.ld_active_files.fc_files
550
            | lnav::itertools::for_each(&logfile::dump_stats);
1,600✔
551
        if (ec.ec_sql_callback != sql_callback) {
1,600✔
552
            retval = ec.ec_accumulator->get_string();
44✔
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,600✔
600
}
1,672✔
601

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

610
    return Ok();
224✔
611
}
612

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

619
    FILE* file;
620

621
    if (path == stdin_path || path == dev_stdin_path) {
37✔
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) {
34✔
627
        return ec.make_error("unable to open file");
×
628
    }
629

630
    std::string out_name;
37✔
631

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

637
    ec.ec_local_vars.top()["0"] = path.string();
111✔
638
    ec.ec_path_stack.emplace_back(path.parent_path());
37✔
639
    exec_context::output_guard og(ec);
74✔
640
    while ((line_size = getline(line.out(), &line_max_size, file)) != -1) {
856✔
641
        TRY(me.push_back(string_fragment::from_bytes(line.in(), line_size)));
824✔
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
}
37✔
658

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

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

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

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

672
    auto split_args_res = lexer.split(scoped_resolver{&ec.ec_local_vars.top()});
39✔
673
    if (split_args_res.isErr()) {
39✔
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();
39✔
685
    if (split_args.empty()) {
39✔
686
        return ec.make_error("no script specified");
×
687
    }
688

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

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

695
    add_ansi_vars(vars);
39✔
696

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

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

711
    auto scripts = find_format_scripts(lnav_data.ld_config_paths);
39✔
712
    auto iter = scripts.as_scripts.find(script_name);
39✔
713
    if (iter != scripts.as_scripts.end()) {
39✔
714
        paths_to_exec = iter->second;
27✔
715
    }
716
    if (lnav::filesystem::is_url(script_name)) {
39✔
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") {
39✔
744
        paths_to_exec.push_back({script_name, "", "", ""});
3✔
745
    } else if (access(script_name.c_str(), R_OK) == 0) {
36✔
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) {
36✔
760
        open_error = lnav::from_errno().message();
×
761
    } else {
762
        auto script_path = std::filesystem::path(script_name);
29✔
763

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

768
        if (std::filesystem::is_regular_file(script_path)) {
29✔
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) {
29✔
775
            open_error = lnav::from_errno().message();
×
776
        }
777
    }
29✔
778

779
    if (!paths_to_exec.empty()) {
39✔
780
        for (auto& path_iter : paths_to_exec) {
69✔
781
            if (!ec.ec_output_stack.empty()) {
37✔
782
                ec.ec_output_stack.back().od_format
37✔
783
                    = path_iter.sm_output_format;
37✔
784
                log_info("%s: setting out format for '%s' to %d",
37✔
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));
37✔
790
        }
791
    }
792
    ec.ec_local_vars.pop();
34✔
793

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

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

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

812
    lnav_data.ld_view_stack.top() | [&ec](auto* tc) {
229✔
813
        ec.ec_top_line = tc->get_selection().value_or(0_vl);
227✔
814
    };
227✔
815
    switch (cmdline[0]) {
229✔
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 ';': {
123✔
823
            if (!ec.ec_msg_callback_stack.empty()) {
123✔
824
                auto src_slash = src.rfind('/');
119✔
825
                auto cmd_al
826
                    = attr_line_t(cmdline.substr(0, cmdline.find('\n')));
119✔
827
                readline_lnav_highlighter(cmd_al, std::nullopt);
119✔
828
                const auto um = lnav::console::user_message::info(
119✔
829
                                    attr_line_t("Executing command at ")
119✔
830
                                        .append(lnav::roles::file(
119✔
831
                                            src_slash != std::string::npos
832
                                                ? src.substr(src_slash + 1)
238✔
833
                                                : src))
834
                                        .append(":")
119✔
835
                                        .append(lnav::roles::number(
238✔
836
                                            fmt::to_string(line_number)))
238✔
837
                                        .append(" \u2014 ")
119✔
838
                                        .append(cmd_al))
119✔
839
                                    .move();
119✔
840
                ec.ec_msg_callback_stack.back()(um);
119✔
841
            }
119✔
842

843
            setup_logline_table(ec);
123✔
844
            retval = TRY(execute_sql(ec, cmdline.substr(1), alt_msg));
123✔
845
            break;
118✔
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(
224✔
856
        "%s:%d:execute result -- %s", src.c_str(), line_number, retval.c_str());
857

858
    return Ok(retval);
224✔
859
}
229✔
860

861
Result<std::string, lnav::console::user_message>
862
execute_any(exec_context& ec, const std::string& cmdline_with_mode)
36✔
863
{
864
    if (cmdline_with_mode.empty()) {
36✔
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);
36✔
877
    auto _cleanup = finally([&ec] {
36✔
878
        if (ec.is_read_write() &&
38✔
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)
2✔
882
        {
883
            rescan_files();
×
884
            wait_for_pipers(std::nullopt);
×
885
            rebuild_indexes_repeatedly();
×
886
        }
887
    });
72✔
888

889
    switch (cmdline_with_mode[0]) {
36✔
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 '|': {
5✔
901
            retval = TRY(execute_file(ec, cmdline));
5✔
902
            break;
4✔
903
        }
904
        default:
×
905
            retval = TRY(execute_command(ec, cmdline));
×
906
            break;
×
907
    }
908

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

912
void
913
execute_init_commands(
592✔
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) {
592✔
919
        return;
×
920
    }
921

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

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

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

935
            std::string alt_msg;
970✔
936

937
            wait_for_children();
970✔
938

939
            lnav_data.ld_view_stack.top() | [&ec](auto* tc) {
970✔
940
                ec.ec_top_line = tc->get_selection().value_or(0_vl);
970✔
941
            };
970✔
942
            log_debug("init cmd: %s", cmd.c_str());
970✔
943
            {
944
                auto top_view = lnav_data.ld_view_stack.top();
970✔
945
                auto _sg
946
                    = ec.enter_source(COMMAND_OPTION_SRC, option_index++, cmd);
970✔
947
                switch (cmd.at(0)) {
970✔
948
                    case ':':
586✔
949
                        msgs.emplace_back(execute_command(ec, cmd.substr(1)),
586✔
950
                                          alt_msg);
951
                        break;
586✔
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();
970✔
967
                auto deadline = ui_clock::now();
970✔
968
                if (lnav_data.ld_flags & LNF_HEADLESS) {
970✔
969
                    deadline += 5s;
969✔
970
                } else {
971
                    deadline += 500ms;
1✔
972
                }
973
                wait_for_pipers(deadline);
970✔
974
                rebuild_indexes_repeatedly();
970✔
975
                if (top_view == lnav_data.ld_view_stack.top()) {
970✔
976
                    setup_initial_view_stack();
909✔
977
                }
978
            }
970✔
979
        }
970✔
980
    }
592✔
981
    lnav_data.ld_commands.clear();
592✔
982
    lnav_data.ld_cmd_init_done = true;
592✔
983
}
592✔
984

985
int
986
sql_callback(exec_context& ec, sqlite3_stmt* stmt)
4,143✔
987
{
988
    auto& dls = *(ec.ec_label_source_stack.back());
4,143✔
989
    const int ncols = sqlite3_column_count(stmt);
4,143✔
990

991
    if (!sqlite3_stmt_busy(stmt)) {
4,143✔
992
        dls.clear();
1,484✔
993

994
        for (int lpc = 0; lpc < ncols; lpc++) {
4,508✔
995
            const int type = sqlite3_column_type(stmt, lpc);
3,024✔
996
            std::string colname = sqlite3_column_name(stmt, lpc);
3,024✔
997

998
            dls.push_header(colname, type);
3,024✔
999
        }
3,024✔
1000
        return 0;
1,484✔
1001
    }
1002

1003
    int retval = 0;
2,659✔
1004
    auto set_vars = dls.dls_row_cursors.empty();
2,659✔
1005

1006
    if (dls.dls_row_cursors.empty()) {
2,659✔
1007
        dls.dls_query_start = std::chrono::steady_clock::now();
1,434✔
1008
        for (int lpc = 0; lpc < ncols; lpc++) {
4,275✔
1009
            int type = sqlite3_column_type(stmt, lpc);
2,841✔
1010
            std::string colname = sqlite3_column_name(stmt, lpc);
2,841✔
1011

1012
            bool graphable = (type == SQLITE_INTEGER || type == SQLITE_FLOAT);
2,841✔
1013
            auto& hm = dls.dls_headers[lpc];
2,841✔
1014
            hm.hm_column_type = type;
2,841✔
1015
            if (lnav_data.ld_db_key_names.count(colname) > 0) {
2,841✔
1016
                hm.hm_graphable = false;
565✔
1017
            } else if (graphable) {
2,276✔
1018
                dls.set_col_as_graphable(lpc);
650✔
1019
            }
1020
        }
2,841✔
1021
    }
1022

1023
    dls.dls_row_cursors.emplace_back(dls.dls_cell_container.end_cursor());
2,659✔
1024
    dls.dls_push_column = 0;
2,659✔
1025
    for (int lpc = 0; lpc < ncols; lpc++) {
11,862✔
1026
        const auto value_type = sqlite3_column_type(stmt, lpc);
9,203✔
1027
        db_label_source::column_value_t value;
9,203✔
1028
        auto& hm = dls.dls_headers[lpc];
9,203✔
1029

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

1084
            vars[hm.hm_name] = value.match(
3,380✔
1085
                [](const string_fragment& sf) {
×
1086
                    return scoped_value_t{sf.to_string()};
743✔
1087
                },
1088
                [](int64_t i) { return scoped_value_t{i}; },
480✔
1089
                [](double d) { return scoped_value_t{d}; },
15✔
1090
                [](null_value_t) { return scoped_value_t{null_value_t{}}; });
3,832✔
1091
        }
1092
    }
9,203✔
1093

1094
    return retval;
2,659✔
1095
}
1096

1097
int
1098
internal_sql_callback(exec_context& ec, sqlite3_stmt* stmt)
20✔
1099
{
1100
    if (!sqlite3_stmt_busy(stmt)) {
20✔
1101
        return 0;
11✔
1102
    }
1103

1104
    const int ncols = sqlite3_column_count(stmt);
9✔
1105

1106
    auto& vars = ec.ec_local_vars.top();
9✔
1107

1108
    for (int lpc = 0; lpc < ncols; lpc++) {
21✔
1109
        const char* column_name = sqlite3_column_name(stmt, lpc);
12✔
1110

1111
        if (sql_ident_needs_quote(column_name)) {
12✔
1112
            continue;
×
1113
        }
1114

1115
        auto value_type = sqlite3_column_type(stmt, lpc);
12✔
1116
        scoped_value_t value;
12✔
1117
        switch (value_type) {
12✔
1118
            case SQLITE_INTEGER:
1✔
1119
                value = (int64_t) sqlite3_column_int64(stmt, lpc);
1✔
1120
                break;
1✔
1121
            case SQLITE_FLOAT:
×
1122
                value = sqlite3_column_double(stmt, lpc);
×
1123
                break;
×
UNCOV
1124
            case SQLITE_NULL:
×
UNCOV
1125
                value = null_value_t{};
×
UNCOV
1126
                break;
×
1127
            default:
11✔
1128
                value
1129
                    = std::string((const char*) sqlite3_column_text(stmt, lpc),
22✔
1130
                                  sqlite3_column_bytes(stmt, lpc));
22✔
1131
                break;
11✔
1132
        }
1133
        vars[column_name] = value;
12✔
1134
    }
12✔
1135

1136
    return 0;
9✔
1137
}
1138

1139
std::future<std::string>
1140
pipe_callback(exec_context& ec, const std::string& cmdline, auto_fd& fd)
7✔
1141
{
1142
    static auto& prompt = lnav::prompt::get();
7✔
1143
    auto out = ec.get_output();
7✔
1144

1145
    if (out) {
7✔
1146
        FILE* file = *out;
7✔
1147

1148
        return std::async(std::launch::async, [fd = std::move(fd), file]() {
14✔
1149
            char buffer[1024];
1150
            ssize_t rc;
1151

1152
            if (file == stdout) {
7✔
1153
                lnav_data.ld_stdout_used = true;
7✔
1154
            }
1155

1156
            while ((rc = read(fd, buffer, sizeof(buffer))) > 0) {
14✔
1157
                fwrite(buffer, rc, 1, file);
7✔
1158
            }
1159

1160
            return std::string();
14✔
1161
        });
7✔
1162
    }
1163
    std::error_code errc;
×
1164
    std::filesystem::create_directories(lnav::paths::workdir(), errc);
×
1165
    auto open_temp_res = lnav::filesystem::open_temp_file(lnav::paths::workdir()
×
1166
                                                          / "exec.XXXXXX");
×
1167
    if (open_temp_res.isErr()) {
×
1168
        return lnav::futures::make_ready_future(
1169
            fmt::format(FMT_STRING("error: cannot open temp file -- {}"),
×
1170
                        open_temp_res.unwrapErr()));
×
1171
    }
1172

1173
    auto tmp_pair = open_temp_res.unwrap();
×
1174

1175
    auto reader = std::thread(
1176
        [in_fd = std::move(fd), out_fd = std::move(tmp_pair.second)]() {
×
1177
            char buffer[1024];
1178
            ssize_t rc;
1179

1180
            while ((rc = read(in_fd, buffer, sizeof(buffer))) > 0) {
×
1181
                write(out_fd, buffer, rc);
×
1182
            }
1183
        });
×
1184
    reader.detach();
×
1185

1186
    static int exec_count = 0;
1187
    auto desc
1188
        = fmt::format(FMT_STRING("exec-{}-output {}"), exec_count++, cmdline);
×
1189
    lnav_data.ld_active_files.fc_file_names[tmp_pair.first]
×
1190
        .with_filename(desc)
×
1191
        .with_include_in_session(false)
×
1192
        .with_detect_format(false)
×
1193
        .with_init_location(0_vl);
×
1194
    lnav_data.ld_files_to_front.emplace_back(desc);
×
1195
    prompt.p_editor.set_alt_value(HELP_MSG_1(X, "to close the file"));
×
1196

1197
    return lnav::futures::make_ready_future(std::string());
×
1198
}
1199

1200
void
1201
add_global_vars(exec_context& ec)
662✔
1202
{
1203
    for (const auto& iter : lnav_config.lc_global_vars) {
10,598✔
1204
        shlex subber(iter.second);
9,936✔
1205
        std::string str;
9,936✔
1206

1207
        if (!subber.eval(str, scoped_resolver{&ec.ec_global_vars})) {
9,936✔
1208
            log_error("Unable to evaluate global variable value: %s",
×
1209
                      iter.second.c_str());
1210
            continue;
×
1211
        }
1212

1213
        ec.ec_global_vars[iter.first] = str;
9,936✔
1214
    }
9,936✔
1215
}
662✔
1216

1217
void
1218
exec_context::set_output(const std::string& name,
593✔
1219
                         FILE* file,
1220
                         int (*closer)(FILE*))
1221
{
1222
    log_info("redirecting command output to: %s", name.c_str());
593✔
1223
    this->ec_output_stack.back().od_output | [](auto out) {
593✔
1224
        if (out.second != nullptr) {
×
1225
            out.second(out.first);
×
1226
        }
1227
    };
1228
    this->ec_output_stack.back()
593✔
1229
        = output_desc{name, std::make_pair(file, closer)};
1,186✔
1230
}
593✔
1231

1232
void
1233
exec_context::clear_output()
72✔
1234
{
1235
    log_info("redirecting command output to screen");
72✔
1236
    this->ec_output_stack.back().od_output | [](auto out) {
72✔
1237
        if (out.second != nullptr) {
35✔
1238
            out.second(out.first);
35✔
1239
        }
1240
    };
35✔
1241
    this->ec_output_stack.back().od_name = "default";
72✔
1242
    this->ec_output_stack.back().od_output = std::nullopt;
72✔
1243
}
72✔
1244

1245
exec_context::exec_context(logline_value_vector* line_values,
3,498✔
1246
                           sql_callback_t sql_callback,
1247
                           pipe_callback_t pipe_callback)
3,498✔
1248
    : ec_line_values(line_values),
3,498✔
1249
      ec_accumulator(std::make_unique<attr_line_t>()),
3,498✔
1250
      ec_sql_callback(sql_callback), ec_pipe_callback(pipe_callback)
6,996✔
1251
{
1252
    this->ec_local_vars.push(std::map<std::string, scoped_value_t>());
3,498✔
1253
    this->ec_path_stack.emplace_back(".");
3,498✔
1254
    this->ec_output_stack.emplace_back("screen", std::nullopt);
3,498✔
1255
}
3,498✔
1256

1257
Result<std::string, lnav::console::user_message>
1258
exec_context::execute(source_location loc, const std::string& cmdline)
2✔
1259
{
1260
    static auto& prompt = lnav::prompt::get();
2✔
1261
    static const auto& dls = lnav_data.ld_db_row_source;
1262

1263
    lnav::textinput::history::op_guard hist_guard;
2✔
1264
    auto sg = this->enter_source(loc, cmdline);
2✔
1265

1266
    auto before_dls_gen = dls.dls_generation;
2✔
1267
    if (this->get_provenance<mouse_input>() && !prompt.p_editor.is_enabled()) {
2✔
1268
        auto& hist = prompt.get_history_for(cmdline[0]);
×
1269
        hist_guard = hist.start_operation(cmdline.substr(1));
×
1270
    }
1271

1272
    auto exec_res = execute_any(*this, cmdline);
2✔
1273
    if (exec_res.isErr()) {
2✔
1274
        hist_guard.og_status = log_level_t::LEVEL_ERROR;
×
1275
        if (!this->ec_msg_callback_stack.empty()) {
×
1276
            this->ec_msg_callback_stack.back()(exec_res.unwrapErr());
×
1277
        }
1278
    } else {
1279
        if (before_dls_gen != dls.dls_generation
4✔
1280
            && dls.dls_row_cursors.size() > 1)
2✔
1281
        {
1282
            ensure_view(LNV_DB);
×
1283
        }
1284
    }
1285

1286
    return exec_res;
4✔
1287
}
2✔
1288

1289
void
1290
exec_context::add_error_context(lnav::console::user_message& um)
72✔
1291
{
1292
    switch (um.um_level) {
72✔
1293
        case lnav::console::user_message::level::raw:
×
1294
        case lnav::console::user_message::level::info:
1295
        case lnav::console::user_message::level::ok:
1296
            return;
×
1297
        default:
72✔
1298
            break;
72✔
1299
    }
1300

1301
    if (!this->get_provenance<keyboard_input>() && um.um_snippets.empty()) {
72✔
1302
        um.with_snippets(this->ec_source);
32✔
1303
        um.remove_internal_snippets();
32✔
1304
    }
1305

1306
    if (this->ec_current_help != nullptr && um.um_help.empty()) {
72✔
1307
        attr_line_t help;
32✔
1308

1309
        format_help_text_for_term(*this->ec_current_help,
32✔
1310
                                  70,
1311
                                  help,
1312
                                  help_text_content::synopsis_and_summary);
1313
        um.with_help(help);
32✔
1314
    }
32✔
1315
}
1316

1317
exec_context::sql_callback_guard
1318
exec_context::push_callback(sql_callback_t cb)
5✔
1319
{
1320
    return sql_callback_guard(*this, cb);
5✔
1321
}
1322

1323
exec_context::source_guard
1324
exec_context::enter_source(source_location loc, const std::string& content)
2,420✔
1325
{
1326
    attr_line_t content_al{content};
2,420✔
1327
    content_al.with_attr_for_all(VC_ROLE.value(role_t::VCR_QUOTED_CODE));
2,420✔
1328
    readline_lnav_highlighter(content_al, -1);
2,420✔
1329
    this->ec_source.emplace_back(lnav::console::snippet::from(loc, content_al));
2,420✔
1330
    return {this};
4,840✔
1331
}
2,420✔
1332

1333
exec_context::output_guard::output_guard(exec_context& context,
662✔
1334
                                         std::string name,
1335
                                         const std::optional<output_t>& file,
1336
                                         text_format_t tf)
662✔
1337
    : sg_context(context), sg_active(!name.empty())
662✔
1338
{
1339
    if (name.empty()) {
662✔
1340
        return;
592✔
1341
    }
1342
    if (file) {
70✔
1343
        log_info("redirecting command output to: %s", name.c_str());
33✔
1344
    } else if (!context.ec_output_stack.empty()) {
37✔
1345
        tf = context.ec_output_stack.back().od_format;
37✔
1346
    }
1347
    context.ec_output_stack.emplace_back(std::move(name), file, tf);
70✔
1348
}
1349

1350
exec_context::output_guard::~output_guard()
662✔
1351
{
1352
    if (this->sg_active) {
662✔
1353
        this->sg_context.clear_output();
70✔
1354
        this->sg_context.ec_output_stack.pop_back();
70✔
1355
    }
1356
}
662✔
1357
exec_context::sql_callback_guard::sql_callback_guard(exec_context& context,
5✔
1358
                                                     sql_callback_t cb)
5✔
1359
    : scg_context(context), scg_old_callback(context.ec_sql_callback)
5✔
1360
{
1361
    context.ec_sql_callback = cb;
5✔
1362
}
5✔
1363
exec_context::sql_callback_guard::sql_callback_guard(sql_callback_guard&& other)
×
1364
    : scg_context(other.scg_context),
×
1365
      scg_old_callback(std::exchange(other.scg_old_callback, nullptr))
×
1366
{
1367
}
1368
exec_context::sql_callback_guard::~sql_callback_guard()
5✔
1369
{
1370
    if (this->scg_old_callback != nullptr) {
5✔
1371
        this->scg_context.ec_sql_callback = this->scg_old_callback;
5✔
1372
    }
1373
}
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