Add misc/url.* with m_url_(un)escaped_form
[libquvi.git] / src / lua / load_util_script.c
bloba0e01a6ec47c63f2d5cec466d047dbe5a45fbf38
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/>.
21 #include "config.h"
23 #include <glib/gi18n-lib.h>
24 #include <glib.h>
25 #include <lauxlib.h>
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 #include "_quvi_script_s.h"
31 /* -- */
32 #include "lua/def.h"
33 #include "lua/setfield.h"
35 static GSList *_match_util_script(_quvi_t q, const gchar *w)
37 _quvi_script_t qs;
38 GSList *curr;
39 gboolean r;
40 gchar *s;
42 curr = q->scripts.util;
43 while (curr != NULL)
45 qs = (_quvi_script_t) curr->data;
46 s = g_path_get_basename(qs->fpath->str);
47 r = (g_strcmp0(s, w) == 0) ? TRUE:FALSE;
49 g_free(s);
50 s = NULL;
52 if (r == TRUE)
53 break;
55 curr = g_slist_next(curr);
57 return (curr);
60 QuviError l_load_util_script(_quvi_t q, const gchar *script_fname,
61 const gchar *script_func)
63 _quvi_script_t qs;
64 lua_State *l;
65 GSList *s;
67 s = _match_util_script(q, script_fname);
68 l = q->handle.lua;
70 if (s == NULL)
72 luaL_error(l, _("Could not the find utility script `%s' in the path"),
73 script_fname);
76 lua_pushnil(l);
77 lua_getglobal(l, script_func);
79 qs = (_quvi_script_t) s->data;
81 if (luaL_dofile(l, qs->fpath->str))
82 luaL_error(l, "%s", lua_tostring(l, -1));
84 lua_getglobal(l, script_func);
86 if (!lua_isfunction(l, -1))
88 luaL_error(l, "%s: the function `%s' was not found",
89 qs->fpath->str, script_func);
92 lua_newtable(l);
93 l_set_reg_userdata(l, USERDATA_QUVI_T, (gpointer) q);
95 return (QUVI_OK);
98 /* vim: set ts=2 sw=2 tw=72 expandtab: */