Skip to content

Commit

Permalink
added tutorial cmd and 2 levels
Browse files Browse the repository at this point in the history
  • Loading branch information
esakh24 committed May 9, 2021
1 parent caaa695 commit 096455b
Show file tree
Hide file tree
Showing 18 changed files with 206 additions and 4 deletions.
5 changes: 5 additions & 0 deletions include/levels.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <string>
void print_instructions(std::string p);
void print_no_access_to_level();

bool check_password(int level);
5 changes: 5 additions & 0 deletions include/tutorial.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

int roosh_exec_tutorial(char **args, int num_args);
int roosh_tutorial();
int create_tutorial_folder();
10 changes: 8 additions & 2 deletions src/launch.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
#include "../include/launch.hpp"

#include <cstring>
#include <string>
#include <vector>

#include "../include/parse.hpp"
#include "../include/pipe.hpp"
#include "../include/redirect.hpp"
#include "../include/tutorial.hpp"

using namespace std;

bool roosh_launch(const string &line)
{
vector<string> cmds = roosh_tokenizer(line, '|');

auto [args, num_args] = roosh_parse(line);
if (strcmp(args[0], "tutorial") == 0)
{
return roosh_exec_tutorial(args, num_args);
}
// take input for first command
// from a file if required
int in_fd = redirect_input(cmds[0]);
Expand All @@ -22,6 +28,6 @@ bool roosh_launch(const string &line)
{
return 1;
}

return pipe_exec(cmds, 0, in_fd);
}
57 changes: 57 additions & 0 deletions src/levels.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

const char *passwd_list[] = {

"XfwQrTY8drdyR8CDdpcgBSYq",
"B7GXVdmXZGDgBXjUrP2aKhnc",
"j7uvv2CwUFSujANxHnM2dM4L",
"wdJPxNNK66cUGra6JJjbCLRe",
"8X8jevLsYwWpZkhvDHXUWu5Q",
"fcyHejqsN4LUgqVD9SACZLzh",
"Y99Ch3hbZy8QhDm639BCxayF",
"Q9n59xHrrTmyRcWc6Ag8m8d9",
"JtfU4VftpqH3wbB6cVdjdLqr",
"NZAbCFvQuSrPmDgrQHRCj2GZ"};

string curr_pass;
int curr_level = 1;
bool unlocked[10];

void print_instructions(string p)
{
string line;
ifstream myfile(p);
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
else
cout << "Unable to open file.\nPlease try again.";
}

void print_no_access_to_level()
{
cout << "Oops! you need to solve all previous levels to unlock this level. Currently you can attempt level " << curr_level << " .\n";
}

bool check_password(int level)
{
cout << "enter password\n";
cin >> curr_pass;
if (curr_pass == passwd_list[level - 1])
{
cout << "Success!!\n";
unlocked[level] = 1;
curr_level++;
}
else
cout << "wrong password!!\n";
return 1;
}
8 changes: 6 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
#include "../include/launch.hpp"
#include "../include/input.hpp"
#include "../include/builtin.hpp"
using namespace std;
#include "../include/tutorial.hpp"

using namespace std;

void signal_handler(int sig_num){
void signal_handler(int sig_num)
{
// Prevent user to exit with Ctrl+C
signal(SIGINT, signal_handler);
cout << "\n Oops! It seems you are trying to exit. Please use exit command\n";
Expand All @@ -23,6 +25,8 @@ int main(int argc, char *argv[])
{

signal(SIGINT, signal_handler);
create_tutorial_folder();

if (argc > 2)
{
cerr << "unexpected arguments\n";
Expand Down
117 changes: 117 additions & 0 deletions src/tutorial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <unistd.h>
#include <pwd.h>
#include "../include/parse.hpp"
#include "../include/launch.hpp"
#include "../include/levels.hpp"
#include "../include/tutorial.hpp"

using namespace std;

string home_dir;
extern bool unlocked[10];
extern int level;
int roosh_tutorial();

int create_tutorial_folder()
{
char *home;
struct passwd *pw = getpwuid(getuid());
home = strdup(pw->pw_dir);
home_dir = home;
roosh_launch("cp -r tutorial " + home_dir);
return 0;
}

int roosh_exec_tutorial(char **args, int num_args)
{
//number of arguments can be =>
// 1 i.e. tutorial
// 3 i.e. tutorial level <1-10>
// 4 i.e. tutorial level <1-10> password

//tutorial cmd
if (num_args == 1)
{

return roosh_tutorial();
}

// case of 3 or 4 arguments
else if ((num_args == 3 || num_args == 4) && (strcmp(args[1], "level") == 0))
{
if (num_args == 4)
{ //error if fourth arg is not password
if ((strcmp(args[3], "password") != 0))
{
cerr << "no such command exists\n";
return 1;
}
}

//parse the level value to int
int level;
stringstream ss;
ss << args[2];
ss >> level;

//level must be b/w 1-10
if (level > 0 && level <= 10 && (strlen(args[2]) == level / 10 + 1))
{

if (num_args == 3)
{
//display corresponding instructins
string path = (home_dir + "/tutorial/lvl_instrns/level" + args[2] + ".txt");
print_instructions(path);

//navigate to corresponding directory
path = (home_dir + "/tutorial/tutorial_levels/level" + args[2]);
chdir(path.c_str());
}

else
{ // for level 1 just check the password entered
if (level == 1)
{
check_password(level);
return 1;
}
//else first check whether the level is unlocked or not
else if (unlocked[level - 1])
{
check_password(level);
return 1;
}

else
{
print_no_access_to_level();
}
}
return 1;
}

else
{
//display error if level is not an integer bw 1 nd 10
cerr << "Please enter a level between 1 to 10.\n";
return 1;
}
}

else
{
cerr << "no such command exists\n";
return 1;
}
}

int roosh_tutorial()
{
cout << "My tutorial :)\n";
return 1;
}
2 changes: 2 additions & 0 deletions tutorial/lvl_instrns/level1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Commands you may need to solve the level
ls cd cat
Empty file.
4 changes: 4 additions & 0 deletions tutorial/lvl_instrns/level2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Commands you may need to solve the level
ls cd cat

Hint: ever heard about hidden files??
Empty file added tutorial/lvl_instrns/level3.txt
Empty file.
Empty file added tutorial/lvl_instrns/level4.txt
Empty file.
Empty file added tutorial/lvl_instrns/level5.txt
Empty file.
Empty file added tutorial/lvl_instrns/level6.txt
Empty file.
Empty file added tutorial/lvl_instrns/level7.txt
Empty file.
Empty file added tutorial/lvl_instrns/level8.txt
Empty file.
Empty file added tutorial/lvl_instrns/level9.txt
Empty file.
1 change: 1 addition & 0 deletions tutorial/tutorial_levels/level1/pass.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
XfwQrTY8drdyR8CDdpcgBSYq
1 change: 1 addition & 0 deletions tutorial/tutorial_levels/level2/.here.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The password is B7GXVdmXZGDgBXjUrP2aKhnc

0 comments on commit 096455b

Please sign in to comment.