+char *redisProtocolToLuaType_Int(lua_State *lua, char *reply);
+char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply);
+char *redisProtocolToLuaType_Status(lua_State *lua, char *reply);
+char *redisProtocolToLuaType_Error(lua_State *lua, char *reply);
+char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply);
+int redis_math_random (lua_State *L);
+int redis_math_randomseed (lua_State *L);
+
+/* Take a Redis reply in the Redis protocol format and convert it into a
+ * Lua type. Thanks to this function, and the introduction of not connected
+ * clients, it is trvial to implement the redis() lua function.
+ *
+ * Basically we take the arguments, execute the Redis command in the context
+ * of a non connected client, then take the generated reply and convert it
+ * into a suitable Lua type. With this trick the scripting feature does not
+ * need the introduction of a full Redis internals API. Basically the script
+ * is like a normal client that bypasses all the slow I/O paths.
+ *
+ * Note: in this function we do not do any sanity check as the reply is
+ * generated by Redis directly. This allows us to go faster.
+ * The reply string can be altered during the parsing as it is discared
+ * after the conversion is completed.
+ *
+ * Errors are returned as a table with a single 'err' field set to the
+ * error string.
+ */
+
+char *redisProtocolToLuaType(lua_State *lua, char* reply) {
+ char *p = reply;
+
+ switch(*p) {
+ case ':':
+ p = redisProtocolToLuaType_Int(lua,reply);
+ break;
+ case '$':
+ p = redisProtocolToLuaType_Bulk(lua,reply);
+ break;
+ case '+':
+ p = redisProtocolToLuaType_Status(lua,reply);
+ break;
+ case '-':
+ p = redisProtocolToLuaType_Error(lua,reply);
+ break;
+ case '*':
+ p = redisProtocolToLuaType_MultiBulk(lua,reply);
+ break;
+ }
+ return p;
+}
+
+char *redisProtocolToLuaType_Int(lua_State *lua, char *reply) {
+ char *p = strchr(reply+1,'\r');
+ long long value;
+
+ string2ll(reply+1,p-reply-1,&value);
+ lua_pushnumber(lua,(lua_Number)value);
+ return p+2;
+}
+
+char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply) {
+ char *p = strchr(reply+1,'\r');
+ long long bulklen;
+
+ string2ll(reply+1,p-reply-1,&bulklen);
+ if (bulklen == -1) {
+ lua_pushboolean(lua,0);
+ return p+2;
+ } else {
+ lua_pushlstring(lua,p+2,bulklen);
+ return p+2+bulklen+2;
+ }
+}
+
+char *redisProtocolToLuaType_Status(lua_State *lua, char *reply) {
+ char *p = strchr(reply+1,'\r');
+
+ lua_newtable(lua);
+ lua_pushstring(lua,"ok");
+ lua_pushlstring(lua,reply+1,p-reply-1);
+ lua_settable(lua,-3);
+ return p+2;
+}
+
+char *redisProtocolToLuaType_Error(lua_State *lua, char *reply) {
+ char *p = strchr(reply+1,'\r');
+
+ lua_newtable(lua);
+ lua_pushstring(lua,"err");
+ lua_pushlstring(lua,reply+1,p-reply-1);
+ lua_settable(lua,-3);
+ return p+2;
+}
+
+char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply) {
+ char *p = strchr(reply+1,'\r');
+ long long mbulklen;
+ int j = 0;
+
+ string2ll(reply+1,p-reply-1,&mbulklen);
+ p += 2;
+ if (mbulklen == -1) {
+ lua_pushboolean(lua,0);
+ return p;
+ }
+ lua_newtable(lua);
+ for (j = 0; j < mbulklen; j++) {
+ lua_pushnumber(lua,j+1);
+ p = redisProtocolToLuaType(lua,p);
+ lua_settable(lua,-3);
+ }
+ return p;
+}
+
+void luaPushError(lua_State *lua, char *error) {
+ lua_newtable(lua);
+ lua_pushstring(lua,"err");
+ lua_pushstring(lua, error);
+ lua_settable(lua,-3);
+}
+
+/* Sort the array currently in the stack. We do this to make the output
+ * of commands like KEYS or SMEMBERS something deterministic when called
+ * from Lua (to play well with AOf/replication).
+ *
+ * The array is sorted using table.sort itself, and assuming all the
+ * list elements are strings. */
+void luaSortArray(lua_State *lua) {
+ /* Initial Stack: array */
+ lua_getglobal(lua,"table");
+ lua_pushstring(lua,"sort");
+ lua_gettable(lua,-2); /* Stack: array, table, table.sort */
+ lua_pushvalue(lua,-3); /* Stack: array, table, table.sort, array */
+ if (lua_pcall(lua,1,0,0)) {
+ /* Stack: array, table, error */
+
+ /* We are not interested in the error, we assume that the problem is
+ * that there are 'false' elements inside the array, so we try
+ * again with a slower function but able to handle this case, that
+ * is: table.sort(table, __redis__compare_helper) */
+ lua_pop(lua,1); /* Stack: array, table */
+ lua_pushstring(lua,"sort"); /* Stack: array, table, sort */
+ lua_gettable(lua,-2); /* Stack: array, table, table.sort */
+ lua_pushvalue(lua,-3); /* Stack: array, table, table.sort, array */
+ lua_getglobal(lua,"__redis__compare_helper");
+ /* Stack: array, table, table.sort, array, __redis__compare_helper */
+ lua_call(lua,2,0);
+ }
+ /* Stack: array (sorted), table */
+ lua_pop(lua,1); /* Stack: array (sorted) */
+}
+
+int luaRedisGenericCommand(lua_State *lua, int raise_error) {