Subversion Repositories planix.SVN

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
99 7u83 1
/*
2
 * This code contains changes by
3
 *      Gunnar Ritter, Freiburg i. Br., Germany, 2002. All rights reserved.
4
 *
5
 * Conditions 1, 2, and 4 and the no-warranty notice below apply
6
 * to these changes.
7
 *
8
 *
9
 * Copyright (c) 1980, 1993
10
 * 	The Regents of the University of California.  All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions
14
 * are met:
15
 * 1. Redistributions of source code must retain the above copyright
16
 *    notice, this list of conditions and the following disclaimer.
17
 * 2. Redistributions in binary form must reproduce the above copyright
18
 *    notice, this list of conditions and the following disclaimer in the
19
 *    documentation and/or other materials provided with the distribution.
20
 * 3. All advertising materials mentioning features or use of this software
21
 *    must display the following acknowledgement:
22
 * 	This product includes software developed by the University of
23
 * 	California, Berkeley and its contributors.
24
 * 4. Neither the name of the University nor the names of its contributors
25
 *    may be used to endorse or promote products derived from this software
26
 *    without specific prior written permission.
27
 *
28
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
 * SUCH DAMAGE.
39
 *
40
 *
41
 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
42
 *
43
 * Redistribution and use in source and binary forms, with or without
44
 * modification, are permitted provided that the following conditions
45
 * are met:
46
 *   Redistributions of source code and documentation must retain the
47
 *    above copyright notice, this list of conditions and the following
48
 *    disclaimer.
49
 *   Redistributions in binary form must reproduce the above copyright
50
 *    notice, this list of conditions and the following disclaimer in the
51
 *    documentation and/or other materials provided with the distribution.
52
 *   All advertising materials mentioning features or use of this software
53
 *    must display the following acknowledgement:
54
 *      This product includes software developed or owned by Caldera
55
 *      International, Inc.
56
 *   Neither the name of Caldera International, Inc. nor the names of
57
 *    other contributors may be used to endorse or promote products
58
 *    derived from this software without specific prior written permission.
59
 *
60
 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
61
 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
62
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
63
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64
 * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
65
 * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
66
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
68
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
69
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
70
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
71
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72
 */
73
 
74
#ifndef	lint
75
#ifdef	DOSCCS
76
static char sccsid[] = "@(#)ex_unix.c	1.16 (gritter) 11/23/04";
77
#endif
78
#endif
79
 
80
/* from ex_unix.c	7.6 (Berkeley) 10/22/85 */
81
 
82
#include "ex.h"
83
#include "ex_temp.h"
84
#include "ex_tty.h"
85
#include "ex_vis.h"
86
#include <sys/wait.h>
87
 
88
/*
89
 * Unix escapes, filtering
90
 */
91
 
92
/*
93
 * First part of a shell escape,
94
 * parse the line, expanding # and % and ! and printing if implied.
95
 */
