media: ident: _chk_results: Rewrite for can_parse_url
[libquvi.git] / src / lua / exec_media_script_ident.c
blobf079c7004642ee4f3f00719f52610a10b3692a3b
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_media_s.h"
29 #include "_quvi_script_s.h"
30 /* -- */
31 #include "lua/getfield.h"
32 #include "lua/setfield.h"
33 #include "lua/chk.h"
34 #include "lua/def.h"
37 * NOTE: The error messages produced in these functions are intended for
38 * developers. They would typically be seen when a new media script is
39 * being developed.
41 * The messages should be clear, indicating the actual error, minimizing
42 * the time spent on locating the actual problem in the script.
45 static const gchar script_func[] = "ident";
47 static QuviError _chk_results(lua_State *l, _quvi_script_t qs,
48 _quvi_media_t qm)
50 QuviError r;
52 r = (l_chk_can_parse_url(l, qs, MS_CAN_PARSE_URL,
53 MS_DOMAINS, script_func) == TRUE)
54 ? QUVI_OK
55 : QUVI_ERROR_NO_SUPPORT;
57 lua_pop(l, 1);
58 return (r);
61 QuviError l_exec_media_script_ident(gpointer p, GSList *sl)
63 _quvi_script_t qs;
64 _quvi_media_t qm;
65 lua_State *l;
67 qm = (_quvi_media_t) p;
68 l = qm->handle.quvi->handle.lua;
70 lua_pushnil(l);
71 qs = (_quvi_script_t) sl->data;
73 if (luaL_dofile(l, qs->fpath->str))
74 luaL_error(l, "%s", lua_tostring(l, -1));
76 lua_getglobal(l, script_func);
78 if (!lua_isfunction(l, -1))
79 luaL_error(l, "%s: function `%s' not found", qs->fpath->str, script_func);
81 lua_newtable(l);
82 l_setfield_b(l, GS_VERBOSE, qm->handle.quvi->opt.scripts.verbose);
83 l_setfield_s(l, MS_INPUT_URL, qm->url.input->str);
85 if (lua_pcall(l, 1, 1, 0))
87 g_string_assign(qm->handle.quvi->status.errmsg, lua_tostring(l, -1));
88 return (QUVI_ERROR_SCRIPT);
91 if (!lua_istable(l, -1))
93 luaL_error(l, "%s: %s: must return a dictionary",
94 qs->fpath->str, script_func);
96 return (_chk_results(l, qs, qm));
99 /* vim: set ts=2 sw=2 tw=72 expandtab: */