]>
diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/miscellany/less-177/lsystem.c
2 * Routines to execute other programs.
3 * Necessarily very OS dependent.
25 void swchar_to_unix();
28 extern char *getenv();
30 extern int screen_trashed
;
31 extern IFILE curr_ifile
;
35 * Pass the specified command to a shell to be executed.
36 * Like plain "system()", but handles resetting terminal modes, etc.
45 register char *curr_filename
;
48 * Print the command which is to be executed,
49 * unless the command starts with a "-".
63 * Close the current input file.
65 curr_filename
= get_filename(curr_ifile
);
69 * De-initialize the terminal and take out of raw mode.
72 flush(); /* Make sure the deinit chars get out */
76 * Restore signals to their defaults.
81 * Force standard input to be the user's terminal
82 * (the normal standard input), even if less's standard input
83 * is coming from a pipe.
90 inp2
= open("CON", O_TEXT
|O_RDONLY
);
96 if (open("/dev/tty", 0) < 0)
101 * Pass the command to the system to be executed.
102 * If we have a SHELL environment variable, use
103 * <$SHELL -c "command"> instead of just <command>.
104 * If the command is empty, just invoke a shell.
111 sw_char
= get_swchar();
113 result
= system(cmd
);
121 if ((shell
= getenv("SHELL")) != NULL
&& *shell
!= '\0')
127 p
= (char *) ecalloc(strlen(shell
) + strlen(cmd
) + 7,
129 sprintf(p
, "%s -c \"%s\"", shell
, cmd
);
145 * Restore standard input, reset signals, raw mode, etc.
163 * Reopen the current input file.
165 (void) edit(curr_filename
, 0);
167 #if defined(SIGWINCH) || defined(SIGWIND)
169 * Since we were ignoring window change signals while we executed
170 * the system command, we must assume the window changed.
171 * Warning: this leaves a signal pending (in "sigs"),
172 * so psignals() should be called soon after lsystem().
181 * Pipe a section of the input file into the given shell command.
182 * The section to be piped is the section "between" the current
183 * position and the position marked by the given letter.
185 * The "current" position means the top line displayed if the mark
186 * is after the current screen, or the bottom line displayed if
187 * the mark is before the current screen.
188 * If the mark is on the current screen, the whole screen is displayed.
195 POSITION mpos
, tpos
, bpos
;
198 * mpos = the marked position.
199 * tpos = top of screen.
200 * bpos = bottom of screen.
203 if (mpos
== NULL_POSITION
)
205 tpos
= position(TOP
);
206 if (tpos
== NULL_POSITION
)
208 bpos
= position(BOTTOM
);
211 return (pipe_data(cmd
, tpos
, bpos
));
212 else if (mpos
<= tpos
)
213 return (pipe_data(cmd
, mpos
, tpos
));
214 else if (bpos
== NULL_POSITION
)
215 return (pipe_data(cmd
, tpos
, bpos
));
217 return (pipe_data(cmd
, tpos
, mpos
));
221 * Create a pipe to the given shell command.
222 * Feed it the file contents between the positions spos and epos.
225 pipe_data(cmd
, spos
, epos
)
232 extern FILE *popen();
235 * This is structured much like lsystem().
236 * Since we're running a shell program, we must be careful
237 * to perform the necessary deinitialization before running
238 * the command, and reinitialization after it.
240 if (ch_seek(spos
) != 0)
242 error("Cannot seek to start position", NULL_PARG
);
246 if ((f
= popen(cmd
, "w")) == NULL
)
248 error("Cannot create pipe", NULL_PARG
);
262 SIGNAL(SIGPIPE
, SIG_IGN
);
265 while (epos
== NULL_POSITION
|| spos
++ <= epos
)
268 * Read a character from the file and give it to the pipe.
273 if (putc(c
, f
) == EOF
)
278 * Finish up the last line.
280 while (c
!= '\n' && c
!= EOI
)
285 if (putc(c
, f
) == EOF
)
292 SIGNAL(SIGPIPE
, SIG_DFL
);
298 #if defined(SIGWINCH) || defined(SIGWIND)
299 /* {{ Probably don't need this here. }} */