Separating the code between a binary and a shared library

This commit is contained in:
Pierre Pronchery 2007-09-09 22:56:56 +00:00
parent b9c7a7718b
commit 86646c34c2
8 changed files with 342 additions and 49 deletions

View File

@ -1,6 +1,7 @@
PACKAGE = cpp
VERSION = 0.0.0
SUBDIRS = src
RM = rm -f
LN = ln -sf
TAR = tar -czvf
@ -20,8 +21,12 @@ dist:
$(RM) -r $(PACKAGE)-$(VERSION)
$(LN) . $(PACKAGE)-$(VERSION)
@$(TAR) $(PACKAGE)-$(VERSION).tar.gz \
$(PACKAGE)-$(VERSION)/src/parser.c \
$(PACKAGE)-$(VERSION)/src/cpp.c \
$(PACKAGE)-$(VERSION)/src/main.c \
$(PACKAGE)-$(VERSION)/src/project.conf \
$(PACKAGE)-$(VERSION)/Makefile \
$(PACKAGE)-$(VERSION)/COPYING \
$(PACKAGE)-$(VERSION)/project.conf
$(RM) $(PACKAGE)-$(VERSION)

View File

@ -1,11 +1,16 @@
TARGETS = cpp
TARGETS = libcpp.a libcpp.so cpp
PREFIX = /usr/local
DESTDIR =
BINDIR = $(PREFIX)/bin
INCLUDEDIR= $(PREFIX)/include
LIBDIR = $(PREFIX)/lib
CC = cc
CPPFLAGS=
CFLAGSF = -W -Wall
CFLAGS = -g
AR = ar -rc
RANLIB = ranlib
LD = ld -shared
BINDIR = $(PREFIX)/bin
INCLUDEDIR= $(PREFIX)/include
RM = rm -f
MKDIR = mkdir -p
INSTALL = install
@ -13,25 +18,47 @@ INSTALL = install
all: $(TARGETS)
cpp_OBJS = cpp.o
cpp_CFLAGS = $(CFLAGSF) $(CFLAGS)
cpp: $(cpp_OBJS)
$(CC) $(LDFLAGSF) $(LDFLAGS) -o cpp $(cpp_OBJS)
libcpp_OBJS = parser.o cpp.o
libcpp_CFLAGS = $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) -fPIC
cpp.o: cpp.c
$(CC) $(cpp_CFLAGS) -c cpp.c
libcpp.a: $(libcpp_OBJS)
$(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:
$(RM) $(cpp_OBJS)
$(RM) $(libcpp_OBJS) $(cpp_OBJS)
distclean: clean
$(RM) $(TARGETS)
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)
$(INSTALL) -m 0755 cpp $(DESTDIR)$(BINDIR)/cpp
uninstall:
$(RM) $(DESTDIR)$(LIBDIR)/libcpp.a
$(RM) $(DESTDIR)$(LIBDIR)/libcpp.so
$(RM) $(DESTDIR)$(BINDIR)/cpp
.PHONY: all clean distclean install uninstall

View File

@ -17,56 +17,77 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "parser.h"
#include "cpp.h"
/* cpp */
static int _cpp_error(char const * message, int ret);
static int _cpp(char const * filename)
/* Cpp */
/* private */
/* types */
struct _Cpp
{
FILE * fp;
char buf[BUFSIZ];
size_t len;
int ret = 0;
Parser ** parser;
size_t parser_cnt;
char error[PATH_MAX];
};
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: ");
perror(message);
return ret;
Cpp * cpp;
if((cpp = malloc(sizeof(*cpp))) == NULL)
return NULL;
memset(cpp, sizeof(*cpp), 0);
return cpp;
}
/* usage */
static int _usage(void)
/* cpp_delete */
void cpp_delete(Cpp * cpp)
{
fprintf(stderr, "%s", "Usage: cpp filename\n");
return 1;
size_t i;
for(i = 0; i < cpp->parser_cnt; i++)
parser_delete(cpp->parser[i]);
free(cpp->parser);
free(cpp);
}
/* main */
int main(int argc, char * argv[])
/* accessors */
/* cpp_get_error */
char const * cpp_get_error(Cpp * cpp)
{
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;
return cpp->error;
}
/* useful */
int cpp_parse(Cpp * cpp, char const * pathname)
{
Parser ** p;
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;
}
/* cpp_read */
ssize_t cpp_read(Cpp * cpp, char * buf, size_t cnt)
{
/* FIXME implement */
return -1;
}

41
src/cpp.h Normal file
View 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
View 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
View 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
View 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 */

View File

@ -1,7 +1,22 @@
targets=cpp
targets=libcpp,cpp
cflags_force=-W -Wall
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]
type=binary
sources=cpp.c
sources=main.c
ldflags=-l cpp -L .
[main.c]
depends=cpp.h