Skip to content

Commit

Permalink
Merge branch 'buffile' into p2
Browse files Browse the repository at this point in the history
Fixed conflicts:
	.gitmodules
	3rd/libtommath
	core/potion.h
  • Loading branch information
Reini Urban committed Jun 12, 2013
2 parents a419eb6 + 631a8d2 commit 90334e0
Show file tree
Hide file tree
Showing 13 changed files with 570 additions and 64 deletions.
7 changes: 4 additions & 3 deletions .gitmodules
@@ -1,9 +1,10 @@
[submodule "3rd/libuv"] [submodule "3rd/libuv"]
path = 3rd/libuv path = 3rd/libuv
url = git://github.com/joyent/libuv.git url = git://github.com/joyent/libuv.git
[submodule "3rd/sregex"]
path = 3rd/sregex
url = git://github.com/agentzh/sregex.git
[submodule "3rd/libtommath"] [submodule "3rd/libtommath"]
path = 3rd/libtommath path = 3rd/libtommath
url = git://github.com/libtom/libtommath.git url = git://github.com/libtom/libtommath.git
[submodule "3rd/pcre"]
path = 3rd/pcre
url = git://github.com/Distrotech/pcre.git
branch = master
1 change: 1 addition & 0 deletions 3rd/pcre
Submodule pcre added at 000f53
1 change: 0 additions & 1 deletion 3rd/sregex
Submodule sregex deleted from 2d2622
59 changes: 47 additions & 12 deletions Makefile
Expand Up @@ -52,14 +52,14 @@ DOCHTML = ${DOC:.textile=.html}
BINS = bin/potion${EXE} bin/p2${EXE} BINS = bin/potion${EXE} bin/p2${EXE}
PLIBS = $(foreach l,potion p2,lib/lib$l${DLL}) PLIBS = $(foreach l,potion p2,lib/lib$l${DLL})
PLIBS += $(foreach s,syntax syntax-p5,lib/potion/lib$s${DLL}) PLIBS += $(foreach s,syntax syntax-p5,lib/potion/lib$s${DLL})
#EXTLIBS = $(foreach m,sregex uv,lib/lib$m.a) #EXTLIBS = $(foreach m,uv pcre,lib/lib$m.a)
DYNLIBS = $(foreach m,readline libtommath m_apm,lib/potion/$m${LOADEXT}) #EXTLIBS = -L3rd/pcre -lpcre -L3rd/libuv -luv -L3rd/libtommath -llibtommath
DYNLIBS = $(foreach m,readline buffile,lib/potion/$m${LOADEXT})
OBJS = .o .o2 OBJS = .o .o2
ifneq (${FPIC},) ifneq (${FPIC},)
OBJS += ${OPIC} ${OPIC}2 OBJS += ${OPIC} ${OPIC}2
endif endif



GREGCFLAGS = -O3 -DNDEBUG GREGCFLAGS = -O3 -DNDEBUG
CAT = /bin/cat CAT = /bin/cat
ECHO ?= /bin/echo ECHO ?= /bin/echo
Expand All @@ -78,7 +78,7 @@ default: pn p2


all: default libs static doc test all: default libs static doc test
bins: ${BINS} bins: ${BINS}
libs: ${PLIBS} libs: ${PLIBS} ${DYNLIBS}
pn: bin/potion${EXE} libs pn: bin/potion${EXE} libs
p2: bin/p2${EXE} libs p2: bin/p2${EXE} libs
static: lib/libpotion.a bin/potion-s${EXE} lib/libp2.a bin/p2-s${EXE} static: lib/libpotion.a bin/potion-s${EXE} lib/libp2.a bin/p2-s${EXE}
Expand Down Expand Up @@ -250,7 +250,7 @@ bin/potion-s${EXE}: ${OBJ_POTION} lib/libpotion.a
@[ -d bin ] || mkdir bin @[ -d bin ] || mkdir bin
@${CC} ${CFLAGS} ${OBJ_POTION} -o $@ ${LIBPTH} lib/libpotion.a ${LIBS} @${CC} ${CFLAGS} ${OBJ_POTION} -o $@ ${LIBPTH} lib/libpotion.a ${LIBS}


