-/*] Copyright (c) 2011, Charles McGarvey [*******************************
+/*] Copyright (c) 2011, Charles McGarvey [**********************************
**] All rights reserved.
*
-* vi:ts=4 sw=4 tw=75
-*
* Distributable under the terms and conditions of the 2-clause BSD license;
* see the file COPYING for a complete text of the license.
*
-**************************************************************************/
+*****************************************************************************/
#define LUA_DIALOG_NAME "dialog"
#define LUA_DIALOG_VERSION "1.0"
/**
* Fork and execute a command with arguments and optionally get a file
* descriptor connected to one of the child's own file descriptors. The
- * process id of the child is returned, or -1 on error. If fd is not NULL,
- * a pipe will be created and connected to *fd. If *fd is 0, then *fd will
- * be set to a write file descriptor connected to the child's standard
- * input. If *fd is not 0, then *fd will be set to a read file descriptor
- * set connected to the specified file descriptor of the child. In either
- * case, the caller has the responsibility to close fd when it is no longer
- * needed.
+ * process id of the child is returned, or -1 on error. If fd is not NULL, a
+ * pipe will be created and connected to *fd. If *fd is 0, then *fd will be
+ * set to a write file descriptor connected to the child's standard input. If
+ * *fd is not 0, then *fd will be set to a read file descriptor set connected
+ * to the specified file descriptor of the child. In either case, the caller
+ * has the responsibility to close fd when it is no longer needed.
*/
static pid_t myexec(const char* command, char* const argv[], int* fd)
{
int p[2];
pid_t child;
- if (fd)
- {
+ if (fd) {
if (pipe(p) != 0) return -1;
parentFd = (*fd == 0);
}
- if (!(child = fork()))
- {
- if (fd)
- {
+ if (!(child = fork())) {
+ if (fd) {
close(p[parentFd]);
if (dup2(p[!parentFd], *fd) == -1) _exit(127);
}
execv(command, argv);
_exit(127);
}
- if (child == -1)
- {
+ if (child == -1) {
if (parentFd != -1)
- {
close(p[0]); close(p[1]);
- }
return -1;
}
- if (parentFd != -1)
- {
+ if (parentFd != -1) {
close(p[!parentFd]);
*fd = p[parentFd];
}
/**
- * Read from a file descriptor until EOF and push contents to the top of
- * the Lua stack. Closes the file descriptor afterward.
+ * Read from a file descriptor until EOF and push contents to the top of the
+ * Lua stack. Closes the file descriptor afterward.
*/
static void pushstream(lua_State* L, int fd)
{
char buffer[BUFSIZ];
ssize_t bytes;
- while ((bytes = read(fd, buffer, sizeof(buffer))))
- {
+ while ((bytes = read(fd, buffer, sizeof(buffer)))) {
if (bytes == -1) break;
luaL_addlstring(&B, buffer, bytes);
}
static void writelstring(int fd, const char* str, size_t len)
{
ssize_t bytes;
- while ((bytes = write(fd, str, len)))
- {
+ while ((bytes = write(fd, str, len))) {
if (bytes == -1) break;
str += bytes;
len -= bytes;
int i; for (i = 0; argv[i]; ++i);
va_start(ap, argv);
const char* arg = va_arg(ap, const char*);
- while (arg)
- {
+ while (arg) {
argv[i++] = arg;
arg = va_arg(ap, const char*);
}
lua_getfield(L, -1, "command");
lua_getfield(L, -2, "title");
addstrings(argv, lua_tostring(L, -2), NULL);
- if (lua_isstring(L, -1))
- {
+ if (lua_isstring(L, -1)) {
addstrings(argv, "--backtitle", lua_tostring(L, -1), NULL);
}
lua_pop(L, 3);
{
if (!lua_istable(L, n)) return 0;
lua_pushnil(L);
- while (lua_next(L, n))
- {
+ while (lua_next(L, n)) {
addstrings(argv, lua_tostring(L, -2), NULL);
if (lua_isstring(L, -1)) addstrings(argv, lua_tostring(L, -1), NULL);
lua_pop(L, 1);
if (searchstrings(argv, "--item-help") != -1) fields = 3;
if (!lua_istable(L, 3)) luaL_argerror(L, 3, "menu items");
- int i; for (i = 1;; ++i)
- {
+ int i; for (i = 1;; ++i) {
lua_pushinteger(L, i);
lua_gettable(L, 3);
- if (lua_isnil(L, -1))
- {
+ if (lua_isnil(L, -1)) {
lua_pop(L, 1);
break;
}
- else if (lua_istable(L, -1))
- {
+ else if (lua_istable(L, -1)) {
int subtable = lua_gettop(L);
lua_pushnil(L);
- int j; for (j = 0; j < fields; ++j)
- {
+ int j; for (j = 0; j < fields; ++j) {
if (!lua_next(L, subtable)) luaL_argerror(L, 3, "not enough fields");
addstrings(argv, lua_tostring(L, -1), NULL);
lua_pop(L, 1);
}
lua_pop(L, 1);
}
- else
- {
+ else {
if (fields == 2) addstrings(argv, "", "", NULL);
else addstrings(argv, "", "", "", NULL);
}
{
lua_getfield(L, LUA_REGISTRYINDEX, "dialog_gauge_pid");
lua_getfield(L, LUA_REGISTRYINDEX, "dialog_gauge_fd");
- if (!lua_isnumber(L, -2) || !lua_isnumber(L, -1))
- {
+ if (!lua_isnumber(L, -2) || !lua_isnumber(L, -1)) {
lua_pop(L, 2);
return;
}
*/
static int updategauge(lua_State* L)
{
- if (!lua_isnumber(L, 1))
- {
+ if (!lua_isnumber(L, 1)) {
closegauge(L);
return 0;
}
if (0.0 <= percent && percent <= 1.0) percent *= 100.0;
lua_pushinteger(L, (lua_Integer)percent);
lua_replace(L, 1);
- if (lua_isstring(L, 2))
- {
+ if (lua_isstring(L, 2)) {
writestring(fd, "XXX");
tostream(L, 1, fd);
tostream(L, 2, fd);
writestring(fd, "XXX");
}
- else
- {
+ else {
tostream(L, 1, fd);
}
return 0;
LUALIB_API int luaopen_dialog(lua_State* L)
{
const struct luaL_Reg dialog_funcs[] = {
- {"gauge", dialog_gauge},
+ {"gauge", dialog_gauge},
{"inputbox", dialog_inputbox},
- {"menu", dialog_menu},
- {"msgbox", dialog_msgbox},
- {"yesno", dialog_yesno},
+ {"menu", dialog_menu},
+ {"msgbox", dialog_msgbox},
+ {"yesno", dialog_yesno},
{NULL, NULL}
};
luaL_register(L, LUA_DIALOG_NAME, dialog_funcs);
const char* names[] = {getenv("DIALOG"), "dialog", "cdialog"};
- int i; for (i = 0; i < 3; ++i)
- {
- if (names[i])
- {
+ int i; for (i = 0; i < 3; ++i) {
+ if (names[i]) {
char* path = strdup(getenv("PATH"));
char* token; char** paths = &path;
- while ((token = strsep(paths, ":")))
- {
+ while ((token = strsep(paths, ":"))) {
luaL_Buffer B;
luaL_buffinit(L, &B);
luaL_addstring(&B, token);
luaL_addstring(&B, "/");
luaL_addstring(&B, names[i]);
luaL_pushresult(&B);
- if (access(lua_tostring(L, -1), X_OK) == 0)
- {
+ if (access(lua_tostring(L, -1), X_OK) == 0) {
lua_setfield(L, -2, "command");
goto break2;
}
free(path);
}
}
- luaL_error(L, "cannot find dialog executable in the path; set DIALOG");
+ luaL_error(L, "cannot find dialog executable in the path; set DIALOG");
break2: