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