bin/p2-s${EXE}: ${OBJ_P2} lib/libp2.a ${EXTLIBS} bin/p2-s${EXE}: ${OBJ_P2} lib/libp2.a
@${ECHO} LINK $@ @${ECHO} LINK $@
@[ -d bin ] || mkdir bin @[ -d bin ] || mkdir bin
@${CC} ${CFLAGS} ${OBJ_P2} -o $@ ${LIBPTH} lib/libp2.a ${LIBS} ${EXTLIBS} @${CC} ${CFLAGS} ${OBJ_P2} -o $@ ${LIBPTH} lib/libp2.a ${LIBS} ${EXTLIBS}
Expand All @@ -277,7 +277,7 @@ lib/libpotion${DLL}: ${PIC_OBJ} ${PIC_OBJ_SYN} core/config.h core/potion.h
${PIC_OBJ} ${PIC_OBJ_SYN} ${LIBS} > /dev/null ${PIC_OBJ} ${PIC_OBJ_SYN} ${LIBS} > /dev/null
@if [ x${DLL} = x.dll ]; then cp $@ bin/; fi @if [ x${DLL} = x.dll ]; then cp $@ bin/; fi


lib/libp2${DLL}: $(subst .${OPIC},.${OPIC}2,${PIC_OBJ}) ${PIC_OBJ_P2_SYN} core/config.h core/potion.h ${EXTLIBS} lib/libp2${DLL}: $(subst .${OPIC},.${OPIC}2,${PIC_OBJ}) ${PIC_OBJ_P2_SYN} core/config.h core/potion.h
@${ECHO} LD $@ @${ECHO} LD $@
@[ -d lib/potion ] || mkdir lib/potion @[ -d lib/potion ] || mkdir lib/potion
@if [ -e $@ ]; then rm -f $@; fi @if [ -e $@ ]; then rm -f $@; fi
Expand All @@ -297,19 +297,31 @@ lib/potion/libsyntax-p5${DLL}: syn/syntax-p5.${OPIC}2
@${CC} ${DEBUGFLAGS} -o $@ $(INCS) ${LDDLLFLAGS} ${RPATH} \ @${CC} ${DEBUGFLAGS} -o $@ $(INCS) ${LDDLLFLAGS} ${RPATH} \
$< ${LIBPTH} -lp2 $(LIBS) $< ${LIBPTH} -lp2 $(LIBS)


# 3rdparty EXTLIBS linked # 3rdparty EXTLIBS statically linked
lib/libuv.a: core/config.h core/potion.h \ lib/libuv.a: core/config.h core/potion.h \
3rd/libuv/Makefile 3rd/libuv/Makefile
@${ECHO} MAKE $@ @${ECHO} MAKE $@
@${MAKE} -s -C 3rd/libuv libuv${DLL} @${MAKE} -s -C 3rd/libuv libuv${DLL}
@cp 3rd/libuv/libuv.a lib/ @cp 3rd/libuv/libuv.a lib/


3rd/libuv/libuv$(DLL): core/config.h core/potion.h \
3rd/libuv/Makefile
@${ECHO} MAKE $@
@${MAKE} -s -C 3rd/libuv libuv${DLL}
@cp 3rd/libuv/libuv$(DLL) lib/

lib/libsregex.a: core/config.h core/potion.h \ lib/libsregex.a: core/config.h core/potion.h \
3rd/sregex/Makefile 3rd/sregex/Makefile
@${ECHO} MAKE $@ @${ECHO} MAKE $@
@${MAKE} -s -C 3rd/sregex CC="${CC}" @${MAKE} -s -C 3rd/sregex CC="${CC}"
@cp 3rd/sregex/libsregex.a lib/ @cp 3rd/sregex/libsregex.a lib/


lib/libpcre.a: core/config.h core/potion.h \
3rd/pcre/Makefile
@${ECHO} MAKE $@
@${MAKE} -s -C 3rd/pcre CC="${CC}"
@cp 3rd/pcre/libpcre.a lib/

