alias cmd with edit

Post tutorials on how to do certain tasks within game or engine code here.
Post Reply
r00k
Posts: 1111
Joined: Sat Nov 13, 2004 10:39 pm

alias cmd with edit

Post by r00k »

I did this some years ago but never really shared this so here goes.
When players like to build alias commands they can go crazy and type long lines of stacked commands
But when you are trying to tweak your alias commands it really sucks to retype the whole alias just to change something minor.
This code, allows the user to type "alias foo" then press enter. If "foo" was already a valid alias it prints the value to the console with the cursor
at the end like
] alias foo

] alias foo "echo this is just a testx!" _
Which would allow the user to cursor back and delete the x without having to retype the whole line..

Code: Select all

void Cmd_Alias_f (void)
{
	cmdalias_t	*a;
	char		cmd[1024];
	int			i, c;
	char		*s,*n;

	if (cls.demoplayback)//R00k dont stuff alias commands when watching a demo.
		return;

	switch (Cmd_Argc())
	{
	case 1: //list all aliases
		for (a = cmd_alias, i = 0; a; a=a->next, i++)
			Con_SafePrintf ("   %s: %s", a->name, a->value);
		if (i)
			Con_SafePrintf ("%i alias command(s)\n", i);
		else
			Con_SafePrintf ("no alias commands found\n");
		break;
	case 2: //output current alias string
		n = Cmd_Argv(1);
		for (a = cmd_alias ; a ; a=a->next)
			if (!strcmp(Cmd_Argv(1), a->name))//R00k empty definition argument edits the current value
			{			
				sprintf (n,"alias %s \"%s",a->name, a->value);

				strcpy (key_lines[edit_line]+1, n);
				key_linepos = (strlen(n) - 1);
				key_lines[edit_line][key_linepos++] = '\"';
				key_lines[edit_line][key_linepos] = 0;
			}
		break;
	default: //set alias string
		s = Cmd_Argv(1);
		if (strlen(s) >= MAX_ALIAS_NAME)
		{
			Con_Printf ("Alias name is too long\n");
			return;
		}

		// if the alias allready exists, reuse it
		for (a = cmd_alias ; a ; a=a->next)
		{
			if (!strcmp(s, a->name))
			{
				Z_Free (a->value);
				break;
			}
		}

		if (!a)
		{
			a = Z_Malloc (sizeof(cmdalias_t));
			a->next = cmd_alias;
			cmd_alias = a;
		}
		strcpy (a->name, s);

		// copy the rest of the command line
		cmd[0] = 0;		// start out with a null string
		c = Cmd_Argc();
		for (i=2 ; i< c ; i++)
		{
			strcat (cmd, Cmd_Argv(i));
			if ((i != c))
				strcat (cmd, " ");
		}
		strcat (cmd, "\n");

		a->value = CopyString (cmd);
		break;
	}
}
I've gotten so used to this, i forgot it wasnt standard. :P
gnounc
Posts: 428
Joined: Mon Apr 06, 2009 6:26 am

Re: alias cmd with edit

Post by gnounc »

hella nice.
mankrip
Posts: 924
Joined: Fri Jul 04, 2008 3:02 am

Re: alias cmd with edit

Post by mankrip »

Taking a quick look at the code, there's a problem: it assumes that the console is active and that the alias command came from it. If the command comes from somewhere else (e.g. a stuffcmd sent by the QC code), it won't work right.

In vanilla Quake, inputting "alias foo" without any other parameters is the default for erasing an alias, so the QC code can create an alias, use it, and erase it afterwards.

Console input should usually be taken care of in keys.c. Implementing this functionality in the Key_Console function would also allow you to activate it through the Tab key instead, which would preserve the user's ability to erase aliases from the console.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
r00k
Posts: 1111
Joined: Sat Nov 13, 2004 10:39 pm

Re: alias cmd with edit

Post by r00k »

In the last 3 years (since i added this) with my mods stuffing alias commands, i havent seen ANY issues.
this code basically acts like normal;unless there is only 1 parameter, the alias name itself.

Can you get a realtime bug from this? I havent had any probs yet.


EDIT: i re-read your message. I m sorry i over looked the factr that alias apon empty alias erased such
as i use unalias for that. I dont know of mods that erase alias per alias. :(


It was merely for user input convenience, you can edit or dis for your needs.
Post Reply