I’m a total Enterprise noob, and don’t really have any experience with C yet either.
We are trying to use a websocket library, which relies on a C file to extend the string library. I’ve copied over the sample enterprise app, and replaced the main.lua with my own, but I’m not exactly sure what I should be doing.
For now we are not trying to build a new plugin, just get something to work, but I cant find any good tutorials on using Enterprise. It seems that because it’s for more advanced projects, it’s assumed that we already know what to do, which to some degree is probably a fair assumption.
Can anyone explain to me very simply, how I can integrate a single C file into a project. This is the C code in question:
#define OP\_ZSTRING 'z' /\* zero-terminated string \*/ #define OP\_BSTRING 'p' /\* string preceded by length byte \*/ #define OP\_WSTRING 'P' /\* string preceded by length word \*/ #define OP\_SSTRING 'a' /\* string preceded by length size\_t \*/ #define OP\_STRING 'A' /\* string \*/ #define OP\_FLOAT 'f' /\* float \*/ #define OP\_DOUBLE 'd' /\* double \*/ #define OP\_NUMBER 'n' /\* Lua number \*/ #define OP\_CHAR 'c' /\* char \*/ #define OP\_BYTE 'b' /\* byte = unsigned char \*/ #define OP\_SHORT 'h' /\* short \*/ #define OP\_USHORT 'H' /\* unsigned short \*/ #define OP\_INT 'i' /\* int \*/ #define OP\_UINT 'I' /\* unsigned int \*/ #define OP\_LONG 'l' /\* long \*/ #define OP\_ULONG 'L' /\* unsigned long \*/ #define OP\_LITTLEENDIAN '\<' /\* little endian \*/ #define OP\_BIGENDIAN '\>' /\* big endian \*/ #define OP\_NATIVE '=' /\* native endian \*/ #include \<ctype.h\> #include \<string.h\> #include "lua.h" #include "lualib.h" #include "lauxlib.h" #include "lpack.h" static void badcode(lua\_State \*L, int c) { char s[]="bad code `?'"; s[sizeof(s)-3]=c; luaL\_argerror(L,1,s); } static int doendian(int c) { int x=1; int e=\*(char\*)&x; if (c==OP\_LITTLEENDIAN) return !e; if (c==OP\_BIGENDIAN) return e; if (c==OP\_NATIVE) return 0; return 0; } static void doswap(int swap, void \*p, size\_t n) { if (swap) { char \*a=(char\*)p; int i,j; for (i=0, j=n-1, n=n/2; n--; i++, j--) { char t=a[i]; a[i]=a[j]; a[j]=t; } } } #define UNPACKNUMBER(OP,T) \ case OP: \ { \ T a; \ int m=sizeof(a); \ if (i+m\>len) goto done; \ memcpy(&a,s+i,m); \ i+=m; \ doswap(swap,&a,m); \ lua\_pushnumber(L,(lua\_Number)a); \ ++n; \ break; \ } #define UNPACKSTRING(OP,T) \ case OP: \ { \ T l; \ int m=sizeof(l); \ if (i+m\>len) goto done; \ memcpy(&l,s+i,m); \ doswap(swap,&l,m); \ if (i+m+l\>len) goto done; \ i+=m; \ lua\_pushlstring(L,s+i,l); \ i+=l; \ ++n; \ break; \ } static int l\_unpack(lua\_State \*L) /\*\* unpack(s,f,[init]) \*/ { size\_t len; const char \*s=luaL\_checklstring(L,1,&len); const char \*f=luaL\_checkstring(L,2); int i=luaL\_optnumber(L,3,1)-1; int n=0; int swap=0; lua\_pushnil(L); while (\*f) { int c=\*f++; int N=1; if (isdigit(\*f)) { N=0; while (isdigit(\*f)) N=10\*N+(\*f++)-'0'; if (N==0 && c==OP\_STRING) { lua\_pushliteral(L,""); ++n; } } while (N--) switch (c) { case OP\_LITTLEENDIAN: case OP\_BIGENDIAN: case OP\_NATIVE: { swap=doendian(c); N=0; break; } case OP\_STRING: { ++N; if (i+N\>len) goto done; lua\_pushlstring(L,s+i,N); i+=N; ++n; N=0; break; } case OP\_ZSTRING: { size\_t l; if (i\>=len) goto done; l=strlen(s+i); lua\_pushlstring(L,s+i,l); i+=l+1; ++n; break; } UNPACKSTRING(OP\_BSTRING, unsigned char) UNPACKSTRING(OP\_WSTRING, unsigned short) UNPACKSTRING(OP\_SSTRING, size\_t) UNPACKNUMBER(OP\_NUMBER, lua\_Number) UNPACKNUMBER(OP\_DOUBLE, double) UNPACKNUMBER(OP\_FLOAT, float) UNPACKNUMBER(OP\_CHAR, char) UNPACKNUMBER(OP\_BYTE, unsigned char) UNPACKNUMBER(OP\_SHORT, short) UNPACKNUMBER(OP\_USHORT, unsigned short) UNPACKNUMBER(OP\_INT, int) UNPACKNUMBER(OP\_UINT, unsigned int) UNPACKNUMBER(OP\_LONG, long) UNPACKNUMBER(OP\_ULONG, unsigned long) case ' ': case ',': break; default: badcode(L,c); break; } } done: lua\_pushnumber(L,i+1); lua\_replace(L,-n-2); return n+1; } static int l\_pack(lua\_State \*L) /\*\* pack(f,...) \*/ { int i=2; const char \*f=luaL\_checkstring(L,1); int swap=0; luaL\_Buffer b; luaL\_buffinit(L,&b); while (\*f) { int c=\*f++; int N=1; if (isdigit(\*f)) { N=0; while (isdigit(\*f)) N=10\*N+(\*f++)-'0'; } while (N--) switch (c) { case OP\_LITTLEENDIAN: case OP\_BIGENDIAN: case OP\_NATIVE: { swap=doendian(c); N=0; break; } case OP\_STRING: case OP\_ZSTRING: { size\_t l; const char \*a=luaL\_checklstring(L,i++,&l); luaL\_addlstring(&b,a,l+(c==OP\_ZSTRING)); break; } PACKSTRING(OP\_BSTRING, unsigned char) PACKSTRING(OP\_WSTRING, unsigned short) PACKSTRING(OP\_SSTRING, size\_t) PACKNUMBER(OP\_NUMBER, lua\_Number) PACKNUMBER(OP\_DOUBLE, double) PACKNUMBER(OP\_FLOAT, float) PACKNUMBER(OP\_CHAR, char) PACKNUMBER(OP\_BYTE, unsigned char) PACKNUMBER(OP\_SHORT, short) PACKNUMBER(OP\_USHORT, unsigned short) PACKNUMBER(OP\_INT, int) PACKNUMBER(OP\_UINT, unsigned int) PACKNUMBER(OP\_LONG, long) PACKNUMBER(OP\_ULONG, unsigned long) case ' ': case ',': break; default: badcode(L,c); break; } } luaL\_pushresult(&b); return 1; } static const luaL\_reg R[] = { {"pack", l\_pack}, {"unpack", l\_unpack}, {NULL, NULL} }; int luaopen\_pack(lua\_State \*L) { #ifdef USE\_GLOBALS lua\_register(L,"bpack",l\_pack); lua\_register(L,"bunpack",l\_unpack); #else luaL\_openlib(L, LUA\_STRLIBNAME, R, 0); #endif return 0; }