summaryrefslogtreecommitdiff
path: root/src/extsh.cpp
blob: 9dfeea8a69432291cc1055f747469e62a3049a9e (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
#include "fs/extfs.hpp"

#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")
    {
    return result::exit;
    }

  return result::unknown;
  }

std::string prompt(fs::extfs const & disk)
  {
  std::cout << '[' << (disk.has_label() ? disk.label() : "No Label") << "] >>> ";
  std::string command{};
  std::cin >> command;
  return command;
  }

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())
    {
    repl(disk);
    }
  else
    {
    std::clog << "Failed to open ext*fs at: '" << path << "'\n";
    }
  }