# DYNLIBS # DYNLIBS
lib/potion/readline${LOADEXT}: core/config.h core/potion.h \ lib/potion/readline${LOADEXT}: core/config.h core/potion.h \
lib/readline/Makefile lib/readline/linenoise.c \ lib/readline/Makefile lib/readline/linenoise.c \
Expand All @@ -319,6 +331,29 @@ lib/potion/readline${LOADEXT}: core/config.h core/potion.h \
@[ -d lib/potion ] || mkdir lib/potion @[ -d lib/potion ] || mkdir lib/potion
@cp lib/readline/readline${LOADEXT} $@ @cp lib/readline/readline${LOADEXT} $@


lib/potion/buffile${LOADEXT}: core/config.h core/potion.h \
lib/buffile.${OPIC}2 lib/buffile.c
@${ECHO} LD $@
@[ -d lib/potion ] || mkdir lib/potion
@${CC} $(DEBUGFLAGS) -o $@ ${LDDLLFLAGS} \
lib/buffile.${OPIC}2 ${LIBPTH} ${LIBS} > /dev/null

lib/potion/aio${LOADEXT}: core/config.h core/potion.h \
lib/aio.c
@[ -d lib/potion ] || mkdir lib/potion
@${ECHO} CC lib/aio.${OPIC}2
@${CC} -c -DP2 ${FPIC} ${CFLAGS} ${INCS} -I3rd/libuv/include -o lib/aio.${OPIC}2 lib/aio.c > /dev/null
@${ECHO} LD $@
@${CC} $(DEBUGFLAGS) -o $@ $(subst libpotion,aio,${LDDLLFLAGS}) \
lib/aio.${OPIC}2 ${LIBS} ${EXTLIBS} > /dev/null

lib/potion/pcre${LOADEXT}: core/config.h core/potion.h \
lib/pcre/Makefile lib/pcre/pcre.c
@${ECHO} MAKE $@
@${MAKE} -s -C lib/pcre
@[ -d lib/potion ] || mkdir lib/potion
@cp lib/pcre/pcre${LOADEXT} $@

lib/potion/m_apm${LOADEXT}: core/config.h core/potion.h \ lib/potion/m_apm${LOADEXT}: core/config.h core/potion.h \
lib/m_apm/Makefile lib/m_apm/Makefile
@${ECHO} MAKE $@ @${ECHO} MAKE $@
Expand All @@ -339,8 +374,8 @@ bench: bin/gc-bench${EXE} bin/potion${EXE}
${ECHO} running GC benchmark; \ ${ECHO} running GC benchmark; \
time bin/gc-bench time bin/gc-bench


check: test.pn test.p2 check: libs test.pn test.p2
test: test.pn test.p2 test: libs test.pn test.p2


test.pn: bin/potion${EXE} bin/potion-test${EXE} test.pn: bin/potion${EXE} bin/potion-test${EXE}
@${ECHO}; \ @${ECHO}; \
Expand Down Expand Up @@ -460,15 +495,15 @@ bin/potion-test${EXE}: ${OBJ_TEST} lib/libpotion.a
@if ${CC} ${CFLAGS} ${OBJ_TEST} -o $@ lib/libpotion.a ${LIBS}; then true; else \ @if ${CC} ${CFLAGS} ${OBJ_TEST} -o $@ lib/libpotion.a ${LIBS}; then true; else \
${CC} ${CFLAGS} ${OBJ_TEST} -o $@ ${OBJ} ${OBJ_SYN} ${LIBS}; fi ${CC} ${CFLAGS} ${OBJ_TEST} -o $@ ${OBJ} ${OBJ_SYN} ${LIBS}; fi


bin/gc-test${EXE}: ${OBJ_GC_TEST} lib/libp2.a ${EXTLIBS} bin/gc-test${EXE}: ${OBJ_GC_TEST} lib/libp2.a
@${ECHO} LINK $@ @${ECHO} LINK $@
@${CC} ${CFLAGS} ${OBJ_GC_TEST} -o $@ lib/libp2.a ${LIBS} ${EXTLIBS} @${CC} ${CFLAGS} ${OBJ_GC_TEST} -o $@ lib/libp2.a ${LIBS} ${EXTLIBS}


