2 * Copyright (C) 1984-2015 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.
24 #define setdisk(n) _chdrive((n)+1)
30 extern int screen_trashed
;
31 extern IFILE curr_ifile
;
37 * Pass the specified command to a shell to be executed.
38 * Like plain "system()", but handles resetting terminal modes, etc.
51 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
52 char cwd
[FILENAME_MAX
+1];
56 * Print the command which is to be executed,
57 * unless the command starts with a "-".
70 #if MSDOS_COMPILER==WIN32C
72 cmd
= getenv("COMSPEC");
75 * Working directory is global on MSDOS.
76 * The child might change the working directory, so we
77 * must save and restore CWD across calls to "system",
78 * or else we won't find our file when we return and
79 * try to "reedit_ifile" it.
81 getcwd(cwd
, FILENAME_MAX
);
86 * Close the current input file.
88 save_ifile
= save_curr_ifile();
89 (void) edit_ifile(NULL_IFILE
);
92 * De-initialize the terminal and take out of raw mode.
95 flush(); /* Make sure the deinit chars get out */
97 #if MSDOS_COMPILER==WIN32C
102 * Restore signals to their defaults.
108 * Force standard input to be the user's terminal
109 * (the normal standard input), even if less's standard input
110 * is coming from a pipe.
115 /* The __open() system call translates "/dev/tty" to "con". */
116 if (__open("/dev/tty", OPEN_READ
) < 0)
118 if (open("/dev/tty", OPEN_READ
) < 0)
124 * Pass the command to the system to be executed.
125 * If we have a SHELL environment variable, use
126 * <$SHELL -c "command"> instead of just <command>.
127 * If the command is empty, just invoke a shell.
131 if ((shell
= lgetenv("SHELL")) != NULL
&& *shell
!= '\0')
137 char *esccmd
= shell_quote(cmd
);
140 int len
= (int) (strlen(shell
) + strlen(esccmd
) + 5);
141 p
= (char *) ecalloc(len
, sizeof(char));
142 SNPRINTF3(p
, len
, "%s %s %s", shell
, shell_coption(), esccmd
);
157 #if MSDOS_COMPILER==DJGPPC
159 * Make stdin of the child be in cooked mode.
163 * We don't need to catch signals of the child (it
164 * also makes trouble with some DPMI servers).
166 __djgpp_exception_toggle();
168 __djgpp_exception_toggle();
176 * Restore standard input, reset signals, raw mode, etc.
183 #if MSDOS_COMPILER==WIN32C
191 putstr(" (press RETURN)");
199 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
201 * Restore the previous directory (possibly
202 * changed by the child program we just ran).
205 #if MSDOS_COMPILER != DJGPPC
207 * Some versions of chdir() don't change to the drive
208 * which is part of CWD. (DJGPP does this in chdir.)
212 if (cwd
[0] >= 'a' && cwd
[0] <= 'z')
213 setdisk(cwd
[0] - 'a');
214 else if (cwd
[0] >= 'A' && cwd
[0] <= 'Z')
215 setdisk(cwd
[0] - 'A');
221 * Reopen the current input file.
223 reedit_ifile(save_ifile
);
225 #if defined(SIGWINCH) || defined(SIGWIND)
227 * Since we were ignoring window change signals while we executed
228 * the system command, we must assume the window changed.
229 * Warning: this leaves a signal pending (in "sigs"),
230 * so psignals() should be called soon after lsystem().
241 * Pipe a section of the input file into the given shell command.
242 * The section to be piped is the section "between" the current
243 * position and the position marked by the given letter.
245 * If the mark is after the current screen, the section between
246 * the top line displayed and the mark is piped.
247 * If the mark is before the current screen, the section between
248 * the mark and the bottom line displayed is piped.
249 * If the mark is on the current screen, or if the mark is ".",
250 * the whole current screen is piped.
257 POSITION mpos
, tpos
, bpos
;
260 * mpos = the marked position.
261 * tpos = top of screen.
262 * bpos = bottom of screen.
265 if (mpos
== NULL_POSITION
)
267 tpos
= position(TOP
);
268 if (tpos
== NULL_POSITION
)
270 bpos
= position(BOTTOM
);
273 return (pipe_data(cmd
, tpos
, bpos
));
274 else if (mpos
<= tpos
)
275 return (pipe_data(cmd
, mpos
, bpos
));
276 else if (bpos
== NULL_POSITION
)
277 return (pipe_data(cmd
, tpos
, bpos
));
279 return (pipe_data(cmd
, tpos
, mpos
));
283 * Create a pipe to the given shell command.
284 * Feed it the file contents between the positions spos and epos.
287 pipe_data(cmd
, spos
, epos
)
294 extern FILE *popen();
297 * This is structured much like lsystem().
298 * Since we're running a shell program, we must be careful
299 * to perform the necessary deinitialization before running
300 * the command, and reinitialization after it.
302 if (ch_seek(spos
) != 0)
304 error("Cannot seek to start position", NULL_PARG
);
308 if ((f
= popen(cmd
, "w")) == NULL
)
310 error("Cannot create pipe", NULL_PARG
);
322 #if MSDOS_COMPILER==WIN32C
326 LSIGNAL(SIGPIPE
, SIG_IGN
);
330 while (epos
== NULL_POSITION
|| spos
++ <= epos
)
333 * Read a character from the file and give it to the pipe.
338 if (putc(c
, f
) == EOF
)
343 * Finish up the last line.
345 while (c
!= '\n' && c
!= EOI
)
350 if (putc(c
, f
) == EOF
)
357 LSIGNAL(SIGPIPE
, SIG_DFL
);
359 #if MSDOS_COMPILER==WIN32C
366 #if defined(SIGWINCH) || defined(SIGWIND)
367 /* {{ Probably don't need this here. }} */