From patchwork Sat Nov 24 16:20:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonio Ospite X-Patchwork-Id: 10696553 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7882C16B1 for ; Sat, 24 Nov 2018 16:20:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 57ECA2A0AA for ; Sat, 24 Nov 2018 16:20:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 460912A0B7; Sat, 24 Nov 2018 16:20:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DB32A2A0AA for ; Sat, 24 Nov 2018 16:20:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726012AbeKYDJR (ORCPT ); Sat, 24 Nov 2018 22:09:17 -0500 Received: from mail.ao2.it ([92.243.12.208]:38389 "EHLO ao2.it" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725774AbeKYDJR (ORCPT ); Sat, 24 Nov 2018 22:09:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=ao2.it; s=20180927; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From; bh=rS4xMV0NBh+6nGKpPd9duuRcA08KcZ9CwaMCZCy/lOE=; b=wvCtb0BP3eAzJgqtQoHY6j4d1DQMZ3Pds9850SlYKYp+/0k/D66cOHKVOATMw2B9c0UimARs7M7We/OpVPGh/ZbaWrM0l3LkhjnBuN1hmhViaplRNzEbZa7k2IEdm3oXPafb+9Z/aje2ZOI/3elLcFLu8vPelDlMGPyq2TqdexxhFFFicGasPf0sCFt5i5aOhE2MRUFZnDOEgQRCIeMy1bCo6L/yX/vNd5Sou+/ScG8Z0rRuntnI3yNzlbPx/Loq7Z9UwU6b/vmcyN9OcJOp9sEEYRhJwCcN1AnYAdJu7fW0ABAxXm+1os4RB5amUP0pbCK17WZcG/YJlvC5+f/6xQ==; Received: from localhost ([::1] helo=jcn) by ao2.it with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1gQaev-0005k9-EH; Sat, 24 Nov 2018 17:19:53 +0100 Received: from ao2 by jcn with local (Exim 4.91) (envelope-from ) id 1gQafV-0002js-8f; Sat, 24 Nov 2018 17:20:29 +0100 From: Antonio Ospite To: dash@vger.kernel.org Cc: Antonio Ospite Subject: [PATCH] main: don't raise exception when executing dotcmd() on a non-existent file Date: Sat, 24 Nov 2018 17:20:21 +0100 Message-Id: <20181124162021.10486-1-ao2@ao2.it> X-Mailer: git-send-email 2.19.2 MIME-Version: 1.0 X-Face: z*RaLf`X<@C75u6Ig9}{oW$H;1_\2t5)({*|jhM/Vb;]yA5\I~93>J<_`<4)A{':UrE Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When sourcing a file with the dotcmd() builtin, dash raises an exception when the input file cannot be opened. For instance the following script fails prematurely and does not output the message: #!/bin/sh . /non-existent-file echo "Survived!" In this case, the behavior of dash is different from other shells (e.g. bash, zsh) which are more forgiving and allow execution to carry on even if the input file cannot be opened. Fix this by passing the INPUT_NOFILE_OK flag when calling setinputfile() in dotcmd(). As a bonus, this also fixes cases like the following: set -e . /non-existent-file || true echo "Survived! Let's do something else..." This idiom is sometimes used in shell script to source a config file with default values only to provide fallback values if the default values were not available. Signed-off-by: Antonio Ospite --- Hi Herbert, I also thought about printing the warning in setinputfile() itself, maybe guarding the printout with a new flag, like INPUT_NOFILE_OK_SILENT, which could be used by readprofile() to keep the current silent behavior when profile files are not found. I though i'd send this simple version first to see if changing the behavior of dotcmd() is acceptable in the first place. If it is we can then discuss about how to do it. Thank you, Antonio src/main.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 6d53e00..a229d68 100644 --- a/src/main.c +++ b/src/main.c @@ -331,12 +331,18 @@ dotcmd(int argc, char **argv) char *fullname; fullname = find_dot_file(*argv); - setinputfile(fullname, INPUT_PUSH_FILE); + status = setinputfile(fullname, INPUT_PUSH_FILE | INPUT_NOFILE_OK); + if (status < 0) { + sh_warnx("%s: %s", fullname, strerror(errno)); + status = 1; + goto out; + } commandname = fullname; status = cmdloop(0); popfile(); } +out: return status; }