[mw-devel] [Git][arthur/mw][master] Tidy up some alias list-related clutter

Andrew Price welshbyte at sucs.org
Sun Nov 8 23:57:26 GMT 2015


Andrew Price pushed to branch master at Justin Mitchell / mw


Commits:
a4e85793 by Andrew Price at 2015-11-08T23:56:19Z
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.

- - - - -


11 changed files:

- src/client/Parse.c
- src/client/alias.c
- src/client/alias.h
- src/client/incoming.c
- src/client/init.c
- src/client/js.c
- src/client/main.c
- src/client/newmain.c
- src/client/script.c
- src/client/script_inst.c
- src/client/talker.c


Changes:

=====================================
src/client/Parse.c
=====================================
--- a/src/client/Parse.c
+++ b/src/client/Parse.c
@@ -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;


=====================================
src/client/alias.c
=====================================
--- a/src/client/alias.c
+++ b/src/client/alias.c
@@ -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;
-
-	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);
-		 	break;
-		}
-		prev = aptr;
-		aptr = aptr->next;
+	alias **prevnext = list;
+	alias *entry = *list;
+
+	while (entry != NULL) {
+		if (!strcasecmp(entry->from, name))
+			break;
+		 prevnext = &entry->next;
+		 entry = entry->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;
+	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));
+	while (al != NULL && strcasecmp(from, al->from))
+		al = al->next;
+
+	if (al == NULL)
+		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;


=====================================
src/client/alias.h
=====================================
--- a/src/client/alias.h
+++ b/src/client/alias.h
@@ -1,20 +1,29 @@
 #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 */


=====================================
src/client/incoming.c
=====================================
--- a/src/client/incoming.c
+++ b/src/client/incoming.c
@@ -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;


=====================================
src/client/init.c
=====================================
--- a/src/client/init.c
+++ b/src/client/init.c
@@ -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"
 


=====================================
src/client/js.c
=====================================
--- a/src/client/js.c
+++ b/src/client/js.c
@@ -38,16 +38,6 @@
 #include "user.h"
 #include "sqlite.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;
-extern Alias eventin_list;
-
 extern struct user * const user;
 extern unsigned long rights;
 extern int busy;


=====================================
src/client/main.c
=====================================
--- a/src/client/main.c
+++ b/src/client/main.c
@@ -50,15 +50,6 @@
 #include "userio.h"
 #include "who.h"
 #include "alias.h"
-extern Alias bind_list;
-extern Alias alias_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;
 
 static char version[]="Milliways III - Release "VER_MAJ"."VER_MIN"."VER_TWK"\n";
 
@@ -1461,8 +1452,6 @@ char **complete_entry(const char *text, int start, int end)
 		{
 			script=script->next;
 		}
-
-		/* remove the memory used by funcname */
 		free(funcname);
 
 		/* check the bound function exists */


=====================================
src/client/newmain.c
=====================================
--- a/src/client/newmain.c
+++ b/src/client/newmain.c
@@ -39,8 +39,6 @@
 #include "incoming.h"
 #include <util.h>
 
-extern Alias alias_list;
-
 extern int currentfolder,last_mesg;
 extern FILE *output;
 extern int busy; /* if true dont display messages  i.e. during new/write */


=====================================
src/client/script.c
=====================================
--- a/src/client/script.c
+++ b/src/client/script.c
@@ -25,8 +25,6 @@
 #include "who.h"
 #include <util.h>
 
-extern Alias bind_list;
-
 #define MAX_ARGC 128
 
 extern struct user * const user;


=====================================
src/client/script_inst.c
=====================================
--- a/src/client/script_inst.c
+++ b/src/client/script_inst.c
@@ -29,8 +29,6 @@
 #include "who.h"
 
 extern struct user * const user;
-extern Alias alias_list;
-extern Alias bind_list;
 /* current script runaway variable */
 extern long runaway;
 


=====================================
src/client/talker.c
=====================================
--- a/src/client/talker.c
+++ b/src/client/talker.c
@@ -33,16 +33,7 @@
 #include "who.h"
 #include "mesg.h"
 #include "incoming.h"
-
 #include "alias.h"
-extern Alias bind_list;
-extern Alias alias_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 int busy; /* if true dont display messages  i.e. during new/write */
 extern unsigned long rights;



View it on GitLab: https://projects.sucs.org/arthur/mw/commit/a4e857931e30b695efe0ed2fa85934c19d6d9e8d
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20151108/580cf975/attachment-0001.html>


More information about the mw-devel mailing list