Skip to content

Commit

Permalink
kernel: consolidate exec function in src/iostream.c
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Sep 30, 2022
1 parent e6d49ff commit aafc65d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 94 deletions.
72 changes: 48 additions & 24 deletions src/iostream.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "stringobj.h"
#include "sysenv.h"
#include "sysfiles.h"
#include "sysopt.h"

#include "hpc/thread.h"

Expand Down Expand Up @@ -547,20 +548,6 @@ StartChildProcess(const Char * dir, const Char * prg, Char * args[])
}


#ifdef GAP_DISABLE_SUBPROCESS_CODE

UInt SyExecuteProcess (
Char * dir,
Char * prg,
Int in,
Int out,
Char * args[] )
{
return 255;
}

#else

/****************************************************************************
**
*F SyExecuteProcess( <dir>, <prg>, <in>, <out>, <args> ) . . . . new process
Expand All @@ -570,15 +557,6 @@ UInt SyExecuteProcess (
** performed, the return value of the process is returned if the operation
** system supports such a concept.
*/

extern UInt FreezeStdin;
UInt FreezeStdin;

/****************************************************************************
**
*f SyExecuteProcess( <dir>, <prg>, <in>, <out>, <args> )
*/

#ifdef HAVE_POSIX_SPAWN

UInt SyExecuteProcess(Char * dir, Char * prg, Int in, Int out, Char * args[])
Expand Down Expand Up @@ -774,7 +752,6 @@ UInt SyExecuteProcess(Char * dir, Char * prg, Int in, Int out, Char * args[])
}
#endif

#endif // !GAP_DISABLE_SUBPROCESS_CODE

// This function assumes that the caller invoked HashLock(PtyIOStreams).
// It unlocks just before throwing any error.
Expand Down Expand Up @@ -1089,6 +1066,52 @@ static Obj FuncFD_OF_IOSTREAM(Obj self, Obj stream)
#endif


/****************************************************************************
**
*F FuncExecuteProcess( <self>, <dir>, <prg>, <in>, <out>, <args> ) process
*/
static Obj
FuncExecuteProcess(Obj self, Obj dir, Obj prg, Obj in, Obj out, Obj args)
{
Obj ExecArgs[1024];
Char * ExecCArgs[1024];

Obj tmp;
Int res;
Int i;

RequireStringRep(SELF_NAME, dir);
RequireStringRep(SELF_NAME, prg);
Int iin = GetSmallInt(SELF_NAME, in);
Int iout = GetSmallInt(SELF_NAME, out);
RequireSmallList(SELF_NAME, args);

// create an argument array
for (i = 1; i <= LEN_LIST(args); i++) {
if (i == 1023)
break;
tmp = ELM_LIST(args, i);
RequireStringRep(SELF_NAME, tmp);
ExecArgs[i] = tmp;
}
ExecCArgs[0] = CSTR_STRING(prg);
ExecCArgs[i] = 0;
for (i--; 0 < i; i--) {
ExecCArgs[i] = CSTR_STRING(ExecArgs[i]);
}
if (SyWindow && out == INTOBJ_INT(1)) // standard output
syWinPut(INT_INTOBJ(out), "@z", "");

// execute the process
res = SyExecuteProcess(CSTR_STRING(dir), CSTR_STRING(prg), iin, iout,
ExecCArgs);

if (SyWindow && out == INTOBJ_INT(1)) // standard output
syWinPut(INT_INTOBJ(out), "@mAgIc", "");
return res == 255 ? Fail : INTOBJ_INT(res);
}


