Reduce differences with root_skels in contrib.
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / lib / lua / dfui / common.c
blob481da2512afdfabc97d917037b858e9f4082fb36
1 /*
2 * Copyright (c) 2004 Scott Ullrich <GeekGod@GeekGod.com>
3 * Portions Copyright (c) 2004 Chris Pressey <cpressey@catseye.mine.nu>
5 * Copyright (c) 2004 The DragonFly Project.
6 * All rights reserved.
8 * This code is derived from software contributed to The DragonFly Project
9 * by Scott Ullrich and Chris Pressey (see above for e-mail addresses).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
21 * distribution.
23 * 3. Neither the name of The DragonFly Project nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific, prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT HOLDERS, CONTRIBUTORS OR VOICES IN THE AUTHOR'S HEAD
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY
33 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
34 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
37 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
38 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
41 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
43 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
49 /*
50 * common.c - common functions for dfuibe_lua
51 * $Id: common.c,v 1.56 2005/04/04 13:56:37 den Exp $
54 #include <sys/stat.h>
55 #include <sys/types.h>
57 #include <dirent.h>
58 #include <err.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include <sysexits.h>
62 #include <unistd.h>
64 #include "lua50/lua.h"
65 #include "lua50/lauxlib.h"
66 #include "lua50/lualib.h"
68 #include "lua_dfui.h"
70 /*----------------------------- utilities ---------------------------*/
73 * Given the names of two global variables, the first a regular
74 * 'Class' table, and the second a metatable which will be attached
75 * to all object 'instances':
76 * - add an __index property to the metatable that redirects
77 * all accesses on the instance to the class table;
78 * - add a __metatable propery to the metatable, to hide it.
80 void
81 lua_set_instance_handler(lua_State *L,
82 const char *table_name, const char *metatable_name)
84 int metatable_idx, methods_idx;
86 lua_pushstring(L, table_name); /* name of our 'class' table */
87 lua_gettable(L, LUA_GLOBALSINDEX); /* get it from globals */
88 methods_idx = lua_gettop(L); /* Find its position on the stack */
90 lua_pushstring(L, metatable_name); /* name of our metatable */
91 lua_gettable(L, LUA_GLOBALSINDEX); /* get it from globals */
92 metatable_idx = lua_gettop(L); /* Find its position on the stack */
95 * Add __index event to metatable (metatable.__index = methods).
96 * This lets the Lua script refer to the methods by indexing
97 * the instance variable like so: x:y(z).
99 lua_pushliteral(L, "__index");
100 lua_pushvalue(L, methods_idx);
101 lua_settable(L, metatable_idx);
103 lua_pushliteral(L, "__metatable"); /* hide metatable */
104 lua_pushvalue(L, methods_idx);
105 lua_settable(L, metatable_idx); /* metatable.__metatable = methods */
107 lua_pop(L, 2);
111 * Retrieve a string from a Lua table.
113 const char *
114 lua_access_table_string(lua_State *L, int table_idx, const char *key)
116 const char *s;
118 lua_pushlstring(L, key, strlen(key));
119 lua_gettable(L, table_idx);
120 if (lua_isstring(L, lua_gettop(L))) {
121 s = luaL_checkstring(L, lua_gettop(L));
122 } else {
123 s = "";
125 lua_pop(L, 1);
127 return(s);
131 * This function is adapted from liolib.c: push a FILE * onto the
132 * Lua stack as a file object that Lua's file module understands.
134 void
135 lua_pushfileptr(lua_State *L, FILE *f)
137 FILE **pf;
139 pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
140 *pf = f;
141 luaL_getmetatable(L, "FILE*");
142 lua_setmetatable(L, -2);
145 void
146 lua_show_debug(lua_State *L)
148 lua_Debug X;
150 lua_getstack(L, 0, &X);
151 lua_getinfo(L, "nluS", &X);
152 fprintf(stderr, "--+-- BEGIN Lua Debug Info --+--\n");
153 fprintf(stderr, "source: %s\n", X.short_src);
154 fprintf(stderr, "linedefined: %d\n", X.linedefined);
155 fprintf(stderr, "what: %s\n", X.what);
156 fprintf(stderr, "name: %s\n", X.name);
157 fprintf(stderr, "namewhat: %s\n", X.namewhat);
158 fprintf(stderr, "nups: %d\n", X.nups);
159 fprintf(stderr, "--+-- END Lua Debug Info --+--\n");
162 /*------------------------ module entry point ----------------------*/
164 LUA_API int
165 luaopen_ldfui(lua_State *L)
167 int container_idx;
170 * Push a new table, which will contain all of our sub-packages,
171 * and right before it, push the name that we will give it.
173 lua_pushstring(L, "DFUI");
174 lua_newtable(L);
177 * Find out where the table is, so we can refer to it easily.
179 container_idx = lua_gettop(L);
182 * Initialize each sub-package and push it on the stack, then
183 * assign it to a slot in our master table.
186 lua_pushliteral(L, "Connection");
187 lua_dfui_register(L);
188 lua_settable(L, container_idx);
190 lua_pushliteral(L, "Progress");
191 lua_dfui_progress_register(L);
192 lua_settable(L, container_idx);
195 * Now that all that is done, put our master table into the
196 * globals table, using the name we gave it at the beginning.
198 lua_settable(L, LUA_GLOBALSINDEX);
201 * Get our master table out of the globals and push it onto
202 * the stack, so we can return it to whatever script require()d us.
204 lua_pushstring(L, "DFUI");
205 lua_gettable(L, LUA_GLOBALSINDEX);
207 return(1);