1*838f5788Ssimonb /* $NetBSD: lsystem.c,v 1.5 2023/10/06 05:49:49 simonb Exp $ */
220006a0bStron
320006a0bStron /*
4*838f5788Ssimonb * Copyright (C) 1984-2023 Mark Nudelman
520006a0bStron *
620006a0bStron * You may distribute under the terms of either the GNU General Public
720006a0bStron * License or the Less License, as specified in the README file.
820006a0bStron *
9ec18bca0Stron * For more information, see the README file.
1020006a0bStron */
1120006a0bStron
1220006a0bStron
1320006a0bStron /*
1420006a0bStron * Routines to execute other programs.
1520006a0bStron * Necessarily very OS dependent.
1620006a0bStron */
1720006a0bStron
1820006a0bStron #include "less.h"
1920006a0bStron #include <signal.h>
2020006a0bStron #include "position.h"
2120006a0bStron
2220006a0bStron #if MSDOS_COMPILER
2320006a0bStron #include <dos.h>
24*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C && defined(MINGW)
25*838f5788Ssimonb #include <direct.h>
26*838f5788Ssimonb #define setdisk(n) _chdrive((n)+1)
27*838f5788Ssimonb #else
2820006a0bStron #ifdef _MSC_VER
2920006a0bStron #include <direct.h>
3020006a0bStron #define setdisk(n) _chdrive((n)+1)
3120006a0bStron #else
3220006a0bStron #include <dir.h>
3320006a0bStron #endif
3420006a0bStron #endif
35*838f5788Ssimonb #endif
3620006a0bStron
3720006a0bStron extern int screen_trashed;
3820006a0bStron extern IFILE curr_ifile;
3920006a0bStron
4020006a0bStron
4120006a0bStron #if HAVE_SYSTEM
4220006a0bStron
4320006a0bStron /*
4420006a0bStron * Pass the specified command to a shell to be executed.
4520006a0bStron * Like plain "system()", but handles resetting terminal modes, etc.
4620006a0bStron */
lsystem(char * cmd,char * donemsg)47*838f5788Ssimonb public void lsystem(char *cmd, char *donemsg)
4820006a0bStron {
49*838f5788Ssimonb int inp;
5020006a0bStron #if HAVE_SHELL
51*838f5788Ssimonb char *shell;
52*838f5788Ssimonb char *p;
5320006a0bStron #endif
5420006a0bStron IFILE save_ifile;
5520006a0bStron #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
5620006a0bStron char cwd[FILENAME_MAX+1];
5720006a0bStron #endif
5820006a0bStron
5920006a0bStron /*
6020006a0bStron * Print the command which is to be executed,
6120006a0bStron * unless the command starts with a "-".
6220006a0bStron */
6320006a0bStron if (cmd[0] == '-')
6420006a0bStron cmd++;
6520006a0bStron else
6620006a0bStron {
6720006a0bStron clear_bot();
6820006a0bStron putstr("!");
6920006a0bStron putstr(cmd);
7020006a0bStron putstr("\n");
7120006a0bStron }
7220006a0bStron
7320006a0bStron #if MSDOS_COMPILER
7420006a0bStron #if MSDOS_COMPILER==WIN32C
7520006a0bStron if (*cmd == '\0')
7620006a0bStron cmd = getenv("COMSPEC");
7720006a0bStron #else
7820006a0bStron /*
7920006a0bStron * Working directory is global on MSDOS.
8020006a0bStron * The child might change the working directory, so we
8120006a0bStron * must save and restore CWD across calls to "system",
8220006a0bStron * or else we won't find our file when we return and
8320006a0bStron * try to "reedit_ifile" it.
8420006a0bStron */
8520006a0bStron getcwd(cwd, FILENAME_MAX);
8620006a0bStron #endif
8720006a0bStron #endif
8820006a0bStron
8920006a0bStron /*
9020006a0bStron * Close the current input file.
9120006a0bStron */
9220006a0bStron save_ifile = save_curr_ifile();
9320006a0bStron (void) edit_ifile(NULL_IFILE);
9420006a0bStron
9520006a0bStron /*
9620006a0bStron * De-initialize the terminal and take out of raw mode.
9720006a0bStron */
9820006a0bStron deinit();
9920006a0bStron flush(); /* Make sure the deinit chars get out */
10020006a0bStron raw_mode(0);
10120006a0bStron #if MSDOS_COMPILER==WIN32C
10220006a0bStron close_getchr();
10320006a0bStron #endif
10420006a0bStron
10520006a0bStron /*
10620006a0bStron * Restore signals to their defaults.
10720006a0bStron */
10820006a0bStron init_signals(0);
10920006a0bStron
11020006a0bStron #if HAVE_DUP
11120006a0bStron /*
11220006a0bStron * Force standard input to be the user's terminal
11320006a0bStron * (the normal standard input), even if less's standard input
11420006a0bStron * is coming from a pipe.
11520006a0bStron */
11620006a0bStron inp = dup(0);
11720006a0bStron close(0);
118*838f5788Ssimonb #if !MSDOS_COMPILER
119*838f5788Ssimonb if (open_tty() < 0)
12020006a0bStron #endif
12120006a0bStron dup(inp);
12220006a0bStron #endif
12320006a0bStron
12420006a0bStron /*
12520006a0bStron * Pass the command to the system to be executed.
12620006a0bStron * If we have a SHELL environment variable, use
12720006a0bStron * <$SHELL -c "command"> instead of just <command>.
12820006a0bStron * If the command is empty, just invoke a shell.
12920006a0bStron */
13020006a0bStron #if HAVE_SHELL
13120006a0bStron p = NULL;
13220006a0bStron if ((shell = lgetenv("SHELL")) != NULL && *shell != '\0')
13320006a0bStron {
13420006a0bStron if (*cmd == '\0')
13520006a0bStron p = save(shell);
13620006a0bStron else
13720006a0bStron {
13820006a0bStron char *esccmd = shell_quote(cmd);
13920006a0bStron if (esccmd != NULL)
14020006a0bStron {
141*838f5788Ssimonb int len = (int) (strlen(shell) + strlen(esccmd) + 5);
14220006a0bStron p = (char *) ecalloc(len, sizeof(char));
14320006a0bStron SNPRINTF3(p, len, "%s %s %s", shell, shell_coption(), esccmd);
14420006a0bStron free(esccmd);
14520006a0bStron }
14620006a0bStron }
14720006a0bStron }
14820006a0bStron if (p == NULL)
14920006a0bStron {
15020006a0bStron if (*cmd == '\0')
15120006a0bStron p = save("sh");
15220006a0bStron else
15320006a0bStron p = save(cmd);
15420006a0bStron }
15520006a0bStron system(p);
15620006a0bStron free(p);
15720006a0bStron #else
15820006a0bStron #if MSDOS_COMPILER==DJGPPC
15920006a0bStron /*
16020006a0bStron * Make stdin of the child be in cooked mode.
16120006a0bStron */
16220006a0bStron setmode(0, O_TEXT);
16320006a0bStron /*
16420006a0bStron * We don't need to catch signals of the child (it
16520006a0bStron * also makes trouble with some DPMI servers).
16620006a0bStron */
16720006a0bStron __djgpp_exception_toggle();
16820006a0bStron system(cmd);
16920006a0bStron __djgpp_exception_toggle();
17020006a0bStron #else
17120006a0bStron system(cmd);
17220006a0bStron #endif
17320006a0bStron #endif
17420006a0bStron
17520006a0bStron #if HAVE_DUP
17620006a0bStron /*
17720006a0bStron * Restore standard input, reset signals, raw mode, etc.
17820006a0bStron */
17920006a0bStron close(0);
18020006a0bStron dup(inp);
18120006a0bStron close(inp);
18220006a0bStron #endif
18320006a0bStron
18420006a0bStron #if MSDOS_COMPILER==WIN32C
18520006a0bStron open_getchr();
18620006a0bStron #endif
18720006a0bStron init_signals(1);
18820006a0bStron raw_mode(1);
18920006a0bStron if (donemsg != NULL)
19020006a0bStron {
19120006a0bStron putstr(donemsg);
19220006a0bStron putstr(" (press RETURN)");
19320006a0bStron get_return();
19420006a0bStron putchr('\n');
19520006a0bStron flush();
19620006a0bStron }
19720006a0bStron init();
19820006a0bStron screen_trashed = 1;
19920006a0bStron
20020006a0bStron #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
20120006a0bStron /*
20220006a0bStron * Restore the previous directory (possibly
20320006a0bStron * changed by the child program we just ran).
20420006a0bStron */
20520006a0bStron chdir(cwd);
20620006a0bStron #if MSDOS_COMPILER != DJGPPC
20720006a0bStron /*
20820006a0bStron * Some versions of chdir() don't change to the drive
20920006a0bStron * which is part of CWD. (DJGPP does this in chdir.)
21020006a0bStron */
21120006a0bStron if (cwd[1] == ':')
21220006a0bStron {
21320006a0bStron if (cwd[0] >= 'a' && cwd[0] <= 'z')
21420006a0bStron setdisk(cwd[0] - 'a');
21520006a0bStron else if (cwd[0] >= 'A' && cwd[0] <= 'Z')
21620006a0bStron setdisk(cwd[0] - 'A');
21720006a0bStron }
21820006a0bStron #endif
21920006a0bStron #endif
22020006a0bStron
22120006a0bStron /*
22220006a0bStron * Reopen the current input file.
22320006a0bStron */
22420006a0bStron reedit_ifile(save_ifile);
22520006a0bStron
22620006a0bStron #if defined(SIGWINCH) || defined(SIGWIND)
22720006a0bStron /*
22820006a0bStron * Since we were ignoring window change signals while we executed
22920006a0bStron * the system command, we must assume the window changed.
23020006a0bStron * Warning: this leaves a signal pending (in "sigs"),
23120006a0bStron * so psignals() should be called soon after lsystem().
23220006a0bStron */
23320006a0bStron winch(0);
23420006a0bStron #endif
23520006a0bStron }
23620006a0bStron
23720006a0bStron #endif
23820006a0bStron
23920006a0bStron #if PIPEC
24020006a0bStron
24120006a0bStron /*
24220006a0bStron * Pipe a section of the input file into the given shell command.
24320006a0bStron * The section to be piped is the section "between" the current
24420006a0bStron * position and the position marked by the given letter.
24520006a0bStron *
24620006a0bStron * If the mark is after the current screen, the section between
24720006a0bStron * the top line displayed and the mark is piped.
24820006a0bStron * If the mark is before the current screen, the section between
24920006a0bStron * the mark and the bottom line displayed is piped.
25020006a0bStron * If the mark is on the current screen, or if the mark is ".",
25120006a0bStron * the whole current screen is piped.
25220006a0bStron */
pipe_mark(int c,char * cmd)253*838f5788Ssimonb public int pipe_mark(int c, char *cmd)
25420006a0bStron {
25520006a0bStron POSITION mpos, tpos, bpos;
25620006a0bStron
25720006a0bStron /*
25820006a0bStron * mpos = the marked position.
25920006a0bStron * tpos = top of screen.
26020006a0bStron * bpos = bottom of screen.
26120006a0bStron */
26220006a0bStron mpos = markpos(c);
26320006a0bStron if (mpos == NULL_POSITION)
26420006a0bStron return (-1);
26520006a0bStron tpos = position(TOP);
26620006a0bStron if (tpos == NULL_POSITION)
26720006a0bStron tpos = ch_zero();
26820006a0bStron bpos = position(BOTTOM);
26920006a0bStron
27020006a0bStron if (c == '.')
27120006a0bStron return (pipe_data(cmd, tpos, bpos));
27220006a0bStron else if (mpos <= tpos)
27320006a0bStron return (pipe_data(cmd, mpos, bpos));
27420006a0bStron else if (bpos == NULL_POSITION)
27520006a0bStron return (pipe_data(cmd, tpos, bpos));
27620006a0bStron else
27720006a0bStron return (pipe_data(cmd, tpos, mpos));
27820006a0bStron }
27920006a0bStron
28020006a0bStron /*
28120006a0bStron * Create a pipe to the given shell command.
28220006a0bStron * Feed it the file contents between the positions spos and epos.
28320006a0bStron */
pipe_data(char * cmd,POSITION spos,POSITION epos)284*838f5788Ssimonb public int pipe_data(char *cmd, POSITION spos, POSITION epos)
28520006a0bStron {
286*838f5788Ssimonb FILE *f;
287*838f5788Ssimonb int c;
28820006a0bStron
28920006a0bStron /*
29020006a0bStron * This is structured much like lsystem().
29120006a0bStron * Since we're running a shell program, we must be careful
29220006a0bStron * to perform the necessary deinitialization before running
29320006a0bStron * the command, and reinitialization after it.
29420006a0bStron */
29520006a0bStron if (ch_seek(spos) != 0)
29620006a0bStron {
29720006a0bStron error("Cannot seek to start position", NULL_PARG);
29820006a0bStron return (-1);
29920006a0bStron }
30020006a0bStron
30120006a0bStron if ((f = popen(cmd, "w")) == NULL)
30220006a0bStron {
30320006a0bStron error("Cannot create pipe", NULL_PARG);
30420006a0bStron return (-1);
30520006a0bStron }
30620006a0bStron clear_bot();
30720006a0bStron putstr("!");
30820006a0bStron putstr(cmd);
30920006a0bStron putstr("\n");
31020006a0bStron
31120006a0bStron deinit();
31220006a0bStron flush();
31320006a0bStron raw_mode(0);
31420006a0bStron init_signals(0);
31520006a0bStron #if MSDOS_COMPILER==WIN32C
31620006a0bStron close_getchr();
31720006a0bStron #endif
31820006a0bStron #ifdef SIGPIPE
31920006a0bStron LSIGNAL(SIGPIPE, SIG_IGN);
32020006a0bStron #endif
32120006a0bStron
32220006a0bStron c = EOI;
32320006a0bStron while (epos == NULL_POSITION || spos++ <= epos)
32420006a0bStron {
32520006a0bStron /*
32620006a0bStron * Read a character from the file and give it to the pipe.
32720006a0bStron */
32820006a0bStron c = ch_forw_get();
32920006a0bStron if (c == EOI)
33020006a0bStron break;
33120006a0bStron if (putc(c, f) == EOF)
33220006a0bStron break;
33320006a0bStron }
33420006a0bStron
33520006a0bStron /*
33620006a0bStron * Finish up the last line.
33720006a0bStron */
33820006a0bStron while (c != '\n' && c != EOI )
33920006a0bStron {
34020006a0bStron c = ch_forw_get();
34120006a0bStron if (c == EOI)
34220006a0bStron break;
34320006a0bStron if (putc(c, f) == EOF)
34420006a0bStron break;
34520006a0bStron }
34620006a0bStron
34720006a0bStron pclose(f);
34820006a0bStron
34920006a0bStron #ifdef SIGPIPE
35020006a0bStron LSIGNAL(SIGPIPE, SIG_DFL);
35120006a0bStron #endif
35220006a0bStron #if MSDOS_COMPILER==WIN32C
35320006a0bStron open_getchr();
35420006a0bStron #endif
35520006a0bStron init_signals(1);
35620006a0bStron raw_mode(1);
35720006a0bStron init();
35820006a0bStron screen_trashed = 1;
35920006a0bStron #if defined(SIGWINCH) || defined(SIGWIND)
36020006a0bStron /* {{ Probably don't need this here. }} */
36120006a0bStron winch(0);
36220006a0bStron #endif
36320006a0bStron return (0);
36420006a0bStron }
36520006a0bStron
36620006a0bStron #endif
367