bin/gc-bench${EXE}: ${OBJ_GC_BENCH} lib/libp2.a ${EXTLIBS} bin/gc-bench${EXE}: ${OBJ_GC_BENCH} lib/libp2.a
@${ECHO} LINK $@ @${ECHO} LINK $@
@${CC} ${CFLAGS} ${OBJ_GC_BENCH} -o $@ lib/libp2.a ${LIBS} ${EXTLIBS} @${CC} ${CFLAGS} ${OBJ_GC_BENCH} -o $@ lib/libp2.a ${LIBS} ${EXTLIBS}


bin/p2-test${EXE}: ${OBJ_P2_TEST} lib/libp2.a ${EXTLIBS} bin/p2-test${EXE}: ${OBJ_P2_TEST} lib/libp2.a
@${ECHO} LINK $@ @${ECHO} LINK $@
@if ${CC} ${CFLAGS} ${OBJ_P2_TEST} -o $@ lib/libp2.a ${LIBS} ${EXTLIBS}; then true; else \ @if ${CC} ${CFLAGS} ${OBJ_P2_TEST} -o $@ lib/libp2.a ${LIBS} ${EXTLIBS}; then true; else \
${CC} ${CFLAGS} ${OBJ_P2_TEST} -o $@ ${OBJ2} ${OBJ_P2_SYN} ${LIBS} ${EXTLIBS}; fi ${CC} ${CFLAGS} ${OBJ_P2_TEST} -o $@ ${OBJ2} ${OBJ_P2_SYN} ${LIBS} ${EXTLIBS}; fi
Expand Down
134 changes: 90 additions & 44 deletions core/file.c
@@ -1,13 +1,22 @@
///\file file.c /** \file file.c
/// PNFile class for file descriptors PNFile class for unbuffered blocking file descriptor IO.
//
// (c) 2008 why the lucky stiff, the freelance professor Only raw and fast POSIX open,read,write,seek calls on fd.
// fgets (aka readline) is only supported on stdin via the \c "read" method.
\seealso http://stackoverflow.com/questions/1658476/c-fopen-vs-open
\see the \c buffile library (PNBufFile) for buffered io via FILE*
for fopen, fscanf, fprintf, fread, fgets see there.
\see the aio library (PNAio) for async non-blocking io, via libuv bindings.
(c) 2008 why the lucky stiff, the freelance professor */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h>
#include <assert.h>
#include "p2.h" #include "p2.h"
#include "internal.h" #include "internal.h"
#include "table.h" #include "table.h"
Expand All @@ -22,11 +31,17 @@


typedef vPN(File) pn_file; typedef vPN(File) pn_file;


///\memberof PNFile PN potion_io_error(Potion *P, PN msg) {
/// constructor method. opens a file with 0755 and returns the created PNFile return potion_error(P, potion_str_format(P, "Error %s: %s", PN_STR_PTR(msg), strerror(errno)),
///\param path PNString 0, 0, 0);
///\param modestr PNString r,r+,w,w+,a,a+ }
///\return self or PN_NIL

/**\memberof PNFile
constructor method. opens a file with 0755 and returns the created PNFile
\param path PNString
\param modestr PNString r,r+,w,w+,a,a+
\return self or PN_NIL */
PN potion_file_new(Potion *P, PN cl, PN self, PN path, PN modestr) { PN potion_file_new(Potion *P, PN cl, PN self, PN path, PN modestr) {
int fd; int fd;
mode_t mode; mode_t mode;
Expand All @@ -46,21 +61,18 @@ PN potion_file_new(Potion *P, PN cl, PN self, PN path, PN modestr) {
// invalid mode // invalid mode
return PN_NIL; return PN_NIL;
} }
if ((fd = open(PN_STR_PTR(path), mode, 0755)) == -1) { if ((fd = open(PN_STR_PTR(path), mode, 0755)) == -1)
perror("open"); return potion_io_error(P, PN_STR("open"));
// TODO: error, perm
return PN_NIL;
}
((struct PNFile *)self)->fd = fd; ((struct PNFile *)self)->fd = fd;
((struct PNFile *)self)->path = path; ((struct PNFile *)self)->path = path;
((struct PNFile *)self)->mode = mode; ((struct PNFile *)self)->mode = mode;
return self; return self;
} }


