summaryrefslogtreecommitdiff
path: root/system.c
blob: cacc123b82ca94eba620193fd246abfb2ab54cfe (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
/* system.c  -- UNIX version */

/* Author:
 *	Steve Kirkendall
 *	14407 SW Teal Blvd. #C
 *	Beaverton, OR 97005
 *	kirkenda@cs.pdx.edu
 */


/* This file contains a new version of the system() function and related stuff.
 *
 * Entry points are:
 *	system(cmd)	- run a single shell command
 *	wildcard(names)	- expand wildcard characters in filanames
 *	filter(m,n,cmd)	- run text lines through a filter program
 *
 * This is probably the single least portable file in the program.  The code
 * shown here should work correctly if it links at all; it will work on UNIX
 * and any O.S./Compiler combination which adheres to UNIX forking conventions.
 */

#include "config.h"
#include "vi.h"
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern char	**environ;

#if ANY_UNIX

/* This is a new version of the system() function.  The only difference
 * between this one and the library one is: this one uses the o_shell option.
 */
int elvis_system(cmd)
	char	*cmd;	/* a command to run */
{
	int	status;	/* exit status of the command */

	/* warn the user if the file hasn't been saved yet */
	if (*o_warn && tstflag(file, MODIFIED))
	{
		if (mode == MODE_VI)
		{
			mode = MODE_COLON;
		}
		msg("Warning: \"%s\" has been modified but not yet saved", origname);
	}

	signal(SIGINT, SIG_IGN);
	switch (fork())
	{
	  case -1:						/* error */
		msg("fork() failed");
		status = -1;
		break;

	  case 0:						/* child */
		/* for the child, close all files except stdin/out/err */
		for (status = 3; status < 60 && (close(status), errno != EINVAL); status++)
		{
		}

		signal(SIGINT, SIG_DFL);
		if (cmd == o_shell)
		{
			execle(o_shell, o_shell, (char *)0, environ);
		}
		else
		{
			execle(o_shell, o_shell, "-c", cmd, (char *)0, environ);
		}
		msg("execle(\"%s\", ...) failed", o_shell);
		exit(1); /* if we get here, the exec failed */

	  default:						/* parent */
		wait(&status);
		signal(SIGINT, trapint);
	}

	return status;
}

/* This private function opens a pipe from a filter.  It is similar to the
 * system() function above, and to popen(cmd, "r").
 */
static int rpipe(cmd, in)
	char	*cmd;	/* the filter command to use */
	int	in;	/* the fd to use for stdin */
{
	int	r0w1[2];/* the pipe fd's */

	/* make the pipe */
	if (pipe(r0w1) < 0)
	{
		return -1;	/* pipe failed */
	}

	/* The parent process (elvis) ignores signals while the filter runs.
	 * The child (the filter program) will reset this, so that it can
	 * catch the signal.
	 */
	signal(SIGINT, SIG_IGN);

	switch (fork())
	{
	  case -1:						/* error */
		return -1;

	  case 0:						/* child */
		/* close the "read" end of the pipe */
		close(r0w1[0]);

		/* redirect stdout to go to the "write" end of the pipe */
		close(1);
		dup(r0w1[1]);
		close(2);
		dup(r0w1[1]);
		close(r0w1[1]);

		/* redirect stdin */
		if (in != 0)
		{
			close(0);
			dup(in);
			close(in);
		}

		/* the filter should accept SIGINT signals */
		signal(SIGINT, SIG_DFL);

		/* exec the shell to run the command */
		execle(o_shell, o_shell, "-c", cmd, (char *)0, environ);
		exit(1); /* if we get here, exec failed */

	  default:						/* parent */
		/* close the "write" end of the pipe */	
		close(r0w1[1]);

		return r0w1[0];
	}
}

#endif /* non-DOS */

#if OSK

/* This private function opens a pipe from a filter.  It is similar to the
 * system() function above, and to popen(cmd, "r").
 */
static int rpipe(cmd, in)
	char	*cmd;	/* the filter command to use */
	int	in;	/* the fd to use for stdin */
{

    char **argblk;
	char *p, *buffer, *command;
	int stdinp, stdoutp;
	unsigned addstack = 0;
	int words, len, loop = 1;
	int fp, pipe_pid;
	extern int os9forkc();
	extern char *index();

	command = cmd;
	words = 0;
	if ((buffer = (char*) malloc(strlen(cmd))) == (char*) 0)
		return 0;

	do {
		if (!(p = index(command, ' '))) {
			loop--;
			len = strlen(command);
		}
		else
			len = p - command;
		words++;	
		while (command[len] && command[len] == ' ')
			len++;
		if (!command[len])
			break;
		command = command + len;
	}	
	while (loop);
	if ((argblk = (char **)malloc((words+1) * sizeof(char*))) == (char **)0)
		return 0;
	command = cmd;
	words = 0;
	do {
		if (!(p = index(command, ' '))) {
			loop--;
			len = strlen(command);
		}
		else
			len = p - command;
		strncpy(buffer, command, len);
		argblk[words++] = buffer;
		buffer += len;
		*buffer++ = '\0';
		while (command[len] && command[len] == ' ')
			len++;
		if (!command[len])
			break;
		command = command + len;
	} while (loop);
	if (argblk[words - 1][0] == '#') 
		addstack = 1024 * atoi(&argblk[--words][1]);
	argblk[words] = 0;

	stdoutp = dup(1);
	close(1);               /* close stdout */
	if ((fp = open("/pipe",S_IREAD)) < 0) {
		dup(stdoutp);
		close(stdoutp);
		return 0;
	}
	if (in != 0) {
		stdinp = dup(0);
		close(0);
		dup(in);
		close(in);
	}
	pipe_pid = os9exec(os9forkc,argblk[0],argblk,environ,addstack,0,3);
	if (pipe_pid == -1) {
		fclose(fp);
		dup(stdoutp);
		close(stdoutp);
		if (in != 0) {
			close(0);
			dup(stdinp);
		}
		return 0;
	}
	fp = (short)dup(1);     /* save pipe */
	close(1);               /* get rid of the pipe */
	dup(stdoutp);           /* restore old stdout */
	close(stdoutp);         /* close path to stdout copy */
	if (in != 0) {
		close(0);
		dup(stdinp);
	}
	return fp;
}	
#endif

#if ANY_UNIX || OSK

/* This function closes the pipe opened by rpipe(), and returns 0 for success */
static int rpclose(fd)
	int	fd;
{
	int	status;

	close(fd);
	wait(&status);
	signal(SIGINT, trapint);
	return status;
}

#endif /* non-DOS */

/* This function expands wildcards in a filename or filenames.  It does this
 * by running the "echo" command on the filenames via the shell; it is assumed
 * that the shell will expand the names for you.  If for any reason it can't
 * run echo, then it returns the names unmodified.
 */

#if MSDOS || TOS
#define	PROG	"wildcard "
#define	PROGLEN	9
#include <string.h>
#else
#define	PROG	"echo "
#define	PROGLEN	5
#endif

char *wildcard(names)
	char	*names;
{
	int	i, j, fd;
	REG char *s, *d;


	/* build the echo command */
	if (names != tmpblk.c)
	{
		/* the names aren't in tmpblk.c, so we can do it the easy way */
		strcpy(tmpblk.c, PROG);
		strcat(tmpblk.c, names);
	}
	else
	{
		/* the names are already in tmpblk.c, so shift them to make
		 * room for the word "echo "
		 */
		for (s = names + strlen(names) + 1, d = s + PROGLEN; s > names; )
		{
			*--d = *--s;
		}
		strncpy(names, PROG, PROGLEN);
	}

	/* run the command & read the resulting names */
	fd = rpipe(tmpblk.c, 0);
	if (fd < 0) return names;
	i = 0;
	do
	{
		j = tread(fd, tmpblk.c + i, BLKSIZE - i);
		i += j;
	} while (j > 0);

	/* successful? */
	if (rpclose(fd) == 0 && j == 0 && i < BLKSIZE && i > 0)
	{
		tmpblk.c[i-1] = '\0'; /* "i-1" so we clip off the newline */
		return tmpblk.c;
	}
	else
	{
		return names;
	}
}

/* This function runs a range of lines through a filter program, and replaces
 * the original text with the filtered version.  As a special case, if "to"
 * is MARK_UNSET, then it runs the filter program with stdin coming from
 * /dev/null, and inserts any output lines.
 */
int filter(from, to, cmd)
	MARK	from, to;	/* the range of lines to filter */
	char	*cmd;		/* the filter command */
{
	int	scratch;	/* fd of the scratch file */
	int	fd;		/* fd of the pipe from the filter */
	char	*scrout = NULL;	/* name of the scratch out file */
	MARK	new;		/* place where new text should go */
	int	i;

	/* write the lines (if specified) to a temp file */
	if (to)
	{
		/* we have lines */
		scrout = malloc(strlen(o_directory) + 9 + 1); /* strlen("/soXXXXXX") */
		if (!scrout)
			return -1;
#if MSDOS || TOS
		strcpy(scrout, o_directory);
		if ((i=strlen(scrout)) && strchr("\\/:", scrout[i-1]))
			scrout[i++]=SLASH;
		strcpy(scrout+i, SCRATCHOUT+3);
#else
		sprintf(scrout, SCRATCHOUT, o_directory);
#endif
#if !USE_MKSTEMP
		mktemp(scrout);
		cmd_write(from, to, CMD_BANG, 0, scrout);

		/* use those lines as stdin */
		scratch = open(scrout, O_RDONLY);
		if (scratch < 0)
		{
			unlink(scrout);
			return -1;
		}
#else
		if ((scratch = mkstemp(scrout)) < 0)
			return -1;
		/* use those lines as stdin */
		cmd_write2(from, to, scratch);
		lseek(scratch, 0L, SEEK_SET);
#endif
	}
	else
	{
		scratch = 0;
	}

	/* start the filter program */
	fd = rpipe(cmd, scratch);
	if (fd < 0)
	{
		if (to)
		{
			close(scratch);
			unlink(scrout);
			free(scrout);
		}
		return -1;
	}

	ChangeText
	{
		/* adjust MARKs for whole lines, and set "new" */
		from &= ~(BLKSIZE - 1);
		if (to)
		{
			to &= ~(BLKSIZE - 1);
			to += BLKSIZE;
			new = to;
		}
		else
		{
			new = from + BLKSIZE;
		}

		/* repeatedly read in new text and add it */
		while ((i = tread(fd, tmpblk.c, BLKSIZE - 1)) > 0)
		{
			tmpblk.c[i] = '\0';
			add(new, tmpblk.c);
			for (i = 0; tmpblk.c[i]; i++)
			{
				if (tmpblk.c[i] == '\n')
				{
					new = (new & ~(BLKSIZE - 1)) + BLKSIZE;
				}
				else
				{
					new++;
				}
			}
		}
	}

	/* delete old text, if any */
	if (to)
	{
		delete(from, to);
	}

	/* Reporting... */
	rptlabel = "more";
	if (rptlines < 0)
	{
		rptlines = -rptlines;
		rptlabel = "less";
	}

	/* cleanup */
	rpclose(fd);
	if (to)
	{
		close(scratch);
		unlink(scrout);
		free(scrout);
	}
	return 0;
}