Project 'arthur/mw' was moved to 'milliways/mw'. Please update any links and bookmarks that may still have the old path.
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
...@@ -8,14 +8,12 @@ ...@@ -8,14 +8,12 @@
#include "user.h" #include "user.h"
#include "talker_privs.h" #include "talker_privs.h"
#include "userio.h" #include "userio.h"
#include "alias.h"
extern unsigned long rights; extern unsigned long rights;
extern CommandList chattable[]; extern CommandList chattable[];
extern struct user * const user; extern struct user * const user;
#include "alias.h"
extern Alias alias_list;
static void shuffle_up(char *bp) static void shuffle_up(char *bp)
{ {
/* The strlen is right its the length of bp, which is the length /* The strlen is right its the length of bp, which is the length
...@@ -108,7 +106,7 @@ int DoCommand(char *input, CommandList *cm) ...@@ -108,7 +106,7 @@ int DoCommand(char *input, CommandList *cm)
CommandList *found=NULL, *exact=NULL, *backup; CommandList *found=NULL, *exact=NULL, *backup;
int count=0, ecount=0; int count=0, ecount=0;
int c; int c;
Alias al; alias *al;
const char *dowhat; const char *dowhat;
char *text; char *text;
char *args = NULL, *ptr, *p2, *tmp; char *args = NULL, *ptr, *p2, *tmp;
......
...@@ -11,77 +11,52 @@ ...@@ -11,77 +11,52 @@
#include "userio.h" #include "userio.h"
#include "alias.h" #include "alias.h"
Alias alias_list=NULL; alias *alias_list = NULL;
Alias bind_list=NULL; alias *bind_list = NULL;
Alias rpc_list=NULL; alias *rpc_list = NULL;
Alias event_list=NULL; alias *event_list = NULL;
Alias onoff_list=NULL; alias *onoff_list = NULL;
Alias ipc_list=NULL; alias *ipc_list = NULL;
Alias force_list=NULL; alias *force_list = NULL;
Alias shutdown_list=NULL; alias *shutdown_list = NULL;
Alias eventin_list=NULL; alias *eventin_list = NULL;
/* completely destroys the given list */ /* completely destroys the given list */
void DestroyAllLinks(Alias *list) void DestroyAllLinks(alias **list)
{ {
Alias free_me; for (alias *node = *list; node != NULL; node = *list) {
*list = node->next;
while (*list!=NULL) free(node);
{
free((*list)->from);
free((*list)->to);
free_me = *list;
*list = (*list)->next;
free(free_me);
} }
} }
/* removes the link given by 'name' in the list */ /* 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; alias **prevnext = list;
int found = 0; alias *entry = *list;
aptr = *list; while (entry != NULL) {
prev=NULL; if (!strcasecmp(entry->from, name))
while (aptr!=NULL) break;
{ prevnext = &entry->next;
if (!strcasecmp(aptr->from, name)) entry = entry->next;
{
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;
} }
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 */ /* 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; int redefine = 0;
size_t size, fromsize;
new=*list;
while (new!=NULL && strcasecmp(new->from, from)) while (new!=NULL && strcasecmp(new->from, from))
new=new->next; new=new->next;
...@@ -91,22 +66,24 @@ int AddLink(Alias *list, const char *from, const char *to) ...@@ -91,22 +66,24 @@ int AddLink(Alias *list, const char *from, const char *to)
redefine = 1; redefine = 1;
} }
new=(Alias)malloc(sizeof(struct alias_record)); fromsize = strlen(from) + 1;
new->from=malloc(sizeof(char) * (strlen(from) + 1)); size = sizeof(*new) + (strlen(to) + 1) + fromsize;
new->to=malloc(sizeof(char) * (strlen(to) + 1)); new = calloc(1, size);
new->from = (char *)(new + 1);
new->to = new->from + fromsize;
strcpy(new->from,from); strcpy(new->from,from);
strcpy(new->to,to); strcpy(new->to,to);
new->next=*list; new->next = *list;
*list=new; *list = new;
return (redefine); return redefine;
} }
/* displays the list */ /* 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]; char buff[10];
Alias al; alias *al;
int max; int max;
int screen_height = screen_h(); int screen_height = screen_h();
...@@ -159,25 +136,23 @@ void ShowLinks(Alias list, const char *prompt, const char *link, int count) ...@@ -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))
while (al!=NULL && strcasecmp(from, al->from)) al = al->next;
{
al=al->next; if (al == NULL)
} return NULL;
if (al==NULL)
return(NULL); return strdup(al->to);
else
return(strdup(al->to));
} }
/* bind listing autocompletion for scripts and tab-completion */ /* bind listing autocompletion for scripts and tab-completion */
char *list_bind(const char *text, int state) char *list_bind(const char *text, int state)
{ {
static Alias bind; static alias *bind;
static int len; static int len;
char *name; char *name;
...@@ -201,7 +176,7 @@ char *list_bind(const char *text, int state) ...@@ -201,7 +176,7 @@ char *list_bind(const char *text, int state)
/* bind listing autocompletion for readline */ /* bind listing autocompletion for readline */
char *list_bind_rl(const char *text, int state) char *list_bind_rl(const char *text, int state)
{ {
static Alias bind; static alias *bind;
static int len; static int len;
char *name; char *name;
...@@ -224,11 +199,8 @@ char *list_bind_rl(const char *text, int state) ...@@ -224,11 +199,8 @@ char *list_bind_rl(const char *text, int state)
return NULL; return NULL;
} }
char *NextLink(Alias list, char *prev) char *NextLink(alias *al, char *prev)
{ {
Alias al;
al = list;
if (prev) if (prev)
{ {
while(al && strcasecmp(al->from, prev)) al = al->next; while(al && strcasecmp(al->from, prev)) al = al->next;
......
#ifndef ALIAS_H #ifndef ALIAS_H
#define ALIAS_H #define ALIAS_H
typedef struct alias_record typedef struct alias {
{
char *from; char *from;
char *to; char *to;
struct alias_record *next; struct alias *next;
} *Alias; } alias;
void DestroyAllLinks(Alias *list); void DestroyAllLinks(alias **list);
int DestroyLink(Alias *list, const char *name); int DestroyLink(alias **list, const char *name);
int AddLink(Alias *list, const char *from, const char *to); int AddLink(alias **list, const char *from, const char *to);
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 *FindLinks(Alias list, const char *from); char *FindLinks(alias *list, const char *from);
char *list_bind(const char *text, int state); char *list_bind(const char *text, int state);
char *list_bind_rl(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 */ #endif /* ALIAS_H */
...@@ -31,10 +31,6 @@ ...@@ -31,10 +31,6 @@
#include <jansson.h> #include <jansson.h>
#include <str_util.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 struct user * const user;
extern int quietmode; extern int quietmode;
extern ipc_connection_t *ipcsock; extern ipc_connection_t *ipcsock;
......
...@@ -19,16 +19,6 @@ ...@@ -19,16 +19,6 @@
#include "js.h" #include "js.h"
#include "init.h" #include "init.h"
#include "user.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 "script.h"
#include "frl.h" #include "frl.h"
......
...@@ -38,16 +38,6 @@ ...@@ -38,16 +38,6 @@
#include "user.h" #include "user.h"
#include "sqlite.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 struct user * const user;
extern unsigned long rights; extern unsigned long rights;
extern int busy; extern int busy;
......
...@@ -50,15 +50,6 @@ ...@@ -50,15 +50,6 @@
#include "userio.h" #include "userio.h"
#include "who.h" #include "who.h"
#include "alias.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"; 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) ...@@ -1461,8 +1452,6 @@ char **complete_entry(const char *text, int start, int end)
{ {
script=script->next; script=script->next;
} }
/* remove the memory used by funcname */
free(funcname); free(funcname);
/* check the bound function exists */ /* check the bound function exists */
......
...@@ -39,8 +39,6 @@ ...@@ -39,8 +39,6 @@
#include "incoming.h" #include "incoming.h"
#include <util.h> #include <util.h>
extern Alias alias_list;
extern int currentfolder,last_mesg; extern int currentfolder,last_mesg;
extern FILE *output; extern FILE *output;
extern int busy; /* if true dont display messages i.e. during new/write */ extern int busy; /* if true dont display messages i.e. during new/write */
......
...@@ -25,8 +25,6 @@ ...@@ -25,8 +25,6 @@
#include "who.h" #include "who.h"
#include <util.h> #include <util.h>
extern Alias bind_list;
#define MAX_ARGC 128 #define MAX_ARGC 128
extern struct user * const user; extern struct user * const user;
......
...@@ -29,8 +29,6 @@ ...@@ -29,8 +29,6 @@
#include "who.h" #include "who.h"
extern struct user * const user; extern struct user * const user;
extern Alias alias_list;
extern Alias bind_list;
/* current script runaway variable */ /* current script runaway variable */
extern long runaway; extern long runaway;
......
...@@ -33,16 +33,7 @@ ...@@ -33,16 +33,7 @@
#include "who.h" #include "who.h"
#include "mesg.h" #include "mesg.h"
#include "incoming.h" #include "incoming.h"
#include "alias.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 int busy; /* if true dont display messages i.e. during new/write */
extern unsigned long rights; extern unsigned long rights;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment