From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2441 invoked by alias); 12 Nov 2013 20:13:43 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 31961 Received: (qmail 21717 invoked from network); 12 Nov 2013 20:12:21 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=XSGHNzFjUsj01eqQUyvCO0Za3d8EjM5F8hjMHES7GcE=; b=eEJ1hA1Z2/Aeagy7Y4kChShbPI0Z8GJfjUMpN77WnilEf9qQ/ayzVzX6ZV4/GOH7xP olISZk3wtV20caTa8nhBSVW0eaesG8sD/SbgDL2f0Sr0GnkF9n/RuKTpBI5s78yKN4mi Ilmz2ZWJE1Ommmob5SWrLVXcOsKGjY31OBLJCYE/c3+nbPQgOlJr4oj75zAmk67w9h02 3TpG5+QBi8xP2Xu/Y9b/RIpRhpqE5uvC6AjMy6bc2f7fYfvN0jBNkGuaRwYL0XM87zbN gs0c4+w5+VGQtEmkY43Z+A2X6yzoc0YDBuj2QESvKlQssrLGChiJjVaT8DHCOrIrw2qj vuUQ== X-Gm-Message-State: ALoCoQn3Pd223klQiNLKcj81ylFoGOe1RIb+JEdklv5m6HdJOzk32bCYIy3DKq10ygVh0fE9WdOS X-Received: by 10.194.176.163 with SMTP id cj3mr28210092wjc.8.1384285591887; Tue, 12 Nov 2013 11:46:31 -0800 (PST) X-ProxyUser-IP: 86.6.157.246 Date: Tue, 12 Nov 2013 19:46:26 +0000 From: Peter Stephenson To: Zsh Hackers' List Subject: Re: Fish-like autosuggestions Message-ID: <20131112194626.43f1ccf2@pws-pc.ntlworld.com> In-Reply-To: <20131106213344.3e9bea64@pws-pc.ntlworld.com> References: <131030092555.ZM8077@torch.brasslantern.com> <131105075700.ZM18043@torch.brasslantern.com> <20131105161858.543037da@pwslap01u.europe.root.pri> <131105114640.ZM18224@torch.brasslantern.com> <131105124000.ZM18277@torch.brasslantern.com> <20131106200715.6e549a6e@pws-pc.ntlworld.com> <20131106213344.3e9bea64@pws-pc.ntlworld.com> X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.7; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 6 Nov 2013 21:33:44 +0000 Peter Stephenson wrote: > I should really rewrite those three separate arrays to use an array of > structures. I hope this doesn't break anything. It does appear to fix one leak. diff --git a/Src/Zle/zle.h b/Src/Zle/zle.h index cf44b47..870e214 100644 --- a/Src/Zle/zle.h +++ b/Src/Zle/zle.h @@ -497,3 +497,15 @@ typedef REFRESH_ELEMENT *REFRESH_STRING; #define METACHECK() #define UNMETACHECK() #endif + + +typedef struct watch_fd *Watch_fd; + +struct watch_fd { + /* Function to call */ + char *func; + /* Watched fd */ + int fd; + /* 1 if func is called as a widget */ + int widget; +}; diff --git a/Src/Zle/zle_main.c b/Src/Zle/zle_main.c index 6822230..040b7cb 100644 --- a/Src/Zle/zle_main.c +++ b/Src/Zle/zle_main.c @@ -197,12 +197,11 @@ static int delayzsetterm; */ /**/ int nwatch; /* Number of fd's we are watching */ +/* + * Array of nwatch structures. + */ /**/ -int *watch_fds; /* The list of fds, not terminated! */ -/**/ -char **watch_funcs; /* The corresponding functions to call, normal array */ -/**/ -int *watch_widgets; /* 1 if corresponding function is called as widget */ +Watch_fd watch_fds; /* set up terminal */ @@ -588,7 +587,7 @@ raw_getbyte(long do_keytmout, char *cptr) */ fds[0].events = POLLIN; for (i = 0; i < nwatch; i++) { - fds[i+1].fd = watch_fds[i]; + fds[i+1].fd = watch_fds[i].fd; fds[i+1].events = POLLIN; } # endif @@ -613,7 +612,7 @@ raw_getbyte(long do_keytmout, char *cptr) FD_SET(SHTTY, &foofd); if (!errtry) { for (i = 0; i < nwatch; i++) { - int fd = watch_fds[i]; + int fd = watch_fds[i].fd; FD_SET(fd, &foofd); if (fd > fdmax) fdmax = fd; @@ -725,33 +724,33 @@ raw_getbyte(long do_keytmout, char *cptr) * handler function. */ int lnwatch = nwatch; - int *lwatch_fds = zalloc(lnwatch*sizeof(int)); - char **lwatch_funcs = zarrdup(watch_funcs); - int *lwatch_widgets = zalloc(lnwatch*sizeof(int)); - memcpy(lwatch_fds, watch_fds, lnwatch*sizeof(int)); - memcpy(lwatch_widgets, watch_widgets, lnwatch*sizeof(int)); + Watch_fd lwatch_fds = zalloc(lnwatch*sizeof(struct watch_fd)); + memcpy(lwatch_fds, watch_fds, lnwatch*sizeof(struct watch_fd)); + for (i = 0; i < lnwatch; i++) + lwatch_fds[i].func = ztrdup(lwatch_fds[i].func); for (i = 0; i < lnwatch; i++) { + Watch_fd lwatch_fd = lwatch_fds + i; if ( # ifdef HAVE_POLL (fds[i+1].revents & POLLIN) # else - FD_ISSET(lwatch_fds[i], &foofd) + FD_ISSET(lwatch_fd->fd, &foofd) # endif ) { /* Handle the fd. */ char *fdbuf; { char buf[BDIGBUFSIZE]; - convbase(buf, lwatch_fds[i], 10); + convbase(buf, lwatch_fd->fd, 10); fdbuf = ztrdup(buf); } - if (lwatch_widgets[i]) { - zlecallhook(lwatch_funcs[i], fdbuf); + if (lwatch_fd->widget) { + zlecallhook(lwatch_fd->func, fdbuf); zsfree(fdbuf); } else { LinkList funcargs = znewlinklist(); - zaddlinknode(funcargs, ztrdup(lwatch_funcs[i])); + zaddlinknode(funcargs, ztrdup(lwatch_fd->func)); zaddlinknode(funcargs, fdbuf); # ifdef HAVE_POLL # ifdef POLLERR @@ -767,7 +766,7 @@ raw_getbyte(long do_keytmout, char *cptr) zaddlinknode(funcargs, ztrdup("nval")); # endif # endif - callhookfunc(lwatch_funcs[i], funcargs, 0, NULL); + callhookfunc(lwatch_fd->func, funcargs, 0, NULL); freelinklist(funcargs, freestr); } if (errflag) { @@ -784,8 +783,9 @@ raw_getbyte(long do_keytmout, char *cptr) /* Function may have invalidated the display. */ if (resetneeded) zrefresh(); - zfree(lwatch_fds, lnwatch*sizeof(int)); - freearray(lwatch_funcs); + for (i = 0; i < lnwatch; i++) + zsfree(lwatch_fds[i].func); + zfree(lwatch_fds, lnwatch*sizeof(struct watch_fd)); } } # ifdef HAVE_POLL diff --git a/Src/Zle/zle_thingy.c b/Src/Zle/zle_thingy.c index 78c7918..7fd3a59 100644 --- a/Src/Zle/zle_thingy.c +++ b/Src/Zle/zle_thingy.c @@ -778,11 +778,12 @@ bin_zle_fd(char *name, char **args, Options ops, UNUSED(char func)) return 1; } for (i = 0; i < nwatch; i++) { - if (*args && watch_fds[i] != fd) + Watch_fd watch_fd = watch_fds + i; + if (*args && watch_fd->fd != fd) continue; found = 1; - printf("%s -F %s%d %s\n", name, watch_widgets[i] ? "-w " : "", - watch_fds[i], watch_funcs[i]); + printf("%s -F %s%d %s\n", name, watch_fd->widget ? "-w " : "", + watch_fd->fd, watch_fd->func); } /* only return status 1 if fd given and not found */ return *args && !found; @@ -793,10 +794,11 @@ bin_zle_fd(char *name, char **args, Options ops, UNUSED(char func)) char *funcnam = ztrdup(args[1]); if (nwatch) { for (i = 0; i < nwatch; i++) { - if (watch_fds[i] == fd) { - zsfree(watch_funcs[i]); - watch_funcs[i] = funcnam; - watch_widgets[i] = OPT_ISSET(ops,'w') ? 1 : 0; + Watch_fd watch_fd = watch_fds + i; + if (watch_fd->fd == fd) { + zsfree(watch_fd->func); + watch_fd->func = funcnam; + watch_fd->widget = OPT_ISSET(ops,'w') ? 1 : 0; found = 1; break; } @@ -805,56 +807,38 @@ bin_zle_fd(char *name, char **args, Options ops, UNUSED(char func)) if (!found) { /* zrealloc handles NULL pointers, so OK for first time through */ int newnwatch = nwatch+1; - watch_fds = (int *)zrealloc(watch_fds, - newnwatch * sizeof(int)); - watch_funcs = (char **)zrealloc(watch_funcs, - (newnwatch+1) * sizeof(char *)); - watch_widgets = (int *)zrealloc(watch_widgets, - newnwatch * sizeof(int)); - watch_fds[nwatch] = fd; - watch_funcs[nwatch] = funcnam; - watch_funcs[newnwatch] = NULL; - watch_widgets[nwatch] = OPT_ISSET(ops,'w') ? 1 : 0; + Watch_fd new_fd; + watch_fds = (Watch_fd)zrealloc(watch_fds, + newnwatch * sizeof(struct watch_fd)); + new_fd = watch_fds + nwatch; + new_fd->fd = fd; + new_fd->func = funcnam; + new_fd->widget = OPT_ISSET(ops,'w') ? 1 : 0; nwatch = newnwatch; } } else { /* Deleting a handler */ for (i = 0; i < nwatch; i++) { - if (watch_fds[i] == fd) { + Watch_fd watch_fd = watch_fds + i; + if (watch_fd->fd == fd) { int newnwatch = nwatch-1; - int *new_fds, *new_widgets; - char **new_funcs; + Watch_fd new_fds; - zsfree(watch_funcs[i]); + zsfree(watch_fd->func); if (newnwatch) { - new_fds = zalloc(newnwatch*sizeof(int)); - new_funcs = zalloc((newnwatch+1)*sizeof(char*)); - new_widgets = zalloc(newnwatch*sizeof(int)); + new_fds = zalloc(newnwatch*sizeof(struct watch_fd)); if (i) { - memcpy(new_fds, watch_fds, i*sizeof(int)); - memcpy(new_funcs, watch_funcs, i*sizeof(char *)); - memcpy(new_widgets, watch_widgets, i*sizeof(int)); + memcpy(new_fds, watch_fds, i*sizeof(struct watch_fd)); } if (i < newnwatch) { memcpy(new_fds+i, watch_fds+i+1, - (newnwatch-i)*sizeof(int)); - memcpy(new_funcs+i, watch_funcs+i+1, - (newnwatch-i)*sizeof(char *)); - memcpy(new_widgets+i, watch_widgets+i+1, - (newnwatch-i)*sizeof(int)); + (newnwatch-i)*sizeof(struct watch_fd)); } - new_funcs[newnwatch] = NULL; } else { new_fds = NULL; - new_funcs = NULL; - new_widgets = NULL; } - zfree(watch_fds, nwatch*sizeof(int)); - zfree(watch_funcs, (nwatch+1)*sizeof(char *)); - zfree(watch_widgets, nwatch*sizeof(int)); + zfree(watch_fds, nwatch*sizeof(struct watch_fd)); watch_fds = new_fds; - watch_funcs = new_funcs; - watch_widgets = new_widgets; nwatch = newnwatch; found = 1; break; -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/