xref: /minix/external/bsd/less/dist/lsystem.c (revision 84d9c625)
1 /*	$NetBSD: lsystem.c,v 1.4 2013/09/04 19:44:21 tron Exp $	*/
2 
3 /*
4  * Copyright (C) 1984-2012  Mark Nudelman
5  *
6  * You may distribute under the terms of either the GNU General Public
7  * License or the Less License, as specified in the README file.
8  *
9  * For more information, see the README file.
10  */
11 
12 
13 /*
14  * Routines to execute other programs.
15  * Necessarily very OS dependent.
16  */
17 
18 #include "less.h"
19 #include <signal.h>
20 #include "position.h"
21 
22 #if MSDOS_COMPILER
23 #include <dos.h>
24 #ifdef _MSC_VER
25 #include <direct.h>
26 #define setdisk(n) _chdrive((n)+1)
27 #else
28 #include <dir.h>
29 #endif
30 #endif
31 
32 extern int screen_trashed;
33 extern IFILE curr_ifile;
34 
35 
36 #if HAVE_SYSTEM
37 
38 /*
39  * Pass the specified command to a shell to be executed.
40  * Like plain "system()", but handles resetting terminal modes, etc.
41  */
42 	public void
lsystem(cmd,donemsg)43 lsystem(cmd, donemsg)
44 	char *cmd;
45 	char *donemsg;
46 {
47 	register int inp;
48 #if HAVE_SHELL
49 	register char *shell;
50 	register char *p;
51 #endif
52 	IFILE save_ifile;
53 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
54 	char cwd[FILENAME_MAX+1];
55 #endif
56 
57 	/*
58 	 * Print the command which is to be executed,
59 	 * unless the command starts with a "-".
60 	 */
61 	if (cmd[0] == '-')
62 		cmd++;
63 	else
64 	{
65 		clear_bot();
66 		putstr("!");
67 		putstr(cmd);
68 		putstr("\n");
69 	}
70 
71 #if MSDOS_COMPILER
72 #if MSDOS_COMPILER==WIN32C
73 	if (*cmd == '\0')
74 		cmd = getenv("COMSPEC");
75 #else
76 	/*
77 	 * Working directory is global on MSDOS.
78 	 * The child might change the working directory, so we
79 	 * must save and restore CWD across calls to "system",
80 	 * or else we won't find our file when we return and
81 	 * try to "reedit_ifile" it.
82 	 */
83 	getcwd(cwd, FILENAME_MAX);
84 #endif
85 #endif
86 
87 	/*
88 	 * Close the current input file.
89 	 */
90 	save_ifile = save_curr_ifile();
91 	(void) edit_ifile(NULL_IFILE);
92 
93 	/*
94 	 * De-initialize the terminal and take out of raw mode.
95 	 */
96 	deinit();
97 	flush();	/* Make sure the deinit chars get out */
98 	raw_mode(0);
99 #if MSDOS_COMPILER==WIN32C
100 	close_getchr();
101 #endif
102 
103 	/*
104 	 * Restore signals to their defaults.
105 	 */
106 	init_signals(0);
107 
108 #if HAVE_DUP
109 	/*
110 	 * Force standard input to be the user's terminal
111 	 * (the normal standard input), even if less's standard input
112 	 * is coming from a pipe.
113 	 */
114 	inp = dup(0);
115 	close(0);
116 #if OS2
117 	/* The __open() system call translates "/dev/tty" to "con". */
118 	if (__open("/dev/tty", OPEN_READ) < 0)
119 #else
120 	if (open("/dev/tty", OPEN_READ) < 0)
121 #endif
122 		dup(inp);
123 #endif
124 
125 	/*
126 	 * Pass the command to the system to be executed.
127 	 * If we have a SHELL environment variable, use
128 	 * <$SHELL -c "command"> instead of just <command>.
129 	 * If the command is empty, just invoke a shell.
130 	 */
131 #if HAVE_SHELL
132 	p = NULL;
133 	if ((shell = lgetenv("SHELL")) != NULL && *shell != '\0')
134 	{
135 		if (*cmd == '\0')
136 			p = save(shell);
137 		else
138 		{
139 			char *esccmd = shell_quote(cmd);
140 			if (esccmd != NULL)
141 			{
142 				int len = strlen(shell) + strlen(esccmd) + 5;
143 				p = (char *) ecalloc(len, sizeof(char));
144 				SNPRINTF3(p, len, "%s %s %s", shell, shell_coption(), esccmd);
145 				free(esccmd);
146 			}
147 		}
148 	}
149 	if (p == NULL)
150 	{
151 		if (*cmd == '\0')
152 			p = save("sh");
153 		else
154 			p = save(cmd);
155 	}
156 	system(p);
157 	free(p);
158 #else
159 #if MSDOS_COMPILER==DJGPPC
160 	/*
161 	 * Make stdin of the child be in cooked mode.
162 	 */
163 	setmode(0, O_TEXT);
164 	/*
165 	 * We don't need to catch signals of the child (it
166 	 * also makes trouble with some DPMI servers).
167 	 */
168 	__djgpp_exception_toggle();
169   	system(cmd);
170 	__djgpp_exception_toggle();
171 #else
172 	system(cmd);
173 #endif
174 #endif
175 
176 #if HAVE_DUP
177 	/*
178 	 * Restore standard input, reset signals, raw mode, etc.
179 	 */
180 	close(0);
181 	dup(inp);
182 	close(inp);
183 #endif
184 
185 #if MSDOS_COMPILER==WIN32C
186 	open_getchr();
187 #endif
188 	init_signals(1);
189 	raw_mode(1);
190 	if (donemsg != NULL)
191 	{
192 		putstr(donemsg);
193 		putstr("  (press RETURN)");
194 		get_return();
195 		putchr('\n');
196 		flush();
197 	}
198 	init();
199 	screen_trashed = 1;
200 
201 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
202 	/*
203 	 * Restore the previous directory (possibly
204 	 * changed by the child program we just ran).
205 	 */
206 	chdir(cwd);
207 #if MSDOS_COMPILER != DJGPPC
208 	/*
209 	 * Some versions of chdir() don't change to the drive
210 	 * which is part of CWD.  (DJGPP does this in chdir.)
211 	 */
212 	if (cwd[1] == ':')
213 	{
214 		if (cwd[0] >= 'a' && cwd[0] <= 'z')
215 			setdisk(cwd[0] - 'a');
216 		else if (cwd[0] >= 'A' && cwd[0] <= 'Z')
217 			setdisk(cwd[0] - 'A');
218 	}
219 #endif
220 #endif
221 
222 	/*
223 	 * Reopen the current input file.
224 	 */
225 	reedit_ifile(save_ifile);
226 
227 #if defined(SIGWINCH) || defined(SIGWIND)
228 	/*
229 	 * Since we were ignoring window change signals while we executed
230 	 * the system command, we must assume the window changed.
231 	 * Warning: this leaves a signal pending (in "sigs"),
232 	 * so psignals() should be called soon after lsystem().
233 	 */
234 	winch(0);
235 #endif
236 }
237 
238 #endif
239 
240 #if PIPEC
241 
242 /*
243  * Pipe a section of the input file into the given shell command.
244  * The section to be piped is the section "between" the current
245  * position and the position marked by the given letter.
246  *
247  * If the mark is after the current screen, the section between
248  * the top line displayed and the mark is piped.
249  * If the mark is before the current screen, the section between
250  * the mark and the bottom line displayed is piped.
251  * If the mark is on the current screen, or if the mark is ".",
252  * the whole current screen is piped.
253  */
254 	public int
pipe_mark(c,cmd)255 pipe_mark(c, cmd)
256 	int c;
257 	char *cmd;
258 {
259 	POSITION mpos, tpos, bpos;
260 
261 	/*
262 	 * mpos = the marked position.
263 	 * tpos = top of screen.
264 	 * bpos = bottom of screen.
265 	 */
266 	mpos = markpos(c);
267 	if (mpos == NULL_POSITION)
268 		return (-1);
269 	tpos = position(TOP);
270 	if (tpos == NULL_POSITION)
271 		tpos = ch_zero();
272 	bpos = position(BOTTOM);
273 
274  	if (c == '.')
275  		return (pipe_data(cmd, tpos, bpos));
276  	else if (mpos <= tpos)
277  		return (pipe_data(cmd, mpos, bpos));
278  	else if (bpos == NULL_POSITION)
279  		return (pipe_data(cmd, tpos, bpos));
280  	else
281  		return (pipe_data(cmd, tpos, mpos));
282 }
283 
284 /*
285  * Create a pipe to the given shell command.
286  * Feed it the file contents between the positions spos and epos.
287  */
288 	public int
pipe_data(cmd,spos,epos)289 pipe_data(cmd, spos, epos)
290 	char *cmd;
291 	POSITION spos;
292 	POSITION epos;
293 {
294 	register FILE *f;
295 	register int c;
296 
297 	/*
298 	 * This is structured much like lsystem().
299 	 * Since we're running a shell program, we must be careful
300 	 * to perform the necessary deinitialization before running
301 	 * the command, and reinitialization after it.
302 	 */
303 	if (ch_seek(spos) != 0)
304 	{
305 		error("Cannot seek to start position", NULL_PARG);
306 		return (-1);
307 	}
308 
309 	if ((f = popen(cmd, "w")) == NULL)
310 	{
311 		error("Cannot create pipe", NULL_PARG);
312 		return (-1);
313 	}
314 	clear_bot();
315 	putstr("!");
316 	putstr(cmd);
317 	putstr("\n");
318 
319 	deinit();
320 	flush();
321 	raw_mode(0);
322 	init_signals(0);
323 #if MSDOS_COMPILER==WIN32C
324 	close_getchr();
325 #endif
326 #ifdef SIGPIPE
327 	LSIGNAL(SIGPIPE, SIG_IGN);
328 #endif
329 
330 	c = EOI;
331 	while (epos == NULL_POSITION || spos++ <= epos)
332 	{
333 		/*
334 		 * Read a character from the file and give it to the pipe.
335 		 */
336 		c = ch_forw_get();
337 		if (c == EOI)
338 			break;
339 		if (putc(c, f) == EOF)
340 			break;
341 	}
342 
343 	/*
344 	 * Finish up the last line.
345 	 */
346  	while (c != '\n' && c != EOI )
347  	{
348  		c = ch_forw_get();
349  		if (c == EOI)
350  			break;
351  		if (putc(c, f) == EOF)
352  			break;
353  	}
354 
355 	pclose(f);
356 
357 #ifdef SIGPIPE
358 	LSIGNAL(SIGPIPE, SIG_DFL);
359 #endif
360 #if MSDOS_COMPILER==WIN32C
361 	open_getchr();
362 #endif
363 	init_signals(1);
364 	raw_mode(1);
365 	init();
366 	screen_trashed = 1;
367 #if defined(SIGWINCH) || defined(SIGWIND)
368 	/* {{ Probably don't need this here. }} */
369 	winch(0);
370 #endif
371 	return (0);
372 }
373 
374 #endif
375