1 /* $MirOS: contrib/code/jupp/ushell.c,v 1.4 2012/12/30 18:18:07 tg Exp $ */
3 * Shell-window functions
5 * (C) 1992 Joseph H. Allen
7 * This file is part of JOE (Joe's Own Editor)
13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h>
16 #ifdef HAVE_SYS_STAT_H
42 /* Executed when shell process terminates */
44 static void cdone(B *b)
51 static void cdone_parse(B *b)
59 /* Executed for each chunk of data we get from the shell */
61 static void cfollow(B *b,long byte)
64 if ((w = maint->topwin) != NULL) {
66 if ((w->watom->what&TYPETW) && ((BW *)w->object)->b==b && ((BW *)w->object)->cursor->byte==byte) {
67 BW *bw = (BW *)w->object;
68 p_goto_eof(bw->cursor);
69 bw->cursor->xcol = piscol(bw->cursor);
72 } while (w != maint->topwin);
76 static void cdata(B *b, unsigned char *dat, int siz)
81 unsigned char bf[1024];
84 for (x = y = 0; x != siz; ++x) {
85 if (dat[x] == 13 || dat[x] == 0) {
87 } else if (dat[x] == 8 || dat[x] == 127) {
96 } else if (dat[x] == 7) {
111 static int doushell(BW *bw, unsigned char *cmd, int *notify, int build)
118 msgnw(bw->parent, "Sorry, no sub-processes in DOS (yet)");
124 const unsigned char *name;
128 u = vsncpy(NULL, 0, sz(name));
131 u = vsncpy(NULL, 0, sc("-c"));
135 u = vsncpy(NULL, 0, sc("-i"));
143 msgnw(bw->parent, US "Program already running in this window");
147 p_goto_eof(bw->cursor);
149 if (!(m = mpxmk(&bw->b->out, name, s, cdata, bw->b, build ? cdone_parse : cdone, bw->b))) {
151 msgnw(bw->parent, US "No ptys available");
162 if (!getenv("SHELL")) {
163 msgnw(bw->parent, US "\"SHELL\" environment variable not defined or exported");
165 return doushell(bw, NULL, NULL, 0);
168 /* Run a program in a window */
170 static int dorun(BW *bw, unsigned char *s, void *object, int *notify)
172 return doushell(bw, s, notify, 0);
179 if (wmkpw(bw->parent, US "Program to run: ", &runhist, dorun, US "Run", NULL, NULL, NULL, NULL, locale_map)) {
186 static int dobuild(BW *bw, unsigned char *s, void *object, int *notify)
188 return doushell(bw, s, notify, 1);
196 if ((bw=wmkpw(bw->parent, US "Build command: ", &buildhist, dobuild, US "Run", NULL, NULL, NULL, NULL, locale_map))) {
199 bw->cursor->xcol = piscol(bw->cursor);
202 } else if (wmkpw(bw->parent, US "Enter build command (for example, 'make'): ", &buildhist, dobuild, US "Run", NULL, NULL, NULL, NULL, locale_map))
209 static int pidabort(BW *bw, int c, void *object, int *notify)
214 if (c != 'y' && c != 'Y') {
228 if (mkqw(bw->parent, sc("Kill program (y,n,^C)?"), pidabort, NULL, NULL, NULL)) {
238 static const char * const getushell_envs[] = {
242 const void *getushell(void)
252 eshell = getenv(getushell_envs[i++]);
253 if (eshell && *eshell &&
254 !stat(eshell, &sbuf) &&
255 S_ISREG(sbuf.st_mode) &&
256 (sbuf.st_mode & 0111) &&
257 /* LINTED use of access */
258 !access(eshell, X_OK)) {
264 return (rshell ? rshell : "/bin/sh");