From ac52abea6b304ee2b7ac4a9718f63fb683adab2b Mon Sep 17 00:00:00 2001 From: Epoch Qwert Date: Sat, 8 Nov 2014 00:28:58 -0600 Subject: added some deletion stuff to libhashtable and made segfault have a couple function to delete his two hashtables and reinitialize them. they aren't builtins as default but can be added with !builtin. :) --- libhashtable/hashtable.h | 14 +++++++---- libhashtable/libhashtable.c | 57 +++++++++++++++++++++++++++++++++++++++------ segfault.c | 43 ++++++++++++++++++++++------------ 3 files changed, 87 insertions(+), 27 deletions(-) diff --git a/libhashtable/hashtable.h b/libhashtable/hashtable.h index f0e2a15..a58a392 100644 --- a/libhashtable/hashtable.h +++ b/libhashtable/hashtable.h @@ -1,21 +1,25 @@ -struct entry {/*linked list node.*/ +struct entry {//linked list node. char *original; void *target; - struct entry *prev;/* doubly linked list. why? */ + struct entry *prev;// doubly linked list. why? struct entry *next; }; -struct hitem {/*dunno why I don't just have this as a linked list. */ +struct hitem { struct entry *ll; }; struct hashtable { - int kl;/*number of keys in the table*/ + int kl;//number of keys in the table struct hitem **bucket; int *keys; }; -unsigned short hash(char *key);/*maybe use a seeded rand()? :) Thanks FreeArtMan*/ +unsigned short hash(char *key);//maybe use a seeded rand()? :) Thanks FreeArtMan void inittable(struct hashtable *ht,int tsize); +void ll_delete(struct entry *ll); +void ll_destroy(struct entry *ll); +void ht_destroy(struct hashtable *ht); +void ht_freevalues(struct hashtable *ht); int ht_setkey(struct hashtable *ht,char *key,void *value); struct entry *ll_getentry(struct entry *start,char *msg); struct entry *ht_getentry(struct hashtable *ht,char *key); diff --git a/libhashtable/libhashtable.c b/libhashtable/libhashtable.c index e89d0cd..e304a68 100644 --- a/libhashtable/libhashtable.c +++ b/libhashtable/libhashtable.c @@ -11,7 +11,7 @@ struct entry {//linked list node. struct entry *next; }; -struct hitem {//dunno why I don't just have this as a linked list. +struct hitem { struct entry *ll; }; @@ -40,6 +40,52 @@ void inittable(struct hashtable *ht,int tsize) { } } +void ll_delete(struct entry *ll) { + //do I have to free() any of this shit? + //keys are always allocated. strdup(). + //gotta free that. anything else isn't my fault. + if(ll->prev) { + ll->prev->next=ll->next; + } else { + //I am the first node. + } + if(ll->next) { + ll->next->prev=ll->prev; + } else { + //I am the last node. + } + free(ll);//all these nodes are malloc()d. +} + +void ll_destroy(struct entry *ll) { + if(ll->next) ll_destroy(ll->next); + free(ll->original); + free(ll); + //destroy_this_node. + //ll->original //malloc()d + //ll->target //I dunno where this comes from. +} + +void ht_destroy(struct hashtable *ht) { + int i=0; + for(i=0;ikl;i++) { + ll_destroy(ht->bucket[ht->keys[i]]->ll); + } + free(ht->bucket); +} + +void ll_freevalues(struct entry *ll) {//only use if you malloc your table. + if(ll->next) ll_destroy(ll->next); + free(ll->target); +} + +void ht_freevalues(struct hashtable *ht) { + int i; + for(i=0;ikl;i++) { + ll_freevalues(ht->bucket[ht->keys[i]]->ll); + } +} + //this seems too complicated. int ht_setkey(struct hashtable *ht,char *key,void *value) { unsigned short h=hash(key); @@ -59,10 +105,7 @@ int ht_setkey(struct hashtable *ht,char *key,void *value) { //don't bother with the new ll entry yet... } if((tmp=ll_getentry(ht->bucket[h]->ll,key)) != NULL) { - //we found this alias in the ll. now to replace the value - //free(tmp->target);//WHY? no. - //if(!(tmp->target=strdup(value))) return 2; - tmp->target=value;//can't strdup for non-strings. do it yourself. + tmp->target=value; return 0; } if(ht->bucket[h]->ll == NULL) { @@ -70,7 +113,6 @@ int ht_setkey(struct hashtable *ht,char *key,void *value) { ht->bucket[h]->ll->next=0; ht->bucket[h]->ll->prev=0; if(!(ht->bucket[h]->ll->original=strdup(key))) return 4; - //if(!(ht->bucket[h]->ll->target=strdup(value))) return 5; ht->bucket[h]->ll->target=value; } else { //go to the end and add another entry to the ll. @@ -79,7 +121,6 @@ int ht_setkey(struct hashtable *ht,char *key,void *value) { tmp->next->prev=tmp; tmp=tmp->next; if(!(tmp->original=strdup(key))) return 7; - //if(!(tmp->target=strdup(value))) return 8; tmp->target=value; tmp->next=0; } @@ -98,12 +139,14 @@ struct entry *ll_getentry(struct entry *start,char *msg) { return NULL; } +//returns the linked list at the key. struct entry *ht_getentry(struct hashtable *ht,char *key) { unsigned short h=hash(key); if(!ht->bucket[h]) return NULL; return ht->bucket[h]->ll; } +//returns the node struct entry *ht_getnode(struct hashtable *ht,char *msg) { return ll_getentry(ht_getentry(ht,msg),msg); } diff --git a/segfault.c b/segfault.c index 2979de6..41fd2ea 100644 --- a/segfault.c +++ b/segfault.c @@ -83,7 +83,7 @@ void (*func)(int fd,...); struct user { char *nick; - char *name; + char *user; char *host; }; @@ -192,8 +192,8 @@ char *format_magic(int fd,char *from,struct user *user,char *orig_fmt,char *arg) i++; switch(fmt[i]) { case '~':case 'p':case 'n':case 'h':case 'u':case 'f':case 's':case 'm':case '%'://here. - args[c]=((fmt[i]=='u')?user->nick: - ((fmt[i]=='n')?user->name: + args[c]=((fmt[i]=='n')?user->nick: + ((fmt[i]=='u')?user->user: ((fmt[i]=='~')?getcwd(seghome,SEGHOMELEN): ((fmt[i]=='h')?user->host: ((fmt[i]=='f')?from: @@ -376,7 +376,7 @@ void file_tail(int fd,char *from,char *file,char *args,char opt,struct user *use tailf[i].inode=st.st_ino; tailf[i].user=malloc(sizeof(struct user)); tailf[i].user->nick=strdup(user->nick); - tailf[i].user->name=strdup(user->name); + tailf[i].user->user=strdup(user->user); tailf[i].user->host=strdup(user->host); if(!tailf[i].user) { mywrite(fd,"QUIT :malloc error 4.5!!! (a strdup again)\r\n"); @@ -532,6 +532,19 @@ void c_builtins(int fd,char *from,char *line,...) { privmsg(fd,from,tmp); } +void c_amnesia(int fd,char *from,char *line,...) {//forget aliases + ht_freevalues(&alias); + ht_destroy(&alias); + inittable(&alias,TSIZE); + //put this as a builtin I guess. +} + +void c_lobotomy(int fd,char *from,char *line,...) {//forget builtins + ht_destroy(&builtin); + inittable(&builtin,TSIZE); + //don't put this as a builtin by default. :P gotta hack that out. +} + void c_aliases_h(int fd,char *from,char *line,...) { char tmp[512]; struct entry *m; @@ -978,16 +991,16 @@ void line_handler(int fd,char *line) {//this should be built into the libary? char *s=line,*t=0,*u=0; struct user *user=malloc(sizeof(struct user)); user->nick=0; - user->name=0; + user->user=0; user->host=0; if(strchr(line,'\r')) *strchr(line,'\r')=0; if(strchr(line,'\n')) *strchr(line,'\n')=0; printf("line: '%s'\n",line); - //:nick!name@host MERP DERP :message - //:nick!name@host s t :u + //:nick!user@host MERP DERP :message + //:nick!user@host s t :u //:armitage.hacking.allowed.org MERP DERP :message //:nickhost s t :u - //only sub-parse nicknamehost stuff if starts with : + //only sub-parse nickuserhost stuff if starts with : //strchr doesn't like null pointers. :/ why not just take them and return null? //check that I haven't gone past the end of the string? nah. it should take care of itself. if(recording_raw) { @@ -1014,10 +1027,10 @@ void line_handler(int fd,char *line) {//this should be built into the libary? } } } - if(((user->name)=strchr((user->nick),'!'))) { - *(user->name)=0; - (user->name)++; - if(((user->host)=strchr((user->name),'@'))) { + if(((user->user)=strchr((user->nick),'!'))) { + *(user->user)=0; + (user->user)++; + if(((user->host)=strchr((user->user),'@'))) { *(user->host)=0; (user->host)++; } @@ -1029,10 +1042,10 @@ void line_handler(int fd,char *line) {//this should be built into the libary? printf("<%s!%s@%s> '%s' '%s' '%s'\n", user->nick, - user->name, + user->user, user->host, s,t,u); - if(!user->name && s) { //server message + if(!user->user && s) { //server message if(!strcmp(s,"433")) {//nick already used. srand(time(NULL)*getpid()); myuser->nick[strlen(myuser->nick)-3]=(rand()%10)+'0'; @@ -1128,7 +1141,7 @@ int main(int argc,char *argv[]) { } myuser=malloc(sizeof(struct user)); myuser->nick=strdup(argc>1?argv[1]:NICK); - myuser->name="I_dunno"; + myuser->user="I_dunno"; myuser->host="I_dunno"; snprintf(pid,6,"%d",getpid()); printf("starting segfault...\n"); -- cgit v1.2.3