From nobody Wed Oct 02 15:55:55 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XJfY80MNBz5Xjgk; Wed, 02 Oct 2024 15:55:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XJfY76z8Rz40qg; Wed, 2 Oct 2024 15:55:55 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727884556; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/wlnaWfGlWyGR8cfFGwa4UYdzuokywc+pTaIny5oI58=; b=FrMexlmVFrBRZA0OsVNe4PRmLBVK7KscIPq136uJSOwUvTuUZmAisFVMei9yLVwLfKQjVh WfJ5qMQ8y3sHQr/3MHnQfaXL/8vK/rTRnWE8sy93iCRe4sTHoUrWEPaULgu2bBMnB0LXEN voum4j8K02mW9n+EmDzfNX4Z9WwWG14uw1RCOF/JS2lSNtGzSwiRysBTmoyAZMKrOYZdJX kg8w5Tj8N43kiMAPeAi9LqQ89s1znA5C4F29Wu1R4hdTH0ZybjhHLYUqJtFUfQdOH+9i6j Rv1bi1cyQRNykU+F0bCdbVMPp+NGckmHjq5nKg9sz9urpHpdj1SNau6dy+38VQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727884556; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/wlnaWfGlWyGR8cfFGwa4UYdzuokywc+pTaIny5oI58=; b=RE7soR4j6gdAiau712n/UZe95Ed1np7hi4jUKKd9xLsUcqkSjvQKUlGHSRiTo80za7nYl0 lORDwIR//tJsENGNybAG3HxovjxQHHS8N9UVy5tIpdWpzQHDCrTsxEwiMvxS7ZOJ5AH91R hrOx+B6dy0zxDjBGlFRf89CuN207n5CWzbdf47+QFWQ3czyD/QOxp9guujwJikCJh54im/ eSYHFNyH8/ow0sNIPwddreWBmcghL6cnB2rRbPeZ48j3xbQjLE9lCF2UEjxMmWVmhVQv1O duQX1M/FA//Z+iI9g5nKzkijAJzNBTnjR9rO+atdzheRAWSZiZjUJXr6dLdnCQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1727884556; a=rsa-sha256; cv=none; b=woQtwPU1nzEXb/VxKmRmZlLnSBhyRNPPO6Rj3QNsVX6Y5m3tohR2HqkkDC88Wd/zWfF5Hn 7Qom9uT97PsD4dU4My0XiguG72vet/1O6+dur796SyfteoiubUdv4Xe3t86cd33+0WVVSF PEwz7aO91r753SH9Pio8mKu3/opFwqSH9vEOFkK3cxs9iECgyT91M9ppR2A9mFNOUsr1tK QQWRMn0HOwJmB/JFmoQDRSSgKvod8yHuN2NBJCYLC9yrr0p8zbJh3AC549DwIL0/Jy4wye WjJq1vewyFXPAZbhneyOyE8RHTvUb1dYasCBdeQTpLFIDEIZgwRr8jOzxa12iQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XJfY76ZPQz1RW0; Wed, 2 Oct 2024 15:55:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 492FttLS050813; Wed, 2 Oct 2024 15:55:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 492FttA0050810; Wed, 2 Oct 2024 15:55:55 GMT (envelope-from git) Date: Wed, 2 Oct 2024 15:55:55 GMT Message-Id: <202410021555.492FttA0050810@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 4e859e67dde2 - main - libdiff: Implement diff coloring. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 4e859e67dde2cb4a583d340b27793a255f62f53e Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=4e859e67dde2cb4a583d340b27793a255f62f53e commit 4e859e67dde2cb4a583d340b27793a255f62f53e Author: Dag-Erling Smørgrav AuthorDate: 2024-10-02 15:54:48 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-10-02 15:55:06 +0000 libdiff: Implement diff coloring. This patch got accidentally left out when libdiff was imported. The rest of the code (command-line option etc.) was present, just not the part that actually prints ANSI color codes. Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D46873 --- contrib/libdiff/include/diff_output.h | 4 ++ contrib/libdiff/lib/diff_output.c | 69 ++++++++++++++++++++++++++++------- usr.bin/diff/diffreg_new.c | 2 + 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/contrib/libdiff/include/diff_output.h b/contrib/libdiff/include/diff_output.h index d2568c5a2b50..500ee6427574 100644 --- a/contrib/libdiff/include/diff_output.h +++ b/contrib/libdiff/include/diff_output.h @@ -110,3 +110,7 @@ int diff_output_chunk_right_version(struct diff_output_info **output_info, const char *diff_output_get_label_left(const struct diff_input_info *info); const char *diff_output_get_label_right(const struct diff_input_info *info); + +void diff_output_set_colors(bool _color, + const char *_del_code, + const char *_add_code); diff --git a/contrib/libdiff/lib/diff_output.c b/contrib/libdiff/lib/diff_output.c index 3b84608882f6..2a0d95896e1a 100644 --- a/contrib/libdiff/lib/diff_output.c +++ b/contrib/libdiff/lib/diff_output.c @@ -30,6 +30,22 @@ #include "diff_internal.h" +static bool color; +static const char *del_code = "31"; +static const char *add_code = "32"; + +void +diff_output_set_colors(bool _color, + const char *_del_code, + const char *_add_code) +{ + color = _color; + if (_del_code) + del_code = _del_code; + if (_add_code) + add_code = _add_code; +} + static int get_atom_byte(int *ch, struct diff_atom *atom, off_t off) { @@ -66,12 +82,25 @@ diff_output_lines(struct diff_output_info *outinfo, FILE *dest, off_t outoff = 0, *offp; uint8_t *typep; int rc; + bool colored; if (outinfo && outinfo->line_offsets.len > 0) { unsigned int idx = outinfo->line_offsets.len - 1; outoff = outinfo->line_offsets.head[idx]; } + if (color) { + colored = true; + if (*prefix == '-' || *prefix == '<') + printf("\033[%sm", del_code); + else if (*prefix == '+' || *prefix == '>') + printf("\033[%sm", add_code); + else + colored = false; + } else { + colored = false; + } + foreach_diff_atom(atom, start_atom, count) { off_t outlen = 0; int i, ch, nbuf = 0; @@ -80,14 +109,16 @@ diff_output_lines(struct diff_output_info *outinfo, FILE *dest, size_t n; n = strlcpy(buf, prefix, sizeof(buf)); - if (n >= DIFF_OUTPUT_BUF_SIZE) /* leave room for '\n' */ - return ENOBUFS; + if (n >= DIFF_OUTPUT_BUF_SIZE) { /* leave room for '\n' */ + rc = ENOBUFS; + goto out; + } nbuf += n; if (len) { rc = get_atom_byte(&ch, atom, len - 1); if (rc) - return rc; + goto out; if (ch == '\n') len--; } @@ -95,11 +126,13 @@ diff_output_lines(struct diff_output_info *outinfo, FILE *dest, for (i = 0; i < len; i++) { rc = get_atom_byte(&ch, atom, i); if (rc) - return rc; + goto out; if (nbuf >= DIFF_OUTPUT_BUF_SIZE) { wlen = fwrite(buf, 1, nbuf, dest); - if (wlen != nbuf) - return errno; + if (wlen != nbuf) { + rc = errno; + goto out; + } outlen += wlen; nbuf = 0; } @@ -107,25 +140,35 @@ diff_output_lines(struct diff_output_info *outinfo, FILE *dest, } buf[nbuf++] = '\n'; wlen = fwrite(buf, 1, nbuf, dest); - if (wlen != nbuf) - return errno; + if (wlen != nbuf) { + rc = errno; + goto out; + } outlen += wlen; if (outinfo) { ARRAYLIST_ADD(offp, outinfo->line_offsets); - if (offp == NULL) - return ENOMEM; + if (offp == NULL) { + rc = ENOMEM; + goto out; + } outoff += outlen; *offp = outoff; ARRAYLIST_ADD(typep, outinfo->line_types); - if (typep == NULL) - return ENOMEM; + if (typep == NULL) { + rc = ENOMEM; + goto out; + } *typep = *prefix == ' ' ? DIFF_LINE_CONTEXT : *prefix == '-' ? DIFF_LINE_MINUS : *prefix == '+' ? DIFF_LINE_PLUS : DIFF_LINE_NONE; } } - return DIFF_RC_OK; + rc = DIFF_RC_OK; +out: + if (colored) + printf("\033[m"); + return rc; } int diff --git a/usr.bin/diff/diffreg_new.c b/usr.bin/diff/diffreg_new.c index a7a40ec12ec9..af9104559986 100644 --- a/usr.bin/diff/diffreg_new.c +++ b/usr.bin/diff/diffreg_new.c @@ -253,6 +253,8 @@ diffreg_new(char *file1, char *file2, int flags, int capsicum) goto done; } + if (color) + diff_output_set_colors(color, del_code, add_code); if (diff_format == D_NORMAL) { rc = diff_output_plain(NULL, stdout, &info, result, false); } else if (diff_format == D_EDIT) {