1 /* $NetBSD: lsystem.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * Routines to execute other programs.
15 * Necessarily very OS dependent.
26 #define setdisk(n) _chdrive((n)+1)
32 extern int screen_trashed
;
33 extern IFILE curr_ifile
;
39 * Pass the specified command to a shell to be executed.
40 * Like plain "system()", but handles resetting terminal modes, etc.
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.
117 /* The __open() system call translates "/dev/tty" to "con". */
118 if (__open("/dev/tty", OPEN_READ
) < 0)
120 if (open("/dev/tty", OPEN_READ
) < 0)
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
= 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. }} */