From 1d4999df168c560faee1e352355772ac2ee1bc35 Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Sat, 19 Feb 2011 20:21:28 +0330 Subject: [PATCH] npp: allow using neatcc's preprocessor as a standalone program --- Makefile | 8 +++++--- cpp.c | 6 ++++++ ncc.c | 15 +++------------ npp.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ tok.c | 2 -- tok.h | 1 + 6 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 npp.c diff --git a/Makefile b/Makefile index 6cc1095..4ec6a99 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,14 @@ CC = cc -CFLAGS = -Wall -Os -g -LDFLAGS = -g +CFLAGS = -Wall -Os +LDFLAGS = -all: ncc +all: ncc npp .c.o: $(CC) -c $(CFLAGS) $< ncc: ncc.o tok.o gen.o out.o cpp.o tab.o $(CC) $(LDFLAGS) -o $@ $^ +npp: npp.o cpp.o tab.o + $(CC) $(LDFLAGS) -o $@ $^ clean: rm -f cc *.o diff --git a/cpp.c b/cpp.c index b8f619c..b55ff1f 100644 --- a/cpp.c +++ b/cpp.c @@ -52,6 +52,12 @@ static struct buf { } bufs[MAXBUFS]; static int nbufs; +void die(char *msg) +{ + write(2, msg, strlen(msg)); + exit(1); +} + static void buf_new(int type, char *dat, int dlen) { if (nbufs) { diff --git a/ncc.c b/ncc.c index ad15f9f..03b1f88 100644 --- a/ncc.c +++ b/ncc.c @@ -1,11 +1,9 @@ /* - * neatcc - A small and simple C compiler + * neatcc - a small and simple C compiler * - * Copyright (C) 2010 Ali Gholami Rudi + * Copyright (C) 2010-2011 Ali Gholami Rudi * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License, as published by the - * Free Software Foundation. + * This file is released under GNU GPL version 2. */ #include #include @@ -19,7 +17,6 @@ #define MAXLOCALS (1 << 10) #define MAXGLOBALS (1 << 10) #define MAXARGS (1 << 5) -#define print(s) write(2, (s), strlen(s)); #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt) #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK) @@ -64,12 +61,6 @@ static void ts_pop(struct type *type) *type = ts[nts]; } -void die(char *msg) -{ - print(msg); - exit(1); -} - void err(char *msg) { char err[1 << 7]; diff --git a/npp.c b/npp.c new file mode 100644 index 0000000..7f70b03 --- /dev/null +++ b/npp.c @@ -0,0 +1,49 @@ +/* + * neatpp - a small and simple C preprocessor + * + * Copyright (C) 2010-2011 Ali Gholami Rudi + * + * This file is released under GNU GPL version 2. + */ +#include +#include +#include +#include +#include +#include "tok.h" + +int main(int argc, char *argv[]) +{ + int ofd = 1; + int i = 1; + char buf[BUFSIZE]; + int nr; + while (i < argc && argv[i][0] == '-') { + if (argv[i][1] == 'I') + cpp_addpath(argv[i][2] ? argv[i] + 2 : argv[++i]); + if (argv[i][1] == 'D') { + char *name = argv[i] + 2; + char *def = ""; + char *eq = strchr(name, '='); + if (eq) { + *eq = '\0'; + def = eq + 1; + } + cpp_define(name, def); + } + i++; + } + if (i + 1 >= argc) { + printf("usage: npp [-I idir] [-D define] input output\n"); + return 0; + } + if (cpp_init(argv[i++])) + die("neatcc: cannot open input file\n"); + ofd = open(argv[i++], O_WRONLY | O_TRUNC | O_CREAT, 0600); + if (ofd < 0) + die("%s: cannot open output file\n"); + while ((nr = cpp_read(buf)) >= 0) + write(ofd, buf, nr); + close(ofd); + return 0; +} diff --git a/tok.c b/tok.c index 721bab0..3b1604f 100644 --- a/tok.c +++ b/tok.c @@ -5,8 +5,6 @@ #include "gen.h" #include "tok.h" -extern int cpp_read(char *s); - static char buf[BUFSIZE]; static int len; static int cur; diff --git a/tok.h b/tok.h index 8b1d297..1a56a45 100644 --- a/tok.h +++ b/tok.h @@ -51,5 +51,6 @@ int cpp_init(char *path); void cpp_addpath(char *s); void cpp_define(char *name, char *def); int cpp_loc(char *s, long offset); +int cpp_read(char *s); void die(char *msg); -- 2.11.4.GIT