2 * Copyright (C) 1984-2022 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.
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.
133 if ((shell = lgetenv("SHELL")) != NULL && *shell != '\0')
139 char *esccmd = shell_quote(cmd);
142 int len = (int) (strlen(shell) + strlen(esccmd) + 5);
143 p = (char *) ecalloc(len, sizeof(char));
144 SNPRINTF3(p, len, "%s %s %s", shell, shell_coption(), esccmd);
159 #if MSDOS_COMPILER==DJGPPC
161 * Make stdin of the child be in cooked mode.
165 * We don't need to catch signals of the child (it
166 * also makes trouble with some DPMI servers).
168 __djgpp_exception_toggle();
170 __djgpp_exception_toggle();
178 * Restore standard input, reset signals, raw mode, etc.
185 #if MSDOS_COMPILER==WIN32C
193 putstr(" (press RETURN)");
201 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
203 * Restore the previous directory (possibly
204 * changed by the child program we just ran).
207 #if MSDOS_COMPILER != DJGPPC
209 * Some versions of chdir() don't change to the drive
210 * which is part of CWD. (DJGPP does this in chdir.)
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');
223 * Reopen the current input file.
225 reedit_ifile(save_ifile);
227 #if defined(SIGWINCH) || defined(SIGWIND)
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().
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.
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.
259 POSITION mpos, tpos, bpos;
262 * mpos = the marked position.
263 * tpos = top of screen.
264 * bpos = bottom of screen.
267 if (mpos == NULL_POSITION)
269 tpos = position(TOP);
270 if (tpos == NULL_POSITION)
272 bpos = position(BOTTOM);
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));
281 return (pipe_data(cmd, tpos, mpos));
285 * Create a pipe to the given shell command.
286 * Feed it the file contents between the positions spos and epos.
289 pipe_data(cmd, spos, epos)
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.
303 if (ch_seek(spos) != 0)
305 error("Cannot seek to start position", NULL_PARG);
309 if ((f = popen(cmd, "w")) == NULL)
311 error("Cannot create pipe", NULL_PARG);
323 #if MSDOS_COMPILER==WIN32C
327 LSIGNAL(SIGPIPE, SIG_IGN);
331 while (epos == NULL_POSITION || spos++ <= epos)
334 * Read a character from the file and give it to the pipe.
339 if (putc(c, f) == EOF)
344 * Finish up the last line.
346 while (c != '\n' && c != EOI )
351 if (putc(c, f) == EOF)
358 LSIGNAL(SIGPIPE, SIG_DFL);
360 #if MSDOS_COMPILER==WIN32C
367 #if defined(SIGWINCH) || defined(SIGWIND)
368 /* {{ Probably don't need this here. }} */