2 * neatpp - a small and simple C preprocessor
4 * Copyright (C) 2010-2014 Ali Gholami Rudi
6 * This program is released under the Modified BSD license.
18 #define OBUFSZ (1 << 19)
20 static int rmcomments(char *d
, char *s
, int l
)
26 if (r
+ 3 < e
&& r
[0] == '/' && r
[1] == '*') {
31 if (r
[0] == '*' && r
[1] == '/') {
38 if (r
+ 1 < e
&& (r
[0] == '\'' || r
[0] == '"')) {
41 while (r
< e
&& *r
!= c
) {
54 static int xwrite(int fd
, char *buf
, int len
)
58 int ret
= write(fd
, buf
+ nw
, len
- nw
);
59 if (ret
== -1 && (errno
== EAGAIN
|| errno
== EINTR
))
68 void err(char *fmt
, ...)
73 vsprintf(msg
, fmt
, ap
);
78 int main(int argc
, char *argv
[])
86 while (i
< argc
&& argv
[i
][0] == '-') {
87 if (argv
[i
][1] == 'I')
88 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
89 if (argv
[i
][1] == 'D') {
90 char *name
= argv
[i
] + 2;
92 char *eq
= strchr(name
, '=');
97 cpp_define(name
, def
);
102 printf("usage: npp [-I idir] [-D define] input output\n");
105 if (cpp_init(argv
[i
++]))
106 die("npp: cannot open <%s>\n", argv
[i
- 1]);
107 ofd
= open(argv
[i
++], O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
109 die("npp: cannot open <%s>\n", argv
[i
- 1]);
113 die("npp: cannot allocate enough memory\n");
114 while (!cpp_read(&cbuf
, &clen
)) {
115 memcpy(s1
+ len
, cbuf
, clen
);
118 len
= rmcomments(s2
, s1
, len
);
119 xwrite(ofd
, s2
, len
);