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

tstack / lnav / 17904983265-2534

22 Sep 2025 04:54AM UTC coverage: 64.976% (-0.003%) from 64.979%
17904983265-2534

push

github

tstack
[external] need to base64 encode the API key

0 of 4 new or added lines in 1 file covered. (0.0%)

45966 of 70743 relevant lines covered (64.98%)

407069.88 hits per line

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

46.85
/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 "libbase64.h"
41
#include "lnav.hh"
42
#include "lnav.indexing.hh"
43
#include "lnav.prompt.hh"
44
#include "lnav_commands.hh"
45
#include "readline_context.hh"
46
#include "scn/scan.h"
47
#include "service_tags.hh"
48
#include "session.export.hh"
49
#include "shlex.hh"
50
#include "sysclip.hh"
51
#include "yajlpp/yajlpp.hh"
52

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

199
    std::string retval;
×
200

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

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

213
    return Ok(retval);
×
214
}
215

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

297
        return Err(um);
×
298
    }
299

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

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

307
    struct stat st;
308

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

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

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

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

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

333
    static size_t EXEC_COUNT = 0;
334

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

478
#ifdef HAVE_RUST_DEPS
479
namespace lnav_rs_ext {
480

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

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

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

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

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

545
    return *retval;
×
546
}
547

548
}  // namespace lnav_rs_ext
549
#endif
550

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

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

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

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

NEW
577
    auto buf = auto_buffer::alloc((args[2].size() * 5) / 3);
×
NEW
578
    auto outlen = buf.capacity();
×
NEW
579
    base64_encode(args[2].data(), args[2].size(), buf.in(), &outlen, 0);
×
580
    auto start_res
NEW
581
        = lnav_rs_ext::start_ext_access(port, ::rust::String(buf.in(), outlen));
×
582
    if (start_res.port == 0) {
×
583
        return ec.make_error(FMT_STRING("unable to start external access: {}"),
×
584
                             (std::string) start_res.error);
×
585
    }
586

587
    retval = fmt::format(FMT_STRING("info: started external access on port {}"),
×
588
                         start_res.port);
×
589
    setenv("LNAV_EXTERNAL_PORT", fmt::to_string(start_res.port).c_str(), 1);
×
590
    auto url = fmt::format(FMT_STRING("http://127.0.0.1:{}"), start_res.port);
×
591
    setenv("LNAV_EXTERNAL_URL", url.c_str(), 1);
×
592

593
    return Ok(retval);
×
594
#else
595
    return ec.make_error("lnav was compiled without Rust extensions");
596
#endif
597
}
598

599
static readline_context::command_t SCRIPTING_COMMANDS[] = {
600
    {
601
        "export-session-to",
602
        com_export_session_to,
603

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

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

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

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

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

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

708
void
709
init_lnav_scripting_commands(readline_context::command_map_t& cmd_map)
573✔
710
{
711
    for (auto& cmd : SCRIPTING_COMMANDS) {
5,157✔
712
        cmd.c_help.index_tags();
4,584✔
713
        cmd_map[cmd.c_name] = &cmd;
13,752✔
714
    }
715
}
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