Skip to content

Commit

Permalink
WIP correctly expanding arguments
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantinos Kallas <[email protected]>
  • Loading branch information
angelhof committed Jun 18, 2022
1 parent 9819a48 commit 946e98e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
21 changes: 18 additions & 3 deletions compiler/ast_to_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,14 @@ def make_call_to_runtime(ir_filename, sequential_script_file_name,
## ```
assignments = [["pash_input_args",
[make_quoted_variable("@")]]]
input_args_command = make_command([],
assignments=assignments)
# input_args_command = make_command([],
# assignments=assignments)
arguments = [string_to_argument("source"),
string_to_argument(config.SAVE_ARGS_EXECUTABLE),
[make_quoted_variable("@")]]
## Pass a relevant argument to the planner
# arguments += config.pass_common_arguments(config.pash_args)
input_args_command = make_command(arguments)

## Disable parallel pipelines if we are in the last command of the script.
## ```
Expand Down Expand Up @@ -946,9 +952,18 @@ def make_call_to_runtime(ir_filename, sequential_script_file_name,
## ```
##
## TODO: Maybe we need to only do this if there is a change.
##
## TODO: We have to go through this backquote nightmare (which also leads to issues),
## because the preprocessed script needs to be POSIX compliant
## (since it goes through our unparser) and therefore we cannot do the
## straightforward array expansion.
arguments = [string_to_argument("source"),
string_to_argument(config.RESTORE_ARGS_EXECUTABLE)]
## Pass a relevant argument to the planner
restore_args_command_node = make_command(arguments)
set_arguments = [string_to_argument("set"),
string_to_argument("--"),
[standard_var_ast("pash_input_args")]]
[make_backquote(restore_args_command_node)]]
set_args_node = make_command(set_arguments)

## Restore the exit code (since now we have executed `set` last)
Expand Down
2 changes: 2 additions & 0 deletions compiler/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
PYTHON_VERSION = "python3"
PLANNER_EXECUTABLE = os.path.join(PASH_TOP, "compiler/pash_runtime.py")
RUNTIME_EXECUTABLE = os.path.join(PASH_TOP, "compiler/pash_runtime.sh")
SAVE_ARGS_EXECUTABLE = os.path.join(PASH_TOP, "runtime/save_args.sh")
RESTORE_ARGS_EXECUTABLE = os.path.join(PASH_TOP, "runtime/restore_args.sh")

## Ensure that PASH_TMP_PREFIX is set by pa.sh
assert(not os.getenv('PASH_TMP_PREFIX') is None)
Expand Down
13 changes: 12 additions & 1 deletion compiler/ir_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def get_command_from_definition(command_definition):
return ''

def format_args(args):
log("Before formatting:", args)
formatted_args = [format_arg_chars(arg_chars) for arg_chars in args]
log("After formatting:", formatted_args)
return formatted_args

def format_arg_chars(arg_chars):
Expand Down Expand Up @@ -109,7 +111,9 @@ def format_arg_char(arg_char):
## This function finds the first raw character in an argument.
## It needs to be called on an expanded string.
def format_expanded_arg_chars(arg_chars):
log("Before formatting 2:", arg_chars)
chars = [format_expanded_arg_char(arg_char) for arg_char in arg_chars]
log("After formatting 2:", chars)
return "".join(chars)

def format_expanded_arg_char(arg_char):
Expand Down Expand Up @@ -162,7 +166,10 @@ def string_to_arguments(string):
return [string_to_argument(word) for word in string.split(" ")]

def string_to_argument(string):
return [char_to_arg_char(char) for char in string]
log(" -- -- -- Before turning to argument:", string)
ret = [char_to_arg_char(char) for char in string]
log(" -- -- -- After turning to argument:", ret)
return ret

## FIXME: This is certainly not complete. It is used to generate the
## AST for the call to the distributed planner. It only handles simple
Expand Down Expand Up @@ -193,6 +200,10 @@ def make_background(body, redirections=[]):
node = make_kv("Background", [lineno, body, redirections])
return node

def make_backquote(node):
node = make_kv("B", node)
return node

def make_subshell(body, redirections=[]):
lineno = 0
node = make_kv("Subshell", [lineno, body, redirections])
Expand Down
13 changes: 10 additions & 3 deletions compiler/pash_wrap_vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ pash_redir_output echo "$$: (3) Reverted to BaSh set state: $-"
# This variable stores arguments as a space-separated stirng, so we need to
# unquote it and to split it into multiple strings by shell's field splitting.
# shellcheck disable=SC2086
set -- $pash_input_args
pash_redir_output echo "$$: (3) Array: ${pash_input_args[@]}"
pash_redir_output echo "$$: (3) Number of arguments: ${#pash_input_args[@]}"
myArray=("$pash_input_args")
pash_redir_output echo "$$: (3) Array: ${myArray[@]}"
pash_redir_output echo "$$: (3) Number of arguments: ${#myArray[@]}"
# set -- $pash_input_args
set -- "${pash_input_args[@]}"
pash_redir_output echo "$$: (3) Reverted to BaSh input arguments: $@"
pash_redir_output echo "$$: (3) Number of arguments: $#"

## Execute the script
pash_redir_output echo "$$: (4) Restoring previous exit code: ${pash_previous_exit_status}"
Expand All @@ -35,15 +42,15 @@ then
source "${script_source}"
internal_exec_status=$?
## Make sure that any input argument changes are propagated outside
export pash_input_args="$@"
export pash_input_args=( "$@" )
(exit "$internal_exec_status")
}
else
{
source "${script_source}"
internal_exec_status=$?
## Make sure that any input argument changes are propagated outside
export pash_input_args="$@"
export pash_input_args=( "$@" )
(exit "$internal_exec_status")
}
fi
6 changes: 6 additions & 0 deletions runtime/restore_args.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# pash_input_args=(1 2 "3 4" "%")
# echo "${pash_input_args[@]@Q}"
printf "\"%s\" " "${pash_input_args[@]}"

4 changes: 4 additions & 0 deletions runtime/save_args.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# echo $@
export pash_input_args=( "$@" )

0 comments on commit 946e98e

Please sign in to comment.