~emersion/mrsh

dc8870d10f9d19ca1f50ecea1f93a9af32d582a3 — Simon Ser 2 years ago be3d989
shell/task/word: implement assign default value parameter operator
2 files changed, 16 insertions(+), 1 deletions(-)

M shell/task/word.c
M test/word.sh
M shell/task/word.c => shell/task/word.c +14 -1
@@ 167,7 167,20 @@ static int apply_parameter_op(struct context *ctx,
		}
		return 0;
	case MRSH_PARAM_EQUAL: // Assign Default Values
		assert(false); // TODO
		// TODO: error out if positional or special parameter
		if (str == NULL || (str[0] == '\0' && wp->colon)) {
			*result = copy_word_or_null(wp->arg);
			int ret = run_word(ctx, result);
			if (ret < 0) {
				return ret;
			}
			char *str = mrsh_word_str(*result);
			mrsh_env_set(ctx->state, wp->name, str, 0);
			free(str);
		} else {
			*result = create_word_string(str);
		}
		return 0;
	case MRSH_PARAM_QMARK: // Indicate Error if Null or Unset
		assert(false); // TODO
	case MRSH_PARAM_PLUS: // Use Alternative Value

M test/word.sh => test/word.sh +2 -0
@@ 20,6 20,8 @@ echo $a ${b} ">$a<"
echo \$a '$a'
echo ${a:-BAD} ${idontexist:-GOOD} ${null:-GOOD} ${idontexist:-}
echo ${a-BAD} ${idontexist-GOOD} ${null-BAD} ${null-}
echo ${c:=GOOD} $c; echo ${c:=BAD} $c; c=""; echo ${c:=GOOD} $c; unset c
echo ${c=GOOD} $c; echo ${c=BAD} $c; c=""; echo ${c=BAD} $c; unset c
echo ${a:+GOOD} ${idontexist:+BAD} ${null:+BAD} ${idontexist:+}
echo ${a+GOOD} ${idontexist+BAD} ${null-GOOD} ${null+}
echo ${#hello} ${#null} ${#idontexist}