summaryrefslogtreecommitdiff
path: root/command.c
blob: c9a2cc57eb8d130d3cfd78b753e077d8522a1aca (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* $XConsortium: command.c,v 2.49 95/04/05 19:59:06 kaleb Exp $ */
/* $XFree86: xc/programs/xmh/command.c,v 3.8 2001/12/09 15:48:36 herrb Exp $ */

/*
 *			  COPYRIGHT 1987, 1989
 *		   DIGITAL EQUIPMENT CORPORATION
 *		       MAYNARD, MASSACHUSETTS
 *			ALL RIGHTS RESERVED.
 *
 * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
 * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
 * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
 * ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
 *
 * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
 * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
 * ADDITION TO THAT SET FORTH ABOVE.
 *
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Digital Equipment Corporation not be
 * used in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.
 */

/* command.c -- interface to exec mh commands. */

#include "xmh.h"
#include <X11/Xpoll.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <sys/wait.h>
#if defined(SVR4)
#include <sys/filio.h>
#endif

/* number of user input events to queue before malloc */
#define TYPEAHEADSIZE 20

#ifndef HAVE_WORKING_VFORK
#define vfork fork
#else
#ifdef HAVE_VFORK_H
#include <vfork.h>
#endif
#endif

typedef struct _CommandStatus {
    Widget	popup;		 /* must be first; see PopupStatus */
    struct _LastInput lastInput; /* must be second; ditto */
    char*	shell_command;	 /* must be third; for XmhShellCommand */
    int		child_pid;
    XtInputId	output_inputId;
    XtInputId	error_inputId;
    int		output_pipe[2];
    int		error_pipe[2];
    char*	output_buffer;
    int		output_buf_size;
    char*	error_buffer;
    int		error_buf_size;
} CommandStatusRec, *CommandStatus;

typedef char* Pointer;
static void FreeStatus(XMH_CB_ARGS);
static void CheckReadFromPipe(int, char **, int *, Bool);

static void SystemError(const char* text)
{
    char msg[BUFSIZ];
    snprintf( msg, sizeof(msg), "%s; errno = %d %s", text, errno,
	     strerror(errno));
    XtWarning( msg );
}


/* Return the full path name of the given mh command. */

static char *FullPathOfCommand(const char *str)
{
    static char result[100];
    snprintf(result, sizeof(result), "%s/%s", app_resources.mh_path, str);
    return result;
}


/*ARGSUSED*/
static void ReadStdout(
    XtPointer closure,
    int *fd,
    XtInputId *id)	/* unused */
{
    register CommandStatus status = (CommandStatus)closure;
    CheckReadFromPipe(*fd, &status->output_buffer, &status->output_buf_size,
		      False);
}


/*ARGSUSED*/
static void ReadStderr(
    XtPointer closure,
    int *fd,
    XtInputId *id)	/* unused */
{
    register CommandStatus status = (CommandStatus)closure;
    CheckReadFromPipe(*fd, &status->error_buffer, &status->error_buf_size,
		      False);
}


static volatile int childdone;		/* Gets nonzero when the child process
				   finishes. */
/* ARGSUSED */
static void
ChildDone(int n)
{
    childdone++;
}

/* Execute the given command, and wait until it has finished.  While the
   command is executing, watch the X socket and cause Xlib to read in any
   incoming data.  This will prevent the socket from overflowing during
   long commands.  Returns 0 if stderr empty, -1 otherwise. */

static int _DoCommandToFileOrPipe(
  char * const *argv,		/* The command to execute, and its args. */
  int inputfd,			/* Input stream for command. */
  int outputfd,			/* Output stream; /dev/null if == -1 */
  char **bufP,			/* output buffer ptr if outputfd == -2 */
  int *lenP)			/* output length ptr if outputfd == -2 */
{
    XtAppContext appCtx = XtWidgetToApplicationContext(toplevel);
    int return_status;
    int old_stdin = 0, old_stdout = 0, old_stderr = 0;
    int pid;
    fd_set readfds, fds;
    Boolean output_to_pipe = False;
    CommandStatus status = XtNew(CommandStatusRec);
    FD_ZERO(&fds);
    FD_SET(ConnectionNumber(theDisplay), &fds);
    DEBUG1("Executing %s ...", argv[0])

    if (inputfd != -1) {
	old_stdin = dup(fileno(stdin));
	(void) dup2(inputfd, fileno(stdin));
	close(inputfd);
    }

    if (outputfd == -1) {
	if (!app_resources.debug) { /* Throw away stdout. */
	    outputfd = open( "/dev/null", O_WRONLY, 0 );
	}
    }
    else if (outputfd == -2) {	/* make pipe */
	if (pipe(status->output_pipe) /*failed*/) {
	    SystemError( "couldn't re-direct standard output" );
	    status->output_pipe[0]=0;
	}
	else {
	    outputfd = status->output_pipe[1];
	    FD_SET(status->output_pipe[0], &fds);
	    status->output_inputId =
		XtAppAddInput( appCtx,
			   status->output_pipe[0], (XtPointer)XtInputReadMask,
			   ReadStdout, (XtPointer)status
			     );
	    status->output_buffer = NULL;
	    status->output_buf_size = 0;
	    output_to_pipe = True;
	}
    }

    if (pipe(status->error_pipe) /*failed*/) {
	SystemError( "couldn't re-direct standard error" );
	status->error_pipe[0]=0;
    }
    else {
	old_stderr = dup(fileno(stderr));
	(void) dup2(status->error_pipe[1], fileno(stderr));
	close(status->error_pipe[1]);
	FD_SET(status->error_pipe[0], &fds);
	status->error_inputId =
	    XtAppAddInput( appCtx,
			   status->error_pipe[0], (XtPointer)XtInputReadMask,
			   ReadStderr, (XtPointer)status
			  );
    }
    if (outputfd != -1) {
	old_stdout = dup(fileno(stdout));
	(void) dup2(outputfd, fileno(stdout));
	close(outputfd);
    }
    childdone = FALSE;
    status->popup = (Widget)NULL;
    status->lastInput = lastInput;
    status->error_buffer = NULL;
    status->error_buf_size = 0;
    (void) signal(SIGCHLD, ChildDone);
    pid = vfork();
    if (inputfd != -1) {
	if (pid != 0) dup2(old_stdin,  fileno(stdin));
	close(old_stdin);
    }
    if (outputfd != -1) {
	if (pid != 0) dup2(old_stdout, fileno(stdout));
	close(old_stdout);
    }
    if (status->error_pipe[0]) {
	if (pid != 0) dup2(old_stderr, fileno(stderr));
	close(old_stderr);
    }

    if (pid == -1) Punt("Couldn't fork!");
    if (pid) {			/* We're the parent process. */
	XEvent typeAheadQueue[TYPEAHEADSIZE], *eventP = typeAheadQueue;
	XEvent *altQueue = NULL;
	int type_ahead_count = 0, alt_queue_size = 0, alt_queue_count = 0;
	XtAppContext app = XtWidgetToApplicationContext(toplevel);
	int num_fds = ConnectionNumber(theDisplay)+1;
	if (output_to_pipe && status->output_pipe[0] >= num_fds)
	    num_fds = status->output_pipe[0]+1;
	if (status->error_pipe[0] >= num_fds)
	    num_fds = status->error_pipe[0]+1;
	status->child_pid = pid;
	DEBUG1( " pid=%d ", pid )
	subProcessRunning = True;
	while (!childdone) {
	    while (!(XtAppPending(app) & XtIMXEvent)) {
		/* this is gross, but the only other way is by
		 * polling on timers or an extra pipe, since we're not
		 * guaranteed to be able to malloc in a signal handler.
		 */
		readfds = fds;
                if (childdone) break;
		DEBUG("blocking.\n")
		(void) Select(num_fds, &readfds, NULL, NULL, NULL);
		DEBUG1("unblocked; child%s done.\n", childdone ? "" : " not")
		if (childdone) break;
		if (!FD_ISSET(ConnectionNumber(theDisplay), &readfds)) {
		    DEBUG("reading alternate input...")
		    XtAppProcessEvent(appCtx, (unsigned)XtIMAlternateInput);
		    DEBUG("read.\n")
		}
	    }
	    if (childdone) break;
	    XtAppNextEvent(app, eventP);
	    switch(eventP->type) {
	      case LeaveNotify:
		if (type_ahead_count) {
		    /* do compress_enterleave here to save memory */
		    XEvent *prevEvent;
		    if (alt_queue_size && (alt_queue_count == 0))
			prevEvent = &typeAheadQueue[type_ahead_count-1];
		    else
			prevEvent = eventP - 1;
		    if (prevEvent->type == EnterNotify
		      && prevEvent->xany.display == eventP->xany.display
		      && prevEvent->xany.window == eventP->xany.window) {
			eventP = prevEvent;
			if (alt_queue_count > 0)
			    alt_queue_count--;
			else
			    type_ahead_count--;
			break;
		    }
		}
		/* fall through */
	      case KeyPress:
	      case KeyRelease:
	      case EnterNotify:
	      case ButtonPress:
	      case ButtonRelease:
	      case MotionNotify:
		if (type_ahead_count < TYPEAHEADSIZE) {
		    if (++type_ahead_count == TYPEAHEADSIZE) {
			altQueue = XtMallocArray(
			    (Cardinal)TYPEAHEADSIZE, sizeof(XEvent) );
			alt_queue_size = TYPEAHEADSIZE;
			eventP = altQueue;
		    }
		    else
			eventP++;
		}
		else {
		    if (++alt_queue_count == alt_queue_size) {
			alt_queue_size += TYPEAHEADSIZE;
			altQueue = XtReallocArray(altQueue,
			    (Cardinal)alt_queue_size, sizeof(XEvent) );
			eventP = &altQueue[alt_queue_count];
		    }
		    else
			eventP++;
		}
		break;

	      default:
		XtDispatchEvent(eventP);
	    }
	}
	(void) wait(0);

	DEBUG("done\n")
	subProcessRunning = False;
	if (output_to_pipe) {
	    CheckReadFromPipe( status->output_pipe[0],
			       &status->output_buffer,
			       &status->output_buf_size,
			       True
			      );
	    *bufP = status->output_buffer;
	    *lenP = status->output_buf_size;
	    close( status->output_pipe[0] );
	    XtRemoveInput( status->output_inputId );
	}
	if (status->error_pipe[0]) {
	    CheckReadFromPipe( status->error_pipe[0],
			       &status->error_buffer,
			       &status->error_buf_size,
			       True
			      );
	    close( status->error_pipe[0] );
	    XtRemoveInput( status->error_inputId );
	}
	if (status->error_buffer != NULL) {
	    /* special case for arbitrary shell commands: capture command */
	    if ((strcmp(argv[0], "/bin/sh") == 0) &&
	        (strcmp(argv[1], "-c") == 0)) {
	        status->shell_command = XtNewString(argv[2]);
            } else status->shell_command = (char*) NULL;

	    while (status->error_buffer[status->error_buf_size-1]  == '\0')
		status->error_buf_size--;
	    while (status->error_buffer[status->error_buf_size-1]  == '\n')
		status->error_buffer[--status->error_buf_size] = '\0';
	    DEBUG1( "stderr = \"%s\"\n", status->error_buffer )
	    PopupNotice( status->error_buffer, FreeStatus, (Pointer)status );
	    return_status = -1;
	}
	else {
	    XtFree( (Pointer)status );
	    return_status = 0;
	}
	for (;alt_queue_count;alt_queue_count--) {
	    XPutBackEvent(theDisplay, --eventP);
	}
	if (type_ahead_count) {
	    if (alt_queue_size) eventP = &typeAheadQueue[type_ahead_count];
	    for (;type_ahead_count;type_ahead_count--) {
		XPutBackEvent(theDisplay, --eventP);
	    }
	}
    } else {			/* We're the child process. */
	/* take it from the user's path, else fall back to the mhPath */
	(void) execvp(argv[0], argv);
	(void) execv(FullPathOfCommand(argv[0]), argv);
        progName = argv[0];	/* for Punt message */
	Punt("(cannot execvp it)");
	return_status = -1;
    }
    return return_status;
}


static void
CheckReadFromPipe(
    int fd,
    char **bufP,
    int *lenP,
    Bool waitEOF)
{
    int nread;
/*  DEBUG2( " CheckReadFromPipe #%d,len=%d,", fd, *lenP )  */
#ifdef FIONREAD
    if (!ioctl( fd, FIONREAD, &nread )) {
/*      DEBUG1( "nread=%d ...", nread )			   */
	if (nread) {
	    int old_end = *lenP;
	    *bufP = XtRealloc( *bufP, (Cardinal) ((*lenP += nread) + 1) );
	    read( fd, *bufP+old_end, nread );
	    (*bufP)[old_end+nread] = '\0';
	}
	return;
    }
#endif
    do {
	char buf[512];
	int old_end = *lenP;
	nread = read( fd, buf, 512 );
	if (nread <= 0)
	    break;
	*bufP = XtRealloc( *bufP, (Cardinal) ((*lenP += nread) + 1) );
	memmove( *bufP+old_end, buf, (int) nread );
	(*bufP)[old_end+nread] = '\0';
    } while (waitEOF);
}


/* ARGSUSED */
static void FreeStatus(
    Widget w,			/* unused */
    XtPointer closure,
    XtPointer call_data)	/* unused */
{
    CommandStatus status = (CommandStatus)closure;
    if (status->popup != (Widget)NULL) {
	XtPopdown( status->popup );
	XtDestroyWidget( status->popup );
    }
    if (status->error_buffer != NULL) XtFree(status->error_buffer);
    XtFree( closure );
}

/* Execute the given command, waiting until it's finished.  Put the output
   in the specified file path.  Returns 0 if stderr empty, -1 otherwise */

int DoCommand(
  char * const *argv,		/* The command to execute, and its args. */
  const char *inputfile,	/* Input file for command. */
  const char *outputfile)	/* Output file for command. */
{
    int fd_in, fd_out;
    int status;

    if (inputfile != NULL) {
	FILEPTR file = FOpenAndCheck(inputfile, "r");
	fd_in = dup(fileno(file));
	myfclose(file);
    }
    else
	fd_in = -1;

    if (outputfile) {
	FILEPTR file = FOpenAndCheck(outputfile, "w");
	fd_out = dup(fileno(file));
	myfclose(file);
    }
    else
	fd_out = -1;

    status = _DoCommandToFileOrPipe( argv, fd_in, fd_out, (char **) NULL,
				    (int *) NULL );
    return status;
}

/* Execute the given command, waiting until it's finished.  Put the output
   in a newly mallocced string, and return a pointer to that string. */

char *DoCommandToString(char * const *argv)
{
    char *result = NULL;
    int len = 0;
    _DoCommandToFileOrPipe( argv, -1, -2, &result, &len );
    if (result == NULL) result = XtMalloc((Cardinal) 1);
    result[len] = '\0';
    DEBUG1("('%s')\n", result)
    return result;
}


/* Execute the command to a temporary file, and return the name of the file. */

char *DoCommandToFile(char * const *argv)
{
    char *name;
    FILEPTR file;
    int fd;
    name = MakeNewTempFileName();
    file = FOpenAndCheck(name, "w");
    fd = dup(fileno(file));
    myfclose(file);
    _DoCommandToFileOrPipe(argv, -1, fd, (char **) NULL, (int *) NULL);
    return name;
}