2 * Copyright (C) 1984-2023 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.
45 public void lsystem(char *cmd
, char *donemsg
)
53 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
54 char cwd
[FILENAME_MAX
+1];
58 * Print the command which is to be executed,
59 * unless the command starts with a "-".
72 #if MSDOS_COMPILER==WIN32C
74 cmd
= getenv("COMSPEC");
77 * Working directory is global on MSDOS.
78 * The child might change the working directory, so we
79 * must save and restore CWD across calls to "system",
80 * or else we won't find our file when we return and
81 * try to "reedit_ifile" it.
83 getcwd(cwd
, FILENAME_MAX
);
88 * Close the current input file.
90 save_ifile
= save_curr_ifile();
91 (void) edit_ifile(NULL_IFILE
);
94 * De-initialize the terminal and take out of raw mode.
97 flush(); /* Make sure the deinit chars get out */
99 #if MSDOS_COMPILER==WIN32C
104 * Restore signals to their defaults.
110 * Force standard input to be the user's terminal
111 * (the normal standard input), even if less's standard input
112 * is coming from a pipe.
123 * Pass the command to the system to be executed.
124 * If we have a SHELL environment variable, use
125 * <$SHELL -c "command"> instead of just <command>.
126 * If the command is empty, just invoke a shell.
130 if ((shell
= lgetenv("SHELL")) != NULL
&& *shell
!= '\0')
136 char *esccmd
= shell_quote(cmd
);
139 int len
= (int) (strlen(shell
) + strlen(esccmd
) + 5);
140 p
= (char *) ecalloc(len
, sizeof(char));
141 SNPRINTF3(p
, len
, "%s %s %s", shell
, shell_coption(), esccmd
);
156 #if MSDOS_COMPILER==DJGPPC
158 * Make stdin of the child be in cooked mode.
162 * We don't need to catch signals of the child (it
163 * also makes trouble with some DPMI servers).
165 __djgpp_exception_toggle();
167 __djgpp_exception_toggle();
175 * Restore standard input, reset signals, raw mode, etc.
182 #if MSDOS_COMPILER==WIN32C
190 putstr(" (press RETURN)");
198 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
200 * Restore the previous directory (possibly
201 * changed by the child program we just ran).
204 #if MSDOS_COMPILER != DJGPPC
206 * Some versions of chdir() don't change to the drive
207 * which is part of CWD. (DJGPP does this in chdir.)
211 if (cwd
[0] >= 'a' && cwd
[0] <= 'z')
212 setdisk(cwd
[0] - 'a');
213 else if (cwd
[0] >= 'A' && cwd
[0] <= 'Z')
214 setdisk(cwd
[0] - 'A');
220 * Reopen the current input file.
222 reedit_ifile(save_ifile
);
224 #if defined(SIGWINCH) || defined(SIGWIND)
226 * Since we were ignoring window change signals while we executed
227 * the system command, we must assume the window changed.
228 * Warning: this leaves a signal pending (in "sigs"),
229 * so psignals() should be called soon after lsystem().
240 * Pipe a section of the input file into the given shell command.
241 * The section to be piped is the section "between" the current
242 * position and the position marked by the given letter.
244 * If the mark is after the current screen, the section between
245 * the top line displayed and the mark is piped.
246 * If the mark is before the current screen, the section between
247 * the mark and the bottom line displayed is piped.
248 * If the mark is on the current screen, or if the mark is ".",
249 * the whole current screen is piped.
251 public int pipe_mark(int c
, char *cmd
)
253 POSITION mpos
, tpos
, bpos
;
256 * mpos = the marked position.
257 * tpos = top of screen.
258 * bpos = bottom of screen.
261 if (mpos
== NULL_POSITION
)
263 tpos
= position(TOP
);
264 if (tpos
== NULL_POSITION
)
266 bpos
= position(BOTTOM
);
269 return (pipe_data(cmd
, tpos
, bpos
));
270 else if (mpos
<= tpos
)
271 return (pipe_data(cmd
, mpos
, bpos
));
272 else if (bpos
== NULL_POSITION
)
273 return (pipe_data(cmd
, tpos
, bpos
));
275 return (pipe_data(cmd
, tpos
, mpos
));
279 * Create a pipe to the given shell command.
280 * Feed it the file contents between the positions spos and epos.
282 public int pipe_data(char *cmd
, POSITION spos
, POSITION epos
)
288 * This is structured much like lsystem().
289 * Since we're running a shell program, we must be careful
290 * to perform the necessary deinitialization before running
291 * the command, and reinitialization after it.
293 if (ch_seek(spos
) != 0)
295 error("Cannot seek to start position", NULL_PARG
);
299 if ((f
= popen(cmd
, "w")) == NULL
)
301 error("Cannot create pipe", NULL_PARG
);
313 #if MSDOS_COMPILER==WIN32C
317 LSIGNAL(SIGPIPE
, SIG_IGN
);
321 while (epos
== NULL_POSITION
|| spos
++ <= epos
)
324 * Read a character from the file and give it to the pipe.
329 if (putc(c
, f
) == EOF
)
334 * Finish up the last line.
336 while (c
!= '\n' && c
!= EOI
)
341 if (putc(c
, f
) == EOF
)
348 LSIGNAL(SIGPIPE
, SIG_DFL
);
350 #if MSDOS_COMPILER==WIN32C
357 #if defined(SIGWINCH) || defined(SIGWIND)
358 /* {{ Probably don't need this here. }} */