Separating the code between a binary and a shared library
This commit is contained in:
parent
b9c7a7718b
commit
86646c34c2
5
Makefile
5
Makefile
|
@ -1,6 +1,7 @@
|
||||||
PACKAGE = cpp
|
PACKAGE = cpp
|
||||||
VERSION = 0.0.0
|
VERSION = 0.0.0
|
||||||
SUBDIRS = src
|
SUBDIRS = src
|
||||||
|
RM = rm -f
|
||||||
LN = ln -sf
|
LN = ln -sf
|
||||||
TAR = tar -czvf
|
TAR = tar -czvf
|
||||||
|
|
||||||
|
@ -20,8 +21,12 @@ dist:
|
||||||
$(RM) -r $(PACKAGE)-$(VERSION)
|
$(RM) -r $(PACKAGE)-$(VERSION)
|
||||||
$(LN) . $(PACKAGE)-$(VERSION)
|
$(LN) . $(PACKAGE)-$(VERSION)
|
||||||
@$(TAR) $(PACKAGE)-$(VERSION).tar.gz \
|
@$(TAR) $(PACKAGE)-$(VERSION).tar.gz \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/parser.c \
|
||||||
$(PACKAGE)-$(VERSION)/src/cpp.c \
|
$(PACKAGE)-$(VERSION)/src/cpp.c \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/main.c \
|
||||||
$(PACKAGE)-$(VERSION)/src/project.conf \
|
$(PACKAGE)-$(VERSION)/src/project.conf \
|
||||||
|
$(PACKAGE)-$(VERSION)/Makefile \
|
||||||
|
$(PACKAGE)-$(VERSION)/COPYING \
|
||||||
$(PACKAGE)-$(VERSION)/project.conf
|
$(PACKAGE)-$(VERSION)/project.conf
|
||||||
$(RM) $(PACKAGE)-$(VERSION)
|
$(RM) $(PACKAGE)-$(VERSION)
|
||||||
|
|
||||||
|
|
47
src/Makefile
47
src/Makefile
|
@ -1,11 +1,16 @@
|
||||||
TARGETS = cpp
|
TARGETS = libcpp.a libcpp.so cpp
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
DESTDIR =
|
DESTDIR =
|
||||||
BINDIR = $(PREFIX)/bin
|
LIBDIR = $(PREFIX)/lib
|
||||||
INCLUDEDIR= $(PREFIX)/include
|
|
||||||
CC = cc
|
CC = cc
|
||||||
|
CPPFLAGS=
|
||||||
CFLAGSF = -W -Wall
|
CFLAGSF = -W -Wall
|
||||||
CFLAGS = -g
|
CFLAGS = -g
|
||||||
|
AR = ar -rc
|
||||||
|
RANLIB = ranlib
|
||||||
|
LD = ld -shared
|
||||||
|
BINDIR = $(PREFIX)/bin
|
||||||
|
INCLUDEDIR= $(PREFIX)/include
|
||||||
RM = rm -f
|
RM = rm -f
|
||||||
MKDIR = mkdir -p
|
MKDIR = mkdir -p
|
||||||
INSTALL = install
|
INSTALL = install
|
||||||
|
@ -13,25 +18,47 @@ INSTALL = install
|
||||||
|
|
||||||
all: $(TARGETS)
|
all: $(TARGETS)
|
||||||
|
|
||||||
cpp_OBJS = cpp.o
|
libcpp_OBJS = parser.o cpp.o
|
||||||
cpp_CFLAGS = $(CFLAGSF) $(CFLAGS)
|
libcpp_CFLAGS = $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) -fPIC
|
||||||
cpp: $(cpp_OBJS)
|
|
||||||
$(CC) $(LDFLAGSF) $(LDFLAGS) -o cpp $(cpp_OBJS)
|
|
||||||
|
|
||||||
cpp.o: cpp.c
|
libcpp.a: $(libcpp_OBJS)
|
||||||
$(CC) $(cpp_CFLAGS) -c cpp.c
|
$(AR) libcpp.a $(libcpp_OBJS)
|
||||||
|
$(RANLIB) libcpp.a
|
||||||
|
|
||||||
|
libcpp.so: $(libcpp_OBJS)
|
||||||
|
$(LD) -o libcpp.so $(libcpp_OBJS)
|
||||||
|
|
||||||
|
cpp_OBJS = main.o
|
||||||
|
cpp_CFLAGS = $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
|
||||||
|
|
||||||
|
cpp: $(cpp_OBJS)
|
||||||
|
$(CC) -o cpp $(cpp_OBJS) $(LDFLAGSF) $(LDFLAGS) -l cpp -L .
|
||||||
|
|
||||||
|
parser.o: parser.c parser.h
|
||||||
|
$(CC) $(libcpp_CFLAGS) -c parser.c
|
||||||
|
|
||||||
|
cpp.o: cpp.c parser.h cpp.h
|
||||||
|
$(CC) $(libcpp_CFLAGS) -c cpp.c
|
||||||
|
|
||||||
|
main.o: main.c cpp.h
|
||||||
|
$(CC) $(cpp_CFLAGS) -c main.c
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) $(cpp_OBJS)
|
$(RM) $(libcpp_OBJS) $(cpp_OBJS)
|
||||||
|
|
||||||
distclean: clean
|
distclean: clean
|
||||||
$(RM) $(TARGETS)
|
$(RM) $(TARGETS)
|
||||||
|
|
||||||
install: all
|
install: all
|
||||||
|
$(MKDIR) $(DESTDIR)$(LIBDIR)
|
||||||
|
$(INSTALL) -m 0644 libcpp.a $(DESTDIR)$(LIBDIR)/libcpp.a
|
||||||
|
$(INSTALL) -m 0755 libcpp.so $(DESTDIR)$(LIBDIR)/libcpp.so
|
||||||
$(MKDIR) $(DESTDIR)$(BINDIR)
|
$(MKDIR) $(DESTDIR)$(BINDIR)
|
||||||
$(INSTALL) -m 0755 cpp $(DESTDIR)$(BINDIR)/cpp
|
$(INSTALL) -m 0755 cpp $(DESTDIR)$(BINDIR)/cpp
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
|
$(RM) $(DESTDIR)$(LIBDIR)/libcpp.a
|
||||||
|
$(RM) $(DESTDIR)$(LIBDIR)/libcpp.so
|
||||||
$(RM) $(DESTDIR)$(BINDIR)/cpp
|
$(RM) $(DESTDIR)$(BINDIR)/cpp
|
||||||
|
|
||||||
.PHONY: all clean distclean install uninstall
|
.PHONY: all clean distclean install uninstall
|
||||||
|
|
89
src/cpp.c
89
src/cpp.c
|
@ -17,56 +17,77 @@
|
||||||
|
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include "parser.h"
|
||||||
|
#include "cpp.h"
|
||||||
|
|
||||||
|
|
||||||
/* cpp */
|
/* Cpp */
|
||||||
static int _cpp_error(char const * message, int ret);
|
/* private */
|
||||||
static int _cpp(char const * filename)
|
/* types */
|
||||||
|
struct _Cpp
|
||||||
{
|
{
|
||||||
FILE * fp;
|
Parser ** parser;
|
||||||
char buf[BUFSIZ];
|
size_t parser_cnt;
|
||||||
size_t len;
|
char error[PATH_MAX];
|
||||||
int ret = 0;
|
};
|
||||||
|
|
||||||
if((fp = fopen(filename, "r")) == NULL)
|
|
||||||
return _cpp_error(filename, 1);
|
|
||||||
while((len = fread(buf, sizeof(char), sizeof(buf), fp)) > 0)
|
|
||||||
fwrite(buf, sizeof(char), len, stdout);
|
|
||||||
if(len == 0 && !feof(fp))
|
|
||||||
ret = _cpp_error(filename, 1);
|
|
||||||
fclose(fp);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int _cpp_error(char const * message, int ret)
|
/* public */
|
||||||
|
/* cpp_new */
|
||||||
|
Cpp * cpp_new(void)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s", "cpp: ");
|
Cpp * cpp;
|
||||||
perror(message);
|
|
||||||
return ret;
|
if((cpp = malloc(sizeof(*cpp))) == NULL)
|
||||||
|
return NULL;
|
||||||
|
memset(cpp, sizeof(*cpp), 0);
|
||||||
|
return cpp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* usage */
|
/* cpp_delete */
|
||||||
static int _usage(void)
|
void cpp_delete(Cpp * cpp)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s", "Usage: cpp filename\n");
|
size_t i;
|
||||||
return 1;
|
|
||||||
|
for(i = 0; i < cpp->parser_cnt; i++)
|
||||||
|
parser_delete(cpp->parser[i]);
|
||||||
|
free(cpp->parser);
|
||||||
|
free(cpp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* main */
|
/* accessors */
|
||||||
int main(int argc, char * argv[])
|
/* cpp_get_error */
|
||||||
|
char const * cpp_get_error(Cpp * cpp)
|
||||||
{
|
{
|
||||||
int o;
|
return cpp->error;
|
||||||
|
}
|
||||||
|
|
||||||
while((o = getopt(argc, argv, "")) != -1)
|
|
||||||
switch(o)
|
/* useful */
|
||||||
|
int cpp_parse(Cpp * cpp, char const * pathname)
|
||||||
{
|
{
|
||||||
default:
|
Parser ** p;
|
||||||
return _usage();
|
|
||||||
|
if((p = realloc(cpp->parser, (cpp->parser_cnt + 1)
|
||||||
|
* sizeof(*p))) == NULL)
|
||||||
|
return 1; /* FIXME get error */
|
||||||
|
cpp->parser = p;
|
||||||
|
if((cpp->parser[cpp->parser_cnt] = parser_new(pathname)) == NULL)
|
||||||
|
return 1; /* FIXME get error */
|
||||||
|
cpp->parser_cnt++;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
if(argc - optind != 1)
|
|
||||||
return _usage();
|
|
||||||
return _cpp(argv[optind]) == 0 ? 0 : 2;
|
/* cpp_read */
|
||||||
|
ssize_t cpp_read(Cpp * cpp, char * buf, size_t cnt)
|
||||||
|
{
|
||||||
|
/* FIXME implement */
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
41
src/cpp.h
Normal file
41
src/cpp.h
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/* $Id$ */
|
||||||
|
/* Copyright (c) 2007 Pierre Pronchery <khorben@defora.org> */
|
||||||
|
/* This file is part of DeforaOS Devel cpp */
|
||||||
|
/* cpp is not free software; you can redistribute it and/or modify it under the
|
||||||
|
* terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0
|
||||||
|
* Unported as published by the Creative Commons organization.
|
||||||
|
*
|
||||||
|
* cpp is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE. See the Creative Commons Attribution-NonCommercial-
|
||||||
|
* ShareAlike 3.0 Unported license for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the Creative Commons Attribution-
|
||||||
|
* NonCommercial-ShareAlike 3.0 along with cpp; if not, browse to
|
||||||
|
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef CPP_CPP_H
|
||||||
|
# define CPP_CPP_H
|
||||||
|
|
||||||
|
|
||||||
|
/* cpp */
|
||||||
|
/* types */
|
||||||
|
typedef struct _Cpp Cpp;
|
||||||
|
|
||||||
|
|
||||||
|
/* functions */
|
||||||
|
Cpp * cpp_new(void);
|
||||||
|
void cpp_delete(Cpp * cpp);
|
||||||
|
|
||||||
|
char const * cpp_get_error(Cpp * cpp);
|
||||||
|
|
||||||
|
/* accessors */
|
||||||
|
char const * cpp_get_filename(Cpp * cpp);
|
||||||
|
|
||||||
|
/* useful */
|
||||||
|
int cpp_parse(Cpp * cpp, char const * pathname);
|
||||||
|
ssize_t cpp_read(Cpp * cpp, char * buf, size_t cnt);
|
||||||
|
|
||||||
|
#endif /* !CPP_CPP_H */
|
78
src/main.c
Normal file
78
src/main.c
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
/* $Id$ */
|
||||||
|
/* Copyright (c) 2007 Pierre Pronchery <khorben@defora.org> */
|
||||||
|
/* This file is part of DeforaOS Devel cpp */
|
||||||
|
/* cpp is not free software; you can redistribute it and/or modify it under the
|
||||||
|
* terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0
|
||||||
|
* Unported as published by the Creative Commons organization.
|
||||||
|
*
|
||||||
|
* cpp is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE. See the Creative Commons Attribution-NonCommercial-
|
||||||
|
* ShareAlike 3.0 Unported license for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the Creative Commons Attribution-
|
||||||
|
* NonCommercial-ShareAlike 3.0 along with cpp; if not, browse to
|
||||||
|
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "cpp.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* cpp */
|
||||||
|
static int _cpp_liberror(char const * message, int ret);
|
||||||
|
|
||||||
|
static int _cpp(char const * filename)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
Cpp * cpp;
|
||||||
|
ssize_t len;
|
||||||
|
char buf[BUFSIZ];
|
||||||
|
|
||||||
|
if((cpp = cpp_new()) == NULL)
|
||||||
|
return _cpp_liberror("Internal error", 1);
|
||||||
|
if(cpp_parse(cpp, filename) != 0)
|
||||||
|
ret = _cpp_liberror(cpp_get_error(cpp), 1);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while((len = cpp_read(cpp, buf, sizeof(buf))) > 0)
|
||||||
|
fwrite(buf, sizeof(char), len, stdout);
|
||||||
|
if(len < 0)
|
||||||
|
ret = _cpp_liberror(cpp_get_error(cpp), 1);
|
||||||
|
}
|
||||||
|
cpp_delete(cpp);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int _cpp_liberror(char const * message, int ret)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s%s", "cpp: ", message);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* usage */
|
||||||
|
static int _usage(void)
|
||||||
|
{
|
||||||
|
fputs("Usage: cpp filename\n", stderr);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* main */
|
||||||
|
int main(int argc, char * argv[])
|
||||||
|
{
|
||||||
|
int o;
|
||||||
|
|
||||||
|
while((o = getopt(argc, argv, "")) != -1)
|
||||||
|
switch(o)
|
||||||
|
{
|
||||||
|
default:
|
||||||
|
return _usage();
|
||||||
|
}
|
||||||
|
if(argc - optind != 1)
|
||||||
|
return _usage();
|
||||||
|
return _cpp(argv[optind]) == 0 ? 0 : 2;
|
||||||
|
}
|
71
src/parser.c
Normal file
71
src/parser.c
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
/* $Id$ */
|
||||||
|
/* Copyright (c) 2007 Pierre Pronchery <khorben@defora.org> */
|
||||||
|
/* This file is part of DeforaOS Devel cpp */
|
||||||
|
/* cpp is not free software; you can redistribute it and/or modify it under the
|
||||||
|
* terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0
|
||||||
|
* Unported as published by the Creative Commons organization.
|
||||||
|
*
|
||||||
|
* cpp is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE. See the Creative Commons Attribution-NonCommercial-
|
||||||
|
* ShareAlike 3.0 Unported license for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the Creative Commons Attribution-
|
||||||
|
* NonCommercial-ShareAlike 3.0 along with cpp; if not, browse to
|
||||||
|
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "parser.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* Parser */
|
||||||
|
/* private */
|
||||||
|
/* types */
|
||||||
|
struct _Parser
|
||||||
|
{
|
||||||
|
char * pathname;
|
||||||
|
FILE * fp;
|
||||||
|
unsigned long line;
|
||||||
|
unsigned long col;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* public */
|
||||||
|
/* parser_new */
|
||||||
|
Parser * parser_new(char const * pathname)
|
||||||
|
{
|
||||||
|
Parser * parser;
|
||||||
|
|
||||||
|
if((parser = malloc(sizeof(*parser))) == NULL)
|
||||||
|
return NULL;
|
||||||
|
parser->pathname = strdup(pathname);
|
||||||
|
parser->fp = fopen(pathname, "r");
|
||||||
|
if(parser->pathname == NULL || parser->fp == NULL)
|
||||||
|
{
|
||||||
|
parser_delete(parser);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
parser->line = 0;
|
||||||
|
parser->col = 0;
|
||||||
|
return parser;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* parser_delete */
|
||||||
|
void parser_delete(Parser * parser)
|
||||||
|
{
|
||||||
|
fclose(parser->fp);
|
||||||
|
free(parser->pathname);
|
||||||
|
free(parser);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* accessors */
|
||||||
|
char const * parser_get_filename(Parser * parser)
|
||||||
|
{
|
||||||
|
return parser->pathname;
|
||||||
|
}
|
35
src/parser.h
Normal file
35
src/parser.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/* $Id$ */
|
||||||
|
/* Copyright (c) 2007 Pierre Pronchery <khorben@defora.org> */
|
||||||
|
/* This file is part of DeforaOS Devel cpp */
|
||||||
|
/* cpp is not free software; you can redistribute it and/or modify it under the
|
||||||
|
* terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0
|
||||||
|
* Unported as published by the Creative Commons organization.
|
||||||
|
*
|
||||||
|
* cpp is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE. See the Creative Commons Attribution-NonCommercial-
|
||||||
|
* ShareAlike 3.0 Unported license for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the Creative Commons Attribution-
|
||||||
|
* NonCommercial-ShareAlike 3.0 along with cpp; if not, browse to
|
||||||
|
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef CPP_PARSER_H
|
||||||
|
# define CPP_PARSER_H
|
||||||
|
|
||||||
|
|
||||||
|
/* cpp */
|
||||||
|
/* types */
|
||||||
|
typedef struct _Parser Parser;
|
||||||
|
|
||||||
|
|
||||||
|
/* functions */
|
||||||
|
Parser * parser_new(char const * pathname);
|
||||||
|
void parser_delete(Parser * parser);
|
||||||
|
|
||||||
|
/* accessors */
|
||||||
|
char const * parser_get_filename(Parser * parser);
|
||||||
|
|
||||||
|
#endif /* !CPP_PARSER_H */
|
|
@ -1,7 +1,22 @@
|
||||||
targets=cpp
|
targets=libcpp,cpp
|
||||||
cflags_force=-W -Wall
|
cflags_force=-W -Wall
|
||||||
cflags=-g
|
cflags=-g
|
||||||
|
|
||||||
|
[libcpp]
|
||||||
|
type=library
|
||||||
|
sources=parser.c,cpp.c
|
||||||
|
cflags=-fPIC
|
||||||
|
|
||||||
|
[cpp.c]
|
||||||
|
depends=parser.h,cpp.h
|
||||||
|
|
||||||
|
[parser.c]
|
||||||
|
depends=parser.h
|
||||||
|
|
||||||
[cpp]
|
[cpp]
|
||||||
type=binary
|
type=binary
|
||||||
sources=cpp.c
|
sources=main.c
|
||||||
|
ldflags=-l cpp -L .
|
||||||
|
|
||||||
|
[main.c]
|
||||||
|
depends=cpp.h
|
||||||
|
|
Loading…
Reference in New Issue
Block a user