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

tstack / lnav / 24959179949-2999

26 Apr 2026 02:37PM UTC coverage: 69.227% (+0.09%) from 69.141%
24959179949-2999

push

github

tstack
[tests] fix paths

53969 of 77959 relevant lines covered (69.23%)

568944.78 hits per line

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

82.96
/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 <set>
31
#include <vector>
32

33
#include "command_executor.hh"
34

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

65
#ifdef HAVE_RUST_DEPS
66
#    include "lnav_rs_ext.cxx.hh"
67
#endif
68

69
using namespace std::literals::chrono_literals;
70
using namespace lnav::roles::literals;
71

72
exec_context INIT_EXEC_CONTEXT;
73

74
static sig_atomic_t sql_counter = 0;
75

76
int
77
sql_progress(const log_cursor& lc)
11,918✔
78
{
79
    if (lnav_data.ld_window == nullptr) {
11,918✔
80
        return 0;
11,907✔
81
    }
82

83
    if (!lnav_data.ld_looping) {
11✔
84
        return 1;
×
85
    }
86

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

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

108
    return 0;
9✔
109
}
110

111
void
112
sql_progress_finished()
1,678✔
113
{
114
    if (sql_counter == 0) {
1,678✔
115
        return;
1,673✔
116
    }
117

118
    sql_counter = 0;
5✔
119

120
    if (lnav_data.ld_window == nullptr) {
5✔
121
        return;
×
122
    }
123

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

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

140
Result<std::string, lnav::console::user_message>
141
execute_command(exec_context& ec, const std::string& cmdline)
897✔
142
{
143
    static auto op = lnav_operation{__FUNCTION__};
897✔
144

145
    auto op_guard = lnav_opid_guard::internal(op);
897✔
146
    std::vector<std::string> args;
897✔
147

148
    log_info("Executing command: %s", cmdline.c_str());
897✔
149

150
    split_ws(cmdline, args);
897✔
151

152
    if (!args.empty()) {
897✔
153
        readline_context::command_map_t::iterator iter;
897✔
154

155
        if ((iter = lnav_commands.find(args[0])) == lnav_commands.end()) {
897✔
156
            auto cmd_names
157
                = lnav_commands | lnav::itertools::map([](const auto& pair) {
1✔
158
                      return pair.first.to_string();
115✔
159
                  });
1✔
160
            auto similar = cmd_names | lnav::itertools::similar_to(args[0], 10)
2✔
161
                | lnav::itertools::sorted();
2✔
162
            auto um = lnav::console::user_message::error(
163
                attr_line_t("unknown command: ").append_quoted(args[0]));
1✔
164
            if (!similar.empty()) {
1✔
165
                auto note = attr_line_t("did you mean one of the following:\n");
1✔
166
                for (const auto& name : similar) {
11✔
167
                    auto cmd_iter = lnav_commands.find(name);
10✔
168
                    note.append("  ").append(lnav::roles::symbol(name));
10✔
169
                    if (cmd_iter != lnav_commands.end()
10✔
170
                        && cmd_iter->second->c_help.ht_summary[0])
10✔
171
                    {
172
                        note.append(" - ").append(
10✔
173
                            cmd_iter->second->c_help.ht_summary);
10✔
174
                    }
175
                    note.append("\n");
10✔
176
                }
177
                um.with_note(note);
1✔
178
            }
1✔
179
            return Err(um);
1✔
180
        }
1✔
181

182
        ec.ec_current_help = &iter->second->c_help;
896✔
183
        try {
184
            auto retval = iter->second->c_func(ec, cmdline, args);
896✔
185
            if (retval.isErr()) {
896✔
186
                auto um = retval.unwrapErr();
53✔
187

188
                ec.add_error_context(um);
53✔
189
                ec.ec_current_help = nullptr;
53✔
190
                return Err(um);
53✔
191
            }
53✔
192
            ec.ec_current_help = nullptr;
843✔
193

194
            return retval;
843✔
195
        } catch (const std::exception& e) {
896✔
196
            auto um = lnav::console::user_message::error(
×
197
                          attr_line_t("unexpected error while executing: ")
×
198
                              .append(lnav::roles::quoted_code(cmdline)))
×
199
                          .with_reason(e.what());
×
200
            return Err(um);
×
201
        }
×
202
    }
203

204
    return ec.make_error("no command to execute");
×
205
}
897✔
206

207
static Result<std::map<std::string, scoped_value_t>,
208
              lnav::console::user_message>
209
bind_sql_parameters(exec_context& ec, sqlite3_stmt* stmt)
1,701✔
210
{
211
    std::map<std::string, scoped_value_t> retval;
1,701✔
212
    auto param_count = sqlite3_bind_parameter_count(stmt);
1,701✔
213
    for (int lpc = 0; lpc < param_count; lpc++) {
1,873✔
214
        const auto* name = sqlite3_bind_parameter_name(stmt, lpc + 1);
173✔
215
        if (name == nullptr) {
173✔
216
            auto um
217
                = lnav::console::user_message::error("invalid SQL statement")
2✔
218
                      .with_reason(
2✔
219
                          "using a question-mark (?) for bound variables "
220
                          "is not supported, only named bound parameters "
221
                          "are supported")
222
                      .with_help(
2✔
223
                          "named parameters start with a dollar-sign "
224
                          "($) or colon (:) followed by the variable name")
225
                      .move();
1✔
226
            ec.add_error_context(um);
1✔
227

228
            return Err(um);
1✔
229
        }
1✔
230

231
        if (name[0] == '$') {
172✔
232
            const auto& lvars = ec.ec_local_vars.top();
145✔
233
            const auto& gvars = ec.ec_global_vars;
145✔
234
            std::map<std::string, scoped_value_t>::const_iterator local_var,
145✔
235
                global_var;
145✔
236
            const char* env_value;
237

238
            if (lnav_data.ld_window) {
145✔
239
                char buf[32];
240
                unsigned int lines, cols;
241

242
                ncplane_dim_yx(lnav_data.ld_window, &lines, &cols);
12✔
243
                if (strcmp(name, "$LINES") == 0) {
12✔
244
                    snprintf(buf, sizeof(buf), "%d", lines);
×
245
                    sqlite3_bind_text(stmt, lpc + 1, buf, -1, SQLITE_TRANSIENT);
×
246
                } else if (strcmp(name, "$COLS") == 0) {
12✔
247
                    snprintf(buf, sizeof(buf), "%d", cols);
×
248
                    sqlite3_bind_text(stmt, lpc + 1, buf, -1, SQLITE_TRANSIENT);
×
249
                }
250
            }
251

252
            if ((local_var = lvars.find(&name[1])) != lvars.end()) {
435✔
253
                mapbox::util::apply_visitor(
126✔
254
                    sqlitepp::bind_visitor(stmt, lpc + 1), local_var->second);
126✔
255
                retval[name] = local_var->second;
378✔
256
            } else if ((global_var = gvars.find(&name[1])) != gvars.end()) {
57✔
257
                mapbox::util::apply_visitor(
6✔
258
                    sqlitepp::bind_visitor(stmt, lpc + 1), global_var->second);
6✔
259
                retval[name] = global_var->second;
18✔
260
            } else if ((env_value = getenv(&name[1])) != nullptr) {
13✔
261
                sqlite3_bind_text(stmt, lpc + 1, env_value, -1, SQLITE_STATIC);
3✔
262
                retval[name] = string_fragment::from_c_str(env_value);
9✔
263
            }
264
        } else if (name[0] == ':' && ec.ec_line_values != nullptr) {
27✔
265
            for (auto& lv : ec.ec_line_values->lvv_values) {
149✔
266
                if (lv.lv_meta.lvm_name != &name[1]) {
122✔
267
                    continue;
95✔
268
                }
269
                switch (lv.lv_meta.lvm_kind) {
27✔
270
                    case value_kind_t::VALUE_BOOLEAN:
×
271
                        sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
272
                        retval[name] = fmt::to_string(lv.lv_value.i);
×
273
                        break;
×
274
                    case value_kind_t::VALUE_FLOAT:
×
275
                        sqlite3_bind_double(stmt, lpc + 1, lv.lv_value.d);
×
276
                        retval[name] = fmt::to_string(lv.lv_value.d);
×
277
                        break;
×
278
                    case value_kind_t::VALUE_INTEGER:
×
279
                        sqlite3_bind_int64(stmt, lpc + 1, lv.lv_value.i);
×
280
                        retval[name] = fmt::to_string(lv.lv_value.i);
×
281
                        break;
×
282
                    case value_kind_t::VALUE_NULL:
×
283
                        sqlite3_bind_null(stmt, lpc + 1);
×
284
                        retval[name] = string_fragment::from_c_str(
×
285
                            db_label_source::NULL_STR);
×
286
                        break;
×
287
                    default:
27✔
288
                        sqlite3_bind_text(stmt,
27✔
289
                                          lpc + 1,
290
                                          lv.text_value(),
291
                                          lv.text_length(),
27✔
292
                                          SQLITE_TRANSIENT);
293
                        retval[name] = lv.to_string();
81✔
294
                        break;
27✔
295
                }
296
            }
297
        } else {
27✔
298
            sqlite3_bind_null(stmt, lpc + 1);
×
299
            log_warning("Could not bind variable: %s", name);
×
300
            retval[name]
×
301
                = string_fragment::from_c_str(db_label_source::NULL_STR);
×
302
        }
303
    }
304

305
    return Ok(retval);
1,700✔
306
}
1,701✔
307

308
static void
309
execute_search(const std::string& search_cmd)
7✔
310
{
311
    textview_curses* tc = get_textview_for_mode(lnav_data.ld_mode);
7✔
312
    auto search_term = string_fragment(search_cmd)
7✔
313
                           .find_right_boundary(0, string_fragment::tag1{'\n'})
7✔
314
                           .to_string();
7✔
315
    tc->execute_search(search_term);
7✔
316
}
7✔
317

318
Result<std::string, lnav::console::user_message>
319
execute_sql(exec_context& ec, const std::string& sql, std::string& alt_msg)
1,716✔
320
{
321
    static auto op = lnav_operation{__FUNCTION__};
1,716✔
322

323
    auto op_guard = lnav_opid_guard::internal(op);
1,716✔
324
    auto_mem<sqlite3_stmt> stmt(sqlite3_finalize);
1,716✔
325
    timeval start_tv, end_tv;
326
    std::string stmt_str = trim(sql);
1,716✔
327
    std::string retval;
1,716✔
328
    int retcode = SQLITE_OK;
1,716✔
329

330
    if (lnav::sql::is_prql(stmt_str)) {
1,716✔
331
        log_info("compiling PRQL: %s", stmt_str.c_str());
45✔
332

333
#if HAVE_RUST_DEPS
334
        extern rust::Vec<lnav_rs_ext::SourceTreeElement> sqlite_extension_prql;
335
        auto opts = lnav_rs_ext::Options{true, "sql.sqlite", true};
45✔
336

337
        auto tree = sqlite_extension_prql;
45✔
338
        for (const auto& mod : lnav_prql_modules) {
135✔
339
            auto name = mod.get_name().to_string();
90✔
340
            log_debug("lnav_rs_ext adding mod %s", name.c_str());
90✔
341
            tree.emplace_back(lnav_rs_ext::SourceTreeElement{
180✔
342
                name.c_str(),
343
                mod.to_string_fragment_producer()->to_string(),
180✔
344
            });
345
        }
90✔
346
        tree.emplace_back(lnav_rs_ext::SourceTreeElement{"", stmt_str});
45✔
347
        log_debug("BEGIN compiling tree");
45✔
348
        auto cr = lnav_rs_ext::compile_tree(tree, opts);
45✔
349
        log_debug("END compiling tree");
45✔
350

351
        for (const auto& msg : cr.messages) {
45✔
352
            if (msg.kind != lnav_rs_ext::MessageKind::Error) {
1✔
353
                continue;
×
354
            }
355

356
            auto stmt_al = attr_line_t(stmt_str);
1✔
357
            readline_sql_highlighter(stmt_al, lnav::sql::dialect::prql, 0);
1✔
358
            auto um
359
                = lnav::console::user_message::error(
×
360
                      attr_line_t("unable to compile PRQL: ").append(stmt_al))
2✔
361
                      .with_reason(
1✔
362
                          attr_line_t::from_ansi_str((std::string) msg.reason));
2✔
363
            if (!msg.display.empty()) {
1✔
364
                um.with_note(
1✔
365
                    attr_line_t::from_ansi_str((std::string) msg.display));
2✔
366
            }
367
            for (const auto& hint : msg.hints) {
1✔
368
                um.with_help(attr_line_t::from_ansi_str((std::string) hint));
×
369
                break;
×
370
            }
371
            return Err(um);
1✔
372
        }
1✔
373
        log_debug("done!");
44✔
374
        stmt_str = (std::string) cr.output;
44✔
375
#else
376
        auto um = lnav::console::user_message::error(
377
            attr_line_t("PRQL is not supported in this build"));
378
        return Err(um);
379
#endif
380
    }
47✔
381

382
    log_info("Executing SQL: %s", stmt_str.c_str());
1,715✔
383

384
    auto old_mode = lnav_data.ld_mode;
1,715✔
385
    lnav_data.ld_mode = ln_mode_t::BUSY;
1,715✔
386
    auto mode_fin = finally([old_mode]() { lnav_data.ld_mode = old_mode; });
1,715✔
387
    lnav_data.ld_bottom_source.grep_error("");
1,715✔
388
    lnav_data.ld_status[LNS_BOTTOM].set_needs_update();
1,715✔
389

390
    if (startswith(stmt_str, ".")) {
1,715✔
391
        std::vector<std::string> args;
7✔
392
        split_ws(stmt_str, args);
7✔
393

394
        const auto* sql_cmd_map
395
            = injector::get<readline_context::command_map_t*,
396
                            sql_cmd_map_tag>();
7✔
397
        auto cmd_iter = sql_cmd_map->find(args[0]);
7✔
398

399
        if (cmd_iter != sql_cmd_map->end()) {
7✔
400
            ec.ec_current_help = &cmd_iter->second->c_help;
7✔
401
            auto retval = cmd_iter->second->c_func(ec, stmt_str, args);
7✔
402
            ec.ec_current_help = nullptr;
7✔
403

404
            return retval;
7✔
405
        }
7✔
406
    }
7✔
407

408
    ec.ec_accumulator->clear();
1,708✔
409

410
    require(!ec.ec_source.empty());
1,708✔
411
    const auto& source = ec.ec_source.back();
1,708✔
412
    sql_progress_guard progress_guard(
413
        sql_progress,
414
        sql_progress_finished,
415
        source.s_location,
416
        source.s_content,
1,708✔
417
        !ec.get_provenance<exec_context::format_rewrite>().has_value());
1,708✔
418
    gettimeofday(&start_tv, nullptr);
1,708✔
419

420
    const auto* curr_stmt = stmt_str.c_str();
1,708✔
421
    auto last_is_readonly = false;
1,708✔
422
    while (curr_stmt != nullptr) {
3,347✔
423
        const char* tail = nullptr;
3,347✔
424
        while (isspace(*curr_stmt)) {
3,347✔
425
            curr_stmt += 1;
×
426
        }
427
        std::set<std::string> touched_tables;
3,347✔
428
        {
429
            sql_table_capture_guard capture_guard(touched_tables);
3,347✔
430
            retcode = sqlite3_prepare_v2(
3,347✔
431
                lnav_data.ld_db.in(), curr_stmt, -1, stmt.out(), &tail);
432
        }
3,347✔
433
        if (retcode != SQLITE_OK) {
3,347✔
434
            const char* errmsg = sqlite3_errmsg(lnav_data.ld_db);
7✔
435

436
            alt_msg = "";
7✔
437

438
            auto um = lnav::console::user_message::error(
14✔
439
                          "failed to compile SQL statement")
440
                          .with_reason(errmsg)
14✔
441
                          .with_snippets(ec.ec_source)
14✔
442
                          .move();
7✔
443

444
            auto annotated_sql = annotate_sql_with_error(
445
                lnav_data.ld_db.in(), curr_stmt, tail);
7✔
446
            auto loc = um.um_snippets.back().s_location;
7✔
447
            if (curr_stmt == stmt_str.c_str()) {
7✔
448
                um.um_snippets.pop_back();
7✔
449
            } else {
450
                auto tail_iter = stmt_str.begin();
×
451

452
                std::advance(tail_iter, (curr_stmt - stmt_str.c_str()));
×
453
                loc.sl_line_number
×
454
                    += std::count(stmt_str.begin(), tail_iter, '\n');
×
455
            }
456

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

459
            return Err(um);
7✔
460
        }
7✔
461
        if (stmt == nullptr) {
3,340✔
462
            retcode = SQLITE_DONE;
1,639✔
463
            break;
1,639✔
464
        }
465
#ifdef HAVE_SQLITE3_STMT_READONLY
466
        last_is_readonly = sqlite3_stmt_readonly(stmt.in());
1,701✔
467
        if (ec.is_read_only() && !last_is_readonly) {
1,701✔
468
            return ec.make_error(
469
                "modifying statements are not allowed in this context: {}",
470
                sql);
×
471
        }
472
#endif
473
        bool done = false;
1,701✔
474

475
        auto bound_values = TRY(bind_sql_parameters(ec, stmt.in()));
1,701✔
476
        if (last_is_readonly) {
1,700✔
477
            ec.ec_sql_callback(ec, stmt.in());
1,549✔
478
            if (!ec.ec_label_source_stack.empty()) {
1,549✔
479
                auto& dls = *ec.ec_label_source_stack.back();
1,549✔
480
                dls.dls_user_query = sql;
1,549✔
481
                if (!ec.ec_source.empty()) {
1,549✔
482
                    dls.dls_user_query_src_loc
483
                        = ec.ec_source.back().s_location;
1,549✔
484
                } else {
485
                    dls.dls_user_query_src_loc = std::nullopt;
×
486
                }
487
                if (!ec.ec_local_vars.empty()) {
1,549✔
488
                    dls.dls_user_query_vars = ec.ec_local_vars.top();
1,549✔
489
                } else {
490
                    dls.dls_user_query_vars.clear();
×
491
                }
492
                dls.dls_query_start = std::chrono::system_clock::now();
1,549✔
493
                auto* vtab_mgr = injector::get<log_vtab_manager*>();
1,549✔
494
                dls.dls_query_touches_log_data
495
                    = vtab_mgr->has_log_backed_table(touched_tables);
1,549✔
496
            }
497
        }
498
        while (!done) {
6,144✔
499
            retcode = sqlite3_step(stmt.in());
4,505✔
500

501
            if (!ec.ec_label_source_stack.empty()) {
4,505✔
502
                ec.ec_label_source_stack.back()->dls_step_rc = retcode;
4,505✔
503
            }
504

505
            switch (retcode) {
4,505✔
506
                case SQLITE_OK:
1,639✔
507
                case SQLITE_DONE: {
508
                    auto changes = sqlite3_changes(lnav_data.ld_db.in());
1,639✔
509

510
                    log_info("sqlite3_changes() -> %d", changes);
1,639✔
511
                    done = true;
1,639✔
512
                    break;
1,639✔
513
                }
514

515
                case SQLITE_ROW:
2,805✔
516
                    ec.ec_sql_callback(ec, stmt.in());
2,805✔
517
                    break;
2,805✔
518

519
                default: {
61✔
520
                    attr_line_t bound_note;
61✔
521

522
                    if (!bound_values.empty()) {
61✔
523
                        bound_note.append(
3✔
524
                            "the bound parameters are set as follows:\n");
525
                        for (const auto& bval : bound_values) {
11✔
526
                            auto val_as_str = fmt::to_string(bval.second);
8✔
527
                            auto sql_type = bval.second.match(
8✔
528
                                [](const std::string&) { return SQLITE_TEXT; },
5✔
529
                                [](const string_fragment&) {
×
530
                                    return SQLITE_TEXT;
×
531
                                },
532
                                [](bool) { return SQLITE_INTEGER; },
×
533
                                [](int64_t) { return SQLITE_INTEGER; },
1✔
534
                                [](null_value_t) { return SQLITE_NULL; },
1✔
535
                                [](double) { return SQLITE_FLOAT; });
1✔
536
                            auto scrubbed_val = scrub_ws(val_as_str.c_str());
8✔
537
                            truncate_to(scrubbed_val, 40);
8✔
538
                            bound_note.append("  ")
8✔
539
                                .append(lnav::roles::variable(bval.first))
16✔
540
                                .append(":")
8✔
541
                                .append(sqlite3_type_to_string(sql_type))
8✔
542
                                .append(" = ")
8✔
543
                                .append_quoted(scrubbed_val)
16✔
544
                                .append("\n");
8✔
545
                        }
8✔
546
                    }
547

548
                    log_error("sqlite3_step error code: %d", retcode);
61✔
549
                    auto um = sqlite3_error_to_user_message(lnav_data.ld_db)
122✔
550
                                  .with_note(bound_note)
61✔
551
                                  .move();
61✔
552

553
                    if (ec.get_provenance<exec_context::keyboard_input>()
61✔
554
                        || ec.get_provenance<exec_context::mouse_input>())
61✔
555
                    {
556
                        auto um_copy = um;
2✔
557
                        um_copy.with_context_snippets(ec.ec_source);
2✔
558
                        auto err_al = um_copy.to_attr_line();
2✔
559
                        log_error("SQL error: %s", err_al.al_string.c_str());
2✔
560
                    } else {
2✔
561
                        um.with_context_snippets(ec.ec_source)
118✔
562
                            .remove_internal_snippets();
59✔
563
                    }
564

565
                    if (last_is_readonly && !ec.ec_label_source_stack.empty())
61✔
566
                    {
567
                        auto& dls = *ec.ec_label_source_stack.back();
27✔
568
                        dls.dls_query_end = std::chrono::system_clock::now();
27✔
569
                        dls.dls_log_gen_at_query
570
                            = lnav_data.ld_log_source.lss_index_generation;
27✔
571
                        dls.dls_log_line_count_at_query
572
                            = lnav_data.ld_log_source.text_line_count();
27✔
573
                        if (&dls == &lnav_data.ld_db_row_source
27✔
574
                            && lnav_data.ld_db_status_source
42✔
575
                                   .update_from_db_source())
15✔
576
                        {
577
                            lnav_data.ld_status[LNS_DB].set_needs_update();
15✔
578
                        }
579
                    }
580

581
                    return Err(um);
61✔
582
                }
61✔
583
            }
584
        }
585

586
        curr_stmt = tail;
1,639✔
587
    }
3,408✔
588

589
    if (last_is_readonly && !ec.ec_label_source_stack.empty()) {
1,639✔
590
        auto& dls = *ec.ec_label_source_stack.back();
1,522✔
591
        dls.dls_query_end = std::chrono::system_clock::now();
1,522✔
592
        dls.dls_log_gen_at_query
593
            = lnav_data.ld_log_source.lss_index_generation;
1,522✔
594
        dls.dls_log_line_count_at_query
595
            = lnav_data.ld_log_source.text_line_count();
1,522✔
596
        if (&dls == &lnav_data.ld_db_row_source
1,522✔
597
            && lnav_data.ld_db_status_source.update_from_db_source())
1,522✔
598
        {
599
            lnav_data.ld_status[LNS_DB].set_needs_update();
338✔
600
        }
601

602
        size_t memory_usage = 0, total_size = 0, cached_chunks = 0;
1,522✔
603
        for (auto cc = dls.dls_cell_container.cc_first.get(); cc != nullptr;
3,045✔
604
             cc = cc->cc_next.get())
1,523✔
605
        {
606
            total_size += cc->cc_capacity;
1,523✔
607
            if (cc->cc_data) {
1,523✔
608
                cached_chunks += 1;
1,522✔
609
                memory_usage += cc->cc_capacity;
1,522✔
610
            } else {
611
                memory_usage += cc->cc_compressed_size;
1✔
612
            }
613
        }
614
        log_debug(
1,522✔
615
            "cell memory footprint: rows=%zu total=%zu; actual=%zu; "
616
            "cached-chunks=%zu",
617
            dls.dls_row_cursors.size(),
618
            total_size,
619
            memory_usage,
620
            cached_chunks);
621
    }
622
    gettimeofday(&end_tv, nullptr);
1,639✔
623
    lnav_data.ld_mode = old_mode;
1,639✔
624
    if (retcode == SQLITE_DONE) {
1,639✔
625
        if (lnav_data.ld_log_source.is_line_meta_changed()) {
1,639✔
626
            lnav_data.ld_log_source.text_filters_changed();
20✔
627
            lnav_data.ld_views[LNV_LOG].reload_data();
20✔
628
        }
629
        lnav_data.ld_filter_view.reload_data();
1,639✔
630
        lnav_data.ld_files_view.reload_data();
1,639✔
631

632
        lnav_data.ld_active_files.fc_files
633
            | lnav::itertools::for_each(&logfile::dump_stats);
1,639✔
634
        if (ec.ec_sql_callback != sql_callback) {
1,639✔
635
            retval = ec.ec_accumulator->get_string();
46✔
636
        } else {
637
            auto& dls = *(ec.ec_label_source_stack.back());
1,593✔
638
            if (!dls.dls_row_cursors.empty()) {
1,593✔
639
                lnav_data.ld_views[LNV_DB].reload_data();
1,462✔
640
                if (lnav_data.ld_flags.is_set<lnav_flags::headless>()) {
1,462✔
641
                    retval = "";
974✔
642
                    alt_msg = "";
974✔
643
                } else if (dls.dls_row_cursors.size() == 1) {
488✔
644
                    retval = dls.get_row_as_string(0_vl);
438✔
645
                } else {
646
                    int row_count = dls.dls_row_cursors.size();
50✔
647
                    char row_count_buf[128];
648
                    timeval diff_tv;
649

650
                    timersub(&end_tv, &start_tv, &diff_tv);
50✔
651
                    snprintf(row_count_buf,
50✔
652
                             sizeof(row_count_buf),
653
                             ANSI_BOLD("%'d") " row%s matched in " ANSI_BOLD(
654
                                 "%ld.%03ld") " seconds",
655
                             row_count,
656
                             row_count == 1 ? "" : "s",
657
                             diff_tv.tv_sec,
658
                             std::max((long) diff_tv.tv_usec / 1000, 1L));
50✔
659
                    retval = row_count_buf;
50✔
660
                    if (dls.has_log_time_column()) {
50✔
661
                        alt_msg
662
                            = HELP_MSG_1(Q,
2✔
663
                                         "to switch back to the previous view "
664
                                         "at the matching 'log_time' value");
665
                    } else {
666
                        alt_msg = "";
48✔
667
                    }
668
                }
669
            }
670
#ifdef HAVE_SQLITE3_STMT_READONLY
671
            else if (last_is_readonly)
131✔
672
            {
673
                retval = "info: No rows matched";
26✔
674
                alt_msg = "";
26✔
675
            }
676
#endif
677
        }
678
    }
679

680
    return Ok(retval);
1,639✔
681
}
1,716✔
682

683
Result<void, lnav::console::user_message>
684
multiline_executor::handle_command(const std::string& cmdline)
234✔
685
{
686
    this->me_last_result = TRY(execute_from_file(this->me_exec_context,
234✔
687
                                                 this->p_source,
688
                                                 this->p_starting_line_number,
689
                                                 cmdline));
690

691
    return Ok();
228✔
692
}
693

694
static Result<std::string, lnav::console::user_message>
695
execute_file_contents(exec_context& ec, const std::filesystem::path& path)
38✔
696
{
697
    static const std::filesystem::path stdin_path("-");
38✔
698
    static const std::filesystem::path dev_stdin_path("/dev/stdin");
38✔
699

700
    FILE* file;
701

702
    if (path == stdin_path || path == dev_stdin_path) {
38✔
703
        if (isatty(STDIN_FILENO)) {
3✔
704
            return ec.make_error("stdin has already been consumed");
×
705
        }
706
        file = stdin;
3✔
707
    } else if ((file = fopen(path.c_str(), "re")) == nullptr) {
35✔
708
        return ec.make_error("unable to open file");
×
709
    }
710

711
    std::string out_name;
38✔
712

713
    auto_mem<char> line;
38✔
714
    size_t line_max_size;
715
    ssize_t line_size;
716
    multiline_executor me(ec, path.string());
38✔
717

718
    ec.ec_local_vars.top()["0"] = path.string();
114✔
719
    ec.ec_path_stack.emplace_back(path.parent_path());
38✔
720
    exec_context::output_guard og(ec);
76✔
721
    while ((line_size = getline(line.out(), &line_max_size, file)) != -1) {
878✔
722
        TRY(me.push_back(string_fragment::from_bytes(line.in(), line_size)));
846✔
723
    }
724

725
    TRY(me.final());
32✔
726
    auto retval = std::move(me.me_last_result);
32✔
727

728
    if (file == stdin) {
32✔
729
        if (isatty(STDOUT_FILENO)) {
3✔
730
            log_perror(dup2(STDOUT_FILENO, STDIN_FILENO));
×
731
        }
732
    } else {
733
        fclose(file);
29✔
734
    }
735
    ec.ec_path_stack.pop_back();
32✔
736

737
    return Ok(retval);
32✔
738
}
38✔
739

740
Result<std::string, lnav::console::user_message>
741
execute_file(exec_context& ec, const std::string& path_and_args)
41✔
742
{
743
    static const intern_string_t SRC = intern_string::lookup("cmdline");
95✔
744
    static auto op = lnav_operation{__FUNCTION__};
41✔
745

746
    auto op_guard = lnav_opid_guard::internal(op);
41✔
747

748
    std::string retval, msg;
41✔
749
    shlex lexer(path_and_args);
41✔
750

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

753
    auto split_args_res = lexer.split(scoped_resolver{&ec.ec_local_vars.top()});
41✔
754
    if (split_args_res.isErr()) {
41✔
755
        auto split_err = split_args_res.unwrapErr();
×
756
        auto um = lnav::console::user_message::error(
×
757
                      "unable to parse script command-line")
758
                      .with_reason(split_err.se_error.te_msg)
×
759
                      .with_snippet(lnav::console::snippet::from(
×
760
                          SRC, lexer.to_attr_line(split_err.se_error)))
×
761
                      .move();
×
762

763
        return Err(um);
×
764
    }
765
    auto split_args = split_args_res.unwrap();
41✔
766
    if (split_args.empty()) {
41✔
767
        return ec.make_error("no script specified");
×
768
    }
769

770
    ec.ec_local_vars.emplace();
41✔
771

772
    auto script_name = split_args[0].se_value;
41✔
773
    auto& vars = ec.ec_local_vars.top();
41✔
774
    std::string star, open_error = "file not found";
82✔
775

776
    add_ansi_vars(vars);
41✔
777

778
    vars["#"] = fmt::to_string(split_args.size() - 1);
123✔
779
    for (size_t lpc = 0; lpc < split_args.size(); lpc++) {
124✔
780
        vars[fmt::to_string(lpc)] = split_args[lpc].se_value;
83✔
781
    }
782
    for (size_t lpc = 1; lpc < split_args.size(); lpc++) {
83✔
783
        if (lpc > 1) {
42✔
784
            star.append(" ");
21✔
785
        }
786
        star.append(split_args[lpc].se_value);
42✔
787
    }
788
    vars["__all__"] = star;
41✔
789

790
    std::vector<script_metadata> paths_to_exec;
41✔
791

792
    auto scripts = find_format_scripts(lnav_data.ld_config_paths);
41✔
793
    auto iter = scripts.as_scripts.find(script_name);
41✔
794
    if (iter != scripts.as_scripts.end()) {
41✔
795
        paths_to_exec = iter->second;
28✔
796
    }
797
    if (lnav::filesystem::is_url(script_name)) {
41✔
798
        auto_mem<CURLU> cu(curl_url_cleanup);
×
799
        cu = curl_url();
×
800
        auto set_rc = curl_url_set(cu, CURLUPART_URL, script_name.c_str(), 0);
×
801
        if (set_rc == CURLUE_OK) {
×
802
            auto_mem<char> scheme_part(curl_free);
×
803
            auto get_rc
804
                = curl_url_get(cu, CURLUPART_SCHEME, scheme_part.out(), 0);
×
805
            if (get_rc == CURLUE_OK
×
806
                && string_fragment::from_c_str(scheme_part.in()) == "file")
×
807
            {
808
                auto_mem<char> path_part;
×
809
                auto get_rc
810
                    = curl_url_get(cu, CURLUPART_PATH, path_part.out(), 0);
×
811
                if (get_rc == CURLUE_OK) {
×
812
                    auto rp_res = lnav::filesystem::realpath(path_part.in());
×
813
                    if (rp_res.isOk()) {
×
814
                        struct script_metadata meta;
×
815

816
                        meta.sm_path = rp_res.unwrap();
×
817
                        extract_metadata_from_file(meta);
×
818
                        paths_to_exec.push_back(meta);
×
819
                    }
820
                }
821
            }
822
        }
823
    }
824
    if (script_name == "-" || script_name == "/dev/stdin") {
41✔
825
        paths_to_exec.push_back({script_name, "", "", ""});
3✔
826
    } else if (access(script_name.c_str(), R_OK) == 0) {
38✔
827
        struct script_metadata meta;
7✔
828
        auto rp_res = lnav::filesystem::realpath(script_name);
7✔
829

830
        if (rp_res.isErr()) {
7✔
831
            log_error("unable to get realpath() of %s -- %s",
×
832
                      script_name.c_str(),
833
                      rp_res.unwrapErr().c_str());
834
            meta.sm_path = script_name;
×
835
        } else {
836
            meta.sm_path = rp_res.unwrap();
7✔
837
        }
838
        extract_metadata_from_file(meta);
7✔
839
        paths_to_exec.push_back(meta);
7✔
840
    } else if (errno != ENOENT) {
38✔
841
        open_error = lnav::from_errno().message();
×
842
    } else {
843
        auto script_path = std::filesystem::path(script_name);
31✔
844

845
        if (!script_path.is_absolute()) {
31✔
846
            script_path = ec.ec_path_stack.back() / script_path;
31✔
847
        }
848

849
        if (std::filesystem::is_regular_file(script_path)) {
31✔
850
            script_metadata meta;
×
851

852
            meta.sm_path = script_path;
×
853
            extract_metadata_from_file(meta);
×
854
            paths_to_exec.push_back(meta);
×
855
        } else if (errno != ENOENT) {
31✔
856
            open_error = lnav::from_errno().message();
×
857
        }
858
    }
31✔
859

860
    if (!paths_to_exec.empty()) {
41✔
861
        for (auto& path_iter : paths_to_exec) {
70✔
862
            if (!ec.ec_output_stack.empty()) {
38✔
863
                ec.ec_output_stack.back().od_format
38✔
864
                    = path_iter.sm_output_format;
38✔
865
                log_info("%s: setting out format for '%s' to %d",
38✔
866
                         script_name.c_str(),
867
                         ec.ec_output_stack.back().od_name.c_str(),
868
                         ec.ec_output_stack.back().od_format);
869
            }
870
            retval = TRY(execute_file_contents(ec, path_iter.sm_path));
38✔
871
        }
872
    }
873
    ec.ec_local_vars.pop();
35✔
874

875
    if (paths_to_exec.empty()) {
35✔
876
        return ec.make_error(
877
            "unknown script -- {} -- {}", script_name, open_error);
3✔
878
    }
879

880
    return Ok(retval);
32✔
881
}
56✔
882

883
Result<std::string, lnav::console::user_message>
884
execute_from_file(exec_context& ec,
234✔
885
                  const std::string& src,
886
                  int line_number,
887
                  const std::string& cmdline)
888
{
889
    std::string retval, alt_msg;
234✔
890
    auto _sg
891
        = ec.enter_source(intern_string::lookup(src), line_number, cmdline);
234✔
892

893
    lnav_data.ld_view_stack.top() | [&ec](auto* tc) {
234✔
894
        ec.ec_top_line = tc->get_selection().value_or(0_vl);
230✔
895
    };
230✔
896
    switch (cmdline[0]) {
234✔
897
        case ':':
103✔
898
            retval = TRY(execute_command(ec, cmdline.substr(1)));
103✔
899
            break;
103✔
900
        case '/':
1✔
901
            execute_search(cmdline.substr(1));
1✔
902
            break;
1✔
903
        case ';': {
128✔
904
            if (!ec.ec_msg_callback_stack.empty()) {
128✔
905
                auto src_slash = src.rfind('/');
124✔
906
                auto cmd_al
907
                    = attr_line_t(cmdline.substr(0, cmdline.find('\n')));
124✔
908
                readline_lnav_highlighter(cmd_al, std::nullopt);
124✔
909
                const auto um = lnav::console::user_message::info(
124✔
910
                                    attr_line_t("Executing command at ")
124✔
911
                                        .append(lnav::roles::file(
124✔
912
                                            src_slash != std::string::npos
913
                                                ? src.substr(src_slash + 1)
248✔
914
                                                : src))
915
                                        .append(":")
124✔
916
                                        .append(lnav::roles::number(
248✔
917
                                            fmt::to_string(line_number)))
248✔
918
                                        .append(" \u2014 ")
124✔
919
                                        .append(cmd_al))
124✔
920
                                    .move();
124✔
921
                ec.ec_msg_callback_stack.back()(um);
124✔
922
            }
124✔
923

924
            setup_logline_table(ec);
128✔
925
            retval = TRY(execute_sql(ec, cmdline.substr(1), alt_msg));
128✔
926
            break;
122✔
927
        }
928
        case '|':
2✔
929
            retval = TRY(execute_file(ec, cmdline.substr(1)));
2✔
930
            break;
2✔
931
        default:
×
932
            retval = TRY(execute_command(ec, cmdline));
×
933
            break;
×
934
    }
935

936
    log_info(
228✔
937
        "%s:%d:execute result -- %s", src.c_str(), line_number, retval.c_str());
938

939
    return Ok(retval);
228✔
940
}
234✔
941

942
Result<std::string, lnav::console::user_message>
943
execute_any(exec_context& ec, const std::string& cmdline_with_mode)
35✔
944
{
945
    if (cmdline_with_mode.empty()) {
35✔
946
        auto um = lnav::console::user_message::error("empty command")
×
947
                      .with_help(
×
948
                          "a command should start with ':', ';', '/', '|' and "
949
                          "followed by the operation to perform")
950
                      .move();
×
951
        if (!ec.ec_source.empty()) {
×
952
            um.with_snippet(ec.ec_source.back());
×
953
        }
954
        return Err(um);
×
955
    }
956

957
    std::string retval, alt_msg, cmdline = cmdline_with_mode.substr(1);
35✔
958
    auto _cleanup = finally([&ec] {
35✔
959
        if (ec.is_read_write() &&
38✔
960
            // only rebuild in a script or non-interactive mode so we don't
961
            // block the UI.
962
            lnav_data.ld_flags.is_set<lnav_flags::headless>())
3✔
963
        {
964
            rescan_files();
×
965
            wait_for_pipers(std::nullopt);
×
966
            rebuild_indexes_repeatedly();
×
967
        }
968
    });
70✔
969

970
    switch (cmdline_with_mode[0]) {
35✔
971
        case ':':
×
972
            retval = TRY(execute_command(ec, cmdline));
×
973
            break;
×
974
        case '/':
×
975
            execute_search(cmdline);
×
976
            break;
×
977
        case ';':
29✔
978
            setup_logline_table(ec);
29✔
979
            retval = TRY(execute_sql(ec, cmdline, alt_msg));
29✔
980
            break;
29✔
981
        case '|': {
6✔
982
            retval = TRY(execute_file(ec, cmdline));
6✔
983
            break;
4✔
984
        }
985
        default:
×
986
            retval = TRY(execute_command(ec, cmdline));
×
987
            break;
×
988
    }
989

990
    return Ok(retval);
33✔
991
}
35✔
992

993
void
994
execute_init_commands(
691✔
995
    exec_context& ec,
996
    std::vector<std::pair<Result<std::string, lnav::console::user_message>,
997
                          std::string>>& msgs)
998
{
999
    if (lnav_data.ld_cmd_init_done) {
691✔
1000
        return;
×
1001
    }
1002

1003
    std::string out_name;
691✔
1004
    std::optional<exec_context::output_t> ec_out;
691✔
1005
    auto_fd fd_copy;
691✔
1006
    int option_index = 1;
691✔
1007

1008
    {
1009
        log_info("Executing initial commands");
691✔
1010
        exec_context::output_guard og(ec, out_name, ec_out);
691✔
1011

1012
        for (auto& cmd : lnav_data.ld_commands) {
1,902✔
1013
            static const auto COMMAND_OPTION_SRC
1014
                = intern_string::lookup("command-option");
2,437✔
1015

1016
            std::string alt_msg;
1,211✔
1017

1018
            wait_for_children();
1,211✔
1019

1020
            lnav_data.ld_view_stack.top() | [&ec](auto* tc) {
1,211✔
1021
                ec.ec_top_line = tc->get_selection().value_or(0_vl);
1,211✔
1022
            };
1,211✔
1023
            log_debug("init cmd: %s", cmd.c_str());
1,211✔
1024
            {
1025
                auto top_view = lnav_data.ld_view_stack.top();
1,211✔
1026
                auto _sg
1027
                    = ec.enter_source(COMMAND_OPTION_SRC, option_index++, cmd);
1,211✔
1028
                switch (cmd.at(0)) {
1,211✔
1029
                    case ':':
788✔
1030
                        msgs.emplace_back(execute_command(ec, cmd.substr(1)),
788✔
1031
                                          alt_msg);
1032
                        break;
788✔
1033
                    case '/':
6✔
1034
                        execute_search(cmd.substr(1));
6✔
1035
                        break;
6✔
1036
                    case ';':
391✔
1037
                        setup_logline_table(ec);
391✔
1038
                        msgs.emplace_back(
391✔
1039
                            execute_sql(ec, cmd.substr(1), alt_msg), alt_msg);
782✔
1040
                        break;
391✔
1041
                    case '|':
26✔
1042
                        msgs.emplace_back(execute_file(ec, cmd.substr(1)),
26✔
1043
                                          alt_msg);
1044
                        break;
26✔
1045
                }
1046

1047
                rescan_files();
1,211✔
1048
                auto deadline = ui_clock::now();
1,211✔
1049
                if (lnav_data.ld_flags.is_set<lnav_flags::headless>()) {
1,211✔
1050
                    deadline += 5s;
1,210✔
1051
                } else {
1052
                    deadline += 500ms;
1✔
1053
                }
1054
                wait_for_pipers(deadline);
1,211✔
1055
                if (lnav_data.ld_flags.is_set<lnav_flags::headless>()
1,211✔
1056
                    || lnav_data.ld_input_dispatcher.id_count == 0)
1,211✔
1057
                {
1058
                    rebuild_indexes_repeatedly();
1,211✔
1059
                } else {
1060
                    rebuild_indexes(deadline);
×
1061
                }
1062
                if (top_view == lnav_data.ld_view_stack.top()) {
1,211✔
1063
                    setup_initial_view_stack();
1,130✔
1064
                }
1065

1066
                lnav::progress_tracker::instance().wait_for_completion(
1,211✔
1067
                    &lnav_data.ld_status_refresher);
1068
            }
1,211✔
1069
        }
1,211✔
1070
    }
691✔
1071
    lnav_data.ld_commands.clear();
691✔
1072
    lnav_data.ld_cmd_init_done = true;
691✔
1073
}
691✔
1074

1075
int
1076
sql_callback(exec_context& ec, sqlite3_stmt* stmt)
4,261✔
1077
{
1078
    auto& dls = *(ec.ec_label_source_stack.back());
4,261✔
1079
    const int ncols = sqlite3_column_count(stmt);
4,261✔
1080

1081
    if (!sqlite3_stmt_busy(stmt)) {
4,261✔
1082
        dls.clear();
1,505✔
1083

1084
        for (int lpc = 0; lpc < ncols; lpc++) {
4,659✔
1085
            const int type = sqlite3_column_type(stmt, lpc);
3,154✔
1086
            std::string colname = sqlite3_column_name(stmt, lpc);
3,154✔
1087

1088
            dls.push_header(colname, type);
3,154✔
1089
        }
3,154✔
1090
        return 0;
1,505✔
1091
    }
1092

1093
    int retval = 0;
2,756✔
1094
    auto set_vars = dls.dls_row_cursors.empty();
2,756✔
1095

1096
    if (dls.dls_row_cursors.empty()) {
2,756✔
1097
        for (int lpc = 0; lpc < ncols; lpc++) {
4,387✔
1098
            int type = sqlite3_column_type(stmt, lpc);
2,933✔
1099
            std::string colname = sqlite3_column_name(stmt, lpc);
2,933✔
1100

1101
            bool graphable = (type == SQLITE_INTEGER || type == SQLITE_FLOAT);
2,933✔
1102
            auto& hm = dls.dls_headers[lpc];
2,933✔
1103
            hm.hm_column_type = type;
2,933✔
1104
            if (lnav_data.ld_db_key_names.count(colname) > 0) {
2,933✔
1105
                hm.hm_graphable = false;
583✔
1106
            } else if (graphable) {
2,350✔
1107
                dls.set_col_as_graphable(lpc);
659✔
1108
            }
1109
        }
2,933✔
1110
    }
1111

1112
    dls.dls_row_cursors.emplace_back(dls.dls_cell_container.end_cursor());
2,756✔
1113
    dls.dls_push_column = 0;
2,756✔
1114
    for (int lpc = 0; lpc < ncols; lpc++) {
12,335✔
1115
        const auto value_type = sqlite3_column_type(stmt, lpc);
9,579✔
1116
        db_label_source::column_value_t value;
9,579✔
1117
        auto& hm = dls.dls_headers[lpc];
9,579✔
1118

1119
        switch (value_type) {
9,579✔
1120
            case SQLITE_INTEGER:
2,799✔
1121
                value = (int64_t) sqlite3_column_int64(stmt, lpc);
2,799✔
1122
                hm.hm_align = text_align_t::end;
2,799✔
1123
                break;
2,799✔
1124
            case SQLITE_FLOAT:
231✔
1125
                value = sqlite3_column_double(stmt, lpc);
231✔
1126
                hm.hm_align = text_align_t::end;
231✔
1127
                break;
231✔
1128
            case SQLITE_NULL:
2,317✔
1129
                value = null_value_t{};
2,317✔
1130
                break;
2,317✔
1131
            default: {
4,232✔
1132
                auto frag = string_fragment::from_bytes(
4,232✔
1133
                    sqlite3_column_text(stmt, lpc),
1134
                    sqlite3_column_bytes(stmt, lpc));
4,232✔
1135
                if (!frag.empty()) {
4,232✔
1136
                    if (isdigit(frag[0])) {
4,168✔
1137
                        hm.hm_align = text_align_t::end;
1,474✔
1138
                        if (!hm.hm_graphable.has_value()) {
1,474✔
1139
                            auto split_res = humanize::try_from<double>(frag);
258✔
1140
                            if (split_res.has_value()) {
258✔
1141
                                dls.set_col_as_graphable(lpc);
60✔
1142
                            } else {
1143
                                hm.hm_graphable = false;
198✔
1144
                            }
1145
                        }
1146
                    } else if (!hm.hm_graphable.has_value()) {
2,694✔
1147
                        hm.hm_graphable = false;
1,059✔
1148
                    }
1149
                }
1150
                value = frag;
4,232✔
1151
                break;
4,232✔
1152
            }
1153
        }
1154
        dls.push_column(value);
9,579✔
1155
        if ((hm.hm_column_type == SQLITE_TEXT
9,579✔
1156
             || hm.hm_column_type == SQLITE_NULL)
5,227✔
1157
            && hm.hm_sub_type == 0)
6,583✔
1158
        {
1159
            switch (value_type) {
6,461✔
1160
                case SQLITE_TEXT:
4,113✔
1161
                    auto* raw_value = sqlite3_column_value(stmt, lpc);
4,113✔
1162
                    hm.hm_column_type = SQLITE_TEXT;
4,113✔
1163
                    hm.hm_sub_type = sqlite3_value_subtype(raw_value);
4,113✔
1164
                    break;
4,113✔
1165
            }
1166
        }
1167
        if (set_vars && !ec.ec_local_vars.empty() && !ec.ec_dry_run) {
9,579✔
1168
            if (sql_ident_needs_quote(hm.hm_name.c_str())) {
2,933✔
1169
                continue;
1,182✔
1170
            }
1171
            auto& vars = ec.ec_local_vars.top();
1,751✔
1172

1173
            vars[hm.hm_name] = value.match(
3,502✔
1174
                [](const string_fragment& sf) {
×
1175
                    return scoped_value_t{sf.to_string()};
772✔
1176
                },
1177
                [](int64_t i) { return scoped_value_t{i}; },
503✔
1178
                [](double d) { return scoped_value_t{d}; },
15✔
1179
                [](null_value_t) { return scoped_value_t{null_value_t{}}; });
3,963✔
1180
        }
1181
    }
9,579✔
1182

1183
    return retval;
2,756✔
1184
}
1185

1186
int
1187
internal_sql_callback(exec_context& ec, sqlite3_stmt* stmt)
26✔
1188
{
1189
    if (!sqlite3_stmt_busy(stmt)) {
26✔
1190
        return 0;
15✔
1191
    }
1192

1193
    const int ncols = sqlite3_column_count(stmt);
11✔
1194

1195
    auto& vars = ec.ec_local_vars.top();
11✔
1196

1197
    for (int lpc = 0; lpc < ncols; lpc++) {
27✔
1198
        const char* column_name = sqlite3_column_name(stmt, lpc);
16✔
1199

1200
        if (sql_ident_needs_quote(column_name)) {
16✔
1201
            continue;
×
1202
        }
1203

1204
        auto value_type = sqlite3_column_type(stmt, lpc);
16✔
1205
        scoped_value_t value;
16✔
1206
        switch (value_type) {
16✔
1207
            case SQLITE_INTEGER:
2✔
1208
                value = (int64_t) sqlite3_column_int64(stmt, lpc);
2✔
1209
                break;
2✔
1210
            case SQLITE_FLOAT:
×
1211
                value = sqlite3_column_double(stmt, lpc);
×
1212
                break;
×
1213
            case SQLITE_NULL:
1✔
1214
                value = null_value_t{};
1✔
1215
                break;
1✔
1216
            default:
13✔
1217
                value
1218
                    = std::string((const char*) sqlite3_column_text(stmt, lpc),
26✔
1219
                                  sqlite3_column_bytes(stmt, lpc));
26✔
1220
                break;
13✔
1221
        }
1222
        vars[column_name] = value;
16✔
1223
    }
16✔
1224

1225
    return 0;
11✔
1226
}
1227

1228
std::future<std::string>
1229
pipe_callback(exec_context& ec, const std::string& cmdline, auto_fd& fd)
7✔
1230
{
1231
    static auto& prompt = lnav::prompt::get();
7✔
1232
    auto out = ec.get_output();
7✔
1233

1234
    if (out) {
7✔
1235
        FILE* file = *out;
7✔
1236

1237
        return std::async(std::launch::async, [fd = std::move(fd), file]() {
14✔
1238
            char buffer[1024];
1239
            ssize_t rc;
1240

1241
            if (file == stdout) {
7✔
1242
                lnav_data.ld_stdout_used = true;
7✔
1243
            }
1244

1245
            while ((rc = read(fd, buffer, sizeof(buffer))) > 0) {
14✔
1246
                fwrite(buffer, rc, 1, file);
7✔
1247
            }
1248

1249
            return std::string();
14✔
1250
        });
7✔
1251
    }
1252
    std::error_code errc;
×
1253
    std::filesystem::create_directories(lnav::paths::workdir(), errc);
×
1254
    auto open_temp_res = lnav::filesystem::open_temp_file(lnav::paths::workdir()
×
1255
                                                          / "exec.XXXXXX");
×
1256
    if (open_temp_res.isErr()) {
×
1257
        return lnav::futures::make_ready_future(
1258
            fmt::format(FMT_STRING("error: cannot open temp file -- {}"),
×
1259
                        open_temp_res.unwrapErr()));
×
1260
    }
1261

1262
    auto tmp_pair = open_temp_res.unwrap();
×
1263

1264
    auto reader = std::thread(
1265
        [in_fd = std::move(fd), out_fd = std::move(tmp_pair.second)]() {
×
1266
            char buffer[1024];
1267
            ssize_t rc;
1268

1269
            while ((rc = read(in_fd, buffer, sizeof(buffer))) > 0) {
×
1270
                write(out_fd, buffer, rc);
×
1271
            }
1272
        });
×
1273
    reader.detach();
×
1274

1275
    static int exec_count = 0;
1276
    auto desc
1277
        = fmt::format(FMT_STRING("exec-{}-output {}"), exec_count++, cmdline);
×
1278
    lnav_data.ld_active_files.fc_file_names[tmp_pair.first]
×
1279
        .with_filename(desc)
×
1280
        .with_include_in_session(false)
×
1281
        .with_detect_format(false)
×
1282
        .with_init_location(0_vl);
×
1283
    lnav_data.ld_files_to_front.emplace_back(desc);
×
1284
    prompt.p_editor.set_alt_value(HELP_MSG_1(X, "to close the file"));
×
1285

1286
    return lnav::futures::make_ready_future(std::string());
×
1287
}
1288

1289
void
1290
add_global_vars(exec_context& ec)
765✔
1291
{
1292
    for (const auto& iter : lnav_config.lc_global_vars) {
12,262✔
1293
        shlex subber(iter.second);
11,497✔
1294
        std::string str;
11,497✔
1295

1296
        if (!subber.eval(str, scoped_resolver{&ec.ec_global_vars})) {
11,497✔
1297
            log_error("Unable to evaluate global variable value: %s",
×
1298
                      iter.second.c_str());
1299
            continue;
×
1300
        }
1301

1302
        ec.ec_global_vars[iter.first] = str;
11,497✔
1303
    }
11,497✔
1304
}
765✔
1305

1306
void
1307
exec_context::set_output(const std::string& name,
692✔
1308
                         FILE* file,
1309
                         int (*closer)(FILE*))
1310
{
1311
    log_info("redirecting command output to: %s", name.c_str());
692✔
1312
    this->ec_output_stack.back().od_output | [](auto out) {
692✔
1313
        if (out.second != nullptr) {
×
1314
            out.second(out.first);
×
1315
        }
1316
    };
1317
    this->ec_output_stack.back()
692✔
1318
        = output_desc{name, std::make_pair(file, closer)};
1,384✔
1319
}
692✔
1320

1321
void
1322
exec_context::clear_output()
71✔
1323
{
1324
    log_info("redirecting command output to screen");
71✔
1325
    this->ec_output_stack.back().od_output | [](auto out) {
71✔
1326
        if (out.second != nullptr) {
33✔
1327
            out.second(out.first);
33✔
1328
        }
1329
    };
33✔
1330
    this->ec_output_stack.back().od_name = "default";
71✔
1331
    this->ec_output_stack.back().od_output = std::nullopt;
71✔
1332
}
71✔
1333

1334
exec_context::exec_context(logline_value_vector* line_values,
3,717✔
1335
                           sql_callback_t sql_callback,
1336
                           pipe_callback_t pipe_callback)
3,717✔
1337
    : ec_line_values(line_values),
3,717✔
1338
      ec_accumulator(std::make_unique<attr_line_t>()),
3,717✔
1339
      ec_sql_callback(sql_callback), ec_pipe_callback(pipe_callback)
7,434✔
1340
{
1341
    this->ec_local_vars.push(std::map<std::string, scoped_value_t>());
3,717✔
1342
    this->ec_path_stack.emplace_back(".");
3,717✔
1343
    this->ec_output_stack.emplace_back("screen", std::nullopt);
3,717✔
1344
}
3,717✔
1345

1346
Result<std::string, lnav::console::user_message>
1347
exec_context::execute(source_location loc, const std::string& cmdline)
2✔
1348
{
1349
    static auto& prompt = lnav::prompt::get();
2✔
1350
    static const auto& dls = lnav_data.ld_db_row_source;
1351

1352
    lnav::textinput::history::op_guard hist_guard;
2✔
1353
    auto sg = this->enter_source(loc, cmdline);
2✔
1354

1355
    auto before_dls_gen = dls.dls_generation;
2✔
1356
    if (this->get_provenance<mouse_input>() && !prompt.p_editor.is_enabled()
2✔
1357
        && !cmdline.empty())
2✔
1358
    {
1359
        auto& hist = prompt.get_history_for(cmdline[0]);
×
1360
        auto cmdline_sf = string_fragment::from_str(cmdline);
×
1361
        hist_guard = hist.start_operation(cmdline_sf.substr(1));
×
1362
    }
1363

1364
    auto exec_res = execute_any(*this, cmdline);
2✔
1365
    if (exec_res.isErr()) {
2✔
1366
        hist_guard.og_status = log_level_t::LEVEL_ERROR;
×
1367
        if (!this->ec_msg_callback_stack.empty()) {
×
1368
            this->ec_msg_callback_stack.back()(exec_res.unwrapErr());
×
1369
        }
1370
    } else {
1371
        if (before_dls_gen != dls.dls_generation
4✔
1372
            && dls.dls_row_cursors.size() > 1)
2✔
1373
        {
1374
            ensure_view(LNV_DB);
×
1375
        }
1376
    }
1377

1378
    return exec_res;
4✔
1379
}
2✔
1380

1381
void
1382
exec_context::add_error_context(lnav::console::user_message& um) const
90✔
1383
{
1384
    switch (um.um_level) {
90✔
1385
        case lnav::console::user_message::level::raw:
×
1386
        case lnav::console::user_message::level::info:
1387
        case lnav::console::user_message::level::ok:
1388
            return;
×
1389
        default:
90✔
1390
            break;
90✔
1391
    }
1392

1393
    if (!this->get_provenance<keyboard_input>()
90✔
1394
        && !this->get_provenance<mouse_input>() && um.um_snippets.empty())
90✔
1395
    {
1396
        um.with_snippets(this->ec_source);
42✔
1397
        um.remove_internal_snippets();
42✔
1398
    }
1399

1400
    if (this->ec_current_help != nullptr && um.um_help.empty()) {
90✔
1401
        attr_line_t help;
42✔
1402

1403
        format_help_text_for_term(*this->ec_current_help,
42✔
1404
                                  70,
1405
                                  help,
1406
                                  help_text_content::synopsis_and_summary);
1407
        um.with_help(help);
42✔
1408
    }
42✔
1409
}
1410

1411
lnav::console::user_message
1412
exec_context::make_error_from_str(std::string&& str) const
35✔
1413
{
1414
    auto retval = lnav::console::user_message::error(std::move(str));
35✔
1415
    this->add_error_context(retval);
35✔
1416
    return retval;
35✔
1417
}
×
1418

1419
exec_context::sql_callback_guard
1420
exec_context::push_callback(sql_callback_t cb)
5✔
1421
{
1422
    return sql_callback_guard(*this, cb);
5✔
1423
}
1424

1425
exec_context::source_guard
1426
exec_context::enter_source(source_location loc, const std::string& content)
2,667✔
1427
{
1428
    attr_line_t content_al{content};
2,667✔
1429
    content_al.with_attr_for_all(VC_ROLE.value(role_t::VCR_QUOTED_CODE));
2,667✔
1430
    readline_lnav_highlighter(content_al, -1);
2,667✔
1431
    this->ec_source.emplace_back(lnav::console::snippet::from(loc, content_al));
2,667✔
1432
    return {this};
5,334✔
1433
}
2,667✔
1434

1435
exec_context::output_guard::output_guard(exec_context& context,
760✔
1436
                                         std::string name,
1437
                                         const std::optional<output_t>& file,
1438
                                         text_format_t tf)
760✔
1439
    : sg_context(context), sg_active(!name.empty())
760✔
1440
{
1441
    if (name.empty()) {
760✔
1442
        return;
691✔
1443
    }
1444
    if (file) {
69✔
1445
        log_info("redirecting command output to: %s", name.c_str());
31✔
1446
    } else if (!context.ec_output_stack.empty()) {
38✔
1447
        tf = context.ec_output_stack.back().od_format;
38✔
1448
    }
1449
    context.ec_output_stack.emplace_back(std::move(name), file, tf);
69✔
1450
}
1451

1452
exec_context::output_guard::~output_guard()
760✔
1453
{
1454
    if (this->sg_active) {
760✔
1455
        this->sg_context.clear_output();
69✔
1456
        this->sg_context.ec_output_stack.pop_back();
69✔
1457
    }
1458
}
760✔
1459
exec_context::sql_callback_guard::sql_callback_guard(exec_context& context,
5✔
1460
                                                     sql_callback_t cb)
5✔
1461
    : scg_context(context), scg_old_callback(context.ec_sql_callback)
5✔
1462
{
1463
    context.ec_sql_callback = cb;
5✔
1464
}
5✔
1465
exec_context::sql_callback_guard::sql_callback_guard(sql_callback_guard&& other)
×
1466
    : scg_context(other.scg_context),
×
1467
      scg_old_callback(std::exchange(other.scg_old_callback, nullptr))
×
1468
{
1469
}
1470
exec_context::sql_callback_guard::~sql_callback_guard()
5✔
1471
{
1472
    if (this->scg_old_callback != nullptr) {
5✔
1473
        this->scg_context.ec_sql_callback = this->scg_old_callback;
5✔
1474
    }
1475
}
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