From: Mark Lord <kernel@teksavvy.com>
To: IDE/ATA development list <linux-ide@vger.kernel.org>,
Tejun Heo <htejun@gmail.com>, Jeff Garzik <jgarzik@pobox.com>
Subject: [PATCH 1/3] libata: glob_match for ata_device_blacklist
Date: Thu, 01 Jul 2010 11:30:31 -0400 [thread overview]
Message-ID: <4C2CB497.3000701@teksavvy.com> (raw)
Replace rudimentry pattern matching with more capable shell-style globbing.
This will enable shrinking ata_device_blacklist[] table in subsequent patches,
and helps with future editions to the table, such as matching only the end
of a firmware revision string etc..
Signed-off-by: Mark Lord <mlord@pobox.com>
---
Eventually, this function should move out of libata into lib/string.c or similar (?).
But since following patches in this series require it for libata,
let's start with it there, and move things later on.
--- 2.6.34/drivers/ata/libata-core.c 2010-05-16 17:17:36.000000000 -0400
+++ linux/drivers/ata/libata-core.c 2010-07-01 11:08:10.509159666 -0400
@@ -4413,29 +4413,65 @@
{ }
};
-static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
+/**
+ * glob_match - match a text string against a glob-style pattern
+ * @text: the string to be examined
+ * @pattern: the glob-style pattern to be matched against
+ *
+ * Either/both of text and pattern can be empty strings.
+ *
+ * Match text against a glob-style pattern, with wildcards and simple sets:
+ *
+ * ? matches any single character.
+ * * matches any run of characters.
+ * [xyz] matches a single character from the set: x, y, or z.
+ *
+ * Note: hyphenated ranges [0-9] are _not_ supported here.
+ * The special characters ?, [, or *, can be matched using a set, eg. [*]
+ *
+ * Example patterns: "SD1?", "SD1[012345]", "*R0", SD*1?[012]*xx"
+ *
+ * This function uses one level of recursion per '*' in pattern.
+ * Since it calls _nothing_ else, and has _no_ explicit local variables,
+ * this will not cause stack problems for any reasonable use here.
+ *
+ * RETURNS:
+ * 0 on match, 1 otherwise.
+ */
+static int glob_match (const char *text, const char *pattern)
{
- const char *p;
- int len;
+ do {
+ /* Match single character or a '?' wildcard */
+ if (*text == *pattern || *pattern == '?') {
+ if (!*pattern++)
+ return 0; /* End of both strings: match */
+ } else {
+ /* Match single char against a '[' bracketed ']' pattern set */
+ if (!*text || *pattern != '[')
+ break; /* Not a pattern set */
+ while (*++pattern && *pattern != ']' && *text != *pattern);
+ if (!*pattern || *pattern == ']')
+ return 1; /* No match */
+ while (*pattern && *pattern++ != ']');
+ }
+ } while (*++text && *pattern);
- /*
- * check for trailing wildcard: *\0
- */
- p = strchr(patt, wildchar);
- if (p && ((*(p + 1)) == 0))
- len = p - patt;
- else {
- len = strlen(name);
- if (!len) {
- if (!*patt)
- return 0;
- return -1;
+ /* Match any run of chars against a '*' wildcard */
+ if (*pattern == '*') {
+ if (!*++pattern)
+ return 0; /* Match: avoid recursion at end of pattern */
+ /* Loop to handle additional pattern chars after the wildcard */
+ while (*text) {
+ if (glob_match(text, pattern) == 0)
+ return 0; /* Remainder matched */
+ ++text; /* Absorb (match) this char and try again */
}
}
-
- return strncmp(patt, name, len);
+ if (!*text && !*pattern)
+ return 0; /* End of both strings: match */
+ return 1; /* No match */
}
-
+
static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
{
unsigned char model_num[ATA_ID_PROD_LEN + 1];
@@ -4446,10 +4482,10 @@
ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
while (ad->model_num) {
- if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
+ if (!glob_match(model_num, ad->model_num)) {
if (ad->model_rev == NULL)
return ad->horkage;
- if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
+ if (!glob_match(model_rev, ad->model_rev))
return ad->horkage;
}
ad++;
next reply other threads:[~2010-07-01 15:30 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-01 15:30 Mark Lord [this message]
2010-07-01 15:31 ` [PATCH 2/3] libata: reduce blacklist size Mark Lord
2010-07-01 15:31 ` [PATCH 3/3] libata: reduce blacklist size even more Mark Lord
2010-07-01 15:36 ` [PATCH 3/3] libata: reduce blacklist size (resend) Mark Lord
2010-07-01 15:34 ` [PATCH 1/3] libata: glob_match for ata_device_blacklist Mark Lord
2010-07-01 15:36 ` James Bottomley
2010-07-01 15:44 ` Mark Lord
2010-07-01 19:24 ` Jeff Garzik
2010-07-01 21:56 ` Mark Lord
2010-07-01 22:09 ` Mark Lord
2010-07-01 22:16 ` [PATCH 1/3] libata: glob_match for ata_device_blacklist (v2) Mark Lord
2010-07-01 22:17 ` [PATCH 2/3] libata: reduce blacklist size (v2) Mark Lord
2010-07-01 22:18 ` [PATCH 3/3] libata: reduce blacklist size even more (v2) Mark Lord
2010-07-02 7:19 ` Jeff Garzik
2010-07-05 14:25 ` [PATCH 4/4] libata: allow hyphenated pattern ranges Mark Lord
2010-07-05 18:33 ` Jeff Garzik
2010-07-05 21:47 ` Mark Lord
2010-07-05 22:20 ` Jeff Garzik
2010-07-05 22:39 ` Mark Lord
2010-07-05 22:50 ` [PATCH 5/5] " Mark Lord
2010-07-05 22:51 ` Mark Lord
2010-07-05 22:53 ` [PATCH 5/5] libata: allow hyphenated pattern ranges (v2) Mark Lord
2010-07-14 7:54 ` Jeff Garzik
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4C2CB497.3000701@teksavvy.com \
--to=kernel@teksavvy.com \
--cc=htejun@gmail.com \
--cc=jgarzik@pobox.com \
--cc=linux-ide@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).