Using common path for lua files
[geeqie.git] / src / lua.c
1 /** \file
2  * \short LUA implementation
3  * \author Klaus Ethgen <Klaus@Ethgen.de>
4  */
5
6 /*
7  *  This file is a part of Geeqie project (http://geeqie.sourceforge.net/).
8  *  Copyright (C) 2008 - 2010 The Geeqie Team
9  *
10  *  This program is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the Free
12  *  Software Foundation; either version 2 of the License, or (at your option)
13  *  any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18  *  more details.
19  */
20
21 #include "config.h"
22
23 #ifdef HAVE_LUA
24
25 #include <lua.h>
26 #include <lauxlib.h>
27 #include <lualib.h>
28
29 #include <stdio.h>
30 #include <glib.h>
31
32 #include "glua.h"
33 #include "ui_fileops.h"
34
35 static lua_State *L; /** The LUA object needed for all operations (NOTE: That is
36                        * a upper-case variable to match the documentation!) */
37
38 /**
39  * \brief Initialize the lua interpreter.
40  */
41 void lua_init(void)
42 {
43         L = luaL_newstate();
44         luaL_openlibs(L); /* Open all libraries for lua programms */
45 }
46
47 /**
48  * \brief Call a lua function to get a single value.
49  */
50 gchar *lua_callvalue(gchar *file, gchar *function)
51 {
52         gint result;
53         gchar *data = NULL;
54         gchar *dir;
55         gchar *path;
56
57         if (file[0] == '\0')
58                 {
59                 result = luaL_dostring(L, function);
60                 }
61         else
62                 {
63                 dir = g_build_filename(get_rc_dir(), "lua", NULL);
64                 path = g_build_filename(dir, file, NULL);
65                 result = luaL_dofile(L, path);
66                 g_free(path);
67                 g_free(dir);
68                 }
69
70         if (result)
71                 {
72                 data = g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
73                 return data;
74                 }
75         data = g_strdup(lua_tostring(L, -1));
76         return data;
77 }
78
79 #endif
80 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */