summaryrefslogtreecommitdiff
path: root/src/extsh.cpp
blob: 1c223d9cd715c0291761b6d5f7c8bc33a8b7f61f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "fs/extfs.hpp"

#include <linenoise.h>

#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>

enum struct result : std::uint8_t
  {
  keep_going,
  fatal_error,
  exit,
  unknown,
  };

result process(std::string const & command)
  {
  if(command == "exit" || command.empty())
    {
    return result::exit;
    }

  return result::unknown;
  }

std::string prompt(fs::extfs const & disk)
  {
  using namespace std::string_literals;

  auto const promptText = "["s + (disk.has_label() ? disk.label() : "No Label") + "] > ";
  auto const input = linenoise(promptText.c_str());
  if(input)
    {
    linenoiseHistoryAdd(input);
    auto const inputString = std::string{input};
    linenoiseFree(input);
    return inputString;
    }

  return {};
  }

void repl(fs::extfs & disk)
  {
  result commandResult = result::unknown;
  while((commandResult = process(prompt(disk))) != result::exit)
    {
    switch(commandResult)
      {
      case result::unknown:
        std::cout << "unknown command\n";
        break;
      case result::fatal_error:
        std::cout << "fatal error\n";
        return;
      default:
        break;
      }
    }

  std::cout << "Bye!\n";
  }

int main(int argc, char const * argv[])
  {
  auto const & path = [&]{ return std::string{argc > 1 ? argv[1] : "vdisk.img"}; }();
  fs::extfs disk{path};

  if(disk.open())
    {
    linenoiseHistorySetMaxLen(1024);
    repl(disk);
    }
  else
    {
    std::clog << "Failed to open ext*fs at: '" << path << "'\n";
    }
  }