2 * Copyright (C) 1984-2000 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.
53 char cwd[FILENAME_MAX+1];
57 * Print the command which is to be executed,
58 * unless the command starts with a "-".
72 * Working directory is global on MSDOS.
73 * The child might change the working directory, so we
74 * must save and restore CWD across calls to "system",
75 * or else we won't find our file when we return and
76 * try to "reedit_ifile" it.
78 getcwd(cwd, FILENAME_MAX);
82 * Close the current input file.
84 save_ifile = save_curr_ifile();
85 (void) edit_ifile(NULL_IFILE);
88 * De-initialize the terminal and take out of raw mode.
91 flush(); /* Make sure the deinit chars get out */
93 #if MSDOS_COMPILER==WIN32C
98 * Restore signals to their defaults.
104 * Force standard input to be the user's terminal
105 * (the normal standard input), even if less's standard input
106 * is coming from a pipe.
111 /* The __open() system call translates "/dev/tty" to "con". */
112 if (__open("/dev/tty", OPEN_READ) < 0)
114 if (open("/dev/tty", OPEN_READ) < 0)
120 * Pass the command to the system to be executed.
121 * If we have a SHELL environment variable, use
122 * <$SHELL -c "command"> instead of just <command>.
123 * If the command is empty, just invoke a shell.
127 if ((shell = lgetenv("SHELL")) != NULL && *shell != '\0')
134 if ((esccmd = esc_metachars(cmd)) == NULL)
136 p = (char *) ecalloc(strlen(shell) +
137 strlen(cmd) + 7, sizeof(char));
138 sprintf(p, "%s -c \"%s\"", shell, cmd);
141 p = (char *) ecalloc(strlen(shell) +
142 strlen(esccmd) + 5, sizeof(char));
143 sprintf(p, "%s -c %s", shell, 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)");
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)
296 extern FILE *popen();
299 * This is structured much like lsystem().
300 * Since we're running a shell program, we must be careful
301 * to perform the necessary deinitialization before running
302 * the command, and reinitialization after it.
304 if (ch_seek(spos) != 0)
306 error("Cannot seek to start position", NULL_PARG);
310 if ((f = popen(cmd, "w")) == NULL)
312 error("Cannot create pipe", NULL_PARG);
324 #if MSDOS_COMPILER==WIN32C
328 LSIGNAL(SIGPIPE, SIG_IGN);
332 while (epos == NULL_POSITION || spos++ <= epos)
335 * Read a character from the file and give it to the pipe.
340 if (putc(c, f) == EOF)
345 * Finish up the last line.
347 while (c != '\n' && c != EOI )
352 if (putc(c, f) == EOF)
359 LSIGNAL(SIGPIPE, SIG_DFL);
361 #if MSDOS_COMPILER==WIN32C
368 #if defined(SIGWINCH) || defined(SIGWIND)
369 /* {{ Probably don't need this here. }} */
379 * Popen, and Pclose, for OS-9.
381 * Based on code copyright (c) 1988 by Wolfgang Ocker, Puchheim,
382 * Ulli Dessauer, Germering and
383 * Reimer Mellin, Muenchen
386 * These functions can be copied and distributed freely for any
387 * non-commercial purposes. It can only be incorporated into
388 * commercial software with the written permission of the authors.
390 * TOP-specific code stripped out and adapted for less by M.Gregorie, 1996
392 * address: Wolfgang Ocker
393 * Lochhauserstrasse 35a
397 * e-mail: weo@altger.UUCP, ud@altger.UUCP, ram@altger.UUCP
398 * pyramid!tmpmbx!recco!weo
399 * pyramid!tmpmbx!nitmar!ud
400 * pyramid!tmpmbx!ramsys!ram
408 * gregorie@logica.com
412 extern char **environ;
413 extern char *getenv();
414 extern int os9forkc();
415 static int pids[_NFILE] = { 0, 0, 0, 0, 0, 0, 0, 0,
416 0, 0, 0, 0, 0, 0, 0, 0,
417 0, 0, 0, 0, 0, 0, 0, 0,
418 0, 0, 0, 0, 0, 0, 0, 0 };
422 FILE *popen(name, mode)
426 int fd, fd2, fdsav, pid;
427 static char *argv[] = {NULL, NULL, NULL };
428 static char cmd[200];
429 static char cmd_path[200];
433 if ((shell = getenv("SHELL")) == NULL)
438 strcpy(cmd_path, cp);
439 if (cp = index(cmd_path, ' '))
442 strcat(cmd, cmd_path);
451 mode is "r" (stdout) or "w" (stdin)
459 default: return(NULL);
466 creat("/pipe", S_IWRITE+S_IREAD);
467 pid = os9exec(os9forkc, argv[0], argv, environ, 0, 0, 3);
475 r = fdopen(fd2, mode);
500 while ((pid = wait(&status)) != -1)
504 for (i = 0; i < _NFILE; i++)