-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
135 lines (133 loc) · 2.95 KB
/
main.cpp
File metadata and controls
135 lines (133 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <string>
#include "rosetta.hpp"
#include <sys/wait.h>
#include "languagelist.hpp"
//#include "childproc.hpp"
#define VERSION "0.0.1.0"
int main(int argc, char *argv[])
{
std::string mrkdwn = "";
std::string language = "";
bool cleanup = true;
bool verbose = false;
if(argc == 1)
{
std::cout << "Please give arguments, or use -h to see help" << std::endl;
return 0;
}
for(int i = 1; i<argc; i++)
{
std::string arg = std::string(argv[i]);
if(arg[0] == '-'){
if(arg == "-h")
{
std::cout << "Rosetta is a tool that lets you run code from markdown files."
<< "\nUsage : \n"
<< " rosetta mardownfile language\n"
<< "Others flags\n"
<< "> -h : Shows help\n"
<< "> -V : Shows version\n"
<< "> -v : verbose\n"
<< "> -km : keep middle files\n"
<< "\nYou can set up the compilers in ~/.config/rosetta/config.txt. If no such file is found, it will fall back on a few default setting, including cpp, python and ipython\n"
<< std::endl;
}
if(arg == "-V")
{
std::cout << "ROSETTA version : " << VERSION << std::endl;
}
if(arg == "-v")
{
verbose = true;
}
if(arg == "-km")
{
cleanup = false;
}
}
else
{
if(mrkdwn == "")
{
mrkdwn = arg;
if(!findFile(mrkdwn))
{
std::cout << "Error : File Not Found at " << mrkdwn << std::endl;
return 0;
}
}
else if(language == "")
{
language = arg;
}
else
{
std::cout << "Too many arguments";
return 0;
}
}
}
//true start
if(mrkdwn == "")
{
std::cout << "No file given" << std::endl;
return 0;
}
if(language == "")
{
std::cout << "No language given" << std::endl;
}
lgn l = findLang(language);
if(l.id == "")
{
return 0;
}
createTempFile(mrkdwn, l);
int f = fork();
if(f < 0)
{
std::cout << "fork failed" << std::endl;
}
if(f == 0)
{
char* program = l.exec.data();
char *args[l.args.size()+2] = {0};
for(int i = 0; i<l.args.size()+1; i++)
{
if(i > 0){
if(l.args[i-1] != "¤")
{
args[i] = l.args[i-1].data();
}
else {
args[i] = ("temp"+l.ext).data();
}
}
else
{
args[i] = l.exec.data();
}
if(verbose)
{
std::cout << "Args " << i << " = " << args[i] << std::endl;
}
}
args[l.args.size()+1] = nullptr;
execvp(program, args);
}
wait(&f);
if(verbose)
{
std::cout << "child process ended" << std::endl;
}
if(cleanup)
{
if(verbose)
{
std::cout << "Cleaning middle files" << std::endl;
}
std::remove( ("temp"+l.ext).data() );
}
return 0;
}