From nobody Tue Sep 12 16:42:54 2023 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 4RlTsV5h2wz4st8v; Tue, 12 Sep 2023 16:42:54 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RlTsV4qCfz3bmS; Tue, 12 Sep 2023 16:42:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694536974; 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=mlDQHTln0BTq7AVXw+ZfVt/gLIZgrEaVkpqeokJ8wus=; b=bJTGiavRcvDeTyY3tdSDVkLkPp07QQso86G+eosGCkx4zWc6wqMCfqg5+lzOeCYj4gHjqH mFx+RlloT99UFVaqfbfq8L4QKDorpp8l4Nw9Xm4CA/8Lt/h6Bjh7FMG7BnRCn+DaKsCfO9 p2oQ8fKbBJSJSgmdr+MNHaVlWc04vZwGH0bggDRKL/wsLJhlBvbuwN0f1xwcjcdH9AK4tT oC7oKBaoJ9NLdqEfnTZ+OBBwDXK80tZVf5uSSDMZAhppRNHSdU/zoKR9PPeL24il6cTE/x aFZCIixtIOmU8aoDMNFq1uFTEEmdD068yZr+L/ybGBoAgydFUvuYLCWWdG8i5w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1694536974; a=rsa-sha256; cv=none; b=M9GsFYcK6tP+beU6D0yrLH6pEuNhLC5602WcxUzzZiil2NA0ZMEe5bwni3YI0pLNSyve5U rStzUbZZYQyCf63T3FhmaGrrg+x/NJkd9uhmNo66ojbyGC6yAR+xNpg0bwaQ9qfPdh/je/ n1gUKsP/nPp2iVXwGZNz8sE+w048dHUlt8ht9b3D+e3Kw1SRbzrp5DBl52l2kbhksbhCiF 1qc2bWGQbV9WSsbbirppqwXMNPdT5bf3f3rhpdhTMGzOM8KZqGG9WuMmErkCvu4L23voEZ WeJo9Cn16mWmWI1N7iOaVyVKzKhBm/jsUHnuBsdbUIVrSCatSMR8zj/aBrWQ/A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694536974; 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=mlDQHTln0BTq7AVXw+ZfVt/gLIZgrEaVkpqeokJ8wus=; b=GUdMtppZIWNgdeTXrJcqSD43yicAyBQNK7YHPQhrzh27XOjPU8jwI/yXMGmrs+FZGwe+/U GH0QT938QlM5BYaRaUGMOA551MtKuNxt2rfd/9AhKkL4OmAKBDRClhXGggqjmxFSiyo3m/ aEL7tg8rhas0mH/3Yj61IFAMV268BHbF3bd1DP4+Y5P/YqApWJ/sWa84gQXQbQ0Wa+SI5E /0MxPxqwUBf5EHSWf90VM1Fr9E4xxSwBkX4RAunWoICdYGH2KXoq5vZ0/VN5Xaji1gBywW 2B2jvfiOHuP33vngGLaylncsTRrSZQcLKghj3he3TNVUaya3kQf/vXW1J4+z1g== 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 4RlTsV3qNkzfVj; Tue, 12 Sep 2023 16:42:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38CGgsXq070416; Tue, 12 Sep 2023 16:42:54 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38CGgsQp070413; Tue, 12 Sep 2023 16:42:54 GMT (envelope-from git) Date: Tue, 12 Sep 2023 16:42:54 GMT Message-Id: <202309121642.38CGgsQp070413@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Piotr Pawel Stefaniak Subject: git: 2fc4a84ed828 - main - sh: introduce a function to iterate over all aliases 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pstef X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2fc4a84ed8288bd25be3de35d756f46ec7785030 Auto-Submitted: auto-generated The branch main has been updated by pstef: URL: https://cgit.FreeBSD.org/src/commit/?id=2fc4a84ed8288bd25be3de35d756f46ec7785030 commit 2fc4a84ed8288bd25be3de35d756f46ec7785030 Author: Piotr Pawel Stefaniak AuthorDate: 2023-04-29 20:23:59 +0000 Commit: Piotr Pawel Stefaniak CommitDate: 2023-09-12 16:39:20 +0000 sh: introduce a function to iterate over all aliases Currently the data structure holding alias information is opaque for consumers outside alias.c and there is no way to iterate over all aliases, which will become needed by a future commit. The new function "iteralias" takes a null pointer to return the first alias or an existing alias to return the next one, unless there is no alias to return, in which case it returns a null pointer. I slightly changed the static function hashalias so that it returns the index into the array holding link heads, and not the link head directly. In this form it's easier to use by iteralias and the slight adjustment in the three existing callers doesn't look too bad. Differential Revision: https://reviews.freebsd.org/D40619 --- bin/sh/alias.c | 29 +++++++++++++++++++++++------ bin/sh/alias.h | 1 + 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/bin/sh/alias.c b/bin/sh/alias.c index 4bac525e2678..da860be704b6 100644 --- a/bin/sh/alias.c +++ b/bin/sh/alias.c @@ -53,7 +53,7 @@ static int aliases; static void setalias(const char *, const char *); static int unalias(const char *); -static struct alias **hashalias(const char *); +static size_t hashalias(const char *); static void @@ -62,7 +62,7 @@ setalias(const char *name, const char *val) struct alias *ap, **app; unalias(name); - app = hashalias(name); + app = &atab[hashalias(name)]; INTOFF; ap = ckmalloc(sizeof (struct alias)); ap->name = savestr(name); @@ -87,7 +87,7 @@ unalias(const char *name) { struct alias *ap, **app; - app = hashalias(name); + app = &atab[hashalias(name)]; for (ap = *app; ap; app = &(ap->next), ap = ap->next) { if (equal(name, ap->name)) { @@ -145,7 +145,7 @@ lookupalias(const char *name, int check) if (aliases == 0) return (NULL); - for (ap = *hashalias(name); ap; ap = ap->next) { + for (ap = atab[hashalias(name)]; ap; ap = ap->next) { if (equal(name, ap->name)) { if (check && (ap->flag & ALIASINUSE)) return (NULL); @@ -242,7 +242,7 @@ unaliascmd(int argc __unused, char **argv __unused) return (i); } -static struct alias ** +static size_t hashalias(const char *p) { unsigned int hashval; @@ -250,5 +250,22 @@ hashalias(const char *p) hashval = (unsigned char)*p << 4; while (*p) hashval+= *p++; - return &atab[hashval % ATABSIZE]; + return (hashval % ATABSIZE); +} + +const struct alias * +iteralias(const struct alias *index) +{ + size_t i = 0; + + if (index != NULL) { + if (index->next != NULL) + return (index->next); + i = hashalias(index->name) + 1; + } + for (; i < ATABSIZE; i++) + if (atab[i] != NULL) + return (atab[i]); + + return (NULL); } diff --git a/bin/sh/alias.h b/bin/sh/alias.h index 0c6ea99e8b60..a8108d44be2d 100644 --- a/bin/sh/alias.h +++ b/bin/sh/alias.h @@ -42,3 +42,4 @@ struct alias { }; struct alias *lookupalias(const char *, int); +const struct alias *iteralias(const struct alias *);