bish/parse.cc

75 lines
1.7 KiB
C++
Raw Permalink Normal View History

2016-11-07 17:43:10 +00:00
// Parser stuff for bish
2016-11-07 17:13:59 +00:00
#include <string>
#include <vector>
#include <iostream>
#include "parse.h"
#include "util_fns.h"
using namespace std;
void print_cmd(command *cmd) {
2016-11-12 02:49:32 +00:00
int i = 0;
2016-11-14 21:59:30 +00:00
cout << "-----------------------------" << endl;
2016-11-12 02:49:32 +00:00
for (auto cmd_iter: cmd->cmds) {
if (cmd->background) cout << "backgroud: true" << endl;
2016-11-16 17:07:43 +00:00
cout << "command " << ++i << endl;
if (cmd_iter.infile != "") cout << "infile: " << cmd_iter.infile << endl;
if (cmd_iter.outfile != "") cout << "outfile: " << cmd_iter.outfile << endl << endl;
2016-11-14 21:59:30 +00:00
cout << ">>\t";
2016-11-12 02:49:32 +00:00
for (auto scmd_iter: cmd_iter.vargs) {
2016-11-14 21:59:30 +00:00
cout << "\"" << scmd_iter << "\" ";
2016-11-12 02:49:32 +00:00
}
2016-11-14 21:59:30 +00:00
cout << endl << endl;
2016-11-12 02:49:32 +00:00
}
2016-11-14 21:59:30 +00:00
cout << "-----------------------------" << endl;
2016-11-07 17:13:59 +00:00
}
command *parse(vector<string> args) {
2016-11-11 04:29:05 +00:00
2016-11-12 02:49:32 +00:00
command *parsed_command = new command();
bool in_flag = false, out_flag = false;
simple_command *current_simple = new simple_command();
2016-11-07 17:13:59 +00:00
2016-11-12 02:49:32 +00:00
for (auto iter: args) {
if (iter == "<") {
2016-11-07 17:13:59 +00:00
in_flag = true;
continue;
}
else if (in_flag) {
in_flag = false;
2016-11-12 02:49:32 +00:00
current_simple->infile = iter;
2016-11-07 17:13:59 +00:00
}
2016-11-12 02:49:32 +00:00
else if (iter == ">") {
2016-11-07 17:13:59 +00:00
out_flag = true;
continue;
}
else if (out_flag) {
out_flag = false;
2016-11-12 02:49:32 +00:00
current_simple->outfile = iter;
2016-11-07 17:13:59 +00:00
}
2016-11-12 02:49:32 +00:00
else if (iter == "|") {
parsed_command->cmds.push_back(*current_simple);
delete current_simple;
current_simple = new simple_command();
2016-11-07 17:13:59 +00:00
continue;
}
2016-11-12 02:49:32 +00:00
else if (iter == args.back() && iter == "&") {
parsed_command->background = true;
2016-11-07 17:13:59 +00:00
}
2016-11-11 04:29:05 +00:00
else {
2016-11-12 02:49:32 +00:00
current_simple->vargs.push_back(iter);
2016-11-11 04:29:05 +00:00
}
}
2016-11-12 02:49:32 +00:00
parsed_command->cmds.push_back(*current_simple);
return parsed_command;
2016-11-07 17:13:59 +00:00
}