From nobody Tue Jan 24 22:13:12 2023 X-Original-To: dev-commits-src-branches@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 4P1h7F0Fw2z3bgNw; Tue, 24 Jan 2023 22:13: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 4P1h7D6LfGz3Fr9; Tue, 24 Jan 2023 22:13:12 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674598392; 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=4DgwyOynCSENFrnibDaGMi8kZTzYE+Q4zosK9TJuH+w=; b=ZkpZ6hvOiWo7BOmLP4t8FSFGru6LeVPD+z3ZNsZqztYQS+6HxKs5ic+rfad7448+uH8jq2 DgCvyPRvD42Zb+7yWBM1aTnArQgK2SBFdRgwOo6ngPcfZ4adfkChyqh8SGepRjoKK7pYB+ kkh66/iJpfKkMIG3pq0hBh7s/BAgdBWTpCabyGDu9hwJDGiRswncYPu1YgBMHABWjB+6dO zicl4xZC4QvyWP3bUNqljXP3PCneUeNWS5mT80AJBXOVxvDCMnVKNrQcngLFQ3bi8ARyTa mGvXwp6Tkoe6zdXMDP61P0RUcN7AVgZabqKJGJfIAlClO2tn4SBU7mHxHlSkiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674598392; 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=4DgwyOynCSENFrnibDaGMi8kZTzYE+Q4zosK9TJuH+w=; b=YoQX5g/Fs9dCxSFWZdlUzxV8Y+23oR8q+JB9OnX8JbeYsXMjh/vmAjs5gzZk70km41bcoX RWG17fYfLa3d+RdEMs2/l2LCYXQcJMSVHq1EKfQ2/QHMzvwYP6SKO6OMbW4FwSA8ZI20Bg lRuDLubJ04jdBNi5yK1Dl3HFIcP3IDVI4m6SwnXd9rn7nObs7WDYn1X4Q7LJoN9hG5SN5S 64CrDfPIzWKmLG6bYYwRb//vwAW2u328RwjwqRklKG6lvZI1CVaF9qY1TjQ8tAFzw14TXn QGoXVz7g6stp4AQ9oNH3/eNfQf8tmVwQC/SPQ7X0Fz1iif/Y9jdHYePretUd9g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674598392; a=rsa-sha256; cv=none; b=r+auvFkqXmbOcTZUaAfmWnep/gqWUGN/XAGASpAvV8JjxcD+IQnioX1MhqpeF5A3qLrLvx Z40sUFud87vxy8B0HRrTfJ8I9W8mOOFr7nMqne4I/MsnVpi7OQsgDUmm5Ku2ewGontqazL AFznxJLV3D6/V59p49yTNX3C0LbBSFCc0kZ4AAJXIq3c0lweEQO0Xd2Zx6tTqvavY/W9Cf XOT95k4ik7u5uoPo6MG5vIQnOdIu2pPa7DJD3WAM+lvwLNiS7bsbr1EYFyd2YdUEexJyUj rFceItyT9qz7NP9owFEQDdmtkhVMXqAJCx5RXgeR/88LAlLbMmrsMDvV1eXu5g== 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 4P1h7D1BMSzQYb; Tue, 24 Jan 2023 22:13:12 +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 30OMDCRc088094; Tue, 24 Jan 2023 22:13:12 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30OMDCSu088093; Tue, 24 Jan 2023 22:13:12 GMT (envelope-from git) Date: Tue, 24 Jan 2023 22:13:12 GMT Message-Id: <202301242213.30OMDCSu088093@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Warner Losh Subject: git: ac8d9f5c8968 - stable/13 - stand: Introduce devparse to parse device / path strings List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: ac8d9f5c8968a96cafb4f7ef0ad0d901633e901d Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=ac8d9f5c8968a96cafb4f7ef0ad0d901633e901d commit ac8d9f5c8968a96cafb4f7ef0ad0d901633e901d Author: Warner Losh AuthorDate: 2022-11-30 22:08:42 +0000 Commit: Warner Losh CommitDate: 2023-01-24 21:49:38 +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 (cherry picked from commit 781ca0afcd6c64ac3fbaf2a19b1880734cdfe947) --- 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 */