Fix our use of $em_tab_dist after it was changed to 0 for 'turned of'.
[nedit.git] / source / shell.c
blobcdb21ec6300123188e246a68b1429d69e4292f8e
1 static const char CVSID[] = "$Id: shell.c,v 1.44 2008/01/04 22:11:04 yooden Exp $";
2 /*******************************************************************************
3 * *
4 * shell.c -- Nirvana Editor shell command execution *
5 * *
6 * Copyright (C) 1999 Mark Edel *
7 * *
8 * This is free software; you can redistribute it and/or modify it under the *
9 * terms of the GNU General Public License as published by the Free Software *
10 * Foundation; either version 2 of the License, or (at your option) any later *
11 * version. In addition, you may distribute version of this program linked to *
12 * Motif or Open Motif. See README for details. *
13 * *
14 * This software is distributed in the hope that it will be useful, but WITHOUT *
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License along with *
20 * software; if not, write to the Free Software Foundation, Inc., 59 Temple *
21 * Place, Suite 330, Boston, MA 02111-1307 USA *
22 * *
23 * Nirvana Text Editor *
24 * December, 1993 *
25 * *
26 * Written by Mark Edel *
27 * *
28 *******************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "../config.h"
32 #endif
34 #include "shell.h"
35 #include "textBuf.h"
36 #include "text.h"
37 #include "nedit.h"
38 #include "window.h"
39 #include "preferences.h"
40 #include "file.h"
41 #include "macro.h"
42 #include "interpret.h"
43 #include "../util/DialogF.h"
44 #include "../util/misc.h"
45 #include "menu.h"
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <signal.h>
51 #include <sys/types.h>
52 #ifndef __MVS__
53 #ifndef VMS
54 #include <sys/param.h>
55 #endif
56 #endif
57 #include <sys/wait.h>
58 #include <unistd.h>
59 #include <fcntl.h>
60 #include <ctype.h>
61 #include <errno.h>
62 #ifdef notdef
63 #ifdef IBM
64 #define NBBY 8
65 #include <sys/select.h>
66 #endif
67 #include <time.h>
68 #endif
69 #ifdef __EMX__
70 #include <process.h>
71 #endif
73 #include <Xm/Xm.h>
74 #include <Xm/MessageB.h>
75 #include <Xm/Text.h>
76 #include <Xm/Form.h>
77 #include <Xm/PushBG.h>
79 #ifdef HAVE_DEBUG_H
80 #include "../debug.h"
81 #endif
84 /* Tuning parameters */
85 #define IO_BUF_SIZE 4096 /* size of buffers for collecting cmd output */
86 #define MAX_OUT_DIALOG_ROWS 30 /* max height of dialog for command output */
87 #define MAX_OUT_DIALOG_COLS 80 /* max width of dialog for command output */
88 #define OUTPUT_FLUSH_FREQ 1000 /* how often (msec) to flush output buffers
89 when process is taking too long */
90 #define BANNER_WAIT_TIME 6000 /* how long to wait (msec) before putting up
91 Shell Command Executing... banner */
93 /* flags for issueCommand */
94 #define ACCUMULATE 1
95 #define ERROR_DIALOGS 2
96 #define REPLACE_SELECTION 4
97 #define RELOAD_FILE_AFTER 8
98 #define OUTPUT_TO_DIALOG 16
99 #define OUTPUT_TO_STRING 32
101 /* element of a buffer list for collecting output from shell processes */
102 typedef struct bufElem {
103 struct bufElem *next;
104 int length;
105 char contents[IO_BUF_SIZE];
106 } buffer;
108 /* data attached to window during shell command execution with
109 information for controling and communicating with the process */
110 typedef struct {
111 int flags;
112 int stdinFD, stdoutFD, stderrFD;
113 pid_t childPid;
114 XtInputId stdinInputID, stdoutInputID, stderrInputID;
115 buffer *outBufs, *errBufs;
116 char *input;
117 char *inPtr;
118 Widget textW;
119 int leftPos, rightPos;
120 int inLength;
121 XtIntervalId bannerTimeoutID, flushTimeoutID;
122 char bannerIsUp;
123 char fromMacro;
124 } shellCmdInfo;
126 static void issueCommand(WindowInfo *window, const char *command, char *input,
127 int inputLen, int flags, Widget textW, int replaceLeft,
128 int replaceRight, int fromMacro);
129 static void stdoutReadProc(XtPointer clientData, int *source, XtInputId *id);
130 static void stderrReadProc(XtPointer clientData, int *source, XtInputId *id);
131 static void stdinWriteProc(XtPointer clientData, int *source, XtInputId *id);
132 static void finishCmdExecution(WindowInfo *window, int terminatedOnError);
133 static pid_t forkCommand(Widget parent, const char *command, const char *cmdDir,
134 int *stdinFD, int *stdoutFD, int *stderrFD);
135 static void addOutput(buffer **bufList, buffer *buf);
136 static char *coalesceOutput(buffer **bufList, int *length);
137 static void freeBufList(buffer **bufList);
138 static void removeTrailingNewlines(char *string);
139 static void createOutputDialog(Widget parent, char *text);
140 static void destroyOutDialogCB(Widget w, XtPointer callback, XtPointer closure);
141 static void measureText(char *text, int wrapWidth, int *rows, int *cols,
142 int *wrapped);
143 static void truncateString(char *string, int length);
144 static void bannerTimeoutProc(XtPointer clientData, XtIntervalId *id);
145 static void flushTimeoutProc(XtPointer clientData, XtIntervalId *id);
146 static void safeBufReplace(textBuffer *buf, int *start, int *end,
147 const char *text);
148 static char *shellCommandSubstitutes(const char *inStr, const char *fileStr,
149 const char *lineStr);
150 static int shellSubstituter(char *outStr, const char *inStr, const char *fileStr,
151 const char *lineStr, int outLen, int predictOnly);
154 ** Filter the current selection through shell command "command". The selection
155 ** is removed, and replaced by the output from the command execution. Failed
156 ** command status and output to stderr are presented in dialog form.
158 void FilterSelection(WindowInfo *window, const char *command, int fromMacro)
160 int left, right, textLen;
161 char *text;
163 /* Can't do two shell commands at once in the same window */
164 if (window->shellCmdData != NULL) {
165 XBell(TheDisplay, 0);
166 return;
169 /* Get the selection and the range in character positions that it
170 occupies. Beep and return if no selection */
171 text = BufGetSelectionText(window->buffer);
172 if (*text == '\0') {
173 XtFree(text);
174 XBell(TheDisplay, 0);
175 return;
177 textLen = strlen(text);
178 BufUnsubstituteNullChars(text, window->buffer);
179 left = window->buffer->primary.start;
180 right = window->buffer->primary.end;
182 /* Issue the command and collect its output */
183 issueCommand(window, command, text, textLen, ACCUMULATE | ERROR_DIALOGS |
184 REPLACE_SELECTION, window->lastFocus, left, right, fromMacro);
188 ** Execute shell command "command", depositing the result at the current
189 ** insert position or in the current selection if the window has a
190 ** selection.
192 void ExecShellCommand(WindowInfo *window, const char *command, int fromMacro)
194 int left, right, flags = 0;
195 char *subsCommand, fullName[MAXPATHLEN];
196 int pos, line, column;
197 char lineNumber[11];
199 /* Can't do two shell commands at once in the same window */
200 if (window->shellCmdData != NULL) {
201 XBell(TheDisplay, 0);
202 return;
205 /* get the selection or the insert position */
206 pos = TextGetCursorPos(window->lastFocus);
207 if (GetSimpleSelection(window->buffer, &left, &right))
208 flags = ACCUMULATE | REPLACE_SELECTION;
209 else
210 left = right = pos;
212 /* Substitute the current file name for % and the current line number
213 for # in the shell command */
214 strcpy(fullName, window->path);
215 strcat(fullName, window->filename);
216 TextPosToLineAndCol(window->lastFocus, pos, &line, &column);
217 sprintf(lineNumber, "%d", line);
219 subsCommand = shellCommandSubstitutes(command, fullName, lineNumber);
220 if (subsCommand == NULL)
222 DialogF(DF_ERR, window->shell, 1, "Shell Command",
223 "Shell command is too long due to\n"
224 "filename substitutions with '%%' or\n"
225 "line number substitutions with '#'", "OK");
226 return;
229 /* issue the command */
230 issueCommand(window, subsCommand, NULL, 0, flags, window->lastFocus, left,
231 right, fromMacro);
232 free(subsCommand);
236 ** Execute shell command "command", on input string "input", depositing the
237 ** in a macro string (via a call back to ReturnShellCommandOutput).
239 void ShellCmdToMacroString(WindowInfo *window, const char *command,
240 const char *input)
242 char *inputCopy;
244 /* Make a copy of the input string for issueCommand to hold and free
245 upon completion */
246 inputCopy = *input == '\0' ? NULL : XtNewString(input);
248 /* fork the command and begin processing input/output */
249 issueCommand(window, command, inputCopy, strlen(input),
250 ACCUMULATE | OUTPUT_TO_STRING, NULL, 0, 0, True);
254 ** Execute the line of text where the the insertion cursor is positioned
255 ** as a shell command.
257 void ExecCursorLine(WindowInfo *window, int fromMacro)
259 char *cmdText;
260 int left, right, insertPos;
261 char *subsCommand, fullName[MAXPATHLEN];
262 int pos, line, column;
263 char lineNumber[11];
265 /* Can't do two shell commands at once in the same window */
266 if (window->shellCmdData != NULL) {
267 XBell(TheDisplay, 0);
268 return;
271 /* get all of the text on the line with the insert position */
272 pos = TextGetCursorPos(window->lastFocus);
273 if (!GetSimpleSelection(window->buffer, &left, &right)) {
274 left = right = pos;
275 left = BufStartOfLine(window->buffer, left);
276 right = BufEndOfLine(window->buffer, right);
277 insertPos = right;
278 } else
279 insertPos = BufEndOfLine(window->buffer, right);
280 cmdText = BufGetRange(window->buffer, left, right);
281 BufUnsubstituteNullChars(cmdText, window->buffer);
283 /* insert a newline after the entire line */
284 BufInsert(window->buffer, insertPos, "\n");
286 /* Substitute the current file name for % and the current line number
287 for # in the shell command */
288 strcpy(fullName, window->path);
289 strcat(fullName, window->filename);
290 TextPosToLineAndCol(window->lastFocus, pos, &line, &column);
291 sprintf(lineNumber, "%d", line);
293 subsCommand = shellCommandSubstitutes(cmdText, fullName, lineNumber);
294 if (subsCommand == NULL)
296 DialogF(DF_ERR, window->shell, 1, "Shell Command",
297 "Shell command is too long due to\n"
298 "filename substitutions with '%%' or\n"
299 "line number substitutions with '#'", "OK");
300 return;
303 /* issue the command */
304 issueCommand(window, subsCommand, NULL, 0, 0, window->lastFocus, insertPos+1,
305 insertPos+1, fromMacro);
306 free(subsCommand);
307 XtFree(cmdText);
311 ** Do a shell command, with the options allowed to users (input source,
312 ** output destination, save first and load after) in the shell commands
313 ** menu.
315 void DoShellMenuCmd(WindowInfo *window, const char *command,
316 int input, int output,
317 int outputReplacesInput, int saveFirst, int loadAfter, int fromMacro)
319 int flags = 0;
320 char *text;
321 char *subsCommand, fullName[MAXPATHLEN];
322 int left = 0, right = 0, textLen;
323 int pos, line, column;
324 char lineNumber[11];
325 WindowInfo *inWindow = window;
326 Widget outWidget;
328 /* Can't do two shell commands at once in the same window */
329 if (window->shellCmdData != NULL) {
330 XBell(TheDisplay, 0);
331 return;
334 /* Substitute the current file name for % and the current line number
335 for # in the shell command */
336 strcpy(fullName, window->path);
337 strcat(fullName, window->filename);
338 pos = TextGetCursorPos(window->lastFocus);
339 TextPosToLineAndCol(window->lastFocus, pos, &line, &column);
340 sprintf(lineNumber, "%d", line);
342 subsCommand = shellCommandSubstitutes(command, fullName, lineNumber);
343 if (subsCommand == NULL)
345 DialogF(DF_ERR, window->shell, 1, "Shell Command",
346 "Shell command is too long due to\n"
347 "filename substitutions with '%%' or\n"
348 "line number substitutions with '#'", "OK");
349 return;
352 /* Get the command input as a text string. If there is input, errors
353 shouldn't be mixed in with output, so set flags to ERROR_DIALOGS */
354 if (input == FROM_SELECTION) {
355 text = BufGetSelectionText(window->buffer);
356 if (*text == '\0') {
357 XtFree(text);
358 free(subsCommand);
359 XBell(TheDisplay, 0);
360 return;
362 flags |= ACCUMULATE | ERROR_DIALOGS;
363 } else if (input == FROM_WINDOW) {
364 text = BufGetAll(window->buffer);
365 flags |= ACCUMULATE | ERROR_DIALOGS;
366 } else if (input == FROM_EITHER) {
367 text = BufGetSelectionText(window->buffer);
368 if (*text == '\0') {
369 XtFree(text);
370 text = BufGetAll(window->buffer);
372 flags |= ACCUMULATE | ERROR_DIALOGS;
373 } else /* FROM_NONE */
374 text = NULL;
376 /* If the buffer was substituting another character for ascii-nuls,
377 put the nuls back in before exporting the text */
378 if (text != NULL) {
379 textLen = strlen(text);
380 BufUnsubstituteNullChars(text, window->buffer);
381 } else
382 textLen = 0;
384 /* Assign the output destination. If output is to a new window,
385 create it, and run the command from it instead of the current
386 one, to free the current one from waiting for lengthy execution */
387 if (output == TO_DIALOG) {
388 outWidget = NULL;
389 flags |= OUTPUT_TO_DIALOG;
390 left = right = 0;
391 } else if (output == TO_NEW_WINDOW) {
392 EditNewFile(GetPrefOpenInTab()?inWindow:NULL, NULL, False, NULL, window->path);
393 outWidget = WindowList->textArea;
394 inWindow = WindowList;
395 left = right = 0;
396 CheckCloseDim();
397 } else { /* TO_SAME_WINDOW */
398 outWidget = window->lastFocus;
399 if (outputReplacesInput && input != FROM_NONE) {
400 if (input == FROM_WINDOW) {
401 left = 0;
402 right = window->buffer->length;
403 } else if (input == FROM_SELECTION) {
404 GetSimpleSelection(window->buffer, &left, &right);
405 flags |= ACCUMULATE | REPLACE_SELECTION;
406 } else if (input == FROM_EITHER) {
407 if (GetSimpleSelection(window->buffer, &left, &right))
408 flags |= ACCUMULATE | REPLACE_SELECTION;
409 else {
410 left = 0;
411 right = window->buffer->length;
414 } else {
415 if (GetSimpleSelection(window->buffer, &left, &right))
416 flags |= ACCUMULATE | REPLACE_SELECTION;
417 else
418 left = right = TextGetCursorPos(window->lastFocus);
422 /* If the command requires the file be saved first, save it */
423 if (saveFirst) {
424 if (!SaveWindow(window)) {
425 if (input != FROM_NONE)
426 XtFree(text);
427 free(subsCommand);
428 return;
432 /* If the command requires the file to be reloaded after execution, set
433 a flag for issueCommand to deal with it when execution is complete */
434 if (loadAfter)
435 flags |= RELOAD_FILE_AFTER;
437 /* issue the command */
438 issueCommand(inWindow, subsCommand, text, textLen, flags, outWidget, left,
439 right, fromMacro);
440 free(subsCommand);
444 ** Cancel the shell command in progress
446 void AbortShellCommand(WindowInfo *window)
448 shellCmdInfo *cmdData = window->shellCmdData;
450 if (cmdData == NULL)
451 return;
452 kill(- cmdData->childPid, SIGTERM);
453 finishCmdExecution(window, True);
457 ** Issue a shell command and feed it the string "input". Output can be
458 ** directed either to text widget "textW" where it replaces the text between
459 ** the positions "replaceLeft" and "replaceRight", to a separate pop-up dialog
460 ** (OUTPUT_TO_DIALOG), or to a macro-language string (OUTPUT_TO_STRING). If
461 ** "input" is NULL, no input is fed to the process. If an input string is
462 ** provided, it is freed when the command completes. Flags:
464 ** ACCUMULATE Causes output from the command to be saved up until
465 ** the command completes.
466 ** ERROR_DIALOGS Presents stderr output separately in popup a dialog,
467 ** and also reports failed exit status as a popup dialog
468 ** including the command output.
469 ** REPLACE_SELECTION Causes output to replace the selection in textW.
470 ** RELOAD_FILE_AFTER Causes the file to be completely reloaded after the
471 ** command completes.
472 ** OUTPUT_TO_DIALOG Send output to a pop-up dialog instead of textW
473 ** OUTPUT_TO_STRING Output to a macro-language string instead of a text
474 ** widget or dialog.
476 ** REPLACE_SELECTION, ERROR_DIALOGS, and OUTPUT_TO_STRING can only be used
477 ** along with ACCUMULATE (these operations can't be done incrementally).
479 static void issueCommand(WindowInfo *window, const char *command, char *input,
480 int inputLen, int flags, Widget textW, int replaceLeft,
481 int replaceRight, int fromMacro)
483 int stdinFD, stdoutFD, stderrFD = 0;
484 XtAppContext context = XtWidgetToApplicationContext(window->shell);
485 shellCmdInfo *cmdData;
486 pid_t childPid;
488 /* verify consistency of input parameters */
489 if ((flags & ERROR_DIALOGS || flags & REPLACE_SELECTION ||
490 flags & OUTPUT_TO_STRING) && !(flags & ACCUMULATE))
491 return;
493 /* a shell command called from a macro must be executed in the same
494 window as the macro, regardless of where the output is directed,
495 so the user can cancel them as a unit */
496 if (fromMacro)
497 window = MacroRunWindow();
499 /* put up a watch cursor over the waiting window */
500 if (!fromMacro)
501 BeginWait(window->shell);
503 /* enable the cancel menu item */
504 if (!fromMacro)
505 SetSensitive(window, window->cancelShellItem, True);
507 /* fork the subprocess and issue the command */
508 childPid = forkCommand(window->shell, command, window->path, &stdinFD,
509 &stdoutFD, (flags & ERROR_DIALOGS) ? &stderrFD : NULL);
511 /* set the pipes connected to the process for non-blocking i/o */
512 if (fcntl(stdinFD, F_SETFL, O_NONBLOCK) < 0)
513 perror("nedit: Internal error (fcntl)");
514 if (fcntl(stdoutFD, F_SETFL, O_NONBLOCK) < 0)
515 perror("nedit: Internal error (fcntl1)");
516 if (flags & ERROR_DIALOGS) {
517 if (fcntl(stderrFD, F_SETFL, O_NONBLOCK) < 0)
518 perror("nedit: Internal error (fcntl2)");
521 /* if there's nothing to write to the process' stdin, close it now */
522 if (input == NULL)
523 close(stdinFD);
525 /* Create a data structure for passing process information around
526 amongst the callback routines which will process i/o and completion */
527 cmdData = (shellCmdInfo *)XtMalloc(sizeof(shellCmdInfo));
528 window->shellCmdData = cmdData;
529 cmdData->flags = flags;
530 cmdData->stdinFD = stdinFD;
531 cmdData->stdoutFD = stdoutFD;
532 cmdData->stderrFD = stderrFD;
533 cmdData->childPid = childPid;
534 cmdData->outBufs = NULL;
535 cmdData->errBufs = NULL;
536 cmdData->input = input;
537 cmdData->inPtr = input;
538 cmdData->textW = textW;
539 cmdData->bannerIsUp = False;
540 cmdData->fromMacro = fromMacro;
541 cmdData->leftPos = replaceLeft;
542 cmdData->rightPos = replaceRight;
543 cmdData->inLength = inputLen;
545 /* Set up timer proc for putting up banner when process takes too long */
546 if (fromMacro)
547 cmdData->bannerTimeoutID = 0;
548 else
549 cmdData->bannerTimeoutID = XtAppAddTimeOut(context, BANNER_WAIT_TIME,
550 bannerTimeoutProc, window);
552 /* Set up timer proc for flushing output buffers periodically */
553 if ((flags & ACCUMULATE) || textW == NULL)
554 cmdData->flushTimeoutID = 0;
555 else
556 cmdData->flushTimeoutID = XtAppAddTimeOut(context, OUTPUT_FLUSH_FREQ,
557 flushTimeoutProc, window);
559 /* set up callbacks for activity on the file descriptors */
560 cmdData->stdoutInputID = XtAppAddInput(context, stdoutFD,
561 (XtPointer)XtInputReadMask, stdoutReadProc, window);
562 if (input != NULL)
563 cmdData->stdinInputID = XtAppAddInput(context, stdinFD,
564 (XtPointer)XtInputWriteMask, stdinWriteProc, window);
565 else
566 cmdData->stdinInputID = 0;
567 if (flags & ERROR_DIALOGS)
568 cmdData->stderrInputID = XtAppAddInput(context, stderrFD,
569 (XtPointer)XtInputReadMask, stderrReadProc, window);
570 else
571 cmdData->stderrInputID = 0;
573 /* If this was called from a macro, preempt the macro untill shell
574 command completes */
575 if (fromMacro)
576 PreemptMacro();
580 ** Called when the shell sub-process stdout stream has data. Reads data into
581 ** the "outBufs" buffer chain in the window->shellCommandData data structure.
583 static void stdoutReadProc(XtPointer clientData, int *source, XtInputId *id)
585 WindowInfo *window = (WindowInfo *)clientData;
586 shellCmdInfo *cmdData = window->shellCmdData;
587 buffer *buf;
588 int nRead;
590 /* read from the process' stdout stream */
591 buf = (buffer *)XtMalloc(sizeof(buffer));
592 nRead = read(cmdData->stdoutFD, buf->contents, IO_BUF_SIZE);
594 /* error in read */
595 if (nRead == -1) { /* error */
596 if (errno != EWOULDBLOCK && errno != EAGAIN) {
597 perror("nedit: Error reading shell command output");
598 XtFree((char *)buf);
599 finishCmdExecution(window, True);
601 return;
604 /* end of data. If the stderr stream is done too, execution of the
605 shell process is complete, and we can display the results */
606 if (nRead == 0) {
607 XtFree((char *)buf);
608 XtRemoveInput(cmdData->stdoutInputID);
609 cmdData->stdoutInputID = 0;
610 if (cmdData->stderrInputID == 0)
611 finishCmdExecution(window, False);
612 return;
615 /* characters were read successfully, add buf to linked list of buffers */
616 buf->length = nRead;
617 addOutput(&cmdData->outBufs, buf);
621 ** Called when the shell sub-process stderr stream has data. Reads data into
622 ** the "errBufs" buffer chain in the window->shellCommandData data structure.
624 static void stderrReadProc(XtPointer clientData, int *source, XtInputId *id)
626 WindowInfo *window = (WindowInfo *)clientData;
627 shellCmdInfo *cmdData = window->shellCmdData;
628 buffer *buf;
629 int nRead;
631 /* read from the process' stderr stream */
632 buf = (buffer *)XtMalloc(sizeof(buffer));
633 nRead = read(cmdData->stderrFD, buf->contents, IO_BUF_SIZE);
635 /* error in read */
636 if (nRead == -1) {
637 if (errno != EWOULDBLOCK && errno != EAGAIN) {
638 perror("nedit: Error reading shell command error stream");
639 XtFree((char *)buf);
640 finishCmdExecution(window, True);
642 return;
645 /* end of data. If the stdout stream is done too, execution of the
646 shell process is complete, and we can display the results */
647 if (nRead == 0) {
648 XtFree((char *)buf);
649 XtRemoveInput(cmdData->stderrInputID);
650 cmdData->stderrInputID = 0;
651 if (cmdData->stdoutInputID == 0)
652 finishCmdExecution(window, False);
653 return;
656 /* characters were read successfully, add buf to linked list of buffers */
657 buf->length = nRead;
658 addOutput(&cmdData->errBufs, buf);
662 ** Called when the shell sub-process stdin stream is ready for input. Writes
663 ** data from the "input" text string passed to issueCommand.
665 static void stdinWriteProc(XtPointer clientData, int *source, XtInputId *id)
667 WindowInfo *window = (WindowInfo *)clientData;
668 shellCmdInfo *cmdData = window->shellCmdData;
669 int nWritten;
671 nWritten = write(cmdData->stdinFD, cmdData->inPtr, cmdData->inLength);
672 if (nWritten == -1) {
673 if (errno == EPIPE) {
674 /* Just shut off input to broken pipes. User is likely feeding
675 it to a command which does not take input */
676 XtRemoveInput(cmdData->stdinInputID);
677 cmdData->stdinInputID = 0;
678 close(cmdData->stdinFD);
679 cmdData->inPtr = NULL;
680 } else if (errno != EWOULDBLOCK && errno != EAGAIN) {
681 perror("nedit: Write to shell command failed");
682 finishCmdExecution(window, True);
684 } else {
685 cmdData->inPtr += nWritten;
686 cmdData->inLength -= nWritten;
687 if (cmdData->inLength <= 0) {
688 XtRemoveInput(cmdData->stdinInputID);
689 cmdData->stdinInputID = 0;
690 close(cmdData->stdinFD);
691 cmdData->inPtr = NULL;
697 ** Timer proc for putting up the "Shell Command in Progress" banner if
698 ** the process is taking too long.
700 #define MAX_TIMEOUT_MSG_LEN (MAX_ACCEL_LEN + 60)
701 static void bannerTimeoutProc(XtPointer clientData, XtIntervalId *id)
703 WindowInfo *window = (WindowInfo *)clientData;
704 shellCmdInfo *cmdData = window->shellCmdData;
705 XmString xmCancel;
706 char* cCancel;
707 char message[MAX_TIMEOUT_MSG_LEN];
709 cmdData->bannerIsUp = True;
711 /* Extract accelerator text from menu PushButtons */
712 XtVaGetValues(window->cancelShellItem, XmNacceleratorText, &xmCancel, NULL);
714 /* Translate Motif string to char* */
715 cCancel = GetXmStringText(xmCancel);
717 /* Free Motif String */
718 XmStringFree(xmCancel);
720 /* Create message */
721 if ('\0' == cCancel[0])
723 strncpy(message, "Shell Command in Progress", MAX_TIMEOUT_MSG_LEN);
724 message[MAX_TIMEOUT_MSG_LEN - 1] = '\0';
725 } else
727 sprintf(message,
728 "Shell Command in Progress -- Press %s to Cancel",
729 cCancel);
732 /* Free C-string */
733 XtFree(cCancel);
735 SetModeMessage(window, message);
736 cmdData->bannerTimeoutID = 0;
740 ** Buffer replacement wrapper routine to be used for inserting output from
741 ** a command into the buffer, which takes into account that the buffer may
742 ** have been shrunken by the user (eg, by Undo). If necessary, the starting
743 ** and ending positions (part of the state of the command) are corrected.
745 static void safeBufReplace(textBuffer *buf, int *start, int *end,
746 const char *text)
748 if (*start > buf->length)
749 *start = buf->length;
750 if (*end > buf->length)
751 *end = buf->length;
752 BufReplace(buf, *start, *end, text);
756 ** Timer proc for flushing output buffers periodically when the process
757 ** takes too long.
759 static void flushTimeoutProc(XtPointer clientData, XtIntervalId *id)
761 WindowInfo *window = (WindowInfo *)clientData;
762 shellCmdInfo *cmdData = window->shellCmdData;
763 textBuffer *buf = TextGetBuffer(cmdData->textW);
764 int len;
765 char *outText;
767 /* shouldn't happen, but it would be bad if it did */
768 if (cmdData->textW == NULL)
769 return;
771 outText = coalesceOutput(&cmdData->outBufs, &len);
772 if (len != 0) {
773 if (BufSubstituteNullChars(outText, len, buf)) {
774 safeBufReplace(buf, &cmdData->leftPos, &cmdData->rightPos, outText);
775 TextSetCursorPos(cmdData->textW, cmdData->leftPos+strlen(outText));
776 cmdData->leftPos += len;
777 cmdData->rightPos = cmdData->leftPos;
778 } else
779 fprintf(stderr, "nedit: Too much binary data\n");
781 XtFree(outText);
783 /* re-establish the timer proc (this routine) to continue processing */
784 cmdData->flushTimeoutID = XtAppAddTimeOut(
785 XtWidgetToApplicationContext(window->shell),
786 OUTPUT_FLUSH_FREQ, flushTimeoutProc, clientData);
790 ** Clean up after the execution of a shell command sub-process and present
791 ** the output/errors to the user as requested in the initial issueCommand
792 ** call. If "terminatedOnError" is true, don't bother trying to read the
793 ** output, just close the i/o descriptors, free the memory, and restore the
794 ** user interface state.
796 static void finishCmdExecution(WindowInfo *window, int terminatedOnError)
798 shellCmdInfo *cmdData = window->shellCmdData;
799 textBuffer *buf;
800 int status, failure, errorReport, reselectStart, outTextLen, errTextLen;
801 int resp, cancel = False, fromMacro = cmdData->fromMacro;
802 char *outText, *errText = NULL;
804 /* Cancel any pending i/o on the file descriptors */
805 if (cmdData->stdoutInputID != 0)
806 XtRemoveInput(cmdData->stdoutInputID);
807 if (cmdData->stdinInputID != 0)
808 XtRemoveInput(cmdData->stdinInputID);
809 if (cmdData->stderrInputID != 0)
810 XtRemoveInput(cmdData->stderrInputID);
812 /* Close any file descriptors remaining open */
813 close(cmdData->stdoutFD);
814 if (cmdData->flags & ERROR_DIALOGS)
815 close(cmdData->stderrFD);
816 if (cmdData->inPtr != NULL)
817 close(cmdData->stdinFD);
819 /* Free the provided input text */
820 XtFree(cmdData->input);
822 /* Cancel pending timeouts */
823 if (cmdData->flushTimeoutID != 0)
824 XtRemoveTimeOut(cmdData->flushTimeoutID);
825 if (cmdData->bannerTimeoutID != 0)
826 XtRemoveTimeOut(cmdData->bannerTimeoutID);
828 /* Clean up waiting-for-shell-command-to-complete mode */
829 if (!cmdData->fromMacro) {
830 EndWait(window->shell);
831 SetSensitive(window, window->cancelShellItem, False);
832 if (cmdData->bannerIsUp)
833 ClearModeMessage(window);
836 /* If the process was killed or became inaccessable, give up */
837 if (terminatedOnError) {
838 freeBufList(&cmdData->outBufs);
839 freeBufList(&cmdData->errBufs);
840 waitpid(cmdData->childPid, &status, 0);
841 goto cmdDone;
844 /* Assemble the output from the process' stderr and stdout streams into
845 null terminated strings, and free the buffer lists used to collect it */
846 outText = coalesceOutput(&cmdData->outBufs, &outTextLen);
847 if (cmdData->flags & ERROR_DIALOGS)
848 errText = coalesceOutput(&cmdData->errBufs, &errTextLen);
850 /* Wait for the child process to complete and get its return status */
851 waitpid(cmdData->childPid, &status, 0);
853 /* Present error and stderr-information dialogs. If a command returned
854 error output, or if the process' exit status indicated failure,
855 present the information to the user. */
856 if (cmdData->flags & ERROR_DIALOGS)
858 failure = WIFEXITED(status) && WEXITSTATUS(status) != 0;
859 errorReport = *errText != '\0';
861 if (failure && errorReport)
863 removeTrailingNewlines(errText);
864 truncateString(errText, DF_MAX_MSG_LENGTH);
865 resp = DialogF(DF_WARN, window->shell, 2, "Warning", "%s", "Cancel",
866 "Proceed", errText);
867 cancel = resp == 1;
868 } else if (failure)
870 truncateString(outText, DF_MAX_MSG_LENGTH-70);
871 resp = DialogF(DF_WARN, window->shell, 2, "Command Failure",
872 "Command reported failed exit status.\n"
873 "Output from command:\n%s", "Cancel", "Proceed", outText);
874 cancel = resp == 1;
875 } else if (errorReport)
877 removeTrailingNewlines(errText);
878 truncateString(errText, DF_MAX_MSG_LENGTH);
879 resp = DialogF(DF_INF, window->shell, 2, "Information", "%s",
880 "Proceed", "Cancel", errText);
881 cancel = resp == 2;
884 XtFree(errText);
885 if (cancel)
887 XtFree(outText);
888 goto cmdDone;
892 /* If output is to a dialog, present the dialog. Otherwise insert the
893 (remaining) output in the text widget as requested, and move the
894 insert point to the end */
895 if (cmdData->flags & OUTPUT_TO_DIALOG) {
896 removeTrailingNewlines(outText);
897 if (*outText != '\0')
898 createOutputDialog(window->shell, outText);
899 } else if (cmdData->flags & OUTPUT_TO_STRING) {
900 ReturnShellCommandOutput(window,outText, WEXITSTATUS(status));
901 } else {
902 buf = TextGetBuffer(cmdData->textW);
903 if (!BufSubstituteNullChars(outText, outTextLen, buf)) {
904 fprintf(stderr,"nedit: Too much binary data in shell cmd output\n");
905 outText[0] = '\0';
907 if (cmdData->flags & REPLACE_SELECTION) {
908 reselectStart = buf->primary.rectangular ? -1 : buf->primary.start;
909 BufReplaceSelected(buf, outText);
910 TextSetCursorPos(cmdData->textW, buf->cursorPosHint);
911 if (reselectStart != -1)
912 BufSelect(buf, reselectStart, reselectStart + strlen(outText));
913 } else {
914 safeBufReplace(buf, &cmdData->leftPos, &cmdData->rightPos, outText);
915 TextSetCursorPos(cmdData->textW, cmdData->leftPos+strlen(outText));
919 /* If the command requires the file to be reloaded afterward, reload it */
920 if (cmdData->flags & RELOAD_FILE_AFTER)
921 RevertToSaved(window);
923 /* Command is complete, free data structure and continue macro execution */
924 XtFree(outText);
925 cmdDone:
926 XtFree((char *)cmdData);
927 window->shellCmdData = NULL;
928 if (fromMacro)
929 ResumeMacroExecution(window);
933 ** Fork a subprocess to execute a command, return file descriptors for pipes
934 ** connected to the subprocess' stdin, stdout, and stderr streams. cmdDir
935 ** sets the default directory for the subprocess. If stderrFD is passed as
936 ** NULL, the pipe represented by stdoutFD is connected to both stdin and
937 ** stderr. The function value returns the pid of the new subprocess, or -1
938 ** if an error occured.
940 static pid_t forkCommand(Widget parent, const char *command, const char *cmdDir,
941 int *stdinFD, int *stdoutFD, int *stderrFD)
943 int childStdoutFD, childStdinFD, childStderrFD, pipeFDs[2];
944 int dupFD;
945 pid_t childPid;
947 /* Ignore SIGPIPE signals generated when user attempts to provide
948 input for commands which don't take input */
949 signal(SIGPIPE, SIG_IGN);
951 /* Create pipes to communicate with the sub process. One end of each is
952 returned to the caller, the other half is spliced to stdin, stdout
953 and stderr in the child process */
954 if (pipe(pipeFDs) != 0) {
955 perror("nedit: Internal error (opening stdout pipe)");
956 return -1;
958 *stdoutFD = pipeFDs[0];
959 childStdoutFD = pipeFDs[1];
960 if (pipe(pipeFDs) != 0) {
961 perror("nedit: Internal error (opening stdin pipe)");
962 return -1;
964 *stdinFD = pipeFDs[1];
965 childStdinFD = pipeFDs[0];
966 if (stderrFD == NULL)
967 childStderrFD = childStdoutFD;
968 else {
969 if (pipe(pipeFDs) != 0) {
970 perror("nedit: Internal error (opening stdin pipe)");
971 return -1;
973 *stderrFD = pipeFDs[0];
974 childStderrFD = pipeFDs[1];
977 /* Fork the process */
978 #ifdef VMS
979 childPid = vfork();
980 #else
981 childPid = fork();
982 #endif
985 ** Child process context (fork returned 0), clean up the
986 ** child ends of the pipes and execute the command
988 if (0 == childPid) {
989 /* close the parent end of the pipes in the child process */
990 close(*stdinFD);
991 close(*stdoutFD);
992 if (stderrFD != NULL)
993 close(*stderrFD);
995 /* close current stdin, stdout, and stderr file descriptors before
996 substituting pipes */
997 close(fileno(stdin));
998 close(fileno(stdout));
999 close(fileno(stderr));
1001 /* duplicate the child ends of the pipes to have the same numbers
1002 as stdout & stderr, so it can substitute for stdout & stderr */
1003 dupFD = dup2(childStdinFD, fileno(stdin));
1004 if (dupFD == -1)
1005 perror("dup of stdin failed");
1006 dupFD = dup2(childStdoutFD, fileno(stdout));
1007 if (dupFD == -1)
1008 perror("dup of stdout failed");
1009 dupFD = dup2(childStderrFD, fileno(stderr));
1010 if (dupFD == -1)
1011 perror("dup of stderr failed");
1013 /* now close the original child end of the pipes
1014 (we now have the 0, 1 and 2 descriptors in their place) */
1015 close(childStdinFD);
1016 close(childStdoutFD);
1017 close(childStderrFD);
1019 /* make this process the leader of a new process group, so the sub
1020 processes can be killed, if necessary, with a killpg call */
1021 #ifndef __EMX__ /* OS/2 doesn't have this */
1022 #ifndef VMS /* VMS doesn't have this */
1023 setsid();
1024 #endif
1025 #endif
1027 /* change the current working directory to the directory of the
1028 current file. */
1029 if (cmdDir[0] != 0) {
1030 if (chdir(cmdDir) == -1) {
1031 perror("chdir to directory of current file failed");
1035 /* execute the command using the shell specified by preferences */
1036 execlp(GetPrefShell(), GetPrefShell(), "-c", command, NULL);
1038 /* if we reach here, execlp failed */
1039 fprintf(stderr, "Error starting shell: %s\n", GetPrefShell());
1040 exit(EXIT_FAILURE);
1043 /* Parent process context, check if fork succeeded */
1044 if (childPid == -1)
1046 DialogF(DF_ERR, parent, 1, "Shell Command",
1047 "Error starting shell command process\n(fork failed)",
1048 "OK");
1051 /* close the child ends of the pipes */
1052 close(childStdinFD);
1053 close(childStdoutFD);
1054 if (stderrFD != NULL)
1055 close(childStderrFD);
1057 return childPid;
1061 ** Add a buffer full of output to a buffer list
1063 static void addOutput(buffer **bufList, buffer *buf)
1065 buf->next = *bufList;
1066 *bufList = buf;
1070 ** coalesce the contents of a list of buffers into a contiguous memory block,
1071 ** freeing the memory occupied by the buffer list. Returns the memory block
1072 ** as the function result, and its length as parameter "length".
1074 static char *coalesceOutput(buffer **bufList, int *outLength)
1076 buffer *buf, *rBufList = NULL;
1077 char *outBuf, *outPtr, *p;
1078 int i, length = 0;
1080 /* find the total length of data read */
1081 for (buf=*bufList; buf!=NULL; buf=buf->next)
1082 length += buf->length;
1084 /* allocate contiguous memory for returning data */
1085 outBuf = XtMalloc(length+1);
1087 /* reverse the buffer list */
1088 while (*bufList != NULL) {
1089 buf = *bufList;
1090 *bufList = buf->next;
1091 buf->next = rBufList;
1092 rBufList = buf;
1095 /* copy the buffers into the output buffer */
1096 outPtr = outBuf;
1097 for (buf=rBufList; buf!=NULL; buf=buf->next) {
1098 p = buf->contents;
1099 for (i=0; i<buf->length; i++)
1100 *outPtr++ = *p++;
1103 /* terminate with a null */
1104 *outPtr = '\0';
1106 /* free the buffer list */
1107 freeBufList(&rBufList);
1109 *outLength = outPtr - outBuf;
1110 return outBuf;
1113 static void freeBufList(buffer **bufList)
1115 buffer *buf;
1117 while (*bufList != NULL) {
1118 buf = *bufList;
1119 *bufList = buf->next;
1120 XtFree((char *)buf);
1125 ** Remove trailing newlines from a string by substituting nulls
1127 static void removeTrailingNewlines(char *string)
1129 char *endPtr = &string[strlen(string)-1];
1131 while (endPtr >= string && *endPtr == '\n')
1132 *endPtr-- = '\0';
1136 ** Create a dialog for the output of a shell command. The dialog lives until
1137 ** the user presses the Dismiss button, and is then destroyed
1139 static void createOutputDialog(Widget parent, char *text)
1141 Arg al[50];
1142 int ac, rows, cols, hasScrollBar, wrapped;
1143 Widget form, textW, button;
1144 XmString st1;
1146 /* measure the width and height of the text to determine size for dialog */
1147 measureText(text, MAX_OUT_DIALOG_COLS, &rows, &cols, &wrapped);
1148 if (rows > MAX_OUT_DIALOG_ROWS) {
1149 rows = MAX_OUT_DIALOG_ROWS;
1150 hasScrollBar = True;
1151 } else
1152 hasScrollBar = False;
1153 if (cols > MAX_OUT_DIALOG_COLS)
1154 cols = MAX_OUT_DIALOG_COLS;
1155 if (cols == 0)
1156 cols = 1;
1157 /* Without completely emulating Motif's wrapping algorithm, we can't
1158 be sure that we haven't underestimated the number of lines in case
1159 a line has wrapped, so let's assume that some lines could be obscured
1161 if (wrapped)
1162 hasScrollBar = True;
1163 ac = 0;
1164 form = CreateFormDialog(parent, "shellOutForm", al, ac);
1166 ac = 0;
1167 XtSetArg(al[ac], XmNlabelString, st1=MKSTRING("OK")); ac++;
1168 XtSetArg(al[ac], XmNmarginWidth, BUTTON_WIDTH_MARGIN); ac++;
1169 XtSetArg(al[ac], XmNhighlightThickness, 2); ac++;
1170 XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
1171 XtSetArg(al[ac], XmNtopAttachment, XmATTACH_NONE); ac++;
1172 button = XmCreatePushButtonGadget(form, "ok", al, ac);
1173 XtManageChild(button);
1174 XtVaSetValues(form, XmNdefaultButton, button, NULL);
1175 XtVaSetValues(form, XmNcancelButton, button, NULL);
1176 XmStringFree(st1);
1177 XtAddCallback(button, XmNactivateCallback, destroyOutDialogCB,
1178 XtParent(form));
1180 ac = 0;
1181 XtSetArg(al[ac], XmNrows, rows); ac++;
1182 XtSetArg(al[ac], XmNcolumns, cols); ac++;
1183 XtSetArg(al[ac], XmNresizeHeight, False); ac++;
1184 XtSetArg(al[ac], XmNtraversalOn, True); ac++;
1185 XtSetArg(al[ac], XmNwordWrap, True); ac++;
1186 XtSetArg(al[ac], XmNscrollHorizontal, False); ac++;
1187 XtSetArg(al[ac], XmNscrollVertical, hasScrollBar); ac++;
1188 XtSetArg(al[ac], XmNhighlightThickness, 2); ac++;
1189 XtSetArg(al[ac], XmNspacing, 0); ac++;
1190 XtSetArg(al[ac], XmNeditMode, XmMULTI_LINE_EDIT); ac++;
1191 XtSetArg(al[ac], XmNeditable, False); ac++;
1192 XtSetArg(al[ac], XmNvalue, text); ac++;
1193 XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
1194 XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
1195 XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_WIDGET); ac++;
1196 XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
1197 XtSetArg(al[ac], XmNbottomWidget, button); ac++;
1198 textW = XmCreateScrolledText(form, "outText", al, ac);
1199 AddMouseWheelSupport(textW);
1200 MakeSingleLineTextW(textW); /* Binds <Return> to activate() */
1201 XtManageChild(textW);
1203 XtVaSetValues(XtParent(form), XmNtitle, "Output from Command", NULL);
1204 ManageDialogCenteredOnPointer(form);
1206 #ifdef LESSTIF_VERSION
1208 * The Lesstif text widget blocks activate() calls in multi-line mode,
1209 * so we put the original focus on the Ok button such that the user
1210 * can simply hit Return to dismiss the dialog.
1212 XmProcessTraversal(button, XmTRAVERSE_CURRENT);
1213 #else
1214 XmProcessTraversal(textW, XmTRAVERSE_CURRENT);
1215 #endif
1219 ** Dispose of the command output dialog when user presses Dismiss button
1221 static void destroyOutDialogCB(Widget w, XtPointer callback, XtPointer closure)
1223 XtDestroyWidget((Widget)callback);
1227 ** Measure the width and height of a string of text. Assumes 8 character
1228 ** tabs. wrapWidth specifies a number of columns at which text wraps.
1230 static void measureText(char *text, int wrapWidth, int *rows, int *cols,
1231 int *wrapped)
1233 int maxCols = 0, line = 1, col = 0, wrapCol;
1234 char *c;
1236 *wrapped = 0;
1237 for (c=text; *c!='\0'; c++) {
1238 if (*c=='\n') {
1239 line++;
1240 col = 0;
1241 continue;
1244 if (*c == '\t') {
1245 col += 8 - (col % 8);
1246 wrapCol = 0; /* Tabs at end of line are not drawn when wrapped */
1247 } else if (*c == ' ') {
1248 col++;
1249 wrapCol = 0; /* Spaces at end of line are not drawn when wrapped */
1250 } else {
1251 col++;
1252 wrapCol = 1;
1255 /* Note: there is a small chance that the number of lines is
1256 over-estimated when a line ends with a space or a tab (ie, followed
1257 by a newline) and that whitespace crosses the boundary, because
1258 whitespace at the end of a line does not cause wrapping. Taking
1259 this into account is very hard, but an over-estimation is harmless.
1260 The worst that can happen is that some extra blank lines are shown
1261 at the end of the dialog (in contrast to an under-estimation, which
1262 could make the last lines invisible).
1263 On the other hand, without emulating Motif's wrapping algorithm
1264 completely, we can't be sure that we don't underestimate the number
1265 of lines (Motif uses word wrap, and this counting algorithm uses
1266 character wrap). Therefore, we remember whether there is a line
1267 that has wrapped. In that case we allways install a scroll bar.
1269 if (col > wrapWidth) {
1270 line++;
1271 *wrapped = 1;
1272 col = wrapCol;
1273 } else if (col > maxCols) {
1274 maxCols = col;
1277 *rows = line;
1278 *cols = maxCols;
1282 ** Truncate a string to a maximum of length characters. If it shortens the
1283 ** string, it appends "..." to show that it has been shortened. It assumes
1284 ** that the string that it is passed is writeable.
1286 static void truncateString(char *string, int length)
1288 if ((int)strlen(string) > length)
1289 memcpy(&string[length-3], "...", 4);
1293 ** Substitute the string fileStr in inStr wherever % appears and
1294 ** lineStr in inStr wherever # appears, storing the
1295 ** result in outStr. If predictOnly is non-zero, the result string length
1296 ** is predicted without creating the string. Returns the length of the result
1297 ** string or -1 in case of an error.
1300 static int shellSubstituter(char *outStr, const char *inStr, const char *fileStr,
1301 const char *lineStr, int outLen, int predictOnly)
1303 const char *inChar;
1304 char *outChar = NULL;
1305 int outWritten = 0;
1306 int fileLen, lineLen;
1308 inChar = inStr;
1309 if (!predictOnly) {
1310 outChar = outStr;
1312 fileLen = strlen(fileStr);
1313 lineLen = strlen(lineStr);
1315 while (*inChar != '\0') {
1317 if (!predictOnly && outWritten >= outLen) {
1318 return(-1);
1321 if (*inChar == '%') {
1322 if (*(inChar + 1) == '%') {
1323 inChar += 2;
1324 if (!predictOnly) {
1325 *outChar++ = '%';
1327 outWritten++;
1328 } else {
1329 if (!predictOnly) {
1330 if (outWritten + fileLen >= outLen) {
1331 return(-1);
1333 strncpy(outChar, fileStr, fileLen);
1334 outChar += fileLen;
1336 outWritten += fileLen;
1337 inChar++;
1339 } else if (*inChar == '#') {
1340 if (*(inChar + 1) == '#') {
1341 inChar += 2;
1342 if (!predictOnly) {
1343 *outChar++ = '#';
1345 outWritten++;
1346 } else {
1347 if (!predictOnly) {
1348 if (outWritten + lineLen >= outLen) {
1349 return(-1);
1351 strncpy(outChar, lineStr, lineLen);
1352 outChar += lineLen;
1354 outWritten += lineLen;
1355 inChar++;
1357 } else {
1358 if (!predictOnly) {
1359 *outChar++ = *inChar;
1361 inChar++;
1362 outWritten++;
1366 if (!predictOnly) {
1367 if (outWritten >= outLen) {
1368 return(-1);
1370 *outChar = '\0';
1372 ++outWritten;
1373 return(outWritten);
1376 static char *shellCommandSubstitutes(const char *inStr, const char *fileStr,
1377 const char *lineStr)
1379 int cmdLen;
1380 char *subsCmdStr = NULL;
1382 cmdLen = shellSubstituter(NULL, inStr, fileStr, lineStr, 0, 1);
1383 if (cmdLen >= 0) {
1384 subsCmdStr = malloc(cmdLen);
1385 if (subsCmdStr) {
1386 cmdLen = shellSubstituter(subsCmdStr, inStr, fileStr, lineStr, cmdLen, 0);
1387 if (cmdLen < 0) {
1388 free(subsCmdStr);
1389 subsCmdStr = NULL;
1393 return(subsCmdStr);