/****************************************************************************
**
*F * * * * * * * * * * * * * initialize module * * * * * * * * * * * * * * *
Expand All @@ -1100,6 +1123,7 @@ static Obj FuncFD_OF_IOSTREAM(Obj self, Obj stream)
*/
static StructGVarFunc GVarFuncs[] = {

GVAR_FUNC_5ARGS(ExecuteProcess, dir, prg, in, out, args),
GVAR_FUNC_3ARGS(CREATE_PTY_IOSTREAM, dir, prog, args),
GVAR_FUNC_3ARGS(WRITE_IOSTREAM, stream, string, len),
GVAR_FUNC_2ARGS(READ_IOSTREAM, stream, len),
Expand Down
55 changes: 0 additions & 55 deletions src/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -1671,60 +1671,6 @@ static Obj FuncUNIXSelect(Obj self,
}
#endif

/****************************************************************************
**
*F * * * * * * * * * * * * * execution functions * * * * * * * * * * * * * *
*/


/****************************************************************************
**
*F FuncExecuteProcess( <self>, <dir>, <prg>, <in>, <out>, <args> ) process
*/
static Obj
FuncExecuteProcess(Obj self, Obj dir, Obj prg, Obj in, Obj out, Obj args)
{
Obj ExecArgs[1024];
Char * ExecCArgs[1024];

Obj tmp;
Int res;
Int i;

RequireStringRep(SELF_NAME, dir);
RequireStringRep(SELF_NAME, prg);
Int iin = GetSmallInt(SELF_NAME, in);
Int iout = GetSmallInt(SELF_NAME, out);
RequireSmallList(SELF_NAME, args);

/* create an argument array */
for ( i = 1; i <= LEN_LIST(args); i++ ) {
if ( i == 1023 )
break;
tmp = ELM_LIST( args, i );
RequireStringRep(SELF_NAME, tmp);
ExecArgs[i] = tmp;
}
ExecCArgs[0] = CSTR_STRING(prg);
ExecCArgs[i] = 0;
for ( i--; 0 < i; i-- ) {
ExecCArgs[i] = CSTR_STRING(ExecArgs[i]);
}
if (SyWindow && out == INTOBJ_INT(1)) /* standard output */
syWinPut( INT_INTOBJ(out), "@z","");

/* execute the process */
res = SyExecuteProcess( CSTR_STRING(dir),
CSTR_STRING(prg),
iin,
iout,
ExecCArgs );

if (SyWindow && out == INTOBJ_INT(1)) /* standard output */
syWinPut( INT_INTOBJ(out), "@mAgIc","");
return res == 255 ? Fail : INTOBJ_INT(res);
}


/****************************************************************************
**
Expand Down Expand Up @@ -1793,7 +1739,6 @@ static StructGVarFunc GVarFuncs[] = {
GVAR_FUNC_5ARGS(
UNIXSelect, inlist, outlist, exclist, timeoutsec, timeoutusec),
#endif
GVAR_FUNC_5ARGS(ExecuteProcess, dir, prg, in, out, args),
{ 0, 0, 0, 0, 0 }

};
Expand Down
6 changes: 3 additions & 3 deletions src/sysfiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -1623,9 +1623,9 @@ Int SyGetch (
static UInt syCTRO; /* number of '<ctr>-O' pending */
static UInt syESCN; /* number of '<Esc>-N' pending */

extern UInt FreezeStdin; // When true, ignore if any new input from stdin
// This is used to stop HPC-GAP from reading stdin
// while forked subprocesses are running.
UInt FreezeStdin; // When true, ignore if any new input from stdin
// This is used to stop HPC-GAP from reading stdin
// while forked subprocesses are running.


#ifdef HAVE_SELECT
Expand Down
14 changes: 2 additions & 12 deletions src/sysfiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Obj SyGetOsRelease(void);
*F * * * * * * * * * * * * * * * window handler * * * * * * * * * * * * * * *
*/

extern UInt FreezeStdin;

/****************************************************************************
**
Expand Down Expand Up @@ -332,20 +333,9 @@ void SySetErrorNo(void);

/****************************************************************************
**
*F * * * * * * * * * * * * * file and execution * * * * * * * * * * * * * * *
*F * * * * * * * * * * * * * * * * * files * * * * * * * * * * * * * * * * *
*/

/****************************************************************************
**
*F SyExecuteProcess( <dir>, <prg>, <in>, <out>, <args> ) . . . . new process
**
** Start <prg> in directory <dir> with standard input connected to <in>,
** standard output connected to <out> and arguments. No path search is
** performed, the return value of the process is returned if the operation
** system supports such a concept.
*/
UInt SyExecuteProcess(Char * dir, Char * prg, Int in, Int out, Char * args[]);


/****************************************************************************
**
Expand Down

0 comments on commit aafc65d

Please sign in to comment.