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

tstack / lnav / 23678721277-2893

28 Mar 2026 05:44AM UTC coverage: 69.061% (-0.005%) from 69.066%
23678721277-2893

push

github

tstack
[formats] add robot framework format

126 of 194 new or added lines in 4 files covered. (64.95%)

48 existing lines in 1 file now uncovered.

53025 of 76780 relevant lines covered (69.06%)

533262.84 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/paths.hh"
47
#include "base/result.h"
48
#include "base/time_util.hh"
49
#include "external_editor.cfg.hh"
50
#include "fmt/format.h"
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
    static const auto HOME_PATH = lnav::paths::userhome();
59

60
    if (config_dir.empty()) {
×
61
        return 0;
×
62
    }
63

64
    auto parent = path.parent_path();
×
65
    std::error_code ec;
×
66

67
    while (!parent.empty()) {
×
NEW
68
        if (parent == HOME_PATH) {
×
NEW
69
            break;
×
70
        }
71
        auto config_path = parent / config_dir;
×
NEW
72
        log_trace("  checking editor config dir: %s", config_path.c_str());
×
73
        auto mtime = std::filesystem::last_write_time(config_path, ec);
×
74
        if (!ec) {
×
75
            time64_t retval = mtime.time_since_epoch().count();
×
76

NEW
77
            log_debug("    found editor config dir: %s (%lld)",
×
78
                      config_path.c_str(),
79
                      retval);
80
            return retval;
×
81
        }
82

NEW
83
        for (const auto& sib : std::filesystem::directory_iterator(parent)) {
×
NEW
84
            if (!sib.is_directory()) {
×
NEW
85
                continue;
×
86
            }
87

NEW
88
            auto sib_config_path = sib.path() / config_dir;
×
NEW
89
            auto mtime = std::filesystem::last_write_time(config_path, ec);
×
NEW
90
            if (!ec) {
×
NEW
91
                time64_t retval = mtime.time_since_epoch().count();
×
92

NEW
93
                log_debug("    found editor config dir: %s (%lld)",
×
94
                          config_path.c_str(),
95
                          retval);
NEW
96
                return retval;
×
97
            }
98
        }
99

100
        auto new_parent = parent.parent_path();
×
101
        if (new_parent == parent) {
×
102
            return 0;
×
103
        }
104
        parent = new_parent;
×
105
    }
106
    return 0;
×
107
}
108

109
static std::optional<std::string>
110
get_impl(const std::filesystem::path& path)
×
111
{
112
    const auto& cfg = injector::get<const config&>();
×
113
    std::vector<std::tuple<time64_t, bool, const impl*>> candidates;
×
114

115
    log_debug("editor impl count: %zu", cfg.c_impls.size());
×
116
    for (const auto& [name, impl] : cfg.c_impls) {
×
117
        const auto full_cmd = fmt::format(FMT_STRING("{} > /dev/null 2>&1"),
×
118
                                          impl.i_test_command);
×
119

120
        log_debug(" testing editor impl %s using: %s",
×
121
                  name.c_str(),
122
                  full_cmd.c_str());
123
        if (system(full_cmd.c_str()) == 0) {
×
124
            log_info("  detected editor: %s", name.c_str());
×
125
            auto prefers = impl.i_prefers.pp_value
×
126
                ? impl.i_prefers.pp_value->find_in(path.string())
×
127
                      .ignore_error()
×
128
                      .has_value()
×
129
                : false;
×
130
            candidates.emplace_back(
×
131
                get_config_dir_mtime(path, impl.i_config_dir), prefers, &impl);
×
132
        }
133
    }
134

135
    std::sort(candidates.begin(),
×
136
              candidates.end(),
137
              [](const auto& lhs, const auto& rhs) {
×
138
                  const auto& [lmtime, lprefers, limpl] = lhs;
×
139
                  const auto& [rmtime, rprefers, rimpl] = rhs;
×
140

141
                  return lmtime > rmtime || (lmtime == rmtime && lprefers);
×
142
              });
143

144
    if (candidates.empty()) {
×
145
        return std::nullopt;
×
146
    }
147

148
    return std::get<2>(candidates.front())->i_command;
×
149
}
150

151
Result<void, std::string>
152
open(std::filesystem::path p, uint32_t line, uint32_t col)
×
153
{
154
    const auto impl = get_impl(p);
×
155

156
    if (!impl) {
×
157
        const static std::string MSG = "no external editor found";
×
158

159
        return Err(MSG);
×
160
    }
161

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

164
    auto err_pipe = TRY(auto_pipe::for_child_fd(STDERR_FILENO));
×
165
    auto child_pid_res = lnav::pid::from_fork();
×
166
    if (child_pid_res.isErr()) {
×
167
        return Err(child_pid_res.unwrapErr());
×
168
    }
169

170
    auto child_pid = child_pid_res.unwrap();
×
171
    err_pipe.after_fork(child_pid.in());
×
172
    if (child_pid.in_child()) {
×
173
        auto open_res
174
            = lnav::filesystem::open_file("/dev/null", O_RDONLY | O_CLOEXEC);
×
175
        open_res.then([](auto_fd&& fd) {
×
176
            fd.copy_to(STDIN_FILENO);
×
177
            fd.copy_to(STDOUT_FILENO);
×
178
        });
×
179
        setenv("FILE_PATH", p.c_str(), 1);
×
180
        auto line_str = fmt::to_string(line);
×
181
        setenv("LINE", line_str.c_str(), 1);
×
182
        auto col_str = fmt::to_string(col);
×
183
        setenv("COL", col_str.c_str(), 1);
×
184

185
        execlp("sh", "sh", "-c", impl->c_str(), nullptr);
×
186
        _exit(EXIT_FAILURE);
×
187
    }
×
188
    log_debug("started external editor, pid: %d", child_pid.in());
×
189

190
    std::string error_queue;
×
191
    std::thread err_reader(
192
        [err = std::move(err_pipe.read_end()), &error_queue]() mutable {
×
193
            while (true) {
194
                char buffer[1024];
195
                auto rc = read(err.get(), buffer, sizeof(buffer));
×
196
                if (rc <= 0) {
×
197
                    break;
×
198
                }
199

200
                error_queue.append(buffer, rc);
×
201
            }
202

203
            log_debug("external editor stderr closed");
×
204
        });
×
205

206
    auto finished_child = std::move(child_pid).wait_for_child();
×
207
    err_reader.join();
×
208
    if (!finished_child.was_normal_exit()) {
×
209
        return Err(fmt::format(FMT_STRING("editor failed with signal {}"),
×
210
                               finished_child.term_signal()));
×
211
    }
212
    auto exit_status = finished_child.exit_status();
×
213
    if (exit_status != 0) {
×
214
        return Err(fmt::format(FMT_STRING("editor failed with status {} -- {}"),
×
215
                               exit_status,
216
                               error_queue));
×
217
    }
218

219
    return Ok();
×
220
}
221

222
}  // 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