2 * Copyright (C) 1984-2021 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.
121 /* The __open() system call translates "/dev/tty" to "con". */
122 if (__open(tty_device(), OPEN_READ) < 0)
124 if (open(tty_device(), OPEN_READ) < 0)
131 * Pass the command to the system to be executed.
132 * If we have a SHELL environment variable, use
133 * <$SHELL -c "command"> instead of just <command>.
134 * If the command is empty, just invoke a shell.
138 if ((shell = lgetenv("SHELL")) != NULL && *shell != '\0')
144 char *esccmd = shell_quote(cmd);
147 int len = (int) (strlen(shell) + strlen(esccmd) + 5);
148 p = (char *) ecalloc(len, sizeof(char));
149 SNPRINTF3(p, len, "%s %s %s", shell, shell_coption(), esccmd);
164 #if MSDOS_COMPILER==DJGPPC
166 * Make stdin of the child be in cooked mode.
170 * We don't need to catch signals of the child (it
171 * also makes trouble with some DPMI servers).
173 __djgpp_exception_toggle();
175 __djgpp_exception_toggle();
183 * Restore standard input, reset signals, raw mode, etc.
190 #if MSDOS_COMPILER==WIN32C
198 putstr(" (press RETURN)");
206 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
208 * Restore the previous directory (possibly
209 * changed by the child program we just ran).
212 #if MSDOS_COMPILER != DJGPPC
214 * Some versions of chdir() don't change to the drive
215 * which is part of CWD. (DJGPP does this in chdir.)
219 if (cwd[0] >= 'a' && cwd[0] <= 'z')
220 setdisk(cwd[0] - 'a');
221 else if (cwd[0] >= 'A' && cwd[0] <= 'Z')
222 setdisk(cwd[0] - 'A');
228 * Reopen the current input file.
230 reedit_ifile(save_ifile);
232 #if defined(SIGWINCH) || defined(SIGWIND)
234 * Since we were ignoring window change signals while we executed
235 * the system command, we must assume the window changed.
236 * Warning: this leaves a signal pending (in "sigs"),
237 * so psignals() should be called soon after lsystem().
248 * Pipe a section of the input file into the given shell command.
249 * The section to be piped is the section "between" the current
250 * position and the position marked by the given letter.
252 * If the mark is after the current screen, the section between
253 * the top line displayed and the mark is piped.
254 * If the mark is before the current screen, the section between
255 * the mark and the bottom line displayed is piped.
256 * If the mark is on the current screen, or if the mark is ".",
257 * the whole current screen is piped.
264 POSITION mpos, tpos, bpos;
267 * mpos = the marked position.
268 * tpos = top of screen.
269 * bpos = bottom of screen.
272 if (mpos == NULL_POSITION)
274 tpos = position(TOP);
275 if (tpos == NULL_POSITION)
277 bpos = position(BOTTOM);
280 return (pipe_data(cmd, tpos, bpos));
281 else if (mpos <= tpos)
282 return (pipe_data(cmd, mpos, bpos));
283 else if (bpos == NULL_POSITION)
284 return (pipe_data(cmd, tpos, bpos));
286 return (pipe_data(cmd, tpos, mpos));
290 * Create a pipe to the given shell command.
291 * Feed it the file contents between the positions spos and epos.
294 pipe_data(cmd, spos, epos)
303 * This is structured much like lsystem().
304 * Since we're running a shell program, we must be careful
305 * to perform the necessary deinitialization before running
306 * the command, and reinitialization after it.
308 if (ch_seek(spos) != 0)
310 error("Cannot seek to start position", NULL_PARG);
314 if ((f = popen(cmd, "w")) == NULL)
316 error("Cannot create pipe", NULL_PARG);
328 #if MSDOS_COMPILER==WIN32C
332 LSIGNAL(SIGPIPE, SIG_IGN);
336 while (epos == NULL_POSITION || spos++ <= epos)
339 * Read a character from the file and give it to the pipe.
344 if (putc(c, f) == EOF)
349 * Finish up the last line.
351 while (c != '\n' && c != EOI )
356 if (putc(c, f) == EOF)
363 LSIGNAL(SIGPIPE, SIG_DFL);
365 #if MSDOS_COMPILER==WIN32C
372 #if defined(SIGWINCH) || defined(SIGWIND)
373 /* {{ Probably don't need this here. }} */