2 * Copyright (C) 1984-2008 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 about less, or for information on how to
8 * contact the author, see the README file.
13 * Routines to execute other programs.
14 * Necessarily very OS dependent.
25 #define setdisk(n) _chdrive((n)+1)
31 extern int screen_trashed
;
32 extern IFILE curr_ifile
;
38 * Pass the specified command to a shell to be executed.
39 * Like plain "system()", but handles resetting terminal modes, etc.
52 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
53 char cwd
[FILENAME_MAX
+1];
57 * Print the command which is to be executed,
58 * unless the command starts with a "-".
71 #if MSDOS_COMPILER==WIN32C
73 cmd
= getenv("COMSPEC");
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.
82 getcwd(cwd
, FILENAME_MAX
);
87 * Close the current input file.
89 save_ifile
= save_curr_ifile();
90 (void) edit_ifile(NULL_IFILE
);
93 * De-initialize the terminal and take out of raw mode.
96 flush(); /* Make sure the deinit chars get out */
98 #if MSDOS_COMPILER==WIN32C
103 * Restore signals to their defaults.
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.
116 /* The __open() system call translates "/dev/tty" to "con". */
117 if (__open("/dev/tty", OPEN_READ
) < 0)
119 if (open("/dev/tty", OPEN_READ
) < 0)
125 * Pass the command to the system to be executed.
126 * If we have a SHELL environment variable, use
127 * <$SHELL -c "command"> instead of just <command>.
128 * If the command is empty, just invoke a shell.
132 if ((shell
= lgetenv("SHELL")) != NULL
&& *shell
!= '\0')
138 char *esccmd
= shell_quote(cmd
);
141 int len
= strlen(shell
) + strlen(esccmd
) + 5;
142 p
= (char *) ecalloc(len
, sizeof(char));
143 SNPRINTF3(p
, len
, "%s %s %s", shell
, shell_coption(), esccmd
);
158 #if MSDOS_COMPILER==DJGPPC
160 * Make stdin of the child be in cooked mode.
164 * We don't need to catch signals of the child (it
165 * also makes trouble with some DPMI servers).
167 __djgpp_exception_toggle();
169 __djgpp_exception_toggle();
177 * Restore standard input, reset signals, raw mode, etc.
184 #if MSDOS_COMPILER==WIN32C
192 putstr(" (press RETURN)");
200 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
202 * Restore the previous directory (possibly
203 * changed by the child program we just ran).
206 #if MSDOS_COMPILER != DJGPPC
208 * Some versions of chdir() don't change to the drive
209 * which is part of CWD. (DJGPP does this in chdir.)
213 if (cwd
[0] >= 'a' && cwd
[0] <= 'z')
214 setdisk(cwd
[0] - 'a');
215 else if (cwd
[0] >= 'A' && cwd
[0] <= 'Z')
216 setdisk(cwd
[0] - 'A');
222 * Reopen the current input file.
224 reedit_ifile(save_ifile
);
226 #if defined(SIGWINCH) || defined(SIGWIND)
228 * Since we were ignoring window change signals while we executed
229 * the system command, we must assume the window changed.
230 * Warning: this leaves a signal pending (in "sigs"),
231 * so psignals() should be called soon after lsystem().
242 * Pipe a section of the input file into the given shell command.
243 * The section to be piped is the section "between" the current
244 * position and the position marked by the given letter.
246 * If the mark is after the current screen, the section between
247 * the top line displayed and the mark is piped.
248 * If the mark is before the current screen, the section between
249 * the mark and the bottom line displayed is piped.
250 * If the mark is on the current screen, or if the mark is ".",
251 * the whole current screen is piped.
258 POSITION mpos
, tpos
, bpos
;
261 * mpos = the marked position.
262 * tpos = top of screen.
263 * bpos = bottom of screen.
266 if (mpos
== NULL_POSITION
)
268 tpos
= position(TOP
);
269 if (tpos
== NULL_POSITION
)
271 bpos
= position(BOTTOM
);
274 return (pipe_data(cmd
, tpos
, bpos
));
275 else if (mpos
<= tpos
)
276 return (pipe_data(cmd
, mpos
, bpos
));
277 else if (bpos
== NULL_POSITION
)
278 return (pipe_data(cmd
, tpos
, bpos
));
280 return (pipe_data(cmd
, tpos
, mpos
));
284 * Create a pipe to the given shell command.
285 * Feed it the file contents between the positions spos and epos.
288 pipe_data(cmd
, spos
, epos
)
295 extern FILE *popen();
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. }} */