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

tstack / lnav / 17896141997-2532

21 Sep 2025 04:27PM UTC coverage: 64.949% (-0.009%) from 64.958%
17896141997-2532

push

github

tstack
[build] missing include

45921 of 70703 relevant lines covered (64.95%)

407184.77 hits per line

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

47.27
/src/cmds.scripting.cc
1
/**
2
 * Copyright (c) 2025, 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 <memory>
31
#include <string>
32
#include <vector>
33

34
#include "base/itertools.hh"
35
#include "base/lnav.console.hh"
36
#include "base/result.h"
37
#include "bound_tags.hh"
38
#include "command_executor.hh"
39
#include "config.h"
40
#include "lnav.hh"
41
#include "lnav.indexing.hh"
42
#include "lnav.prompt.hh"
43
#include "lnav_commands.hh"
44
#include "readline_context.hh"
45
#include "scn/scan.h"
46
#include "service_tags.hh"
47
#include "session.export.hh"
48
#include "shlex.hh"
49
#include "sysclip.hh"
50
#include "yajlpp/yajlpp.hh"
51

52
#ifdef HAVE_RUST_DEPS
53
#    include "lnav_rs_ext.cxx.hh"
54
#endif
55

56
static Result<std::string, lnav::console::user_message>
57
com_export_session_to(exec_context& ec,
5✔
58
                      std::string cmdline,
59
                      std::vector<std::string>& args)
60
{
61
    std::string retval;
5✔
62

63
    if (!ec.ec_dry_run) {
5✔
64
        auto_mem<FILE> outfile(fclose);
5✔
65
        auto fn = trim(remaining_args(cmdline, args));
5✔
66
        auto to_term = false;
5✔
67

68
        if (fn == "-" || fn == "/dev/stdout") {
5✔
69
            auto ec_out = ec.get_output();
2✔
70

71
            if (!ec_out) {
2✔
72
                outfile = auto_mem<FILE>::leak(stdout);
×
73

74
                if (ec.ec_ui_callbacks.uc_pre_stdout_write) {
×
75
                    ec.ec_ui_callbacks.uc_pre_stdout_write();
×
76
                }
77
                setvbuf(stdout, nullptr, _IONBF, 0);
×
78
                to_term = true;
×
79
                fprintf(outfile,
×
80
                        "\n---------------- Press any key to exit "
81
                        "lo-fi "
82
                        "display "
83
                        "----------------\n\n");
84
            } else {
85
                outfile = auto_mem<FILE>::leak(ec_out.value());
2✔
86
            }
87
            if (outfile.in() == stdout) {
2✔
88
                lnav_data.ld_stdout_used = true;
2✔
89
            }
90
        } else if (fn == "/dev/clipboard") {
3✔
91
            auto open_res = sysclip::open(sysclip::type_t::GENERAL);
×
92
            if (open_res.isErr()) {
×
93
                alerter::singleton().chime("cannot open clipboard");
×
94
                return ec.make_error("Unable to copy to clipboard: {}",
95
                                     open_res.unwrapErr());
×
96
            }
97
            outfile = open_res.unwrap();
×
98
        } else if (lnav_data.ld_flags & LNF_SECURE_MODE) {
3✔
99
            return ec.make_error("{} -- unavailable in secure mode", args[0]);
×
100
        } else {
101
            if ((outfile = fopen(fn.c_str(), "we")) == nullptr) {
3✔
102
                return ec.make_error("unable to open file -- {}", fn);
×
103
            }
104
            fchmod(fileno(outfile.in()), S_IRWXU);
3✔
105
        }
106

107
        auto export_res = lnav::session::export_to(outfile.in());
5✔
108

109
        fflush(outfile.in());
5✔
110
        if (to_term) {
5✔
111
            if (ec.ec_ui_callbacks.uc_post_stdout_write) {
×
112
                ec.ec_ui_callbacks.uc_post_stdout_write();
×
113
            }
114
        }
115
        if (export_res.isErr()) {
5✔
116
            return Err(export_res.unwrapErr());
×
117
        }
118

119
        retval = fmt::format(
5✔
120
            FMT_STRING("info: wrote session commands to -- {}"), fn);
20✔
121
    }
5✔
122

123
    return Ok(retval);
5✔
124
}
5✔
125

126
static Result<std::string, lnav::console::user_message>
127
com_rebuild(exec_context& ec,
9✔
128
            std::string cmdline,
129
            std::vector<std::string>& args)
130
{
131
    if (!ec.ec_dry_run) {
9✔
132
        rescan_files(true);
9✔
133
        rebuild_indexes_repeatedly();
9✔
134
    }
135

136
    return Ok(std::string());
9✔
137
}
138

139
static Result<std::string, lnav::console::user_message>
140
com_echo(exec_context& ec, std::string cmdline, std::vector<std::string>& args)
45✔
141
{
142
    std::string retval = "error: expecting a message";
45✔
143

144
    if (args.size() >= 1) {
45✔
145
        bool lf = true;
45✔
146
        std::string src;
45✔
147

148
        if (args.size() > 2 && args[1] == "-n") {
45✔
149
            std::string::size_type index_in_cmdline = cmdline.find(args[1]);
1✔
150

151
            lf = false;
1✔
152
            src = cmdline.substr(index_in_cmdline + args[1].length() + 1);
1✔
153
        } else if (args.size() >= 2) {
44✔
154
            src = cmdline.substr(args[0].length() + 1);
30✔
155
        } else {
156
            src = "";
14✔
157
        }
158

159
        auto lexer = shlex(src);
45✔
160
        lexer.eval(retval, ec.create_resolver());
45✔
161

162
        auto ec_out = ec.get_output();
45✔
163
        if (ec.ec_dry_run) {
45✔
164
            lnav_data.ld_preview_status_source[0].get_description().set_value(
×
165
                "The text to output:"_frag);
×
166
            lnav_data.ld_status[LNS_PREVIEW0].set_needs_update();
×
167
            lnav_data.ld_preview_view[0].set_sub_source(
×
168
                &lnav_data.ld_preview_source[0]);
169
            lnav_data.ld_preview_source[0].replace_with(attr_line_t(retval));
×
170
            retval = "";
×
171
        } else if (ec_out) {
45✔
172
            FILE* outfile = *ec_out;
43✔
173

174
            if (outfile == stdout) {
43✔
175
                lnav_data.ld_stdout_used = true;
37✔
176
            }
177

178
            fprintf(outfile, "%s", retval.c_str());
43✔
179
            if (lf) {
43✔
180
                putc('\n', outfile);
42✔
181
            }
182
            fflush(outfile);
43✔
183

184
            retval = "";
43✔
185
        }
186
    }
45✔
187

188
    return Ok(retval);
90✔
189
}
45✔
190

191
static Result<std::string, lnav::console::user_message>
192
com_alt_msg(exec_context& ec,
×
193
            std::string cmdline,
194
            std::vector<std::string>& args)
195
{
196
    static auto& prompt = lnav::prompt::get();
197

198
    std::string retval;
×
199

200
    if (ec.ec_dry_run) {
×
201
        retval = "";
×
202
    } else if (args.size() == 1) {
×
203
        prompt.p_editor.clear_alt_value();
×
204
        retval = "";
×
205
    } else {
206
        std::string msg = remaining_args(cmdline, args);
×
207

208
        prompt.p_editor.set_alt_value(msg);
×
209
        retval = "";
×
210
    }
211

212
    return Ok(retval);
×
213
}
214

215
static Result<std::string, lnav::console::user_message>
216
com_eval(exec_context& ec, std::string cmdline, std::vector<std::string>& args)
22✔
217
{
218
    std::string retval;
22✔
219

220
    if (args.size() > 1) {
22✔
221
        static intern_string_t EVAL_SRC = intern_string::lookup(":eval");
32✔
222

223
        std::string all_args = remaining_args(cmdline, args);
22✔
224
        std::string expanded_cmd;
22✔
225
        shlex lexer(all_args.c_str(), all_args.size());
22✔
226

227
        log_debug("Evaluating: %s", all_args.c_str());
22✔
228
        if (!lexer.eval(expanded_cmd,
22✔
229
                        {
230
                            &ec.ec_local_vars.top(),
22✔
231
                            &ec.ec_global_vars,
22✔
232
                        }))
233
        {
234
            return ec.make_error("invalid arguments");
×
235
        }
236
        log_debug("Expanded command to evaluate: %s", expanded_cmd.c_str());
22✔
237

238
        if (expanded_cmd.empty()) {
22✔
239
            return ec.make_error("empty result after evaluation");
×
240
        }
241

242
        if (ec.ec_dry_run) {
22✔
243
            attr_line_t al(expanded_cmd);
×
244

245
            lnav_data.ld_preview_status_source[0].get_description().set_value(
×
246
                "The command to be executed:"_frag);
×
247
            lnav_data.ld_status[LNS_PREVIEW0].set_needs_update();
×
248

249
            lnav_data.ld_preview_view[0].set_sub_source(
×
250
                &lnav_data.ld_preview_source[0]);
251
            lnav_data.ld_preview_source[0].replace_with(al);
×
252

253
            return Ok(std::string());
×
254
        }
255

256
        auto src_guard = ec.enter_source(EVAL_SRC, 1, expanded_cmd);
22✔
257
        auto content = string_fragment::from_str(expanded_cmd);
22✔
258
        multiline_executor me(ec, ":eval");
22✔
259
        for (auto line : content.split_lines()) {
106✔
260
            TRY(me.push_back(line));
84✔
261
        }
22✔
262
        TRY(me.final());
22✔
263
        retval = std::move(me.me_last_result);
22✔
264
    } else {
22✔
265
        return ec.make_error("expecting a command or query to evaluate");
×
266
    }
267

268
    return Ok(retval);
22✔
269
}
22✔
270

271
static Result<std::string, lnav::console::user_message>
272
com_cd(exec_context& ec, std::string cmdline, std::vector<std::string>& args)
4✔
273
{
274
    static const intern_string_t SRC = intern_string::lookup("path");
12✔
275

276
    if (lnav_data.ld_flags & LNF_SECURE_MODE) {
4✔
277
        return ec.make_error("{} -- unavailable in secure mode", args[0]);
×
278
    }
279

280
    std::vector<std::string> word_exp;
4✔
281
    std::string pat;
4✔
282

283
    pat = trim(remaining_args(cmdline, args));
4✔
284

285
    shlex lexer(pat);
4✔
286
    auto split_args_res = lexer.split(ec.create_resolver());
4✔
287
    if (split_args_res.isErr()) {
4✔
288
        auto split_err = split_args_res.unwrapErr();
×
289
        auto um
290
            = lnav::console::user_message::error("unable to parse file name")
×
291
                  .with_reason(split_err.se_error.te_msg)
×
292
                  .with_snippet(lnav::console::snippet::from(
×
293
                      SRC, lexer.to_attr_line(split_err.se_error)))
×
294
                  .move();
×
295

296
        return Err(um);
×
297
    }
298

299
    auto split_args = split_args_res.unwrap()
8✔
300
        | lnav::itertools::map([](const auto& elem) { return elem.se_value; });
12✔
301

302
    if (split_args.size() != 1) {
4✔
303
        return ec.make_error("expecting a single argument");
×
304
    }
305

306
    struct stat st;
307

308
    if (stat(split_args[0].c_str(), &st) != 0) {
4✔
309
        return Err(ec.make_error_msg("cannot access -- {}", split_args[0])
3✔
310
                       .with_errno_reason());
1✔
311
    }
312

313
    if (!S_ISDIR(st.st_mode)) {
3✔
314
        return ec.make_error("{} is not a directory", split_args[0]);
2✔
315
    }
316

317
    if (!ec.ec_dry_run) {
2✔
318
        chdir(split_args[0].c_str());
2✔
319
        setenv("PWD", split_args[0].c_str(), 1);
2✔
320
    }
321

322
    return Ok(std::string());
2✔
323
}
4✔
324

325
static Result<std::string, lnav::console::user_message>
326
com_sh(exec_context& ec, std::string cmdline, std::vector<std::string>& args)
5✔
327
{
328
    if (lnav_data.ld_flags & LNF_SECURE_MODE) {
5✔
329
        return ec.make_error("{} -- unavailable in secure mode", args[0]);
×
330
    }
331

332
    static size_t EXEC_COUNT = 0;
333

334
    if (!ec.ec_dry_run) {
5✔
335
        std::optional<std::string> name_flag;
5✔
336

337
        shlex lexer(cmdline);
5✔
338
        auto cmd_start = args[0].size();
5✔
339
        auto split_res = lexer.split(ec.create_resolver());
5✔
340
        if (split_res.isOk()) {
5✔
341
            auto flags = split_res.unwrap();
5✔
342
            if (flags.size() >= 2) {
5✔
343
                static const char* NAME_FLAG = "--name=";
344

345
                if (startswith(flags[1].se_value, NAME_FLAG)) {
5✔
346
                    name_flag = flags[1].se_value.substr(strlen(NAME_FLAG));
×
347
                    cmd_start = flags[1].se_origin.sf_end;
×
348
                }
349
            }
350
        }
5✔
351

352
        auto carg = trim(cmdline.substr(cmd_start));
5✔
353

354
        log_info("executing: %s", carg.c_str());
5✔
355

356
        auto child_fds_res
357
            = auto_pipe::for_child_fds(STDOUT_FILENO, STDERR_FILENO);
5✔
358
        if (child_fds_res.isErr()) {
5✔
359
            auto um = lnav::console::user_message::error(
×
360
                          "unable to create child pipes")
361
                          .with_reason(child_fds_res.unwrapErr())
×
362
                          .move();
×
363
            ec.add_error_context(um);
×
364
            return Err(um);
×
365
        }
366
        auto child_res = lnav::pid::from_fork();
5✔
367
        if (child_res.isErr()) {
5✔
368
            auto um
369
                = lnav::console::user_message::error("unable to fork() child")
×
370
                      .with_reason(child_res.unwrapErr())
×
371
                      .move();
×
372
            ec.add_error_context(um);
×
373
            return Err(um);
×
374
        }
375

376
        auto child_fds = child_fds_res.unwrap();
5✔
377
        auto child = child_res.unwrap();
5✔
378
        for (auto& child_fd : child_fds) {
15✔
379
            child_fd.after_fork(child.in());
10✔
380
        }
381
        if (child.in_child()) {
5✔
382
            auto dev_null = open("/dev/null", O_RDONLY | O_CLOEXEC);
×
383

384
            dup2(dev_null, STDIN_FILENO);
×
385
            const char* exec_args[] = {
×
386
                getenv_opt("SHELL").value_or("bash"),
×
387
                "-c",
388
                carg.c_str(),
×
389
                nullptr,
390
            };
391

392
            for (const auto& pair : ec.ec_local_vars.top()) {
×
393
                pair.second.match(
×
394
                    [&pair](const std::string& val) {
×
395
                        setenv(pair.first.c_str(), val.c_str(), 1);
×
396
                    },
×
397
                    [&pair](const string_fragment& sf) {
×
398
                        setenv(pair.first.c_str(), sf.to_string().c_str(), 1);
×
399
                    },
×
400
                    [](null_value_t) {},
×
401
                    [&pair](int64_t val) {
×
402
                        setenv(
×
403
                            pair.first.c_str(), fmt::to_string(val).c_str(), 1);
×
404
                    },
×
405
                    [&pair](double val) {
×
406
                        setenv(
×
407
                            pair.first.c_str(), fmt::to_string(val).c_str(), 1);
×
408
                    },
×
409
                    [&pair](bool val) {
×
410
                        setenv(pair.first.c_str(), val ? "1" : "0", 1);
×
411
                    });
×
412
            }
413

414
            execvp(exec_args[0], (char**) exec_args);
×
415
            _exit(EXIT_FAILURE);
×
416
        }
417

418
        std::string display_name;
5✔
419
        auto open_prov = ec.get_provenance<exec_context::file_open>();
5✔
420
        if (open_prov) {
5✔
421
            if (name_flag) {
1✔
422
                display_name = fmt::format(
×
423
                    FMT_STRING("{}/{}"), open_prov->fo_name, name_flag.value());
×
424
            } else {
425
                display_name = open_prov->fo_name;
1✔
426
            }
427
        } else if (name_flag) {
4✔
428
            display_name = name_flag.value();
×
429
        } else {
430
            display_name
431
                = fmt::format(FMT_STRING("sh-{} {}"), EXEC_COUNT++, carg);
16✔
432
        }
433

434
        auto name_base = display_name;
5✔
435
        size_t name_counter = 0;
5✔
436

437
        while (true) {
438
            auto fn_iter
439
                = lnav_data.ld_active_files.fc_file_names.find(display_name);
5✔
440
            if (fn_iter == lnav_data.ld_active_files.fc_file_names.end()) {
5✔
441
                break;
5✔
442
            }
443
            name_counter += 1;
×
444
            display_name
445
                = fmt::format(FMT_STRING("{} [{}]"), name_base, name_counter);
×
446
        }
447

448
        auto create_piper_res
449
            = lnav::piper::create_looper(display_name,
450
                                         std::move(child_fds[0].read_end()),
5✔
451
                                         std::move(child_fds[1].read_end()));
10✔
452

453
        if (create_piper_res.isErr()) {
5✔
454
            auto um
455
                = lnav::console::user_message::error("unable to create piper")
×
456
                      .with_reason(create_piper_res.unwrapErr())
×
457
                      .move();
×
458
            ec.add_error_context(um);
×
459
            return Err(um);
×
460
        }
461

462
        lnav_data.ld_active_files.fc_file_names[display_name].with_piper(
10✔
463
            create_piper_res.unwrap());
10✔
464
        lnav_data.ld_child_pollers.emplace_back(child_poller{
15✔
465
            display_name,
466
            std::move(child),
5✔
467
            [](auto& fc, auto& child) {},
5✔
468
        });
469
        lnav_data.ld_files_to_front.emplace_back(display_name);
5✔
470

471
        return Ok(fmt::format(FMT_STRING("info: executing -- {}"), carg));
20✔
472
    }
5✔
473

474
    return Ok(std::string());
×
475
}
476

477
#ifdef HAVE_RUST_DEPS
478
namespace lnav_rs_ext {
479

480
::rust::String
481
version_info()
×
482
{
483
    yajlpp_gen gen;
×
484
    {
485
        yajlpp_map root(gen);
×
486

487
        root.gen("product");
×
488
        root.gen(PACKAGE);
×
489
        root.gen("version");
×
490
        root.gen(PACKAGE_VERSION);
×
491
    }
492

493
    return gen.to_string_fragment().to_string();
×
494
}
495

496
ExecResult
497
execute_external_command(::rust::String rs_src,
×
498
                         ::rust::String rs_script,
499
                         ::rust::String hdrs)
500
{
501
    auto src = (std::string) rs_src;
×
502
    auto script = (std::string) rs_script;
×
503
    auto retval = std::make_shared<ExecResult>();
×
504

505
    log_debug("sending remote command to main looper");
×
506
    isc::to<main_looper&, services::main_t>().send_and_wait(
×
507
        [src, script, hdrs, &retval](auto& mlooper) {
×
508
            log_debug("executing remote command from: %s", src.c_str());
×
509
            auto& ec = lnav_data.ld_exec_context;
×
510
            auto* outfile = tmpfile();
×
511
            auto ec_out = exec_context::output_t{outfile, fclose};
×
512
            auto og = exec_context::output_guard{ec, "default", ec_out};
×
513
            auto me = multiline_executor{ec, src};
×
514
            auto pg = ec.with_provenance(exec_context::external_access{src});
×
515
            ec.ec_local_vars.push(std::map<std::string, scoped_value_t>{
×
516
                {"headers", scoped_value_t{(std::string) hdrs}}});
×
517
            auto script_frag = string_fragment::from_str(script);
×
518
            for (const auto& line : script_frag.split_lines()) {
×
519
                auto res = me.push_back(line);
×
520
                if (res.isErr()) {
×
521
                    auto um = res.unwrapErr();
×
522
                    retval->error.msg = um.um_message.al_string;
×
523
                    retval->error.reason = um.um_reason.al_string;
×
524
                    retval->error.help = um.um_help.al_string;
×
525
                    ec.ec_local_vars.pop();
×
526
                    return;
×
527
                }
528
            }
529
            auto res = me.final();
×
530
            if (res.isErr()) {
×
531
                auto um = res.unwrapErr();
×
532
                retval->error.msg = um.um_message.al_string;
×
533
                retval->error.reason = um.um_reason.al_string;
×
534
                retval->error.help = um.um_help.al_string;
×
535
            } else {
×
536
                fseek(ec_out.first, 0, SEEK_SET);
×
537
                retval->status = me.me_last_result;
×
538
                retval->content_type = fmt::to_string(ec.get_output_format());
×
539
                retval->content_fd = dup(fileno(ec_out.first));
×
540
            }
541
            ec.ec_local_vars.pop();
×
542
        });
×
543

544
    return *retval;
×
545
}
546

547
}  // namespace lnav_rs_ext
548
#endif
549

550
static Result<std::string, lnav::console::user_message>
551
com_external_access(exec_context& ec,
×
552
                    std::string cmdline,
553
                    std::vector<std::string>& args)
554
{
555
#ifdef HAVE_RUST_DEPS
556
    if (args.size() != 3) {
×
557
        return ec.make_error("Expecting port number and API key");
×
558
    }
559

560
    if (lnav_data.ld_flags & LNF_SECURE_MODE) {
×
561
        return ec.make_error("External access is not available in secure mode");
×
562
    }
563

564
    std::string retval;
×
565
    if (ec.ec_dry_run) {
×
566
        return Ok(retval);
×
567
    }
568

569
    auto scan_res = scn::scan_int<uint16_t>(args[1]);
×
570
    if (!scan_res || !scan_res.value().range().empty()) {
×
571
        return ec.make_error(FMT_STRING("port value is not a number: {}"),
×
572
                             args[1]);
×
573
    }
574
    auto port = scan_res->value();
×
575

576
    auto start_res = lnav_rs_ext::start_ext_access(port, args[2]);
×
577
    if (start_res.port == 0) {
×
578
        return ec.make_error(FMT_STRING("unable to start external access: {}"),
×
579
                             (std::string) start_res.error);
×
580
    }
581

582
    retval = fmt::format(FMT_STRING("info: started external access on port {}"),
×
583
                         start_res.port);
×
584
    setenv("LNAV_EXTERNAL_PORT", fmt::to_string(start_res.port).c_str(), 1);
×
585
    auto url = fmt::format(FMT_STRING("http://127.0.0.1:{}"), start_res.port);
×
586
    setenv("LNAV_EXTERNAL_URL", url.c_str(), 1);
×
587

588
    return Ok(retval);
×
589
#else
590
    return ec.make_error("lnav was compiled without Rust extensions");
591
#endif
592
}
593

594
static readline_context::command_t SCRIPTING_COMMANDS[] = {
595
    {
596
        "export-session-to",
597
        com_export_session_to,
598

599
        help_text(":export-session-to")
600
            .with_summary("Export the current lnav state to an executable lnav "
601
                          "script file that contains the commands needed to "
602
                          "restore the current session")
603
            .with_parameter(
604
                help_text("path", "The path to the file to write")
605
                    .with_format(help_parameter_format_t::HPF_LOCAL_FILENAME))
606
            .with_tags({"io", "scripting"}),
607
    },
608
    {
609
        "rebuild",
610
        com_rebuild,
611
        help_text(":rebuild")
612
            .with_summary("Forcefully rebuild file indexes")
613
            .with_tags({"scripting"}),
614
    },
615
    {
616
        "echo",
617
        com_echo,
618

619
        help_text(":echo")
620
            .with_summary(
621
                "Echo the given message to the screen or, if "
622
                ":redirect-to has "
623
                "been called, to output file specified in the "
624
                "redirect.  "
625
                "Variable substitution is performed on the message.  "
626
                "Use a "
627
                "backslash to escape any special characters, like '$'")
628
            .with_parameter(help_text("-n",
629
                                      "Do not print a line-feed at "
630
                                      "the end of the output")
631
                                .optional()
632
                                .with_format(help_parameter_format_t::HPF_TEXT))
633
            .with_parameter(help_text("msg", "The message to display"))
634
            .with_tags({"io", "scripting"})
635
            .with_example({"To output 'Hello, World!'", "Hello, World!"}),
636
    },
637
    {
638
        "alt-msg",
639
        com_alt_msg,
640

641
        help_text(":alt-msg")
642
            .with_summary("Display a message in the alternate command position")
643
            .with_parameter(help_text("msg", "The message to display")
644
                                .with_format(help_parameter_format_t::HPF_TEXT))
645
            .with_tags({"scripting"})
646
            .with_example({"To display 'Press t to switch to the text view' on "
647
                           "the bottom right",
648
                           "Press t to switch to the text view"}),
649
    },
650
    {
651
        "eval",
652
        com_eval,
653

654
        help_text(":eval")
655
            .with_summary("Evaluate the given command/query after doing "
656
                          "environment variable substitution")
657
            .with_parameter(help_text(
658
                "command", "The command or query to perform substitution on."))
659
            .with_tags({"scripting"})
660
            .with_examples({{"To substitute the table name from a variable",
661
                             ";SELECT * FROM ${table}"}}),
662
    },
663
    {
664
        "sh",
665
        com_sh,
666

667
        help_text(":sh")
668
            .with_summary("Execute the given command-line and display the "
669
                          "captured output")
670
            .with_parameter(help_text(
671
                "--name=<name>", "The name to give to the captured output"))
672
            .with_parameter(
673
                help_text("cmdline", "The command-line to execute."))
674
            .with_tags({"scripting"}),
675
    },
676
    {
677
        "cd",
678
        com_cd,
679

680
        help_text(":cd")
681
            .with_summary("Change the current directory")
682
            .with_parameter(
683
                help_text("dir", "The new current directory")
684
                    .with_format(help_parameter_format_t::HPF_DIRECTORY))
685
            .with_tags({"scripting"}),
686
    },
687
    {
688
        "external-access",
689
        com_external_access,
690
        help_text(":external-access")
691
            .with_summary(
692
                "Open a port to give remote access to this lnav instance")
693
            .with_parameter(
694
                help_text("port", "The port number to listen on")
695
                    .with_format(help_parameter_format_t::HPF_NUMBER))
696
            .with_parameter(
697
                help_text("api-key", "The API key")
698
                    .with_format(help_parameter_format_t::HPF_STRING))
699
            .with_tags({"scripting"}),
700
    },
701
};
702

703
void
704
init_lnav_scripting_commands(readline_context::command_map_t& cmd_map)
573✔
705
{
706
    for (auto& cmd : SCRIPTING_COMMANDS) {
5,157✔
707
        cmd.c_help.index_tags();
4,584✔
708
        cmd_map[cmd.c_name] = &cmd;
13,752✔
709
    }
710
}
573✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc