2 * Copyright (C) 1984-2019 Mark Nudelman
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.
7 * For more information, see the README file.
12 * Routines to execute other programs.
13 * Necessarily very OS dependent.
22 #if MSDOS_COMPILER==WIN32C && defined(MINGW)
24 #define setdisk(n) _chdrive((n)+1)
28 #define setdisk(n) _chdrive((n)+1)
35 extern int screen_trashed
;
36 extern IFILE curr_ifile
;
42 * Pass the specified command to a shell to be executed.
43 * Like plain "system()", but handles resetting terminal modes, etc.
56 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
57 char cwd
[FILENAME_MAX
+1];
61 * Print the command which is to be executed,
62 * unless the command starts with a "-".
75 #if MSDOS_COMPILER==WIN32C
77 cmd
= getenv("COMSPEC");
80 * Working directory is global on MSDOS.
81 * The child might change the working directory, so we
82 * must save and restore CWD across calls to "system",
83 * or else we won't find our file when we return and
84 * try to "reedit_ifile" it.
86 getcwd(cwd
, FILENAME_MAX
);
91 * Close the current input file.
93 save_ifile
= save_curr_ifile();
94 (void) edit_ifile(NULL_IFILE
);
97 * De-initialize the terminal and take out of raw mode.
100 flush(); /* Make sure the deinit chars get out */
102 #if MSDOS_COMPILER==WIN32C
107 * Restore signals to their defaults.
113 * Force standard input to be the user's terminal
114 * (the normal standard input), even if less's standard input
115 * is coming from a pipe.
120 /* The __open() system call translates "/dev/tty" to "con". */
121 if (__open("/dev/tty", OPEN_READ
) < 0)
123 if (open("/dev/tty", OPEN_READ
) < 0)
129 * Pass the command to the system to be executed.
130 * If we have a SHELL environment variable, use
131 * <$SHELL -c "command"> instead of just <command>.
132 * If the command is empty, just invoke a shell.
136 if ((shell
= lgetenv("SHELL")) != NULL
&& *shell
!= '\0')
142 char *esccmd
= shell_quote(cmd
);
145 int len
= (int) (strlen(shell
) + strlen(esccmd
) + 5);
146 p
= (char *) ecalloc(len
, sizeof(char));
147 SNPRINTF3(p
, len
, "%s %s %s", shell
, shell_coption(), esccmd
);
162 #if MSDOS_COMPILER==DJGPPC
164 * Make stdin of the child be in cooked mode.
168 * We don't need to catch signals of the child (it
169 * also makes trouble with some DPMI servers).
171 __djgpp_exception_toggle();
173 __djgpp_exception_toggle();
181 * Restore standard input, reset signals, raw mode, etc.
188 #if MSDOS_COMPILER==WIN32C
196 putstr(" (press RETURN)");
204 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
206 * Restore the previous directory (possibly
207 * changed by the child program we just ran).
210 #if MSDOS_COMPILER != DJGPPC
212 * Some versions of chdir() don't change to the drive
213 * which is part of CWD. (DJGPP does this in chdir.)
217 if (cwd
[0] >= 'a' && cwd
[0] <= 'z')
218 setdisk(cwd
[0] - 'a');
219 else if (cwd
[0] >= 'A' && cwd
[0] <= 'Z')
220 setdisk(cwd
[0] - 'A');
226 * Reopen the current input file.
228 reedit_ifile(save_ifile
);
230 #if defined(SIGWINCH) || defined(SIGWIND)
232 * Since we were ignoring window change signals while we executed
233 * the system command, we must assume the window changed.
234 * Warning: this leaves a signal pending (in "sigs"),
235 * so psignals() should be called soon after lsystem().
246 * Pipe a section of the input file into the given shell command.
247 * The section to be piped is the section "between" the current
248 * position and the position marked by the given letter.
250 * If the mark is after the current screen, the section between
251 * the top line displayed and the mark is piped.
252 * If the mark is before the current screen, the section between
253 * the mark and the bottom line displayed is piped.
254 * If the mark is on the current screen, or if the mark is ".",
255 * the whole current screen is piped.
262 POSITION mpos
, tpos
, bpos
;
265 * mpos = the marked position.
266 * tpos = top of screen.
267 * bpos = bottom of screen.
270 if (mpos
== NULL_POSITION
)
272 tpos
= position(TOP
);
273 if (tpos
== NULL_POSITION
)
275 bpos
= position(BOTTOM
);
278 return (pipe_data(cmd
, tpos
, bpos
));
279 else if (mpos
<= tpos
)
280 return (pipe_data(cmd
, mpos
, bpos
));
281 else if (bpos
== NULL_POSITION
)
282 return (pipe_data(cmd
, tpos
, bpos
));
284 return (pipe_data(cmd
, tpos
, mpos
));
288 * Create a pipe to the given shell command.
289 * Feed it the file contents between the positions spos and epos.
292 pipe_data(cmd
, spos
, epos
)
301 * This is structured much like lsystem().
302 * Since we're running a shell program, we must be careful
303 * to perform the necessary deinitialization before running
304 * the command, and reinitialization after it.
306 if (ch_seek(spos
) != 0)
308 error("Cannot seek to start position", NULL_PARG
);
312 if ((f
= popen(cmd
, "w")) == NULL
)
314 error("Cannot create pipe", NULL_PARG
);
326 #if MSDOS_COMPILER==WIN32C
330 LSIGNAL(SIGPIPE
, SIG_IGN
);
334 while (epos
== NULL_POSITION
|| spos
++ <= epos
)
337 * Read a character from the file and give it to the pipe.
342 if (putc(c
, f
) == EOF
)
347 * Finish up the last line.
349 while (c
!= '\n' && c
!= EOI
)
354 if (putc(c
, f
) == EOF
)
361 LSIGNAL(SIGPIPE
, SIG_DFL
);
363 #if MSDOS_COMPILER==WIN32C
370 #if defined(SIGWINCH) || defined(SIGWIND)
371 /* {{ Probably don't need this here. }} */