Commit a4e85793 authored by Andrew Price's avatar Andrew Price
Browse files

Tidy up some alias list-related clutter

Also make the alias type not-a-pointer to improve readability and
allocate all of the memory required for an alias in one go.
parent 54027aa0
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -8,14 +8,12 @@
#include "user.h"
#include "talker_privs.h"
#include "userio.h"
#include "alias.h"

extern unsigned long rights;
extern CommandList chattable[];
extern struct user * const user;

#include "alias.h"
extern Alias alias_list;

static void shuffle_up(char *bp)
{
	/* The strlen is right its the length of bp, which is the length
@@ -108,7 +106,7 @@ int DoCommand(char *input, CommandList *cm)
	CommandList *found=NULL, *exact=NULL, *backup;
	int count=0, ecount=0;
	int c;
	Alias al;
	alias *al;
	const char *dowhat;
	char *text;
	char *args = NULL, *ptr, *p2, *tmp;
+53 −81
Original line number Diff line number Diff line
@@ -11,77 +11,52 @@
#include "userio.h"

#include "alias.h"
Alias alias_list=NULL;
Alias bind_list=NULL;
Alias rpc_list=NULL;
Alias event_list=NULL;
Alias onoff_list=NULL;
Alias ipc_list=NULL;
Alias force_list=NULL;
Alias shutdown_list=NULL;
Alias eventin_list=NULL;
alias *alias_list = NULL;
alias *bind_list = NULL;
alias *rpc_list = NULL;
alias *event_list = NULL;
alias *onoff_list = NULL;
alias *ipc_list = NULL;
alias *force_list = NULL;
alias *shutdown_list = NULL;
alias *eventin_list = NULL;

/* completely destroys the given list */
void DestroyAllLinks(Alias *list)
void DestroyAllLinks(alias **list)
{
	Alias free_me;

	while (*list!=NULL)
	{
		free((*list)->from);
		free((*list)->to);
		free_me = *list;
		*list = (*list)->next;
		free(free_me);
	for (alias *node = *list; node != NULL; node = *list) {
		*list = node->next;
		free(node);
	}
}

/* removes the link given by 'name' in the list */
int DestroyLink(Alias *list, const char *name)
int DestroyLink(alias **list, const char *name)
{
 	Alias prev, aptr;
	int found = 0;
	alias **prevnext = list;
	alias *entry = *list;

	aptr = *list;
	prev=NULL;
	while (aptr!=NULL)
	{
		if (!strcasecmp(aptr->from, name))
		{
			found = 1;
		 	free(aptr->from);
		 	free(aptr->to);
		 	if (prev == NULL)
		 	{
		 		/* first */
		 		*list = aptr->next;
		 	}
		 	else if (aptr->next == NULL)
		 	{
		 		/* last */
		 		prev->next = NULL;
		 	}
		 	else
		 	{
		 		/* middle */
		 		prev->next = aptr->next;
		 	}
		 	free(aptr);
	while (entry != NULL) {
		if (!strcasecmp(entry->from, name))
			break;
		 prevnext = &entry->next;
		 entry = entry->next;
	}
		prev = aptr;
		aptr = aptr->next;
	}
	return(found);
	if (entry == NULL)
		return 0; /* Not found or empty */

	*prevnext = entry->next;
	free(entry);
	return 1;
}

/* adds the 'from/to' node to the given list, or redefines if in existance */
int AddLink(Alias *list, const char *from, const char *to)
int AddLink(alias **list, const char *from, const char *to)
{
	Alias new;
	alias *new = *list;
	int redefine = 0;
	size_t size, fromsize;

	new=*list;
	while (new!=NULL && strcasecmp(new->from, from))
	 	new=new->next;

@@ -91,22 +66,24 @@ int AddLink(Alias *list, const char *from, const char *to)
		redefine = 1;
	}

	new=(Alias)malloc(sizeof(struct alias_record));
	new->from=malloc(sizeof(char) * (strlen(from) + 1));
	new->to=malloc(sizeof(char) * (strlen(to) + 1));
	fromsize = strlen(from) + 1;
	size = sizeof(*new) + (strlen(to) + 1) + fromsize;
	new = calloc(1, size);
	new->from = (char *)(new + 1);
	new->to = new->from + fromsize;
	strcpy(new->from,from);
	strcpy(new->to,to);
	new->next = *list;
	*list = new;

	return (redefine);
	return redefine;
}

/* displays the list */
void ShowLinks(Alias list, const char *prompt, const char *link, int count)
void ShowLinks(alias *list, const char *prompt, const char *link, int count)
{
 	char	buff[10];
 	Alias	al;
	alias *al;
 	int	max;
 	int	screen_height = screen_h();

@@ -159,25 +136,23 @@ void ShowLinks(Alias list, const char *prompt, const char *link, int count)
	}
}

char *FindLinks(Alias list, const char *from)
char *FindLinks(alias *list, const char *from)
{
	Alias al;
	alias *al = list;

	al = list;
	while (al != NULL && strcasecmp(from, al->from))
 	{
		al = al->next;
	}

	if (al == NULL)
		return(NULL);
	else
		return(strdup(al->to));
		return NULL;

	return strdup(al->to);
}

/* bind listing autocompletion for scripts and tab-completion */
char *list_bind(const char *text, int state)
{
	static Alias	bind;
	static alias *bind;
	static int	len;
	char		*name;

@@ -201,7 +176,7 @@ char *list_bind(const char *text, int state)
/* bind listing autocompletion for readline */
char *list_bind_rl(const char *text, int state)
{
	static Alias	bind;
	static alias *bind;
	static int	len;
	char		*name;

@@ -224,11 +199,8 @@ char *list_bind_rl(const char *text, int state)
	return NULL;
}

char *NextLink(Alias list, char *prev)
char *NextLink(alias *al, char *prev)
{
	Alias al;

	al = list;
	if (prev)
	{
		while(al && strcasecmp(al->from, prev)) al = al->next;
+19 −10
Original line number Diff line number Diff line
#ifndef ALIAS_H
#define ALIAS_H

typedef struct alias_record
{
typedef struct alias {
	char *from;
	char *to;
	struct alias_record *next;
} *Alias;
	struct alias *next;
} alias;

void DestroyAllLinks(Alias *list);
int DestroyLink(Alias *list, const char *name);
int AddLink(Alias *list, const char *from, const char *to);
void ShowLinks(Alias list, const char *prompt, const char *link, int count);
char *FindLinks(Alias list, const char *from);
void DestroyAllLinks(alias **list);
int DestroyLink(alias **list, const char *name);
int AddLink(alias **list, const char *from, const char *to);
void ShowLinks(alias *list, const char *prompt, const char *link, int count);
char *FindLinks(alias *list, const char *from);
char *list_bind(const char *text, int state);
char *list_bind_rl(const char *text, int state);
char *NextLink(Alias list, char *prev);
char *NextLink(alias *list, char *prev);

extern alias *alias_list;
extern alias *bind_list;
extern alias *rpc_list;
extern alias *event_list;
extern alias *onoff_list;
extern alias *ipc_list;
extern alias *force_list;
extern alias *shutdown_list;
extern alias *eventin_list;

#endif /* ALIAS_H */
+0 −4
Original line number Diff line number Diff line
@@ -31,10 +31,6 @@
#include <jansson.h>
#include <str_util.h>

extern Alias rpc_list;
extern Alias event_list;
extern Alias onoff_list;
extern Alias ipc_list;
extern struct user * const user;
extern int quietmode;
extern ipc_connection_t *ipcsock;
+0 −10
Original line number Diff line number Diff line
@@ -19,16 +19,6 @@
#include "js.h"
#include "init.h"
#include "user.h"

extern Alias alias_list;
extern Alias bind_list;
extern Alias rpc_list;
extern Alias event_list;
extern Alias onoff_list;
extern Alias ipc_list;
extern Alias force_list;
extern Alias shutdown_list;

#include "script.h"
#include "frl.h"

Loading