///\memberof PNFile /**\memberof PNFile
/// "fd" class method. \c "fd" class method.
///\param fd PNNumber \param fd PNNumber
///\return a new PNFile object for the already opened file descriptor (sorry, empty path). \return a new PNFile object for the already opened file descriptor (sorry, empty path). */
PN potion_file_with_fd(Potion *P, PN cl, PN self, PN fd) { PN potion_file_with_fd(Potion *P, PN cl, PN self, PN fd) {
struct PNFile *file = (struct PNFile *)potion_object_new(P, PN_NIL, PN_VTABLE(PN_TFILE)); struct PNFile *file = (struct PNFile *)potion_object_new(P, PN_NIL, PN_VTABLE(PN_TFILE));
file->fd = PN_INT(fd); file->fd = PN_INT(fd);
Expand All @@ -75,49 +87,82 @@ PN potion_file_with_fd(Potion *P, PN cl, PN self, PN fd) {
return (PN)file; return (PN)file;
} }


///\memberof PNFile /**\memberof PNFile
/// "close" method. \c "close" the file
///\return PN_NIL \return PN_NIL */
PN potion_file_close(Potion *P, PN cl, pn_file self) { PN potion_file_close(Potion *P, PN cl, pn_file self) {
close(self->fd); int retval;
while (retval = close(self->fd), retval == -1 && errno == EINTR) ;
self->fd = -1; self->fd = -1;
return PN_NIL; return PN_NIL;
} }


///\memberof PNFile /**\memberof PNFile
/// "read" method. \c "read" n PNBytes from the file
///\param n PNNumber \param n PNNumber
///\return n PNBytes \return n PNBytes */
PN potion_file_read(Potion *P, PN cl, pn_file self, PN n) { PN potion_file_read(Potion *P, PN cl, pn_file self, PN n) {
n = PN_INT(n); n = PN_INT(n);
char buf[n]; char buf[n];
int r = read(self->fd, buf, n); int r = read(self->fd, buf, n);
if (r == -1) { if (r == -1) {
perror("read"); return potion_io_error(P, PN_STR("read"));
//perror("read");
// TODO: error // TODO: error
return PN_NUM(-1); //return PN_NUM(-1);
} else if (r == 0) { } else if (r == 0) {
return PN_NIL; return PN_NIL;
} }
return potion_byte_str2(P, buf, r); return potion_byte_str2(P, buf, r);
} }


///\memberof PNFile /**\memberof PNFile
/// "write" method. \c "write" a binary representation of obj to the file handle.
///\param str PNString \param obj PNString, PNBytes, PNNumber (long or double), PNBoolean (char 0 or 1)
///\return PNNumber written bytes or PN_NIL \return PNNumber written bytes or PN_NIL */
PN potion_file_write(Potion *P, PN cl, pn_file self, PN str) { PN potion_file_write(Potion *P, PN cl, pn_file self, PN obj) {
int r = write(self->fd, PN_STR_PTR(str), PN_STR_LEN(str)); long len = 0;
if (r == -1) { char *ptr = NULL;
perror("write"); //TODO: maybe extract ptr+len to seperate function
// TODO: error if (!PN_IS_PTR(obj)) {
return PN_NIL; if (!obj) return PN_NIL; //silent
else if (PN_IS_NUM(obj)) {
long tmp = PN_NUM(obj); len = sizeof(tmp); ptr = (char *)&tmp;
}
else if (PN_IS_BOOL(obj)) {
char tmp = (obj == PN_TRUE) ? 1 : 0; len = 1; ptr = (char *)&tmp;
}
else {
assert(0 && "Invalid primitive type");
}
} else {
switch (PN_TYPE(obj)) {
case PN_TSTRING: len = PN_STR_LEN(obj); ptr = PN_STR_PTR(obj); break;
case PN_TBYTES: len = potion_send(obj, PN_STR("length")); ptr = PN_STR_PTR(obj); break;
case PN_TNUMBER: {
double tmp = PN_DBL(obj); len = sizeof(tmp); ptr = (char *)&tmp;
break;
}
default: return potion_type_error(P, obj);
}
} }
int r = write(self->fd, ptr, len);
if (r == -1)
return potion_io_error(P, PN_STR("write"));
return PN_NUM(r); return PN_NUM(r);
} }


