From patchwork Sun Mar 25 07:55:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 10306497 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.web.codeaurora.org (Postfix) with ESMTP id BB070600CC for ; Sun, 25 Mar 2018 07:55:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B00E929378 for ; Sun, 25 Mar 2018 07:55:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A358C29381; Sun, 25 Mar 2018 07:55:48 +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=-6.9 required=2.0 tests=BAYES_00,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 016B529378 for ; Sun, 25 Mar 2018 07:55:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750972AbeCYHzr (ORCPT ); Sun, 25 Mar 2018 03:55:47 -0400 Received: from orcrist.hmeau.com ([104.223.48.154]:49554 "EHLO deadmen.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750788AbeCYHzr (ORCPT ); Sun, 25 Mar 2018 03:55:47 -0400 Received: from gondobar.mordor.me.apana.org.au ([192.168.128.4] helo=gondobar) by deadmen.hmeau.com with esmtp (Exim 4.84_2 #2 (Debian)) id 1f00VC-0007sH-PT; Sun, 25 Mar 2018 15:55:42 +0800 Received: from herbert by gondobar with local (Exim 4.84_2) (envelope-from ) id 1f00VA-0001Pf-9x; Sun, 25 Mar 2018 15:55:40 +0800 Date: Sun, 25 Mar 2018 15:55:40 +0800 From: Herbert Xu To: DASH Mailing List Subject: builtin: Move echo space/nl handling into print_escape_str Message-ID: <20180325075540.GA5414@gondor.apana.org.au> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently echocmd uses print_escape_str to do everything apart from printing the spaces/newlines separating its arguments. This patch moves the actual printing into print_escape_str as well using the format parameter. Signed-off-by: Herbert Xu diff --git a/src/bltin/printf.c b/src/bltin/printf.c index eaf14b6..7785735 100644 --- a/src/bltin/printf.c +++ b/src/bltin/printf.c @@ -103,6 +103,8 @@ static int print_escape_str(const char *f, int *param, int *array, char *s) len = q - p; total = len - 1; + q[-1] = (!!((f[1] - 's') | done) - 1) & f[2]; + total += !!q[-1]; if (f[1] == 's') goto easy; @@ -455,21 +457,22 @@ check_conversion(const char *s, const char *ep) int echocmd(int argc, char **argv) { + const char *lastfmt = snlfmt; int nonl; - nonl = *++argv ? equal(*argv, "-n") : 0; - argv += nonl; + if (*++argv && equal(*argv, "-n")) { + argv++; + lastfmt = "%s"; + } do { - int c; + const char *fmt = "%s "; + char *s = *argv; - if (likely(*argv)) - nonl += print_escape_str("%s", NULL, NULL, *argv++); - if (likely((nonl + !*argv) > 1)) - break; + if (!s || !*++argv) + fmt = lastfmt; - c = *argv ? ' ' : '\n'; - out1c(c); - } while (*argv); + nonl = print_escape_str(fmt, NULL, NULL, s ?: nullstr); + } while (!nonl && *argv); return 0; }