forked from facebookarchive/warp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.d
More file actions
109 lines (86 loc) · 2.7 KB
/
Copy pathmain.d
File metadata and controls
109 lines (86 loc) · 2.7 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
/**
* C preprocessor
* Copyright: 2013 by Digital Mars
* License: $(LINK2 http://boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Walter Bright
*/
import std.array;
import std.format;
import std.stdio;
import core.stdc.stdlib;
import core.memory;
import cmdline;
import context;
import loc;
import sources;
// Data type for C source code characters
alias ubyte uchar;
alias immutable(uchar)[] ustring;
extern (C) int isatty(int);
alias typeof(File.lockingTextWriter()) R;
version (unittest)
{
int main() { writeln("unittests successful"); return EXIT_SUCCESS; }
}
else
{
int main(string[] args)
{
// No need to collect
GC.disable();
const params = parseCommandLine(args);
auto context = Context!R(params);
try
{
// Preprocess each file
foreach (i; 0 .. params.sourceFilenames.length)
{
if (i)
context.reset();
auto srcFilename = params.sourceFilenames[i];
auto outFilename = params.stdout ? "-" : params.outFilenames[i];
if (context.params.verbose)
writefln("from %s to %s", srcFilename, outFilename);
auto sf = SrcFile.lookup(srcFilename);
if (!sf.read())
err_fatal("cannot read file %s", srcFilename);
if (context.doDeps)
context.deps ~= srcFilename;
scope(failure) if (!params.stdout) std.file.remove(outFilename);
auto fout = params.stdout ? stdout : File(outFilename, "wb");
if (!isatty(fout.fileno))
fout.setvbuf(0x100000);
auto foutr = fout.lockingTextWriter(); // has destructor
context.localStart(sf, &foutr);
context.preprocess();
context.localFinish();
/* The one source file we don't need to cache the contents
* of is the .c file.
*/
sf.freeContents();
}
}
catch (Exception e)
{
context.loc().write(&stderr);
stderr.writeln(e.msg);
exit(EXIT_FAILURE);
}
context.globalFinish();
exit(EXIT_SUCCESS); // this prevents the collector from running on exit
// (it also prevents -profile from working)
return EXIT_SUCCESS;
}
}
void err_fatal(T...)(T args)
{
auto app = appender!string();
app.formattedWrite(args);
throw new Exception(app.data);
}
void err_warning(T...)(Loc loc, T args)
{
loc.write(&stderr);
stderr.write("warning: ");
stderr.writefln(args);
}