This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
lcpp
executable file
·87 lines (73 loc) · 2.13 KB
/
lcpp
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
#!/usr/bin/env lua
-- Copyright (c) Facebook, Inc. and its affiliates.
-- This source code is licensed under the MIT license found in the
-- LICENSE file in the root directory of this source tree.
--
-- Front-end for the Lua C preprocessor.
local function smartrequire(s)
local savedpath=package.path
local thisfile=debug.getinfo(2).source
if type(thisfile)=='string' and thisfile:find("^@") then
local thisdir=thisfile:gsub("^@",""):gsub("[^/]*$","")
package.path=thisdir.."?.lua;"..package.path
end
local r = require(s)
package.path = savedpath
return r
end
cparser = smartrequire 'cparser'
io = require 'io'
local function die(s,...)
if s then print(string.format(s,...)) end
error(nil)
end
local function usage()
die([[
Usage: lcpp [options] inputfile.c [-o outputfile.c]
Main options:
-Idirname : Add dirname to the include search path
-I- : Marks beginning of system include search path
-Dsym[=value] : Define a macro symbol.
-Usym : Undefine a macro symbol.
-Zcppdef : Extracts initial macro definitions from cpp.
-Znopass : Do not copy unrecognized preprocessor directives.
-dM : Dump final macro definitions.
]])
end
local options={}
local outputfile
local inputfile
local outputi
for i,v in ipairs{...} do
if outputi == i then
outputfile = v
elseif v == '-o' and not outputfile then
outputi = i+1
elseif v:find("^-") then
options[1+#options] = v
elseif not inputfile then
inputfile = v
else
usage()
end
end
local function exists(fname)
local fd = io.open(fname,"r")
if fd then fd:close() return true end
return false
end
if not inputfile then
usage()
elseif not exists(inputfile) then
die("cparser: cannot read file '%s'", inputfile)
end
local outputfd
if outputfile then
outputfd = io.open(outputfile,"w")
if not outputfd then
die("cparser: cannot open '%s' for writing", outputfile)
end
end
local s,m = pcall(cparser.cpp, inputfile, outputfd, options)
if io.type(outputfd)=='file' then outputfd:close() end
if not s then die( not m:find("aborted") and m ) end