From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 29725 invoked from network); 28 Jul 2021 03:48:03 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 28 Jul 2021 03:48:03 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1627444083; b=wAJ0hTyr7q6fNKnYXYGKHkkjpUv2KIn/6AsGTdzKi2GirqEmO9GfHF5ROWEZm1qXE0NE1GEHR3 6EnpFe5tehwKipaLf/qwg8neu7tXyR93GeQicYgYpOI9e0134Ho18J81Hmh+5bRmssDbIAyzhg Us0XOxpMqalV7MyRz9dfw1L/6/NaCLcImN3VIEt5BArMKFsycf0Q3U7KtuSd4x+OgoB3kO4es9 0ATlcXaOReZiDx6k/Yisesf6yQnpB4BzqaXrGCY06OwE9Ez4bdz4lgtTjP1+P41TW8LBoA7AWF WHdbHhY0CECk71HXGD6v86WnMhXhEVcm8bER09xeIaVPPA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-wr1-f47.google.com) smtp.remote-ip=209.85.221.47; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1627444083; bh=vxlrbxI5unKW7LyNN0yD9GBVulkSlNak3WI6tp1xNYQ=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:To:Subject:Message-ID:Date:From:MIME-Version: DKIM-Signature:DKIM-Signature; b=nNwbLvufxAT0kjS+8f/U2oCCaPAJUpMb1rMWxr67qYS/JWC5wQlTPbVGrKJevjMcC7a9eQFIzS iwt2ypaGQhX87GoXEergHLYpNy+XW8qIL3yThsyth3eRXiMSjNrcOBxkJdCBCCl4s9FlL3YCxc s1TFeqjF7DlvYR+OppIsjGXk1IYfTQVZRXapsldLQ9rrJOtixR6nTu/kQQS8k1WkIrR+AR8vMi K2sQZJYiHAZgUPH4JrYxuYTfNr0OqhFLlaBoq4V0quI0/1Eme3zm5M4QC/YEp9Z+iA+/FjNANO 5yCACS3vx83SikX2G4HAMnSUhklYM9/CAtkbM99kvqJ4Lg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:To:Subject:Message-ID: Date:From:MIME-Version:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References; bh=IxSq1vxOg5jHjq3OabzxR5EUfrmY0D4FYkXyyWxBB5Y=; b=LunjukUQ5BPgVeBDwXwKpr4HvL 28Fbho3HS32QpsNYkv5Aer/2sLtbNPFrxcHuth/fKIhkjg6v3GVvNiI7A8iWtQy6YI4Qaq+7IF4qv rQfmgpxwTuOjVplCeBy3Bmsvh9DOtsN7Y2Q3utdBEyZ1FDTxei7nAY/5FY/L0XCkq1zIGswChruDQ ZkhAso8qdetw/DDCNX8hdr1OzRSqa0xXgaAYBAc0W4C6chUV2dBpfjkvi5AgsC/ezUNpbOHtIh3FL +RyWMbny3T4aq0e//J7fK+k3haiJYl/FrTF+s3RNPSx8SWeCPXHFol/D8w/1EP4Nwnr2Md6r5yJdN 6RpImklg==; Received: from authenticated user by zero.zsh.org with local id 1m8aY6-000694-R2; Wed, 28 Jul 2021 03:48:02 +0000 Authentication-Results: zsh.org; iprev=pass (mail-wr1-f47.google.com) smtp.remote-ip=209.85.221.47; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-wr1-f47.google.com ([209.85.221.47]:46682) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1m8aXY-0005QW-CK; Wed, 28 Jul 2021 03:47:28 +0000 Received: by mail-wr1-f47.google.com with SMTP id c16so734144wrp.13 for ; Tue, 27 Jul 2021 20:47:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=IxSq1vxOg5jHjq3OabzxR5EUfrmY0D4FYkXyyWxBB5Y=; b=hekYxBgEw7BfH/7eG/ZbSd84APy+IGmn/PpuHULqCNQ9kNcq8Nf/076Us4abS+7zGT mm+2uLKvk0z45+XWq6Efz4wTJopUwlORKm8gaaEkVh4wxduNN4pv5y9SpH6Qu5gjU/uq U0OTnAw0XnslYqOHXtnS+3Qve3p+QY0+WvnIf/HUyn2AuvHomIEPEPN5FY9S4WCnU166 1W1SK2XYFUqs05H7swW3w7SidZ+Go4MWtquVoVmz/KPT93v+jATeAh6vht4OPj8OVJBa iRg65q6A28Htk8HjRdvSB2gTsi9Qrno7EVuz3PD/Lrosh+XwhxchJgAtBnUzbaq6MMzE /KHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=IxSq1vxOg5jHjq3OabzxR5EUfrmY0D4FYkXyyWxBB5Y=; b=ChpBhEtk5auIjG4onAq21DeTSRjJFaL9TasaKWrUdSgzJ06J1ipZyAP/cTn4WhBOJ4 twhcFjyY6i/X2oIYQSxDDe8Pp57SkexeB8pK2toeYQUEmg2K/tzRXWopJMUaUGUfzm19 2PIEtX8jpyPvaGJX7qi1PuLri4X80bUWh8vd3QeeA/88Z39F3YczA0QosNvfgoqp9jsc ep/6psKqOiGeva7KSMaEtP0O0Meof2fMGDW6YC7Yxd85uRhZDdO3+5y1oFORldBL/h92 kJVLSkBvqshl7otFnBR1MsKiRRTmlSBjTrafWSNIAVlnRcuwcRb3/HZibKHXkR0SxIv8 j9Hw== X-Gm-Message-State: AOAM530IhgLXzEhXhaKHLLqa7114YbZUIb0ZRxKrWVEXMe/gdkEePGRn PfVzMc3kHXfHCfJY0EQk/e5YCuiorPD0V50Q3dkwlcctGco= X-Google-Smtp-Source: ABdhPJxqxetaoob5hwRyquslNjrT2ZsuQ416J4Uo984vKK0YLWaXP4hPyPrjyvjzVbefLlfEmDl5geHrCPxnWOPLfFY= X-Received: by 2002:a5d:61c8:: with SMTP id q8mr16225241wrv.151.1627444047584; Tue, 27 Jul 2021 20:47:27 -0700 (PDT) MIME-Version: 1.0 From: Zach Riggle Date: Tue, 27 Jul 2021 22:47:16 -0500 Message-ID: Subject: Differences between "TRAPERR()" and "trap foo ERR" To: zsh-users@zsh.org Content-Type: text/plain; charset="UTF-8" X-Seq: 26820 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: --------------------------------- BACKGROUND --------------------------------- I've discovered some undocumented behavior of TRAPNAL functions, and some divergence of behavior between TRAPNAL and "trap foo NAL". Specifically, the former receives a $1 with the value of the signal which invoked the routine, but the latter does not -- and that value is the actual value of the signal (e.g. $1 == SIGSEGV == 11 for TRAPSEGV). For example, TRAPERR receives a value of 32, which is not a valid signal value on my system (NSIG==32). When setting via "trap 'my_trapfunc $@' SEGV", no argument is provided to 'my_trapfunc'. All of this is on ZSH v5.8. ---------------------------------- EXAMPLES ---------------------------------- So I've got two scripts, traperr and trapfunc. $ cat traperr #!/usr/bin/env zsh TRAPERR() { echo "Got ${(q-)@}" } false $ cat trapfunc #!/usr/bin/env zsh trap_err() { echo "Got ${(q-)@}" } trap trap_err ERR false Note that "traperr" declares an all-caps TRAPERR. "trapfunc" instead uses the "trap" command to set the ERR trap. Both generally work as expected, but the former ("TRAPERR") receives an argument in "$1" that is undocumented. $ ./traperr Got 32 $ ./trapfunc Got ------------------------------- DOCUMENTATION -------------------------------- I looked at the manual page (section 9.3.1) for trap functions: https://zsh.sourceforge.io/Doc/Release/Functions.html#Trap-Functions However, there's nothing mentioned about any parameters being specified to TRAPNAL functions, and it generally seems that "TRAPNAL" and "trap foo NAL" behave the same. ------------------------------ EXPERIMENTATION ------------------------------- At first, I thought the provenance of the value 32 comes from the value of NSIG on my system (which on macOS is 32). $ cat nsig.c #include #include int main() { printf("NSIG = %d\n", NSIG); } $ gcc -o nsig nsig.c $ ./nsig NSIG = 32 Second, I thought that perhaps it might come from ZSH's own list of signals. There is a ZERR entry, in any case. $ echo $signals EXIT HUP ... ZERR DEBUG $ for i in {0..${#signals}}; do echo "$i: ${signals[$i]}"; done 0: 1: EXIT 2: HUP ... 31: USR1 32: USR2 33: ZERR 34: DEBUG As we can see here, the value received by "33: ZERR", is not the 32'th entry in the list of $signals. Since ZSH array indices start at 1, this makes more sense and getting "32" for ZERR, is correct. This is confirmed by setting e.g. TRAPSEGV, where SIGSEGV==11. $ cat trapsegv #!/usr/bin/env zsh TRAPSEGV() { echo "Got ${(q-)@}" } kill -s SEGV $$ $ ./trapsegv Got 11 So we are receiving the actual value of the signal, when we declare a TRAPNAL function. This corresponds to the NAL+1'th offset in the $signals array. --------------------- ATTEMPT TO CONVERGE FUNCTIONALITY ---------------------- Since arguments are being provided to TRAPNAL functions, I thought I would try to modify the "trapfunc" script to include its arguments when invoking trap_err (note the extra $@ inside the trap statement versus before). $ cat trapfunc_args #!/usr/bin/env zsh trap_err() { echo "Got ${(q-)@}" } trap 'trap_err $@' ERR false $ ./trapfunc_args Got It seems this does not work to specify the signal number by including e.g. $@ in the trap command list. ------------------------ CONCLUSION AND OBSERVATIONS ------------------------- Overall, there are two observations. 1. The behavior that TRAPNAL will receive NAL as its first argument, where NAL corresponds to a the system-defined signal values (e.g. SIGSEGV == 11). The name for this signal can be gotten from ${signals[x+1]}. This should be documented. 2. This behavior does not expand to "trap func NAL" statements, even if we try to pass "$@" to some function. This seems like a bug. Am I missing something in the docs (quite likely) -- and is the behavior difference intended -- or are these bugs. Thanks for reading! Zach Riggle