96
void 
97
unix0(int warn)
98
{
99
	register char *up, *fp;
100
	register short c;
101
	char printub, puxb[UXBSIZE + sizeof (int)];
102
 
103
	printub = 0;
104
	CP(puxb, uxb);
105
	c = getchar();
106
	if (c == '\n' || c == EOF)
107
		error(catgets(catd, 1, 192,
108
	"Incomplete shell escape command@- use 'shell' to get a shell"));
109
	up = uxb;
110
	do {
111
		switch (c) {
112
 
113
		case '\\':
114
			if (any(peekchar(), "%#!"))
115
				c = getchar();
116
		default:
117
			if (up >= &uxb[UXBSIZE]) {
118
tunix:
119
				uxb[0] = 0;
120
				error(catgets(catd, 1, 193,
121
							"Command too long"));
122
			}
123
			*up++ = c;
124
			break;
125
 
126
		case '!':
127
			fp = puxb;
128
			if (*fp == 0) {
129
				uxb[0] = 0;
130
				error(catgets(catd, 1, 194,
131
			"No previous command@to substitute for !"));
132
			}
133
			printub++;
134
			while (*fp) {
135
				if (up >= &uxb[UXBSIZE])
136
					goto tunix;
137
				*up++ = *fp++;
138
			}
139
			break;
140
 
141
		case '#':
142
			fp = altfile;
143
			if (*fp == 0) {
144
				uxb[0] = 0;
145
				error(catgets(catd, 1, 195,
146
				"No alternate filename@to substitute for #"));
147
			}
148
			goto uexp;
149
 
150
		case '%':
151
			fp = savedfile;
152
			if (*fp == 0) {
153
				uxb[0] = 0;
154
				error(catgets(catd, 1, 196,
155
					"No filename@to substitute for %%"));
156
			}
157
uexp:
158
			printub++;
159
			while (*fp) {
160
				if (up >= &uxb[UXBSIZE])
161
					goto tunix;
162
#ifndef	BIT8
163
				*up++ = *fp++ | QUOTE;
164
#else
165
				*up++ = *fp++;
166
#endif
167
			}
168
			break;
169
		}
170
		c = getchar();
171
	} while (c == '"' || c == '|' || !endcmd(c));
172
	if (c == EOF)
173
		ungetchar(c);
174
	*up = 0;
175
	if (!inopen)
176
		resetflav();
177
	if (warn)
178
		ckaw();
179
	if (warn && hush == 0 && chng && xchng != chng && value(WARN) && dol > zero) {
180
		xchng = chng;
181
		vnfl();
182
		printf(mesg(catgets(catd, 1, 197,
183
				"[No write]|[No write since last change]")));
184
		noonl();
185
		flush();
186
	} else
187
		warn = 0;
188
	if (printub) {
189
		if (uxb[0] == 0)
190
			error(catgets(catd, 1, 198,
191
					"No previous command@to repeat"));
192
		if (inopen) {
193
			splitw++;
194
			vclean();
195
			vgoto(WECHO, 0);
196
		}
197
		if (warn)
198
			vnfl();
199
		if (hush == 0)
200
			lprintf("!%s", uxb);
201
		if (inopen && Outchar != termchar) {
202
			vclreol();
203
			vgoto(WECHO, 0);
204
		} else
205
			putnl();
206
		flush();
207
	}
208
}
209
 
210
/*
211
 * Do the real work for execution of a shell escape.
212
 * Mode is like the number passed to open system calls
213
 * and indicates filtering.  If input is implied, newstdin
214
 * must have been setup already.
215
 */
216
struct termios 
217
unixex(char *opt, char *up, int newstdin, int mode)
218
{
219
	int pvec[2];
220
	struct termios f;
221
 
222
	signal(SIGINT, SIG_IGN);
223
#ifdef SIGTSTP
224
	if (dosusp)
225
		signal(SIGTSTP, SIG_DFL);
226
#endif
227
	if (inopen)
228
		f = setty(normf);
229
	if ((mode & 1) && pipe(pvec) < 0) {
230
		/* Newstdin should be io so it will be closed */
231
		if (inopen)
232
			setty(f);
233
		error(catgets(catd, 1, 199, "Can't make pipe for filter"));
234
	}
235
#ifndef VFORK
236
	pid = fork();
237
#else
238
	pid = vfork();
239
#endif
240
	if (pid < 0) {
241
		if (mode & 1) {
242
			close(pvec[0]);
243
			close(pvec[1]);
244
		}
245
		setrupt();
246
		error(catgets(catd, 1, 200, "No more processes"));
247
	}
248
	if (pid == 0) {
249
		if (mode & 2) {
250
			close(0);
251
			dup(newstdin);
252
			close(newstdin);
253
		}
254
		if (mode & 1) {
255
			close(pvec[0]);
256
			close(1);
257
			dup(pvec[1]);
258
			if (inopen) {
259
				close(2);
260
				dup(1);
261
			}
262
			close(pvec[1]);
263
		}
264
		if (io)
265
			close(io);
266
		if (tfile)
267
			close(tfile);
268
#ifndef VMUNIX
269
		close(erfile);
270
#endif
271
		signal(SIGHUP, oldhup);
272
		signal(SIGQUIT, oldquit);
273
#ifdef	SIGXFSZ
274
		signal(SIGXFSZ, oldxfsz);
275
#endif
276
		if (ruptible)
277
			signal(SIGINT, SIG_DFL);
278
		execl(svalue(SHELL), "sh", opt, up, (char *)0);
279
		printf(catgets(catd, 1, 201, "No %s!\n"), svalue(SHELL));
280
		error(NOSTR);
281
	}
282
	if (mode & 1) {
283
		io = pvec[0];
284
		close(pvec[1]);
285
	}
286
	if (newstdin)
287
		close(newstdin);
288
	return (f);
289
}
290
 
291
/*
292
 * Wait for the command to complete.
293
 * F is for restoration of tty mode if from open/visual.
294
 * C flags suppression of printing.
295
 */
