rework alias creation/deletion. Aliases are now sorted (like cvars and

commands), and the alias name length limit has been removed.
This commit is contained in:
Bill Currie 2001-07-06 17:45:32 +00:00
parent 6cda415f06
commit 124dff1bd6

View file

@ -50,11 +50,9 @@
#include "QF/vfs.h" #include "QF/vfs.h"
#include "QF/zone.h" #include "QF/zone.h"
#define MAX_ALIAS_NAME 32
typedef struct cmdalias_s { typedef struct cmdalias_s {
struct cmdalias_s *next; struct cmdalias_s *next;
char name[MAX_ALIAS_NAME]; char *name;
char *value; char *value;
} cmdalias_t; } cmdalias_t;
@ -380,54 +378,54 @@ CopyString (char *in)
void void
Cmd_Alias_f (void) Cmd_Alias_f (void)
{ {
cmdalias_t *a; cmdalias_t *alias;
char cmd[1024]; char *cmd;
int i, c; int i, c;
char *s; char *s;
if (Cmd_Argc () == 1) { if (Cmd_Argc () == 1) {
Con_Printf ("Current alias commands:\n"); Con_Printf ("Current alias commands:\n");
for (a = cmd_alias; a; a = a->next) for (alias = cmd_alias; alias; alias = alias->next)
Con_Printf ("%s : %s\n", a->name, a->value); Con_Printf ("%s : %s\n", alias->name, alias->value);
return; return;
} }
s = Cmd_Argv (1); s = Cmd_Argv (1);
if (strlen (s) >= MAX_ALIAS_NAME) {
Con_Printf ("Alias name is too long\n");
return;
}
// if the alias already exists, reuse it // if the alias already exists, reuse it
a = (cmdalias_t*)Hash_Find (cmd_alias_hash, s); alias = (cmdalias_t*)Hash_Find (cmd_alias_hash, s);
if (a) { if (alias) {
free (a->value); free (alias->value);
} } else {
cmdalias_t **a;
if (!a) { alias = calloc (1, sizeof (cmdalias_t));
a = calloc (1, sizeof (cmdalias_t)); alias->name = strdup (s);
a->next = cmd_alias; Hash_Add (cmd_alias_hash, alias);
cmd_alias = a; for (a = &cmd_alias; *a; a = &(*a)->next)
strcpy (a->name, s); if (strcmp ((*a)->name, alias->name) >=0)
Hash_Add (cmd_alias_hash, a); break;
alias->next = *a;
*a = alias;
} }
// copy the rest of the command line // copy the rest of the command line
cmd = malloc (strlen (Cmd_Args ()) + 2);// can never be longer
cmd[0] = 0; // start out with a null string cmd[0] = 0; // start out with a null string
c = Cmd_Argc (); c = Cmd_Argc ();
for (i = 2; i < c; i++) { for (i = 2; i < c; i++) {
strncat (cmd, Cmd_Argv (i), sizeof (cmd) - strlen (cmd)); strcat (cmd, Cmd_Argv (i));
if (i != c) if (i != c - 1)
strncat (cmd, " ", sizeof (cmd) - strlen (cmd)); strcat (cmd, " ");
} }
strncat (cmd, "\n", sizeof (cmd) - strlen (cmd)); strncat (cmd, "\n", sizeof (cmd) - strlen (cmd));
a->value = CopyString (cmd); alias->value = cmd;
} }
void void
Cmd_UnAlias_f (void) Cmd_UnAlias_f (void)
{ {
cmdalias_t *a, *prev; cmdalias_t *alias;
char *s; char *s;
if (Cmd_Argc () != 2) { if (Cmd_Argc () != 2) {
@ -436,25 +434,21 @@ Cmd_UnAlias_f (void)
} }
s = Cmd_Argv (1); s = Cmd_Argv (1);
if (strlen (s) >= MAX_ALIAS_NAME) { alias = Hash_Del (cmd_alias_hash, s);
Con_Printf ("Alias name is too long\n");
return;
}
prev = cmd_alias; if (alias) {
for (a = cmd_alias; a; a = a->next) { cmdalias_t **a;
if (!strcmp (s, a->name)) {
Hash_Del (cmd_alias_hash, s); for (a = &cmd_alias; *a != alias; a = &(*a)->next)
free (a->value); ;
prev->next = a->next; *a = alias->next;
if (a == cmd_alias)
cmd_alias = a->next; free (alias->name);
free (a); free (alias->value);
return; free (alias);
} } else {
prev = a; Con_Printf ("Unknown alias \"%s\"\n", s);
} }
Con_Printf ("Unknown alias \"%s\"\n", s);
} }
/* /*
@ -721,8 +715,8 @@ Cmd_CompleteBuildList (char *partial)
Thanks to taniwha Thanks to taniwha
*/ */
char char *
*Cmd_CompleteAlias (char * partial) Cmd_CompleteAlias (char * partial)
{ {
cmdalias_t *alias; cmdalias_t *alias;
int len; int len;
@ -967,6 +961,7 @@ static void
cmd_alias_free (void *_a, void *unused) cmd_alias_free (void *_a, void *unused)
{ {
cmdalias_t *a = (cmdalias_t*)_a; cmdalias_t *a = (cmdalias_t*)_a;
free (a->name);
free (a->value); free (a->value);
free (a); free (a);
} }