Skip to content

Commit

Permalink
Merge pull request JaiMaaSheeravali#13 from JaiMaaSheeravali/input-fix
Browse files Browse the repository at this point in the history
Input fixed and builtin arguments
  • Loading branch information
guptaprakhariitr committed Apr 30, 2021
2 parents 14fbc2c + ceba9e9 commit e65096a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 37 deletions.
8 changes: 4 additions & 4 deletions include/builtin.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once
#include <string>

int roosh_cd(char **args);
int roosh_history(char **args);
int roosh_exit(char **args);
int roosh_rsh(char **args);
int roosh_cd(char **args, int num_args);
int roosh_history(char **args, int num_args);
int roosh_exit(char **args, int num_args);
int roosh_rsh(char **args, int num_args);

void push_command(std::string cmd);
17 changes: 17 additions & 0 deletions include/color.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
49 changes: 38 additions & 11 deletions src/builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <vector>
#include <string>
#include <iomanip>
#include <sys/types.h>
#include <pwd.h>

#include "../include/parse.hpp"
#include "../include/launch.hpp"
Expand All @@ -14,21 +16,39 @@ using namespace std;
// vector to store list of commands
vector<string> list_cmds;

int roosh_cd(char **args)
// Function to print error message when
void invalid_arg_count_error(int num_args, int required_args){
cerr << "Error: Invalid number of Arguments(" << num_args-1 << " given) - " << required_args << " Required!\n";
}

int roosh_cd(char **args, int num_args)
{
// number of arguments must be exactly two
// first one is "cd"
// second is directory path
if (args[1] == NULL)
if (num_args == 1)
{
cerr << "cd: unexpected number of arguments\n";
cerr << "Error cd: Target directory address not given\n";
return 1;
}

// Display error if extra arguments are given
if(num_args > 2){
invalid_arg_count_error(num_args, 1);
return 1;
}

// change args[1] by home directory location if "cd ~" is used
if(strcmp(args[1], "~") == 0){

struct passwd *pw = getpwuid(getuid());
args[1] = strdup(pw->pw_dir);
}

// if chdir failed print the error_message
if (chdir(args[1]) == -1)
{
cerr << "cd: " << args[1] << ": No such file or directory\n";
cerr << "Error! " << args[1] << ": No such directory exits\n";
}
return 1;
}
Expand Down Expand Up @@ -62,16 +82,22 @@ void roosh_batch_loop(std::istream &in)
}
}

int roosh_rsh(char **args)
int roosh_rsh(char **args, int num_args)
{

// Produce error if no file is specified.
if (args[1] == NULL)
if (num_args == 1)
{
cerr << "Batch Mode: No file specified to run in Batch Mode\n";
return 1;
}

// Display error if extra arguments are given
if(num_args > 2){
invalid_arg_count_error(num_args, 1);
return 1;
}

// Check if extension of file is correct or not
int slen = strlen(args[1]);
char buf[5];
Expand Down Expand Up @@ -117,13 +143,13 @@ void push_command(string line)
}

// prints the commands ran so far
int roosh_history(char **args)
int roosh_history(char **args, int num_args)
{
// only one argument i.e history
// is expected
if (args[1] != NULL)
if (num_args > 1)
{
cerr << "error history: unexpected number of arguments\n";
invalid_arg_count_error(num_args, 0);
return 1;
}

Expand All @@ -138,11 +164,12 @@ int roosh_history(char **args)
return 1;
}

int roosh_exit(char **args)
int roosh_exit(char **args, int num_args)
{
// only one arg should be given
if (args[1] != NULL)
if (num_args > 1)
{
invalid_arg_count_error(num_args, 0);
return 1;
}

Expand Down
44 changes: 25 additions & 19 deletions src/input.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,67 @@
#include <unistd.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

#include <../include/color.hpp>

using namespace std;

void print_git_branch();

void print_input_format()
{
cout << flush;

char hostname[50];
struct passwd *p = getpwuid(getuid());

// Get the host name and user name of current user
if (gethostname(hostname, sizeof(hostname)) == 0 && p)
{
printf("\033[;34m%s@", p->pw_name);
printf("%s\033[0m:", hostname);
cout << BLUE << p->pw_name << "@";
cout << hostname << RESET;
}

// Print the current working directory of user
char cwd[100];

cout << GREEN;
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
printf("\033[;32m%s\033[0m", cwd);
}
cout << cwd;
else
{
printf("\033[;32m.\033[0m");
}
cout << ".";
cout << RESET;

// Get git branch if current directory is a git directory
print_git_branch();
printf("");
cout << "" << flush;
}

void print_git_branch()
{
FILE *fp;
char path[400];
char branch_name[400];

// Get current branch and save its name in a file.
fp = popen("git branch --show-current 2>/dev/null", "r");

if (fp == NULL)
if (fp == NULL){
pclose(fp);
return;
}

// Read branch name and print current branch name
fgets(path, 200, fp);
path[strcspn(path, "\n")] = 0;
fgets(branch_name, 200, fp);
branch_name[strcspn(branch_name, "\n")] = 0;

// Should check status if any error occurs
pclose(fp);

if (path && strcmp(path, "") == 0)
if (branch_name && strcmp(branch_name, "") == 0)
return;

// print current branch name in red enclosed in round braces
printf(" \033[31m(%s)\033[0m", path);

// Should check status if any error occurs
pclose(fp);
cout << RED << " (" << branch_name << ")" << RESET;
}
6 changes: 3 additions & 3 deletions src/launch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const char *builtin_list[] = {
"cd",
"history",
"exit",
"rsh"};
"roosh"};

int (*builtin_func[])(char **) = {
int (*builtin_func[])(char **, int) = {
&roosh_cd,
&roosh_history,
&roosh_exit,
Expand Down Expand Up @@ -57,7 +57,7 @@ bool roosh_launch(char **args, int num_args)
{
if (strcmp(builtin_list[i], args[0]) == 0)
{
return (*builtin_func[i])(args);
return (*builtin_func[i])(args, num_args);
}
}

Expand Down

0 comments on commit e65096a

Please sign in to comment.