Skip to content

Commit 6880075

Browse files
committed
More code cleanups
1 parent c3a73e4 commit 6880075

File tree

2 files changed

+3
-221
lines changed

2 files changed

+3
-221
lines changed

scripting/lua_scripting.cpp

Lines changed: 2 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// General Lua scripting support
2+
13
#include "stdafx.h"
24
#include "..\mainfrm.h"
35
#include "..\MUSHclient.h"
@@ -771,224 +773,3 @@ int CallLuaWithTraceBack (lua_State *L, const int iArguments, const int iReturn)
771773
return error;
772774
} // end of CallLuaWithTraceBack
773775

774-
//-------------------------------------------------------------------------------
775-
// stuff for popen, pclose
776-
//-------------------------------------------------------------------------------
777-
778-
// see: http://lua-users.org/wiki/PipesOnWindows
779-
780-
/*
781-
782-
This is all pretty crappy.
783-
784-
I don't like the way it builds up a command (eg. cmd.exe /c blah blah )
785-
786-
We need to tap into the metatable for io to handle the close correctly.
787-
788-
And, it just crashes when I test it. ;)
789-
790-
*/
791-
792-
793-
#undef POPEN_STUFF_WHICH_DOESNT_WORK
794-
795-
#ifdef POPEN_STUFF_WHICH_DOESNT_WORK
796-
/*------------------------------------------------------------------------------
797-
Globals for the Routines pt_popen() / pt_pclose()
798-
------------------------------------------------------------------------------*/
799-
static HANDLE my_pipein[2], my_pipeout[2], my_pipeerr[2];
800-
static char my_popenmode = ' ';
801-
802-
static int
803-
my_pipe(HANDLE *readwrite)
804-
{
805-
SECURITY_ATTRIBUTES sa;
806-
807-
sa.nLength = sizeof(sa); /* Length in bytes */
808-
sa.bInheritHandle = 1; /* the child must inherit these handles */
809-
sa.lpSecurityDescriptor = NULL;
810-
811-
if (! CreatePipe (&readwrite[0],&readwrite[1],&sa,1 << 13))
812-
{
813-
errno = -1; /* EMFILE; que? */
814-
return -1;
815-
}
816-
817-
return 0;
818-
}
819-
820-
/*------------------------------------------------------------------------------
821-
Replacement for 'popen()' under WIN32.
822-
NOTE: if cmd contains '2>&1', we connect the standard error file handle
823-
to the standard output file handle.
824-
------------------------------------------------------------------------------*/
825-
FILE *
826-
pt_popen(const char *cmd, const char *mode)
827-
{
828-
FILE *fptr = (FILE *)0;
829-
PROCESS_INFORMATION piProcInfo;
830-
STARTUPINFO siStartInfo;
831-
int success, redirect_error = 0;
832-
char cmd_buff[2048];
833-
char *err2out;
834-
835-
const char *shell_cmd = getenv("COMSPEC");
836-
if (! shell_cmd) shell_cmd = "cmd";
837-
strcpy(cmd_buff,shell_cmd);
838-
strcat(cmd_buff," /c ");
839-
strcat(cmd_buff,cmd);
840-
841-
my_pipein[0] = INVALID_HANDLE_VALUE;
842-
my_pipein[1] = INVALID_HANDLE_VALUE;
843-
my_pipeout[0] = INVALID_HANDLE_VALUE;
844-
my_pipeout[1] = INVALID_HANDLE_VALUE;
845-
my_pipeerr[0] = INVALID_HANDLE_VALUE;
846-
my_pipeerr[1] = INVALID_HANDLE_VALUE;
847-
848-
if (!mode || !*mode)
849-
goto finito;
850-
851-
my_popenmode = *mode;
852-
if (my_popenmode != 'r' && my_popenmode != 'w')
853-
goto finito;
854-
855-
/*
856-
* Shall we redirect stderr to stdout ? */
857-
if ((err2out = strstr("2>&1",cmd)) != NULL) {
858-
/* this option doesn't apply to win32 shells, so we clear it out! */
859-
strncpy(err2out," ",4);
860-
redirect_error = 1;
861-
}
862-
863-
/*
864-
* Create the Pipes... */
865-
if (my_pipe(my_pipein) == -1 ||
866-
my_pipe(my_pipeout) == -1)
867-
goto finito;
868-
if (!redirect_error && my_pipe(my_pipeerr) == -1)
869-
goto finito;
870-
871-
/*
872-
* Now create the child process */
873-
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
874-
siStartInfo.cb = sizeof(STARTUPINFO);
875-
siStartInfo.hStdInput = my_pipein[0];
876-
siStartInfo.hStdOutput = my_pipeout[1];
877-
if (redirect_error)
878-
siStartInfo.hStdError = my_pipeout[1];
879-
else
880-
siStartInfo.hStdError = my_pipeerr[1];
881-
siStartInfo.dwFlags = STARTF_USESTDHANDLES;
882-
883-
success = CreateProcess(NULL,
884-
(LPTSTR)cmd_buff, // command line
885-
NULL, // process security attributes
886-
NULL, // primary thread security attributes
887-
TRUE, // handles are inherited
888-
DETACHED_PROCESS, // creation flags: without window (?)
889-
NULL, // use parent's environment
890-
NULL, // use parent's current directory
891-
&siStartInfo, // STARTUPINFO pointer
892-
&piProcInfo); // receives PROCESS_INFORMATION
893-
894-
if (!success)
895-
goto finito;
896-
897-
/*
898-
* These handles listen to the Child process */
899-
CloseHandle(my_pipein[0]); my_pipein[0] = INVALID_HANDLE_VALUE;
900-
CloseHandle(my_pipeout[1]); my_pipeout[1] = INVALID_HANDLE_VALUE;
901-
CloseHandle(my_pipeerr[1]); my_pipeerr[1] = INVALID_HANDLE_VALUE;
902-
903-
if (my_popenmode == 'r')
904-
fptr = _fdopen(_open_osfhandle((long)my_pipeout[0],_O_BINARY),"r");
905-
else
906-
fptr = _fdopen(_open_osfhandle((long)my_pipein[1],_O_BINARY),"w");
907-
908-
finito:
909-
if (!fptr)
910-
{
911-
if (my_pipein[0] != INVALID_HANDLE_VALUE)
912-
CloseHandle(my_pipein[0]);
913-
if (my_pipein[1] != INVALID_HANDLE_VALUE)
914-
CloseHandle(my_pipein[1]);
915-
if (my_pipeout[0] != INVALID_HANDLE_VALUE)
916-
CloseHandle(my_pipeout[0]);
917-
if (my_pipeout[1] != INVALID_HANDLE_VALUE)
918-
CloseHandle(my_pipeout[1]);
919-
if (my_pipeerr[0] != INVALID_HANDLE_VALUE)
920-
CloseHandle(my_pipeerr[0]);
921-
if (my_pipeerr[1] != INVALID_HANDLE_VALUE)
922-
CloseHandle(my_pipeerr[1]);
923-
}
924-
return fptr;
925-
926-
}
927-
928-
/*------------------------------------------------------------------------------
929-
Replacement for 'pclose()' under WIN32
930-
------------------------------------------------------------------------------*/
931-
int
932-
pt_pclose(FILE *fle)
933-
{
934-
if (fle)
935-
{
936-
(void)fclose(fle);
937-
938-
CloseHandle(my_pipeerr[0]);
939-
if (my_popenmode == 'r')
940-
CloseHandle(my_pipein[1]);
941-
else
942-
CloseHandle(my_pipeout[0]);
943-
return 0;
944-
}
945-
return -1;
946-
}
947-
948-
949-
// copies from Lua source
950-
951-
static FILE **newfile (lua_State *L) {
952-
FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
953-
*pf = NULL; /* file handle is currently `closed' */
954-
luaL_getmetatable(L, LUA_FILEHANDLE);
955-
lua_setmetatable(L, -2);
956-
return pf;
957-
}
958-
959-
static int pushresult (lua_State *L, int i, const char *filename) {
960-
int en = errno; /* calls to Lua API may change this value */
961-
if (i) {
962-
lua_pushboolean(L, 1);
963-
return 1;
964-
}
965-
else {
966-
lua_pushnil(L);
967-
if (filename)
968-
lua_pushfstring(L, "%s: %s", filename, strerror(en));
969-
else
970-
lua_pushfstring(L, "%s", strerror(en));
971-
lua_pushinteger(L, en);
972-
return 3;
973-
}
974-
}
975-
976-
int win_io_popen (lua_State *L) {
977-
const char *filename = luaL_checkstring(L, 1);
978-
const char *mode = luaL_optstring(L, 2, "r");
979-
FILE **pf = newfile(L);
980-
*pf = pt_popen(filename, mode);
981-
return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
982-
}
983-
984-
#define topfile(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
985-
986-
int win_io_pclose (lua_State *L) {
987-
FILE **p = topfile(L);
988-
int ok = pt_pclose(*p) != -1;
989-
*p = NULL;
990-
return pushresult(L, ok, NULL);
991-
}
992-
993-
#endif // POPEN_STUFF_WHICH_DOESNT_WORK
994-

scripting/methods.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//=============================================================
55
// IMPORTANT - when adding methods here remember to add the 'glue'
66
// routine in lua_methods.cpp and add it to the library table
7+
// - see comments at the end of this file
78
//=============================================================
89
#include "..\doc.h"
910
#include "..\MUSHview.h"

0 commit comments

Comments
 (0)