[523079]: joe / ushell.c  Maximize  Restore  History

Download this file

234 lines (201 with data), 4.8 kB

  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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Shell-window functions
* Copyright
* (C) 1992 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
/* Executed when shell process terminates */
static void cdone(B *b)
{
b->pid = 0;
close(b->out);
b->out = -1;
}
static void cdone_parse(B *b)
{
b->pid = 0;
close(b->out);
b->out = -1;
parserrb(b);
}
/* Executed for each chunk of data we get from the shell */
static void cfollow(B *b,long byte)
{
W *w;
if ((w = maint->topwin) != NULL) {
do {
if ((w->watom->what&TYPETW) && ((BW *)w->object)->b==b && ((BW *)w->object)->cursor->byte==byte) {
BW *bw = (BW *)w->object;
p_goto_eof(bw->cursor);
bw->cursor->xcol = piscol(bw->cursor);
}
w = w->link.next;
} while (w != maint->topwin);
}
}
static void cdata(B *b, unsigned char *dat, int siz)
{
P *q = pdup(b->eof, USTR "cdata");
P *r = pdup(b->eof, USTR "cdata");
long byte = q->byte;
unsigned 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 {
pset(q, r);
prgetc(q);
bdel(q, r);
--byte;
}
} else if (dat[x] == 7) {
ttputc(7);
} else {
bf[y++] = dat[x];
}
}
if (y) {
binsm(r, bf, y);
}
prm(r);
prm(q);
cfollow(b,byte);
undomark();
}
int cstart(BW *bw, unsigned char *name, unsigned char **s, void *obj, int build, int out_only)
{
#ifdef __MSDOS__
varm(s);
msgnw(bw->parent, joe_gettext(_("Sorry, no sub-processes in DOS (yet)")));
return -1;
#else
MPX *m;
if (bw->b->pid) {
msgnw(bw->parent, joe_gettext(_("Program already running in this window")));
varm(s);
return -1;
}
/* p_goto_eof(bw->cursor); */
if (!(m = mpxmk(&bw->b->out, name, s, cdata, bw->b, build ? cdone_parse : cdone, bw->b, out_only))) {
varm(s);
msgnw(bw->parent, joe_gettext(_("No ptys available")));
return -1;
} else {
bw->b->pid = m->pid;
}
return 0;
#endif
}
int ubknd(BW *bw)
{
unsigned char **a;
unsigned char *s;
unsigned char *sh;
if (!modify_logic(bw,bw->b))
return -1;
sh=(unsigned char *)getenv("SHELL");
if (file_exists(sh) && zcmp(sh,USTR "/bin/sh")) goto ok;
if (file_exists(sh=USTR "/bin/bash")) goto ok;
if (file_exists(sh=USTR "/usr/bin/bash")) goto ok;
if (file_exists(sh=USTR "/bin/sh")) goto ok;
msgnw(bw->parent, joe_gettext(_("\"SHELL\" environment variable not defined or exported")));
return -1;
ok:
a = vamk(3);
vaperm(a);
s = vsncpy(NULL, 0, sz(sh));
a = vaadd(a, s);
s = vsncpy(NULL, 0, sc("-i"));
a = vaadd(a, s);
return cstart(bw, sh, a, NULL, 0, 0);
}
/* Run a program in a window */
B *runhist = NULL;
int urun(BW *bw)
{
unsigned char *s = ask(bw->parent, joe_gettext(_("Program to run: ")), &runhist, USTR "Run",
utypebw, locale_map, 0, 0, NULL);
if (s) {
unsigned char **a;
unsigned char *cmd;
if (!modify_logic(bw,bw->b))
return -1;
a = vamk(10);
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, USTR "/bin/sh", a, NULL, 0, 0);
} else {
return -1;
}
}
B *buildhist = NULL;
int ubuild(BW *bw)
{
unsigned char *s;
int prev = 0;
if (buildhist) {
s = joe_gettext(_("Build command: "));
prev = 1;
} else {
s = joe_gettext(_("Enter build command (for example, 'make'): "));
}
/* "file prompt" was set for this... */
s = ask(bw->parent, s, &buildhist, USTR "Run", utypebw, locale_map, 0, prev, NULL);
if (s) {
unsigned char **a = vamk(10);
unsigned 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, USTR "/bin/sh", a, NULL, 1, 0);
} else {
return -1;
}
}
B *grephist = NULL;
int ugrep(BW *bw)
{
unsigned char *s;
int prev = 0;
if (grephist) {
s = joe_gettext(_("Grep command: "));
prev = 1;
} else {
s = joe_gettext(_("Enter grep command (for example, 'grep -n foo *.c'): "));
}
/* "file prompt" was set for this... */
s = ask(bw->parent, s, &grephist, USTR "Run", utypebw, locale_map, 0, prev, NULL);
if (s) {
unsigned char **a = vamk(10);
unsigned 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, USTR "/bin/sh", a, NULL, 1, 0);
} else {
return -1;
}
}
/* Kill program */
int ukillpid(BW *bw)
{
if (bw->b->pid) {
int c = query(bw->parent, sz(joe_gettext(_("Kill program (y,n,^C)?"))), 0);
if (bw->b->pid && (c == YES_CODE || yncheck(yes_key, c)))
kill(bw->b->pid, 1);
return -1;
} else {
return 0;
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks