1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-11-16 09:36:29 -05:00
uemacs/spawn.c

339 lines
6.7 KiB
C
Raw Normal View History

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