2 * neatpp - a small and simple C preprocessor
4 * Copyright (C) 2010-2011 Ali Gholami Rudi
6 * This file is released under GNU GPL version 2.
17 #define OBUFSZ (1 << 19)
19 static int rmcomments(char *d
, char *s
, int l
)
25 if (r
+ 3 < e
&& r
[0] == '/' && r
[1] == '*') {
30 if (r
[0] == '*' && r
[1] == '/') {
37 if (r
+ 1 < e
&& (r
[0] == '\'' || r
[0] == '"')) {
40 while (r
< e
&& *r
!= c
) {
53 static int xwrite(int fd
, char *buf
, int len
)
57 int ret
= write(fd
, buf
+ nw
, len
- nw
);
58 if (ret
== -1 && (errno
== EAGAIN
|| errno
== EINTR
))
67 int main(int argc
, char *argv
[])
74 while (i
< argc
&& argv
[i
][0] == '-') {
75 if (argv
[i
][1] == 'I')
76 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
77 if (argv
[i
][1] == 'D') {
78 char *name
= argv
[i
] + 2;
80 char *eq
= strchr(name
, '=');
85 cpp_define(name
, def
);
90 printf("usage: npp [-I idir] [-D define] input output\n");
93 if (cpp_init(argv
[i
++]))
94 die("npp: cannot open input file\n");
95 ofd
= open(argv
[i
++], O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
97 die("npp: cannot open output file\n");
101 die("npp: cannot allocate enough memory\n");
102 while ((nr
= cpp_read(s1
+ len
)) >= 0)
104 len
= rmcomments(s2
, s1
, len
);
105 xwrite(ofd
, s2
, len
);