1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-02 23:32:06 +00:00

Shell: Oops, don't exit() when ~/.history does not exist

This commit is contained in:
Andreas Kling 2019-09-04 15:14:12 +02:00
parent 16de02cce0
commit 0b1981ddae

View file

@ -533,18 +533,18 @@ CFile get_history_file(CIODevice::OpenMode mode)
sb.append(g.home); sb.append(g.home);
sb.append("/.history"); sb.append("/.history");
CFile f(sb.to_string()); CFile f(sb.to_string());
if (!f.open(mode)) { if (!f.open(mode))
fprintf(stderr, "Error opening file '%s': '%s'\n", f.filename().characters(), f.error_string()); return {};
exit(1);
}
return f; return f;
} }
void load_history() void load_history()
{ {
CFile history_file = get_history_file(CIODevice::ReadOnly); auto history_file = get_history_file(CIODevice::ReadOnly);
if (!history_file.is_open())
return;
while (history_file.can_read_line()) { while (history_file.can_read_line()) {
const auto&b = history_file.read_line(1024); auto b = history_file.read_line(1024);
// skip the newline and terminating bytes // skip the newline and terminating bytes
editor.add_to_history(String(reinterpret_cast<const char*>(b.pointer()), b.size() - 2)); editor.add_to_history(String(reinterpret_cast<const char*>(b.pointer()), b.size() - 2));
} }
@ -552,7 +552,9 @@ void load_history()
void save_history() void save_history()
{ {
CFile history_file = get_history_file(CIODevice::WriteOnly); auto history_file = get_history_file(CIODevice::WriteOnly);
if (!history_file.is_open())
return;
for (const auto& line : editor.history()) { for (const auto& line : editor.history()) {
history_file.write(line); history_file.write(line);
history_file.write("\n"); history_file.write("\n");