X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=build%2Fdialog.c;h=e4c76d723ede46818557fbb4d7568548d6db9fdb;hp=ee8a2a8a669c61157eb4ccdced3ca442ca807858;hb=574af38ed616d1adfa5e6ce35f67cda1f707f89d;hpb=6c9943707d4f33035830eba0587a61a34eaecbc2 diff --git a/build/dialog.c b/build/dialog.c index ee8a2a8..e4c76d7 100644 --- a/build/dialog.c +++ b/build/dialog.c @@ -1,13 +1,11 @@ -/*] 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" @@ -41,13 +39,12 @@ static void printarray(const char* argv[]) /** * 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) { @@ -55,31 +52,24 @@ 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]; } @@ -99,8 +89,8 @@ static int mywait(pid_t pid) /** - * 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) { @@ -109,8 +99,7 @@ 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); } @@ -125,8 +114,7 @@ static void pushstream(lua_State* L, int fd) 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; @@ -168,8 +156,7 @@ static void addstrings(const char* argv[], ...) 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*); } @@ -197,8 +184,7 @@ static void addcommand(lua_State* L, const char* argv[]) 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); @@ -261,8 +247,7 @@ static int addextra(lua_State* L, const char* argv[], int n) { 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); @@ -296,29 +281,24 @@ static void addmenuitems(lua_State* L, const char* argv[]) 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); } @@ -356,8 +336,7 @@ static void closegauge(lua_State* L) { 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; } @@ -385,8 +364,7 @@ static void closegauge(lua_State* L) */ static int updategauge(lua_State* L) { - if (!lua_isnumber(L, 1)) - { + if (!lua_isnumber(L, 1)) { closegauge(L); return 0; } @@ -399,15 +377,13 @@ static int updategauge(lua_State* L) 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; @@ -530,32 +506,28 @@ static int dialog_yesno(lua_State* L) 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; } @@ -564,7 +536,7 @@ LUALIB_API int luaopen_dialog(lua_State* L) 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: