| 1 |
/*=========================================================================*\ |
| 2 |
* Serial stream |
| 3 |
* LuaSocket toolkit |
| 4 |
\*=========================================================================*/ |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "lua.h" |
| 8 |
#include "lauxlib.h" |
| 9 |
|
| 10 |
#include "auxiliar.h" |
| 11 |
#include "socket.h" |
| 12 |
#include "options.h" |
| 13 |
#include "unix.h" |
| 14 |
#ifndef _WIN32 |
| 15 |
#include <sys/un.h> |
| 16 |
#endif |
| 17 |
#include <sys/types.h> |
| 18 |
#include <sys/stat.h> |
| 19 |
#include <fcntl.h> |
| 20 |
/* |
| 21 |
Reuses userdata definition from unix.h, since it is useful for all |
| 22 |
stream-like objects. |
| 23 |
|
| 24 |
If we stored the serial path for use in error messages or userdata |
| 25 |
printing, we might need our own userdata definition. |
| 26 |
|
| 27 |
Group usage is semi-inherited from unix.c, but unnecessary since we |
| 28 |
have only one object type. |
| 29 |
*/ |
| 30 |
|
| 31 |
/*=========================================================================*\ |
| 32 |
* Internal function prototypes |
| 33 |
\*=========================================================================*/ |
| 34 |
/*static int global_create(lua_State *L);*/ |
| 35 |
/* static int meth_send(lua_State *L); */ |
| 36 |
/* static int meth_receive(lua_State *L); */ |
| 37 |
/* static int meth_close(lua_State *L); */ |
| 38 |
/* static int meth_settimeout(lua_State *L); */ |
| 39 |
/* static int meth_getfd(lua_State *L); */ |
| 40 |
/* static int meth_setfd(lua_State *L); */ |
| 41 |
/* static int meth_dirty(lua_State *L); */ |
| 42 |
/* static int meth_getstats(lua_State *L); */ |
| 43 |
/* static int meth_setstats(lua_State *L); */ |
| 44 |
|
| 45 |
/* serial object methods */ |
| 46 |
/* static luaL_Reg serial_methods[] = { */ |
| 47 |
/* {"__gc", meth_close}, */ |
| 48 |
/* {"__tostring", auxiliar_tostring}, */ |
| 49 |
/* {"close", meth_close}, */ |
| 50 |
/* {"dirty", meth_dirty}, */ |
| 51 |
/* {"getfd", meth_getfd}, */ |
| 52 |
/* {"getstats", meth_getstats}, */ |
| 53 |
/* {"setstats", meth_setstats}, */ |
| 54 |
/* {"receive", meth_receive}, */ |
| 55 |
/* {"send", meth_send}, */ |
| 56 |
/* {"setfd", meth_setfd}, */ |
| 57 |
/* {"settimeout", meth_settimeout}, */ |
| 58 |
/* {NULL, NULL} */ |
| 59 |
/* }; */ |
| 60 |
|
| 61 |
/*-------------------------------------------------------------------------*\ |
| 62 |
* Initializes module (luatex extension, unused ) |
| 63 |
\*-------------------------------------------------------------------------*/ |
| 64 |
/* LUASOCKET_API int luaopen_socket_serial(lua_State *L) { */ |
| 65 |
/* /\* create classes *\/ */ |
| 66 |
/* auxiliar_newclass(L, "serial{client}", serial_methods); */ |
| 67 |
/* /\* create class groups *\/ */ |
| 68 |
/* auxiliar_add2group(L, "serial{client}", "serial{any}"); */ |
| 69 |
/* lua_pushcfunction(L, global_create); */ |
| 70 |
/* return 1; */ |
| 71 |
/* } */ |
| 72 |
|
| 73 |
/*=========================================================================*\ |
| 74 |
* Lua methods |
| 75 |
\*=========================================================================*/ |
| 76 |
/*-------------------------------------------------------------------------*\ |
| 77 |
* Just call buffered IO methods |
| 78 |
\*-------------------------------------------------------------------------*/ |
| 79 |
/* static int meth_send(lua_State *L) { */ |
| 80 |
/* p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); */ |
| 81 |
/* return buffer_meth_send(L, &un->buf); */ |
| 82 |
/* } */ |
| 83 |
|
| 84 |
/* static int meth_receive(lua_State *L) { */ |
| 85 |
/* p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); */ |
| 86 |
/* return buffer_meth_receive(L, &un->buf); */ |
| 87 |
/* } */ |
| 88 |
|
| 89 |
/* static int meth_getstats(lua_State *L) { */ |
| 90 |
/* p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); */ |
| 91 |
/* return buffer_meth_getstats(L, &un->buf); */ |
| 92 |
/* } */ |
| 93 |
|
| 94 |
/* static int meth_setstats(lua_State *L) { */ |
| 95 |
/* p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); */ |
| 96 |
/* return buffer_meth_setstats(L, &un->buf); */ |
| 97 |
/* } */ |
| 98 |
|
| 99 |
/*-------------------------------------------------------------------------*\ |
| 100 |
* Select support methods |
| 101 |
\*-------------------------------------------------------------------------*/ |
| 102 |
/* static int meth_getfd(lua_State *L) { */ |
| 103 |
/* p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); */ |
| 104 |
/* lua_pushnumber(L, (int) un->sock); */ |
| 105 |
/* return 1; */ |
| 106 |
/* } */ |
| 107 |
|
| 108 |
/* /\* this is very dangerous, but can be handy for those that are brave enough *\/ */ |
| 109 |
/* static int meth_setfd(lua_State *L) { */ |
| 110 |
/* p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); */ |
| 111 |
/* un->sock = (t_socket) luaL_checknumber(L, 2); */ |
| 112 |
/* return 0; */ |
| 113 |
/* } */ |
| 114 |
|
| 115 |
/* static int meth_dirty(lua_State *L) { */ |
| 116 |
/* p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); */ |
| 117 |
/* lua_pushboolean(L, !buffer_isempty(&un->buf)); */ |
| 118 |
/* return 1; */ |
| 119 |
/* } */ |
| 120 |
|
| 121 |
/*-------------------------------------------------------------------------*\ |
| 122 |
* Closes socket used by object |
| 123 |
\*-------------------------------------------------------------------------*/ |
| 124 |
/* static int meth_close(lua_State *L) */ |
| 125 |
/* { */ |
| 126 |
/* p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); */ |
| 127 |
/* socket_destroy(&un->sock); */ |
| 128 |
/* lua_pushnumber(L, 1); */ |
| 129 |
/* return 1; */ |
| 130 |
/* } */ |
| 131 |
|
| 132 |
|
| 133 |
/*-------------------------------------------------------------------------*\ |
| 134 |
* Just call tm methods |
| 135 |
\*-------------------------------------------------------------------------*/ |
| 136 |
/* static int meth_settimeout(lua_State *L) { */ |
| 137 |
/* p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); */ |
| 138 |
/* return timeout_meth_settimeout(L, &un->tm); */ |
| 139 |
/* } */ |
| 140 |
|
| 141 |
/*=========================================================================*\ |
| 142 |
* Library functions |
| 143 |
\*=========================================================================*/ |
| 144 |
|
| 145 |
|
| 146 |
/*-------------------------------------------------------------------------*\ |
| 147 |
* Creates a serial object |
| 148 |
\*-------------------------------------------------------------------------*/ |
| 149 |
/* static int global_create(lua_State *L) { */ |
| 150 |
/* const char* path = luaL_checkstring(L, 1); */ |
| 151 |
|
| 152 |
/* /\* allocate unix object *\/ */ |
| 153 |
/* p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); */ |
| 154 |
|
| 155 |
/* /\* open serial device *\/ */ |
| 156 |
/* #if defined(_WIN32) */ |
| 157 |
/* t_socket sock = open(path, O_RDWR); */ |
| 158 |
/* #else */ |
| 159 |
/* t_socket sock = open(path, O_NOCTTY|O_RDWR); */ |
| 160 |
/* #endif */ |
| 161 |
|
| 162 |
/* /\*printf("open %s on %d\n", path, sock);*\/ */ |
| 163 |
|
| 164 |
/* if (sock < 0) { */ |
| 165 |
/* lua_pushnil(L); */ |
| 166 |
/* lua_pushstring(L, socket_strerror(errno)); */ |
| 167 |
/* lua_pushnumber(L, errno); */ |
| 168 |
/* return 3; */ |
| 169 |
/* } */ |
| 170 |
/* /\* set its type as client object *\/ */ |
| 171 |
/* auxiliar_setclass(L, "serial{client}", -1); */ |
| 172 |
/* /\* initialize remaining structure fields *\/ */ |
| 173 |
/* socket_setnonblocking(&sock); */ |
| 174 |
/* un->sock = sock; */ |
| 175 |
/* io_init(&un->io, (p_send) socket_write, (p_recv) socket_read, */ |
| 176 |
/* (p_error) socket_ioerror, &un->sock); */ |
| 177 |
/* timeout_init(&un->tm, -1, -1); */ |
| 178 |
/* buffer_init(&un->buf, &un->io, &un->tm); */ |
| 179 |
/* return 1; */ |
| 180 |
/* } */ |