From c6923e5e44a61127b0cc19c76054089aa5b5157c Mon Sep 17 00:00:00 2001 From: khamidou Date: Fri, 17 Jul 2009 17:59:41 +0200 Subject: [PATCH] falling back on python as scripting language --- Makefile | 4 +- buffer.h | 2 + interp.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++--------- interp.h | 135 +++++++++++++++++++++++++++++++++++--------- main.c | 5 +- 5 files changed, 255 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index d28f336..2050faf 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC=gcc -CFLAGS=-c -g -LDFLAGS=-lcurses -lslang +CFLAGS=-c -g `python2.5-config --includes` +LDFLAGS=-lcurses `python2.5-config --libs` OBJS=main.o errors.o screen.o buffer.o keyb.o interp.o bstrlib.o te: $(OBJS) diff --git a/buffer.h b/buffer.h index 00755bb..a76692b 100644 --- a/buffer.h +++ b/buffer.h @@ -2,6 +2,7 @@ #define __BUFFER_H__ #include +#include #include "bstrlib.h" #include "queue.h" #include "config.h" @@ -14,6 +15,7 @@ struct te_buffer { bstring contents; int point; /* offset in "contents" */ int top_char; /* the position of the char at the top of the screen */ + PyObject *pyBuffer; /* pointer to hte python object representing this buffer */ TAILQ_ENTRY(te_buffer) buffers; }; diff --git a/interp.c b/interp.c index 8bf574c..6c5e0b5 100644 --- a/interp.c +++ b/interp.c @@ -1,37 +1,150 @@ #include "interp.h" -#include "screen.h" -void init_interp(void) +PyMODINIT_FUNC init_python(void) { - if (SLang_init_all () == - 1) - exit (EXIT_FAILURE); - - if (SLadd_intrin_fun_table(te_intrinsics, NULL) == -1) - { - fail("Unable to load the S-lang interface - exiting."); + Py_Initialize(); + + if (PyType_Ready(&binding_BufferType) < 0) + return; + + PyObject *m = Py_InitModule3("te", TeMethods, + "te main python api."); + + if (m == NULL) + return; + + Py_INCREF(&binding_BufferType); + PyModule_AddObject(m, "Buffer", (PyObject *)&binding_BufferType); + +} + +PyObject *Buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + TeBuffer *self; + + self = (TeBuffer *)type->tp_alloc(type, 0); + if (self != NULL) { + self->buf = alloc_and_insert_buffer(); + if (self->buf == NULL) + { + Py_DECREF(self); + return NULL; + } + + self->buf->pyBuffer = (PyObject *) self; } - - if (SLang_load_file ("tests/test.sl") == -1) - { - /* Clear the error and reset the interpreter */ - SLang_restart (1); + + return (PyObject *)self; +} + +void Buffer_dealloc(TeBuffer *self) +{ + if (self == NULL) + return; + + free_buffer(self->buf); + self->ob_type->tp_free((PyObject*)self); +} + +int Buffer_init(TeBuffer *self, PyObject *args, PyObject *kwds) +{ + PyObject *tmp; + + static char *kwlist[] = {"name", /* "file" */ NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|S", kwlist, + &tmp)) + return -1; + + + if (self != NULL) + self->buf->name = PyString_AsString(tmp); + + Py_XDECREF(tmp); + +} + +PyObject *Buffer_getname(TeBuffer *self, void *closure) +{ + if (self->buf == NULL) + return NULL; + + PyObject *s = PyString_FromString(self->buf->name); + Py_INCREF(s); + return s; +} + +int Buffer_setname(TeBuffer *self, PyObject *value, void *closure) +{ + if (self->buf == NULL) + return -1; + + if (value == NULL) { + PyErr_SetString(PyExc_TypeError, "Cannot change buffer name"); + return -1; } - -/* TODO : load the file named .terc. */ -/* if (SLang_load_file ("site.sl") == -1) */ -/* { */ -/* /\* Clear the error and reset the interpreter *\/ */ -/* SLang_restart (1); */ -/* } */ - + + char *s = PyString_AsString(value); + free(self->buf->name); + self->buf->name = strdup(s); } - -void intrin_miniprintf(char **s) + +PyObject *Buffer_getpoint(TeBuffer *self, void *closure) { - miniprintf("%s", *s); + if (self->buf == NULL) + return NULL; + + PyObject *i = PyInt_FromLong((long) self->buf->point); + Py_INCREF(i); + return i; } - -void intrin_statusprintf(char **s) + +int Buffer_setpoint(TeBuffer *self, PyObject *value, void *closure) +{ + if (self->buf == NULL) + return -1; + + if (value == NULL) { + PyErr_SetString(PyExc_TypeError, "Invalid object"); + return -1; + } + + if (!PyInt_Check(value)) { + PyErr_SetString(PyExc_TypeError, "Parameter is not of integer type"); + return -1; + } + + self->buf->point = (int) PyInt_AsLong(value); +} + +PyObject *Buffer_getcontents(TeBuffer *self, void *closure) +{ + if (self->buf == NULL) + return NULL; + + char *cstr = bstr2cstr(self->buf->contents, '\0'); + PyObject *s = PyString_FromString(cstr); + + free(cstr); + + Py_INCREF(s); + return s; +} + +int Buffer_setcontents(TeBuffer *self, PyObject *value, void *closure) { - statusprintf("%s", *s); + if (self->buf == NULL) + return -1; + + if (value == NULL) { + PyErr_SetString(PyExc_TypeError, "Invalid object"); + return -1; + } + + if (!PyInt_Check(value)) { + PyErr_SetString(PyExc_TypeError, "Parameter is not of integer type"); + return -1; + } + + self->buf->point = (int) PyInt_AsLong(value); } diff --git a/interp.h b/interp.h index bdc3961..595e837 100644 --- a/interp.h +++ b/interp.h @@ -1,32 +1,113 @@ /* - interp.h : bindings for the S-lang interpreter. - */ -#ifndef __INTERP_H__ -#define __INTERP_H__ - -#include -#include +conventions : the declarations using mixed case always refer to elements of the python binding. + the names of methods are prefixed with the name of their class (ex: Buffer_getname for Buffer.getname()) +*/ + +#ifndef __TE_PYTHON_BINDINGS__ +#define __TE_PYTHON_BINDINGS__ + +#include +#include #include "buffer.h" - -void intrin_statusprintf(char **s); -void intrin_miniprintf(char **s); - -SLang_Intrin_Fun_Type te_intrinsics [] = -{ - /* table entries */ - MAKE_INTRINSIC_1("miniprintf", intrin_miniprintf, SLANG_VOID_TYPE, SLANG_STRING_TYPE), - MAKE_INTRINSIC_1("statusprintf", intrin_statusprintf, SLANG_VOID_TYPE, SLANG_STRING_TYPE), - - SLANG_END_INTRIN_FUN_TABLE + +void init_interp(void); + +typedef struct { + PyObject_HEAD + struct te_buffer *buf; +} TeBuffer; + +int Buffer_init(TeBuffer *self, PyObject *args, PyObject *kwds); +void Buffer_dealloc(TeBuffer *self); +PyObject *Buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds); + +PyObject *Buffer_getname(TeBuffer *self, void *closure); +int Buffer_setname(TeBuffer *self, PyObject *value, void *closure); + +PyObject *Buffer_getpoint(TeBuffer *self, void *closure); +int Buffer_setpoint(TeBuffer *self, PyObject *value, void *closure); + +PyObject *Buffer_getcontents(TeBuffer *self, void *closure); +int Buffer_setcontents(TeBuffer *self, PyObject *value, void *closure); + +static PyMemberDef Buffer_members[] = { + {NULL} /* Sentinel */ }; - -SLang_CStruct_Field_Type buffer_type_layout [] = -{ - MAKE_CSTRUCT_FIELD(struct te_buffer, name, "name", SLANG_STRING_TYPE, 0), - MAKE_CSTRUCT_FIELD(struct te_buffer, point, "point", SLANG_INT_TYPE, 0), - MAKE_CSTRUCT_FIELD(struct te_buffer, dirty, "dirty", SLANG_INT_TYPE, 0), - - SLANG_END_CSTRUCT_TABLE + +static PyMethodDef Buffer_methods[] = { + {NULL} /* Sentinel */ }; - + +static PyGetSetDef Buffer_getsetters[] = { + {"point", + (getter)Buffer_getpoint, (setter)Buffer_setpoint, + "point of a buffer", + NULL}, + + {"name", + (getter)Buffer_getname, (setter)Buffer_setname, + "name of a buffer", + NULL}, + + {"contents", + (getter)Buffer_getcontents, (setter)Buffer_setcontents, + "contents of a buffer", + NULL}, + + {NULL} +}; + +static PyTypeObject binding_BufferType = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "te.Buffer", /*tp_name*/ + sizeof(TeBuffer), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + 0, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + "A representation of a buffer in memory.", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Buffer_methods, /* tp_methods */ + Buffer_members, /* tp_members */ + Buffer_getsetters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)Buffer_init, /* tp_init */ + 0, /* tp_alloc */ + Buffer_new, /* tp_new */ + +}; + +/* the methods of the te module */ +static PyMethodDef TeMethods[] = { +// {"system", spam_system, METH_VARARGS, +// "Execute a shell command."}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +PyMODINIT_FUNC init_python(void); + #endif + diff --git a/main.c b/main.c index 29c3707..87196c8 100644 --- a/main.c +++ b/main.c @@ -4,7 +4,7 @@ #include #include -//#include "interp.h" +#include "interp.h" #include "screen.h" void init_interp(void); @@ -14,8 +14,9 @@ extern struct te_buffer *current_buf; int main(int argc, char **argv) { init_buffers(); - init_interp(); init_windows(); + init_python(); + if (argc != 1) current_buf = load_buffer(argv[1]); -- 2.43.0