296
void 
297
unixwt(int c, struct termios f)
298
{
299
 
300
	waitfor();
301
#ifdef SIGTSTP
302
	if (dosusp)
303
		signal(SIGTSTP, onsusp);
304
#endif
305
	if (inopen)
306
		setty(f);
307
	setrupt();
308
	if (!inopen && c && hush == 0) {
309
		printf("!\n");
310
		flush();
311
		termreset();
312
		gettmode();
313
	}
314
}
315
 
316
/*
317
 * Setup a pipeline for the filtration implied by mode
318
 * which is like a open number.  If input is required to
319
 * the filter, then a child editor is created to write it.
320
 * If output is catch it from io which is created by unixex.
321
 */
322
void 
323
filter(register int mode)
324
{
325
	static int pvec[2];
326
	struct termios f;	/* mjm: was register */
327
	register int lines = lineDOL();
328
	struct stat statb;
329
 
330
	mode++;
331
	if (mode & 2) {
332
		signal(SIGINT, SIG_IGN);
333
		if (pipe(pvec) < 0)
334
			error(catgets(catd, 1, 202, "Can't make pipe"));
335
		pid = fork();
336
		io = pvec[0];
337
		if (pid < 0) {
338
			setrupt();
339
			close(pvec[1]);
340
			error(catgets(catd, 1, 203, "No more processes"));
341
		}
342
		if (pid == 0) {
343
			setrupt();
344
			io = pvec[1];
345
			close(pvec[0]);
346
			putfile(1);
347
			exitex(0);
348
		}
349
		close(pvec[1]);
350
		io = pvec[0];
351
		setrupt();
352
	}
353
	f = unixex("-c", uxb, (mode & 2) ? pvec[0] : 0, mode);
354
	if (mode == 3) {
355
		delete(0);
356
		addr2 = addr1 - 1;
357
	} else if (mode == 1)
358
		deletenone();
359
	if (mode & 1) {
360
		if(FIXUNDO)
361
			undap1 = undap2 = addr2+1;
362
		if (fstat(io, &statb) < 0 || statb.st_blksize > LBSIZE)
363
			bsize = LBSIZE;
364
		else {
365
			bsize = statb.st_blksize;
366
			if (bsize <= 0)
367
				bsize = LBSIZE;
368
		}
369
		ignore(append(getfile, addr2));
370
#ifdef TRACE
371
		if (trace)
372
			vudump("after append in filter");
373
#endif
374
	}
375
	close(io);
376
	io = -1;
377
	unixwt(!inopen, f);
378
	netchHAD(lines);
379
}
380
 
381
/*
382
 * Set up to do a recover, getting io to be a pipe from
383
 * the recover process.
384
 */
385
void 
386
recover(void)
387
{
388
	static int pvec[2];
389
 
390
	if (pipe(pvec) < 0)
391
		error(catgets(catd, 1, 204, " Can't make pipe for recovery"));
392
	pid = fork();
393
	io = pvec[0];
394
	if (pid < 0) {
395
		close(pvec[1]);
396
		error(catgets(catd, 1, 205, " Can't fork to execute recovery"));
397
	}
398
	if (pid == 0) {
399
		close(2);
400
		dup(1);
401
		close(1);
402
		dup(pvec[1]);
403
	        close(pvec[1]);
404
		execl(EXRECOVER, "exrecover", svalue(DIRECTORY), file, (char *) 0);
405
		close(1);
406
		dup(2);
407
		error(catgets(catd, 1, 206, " No recovery routine"));
408
	}
409
	close(pvec[1]);
410
}
411
 
412
/*
413
 * Wait for the process (pid an external) to complete.
414
 */
415
void 
416
waitfor(void)
417
{
418
	int stat = 0;
419
	pid_t wpid;
420
 
421
	do {
422
		wpid = wait(&stat);
423
		if (wpid == pid) {
424
			status = stat;
425
			rpid = wpid;
426
		}
427
	} while (wpid != -1);
428
	if (status) {
429
		if (WIFEXITED(status))
430
			status = WEXITSTATUS(status);
431
		else
432
			status = 0;
433
	}
434
}
435
 
436
/*
437
 * The end of a recover operation.  If the process
438
 * exits non-zero, force not edited; otherwise force
439
 * a write.
440
 */
441
void 
442
revocer(void)
443
{
444
 
445
	waitfor();
446
	if (pid == rpid && status != 0)
447
		edited = 0;
448
	else
449
		change();
450
}