From nobody Tue Sep 12 16:42:56 2023 X-Original-To: dev-commits-src-main@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 4RlTsY2mr4z4stQG; Tue, 12 Sep 2023 16:42:57 +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 4RlTsY1TJBz3bYx; Tue, 12 Sep 2023 16:42:57 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694536977; 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=zukko6J0j3fqF+pE2yUMtwJpyGFTJqZayiHzMoirlZs=; b=hU+B+1EH2GCE/YToFNCklmYWtLHQ4VM1nN7EhnUyLhnjaFG/03+p0B5UlrkPnPBAiH8ALH BUhBOJHi9P6PWerVRot9bEvmcLxeCocCbLIg8IA7hG0sIOqtv19kz+SBODnonkF443nmtX 1yHDb/Y16eeOciFiyaQRIO7piZhPXzJGFOHLcwC70T/g0hlkgCIIeVddF3ugAXmzorr56v P13yANDuHLZWOlKqz2x4pv9Ojqb3Gst0b1ZLOlDufKvrKiTlcFJvviXZm3XF+A5beaJM/U vCWZpYRQo2ypimsG+0DSlVqOtcQBAULS1PpCbp7GO5GPd8qLbQabsTYCplVd+A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1694536977; a=rsa-sha256; cv=none; b=XpYnkZyft4JV5BSFV65TmjlQ82/BlERvj1KnfYiMlNHpciKKbXXCNLQTqCcXPalD0xJ/PR dgss8GHUF2gC0miJ+BzDpcq3remwRgeHA8ywQh2fQ2pQUhVrxbXWms0VXy1mD1DEEOjUi9 NhB6EDfu3P2FLclVcWckmXfEgAAQMWJOOEs2+90hwldU5wwmVWrS+N/oO3sCs2UO9QecZb +uBtNpMvht0o+BOMiotzmfppzaUcjKvB6eLijVeRuhkvu78j6eYK/MuLP2U/qjorsrhv9l ACCSnxScx/lafI/TDJcPJjXLjBJ+UcFl8r8kOmvhzvipFQ+30K1HNMdn8YkNNQ== 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=1694536977; 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=zukko6J0j3fqF+pE2yUMtwJpyGFTJqZayiHzMoirlZs=; b=ExNL/Umu3SQVI9KFx7Q/26Jxs9x/n9L9y+9ad2Cdpbji3Hsy+/xoGkvJrim3ED6w2GVIVZ LBSpS4Kq7Z0Bo/qKN6ZnJR99yGxIk/mOQDM3EBQSyOi9gHJi+RA9PE7bWeTbkfSkiHJ6ds lRSR+6q2ZiEBnI77kSJrRHAbcZnBUwEMNKTCPSqsjCZQrxHtOXLi7Ka101qBGQ8mZkV/hT YNoEeVI3eehLssOgCSrv7YrvWcyJMC0QTOU7gKQCPgt5o0+HY//7sOO3yJf6WHif7UBCl8 87sfdH/dFmbQr7FwKWqUZKUclTu790XIdNxR2QjnXwMdF0Yutx9OD+e6XQykxQ== 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 4RlTsX5vczzfjc; Tue, 12 Sep 2023 16:42:56 +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 38CGguNU070519; Tue, 12 Sep 2023 16:42:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38CGguK8070516; Tue, 12 Sep 2023 16:42:56 GMT (envelope-from git) Date: Tue, 12 Sep 2023 16:42:56 GMT Message-Id: <202309121642.38CGguK8070516@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: bec7b9a21979 - main - sh: introduce a function to iterate over all hashed commands List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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: bec7b9a219790199c64d11833cc34d1ea505d1f4 Auto-Submitted: auto-generated The branch main has been updated by pstef: URL: https://cgit.FreeBSD.org/src/commit/?id=bec7b9a219790199c64d11833cc34d1ea505d1f4 commit bec7b9a219790199c64d11833cc34d1ea505d1f4 Author: Piotr Pawel Stefaniak AuthorDate: 2023-04-30 16:30:56 +0000 Commit: Piotr Pawel Stefaniak CommitDate: 2023-09-12 16:39:54 +0000 sh: introduce a function to iterate over all hashed commands While aliases and built-ins are opportunistically stored in cmdtable, each function will be added to it immediately on definition. Factor out the hashing function, write the iterator function and make it use the hashing function. Add the cmdname pointer to struct cmdentry so that the command name can be exposed that way. Differential Revision: https://reviews.freebsd.org/D40619 --- bin/sh/exec.c | 46 +++++++++++++++++++++++++++++++++++++++------- bin/sh/exec.h | 2 ++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/bin/sh/exec.c b/bin/sh/exec.c index 07ed6d4173ae..a3df254a39dd 100644 --- a/bin/sh/exec.c +++ b/bin/sh/exec.c @@ -545,6 +545,19 @@ clearcmdentry(void) } +static unsigned int +hashname(const char *p) +{ + unsigned int hashval; + + hashval = (unsigned char)*p << 4; + while (*p) + hashval += *p++; + + return (hashval % CMDTABLESIZE); +} + + /* * Locate a command in the command hash table. If "add" is nonzero, * add the command to the table if it is not already present. The @@ -559,17 +572,11 @@ static struct tblentry **lastcmdentry; static struct tblentry * cmdlookup(const char *name, int add) { - unsigned int hashval; - const char *p; struct tblentry *cmdp; struct tblentry **pp; size_t len; - p = name; - hashval = (unsigned char)*p << 4; - while (*p) - hashval += *p++; - pp = &cmdtable[hashval % CMDTABLESIZE]; + pp = &cmdtable[hashname(name)]; for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { if (equal(cmdp->cmdname, name)) break; @@ -588,6 +595,31 @@ cmdlookup(const char *name, int add) return cmdp; } +const void * +itercmd(const void *entry, struct cmdentry *result) +{ + const struct tblentry *e = entry; + size_t i = 0; + + if (e != NULL) { + if (e->next != NULL) { + e = e->next; + goto success; + } + i = hashname(e->cmdname) + 1; + } + for (; i < CMDTABLESIZE; i++) + if ((e = cmdtable[i]) != NULL) + goto success; + + return (NULL); +success: + result->cmdtype = e->cmdtype; + result->cmdname = e->cmdname; + + return (e); +} + /* * Delete the command entry returned on the last lookup. */ diff --git a/bin/sh/exec.h b/bin/sh/exec.h index 04d1b97d7386..493be0178272 100644 --- a/bin/sh/exec.h +++ b/bin/sh/exec.h @@ -53,6 +53,7 @@ struct cmdentry { struct funcdef *func; } u; int special; + const char *cmdname; }; @@ -71,3 +72,4 @@ int unsetfunc(const char *); int isfunc(const char *); int typecmd_impl(int, char **, int, const char *); void clearcmdentry(void); +const void *itercmd(const void *, struct cmdentry *);