1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/* Shell-window functions
Copyright (C) 1992 Joseph H. Allen
This file is part of JOE (Joe's Own Editor)
JOE is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 1, or (at your option) any later version.
JOE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
JOE; see the file COPYING. If not, write to the Free Software Foundation,
675 Mass Ave, Cambridge, MA 02139, USA. */
#include "config.h"
#include "b.h"
#include "bw.h"
#include "w.h"
#include "pw.h"
#include "qw.h"
#include "vs.h"
#include "va.h"
#include "ufile.h"
#include "main.h"
#include "ushell.h"
extern int orphan;
/* Executed when shell process terminates */
static void cdone(bw)
BW *bw;
{
bw->pid=0;
close(bw->out); bw->out= -1;
if(piseof(bw->cursor))
{
binss(bw->cursor,"** Program finished **\n");
peof(bw->cursor);
bw->cursor->xcol=piscol(bw->cursor);
}
else
{
P *q=pdup(bw->b->eof);
binss(q,"** Program finished **\n");
prm(q);
}
}
/* Executed for each chunk of data we get from the shell */
static void cdata(bw,dat,siz)
BW *bw;
char *dat;
{
P *q=pdup(bw->cursor);
P *r=pdup(bw->b->eof);
char bf[1024];
int x, y;
for(x=y=0;x!=siz;++x)
if(dat[x]==13 || dat[x]==0);
else if(dat[x]==8 || dat[x]==127)
if(y) --y;
else
if(piseof(bw->cursor))
{
pset(q,bw->cursor), prgetc(q), bdel(q,bw->cursor);
bw->cursor->xcol=piscol(bw->cursor);
}
else pset(q,r), prgetc(q), bdel(q,r);
else bf[y++]=dat[x];
if(y)
if(piseof(bw->cursor))
{
binsm(bw->cursor,bf,y);
peof(bw->cursor);
bw->cursor->xcol=piscol(bw->cursor);
}
else binsm(r,bf,y);
prm(r);
prm(q);
}
static int cstart(bw,name,s,obj,notify)
BW *bw;
char *name;
char **s;
void *obj;
int *notify;
{
#ifdef __MSDOS__
if(notify) *notify=1;
varm(s);
msgnw(bw,"Sorry, no sub-processes in DOS (yet)");
return -1;
#else
MPX *m;
if(notify) *notify=1;
if(bw->pid && orphan)
{
msgnw(bw,"Program already running in this window");
varm(s);
return -1;
}
if(doedit(bw,vsncpy(NULL,0,sc("")),NULL,NULL))
{
varm(s);
return -1;
}
bw=(BW *)maint->curwin->object;
if(!(m=mpxmk(&bw->out,name,s,cdata,bw,cdone,bw)))
{
varm(s);
msgnw(bw,"No ptys available");
return -1;
}
else bw->pid= m->pid;
return 0;
#endif
}
int ubknd(bw)
BW *bw;
{
char **a;
char *s;
a=vamk(3);
s=vsncpy(NULL,0,sz(getenv("SHELL"))); a=vaadd(a,s);
s=vsncpy(NULL,0,sc("-i")); a=vaadd(a,s);
return cstart(bw,getenv("SHELL"),a,NULL,NULL);
}
/* Run a program in a window */
static int dorun(bw,s,object,notify)
BW *bw;
char *s;
void *object;
int *notify;
{
char **a=vamk(10);
char *cmd=vsncpy(NULL,0,sc("/bin/sh"));
a=vaadd(a,cmd);
cmd=vsncpy(NULL,0,sc("-c"));
a=vaadd(a,cmd);
a=vaadd(a,s);
return cstart(bw,"/bin/sh",a,NULL,notify);
}
B *runhist=0;
int urun(bw)
BW *bw;
{
if(wmkpw(bw,"Program to run: ",&runhist,dorun,"Run",NULL,NULL,NULL,NULL))
return 0;
else return -1;
}
/* Kill program */
int pidabort(bw,c,object,notify)
BW *bw;
void *object;
int *notify;
{
if(notify) *notify=1;
if(c!='y' && c!='Y') return -1;
if(bw->pid) { kill(bw->pid,1); return -1; }
else return -1;
}
int ukillpid(bw)
BW *bw;
{
if(bw->pid)
{
if(mkqw(bw,sc("Kill program (y,n,^C)?"),pidabort,NULL,NULL,NULL)) return 0;
else return -1;
}
else return 0;
}
|