1 /**
2 * Copyright (c) 2013, 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 ON
25 * 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 * @file lnav_util.cc
30 *
31 * Dumping ground for useful functions with no other home.
32 */
33
34 #include "config.h"
35
36 #include <stdio.h>
37 #include <ctype.h>
38
39 #include "lnav_util.hh"
40 #include "base/opt_util.hh"
41 #include "base/result.h"
42 #include "ansi_scrubber.hh"
43 #include "fmt/format.h"
44 #include "view_curses.hh"
45
46 using namespace std;
47
change_to_parent_dir()48 bool change_to_parent_dir()
49 {
50 bool retval = false;
51 char cwd[3] = "";
52
53 if (getcwd(cwd, sizeof(cwd)) == nullptr) {
54 /* perror("getcwd"); */
55 }
56 if (strcmp(cwd, "/") != 0) {
57 if (chdir("..") == -1) {
58 perror("chdir('..')");
59 }
60 else {
61 retval = true;
62 }
63 }
64
65 return retval;
66 }
67
build_path(const vector<ghc::filesystem::path> & paths)68 string build_path(const vector<ghc::filesystem::path> &paths)
69 {
70 string retval;
71
72 for (const auto &path : paths) {
73 if (path.empty()) {
74 continue;
75 }
76 if (!retval.empty()) {
77 retval += ":";
78 }
79 retval += path.string();
80 }
81 auto env_path = getenv_opt("PATH");
82 if (env_path) {
83 retval += ":" + string(*env_path);
84 }
85 return retval;
86 }
87
read_file(const ghc::filesystem::path & path)88 Result<std::string, std::string> read_file(const ghc::filesystem::path &path)
89 {
90 try {
91 ghc::filesystem::ifstream file_stream(path);
92
93 if (!file_stream) {
94 return Err(std::string(strerror(errno)));
95 }
96
97 std::string retval;
98 retval.assign((std::istreambuf_iterator<char>(file_stream)),
99 std::istreambuf_iterator<char>());
100 return Ok(retval);
101 } catch (const std::exception& e) {
102 return Err(std::string(e.what()));
103 }
104 }
105
106 Result<std::pair<ghc::filesystem::path, int>, std::string>
open_temp_file(const ghc::filesystem::path & pattern)107 open_temp_file(const ghc::filesystem::path &pattern)
108 {
109 auto pattern_str = pattern.string();
110 char pattern_copy[pattern_str.size() + 1];
111 int fd;
112
113 strcpy(pattern_copy, pattern_str.c_str());
114 if ((fd = mkstemp(pattern_copy)) == -1) {
115 return Err(fmt::format("unable to create temporary file: {} -- {}",
116 pattern.string(), strerror(errno)));
117 }
118
119 return Ok(make_pair(ghc::filesystem::path(pattern_copy), fd));
120 }
121
is_dev_null(const struct stat & st)122 bool is_dev_null(const struct stat &st)
123 {
124 struct stat null_stat;
125
126 stat("/dev/null", &null_stat);
127
128 return st.st_dev == null_stat.st_dev &&
129 st.st_ino == null_stat.st_ino;
130 }
131
is_dev_null(int fd)132 bool is_dev_null(int fd)
133 {
134 struct stat fd_stat;
135
136 fstat(fd, &fd_stat);
137 return is_dev_null(fd_stat);
138 }
139
ok_prefix(std::string msg)140 std::string ok_prefix(std::string msg)
141 {
142 if (msg.empty()) {
143 return msg;
144 }
145
146 return std::string(ANSI_COLOR(COLOR_GREEN) "\u2714" ANSI_NORM " ") + msg;
147 }
148
err_prefix(const std::string msg)149 std::string err_prefix(const std::string msg)
150 {
151 if (msg.empty()) {
152 return msg;
153 }
154
155 return std::string(ANSI_COLOR(COLOR_RED) "\u2718" ANSI_NORM " ") + msg;
156 }
157
err_to_ok(const std::string msg)158 Result<std::string, std::string> err_to_ok(const std::string msg)
159 {
160 return Ok(err_prefix(msg));
161 }
162