-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (73 loc) · 1.79 KB
/
main.cpp
File metadata and controls
81 lines (73 loc) · 1.79 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
#include <vector>
#include "Job.h"
#include "Pipe.h"
int main()
{
std::vector<std::filesystem::path> pathes;
try
{
pathes = GetPathes();
}
catch (const std::runtime_error &e)
{
fprintf(stderr, "GetPathes error, %s\n", e.what());
return EXIT_FAILURE;
}
while (true)
{
fputs("> ", stdout);
std::vector<char> buff(1024);
if (!fgets(buff.data(), buff.size(), stdin))
{
break;
}
Job job = ParseJob(buff.data());
try
{
job = ResolveCommandPath(pathes, job);
}
catch (const std::runtime_error &e)
{
fprintf(stderr, "ResolveCommandPath error, %s\n", e.what());
return EXIT_FAILURE;
}
FILE *outfile = nullptr;
try
{
outfile = PipeCommand(stdin, job);
}
catch (const std::runtime_error &e)
{
fprintf(stderr, "PipeCommand error, %s\n", e.what());
return EXIT_FAILURE;
}
if (!job.redirectFilename.empty())
{
try
{
Redirect(outfile, job.redirectFilename);
}
catch (const std::runtime_error &e)
{
fclose(outfile);
fprintf(stderr, "Redirect error, %s\n", e.what());
return EXIT_FAILURE;
}
}
else
{
try
{
WriteStdout(outfile);
}
catch (const std::runtime_error &e)
{
fclose(outfile);
fprintf(stderr, "WriteStdout error, %s\n", e.what());
return EXIT_FAILURE;
}
}
fclose(outfile);
}
return EXIT_SUCCESS;
}