From fe7220bb20132092a44fcb79f3ed68de5c9348d1 Mon Sep 17 00:00:00 2001 From: fickleheart Date: Fri, 12 Apr 2019 00:46:52 -0500 Subject: [PATCH 1/3] Allow argument substitution in aliases --- src/command.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/command.c b/src/command.c index a5d45bc1..543df6ce 100644 --- a/src/command.c +++ b/src/command.c @@ -537,10 +537,48 @@ static void COM_ExecuteString(char *ptext) { CONS_Alert(CONS_WARNING, M_GetText("Alias recursion cycle detected!\n")); recursion = 0; - return; } - recursion++; - COM_BufInsertText(a->value); + else + { + char buf[1024]; + char *write = buf, *read = a->value, *seek = read; + + while (*seek != '\0') + { + if (*seek == '$') + { + memcpy(write, read, seek-read); + write += seek-read; + + seek++; + + if (*seek >= '1' && *seek <= '9') + { + if (com_argc > (size_t)(*seek - '0')) + { + memcpy(write, com_argv[*seek - '0'], strlen(com_argv[*seek - '0'])); + write += strlen(com_argv[*seek - '0']); + } + seek++; + } + else + { + *write = '$'; + write++; + } + + read = seek; + } + else + seek++; + } + memcpy(write, read, seek-read); + write += seek-read; + *write = '\0'; + + recursion++; + COM_BufInsertText(buf); + } return; } } From 3286d9ac9384556b515db327439ac05dc75b8db4 Mon Sep 17 00:00:00 2001 From: fickleheart Date: Sat, 13 Apr 2019 10:16:54 -0500 Subject: [PATCH 2/3] Use strchr? --- src/command.c | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/src/command.c b/src/command.c index 543df6ce..8e2d0037 100644 --- a/src/command.c +++ b/src/command.c @@ -543,38 +543,31 @@ static void COM_ExecuteString(char *ptext) char buf[1024]; char *write = buf, *read = a->value, *seek = read; - while (*seek != '\0') + while ((seek = strchr(seek, '$')) != NULL) { - if (*seek == '$') + memcpy(write, read, seek-read); + write += seek-read; + + seek++; + + if (*seek >= '1' && *seek <= '9') { - memcpy(write, read, seek-read); - write += seek-read; - + if (com_argc > (size_t)(*seek - '0')) + { + memcpy(write, com_argv[*seek - '0'], strlen(com_argv[*seek - '0'])); + write += strlen(com_argv[*seek - '0']); + } seek++; - - if (*seek >= '1' && *seek <= '9') - { - if (com_argc > (size_t)(*seek - '0')) - { - memcpy(write, com_argv[*seek - '0'], strlen(com_argv[*seek - '0'])); - write += strlen(com_argv[*seek - '0']); - } - seek++; - } - else - { - *write = '$'; - write++; - } - - read = seek; } else - seek++; + { + *write = '$'; + write++; + } + + read = seek; } - memcpy(write, read, seek-read); - write += seek-read; - *write = '\0'; + WRITESTRING(write, read); recursion++; COM_BufInsertText(buf); From 451ff30a7a0e7a004547d2e81467974a15004b6c Mon Sep 17 00:00:00 2001 From: fickleheart Date: Sat, 13 Apr 2019 10:25:56 -0500 Subject: [PATCH 3/3] Preserve quote and etc in aliases --- src/command.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/command.c b/src/command.c index 8e2d0037..7d7a357a 100644 --- a/src/command.c +++ b/src/command.c @@ -594,8 +594,6 @@ static void COM_ExecuteString(char *ptext) static void COM_Alias_f(void) { cmdalias_t *a; - char cmd[1024]; - size_t i, c; if (COM_Argc() < 3) { @@ -608,19 +606,9 @@ static void COM_Alias_f(void) com_alias = a; a->name = Z_StrDup(COM_Argv(1)); - - // copy the rest of the command line - cmd[0] = 0; // start out with a null string - c = COM_Argc(); - for (i = 2; i < c; i++) - { - strcat(cmd, COM_Argv(i)); - if (i != c) - strcat(cmd, " "); - } - strcat(cmd, "\n"); - - a->value = Z_StrDup(cmd); + // Just use arg 2 if it's the only other argument, in case the alias is wrapped in quotes (backward compat, or multiple commands in one string). + // Otherwise pull the whole string and seek to the end of the alias name. The strctr is in case the alias is quoted. + a->value = Z_StrDup(COM_Argc() == 3 ? COM_Argv(2) : (strchr(COM_Args() + strlen(a->name), ' ') + 1)); } /** Prints a line of text to the console.