Skip to content

Instantly share code, notes, and snippets.

@solomon081
Created July 3, 2012 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solomon081/3042917 to your computer and use it in GitHub Desktop.
Save solomon081/3042917 to your computer and use it in GitHub Desktop.
bash2cmd
#include "stdafx.h"
#include <iostream>
#include <string>
#include <regex>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;
using namespace std::tr1;
class Parser
{
regex ls_regex, mkdir_regex, stop_regex, rand_regex, rm_regex, touch_regex, cat_regex, open_regex, cp_regex, lat_regex, pwd_regex, wf_regex;
string touch_match, stop_match, mkdir_match, ls_match, rm_match, open_match, cp_match, pwd_match;
public:
void SetRegexes();
void RunCommand(string);
string ParseCommand(string);
string SearchAfter(int, string);
string GuardSpace(string);
};
void Parser::SetRegexes()
{
// Regex is the bash command, match is the cmd command
ls_regex = "^ls$";
ls_match = "dir";
mkdir_regex = "^mkdir";
mkdir_match = "md";
stop_regex = "^stop$";
stop_match = "pause";
rand_regex = "^timerand$";
rm_regex = "^rm";
rm_match = "del";
touch_regex = "^touch";
cat_regex = "^cat";
open_regex = "^open";
open_match = "notepad";
cp_regex = "^cp";
cp_match = "copy";
lat_regex = "^lat";
pwd_regex = "^pwd$";
pwd_match = "echo %CD%";
wf_regex = "^wf";
}
string Parser::ParseCommand(string unparsed)
{
// Checks if the regex can be found in the unparsed string
if (regex_search(unparsed, ls_regex))
{
// Unparsed now is actually parsed
unparsed = unparsed.replace(0, 3, ls_match);
}
else if (regex_search(unparsed, mkdir_regex))
{
unparsed = unparsed.replace(0, 5, mkdir_match);
}
else if (regex_search(unparsed, stop_regex))
{
unparsed = unparsed.replace(0, 4, stop_match);
}
else if (regex_search(unparsed, rand_regex))
{
srand(time(NULL));
cout << rand() << endl;
return " ";
}
else if (regex_search(unparsed, rm_regex))
{
unparsed = unparsed.replace(0, 2, rm_match);
}
else if (regex_search(unparsed, touch_regex))
{
string tfile;
tfile = SearchAfter(5, unparsed);
ofstream create_file(tfile);
return " ";
}
else if (regex_search(unparsed, cat_regex))
{
string op_line;
string catfile;
catfile = SearchAfter(3, unparsed);
ifstream input_file(catfile);
while (input_file.good())
{
getline(input_file, op_line);
cout << op_line << endl;
}
cout << endl;
return " ";
}
else if (regex_search(unparsed, open_regex))
{
unparsed = unparsed.replace(0, 5, open_match + " ");
}
else if (regex_search(unparsed, cp_regex))
{
unparsed = unparsed.replace(0, 4, cp_match + " ");
}
else if (regex_search(unparsed, lat_regex))
{
string latfile;
string op_line;
char holder;
latfile = SearchAfter(3, unparsed);
cout << latfile << endl << endl;
ifstream input_file(latfile);
while (input_file.good())
{
getline(input_file, op_line);
cout << op_line << endl;
// getch() makes it so the letters are not echoed to the keyboard
holder = getch();
}
return " ";
}
else if (regex_search(unparsed, pwd_regex))
{
unparsed = unparsed.replace(0, 3, "echo %CD%");
}
else if (regex_search(unparsed, wf_regex))
{
string wffile;
string input_text;
wffile = SearchAfter(2, unparsed);
ofstream output_file(wffile);
while (true)
{
getline(cin, input_text);
if (input_text == "^EOF")
{
break;
}
output_file << input_text << endl;
output_file.flush();
}
output_file.close();
return " ";
}
unparsed = GuardSpace(unparsed);
return unparsed;
}
void Parser::RunCommand(string parsed)
{
// Converts to a different type so it can be used in the system method
const char *cparsed = parsed.c_str();
system(cparsed);
}
string Parser::SearchAfter(int place, string to_search)
{
string to_return;
for (int i = 0; i < to_search.length(); i++)
{
if (i > place)
{
to_return += to_search[i];
}
}
return to_return;
}
string Parser::GuardSpace(string halfparsed)
{
if (halfparsed == "")
{
halfparsed += " ";
}
return halfparsed;
}
void _tmain(int argc, _TCHAR* argv[])
{
string name;
Parser main_parser;
string command;
int line_counter = 0;
main_parser.SetRegexes();
// Provides a never-ending command line interface
while (true)
{
line_counter++;
cout << "<-" << line_counter << name << "-> ";
//cin >> command;
getline(cin, command);
command = main_parser.ParseCommand(command);
main_parser.RunCommand(command);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment