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

Download this file

ushell.c    260 lines (226 with data), 5.3 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* 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 *notify, int build, int out_only)
{
#ifdef __MSDOS__
if (notify) {
*notify = 1;
}
varm(s);
msgnw(bw->parent, joe_gettext(_("Sorry, no sub-processes in DOS (yet)")));
return -1;
#else
MPX *m;
if (notify) {
*notify = 1;
}
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);
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, NULL, 0, 0);
}
/* Run a program in a window */
static int dorun(BW *bw, unsigned char *s, void *object, int *notify)
{
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, notify, 0, 0);
}
B *runhist = NULL;
int urun(BW *bw)
{
if (wmkpw(bw->parent, joe_gettext(_("Program to run: ")), &runhist, dorun, USTR "Run", NULL, NULL, NULL, NULL, locale_map, 1)) {
return 0;
} else {
return -1;
}
}
static int dobuild(BW *bw, unsigned char *s, void *object, int *notify)
{
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, notify, 1, 0);
}
B *buildhist = NULL;
int ubuild(BW *bw)
{
if (buildhist) {
if ((bw=wmkpw(bw->parent, joe_gettext(_("Build command: ")), &buildhist, dobuild, USTR "Run", NULL, NULL, NULL, NULL, locale_map, 1))) {
uuparw(bw);
u_goto_eol(bw);
bw->cursor->xcol = piscol(bw->cursor);
return 0;
} else {
return -1;
}
} else {
if (wmkpw(bw->parent, joe_gettext(_("Enter build command (for example, 'make'): ")), &buildhist, dobuild, USTR "Run", NULL, NULL, NULL, NULL, locale_map, 1)) {
return 0;
} else {
return -1;
}
}
}
B *grephist = NULL;
int ugrep(BW *bw)
{
/* Set parser to grep */
bw->b->parseone = parseone_grep;
if (grephist) {
if ((bw=wmkpw(bw->parent, joe_gettext(_("Grep command: ")), &grephist, dobuild, USTR "Run", NULL, NULL, NULL, NULL, locale_map, 1))) {
uuparw(bw);
u_goto_eol(bw);
bw->cursor->xcol = piscol(bw->cursor);
return 0;
} else {
return -1;
}
} else {
if (wmkpw(bw->parent, joe_gettext(_("Enter grep command (for example, 'grep -n foo *.c'): ")), &grephist, dobuild, USTR "Run", NULL, NULL, NULL, NULL, locale_map, 1)) {
return 0;
} else {
return -1;
}
}
}
/* Kill program */
static int pidabort(BW *bw, int c, void *object, int *notify)
{
if (notify) {
*notify = 1;
}
if (c != YES_CODE && !yncheck(yes_key, c)) {
return -1;
}
if (bw->b->pid) {
kill(bw->b->pid, 1);
return -1;
} else {
return -1;
}
}
int ukillpid(BW *bw)
{
if (bw->b->pid) {
if (mkqw(bw->parent, sz(joe_gettext(_("Kill program (y,n,^C)?"))), pidabort, NULL, NULL, NULL)) {
return 0;
} else {
return -1;
}
} else {
return 0;
}
}