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

tstack / lnav / 19153812334-2653

07 Nov 2025 12:09AM UTC coverage: 69.033% (+0.003%) from 69.03%
19153812334-2653

push

github

tstack
[tidy] add format-string warnings

54 of 97 new or added lines in 39 files covered. (55.67%)

3 existing lines in 2 files now uncovered.

50702 of 73446 relevant lines covered (69.03%)

435988.05 hits per line

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

0.0
/src/external_editor.cc
1
/**
2
 * Copyright (c) 2024, 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 <filesystem>
31
#include <optional>
32
#include <thread>
33
#include <vector>
34

35
#include "external_editor.hh"
36

37
#include <fcntl.h>
38
#include <stdlib.h>
39
#include <unistd.h>
40

41
#include "base/auto_fd.hh"
42
#include "base/auto_pid.hh"
43
#include "base/fs_util.hh"
44
#include "base/injector.hh"
45
#include "base/lnav_log.hh"
46
#include "base/result.h"
47
#include "base/time_util.hh"
48
#include "external_editor.cfg.hh"
49
#include "fmt/format.h"
50
#include "shlex.hh"
51

52
namespace lnav::external_editor {
53

54
static time64_t
55
get_config_dir_mtime(const std::filesystem::path& path,
×
56
                     const std::filesystem::path& config_dir)
57
{
58
    if (config_dir.empty()) {
×
59
        return 0;
×
60
    }
61

62
    auto parent = path.parent_path();
×
63
    std::error_code ec;
×
64

65
    while (!parent.empty()) {
×
66
        auto config_path = parent / config_dir;
×
67
        auto mtime = std::filesystem::last_write_time(config_path, ec);
×
68
        if (!ec) {
×
NEW
69
            time64_t retval = mtime.time_since_epoch().count();
×
70

71
            log_debug("  found editor config dir: %s (%lld)",
×
72
                      config_path.c_str(),
73
                      retval);
74
            return retval;
×
75
        }
76
        auto new_parent = parent.parent_path();
×
77
        if (new_parent == parent) {
×
78
            return 0;
×
79
        }
80
        parent = new_parent;
×
81
    }
82
    return 0;
×
83
}
84

85
static std::optional<std::string>
86
get_impl(const std::filesystem::path& path)
×
87
{
88
    const auto& cfg = injector::get<const config&>();
×
89
    std::vector<std::tuple<time64_t, bool, const impl*>> candidates;
×
90

NEW
91
    log_debug("editor impl count: %zu", cfg.c_impls.size());
×
92
    for (const auto& [name, impl] : cfg.c_impls) {
×
93
        const auto full_cmd = fmt::format(FMT_STRING("{} > /dev/null 2>&1"),
×
94
                                          impl.i_test_command);
×
95

96
        log_debug(" testing editor impl %s using: %s",
×
97
                  name.c_str(),
98
                  full_cmd.c_str());
99
        if (system(full_cmd.c_str()) == 0) {
×
100
            log_info("  detected editor: %s", name.c_str());
×
101
            auto prefers = impl.i_prefers.pp_value
×
102
                ? impl.i_prefers.pp_value->find_in(path.string())
×
103
                      .ignore_error()
×
104
                      .has_value()
×
105
                : false;
×
106
            candidates.emplace_back(
×
107
                get_config_dir_mtime(path, impl.i_config_dir), prefers, &impl);
×
108
        }
109
    }
110

111
    std::sort(candidates.begin(),
×
112
              candidates.end(),
113
              [](const auto& lhs, const auto& rhs) {
×
114
                  const auto& [lmtime, lprefers, limpl] = lhs;
×
115
                  const auto& [rmtime, rprefers, rimpl] = rhs;
×
116

117
                  return lmtime > rmtime || (lmtime == rmtime && lprefers);
×
118
              });
119

120
    if (candidates.empty()) {
×
121
        return std::nullopt;
×
122
    }
123

124
    return std::get<2>(candidates.front())->i_command;
×
125
}
126

127
Result<void, std::string>
128
open(std::filesystem::path p, uint32_t line, uint32_t col)
×
129
{
130
    const auto impl = get_impl(p);
×
131

132
    if (!impl) {
×
133
        const static std::string MSG = "no external editor found";
×
134

135
        return Err(MSG);
×
136
    }
137

138
    log_info("external editor command: %s", impl->c_str());
×
139

140
    auto err_pipe = TRY(auto_pipe::for_child_fd(STDERR_FILENO));
×
141
    auto child_pid_res = lnav::pid::from_fork();
×
142
    if (child_pid_res.isErr()) {
×
143
        return Err(child_pid_res.unwrapErr());
×
144
    }
145

146
    auto child_pid = child_pid_res.unwrap();
×
147
    err_pipe.after_fork(child_pid.in());
×
148
    if (child_pid.in_child()) {
×
149
        auto open_res
150
            = lnav::filesystem::open_file("/dev/null", O_RDONLY | O_CLOEXEC);
×
151
        open_res.then([](auto_fd&& fd) {
×
152
            fd.copy_to(STDIN_FILENO);
×
153
            fd.copy_to(STDOUT_FILENO);
×
154
        });
×
155
        setenv("FILE_PATH", p.c_str(), 1);
×
156
        auto line_str = fmt::to_string(line);
×
157
        setenv("LINE", line_str.c_str(), 1);
×
158
        auto col_str = fmt::to_string(col);
×
159
        setenv("COL", col_str.c_str(), 1);
×
160

161
        execlp("sh", "sh", "-c", impl->c_str(), nullptr);
×
162
        _exit(EXIT_FAILURE);
×
163
    }
×
164
    log_debug("started external editor, pid: %d", child_pid.in());
×
165

166
    std::string error_queue;
×
167
    std::thread err_reader(
168
        [err = std::move(err_pipe.read_end()), &error_queue]() mutable {
×
169
            while (true) {
170
                char buffer[1024];
171
                auto rc = read(err.get(), buffer, sizeof(buffer));
×
172
                if (rc <= 0) {
×
173
                    break;
×
174
                }
175

176
                error_queue.append(buffer, rc);
×
177
            }
178

179
            log_debug("external editor stderr closed");
×
180
        });
×
181

182
    auto finished_child = std::move(child_pid).wait_for_child();
×
183
    err_reader.join();
×
184
    if (!finished_child.was_normal_exit()) {
×
185
        return Err(fmt::format(FMT_STRING("editor failed with signal {}"),
×
186
                               finished_child.term_signal()));
×
187
    }
188
    auto exit_status = finished_child.exit_status();
×
189
    if (exit_status != 0) {
×
190
        return Err(fmt::format(FMT_STRING("editor failed with status {} -- {}"),
×
191
                               exit_status,
192
                               error_queue));
×
193
    }
194

195
    return Ok();
×
196
}
197

198
}  // namespace lnav::external_editor
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