lua/exec_scan_script_parse.c: Revise messages
[libquvi.git] / src / lua / exec_scan_script_parse.c
blobc37e35acbcbbfb63229cfbc4ae481ffd293e8cee
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
20 #include "config.h"
22 #include <lauxlib.h>
23 #include <glib.h>
25 #include "quvi.h"
26 /* -- */
27 #include "_quvi_s.h"
28 #include "_quvi_scan_s.h"
29 #include "_quvi_script_s.h"
30 /* -- */
31 #include "lua/setfield.h"
32 #include "lua/def.h"
35 * NOTE: The error messages produced in these functions are intended for
36 * developers. They would typically be seen when a new media script is
37 * being developed or an old one is being maintained.
39 * The messages should be clear, indicating the actual error, minimizing
40 * the time spent on locating the actual problem in the script.
43 static const gchar script_func[] = "parse";
45 static void _foreach_media_url(lua_State *l, _quvi_scan_t qs,
46 const gchar *script_path)
48 lua_pushnil(l);
49 while (lua_next(l, LI_KEY))
51 if (lua_isstring(l, LI_KEY) && lua_isstring(l, LI_VALUE))
53 const gchar *url = lua_tostring(l, LI_VALUE);
54 qs->url.media = g_slist_prepend(qs->url.media, g_strdup(url));
56 lua_pop(l, 1);
58 qs->url.media = g_slist_reverse(qs->url.media);
61 /* Check for 'qargs.media_url'. */
62 static void _chk_media_url(lua_State *l, _quvi_scan_t qs,
63 const gchar *script_path)
65 lua_pushstring(l, SS_MEDIA_URL);
66 lua_gettable(l, LI_KEY);
68 if (lua_istable(l, LI_VALUE))
69 _foreach_media_url(l, qs, script_path);
70 else
72 g_warning("%s: %s: should return a dictionary containing "
73 "the `qargs.%s'",
74 script_path, script_func, SS_MEDIA_URL);
76 lua_pop(l, 1);
79 QuviError l_exec_scan_script_parse(gpointer p, gpointer _qss,
80 const gchar *data)
82 _quvi_script_t qss;
83 _quvi_scan_t qs;
84 lua_State *l;
86 qss = (_quvi_script_t) _qss;
87 qs = (_quvi_scan_t) p;
89 l = qs->handle.quvi->handle.lua;
90 lua_pushnil(l);
92 if (luaL_dofile(l, qss->fpath->str))
93 luaL_error(l, "%s", lua_tostring(l, -1));
95 lua_getglobal(l, script_func);
97 if (!lua_isfunction(l, -1))
99 luaL_error(l, "%s: the function `%s' was not found",
100 qss->fpath->str, script_func);
103 lua_newtable(l);
104 l_set_reg_userdata(l, USERDATA_QUVI_T, (gpointer) qs->handle.quvi);
105 l_setfield_b(l, GS_VERBOSE, qs->handle.quvi->opt.scripts.verbose);
106 l_setfield_s(l, SS_INPUT_URL, qs->url.input->str);
107 l_setfield_s(l, SS_CONTENT, data);
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: */