Skip to content

Commit

Permalink
Execute pushes exit code onto the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Sep 16, 2024
1 parent 66638eb commit 3cdf0d4
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions mshell/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,17 +519,19 @@ public EvalResult Evaluate(List<TokenNew> tokens, Stack<MShellObject> stack, Exe
{
if (stack.TryPop(out var arg))
{
EvalResult result = arg.Match(
(EvalResult result, int exitCode) = arg.Match(
literalToken => RunProcess(new MShellList(new List<MShellObject>(1) { literalToken }), context),
list => RunProcess(list, context),
_ => FailWithMessage("Cannot execute a quotation.\n"),
_ => FailWithMessage("Cannot execute an integer.\n"),
_ => FailWithMessage("Cannot execute a boolean.\n"),
_ => (FailWithMessage("Cannot execute a quotation.\n"), 1),
_ => (FailWithMessage("Cannot execute an integer.\n"), 1),
_ => (FailWithMessage("Cannot execute a boolean.\n"), 1),
str => RunProcess(new MShellList(new List<MShellObject>(1) { str }), context),
pipe => RunProcess(pipe, context)
);

if (!result.Success) return result;

_push(new IntToken(new TokenNew(t.Line, t.Column, t.Start, exitCode.ToString(), TokenType.INTEGER)), stack);
}
else
{
Expand Down Expand Up @@ -886,17 +888,17 @@ private void ExecuteQuotation(MShellQuotation q)
}
}

public EvalResult RunProcess(MShellList list, ExecuteContext context)
public (EvalResult, int) RunProcess(MShellList list, ExecuteContext context)
{
if (list.Items.Any(o => !o.IsCommandLineable()))
{
var badTypes = list.Items.Where(o => !o.IsCommandLineable());
return FailWithMessage($"Can't handle a process argument of type {string.Join(", ", badTypes.Select(o => o.TypeName()))}.");
return (FailWithMessage($"Can't handle a process argument of type {string.Join(", ", badTypes.Select(o => o.TypeName()))}."), 1);
}

List<string> arguments = list.Items.Select(o => o.CommandLine()).ToList();

if (arguments.Count == 0) return FailWithMessage("Cannot execute an empty list");
if (arguments.Count == 0) return (FailWithMessage("Cannot execute an empty list"), 1);

// Console.Write(context);

Expand Down Expand Up @@ -952,15 +954,15 @@ public EvalResult RunProcess(MShellList list, ExecuteContext context)
}
catch (Exception e)
{
return FailWithMessage($"There was an exception running process with args { string.Join(", ", arguments.Select(s => $"'{s}'")) }.\n{e.Message}\n");
return (FailWithMessage($"There was an exception running process with args { string.Join(", ", arguments.Select(s => $"'{s}'")) }.\n{e.Message}\n"), 1);
}

return new EvalResult(true, -1);
return (new EvalResult(true, -1), p.ExitCode);
}

public EvalResult RunProcess(MShellPipe pipe, ExecuteContext context)
public (EvalResult, int) RunProcess(MShellPipe pipe, ExecuteContext context)
{
if (pipe.List.Items.Count == 0) return new EvalResult(true, -1);
if (pipe.List.Items.Count == 0) return (new EvalResult(true, -1), 1);

List<MShellList> listItems = new List<MShellList>();
foreach (var i in pipe.List.Items)
Expand All @@ -971,7 +973,7 @@ public EvalResult RunProcess(MShellPipe pipe, ExecuteContext context)
}
else
{
return FailWithMessage($"Pipelines are only supported with list items currently.\n");
return (FailWithMessage($"Pipelines are only supported with list items currently.\n"), 1);
}
}

Expand All @@ -983,7 +985,7 @@ public EvalResult RunProcess(MShellPipe pipe, ExecuteContext context)

if (firstList.Items.Any(i => !i.IsCommandLineable()))
{
return FailWithMessage("Not all elements in list are valid for command.\n");
return (FailWithMessage("Not all elements in list are valid for command.\n"), 1);
}

var firstProcessStartInfo = new ProcessStartInfo()
Expand Down Expand Up @@ -1063,7 +1065,7 @@ public EvalResult RunProcess(MShellPipe pipe, ExecuteContext context)

foreach (var p in processes) p.WaitForExit();

return new EvalResult(true, -1);
return (new EvalResult(true, -1), 1);
}

public byte[] ReadAllBytesFromStream(Stream stream)
Expand Down

0 comments on commit 3cdf0d4

Please sign in to comment.