From nobody Wed Nov 30 22:31:13 2022 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 4NMv7P6dykz4jCJt; Wed, 30 Nov 2022 22:31:13 +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 4NMv7P66dmz43pq; Wed, 30 Nov 2022 22:31:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1669847473; 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=3Uj+tmTok3khYlx2bqszOtcpQ5MUF3bAbxss/0rm65Q=; b=hanlEEfh257ggJDm2B0U0+/GBGM9VWWNVEwuVaHdhtJ8O8kUQIokgfIA9sNeLeDZb8AaWT pKmLWXnRzF2UCuUXZSLG19nABlzRrCLxPRz6F3SyrlN+rZl1/YrbkP5fdhGfNcRuFd4GiG DudJlxY5n9MvmqwO076YnJymjO+fzphDwk7KrYXEvQmAmsulTytJZwEmSZTEFSjxHDGfEz zQbEhmqzGwrUP7G1RVIRCEl3ztXwots28ac6pmMPDO0TBRgRLsBfMkRkhj8I/IK4i78L7t Mmmw2qJdcpCHEwkqnMQpFZKd5FUGgZCMHOXpi4YHffiwLKIvRNz3wHQvpJ2Rfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1669847473; 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=3Uj+tmTok3khYlx2bqszOtcpQ5MUF3bAbxss/0rm65Q=; b=GmdkaNO0PXpDSXh28bVRQ5G9V7jAAz80AMhyZcJwz6J7O2ESVP5+gEV7O/Xa1AlBnWrXR6 MFykn1KYwZq21HUjSWEJ68tDVryB7EPlDuvgPuyR7LkBArSJKMmhpj0Fv8pAS9i61i+wSh jR7ndNdwX+V5k6I0WnWH6O3gPZNgY+PbK55s9T1iAaVcxuk+4vxxqN7GwXDqpZanbNN+ML MVDtz3p0oCezOpY17tUOedX+Lz7tIKhR0DZG6z4L0IDHyGj5++T9nJ2XN9OFlwUtRkQMnF +P8AVa5TXmGsbbOeZHUDMIWKlsEccwN5rk/uw8WRmD+5jzHMI0nev0zBs0CNGQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1669847473; a=rsa-sha256; cv=none; b=CltfLDiltmgTx/XCsF6TOOOmwgQoAGxgYnuoNoRWdgJJsp7wr5TnJPskZsFX3c9552G9jK pFFAKhIX4+uXAIdaj8+skJ2M4nBCkqTNjnJ5BJ3hLpcpqOEf/L905Hyg9GVfm7oQnXVybr USdKw5hk6tf69kf6r/m0AEwp7XTVYW7ujDgOYVNppmENF6OHaRw24gSeXU9QaIeGPtpEx3 tsBFSARsZ+JK5BHj7HD9xogNdTXTfFtjN3rvInVOQDjESMtH0ZeKVG696jZ5/6pSYaiEv3 C5ss2+kW/KwXNrp9ocJNTwf7vnW778DV9db+TSQy6hfr8m5aHiVO/Na7eJ1Xag== 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 4NMv7P57CBzDdS; Wed, 30 Nov 2022 22:31:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2AUMVDMH023326; Wed, 30 Nov 2022 22:31:13 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2AUMVDlu023325; Wed, 30 Nov 2022 22:31:13 GMT (envelope-from git) Date: Wed, 30 Nov 2022 22:31:13 GMT Message-Id: <202211302231.2AUMVDlu023325@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: 781ca0afcd6c - main - stand: Introduce devparse to parse device / path strings 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: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 781ca0afcd6c64ac3fbaf2a19b1880734cdfe947 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=781ca0afcd6c64ac3fbaf2a19b1880734cdfe947 commit 781ca0afcd6c64ac3fbaf2a19b1880734cdfe947 Author: Warner Losh AuthorDate: 2022-11-30 22:08:42 +0000 Commit: Warner Losh CommitDate: 2022-11-30 22:30:31 +0000 stand: Introduce devparse to parse device / path strings devparse is now the preferred interface to use to parse device strings or device:/path strings. It parses the passed in string, mallocs the device's particular devdesc string and returns the 'remainder' of the device:/path for further processing. Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D37338 --- stand/libsa/dev.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ stand/libsa/libsa.3 | 38 +++++++++++++++++++++++-- stand/libsa/stand.h | 1 + 3 files changed, 119 insertions(+), 2 deletions(-) diff --git a/stand/libsa/dev.c b/stand/libsa/dev.c index 2ccdce22d596..c0bcce07718b 100644 --- a/stand/libsa/dev.c +++ b/stand/libsa/dev.c @@ -67,3 +67,85 @@ devformat(struct devdesc *d) snprintf(name, sizeof(name), "%s%d:", d->d_dev->dv_name, d->d_unit); return (name); } + +/* NB: devspec points to the remainder of the device name after dv_name */ +static int +default_parsedev(struct devdesc **dev, const char *devspec, + const char **path) +{ + struct devdesc *idev; + int unit, err; + char *cp; + + idev = malloc(sizeof(struct devdesc)); + if (idev == NULL) + return (ENOMEM); + + unit = 0; + cp = (char *)devspec; /* strtol interface, alas */ + + if (*devspec != '\0' && *devspec != ':') { + errno = 0; + unit = strtol(devspec, &cp, 0); + if (errno != 0 || cp == devspec) { + err = EUNIT; + goto fail; + } + } + if (*cp != '\0' && *cp != ':') { + err = EINVAL; + goto fail; + } + + idev->d_unit = unit; + if (path != NULL) + *path = (*cp == 0) ? cp : cp + 1; + if (dev != NULL) /* maybe this can be required? */ + *dev = idev; + else + free(idev); + return (0); +fail: + free(idev); + return (err); +} + +/* NB: devspec points to the whole device spec, and possible trailing path */ +int +devparse(struct devdesc **dev, const char *devspec, const char **path) +{ + struct devdesc *idev; + struct devsw *dv; + int i, err; + const char *np; + + /* minimum length check */ + if (strlen(devspec) < 2) + return (EINVAL); + + /* look for a device that matches */ + for (i = 0; devsw[i] != NULL; i++) { + dv = devsw[i]; + if (!strncmp(devspec, dv->dv_name, strlen(dv->dv_name))) + break; + } + if (devsw[i] == NULL) + return (ENOENT); + idev = NULL; + err = 0; + if (dv->dv_parsedev) { + err = dv->dv_parsedev(&idev, np, path); + } else { + np = devspec + strlen(dv->dv_name); + err = default_parsedev(&idev, np, path); + } + if (err != 0) + return (err); + + idev->d_dev = dv; + if (dev != NULL) + *dev = idev; + else + free(idev); + return (0); +} diff --git a/stand/libsa/libsa.3 b/stand/libsa/libsa.3 index f732595bc647..210f54a5aa6a 100644 --- a/stand/libsa/libsa.3 +++ b/stand/libsa/libsa.3 @@ -508,6 +508,35 @@ Returns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading. .Pp Format the specified device as a string. .It Xo +.Ft int +.Fn devparse "struct devdesc **dev" "const char *devdesc" "const char **path" +.Xc +.Pp +Parse the +.Dv devdesc +string of the form +.Sq device:[/path/to/file] . +The +.Dv devsw +table is used to match the start of the +.Sq device +string with +.Fa dv_name . +If +.Fa dv_parsedev +is non-NULL, then it will be called to parse the rest of the string and allocate +the +.Dv struct devdesc +for this path. +If NULL, then a default routine will be called that will allocate a simple +.Dv struct devdesc , +parse a unit number and ensure there's no trailing characters. +If +.Dv path +is non-NULL, then a pointer to the remainder of the +.Dv devdesc +string after the device specification is written. +.It Xo .Ft void .Fn twiddle void .Xc @@ -765,8 +794,10 @@ The is, by convention, the part of the device specification that follows the .Fa dv_name part of the string. -So when parsing the string -.Dq disk3p5:/xxx +So when +.Fa devparse +is parsing the string +.Dq disk3p5:/xxx , .Dv devpart will point to the .Sq 3 @@ -781,6 +812,9 @@ This routine should set to point to the portion of the string after device specification, or .Dq /xxx in the earlier example. +Generally, code needing to parse a path will use +.Fa devparse +instead of calling this routine directly. .El .Sh HISTORY The diff --git a/stand/libsa/stand.h b/stand/libsa/stand.h index 716486107b27..6e52325ec166 100644 --- a/stand/libsa/stand.h +++ b/stand/libsa/stand.h @@ -187,6 +187,7 @@ struct devdesc { }; char *devformat(struct devdesc *d); +int devparse(struct devdesc **, const char *, const char **); struct open_file { int f_flags; /* see F_* below */