/*1* Copyright (C) 1984-2025 Mark Nudelman2*3* You may distribute under the terms of either the GNU General Public4* License or the Less License, as specified in the README file.5*6* For more information, see the README file.7*/8910/*11* Routines to execute other programs.12* Necessarily very OS dependent.13*/1415#include "less.h"16#include <signal.h>17#include "position.h"1819#if MSDOS_COMPILER20#include <dos.h>21#if MSDOS_COMPILER==WIN32C && defined(__MINGW32__)22#include <direct.h>23#define setdisk(n) _chdrive((n)+1)24#else25#ifdef _MSC_VER26#include <direct.h>27#define setdisk(n) _chdrive((n)+1)28#else29#include <dir.h>30#endif31#endif32#endif3334extern int sigs;35extern IFILE curr_ifile;363738#if HAVE_SYSTEM3940/*41* Pass the specified command to a shell to be executed.42* Like plain "system()", but handles resetting terminal modes, etc.43*/44public void lsystem(constant char *cmd, constant char *donemsg)45{46int inp;47#if HAVE_SHELL48constant char *shell;49char *p;50#endif51IFILE save_ifile;52#if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C53char cwd[FILENAME_MAX+1];54#endif5556/*57* Print the command which is to be executed,58* unless the command starts with a "-".59*/60if (cmd[0] == '-')61cmd++;62else63{64clear_bot();65putstr("!");66putstr(cmd);67putstr("\n");68}6970#if MSDOS_COMPILER71#if MSDOS_COMPILER==WIN32C72if (*cmd == '\0')73cmd = getenv("COMSPEC");74#else75/*76* Working directory is global on MSDOS.77* The child might change the working directory, so we78* must save and restore CWD across calls to "system",79* or else we won't find our file when we return and80* try to "reedit_ifile" it.81*/82getcwd(cwd, FILENAME_MAX);83#endif84#endif8586/*87* Close the current input file.88*/89save_ifile = save_curr_ifile();90(void) edit_ifile(NULL_IFILE);9192/*93* De-initialize the terminal and take out of raw mode.94*/95term_deinit();96flush(); /* Make sure the deinit chars get out */97raw_mode(0);98#if MSDOS_COMPILER==WIN32C99close_getchr();100#endif101102/*103* Restore signals to their defaults.104*/105init_signals(0);106107#if HAVE_DUP108/*109* Force standard input to be the user's terminal110* (the normal standard input), even if less's standard input111* is coming from a pipe.112*/113inp = dup(0);114close(0);115#if !MSDOS_COMPILER116if (open_tty() < 0)117#endif118dup(inp);119#endif120121/*122* Pass the command to the system to be executed.123* If we have a SHELL environment variable, use124* <$SHELL -c "command"> instead of just <command>.125* If the command is empty, just invoke a shell.126*/127#if HAVE_SHELL128p = NULL;129if ((shell = lgetenv("SHELL")) != NULL && *shell != '\0')130{131if (*cmd == '\0')132p = save(shell);133else134{135char *esccmd = shell_quote(cmd);136if (esccmd != NULL)137{138constant char *copt = shell_coption();139size_t len = strlen(shell) + strlen(esccmd) + strlen(copt) + 3;140p = (char *) ecalloc(len, sizeof(char));141SNPRINTF3(p, len, "%s %s %s", shell, copt, esccmd);142free(esccmd);143}144}145}146if (p == NULL)147{148if (*cmd == '\0')149p = save("sh");150else151p = save(cmd);152}153system(p);154free(p);155#else156#if MSDOS_COMPILER==DJGPPC157/*158* Make stdin of the child be in cooked mode.159*/160setmode(0, O_TEXT);161/*162* We don't need to catch signals of the child (it163* also makes trouble with some DPMI servers).164*/165__djgpp_exception_toggle();166system(cmd);167__djgpp_exception_toggle();168#else169system(cmd);170#endif171#endif172173#if HAVE_DUP174/*175* Restore standard input, reset signals, raw mode, etc.176*/177close(0);178dup(inp);179close(inp);180#endif181182#if MSDOS_COMPILER==WIN32C183open_getchr();184#endif185init_signals(1);186raw_mode(1);187if (donemsg != NULL)188{189putstr(donemsg);190putstr(" (press RETURN)");191get_return();192putchr('\n');193flush();194}195term_init();196screen_trashed();197198#if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C199/*200* Restore the previous directory (possibly201* changed by the child program we just ran).202*/203chdir(cwd);204#if MSDOS_COMPILER != DJGPPC205/*206* Some versions of chdir() don't change to the drive207* which is part of CWD. (DJGPP does this in chdir.)208*/209if (cwd[1] == ':')210{211if (cwd[0] >= 'a' && cwd[0] <= 'z')212setdisk(cwd[0] - 'a');213else if (cwd[0] >= 'A' && cwd[0] <= 'Z')214setdisk(cwd[0] - 'A');215}216#endif217#endif218219/*220* Reopen the current input file.221*/222reedit_ifile(save_ifile);223224/*225* Since we were ignoring window change signals while we executed226* 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().229*/230sigs |= S_WINCH;231}232233#endif234235#if PIPEC236237/*238* Pipe a section of the input file into the given shell command.239* The section to be piped is the section "between" the current240* position and the position marked by the given letter.241*242* If the mark is after the current screen, the section between243* the top line displayed and the mark is piped.244* If the mark is before the current screen, the section between245* the mark and the bottom line displayed is piped.246* If the mark is on the current screen, or if the mark is ".",247* the whole current screen is piped.248*/249public int pipe_mark(char c, constant char *cmd)250{251POSITION mpos, tpos, bpos;252253/*254* mpos = the marked position.255* tpos = top of screen.256* bpos = bottom of screen.257*/258mpos = markpos(c);259if (mpos == NULL_POSITION)260return (-1);261tpos = position(TOP);262if (tpos == NULL_POSITION)263tpos = ch_zero();264bpos = position(BOTTOM);265266if (c == '.')267return (pipe_data(cmd, tpos, bpos));268else if (mpos <= tpos)269return (pipe_data(cmd, mpos, bpos));270else if (bpos == NULL_POSITION)271return (pipe_data(cmd, tpos, bpos));272else273return (pipe_data(cmd, tpos, mpos));274}275276/*277* Create a pipe to the given shell command.278* Feed it the file contents between the positions spos and epos.279*/280public int pipe_data(constant char *cmd, POSITION spos, POSITION epos)281{282FILE *f;283int c;284285/*286* This is structured much like lsystem().287* Since we're running a shell program, we must be careful288* to perform the necessary deinitialization before running289* the command, and reinitialization after it.290*/291if (ch_seek(spos) != 0)292{293error("Cannot seek to start position", NULL_PARG);294return (-1);295}296297if ((f = popen(cmd, "w")) == NULL)298{299error("Cannot create pipe", NULL_PARG);300return (-1);301}302clear_bot();303putstr("!");304putstr(cmd);305putstr("\n");306307term_deinit();308flush();309raw_mode(0);310init_signals(0);311#if MSDOS_COMPILER==WIN32C312close_getchr();313#endif314#ifdef SIGPIPE315LSIGNAL(SIGPIPE, SIG_IGN);316#endif317318c = EOI;319while (epos == NULL_POSITION || spos++ <= epos)320{321/*322* Read a character from the file and give it to the pipe.323*/324c = ch_forw_get();325if (c == EOI)326break;327if (putc(c, f) == EOF)328break;329}330331/*332* Finish up the last line.333*/334while (c != '\n' && c != EOI )335{336c = ch_forw_get();337if (c == EOI)338break;339if (putc(c, f) == EOF)340break;341}342343pclose(f);344345#ifdef SIGPIPE346LSIGNAL(SIGPIPE, SIG_DFL);347#endif348#if MSDOS_COMPILER==WIN32C349open_getchr();350#endif351init_signals(1);352raw_mode(1);353term_init();354screen_trashed();355#if defined(SIGWINCH) || defined(SIGWIND)356/* {{ Probably don't need this here. }} */357lwinch(0);358#endif359return (0);360}361362#endif363364365