2013-05-20 01:16:08 -04:00
|
|
|
/* spawn.c -- implements spawn.h */
|
|
|
|
#include "spawn.h"
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* Various operating system access commands.
|
2010-11-14 21:10:03 -05:00
|
|
|
*
|
2019-07-23 09:04:50 -04:00
|
|
|
* Modified by Petri Kutvonen
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
|
|
|
|
2021-07-19 08:50:32 -04:00
|
|
|
#include <assert.h>
|
2010-11-14 21:10:03 -05:00
|
|
|
#include <stdio.h>
|
2013-10-08 21:43:15 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-11-14 21:10:03 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "defines.h"
|
|
|
|
|
2013-05-28 01:25:14 -04:00
|
|
|
#include "buffer.h"
|
2013-05-28 02:57:03 -04:00
|
|
|
#include "display.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "estruct.h"
|
2013-10-09 21:55:03 -04:00
|
|
|
#include "exec.h"
|
2013-05-25 00:28:17 -04:00
|
|
|
#include "file.h"
|
2013-09-19 02:13:07 -04:00
|
|
|
#include "flook.h"
|
2013-05-28 02:46:44 -04:00
|
|
|
#include "input.h"
|
2013-09-25 09:45:05 -04:00
|
|
|
#include "terminal.h"
|
2013-05-28 00:47:48 -04:00
|
|
|
#include "window.h"
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
|
2019-07-23 09:04:50 -04:00
|
|
|
#if USG | BSD
|
2005-05-31 11:50:56 -04:00
|
|
|
#include <signal.h>
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-12-24 21:51:47 -05:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* Create a subjob with a copy of the command intrepreter in it. When the
|
2005-05-31 11:50:56 -04:00
|
|
|
* command interpreter exits, mark the screen as garbage so that you do a full
|
2019-07-03 20:06:13 -04:00
|
|
|
* repaint. Bound to "^X C".
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( spawncli) {
|
2019-07-23 09:04:50 -04:00
|
|
|
#if USG | BSD
|
2010-02-27 05:38:22 -05:00
|
|
|
char *cp;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* don't allow this command if restricted */
|
|
|
|
if (restflag)
|
2010-08-29 06:03:55 -04:00
|
|
|
return resterr();
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2019-07-23 09:04:50 -04:00
|
|
|
#if USG | BSD
|
2005-09-30 18:26:09 -04:00
|
|
|
movecursor(term.t_nrow, 0); /* Seek to last line. */
|
|
|
|
TTflush();
|
|
|
|
TTclose(); /* stty to old settings */
|
2011-08-25 19:01:28 -04:00
|
|
|
TTkclose(); /* Close "keyboard" */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((cp = getenv("SHELL")) != NULL && *cp != '\0')
|
2014-12-24 21:51:47 -05:00
|
|
|
ue_system( cp) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
else
|
2005-05-31 11:50:56 -04:00
|
|
|
#if BSD
|
2005-09-30 18:26:09 -04:00
|
|
|
system("exec /bin/csh");
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2014-12-24 21:51:47 -05:00
|
|
|
ue_system( "exec /bin/sh") ;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
sgarbf = TRUE;
|
2014-06-18 02:59:40 -04:00
|
|
|
usleep( 2000000L) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
TTopen();
|
2005-05-31 11:50:56 -04:00
|
|
|
TTkopen();
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
/*
|
|
|
|
* This fools the update routines to force a full
|
|
|
|
* redraw with complete window size checking.
|
|
|
|
* -lbt
|
|
|
|
*/
|
|
|
|
chg_width = term.t_ncol;
|
2005-09-30 18:26:09 -04:00
|
|
|
chg_height = term.t_nrow + 1;
|
2005-05-31 11:50:56 -04:00
|
|
|
term.t_nrow = term.t_ncol = 0;
|
|
|
|
#endif
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-08-07 01:17:26 -04:00
|
|
|
#if BSD | SVR4
|
2021-08-11 05:02:19 -04:00
|
|
|
/* suspend MicroEMACS and wait to wake up */
|
|
|
|
BINDABLE( bktoshell) {
|
2005-05-31 11:50:56 -04:00
|
|
|
vttidy();
|
|
|
|
/******************************
|
2005-10-01 02:52:55 -04:00
|
|
|
int pid;
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
pid = getpid();
|
|
|
|
kill(pid,SIGTSTP);
|
|
|
|
******************************/
|
|
|
|
kill(0, SIGTSTP);
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
void rtfrmshell(void)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
TTopen();
|
|
|
|
curwp->w_flag = WFHARD;
|
|
|
|
sgarbf = TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Run a one-liner in a subjob. When the command returns, wait for a single
|
2005-05-31 11:50:56 -04:00
|
|
|
* character to be typed, then mark the screen as garbage so a full repaint is
|
|
|
|
* done. Bound to "C-X !".
|
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( spawn) {
|
2015-10-05 02:06:53 -04:00
|
|
|
int s ;
|
|
|
|
char *line ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* don't allow this command if restricted */
|
2015-10-05 02:06:53 -04:00
|
|
|
if( restflag)
|
2010-08-29 06:03:55 -04:00
|
|
|
return resterr();
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2019-07-23 09:04:50 -04:00
|
|
|
#if USG | BSD
|
2015-10-05 02:06:53 -04:00
|
|
|
s = newmlarg( &line, "!", 0) ;
|
|
|
|
if( s != TRUE)
|
|
|
|
return s ;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
TTflush();
|
|
|
|
TTclose(); /* stty to old modes */
|
2005-05-31 11:50:56 -04:00
|
|
|
TTkclose();
|
2014-12-24 21:51:47 -05:00
|
|
|
ue_system( line) ;
|
2015-10-05 02:06:53 -04:00
|
|
|
free( line) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
fflush(stdout); /* to be sure P.K. */
|
|
|
|
TTopen();
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
if (clexec == FALSE) {
|
2015-01-21 04:16:43 -05:00
|
|
|
mlwrite( "(End)") ; /* Pause. */
|
2005-05-31 11:50:56 -04:00
|
|
|
TTflush();
|
2005-09-30 18:26:09 -04:00
|
|
|
while ((s = tgetc()) != '\r' && s != ' ');
|
2015-01-21 04:16:43 -05:00
|
|
|
mlwrite( "\r\n") ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
TTkopen();
|
2005-09-30 18:26:09 -04:00
|
|
|
sgarbf = TRUE;
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Run an external program with arguments. When it returns, wait for a single
|
2005-05-31 11:50:56 -04:00
|
|
|
* character to be typed, then mark the screen as garbage so a full repaint is
|
|
|
|
* done. Bound to "C-X $".
|
|
|
|
*/
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( execprg) {
|
2015-10-05 02:06:53 -04:00
|
|
|
int s ;
|
|
|
|
char *line ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* don't allow this command if restricted */
|
2015-10-05 02:06:53 -04:00
|
|
|
if( restflag)
|
|
|
|
return resterr() ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2019-07-23 09:04:50 -04:00
|
|
|
#if USG | BSD
|
2015-10-05 02:06:53 -04:00
|
|
|
s = newmlarg( &line, "$", 0) ;
|
|
|
|
if( s != TRUE)
|
|
|
|
return s ;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
TTputc('\n'); /* Already have '\r' */
|
|
|
|
TTflush();
|
|
|
|
TTclose(); /* stty to old modes */
|
2011-08-25 19:01:28 -04:00
|
|
|
TTkclose();
|
2014-12-24 21:51:47 -05:00
|
|
|
ue_system( line) ;
|
2015-10-05 02:06:53 -04:00
|
|
|
free( line) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
fflush(stdout); /* to be sure P.K. */
|
|
|
|
TTopen();
|
2015-01-21 04:16:43 -05:00
|
|
|
mlwrite( "(End)") ; /* Pause. */
|
2005-09-30 18:26:09 -04:00
|
|
|
TTflush();
|
|
|
|
while ((s = tgetc()) != '\r' && s != ' ');
|
|
|
|
sgarbf = TRUE;
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Pipe a one line command into a window
|
2005-05-31 11:50:56 -04:00
|
|
|
* Bound to ^X @
|
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( pipecmd) {
|
2015-10-05 02:06:53 -04:00
|
|
|
int s ; /* return status from CLI */
|
|
|
|
struct window *wp ; /* pointer to new window */
|
2021-08-03 01:37:06 -04:00
|
|
|
buffer_p bp ; /* pointer to buffer to zot */
|
2015-10-05 02:06:53 -04:00
|
|
|
char *mlarg ;
|
|
|
|
char *line ; /* command line send to shell */
|
|
|
|
static char bname[] = "command" ;
|
|
|
|
static char filnam[ NSTRING] = "command" ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* don't allow this command if restricted */
|
2015-10-05 02:06:53 -04:00
|
|
|
if( restflag)
|
|
|
|
return resterr() ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2015-10-05 02:06:53 -04:00
|
|
|
/* get the command to pipe in */
|
|
|
|
s = newmlarg( &mlarg, "@", 0) ;
|
|
|
|
if( s != TRUE)
|
|
|
|
return s ;
|
|
|
|
|
|
|
|
line = malloc( strlen( mlarg) + strlen( filnam) + 2) ;
|
|
|
|
if( line == NULL) {
|
|
|
|
free( mlarg) ;
|
|
|
|
return FALSE ;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2015-10-05 02:06:53 -04:00
|
|
|
strcpy( line, mlarg) ;
|
|
|
|
free( mlarg) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* get rid of the command output buffer if it exists */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((bp = bfind(bname, FALSE, 0)) != FALSE) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* try to make sure we are off screen */
|
|
|
|
wp = wheadp;
|
|
|
|
while (wp != NULL) {
|
|
|
|
if (wp->w_bufp == bp) {
|
|
|
|
#if PKCODE
|
|
|
|
if (wp == curwp)
|
|
|
|
delwind(FALSE, 1);
|
|
|
|
else
|
|
|
|
onlywind(FALSE, 1);
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
onlywind(FALSE, 1);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
wp = wp->w_wndp;
|
|
|
|
}
|
|
|
|
|
2015-10-05 02:06:53 -04:00
|
|
|
if( zotbuf( bp) != TRUE) {
|
|
|
|
free( line) ;
|
|
|
|
return FALSE ;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2019-07-23 09:04:50 -04:00
|
|
|
#if USG | BSD
|
2005-09-30 18:26:09 -04:00
|
|
|
TTflush();
|
|
|
|
TTclose(); /* stty to old modes */
|
2011-08-25 19:01:28 -04:00
|
|
|
TTkclose();
|
2015-10-05 02:06:53 -04:00
|
|
|
strcat( line, ">") ;
|
|
|
|
strcat( line, filnam) ;
|
2014-12-24 21:51:47 -05:00
|
|
|
ue_system( line) ;
|
2015-10-05 02:06:53 -04:00
|
|
|
free( line) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
TTopen();
|
2011-08-25 19:01:28 -04:00
|
|
|
TTkopen();
|
2005-09-30 18:26:09 -04:00
|
|
|
TTflush();
|
|
|
|
sgarbf = TRUE;
|
|
|
|
s = TRUE;
|
2015-03-19 02:27:58 -04:00
|
|
|
#else
|
2005-05-31 11:50:56 -04:00
|
|
|
if (s != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2015-03-19 02:27:58 -04:00
|
|
|
#endif
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* split the current window to make room for the command output */
|
|
|
|
if (splitwind(FALSE, 1) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and read the stuff in */
|
|
|
|
if (getfile(filnam, FALSE) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* make this window in VIEW mode, update all mode lines */
|
|
|
|
curwp->w_bufp->b_mode |= MDVIEW;
|
|
|
|
wp = wheadp;
|
|
|
|
while (wp != NULL) {
|
|
|
|
wp->w_flag |= WFMODE;
|
|
|
|
wp = wp->w_wndp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and get rid of the temporary file */
|
|
|
|
unlink(filnam);
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* filter a buffer through an external DOS program
|
2005-05-31 11:50:56 -04:00
|
|
|
* Bound to ^X #
|
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( filter_buffer) {
|
2015-10-05 02:06:53 -04:00
|
|
|
int s ; /* return status from CLI */
|
2021-08-03 01:37:06 -04:00
|
|
|
buffer_p bp ; /* pointer to buffer to zot */
|
2015-10-05 02:06:53 -04:00
|
|
|
char *mlarg ;
|
|
|
|
char *line ; /* command line send to shell */
|
2014-05-30 03:27:56 -04:00
|
|
|
fname_t tmpnam ; /* place to store real file name */
|
2015-10-05 02:06:53 -04:00
|
|
|
static char bname1[] = "fltinp" ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2015-10-05 02:06:53 -04:00
|
|
|
static char filnam1[] = "fltinp" ;
|
|
|
|
static char filnam2[] = "fltout" ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* don't allow this command if restricted */
|
2015-10-05 02:06:53 -04:00
|
|
|
if( restflag)
|
|
|
|
return resterr() ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2021-07-19 08:50:32 -04:00
|
|
|
assert( !(curbp->b_mode & MDVIEW)) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* get the filter name and its args */
|
2015-10-05 02:06:53 -04:00
|
|
|
s = newmlarg( &mlarg, "#", 0) ;
|
|
|
|
if( s != TRUE)
|
|
|
|
return s ;
|
|
|
|
|
|
|
|
line = malloc( strlen( mlarg) + 16 + 1) ;
|
|
|
|
if( line == NULL) {
|
|
|
|
free( mlarg) ;
|
|
|
|
return FALSE ;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy( line, mlarg) ;
|
|
|
|
free( mlarg) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* setup the proper file names */
|
|
|
|
bp = curbp;
|
|
|
|
strcpy(tmpnam, bp->b_fname); /* save the original name */
|
|
|
|
strcpy(bp->b_fname, bname1); /* set it to our new one */
|
|
|
|
|
|
|
|
/* write it out, checking for errors */
|
2015-10-05 02:06:53 -04:00
|
|
|
if( writeout( filnam1) != TRUE) {
|
2015-01-21 04:16:43 -05:00
|
|
|
mlwrite( "(Cannot write filter file)") ;
|
2015-10-05 02:06:53 -04:00
|
|
|
strcpy( bp->b_fname, tmpnam) ;
|
|
|
|
free( line) ;
|
|
|
|
return FALSE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2019-07-23 09:04:50 -04:00
|
|
|
#if USG | BSD
|
2005-09-30 18:26:09 -04:00
|
|
|
TTputc('\n'); /* Already have '\r' */
|
|
|
|
TTflush();
|
|
|
|
TTclose(); /* stty to old modes */
|
2011-08-25 19:01:28 -04:00
|
|
|
TTkclose();
|
2005-09-30 18:26:09 -04:00
|
|
|
strcat(line, " <fltinp >fltout");
|
2014-12-24 21:51:47 -05:00
|
|
|
ue_system( line) ;
|
2015-10-05 02:06:53 -04:00
|
|
|
free( line) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
TTopen();
|
2011-08-25 19:01:28 -04:00
|
|
|
TTkopen();
|
2005-09-30 18:26:09 -04:00
|
|
|
TTflush();
|
|
|
|
sgarbf = TRUE;
|
|
|
|
s = TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* on failure, escape gracefully */
|
2005-09-30 18:26:09 -04:00
|
|
|
if (s != TRUE || (readin(filnam2, FALSE) == FALSE)) {
|
2015-01-21 04:16:43 -05:00
|
|
|
mlwrite( "(Execution failed)") ;
|
2005-05-31 11:50:56 -04:00
|
|
|
strcpy(bp->b_fname, tmpnam);
|
|
|
|
unlink(filnam1);
|
|
|
|
unlink(filnam2);
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* reset file name */
|
|
|
|
strcpy(bp->b_fname, tmpnam); /* restore name */
|
2005-09-30 18:26:09 -04:00
|
|
|
bp->b_flag |= BFCHG; /* flag it as changed */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and get rid of the temporary file */
|
|
|
|
unlink(filnam1);
|
|
|
|
unlink(filnam2);
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2019-07-23 09:04:50 -04:00
|
|
|
/* end of spawn.c */
|