///\memberof PNFile /**\memberof PNFile
/// "string" method. some internal descr \c "print" a stringification of any object to the filehandle.
Note that \c write prints the binary value of the object.
\param obj any
\return PN_NIL */
PN potion_file_print(Potion *P, PN cl, pn_file self, PN obj) {
return potion_file_write(P, cl, self, potion_send(obj, PN_string));
}

/**\memberof PNFile
"string" method. some internal descr */
PN potion_file_string(Potion *P, PN cl, pn_file self) { PN potion_file_string(Potion *P, PN cl, pn_file self) {
int fd = self->fd, rv; int fd = self->fd, rv;
char *buf; char *buf;
Expand All @@ -135,9 +180,9 @@ PN potion_file_string(Potion *P, PN cl, pn_file self) {
return str; return str;
} }


/// memberof Lobby /**\memberof Lobby
/// global "read" method, read next line from stdin via fgets() global "read" method, read next line from stdin via fgets()
///\return PNString or or PN_NIL \return PNString or or PN_NIL */
PN potion_lobby_read(Potion *P, PN cl, PN self) { PN potion_lobby_read(Potion *P, PN cl, PN self) {
const int linemax = 1024; const int linemax = 1024;
char line[linemax]; char line[linemax];
Expand Down Expand Up @@ -171,4 +216,5 @@ void potion_file_init(Potion *P) {
potion_method(file_vt, "close", potion_file_close, 0); potion_method(file_vt, "close", potion_file_close, 0);
potion_method(file_vt, "read", potion_file_read, "n=N"); potion_method(file_vt, "read", potion_file_read, "n=N");
potion_method(file_vt, "write", potion_file_write, "str=S"); potion_method(file_vt, "write", potion_file_write, "str=S");
potion_method(file_vt, "print", potion_file_print, "obj=o");
} }
13 changes: 13 additions & 0 deletions core/internal.c
Expand Up @@ -237,6 +237,19 @@ void potion_error_init(Potion *P) {
potion_method(err_vt, "string", potion_error_string, 0); potion_method(err_vt, "string", potion_error_string, 0);
} }


static inline char *potion_type_name(Potion *P, PN obj) {
return PN_IS_PTR(obj)
? AS_STR(potion_send(PN_VTABLE(obj), PN_name))
: PN_IS_NIL(obj) ? "nil"
: PN_IS_NUM(obj) ? "Number"
: "Boolean";
}

PN potion_type_error(Potion *P, PN obj) {
return potion_error(P, potion_str_format(P, "Invalid type %s", potion_type_name(P, obj)),
0, 0, 0);
}

#define PN_EXIT_ERROR 1 #define PN_EXIT_ERROR 1
#define PN_EXIT_FATAL 2 #define PN_EXIT_FATAL 2


Expand Down
5 changes: 2 additions & 3 deletions core/objmodel.c
Expand Up @@ -485,10 +485,9 @@ PN potion_lobby_kind(Potion *P, PN cl, PN self) {


/**\memberof Lobby /**\memberof Lobby
\c "can" the object call the named method? \c "can" the object call the named method?
same as potion_bind(). \return true or false */
\return the found method or nil */
PN potion_lobby_can(Potion *P, PN cl, PN self, PN method) { PN potion_lobby_can(Potion *P, PN cl, PN self, PN method) {
return potion_bind(P, self, method); return potion_bind(P, self, method) ? PN_TRUE : PN_FALSE;
} }


/**\memberof Lobby /**\memberof Lobby
Expand Down

0 comments on commit 90334e0

Please sign in to comment.