start parrse

This commit is contained in:
Ben Harris 2016-11-04 16:52:10 -04:00
parent 90aa337d31
commit 21a5e7c1f9
1 changed files with 59 additions and 6 deletions

65
bish.cc
View File

@ -6,15 +6,18 @@
#include <vector>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sstream>
#include <stdio.h>
#include <pwd.h>
#include <readline/readline.h>
#include <readline/history.h>
using namespace std;
// util methods
vector<string> split(const char *str, char c = ' '){
vector<string> split(const char *str, char c = ' ') {
vector<string> result;
do {
const char *begin = str;
@ -35,6 +38,49 @@ char** v_to_cpp(vector<string> vargs) {
return args;
}
// command struct
struct command {
char*** args;
bool background;
string piping;
string infile;
string outfile;
};
command parse(vector<string> args) {
command parseinfo;
bool in_flag = false, out_flag = false, piping_flag = false;
vector<string> cmd;
for (auto it: args) {
if (it == "<") in_flag = true;
if (in_flag) {
in_flag = false;
parseinfo.infile = it;
}
if (it == ">") out_flag = true;
if (out_flag) {
out_flag = false;
parseinfo.outfile = it;
}
if (it == "|") piping_flag = true;
if (piping_flag) {
piping_flag = false;
parseinfo.piping = it;
}
if (it == args.back() && it == "&") {
parseinfo.background = true;
}
cmd.push_back(it);
}
parseinfo.args[0] = v_to_cpp(cmd);
return parseinfo;
}
int main(int argc, char **argv){
@ -43,11 +89,18 @@ int main(int argc, char **argv){
static char* line = (char*)NULL;
vector<string> path = split(getenv("PATH"), ':');
const char *homedir;
if ((homedir = getenv("HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir;
}
// set up history
using_history();
if (read_history(".bish_history")) {
printf("history file not found. creating `.bish_history` in current directory.\n");
cout << "(" << system("touch .bish_history") << "):";
string histpath = string(homedir) + string("/.bish_history");
if (read_history(histpath.c_str())) {
printf("history file not found. creating `~/.bish_history`.\n");
int hist = open(histpath.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
close(hist);
}
while ("bish") {
@ -69,7 +122,7 @@ int main(int argc, char **argv){
// handle chdir
else if (strcmp(args[0], "cd") == 0) {
if (args[1] == NULL) {
if (chdir("/") < 0) perror("chdir");
if (chdir(homedir) < 0) perror("chdir");
}
else {
if (chdir(args[1]) < 0) perror("chdir");
@ -135,7 +188,7 @@ int main(int argc, char **argv){
}
printf("\n");
if (write_history(".bish_history")) perror("write_history");
if (write_history(histpath.c_str())) perror("write_history");
return 0;
}