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