l_setfield_s: Take length parameter
[libquvi.git] / src / lua / exec_scan_script_parse.c
blob62338db7f3838bb1c06254348d510d435f977c7c
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
22 * NOTE: The error messages produced in these functions are intended for
23 * developers. They would typically be seen when a new script is
24 * being developed or an old one is being maintained.
26 * These messages should be clear, indicating the actual error,
27 * minimizing the time spent on locating the problem in the script.
30 #include "config.h"
32 #include <lauxlib.h>
33 #include <glib.h>
35 #include "quvi.h"
36 /* -- */
37 #include "_quvi_s.h"
38 #include "_quvi_scan_s.h"
39 #include "_quvi_script_s.h"
40 /* -- */
41 #include "lua/setfield.h"
42 #include "lua/def.h"
44 static const gchar script_func[] = "parse";
46 static void _foreach_media_url(lua_State *l, _quvi_scan_t qs,
47 const gchar *script_path)
49 lua_pushnil(l);
50 while (lua_next(l, LI_KEY))
52 if (lua_isstring(l, LI_KEY) && lua_isstring(l, LI_VALUE))
54 const gchar *url = lua_tostring(l, LI_VALUE);
55 qs->url.media = g_slist_prepend(qs->url.media, g_strdup(url));
57 lua_pop(l, 1);
59 qs->url.media = g_slist_reverse(qs->url.media);
62 /* Check for 'qargs.media_url'. */
63 static void _chk_media_url(lua_State *l, _quvi_scan_t qs,
64 const gchar *script_path)
66 lua_pushstring(l, SS_MEDIA_URL);
67 lua_gettable(l, LI_KEY);
69 if (lua_istable(l, LI_VALUE))
70 _foreach_media_url(l, qs, script_path);
71 else
73 g_warning("%s: %s: should return a dictionary containing "
74 "the `qargs.%s'",
75 script_path, script_func, SS_MEDIA_URL);
77 lua_pop(l, 1);
80 QuviError l_exec_scan_script_parse(gpointer p, gpointer _qss,
81 const gchar *data)
83 _quvi_script_t qss;
84 _quvi_scan_t qs;
85 lua_State *l;
87 qss = (_quvi_script_t) _qss;
88 qs = (_quvi_scan_t) p;
90 l = qs->handle.quvi->handle.lua;
91 lua_pushnil(l);
93 if (luaL_dofile(l, qss->fpath->str))
94 luaL_error(l, "%s", lua_tostring(l, -1));
96 lua_getglobal(l, script_func);
98 if (!lua_isfunction(l, -1))
100 luaL_error(l, "%s: the function `%s' was not found",
101 qss->fpath->str, script_func);
104 lua_newtable(l);
105 l_set_reg_userdata(l, USERDATA_QUVI_T, (gpointer) qs->handle.quvi);
106 l_setfield_s(l, SS_INPUT_URL, qs->url.input->str, -1);
107 l_setfield_s(l, SS_CONTENT, data, -1);
109 if (lua_pcall(l, 1, 1, 0))
111 g_string_assign(qs->handle.quvi->status.errmsg, lua_tostring(l, -1));
112 return (QUVI_ERROR_SCRIPT);
115 if (!lua_istable(l, -1))
117 static const gchar *_E =
118 "%s: %s: must return a dictionary, this is typically the `qargs'";
120 luaL_error(l, _E, qss->fpath->str, script_func);
123 _chk_media_url(l, qs, qss->fpath->str);
124 lua_pop(l, 1);
126 return (QUVI_OK);
129 /* vim: set ts=2 sw=2 tw=72 expandtab: */