From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 84382bcf for ; Tue, 10 Sep 2019 22:20:17 +0000 (UTC) Received: (qmail 12324 invoked by alias); 10 Sep 2019 22:20:09 -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: List-Unsubscribe: X-Seq: 44750 Received: (qmail 20219 invoked by uid 1010); 10 Sep 2019 22:20:09 -0000 X-Qmail-Scanner-Diagnostics: from mail-lj1-f169.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.101.2/25566. spamassassin: 3.4.2. Clear:RC:0(209.85.208.169):SA:0(-2.0/5.0):. Processed in 1.831501 secs); 10 Sep 2019 22:20:09 -0000 X-Envelope-From: timothee.mazzucotelli@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 209.85.208.169 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=MDLHmbC3KAhTnXfQUCIsbLeE7EzAYDXhd47bPd0K4qg=; b=RvylqTqNjjVqyAgxcmtlB5SW4lUcVvjulOGM1Ujwd0GVqMhMokuEzi8Ml/oS4TWffB jRReuuGsYU4VRfs08lJSDpDK6NpQtfxz/FvPphDeR77xz3uXzqcOBXrMS3SSeawYOge/ FCeZNmN5b9/tgCDyr95EBS9fQFT9rXBfZQ1qwFLi3LyUE1GZYRYZaITbynAl8adQz3Kb dsOAViv+Bfn0nWyDw8WqE+llhPnvUdNvtA01hS//rPPS/dZ1zlF3YdHRbfoTksHHBq4N 6x8wD5W5ArRT18GJB1w6qj0Dv1SB57qDn+2myKtDR6itpDvyPtyynnBwwJOtVvuqi6qa OHig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=MDLHmbC3KAhTnXfQUCIsbLeE7EzAYDXhd47bPd0K4qg=; b=caSCyQpj7RnIAFyIOVs0L185LAr84CoL4KYW0KXKAUgw381H4qKdWvzkEickRoHzSM 0BA4B8URElxLdkeXqhA3pzavKUyMpkWbX5jNk+SPjRyBp2L6yFcVzFa0QydzOYgeLJT6 HbPV+ckn6JgWQEundlkaiHK49V622pJNxlswBbTokfN6Dfd75ogPGPC6DTqCDb8fVn6n gNAECTsTF/KbYTiqEsTqafSH/4p7B8uvf4tIgvxI5tCtWft324LTjhR8OTGpp7IrgZAj o1vsqm3XJTHbnnQf1B2lYM53BZiduK73/zgit0g1oiOfqgY44Q6s6wj+1ZWOu/bRtzTh 3DUQ== X-Gm-Message-State: APjAAAVURI2ORwrBzXBUwGKfGnHPpE7SPY87LZ8RocyrQlBNiC3ofNL5 p/wIVoT3KHlqwZosogca/UPgKDtXVN7qtvAz2lzSrpQA X-Google-Smtp-Source: APXvYqyV8D6HTNGGcRFthoFZ000NSuogdYmtDQQzjzbICA2b++NrBSPXNN5ze8bVxV7oiJTduZiBQzsd0kVJUDH9ano= X-Received: by 2002:a2e:3c0b:: with SMTP id j11mr21307460lja.78.1568153971357; Tue, 10 Sep 2019 15:19:31 -0700 (PDT) MIME-Version: 1.0 References: <20190518075514.hbygmb5dl5wz23h5@chaz.gmail.com> <20190520103444.qyih7lvoigvf3rfx@chaz.gmail.com> <1563722540.4311.24.camel@samsung.com> <1565710707.5633.11.camel@samsung.com> In-Reply-To: <1565710707.5633.11.camel@samsung.com> From: =?UTF-8?Q?Timoth=C3=A9e_Mazzucotelli?= Date: Wed, 11 Sep 2019 00:19:36 +0200 Message-ID: Subject: Re: Feature request: ZSH_XTRACEFD variable To: "zsh-workers@zsh.org" Content-Type: multipart/alternative; boundary="0000000000006a7bf905923a4c17" --0000000000006a7bf905923a4c17 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Well it looks good! I pulled the changes for using valgrind, as previously suggested by Sebastian Gniazdowski, and ran the test suite, but got way more than just one leak: 255 actually (funny number), totaling 57,299 bytes definitely lost in 596 blocks, and 2,274 bytes indirectly lost in 557 blocks. Is this expected? Unless you think the code is not ready yet, I'm going to look at the tests now, and try to write some for this new xtracefd feature. Timoth=C3=A9e On Tue, Aug 13, 2019 at 5:39 PM Peter Stephenson wrote: > How about this? > > I've tried to make it so that if the fd is 2 or less it always uses > the standard file, if it's greater it always creates or frees one, and > it only creates a new file when it's requested to switch. > > One other fix is that we only need to fclose(xtrerr) which then closes > the file descriptor. There's no way of just closing the file and not > the file descriptor --- we'll need to document that the file descriptor > is closed if ZSH_XTRACEFD changes again since it's a bit illogical given > that the user opened the fd in the first place. > > pws > > diff --git a/Src/exec.c b/Src/exec.c > index e81053d67..fe702b644 100644 > --- a/Src/exec.c > +++ b/Src/exec.c > @@ -5394,7 +5394,7 @@ execshfunc(Shfunc shf, LinkList args) > cmdsp =3D 0; > if ((osfc =3D sfcontext) =3D=3D SFC_NONE) > sfcontext =3D SFC_DIRECT; > - xtrerr =3D stderr; > + xtrerr =3D xtrace_file; > > doshfunc(shf, args, 0); > > diff --git a/Src/init.c b/Src/init.c > index 445cd3937..c51197079 100644 > --- a/Src/init.c > +++ b/Src/init.c > @@ -609,8 +609,10 @@ init_io(char *cmd) > SHTTY =3D -1; > } > > - /* Send xtrace output to stderr -- see execcmd() */ > - xtrerr =3D stderr; > + /* Send xtrace output to zsh_xtracefd file descriptor -- see > execcmd() */ > + if (zsh_xtracefd =3D=3D 0) > + zsh_xtracefd =3D 2; > + xtracefdassign(); > > /* Make sure the tty is opened read/write. */ > if (isatty(0)) { > diff --git a/Src/params.c b/Src/params.c > index 1499e3a40..2347ee9c3 100644 > --- a/Src/params.c > +++ b/Src/params.c > @@ -102,7 +102,8 @@ zlong lastval, /* $? */ > zterm_lines, /* $LINES */ > rprompt_indent, /* $ZLE_RPROMPT_INDENT */ > ppid, /* $PPID */ > - zsh_subshell; /* $ZSH_SUBSHELL */ > + zsh_subshell, /* $ZSH_SUBSHELL */ > + zsh_xtracefd; /* $ZSH_XTRACEFD */ > > /* $FUNCNEST */ > /**/ > @@ -264,6 +265,9 @@ static const struct gsu_array pipestatus_gsu =3D > static const struct gsu_integer rprompt_indent_gsu =3D > { intvargetfn, zlevarsetfn, rprompt_indent_unsetfn }; > > +static const struct gsu_integer xtracefd_gsu =3D > +{ intvargetfn, xtracefdsetfn, xtracefdunsetfn }; > + > /* Nodes for special parameters for parameter hash table */ > > #ifdef HAVE_UNION_INIT > @@ -353,6 +357,7 @@ IPDEF5("LINES", &zterm_lines, zlevar_gsu), > IPDEF5U("ZLE_RPROMPT_INDENT", &rprompt_indent, rprompt_indent_gsu), > IPDEF5("SHLVL", &shlvl, varinteger_gsu), > IPDEF5("FUNCNEST", &zsh_funcnest, varinteger_gsu), > +IPDEF5("ZSH_XTRACEFD", &zsh_xtracefd, xtracefd_gsu), > > /* Don't import internal integer status variables. */ > #define IPDEF6(A,B,F) > {{NULL,A,PM_INTEGER|PM_SPECIAL|PM_DONTIMPORT},BR((void > *)B),GSU(F),10,0,NULL,NULL,NULL,0} > @@ -4387,6 +4392,71 @@ setsecondstype(Param pm, int on, int off) > return 0; > } > > +/* Open / assign the XTRACE fd */ > + > +/**/ > +void xtracefdassign(void) > +{ > + int fd =3D (int)zsh_xtracefd; > + switch (fd) > + { > + case 0: /* bizarre, but handle for consistency */ > + xtrerr =3D stdin; > + break; > + > + case 1: > + xtrerr =3D stdout; > + break; > + > + case 2: > + xtrerr =3D stderr; > + break; > + > + default: > + xtrerr =3D fdopen(fd, "w"); > + break; > + } > + xtrace_file =3D xtrerr; > +} > + > +/* Function to set value of special parameter `ZSH_XTRACEFD' */ > + > +/**/ > +void > +xtracefdsetfn(Param pm, zlong fd) > +{ > + int current_fd; > + > + /* Check that the given file descriptor is valid */ > + if (fcntl(fd, F_GETFD) !=3D -1 || errno !=3D EBADF) { > + current_fd =3D intvargetfn(pm); > + /* We never close file descriptors 0 (stdin), 1 (stdout) or 2 > stderr) */ > + if (current_fd > 2) > + fclose(xtrerr); > + intvarsetfn(pm, fd); > + xtracefdassign(); > + } else > + zwarn("file descriptor %d is not valid", fd); > + > +} > + > +/* Function to unset value of special parameter `ZSH_XTRACEFD' */ > + > +/**/ > +void > +xtracefdunsetfn(Param pm, UNUSED(int exp)) > +{ > + int current_fd =3D intvargetfn(pm); > + if (current_fd =3D=3D 2) /* Nothing to do, already using stderr */ > + return; > + else { /* Reset to file descriptor 2 (stderr) */ > + intvarsetfn(pm, 2); > + if (current_fd > 2) > + fclose(xtrerr); /* Never close standard descriptors */ > + xtrerr =3D xtrace_file =3D stderr; > + } > +} > + > /* Function to get value for special parameter `USERNAME' */ > > /**/ > diff --git a/Src/utils.c b/Src/utils.c > index 46cf7bcf6..2eee27697 100644 > --- a/Src/utils.c > +++ b/Src/utils.c > @@ -1760,12 +1760,19 @@ checkmailpath(char **s) > /**/ > FILE *xtrerr =3D 0; > > +/* This records the last file XTRACE was open too. > + * It's used for restoring XTRACE after a possible redirection.gggg > + */ > + > +/**/ > +FILE *xtrace_file; > + > /**/ > void > printprompt4(void) > { > if (!xtrerr) > - xtrerr =3D stderr; > + xtracefdassign(); > if (prompt4) { > int l, t =3D opts[XTRACE]; > char *s =3D dupstring(prompt4); > --0000000000006a7bf905923a4c17--