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.
41 lsystem(char *cmd, char *donemsg)
49 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
50 char cwd[FILENAME_MAX+1];
54 * Print the command which is to be executed,
55 * unless the command starts with a "-".
68 #if MSDOS_COMPILER==WIN32C
70 cmd = getenv("COMSPEC");
73 * Working directory is global on MSDOS.
74 * The child might change the working directory, so we
75 * must save and restore CWD across calls to "system",
76 * or else we won't find our file when we return and
77 * try to "reedit_ifile" it.
79 getcwd(cwd, FILENAME_MAX);
84 * Close the current input file.
86 save_ifile = save_curr_ifile();
87 (void) edit_ifile(NULL_IFILE);
90 * De-initialize the terminal and take out of raw mode.
93 flush(); /* Make sure the deinit chars get out */
95 #if MSDOS_COMPILER==WIN32C
100 * Restore signals to their defaults.
106 * Force standard input to be the user's terminal
107 * (the normal standard input), even if less's standard input
108 * is coming from a pipe.
113 /* The __open() system call translates "/dev/tty" to "con". */
114 if (__open("/dev/tty", OPEN_READ) < 0)
116 if (open("/dev/tty", OPEN_READ) < 0)
122 * Pass the command to the system to be executed.
123 * If we have a SHELL environment variable, use
124 * <$SHELL -c "command"> instead of just <command>.
125 * If the command is empty, just invoke a shell.
129 if ((shell = lgetenv("SHELL")) != NULL && *shell != '\0')
135 char *esccmd = shell_quote(cmd);
138 int len = (int) (strlen(shell) + strlen(esccmd) + 5);
139 p = (char *) ecalloc(len, sizeof(char));
140 SNPRINTF3(p, len, "%s %s %s", shell, shell_coption(), esccmd);
155 #if MSDOS_COMPILER==DJGPPC
157 * Make stdin of the child be in cooked mode.
161 * We don't need to catch signals of the child (it
162 * also makes trouble with some DPMI servers).
164 __djgpp_exception_toggle();
166 __djgpp_exception_toggle();
174 * Restore standard input, reset signals, raw mode, etc.
181 #if MSDOS_COMPILER==WIN32C
189 putstr(" (press RETURN)");
197 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
199 * Restore the previous directory (possibly
200 * changed by the child program we just ran).
203 #if MSDOS_COMPILER != DJGPPC
205 * Some versions of chdir() don't change to the drive
206 * which is part of CWD. (DJGPP does this in chdir.)
210 if (cwd[0] >= 'a' && cwd[0] <= 'z')
211 setdisk(cwd[0] - 'a');
212 else if (cwd[0] >= 'A' && cwd[0] <= 'Z')
213 setdisk(cwd[0] - 'A');
219 * Reopen the current input file.
221 reedit_ifile(save_ifile);
223 #if defined(SIGWINCH) || defined(SIGWIND)
225 * Since we were ignoring window change signals while we executed
226 * the system command, we must assume the window changed.
227 * Warning: this leaves a signal pending (in "sigs"),
228 * so psignals() should be called soon after lsystem().
239 * Pipe a section of the input file into the given shell command.
240 * The section to be piped is the section "between" the current
241 * position and the position marked by the given letter.
243 * If the mark is after the current screen, the section between
244 * the top line displayed and the mark is piped.
245 * If the mark is before the current screen, the section between
246 * the mark and the bottom line displayed is piped.
247 * If the mark is on the current screen, or if the mark is ".",
248 * the whole current screen is piped.
251 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.
283 pipe_data(char *cmd, POSITION spos, POSITION epos)
287 extern FILE *popen();
290 * This is structured much like lsystem().
291 * Since we're running a shell program, we must be careful
292 * to perform the necessary deinitialization before running
293 * the command, and reinitialization after it.
295 if (ch_seek(spos) != 0)
297 error("Cannot seek to start position", NULL_PARG);
301 if ((f = popen(cmd, "w")) == NULL)
303 error("Cannot create pipe", NULL_PARG);
315 #if MSDOS_COMPILER==WIN32C
319 LSIGNAL(SIGPIPE, SIG_IGN);
323 while (epos == NULL_POSITION || spos++ <= epos)
326 * Read a character from the file and give it to the pipe.
331 if (putc(c, f) == EOF)
336 * Finish up the last line.
338 while (c != '\n' && c != EOI )
343 if (putc(c, f) == EOF)
350 LSIGNAL(SIGPIPE, SIG_DFL);
352 #if MSDOS_COMPILER==WIN32C
359 #if defined(SIGWINCH) || defined(SIGWIND)
360 /* {{ Probably don't need this here. }} */